platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config-util.h>
  19. #include <windows.h>
  20. #include <grub/util/install.h>
  21. #include <grub/util/misc.h>
  22. #include <grub/efi/api.h>
  23. #include <grub/charset.h>
  24. #include <grub/gpt_partition.h>
  25. #define GRUB_EFI_GLOBAL_VARIABLE_GUID_WINDOWS_STR L"{8be4df61-93ca-11d2-aa0d-00e098032b8c}"
  26. static enum { PLAT_UNK, PLAT_BIOS, PLAT_EFI } platform;
  27. static DWORD (WINAPI * func_GetFirmwareEnvironmentVariableW) (LPCWSTR lpName,
  28. LPCWSTR lpGuid,
  29. PVOID pBuffer,
  30. DWORD nSize);
  31. static BOOL (WINAPI * func_SetFirmwareEnvironmentVariableW) (LPCWSTR lpName,
  32. LPCWSTR lpGuid,
  33. PVOID pBuffer,
  34. DWORD nSize);
  35. static void (WINAPI * func_GetNativeSystemInfo) (LPSYSTEM_INFO lpSystemInfo);
  36. static int
  37. get_efi_privilegies (void)
  38. {
  39. int ret = 1;
  40. HANDLE hSelf;
  41. TOKEN_PRIVILEGES tkp;
  42. if (!OpenProcessToken (GetCurrentProcess(),
  43. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hSelf))
  44. return 0;
  45. LookupPrivilegeValue (NULL, SE_SYSTEM_ENVIRONMENT_NAME,
  46. &tkp.Privileges[0].Luid);
  47. tkp.PrivilegeCount = 1;
  48. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  49. if (!AdjustTokenPrivileges (hSelf, FALSE, &tkp, 0, NULL, 0))
  50. ret = 0;
  51. if (GetLastError () != ERROR_SUCCESS)
  52. ret = 0;
  53. CloseHandle (hSelf);
  54. return 1;
  55. }
  56. static void
  57. get_platform (void)
  58. {
  59. HMODULE kernel32;
  60. char buffer[256];
  61. if (platform != PLAT_UNK)
  62. return;
  63. kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  64. if (!kernel32)
  65. {
  66. platform = PLAT_BIOS;
  67. return;
  68. }
  69. func_GetFirmwareEnvironmentVariableW = (void *)
  70. GetProcAddress (kernel32, "GetFirmwareEnvironmentVariableW");
  71. func_SetFirmwareEnvironmentVariableW = (void *)
  72. GetProcAddress (kernel32, "SetFirmwareEnvironmentVariableW");
  73. func_GetNativeSystemInfo = (void *)
  74. GetProcAddress (kernel32, "GetNativeSystemInfo");
  75. if (!func_GetNativeSystemInfo)
  76. func_GetNativeSystemInfo = GetSystemInfo;
  77. if (!func_GetFirmwareEnvironmentVariableW
  78. || !func_SetFirmwareEnvironmentVariableW)
  79. {
  80. platform = PLAT_BIOS;
  81. return;
  82. }
  83. if (!get_efi_privilegies ())
  84. {
  85. grub_util_warn (_("Insufficient privileges to access firmware, assuming BIOS"));
  86. platform = PLAT_BIOS;
  87. }
  88. if (!func_GetFirmwareEnvironmentVariableW (L"BootOrder", GRUB_EFI_GLOBAL_VARIABLE_GUID_WINDOWS_STR,
  89. buffer, sizeof (buffer))
  90. && GetLastError () == ERROR_INVALID_FUNCTION)
  91. {
  92. platform = PLAT_BIOS;
  93. return;
  94. }
  95. platform = PLAT_EFI;
  96. return;
  97. }
  98. const char *
  99. grub_install_get_default_x86_platform (void)
  100. {
  101. SYSTEM_INFO si;
  102. get_platform ();
  103. if (platform != PLAT_EFI)
  104. return "i386-pc";
  105. /* EFI */
  106. /* Assume 64-bit in case of failure. */
  107. si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64;
  108. func_GetNativeSystemInfo (&si);
  109. if (si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL)
  110. return "x86_64-efi";
  111. else
  112. return "i386-efi";
  113. }
  114. static void *
  115. get_efi_variable (const wchar_t *varname, ssize_t *len)
  116. {
  117. void *ret = NULL;
  118. size_t alloc_size = 256, read_size;
  119. get_platform ();
  120. while (1)
  121. {
  122. DWORD err;
  123. ret = xmalloc (alloc_size);
  124. read_size = func_GetFirmwareEnvironmentVariableW (varname, GRUB_EFI_GLOBAL_VARIABLE_GUID_WINDOWS_STR,
  125. ret, alloc_size);
  126. err = GetLastError ();
  127. if (read_size)
  128. {
  129. *len = read_size;
  130. return ret;
  131. }
  132. if (err == ERROR_INSUFFICIENT_BUFFER
  133. && alloc_size * 2 != 0)
  134. {
  135. alloc_size *= 2;
  136. free (ret);
  137. continue;
  138. }
  139. if (err == ERROR_ENVVAR_NOT_FOUND)
  140. {
  141. *len = -1;
  142. return NULL;
  143. }
  144. *len = -2;
  145. return NULL;
  146. }
  147. }
  148. static void
  149. set_efi_variable (const wchar_t *varname, void *in, grub_size_t len)
  150. {
  151. get_platform ();
  152. func_SetFirmwareEnvironmentVariableW (varname, GRUB_EFI_GLOBAL_VARIABLE_GUID_WINDOWS_STR,
  153. in, len);
  154. }
  155. static char
  156. bin2hex (int v)
  157. {
  158. if (v < 10)
  159. return '0' + v;
  160. return 'A' + v - 10;
  161. }
  162. static void *
  163. get_efi_variable_bootn (grub_uint16_t n, ssize_t *len)
  164. {
  165. wchar_t varname[20] = L"Boot0000";
  166. varname[7] = bin2hex (n & 0xf);
  167. varname[6] = bin2hex ((n >> 4) & 0xf);
  168. varname[5] = bin2hex ((n >> 8) & 0xf);
  169. varname[4] = bin2hex ((n >> 12) & 0xf);
  170. return get_efi_variable (varname, len);
  171. }
  172. static void
  173. set_efi_variable_bootn (grub_uint16_t n, void *in, grub_size_t len)
  174. {
  175. wchar_t varname[20] = L"Boot0000";
  176. varname[7] = bin2hex (n & 0xf);
  177. varname[6] = bin2hex ((n >> 4) & 0xf);
  178. varname[5] = bin2hex ((n >> 8) & 0xf);
  179. varname[4] = bin2hex ((n >> 12) & 0xf);
  180. set_efi_variable (varname, in, len);
  181. }
  182. int
  183. grub_install_register_efi (grub_device_t efidir_grub_dev,
  184. const char *efifile_path,
  185. const char *efi_distributor)
  186. {
  187. grub_uint16_t *boot_order, *new_boot_order;
  188. grub_uint16_t *distributor16;
  189. grub_uint8_t *entry;
  190. grub_size_t distrib8_len, distrib16_len, path16_len, path8_len;
  191. ssize_t boot_order_len, new_boot_order_len;
  192. grub_uint16_t order_num = 0;
  193. int have_order_num = 0;
  194. grub_size_t max_path_length;
  195. grub_uint8_t *path;
  196. void *pathptr;
  197. struct grub_efi_hard_drive_device_path *hddp;
  198. struct grub_efi_file_path_device_path *filep;
  199. struct grub_efi_device_path *endp;
  200. get_platform ();
  201. if (platform != PLAT_EFI)
  202. grub_util_error ("%s", _("no EFI routines are available when running in BIOS mode"));
  203. distrib8_len = grub_strlen (efi_distributor);
  204. distributor16 = xcalloc (distrib8_len + 1,
  205. GRUB_MAX_UTF16_PER_UTF8 * sizeof (grub_uint16_t));
  206. distrib16_len = grub_utf8_to_utf16 (distributor16, distrib8_len * GRUB_MAX_UTF16_PER_UTF8,
  207. (const grub_uint8_t *) efi_distributor,
  208. distrib8_len, 0);
  209. distributor16[distrib16_len] = 0;
  210. /* Windows doesn't allow to list variables so first look for bootorder to
  211. find if there is an entry from the same distributor. If not try sequentially
  212. until we find same distributor or empty spot. */
  213. boot_order = get_efi_variable (L"BootOrder", &boot_order_len);
  214. if (boot_order_len < -1)
  215. grub_util_error ("%s", _("unexpected EFI error"));
  216. if (boot_order_len > 0)
  217. {
  218. size_t i;
  219. for (i = 0; i < boot_order_len / 2; i++)
  220. {
  221. void *current = NULL;
  222. ssize_t current_len;
  223. current = get_efi_variable_bootn (i, &current_len);
  224. if (current_len < 0)
  225. continue; /* FIXME Should we abort on error? */
  226. if (current_len < (distrib16_len + 1) * sizeof (grub_uint16_t)
  227. + 6)
  228. {
  229. grub_free (current);
  230. continue;
  231. }
  232. if (grub_memcmp ((grub_uint16_t *) current + 3,
  233. distributor16,
  234. (distrib16_len + 1) * sizeof (grub_uint16_t)) != 0)
  235. {
  236. grub_free (current);
  237. continue;
  238. }
  239. order_num = i;
  240. have_order_num = 1;
  241. grub_util_info ("Found matching distributor at Boot%04x",
  242. order_num);
  243. grub_free (current);
  244. break;
  245. }
  246. }
  247. if (!have_order_num)
  248. {
  249. size_t i;
  250. for (i = 0; i < 0x10000; i++)
  251. {
  252. void *current = NULL;
  253. ssize_t current_len;
  254. current = get_efi_variable_bootn (i, &current_len);
  255. if (current_len < -1)
  256. continue; /* FIXME Should we abort on error? */
  257. if (current_len == -1)
  258. {
  259. if (!have_order_num)
  260. {
  261. order_num = i;
  262. have_order_num = 1;
  263. grub_util_info ("Creating new entry at Boot%04x",
  264. order_num);
  265. }
  266. continue;
  267. }
  268. if (current_len < (distrib16_len + 1) * sizeof (grub_uint16_t)
  269. + 6)
  270. {
  271. grub_free (current);
  272. continue;
  273. }
  274. if (grub_memcmp ((grub_uint16_t *) current + 3,
  275. distributor16,
  276. (distrib16_len + 1) * sizeof (grub_uint16_t)) != 0)
  277. {
  278. grub_free (current);
  279. continue;
  280. }
  281. order_num = i;
  282. have_order_num = 1;
  283. grub_util_info ("Found matching distributor at Boot%04x",
  284. order_num);
  285. grub_free (current);
  286. break;
  287. }
  288. }
  289. if (!have_order_num)
  290. grub_util_error ("%s", _("Couldn't find a free BootNNNN slot"));
  291. path8_len = grub_strlen (efifile_path);
  292. max_path_length = sizeof (*hddp) + sizeof (*filep) + (path8_len * GRUB_MAX_UTF16_PER_UTF8 + 1) * sizeof (grub_uint16_t) + sizeof (*endp);
  293. entry = xmalloc (6 + (distrib16_len + 1) * sizeof (grub_uint16_t) + max_path_length);
  294. /* attributes: active. */
  295. entry[0] = 1;
  296. entry[1] = 0;
  297. entry[2] = 0;
  298. entry[3] = 0;
  299. grub_memcpy (entry + 6,
  300. distributor16,
  301. (distrib16_len + 1) * sizeof (grub_uint16_t));
  302. path = entry + 6 + (distrib16_len + 1) * sizeof (grub_uint16_t);
  303. pathptr = path;
  304. hddp = pathptr;
  305. grub_memset (hddp, 0, sizeof (*hddp));
  306. hddp->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
  307. hddp->header.subtype = GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE;
  308. hddp->header.length = sizeof (*hddp);
  309. hddp->partition_number = efidir_grub_dev->disk->partition ? efidir_grub_dev->disk->partition->number + 1 : 1;
  310. if (efidir_grub_dev->disk->partition
  311. && grub_strcmp (efidir_grub_dev->disk->partition->partmap->name, "msdos") == 0)
  312. {
  313. grub_partition_t p;
  314. p = efidir_grub_dev->disk->partition;
  315. efidir_grub_dev->disk->partition = p->parent;
  316. if (grub_disk_read (efidir_grub_dev->disk, 0, 440,
  317. 4, hddp->partition_signature))
  318. grub_util_error ("%s", grub_errmsg);
  319. efidir_grub_dev->disk->partition = p;
  320. hddp->partmap_type = 1;
  321. hddp->signature_type = 1;
  322. }
  323. else if (efidir_grub_dev->disk->partition
  324. && grub_strcmp (efidir_grub_dev->disk->partition->partmap->name, "gpt") == 0)
  325. {
  326. struct grub_gpt_partentry gptdata;
  327. grub_partition_t p;
  328. p = efidir_grub_dev->disk->partition;
  329. efidir_grub_dev->disk->partition = p->parent;
  330. if (grub_disk_read (efidir_grub_dev->disk,
  331. p->offset, p->index,
  332. sizeof (gptdata), &gptdata))
  333. grub_util_error ("%s", grub_errmsg);
  334. efidir_grub_dev->disk->partition = p;
  335. grub_memcpy (hddp->partition_signature,
  336. &gptdata.guid, 16);
  337. hddp->partmap_type = 2;
  338. hddp->signature_type = 2;
  339. }
  340. hddp->partition_start = grub_partition_get_start (efidir_grub_dev->disk->partition)
  341. >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
  342. hddp->partition_size = grub_disk_native_sectors (efidir_grub_dev->disk)
  343. >> (efidir_grub_dev->disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
  344. pathptr = hddp + 1;
  345. filep = pathptr;
  346. filep->header.type = GRUB_EFI_MEDIA_DEVICE_PATH_TYPE;
  347. filep->header.subtype = GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE;
  348. #if __GNUC__ >= 9
  349. #pragma GCC diagnostic push
  350. #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
  351. #endif
  352. path16_len = grub_utf8_to_utf16 (filep->path_name,
  353. path8_len * GRUB_MAX_UTF16_PER_UTF8,
  354. (const grub_uint8_t *) efifile_path,
  355. path8_len, 0);
  356. #if __GNUC__ >= 9
  357. #pragma GCC diagnostic pop
  358. #endif
  359. filep->path_name[path16_len] = 0;
  360. filep->header.length = sizeof (*filep) + (path16_len + 1) * sizeof (grub_uint16_t);
  361. pathptr = &filep->path_name[path16_len + 1];
  362. endp = pathptr;
  363. endp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
  364. endp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
  365. endp->length = sizeof (*endp);
  366. pathptr = endp + 1;
  367. entry[4] = (grub_uint8_t *) pathptr - path;
  368. entry[5] = ((grub_uint8_t *) pathptr - path) >> 8;
  369. new_boot_order = xmalloc ((boot_order_len > 0 ? boot_order_len : 0) + 2);
  370. new_boot_order[0] = order_num;
  371. new_boot_order_len = 1;
  372. {
  373. ssize_t i;
  374. for (i = 0; i < boot_order_len / 2; i++)
  375. if (boot_order[i] != order_num)
  376. new_boot_order[new_boot_order_len++] = boot_order[i];
  377. }
  378. set_efi_variable_bootn (order_num, entry, (grub_uint8_t *) pathptr - entry);
  379. set_efi_variable (L"BootOrder", new_boot_order, new_boot_order_len * sizeof (grub_uint16_t));
  380. return 0;
  381. }
  382. void
  383. grub_install_register_ieee1275 (int is_prep, const char *install_device,
  384. int partno, const char *relpath)
  385. {
  386. grub_util_error ("%s", _("no IEEE1275 routines are available for your platform"));
  387. }
  388. void
  389. grub_install_sgi_setup (const char *install_device,
  390. const char *imgfile, const char *destname)
  391. {
  392. grub_util_error ("%s", _("no SGI routines are available for your platform"));
  393. }