efi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /* efi.c - generic EFI support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/misc.h>
  20. #include <grub/charset.h>
  21. #include <grub/efi/api.h>
  22. #include <grub/efi/efi.h>
  23. #include <grub/efi/console_control.h>
  24. #include <grub/efi/pe32.h>
  25. #include <grub/time.h>
  26. #include <grub/term.h>
  27. #include <grub/kernel.h>
  28. #include <grub/mm.h>
  29. #include <grub/loader.h>
  30. /* The handle of GRUB itself. Filled in by the startup code. */
  31. grub_efi_handle_t grub_efi_image_handle;
  32. /* The pointer to a system table. Filled in by the startup code. */
  33. grub_efi_system_table_t *grub_efi_system_table;
  34. static grub_efi_guid_t console_control_guid = GRUB_EFI_CONSOLE_CONTROL_GUID;
  35. static grub_efi_guid_t loaded_image_guid = GRUB_EFI_LOADED_IMAGE_GUID;
  36. static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID;
  37. void *
  38. grub_efi_locate_protocol (grub_efi_guid_t *protocol, void *registration)
  39. {
  40. void *interface;
  41. grub_efi_status_t status;
  42. status = efi_call_3 (grub_efi_system_table->boot_services->locate_protocol,
  43. protocol, registration, &interface);
  44. if (status != GRUB_EFI_SUCCESS)
  45. return 0;
  46. return interface;
  47. }
  48. /* Return the array of handles which meet the requirement. If successful,
  49. the number of handles is stored in NUM_HANDLES. The array is allocated
  50. from the heap. */
  51. grub_efi_handle_t *
  52. grub_efi_locate_handle (grub_efi_locate_search_type_t search_type,
  53. grub_efi_guid_t *protocol,
  54. void *search_key,
  55. grub_efi_uintn_t *num_handles)
  56. {
  57. grub_efi_boot_services_t *b;
  58. grub_efi_status_t status;
  59. grub_efi_handle_t *buffer;
  60. grub_efi_uintn_t buffer_size = 8 * sizeof (grub_efi_handle_t);
  61. buffer = grub_malloc (buffer_size);
  62. if (! buffer)
  63. return 0;
  64. b = grub_efi_system_table->boot_services;
  65. status = efi_call_5 (b->locate_handle, search_type, protocol, search_key,
  66. &buffer_size, buffer);
  67. if (status == GRUB_EFI_BUFFER_TOO_SMALL)
  68. {
  69. grub_free (buffer);
  70. buffer = grub_malloc (buffer_size);
  71. if (! buffer)
  72. return 0;
  73. status = efi_call_5 (b->locate_handle, search_type, protocol, search_key,
  74. &buffer_size, buffer);
  75. }
  76. if (status != GRUB_EFI_SUCCESS)
  77. {
  78. grub_free (buffer);
  79. return 0;
  80. }
  81. *num_handles = buffer_size / sizeof (grub_efi_handle_t);
  82. return buffer;
  83. }
  84. void *
  85. grub_efi_open_protocol (grub_efi_handle_t handle,
  86. grub_efi_guid_t *protocol,
  87. grub_efi_uint32_t attributes)
  88. {
  89. grub_efi_boot_services_t *b;
  90. grub_efi_status_t status;
  91. void *interface;
  92. b = grub_efi_system_table->boot_services;
  93. status = efi_call_6 (b->open_protocol, handle,
  94. protocol,
  95. &interface,
  96. grub_efi_image_handle,
  97. 0,
  98. attributes);
  99. if (status != GRUB_EFI_SUCCESS)
  100. return 0;
  101. return interface;
  102. }
  103. int
  104. grub_efi_set_text_mode (int on)
  105. {
  106. grub_efi_console_control_protocol_t *c;
  107. grub_efi_screen_mode_t mode, new_mode;
  108. c = grub_efi_locate_protocol (&console_control_guid, 0);
  109. if (! c)
  110. /* No console control protocol instance available, assume it is
  111. already in text mode. */
  112. return 1;
  113. if (efi_call_4 (c->get_mode, c, &mode, 0, 0) != GRUB_EFI_SUCCESS)
  114. return 0;
  115. new_mode = on ? GRUB_EFI_SCREEN_TEXT : GRUB_EFI_SCREEN_GRAPHICS;
  116. if (mode != new_mode)
  117. if (efi_call_2 (c->set_mode, c, new_mode) != GRUB_EFI_SUCCESS)
  118. return 0;
  119. return 1;
  120. }
  121. void
  122. grub_efi_stall (grub_efi_uintn_t microseconds)
  123. {
  124. efi_call_1 (grub_efi_system_table->boot_services->stall, microseconds);
  125. }
  126. grub_efi_loaded_image_t *
  127. grub_efi_get_loaded_image (grub_efi_handle_t image_handle)
  128. {
  129. return grub_efi_open_protocol (image_handle,
  130. &loaded_image_guid,
  131. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  132. }
  133. void
  134. grub_reboot (void)
  135. {
  136. grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
  137. efi_call_4 (grub_efi_system_table->runtime_services->reset_system,
  138. GRUB_EFI_RESET_COLD, GRUB_EFI_SUCCESS, 0, NULL);
  139. for (;;) ;
  140. }
  141. void
  142. grub_exit (void)
  143. {
  144. grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
  145. efi_call_4 (grub_efi_system_table->boot_services->exit,
  146. grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
  147. for (;;) ;
  148. }
  149. grub_err_t
  150. grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
  151. grub_efi_uintn_t descriptor_size,
  152. grub_efi_uint32_t descriptor_version,
  153. grub_efi_memory_descriptor_t *virtual_map)
  154. {
  155. grub_efi_runtime_services_t *r;
  156. grub_efi_status_t status;
  157. r = grub_efi_system_table->runtime_services;
  158. status = efi_call_4 (r->set_virtual_address_map, memory_map_size,
  159. descriptor_size, descriptor_version, virtual_map);
  160. if (status == GRUB_EFI_SUCCESS)
  161. return GRUB_ERR_NONE;
  162. return grub_error (GRUB_ERR_IO, "set_virtual_address_map failed");
  163. }
  164. grub_err_t
  165. grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
  166. void *data, grub_size_t datasize)
  167. {
  168. grub_efi_status_t status;
  169. grub_efi_runtime_services_t *r;
  170. grub_efi_char16_t *var16;
  171. grub_size_t len, len16;
  172. len = grub_strlen (var);
  173. len16 = len * GRUB_MAX_UTF16_PER_UTF8;
  174. var16 = grub_malloc ((len16 + 1) * sizeof (var16[0]));
  175. if (!var16)
  176. return grub_errno;
  177. len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
  178. var16[len16] = 0;
  179. r = grub_efi_system_table->runtime_services;
  180. status = efi_call_5 (r->set_variable, var16, guid,
  181. (GRUB_EFI_VARIABLE_NON_VOLATILE
  182. | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
  183. | GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
  184. datasize, data);
  185. grub_free (var16);
  186. if (status == GRUB_EFI_SUCCESS)
  187. return GRUB_ERR_NONE;
  188. return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
  189. }
  190. void *
  191. grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
  192. grub_size_t *datasize_out)
  193. {
  194. grub_efi_status_t status;
  195. grub_efi_uintn_t datasize = 0;
  196. grub_efi_runtime_services_t *r;
  197. grub_efi_char16_t *var16;
  198. void *data;
  199. grub_size_t len, len16;
  200. *datasize_out = 0;
  201. len = grub_strlen (var);
  202. len16 = len * GRUB_MAX_UTF16_PER_UTF8;
  203. var16 = grub_malloc ((len16 + 1) * sizeof (var16[0]));
  204. if (!var16)
  205. return NULL;
  206. len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
  207. var16[len16] = 0;
  208. r = grub_efi_system_table->runtime_services;
  209. status = efi_call_5 (r->get_variable, var16, guid, NULL, &datasize, NULL);
  210. if (status != GRUB_EFI_BUFFER_TOO_SMALL || !datasize)
  211. {
  212. grub_free (var16);
  213. return NULL;
  214. }
  215. data = grub_malloc (datasize);
  216. if (!data)
  217. {
  218. grub_free (var16);
  219. return NULL;
  220. }
  221. status = efi_call_5 (r->get_variable, var16, guid, NULL, &datasize, data);
  222. grub_free (var16);
  223. if (status == GRUB_EFI_SUCCESS)
  224. {
  225. *datasize_out = datasize;
  226. return data;
  227. }
  228. grub_free (data);
  229. return NULL;
  230. }
  231. #pragma GCC diagnostic ignored "-Wcast-align"
  232. /* Search the mods section from the PE32/PE32+ image. This code uses
  233. a PE32 header, but should work with PE32+ as well. */
  234. grub_addr_t
  235. grub_efi_modules_addr (void)
  236. {
  237. grub_efi_loaded_image_t *image;
  238. struct grub_pe32_header *header;
  239. struct grub_pe32_coff_header *coff_header;
  240. struct grub_pe32_section_table *sections;
  241. struct grub_pe32_section_table *section;
  242. struct grub_module_info *info;
  243. grub_uint16_t i;
  244. image = grub_efi_get_loaded_image (grub_efi_image_handle);
  245. if (! image)
  246. return 0;
  247. header = image->image_base;
  248. coff_header = &(header->coff_header);
  249. sections
  250. = (struct grub_pe32_section_table *) ((char *) coff_header
  251. + sizeof (*coff_header)
  252. + coff_header->optional_header_size);
  253. for (i = 0, section = sections;
  254. i < coff_header->num_sections;
  255. i++, section++)
  256. {
  257. if (grub_strcmp (section->name, "mods") == 0)
  258. break;
  259. }
  260. if (i == coff_header->num_sections)
  261. return 0;
  262. info = (struct grub_module_info *) ((char *) image->image_base
  263. + section->virtual_address);
  264. if (info->magic != GRUB_MODULE_MAGIC)
  265. return 0;
  266. return (grub_addr_t) info;
  267. }
  268. #pragma GCC diagnostic error "-Wcast-align"
  269. char *
  270. grub_efi_get_filename (grub_efi_device_path_t *dp0)
  271. {
  272. char *name = 0, *p, *pi;
  273. grub_size_t filesize = 0;
  274. grub_efi_device_path_t *dp;
  275. if (!dp0)
  276. return NULL;
  277. dp = dp0;
  278. while (1)
  279. {
  280. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  281. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  282. if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
  283. break;
  284. if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
  285. && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
  286. {
  287. grub_efi_uint16_t len;
  288. len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
  289. / sizeof (grub_efi_char16_t));
  290. filesize += GRUB_MAX_UTF8_PER_UTF16 * len + 2;
  291. }
  292. dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
  293. }
  294. if (!filesize)
  295. return NULL;
  296. dp = dp0;
  297. p = name = grub_malloc (filesize);
  298. if (!name)
  299. return NULL;
  300. while (1)
  301. {
  302. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  303. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  304. if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
  305. break;
  306. else if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
  307. && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
  308. {
  309. grub_efi_file_path_device_path_t *fp;
  310. grub_efi_uint16_t len;
  311. grub_efi_char16_t *dup_name;
  312. *p++ = '/';
  313. len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
  314. / sizeof (grub_efi_char16_t));
  315. fp = (grub_efi_file_path_device_path_t *) dp;
  316. /* According to EFI spec Path Name is NULL terminated */
  317. while (len > 0 && fp->path_name[len - 1] == 0)
  318. len--;
  319. dup_name = grub_malloc (len * sizeof (*dup_name));
  320. if (!dup_name)
  321. {
  322. grub_free (name);
  323. return NULL;
  324. }
  325. p = (char *) grub_utf16_to_utf8 ((unsigned char *) p,
  326. grub_memcpy (dup_name, fp->path_name, len * sizeof (*dup_name)),
  327. len);
  328. grub_free (dup_name);
  329. }
  330. dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
  331. }
  332. *p = '\0';
  333. for (pi = name, p = name; *pi;)
  334. {
  335. /* EFI breaks paths with backslashes. */
  336. if (*pi == '\\' || *pi == '/')
  337. {
  338. *p++ = '/';
  339. while (*pi == '\\' || *pi == '/')
  340. pi++;
  341. continue;
  342. }
  343. *p++ = *pi++;
  344. }
  345. *p = '\0';
  346. return name;
  347. }
  348. grub_efi_device_path_t *
  349. grub_efi_get_device_path (grub_efi_handle_t handle)
  350. {
  351. return grub_efi_open_protocol (handle, &device_path_guid,
  352. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  353. }
  354. /* Return the device path node right before the end node. */
  355. grub_efi_device_path_t *
  356. grub_efi_find_last_device_path (const grub_efi_device_path_t *dp)
  357. {
  358. grub_efi_device_path_t *next, *p;
  359. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  360. return 0;
  361. for (p = (grub_efi_device_path_t *) dp, next = GRUB_EFI_NEXT_DEVICE_PATH (p);
  362. ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (next);
  363. p = next, next = GRUB_EFI_NEXT_DEVICE_PATH (next))
  364. ;
  365. return p;
  366. }
  367. /* Duplicate a device path. */
  368. grub_efi_device_path_t *
  369. grub_efi_duplicate_device_path (const grub_efi_device_path_t *dp)
  370. {
  371. grub_efi_device_path_t *p;
  372. grub_size_t total_size = 0;
  373. for (p = (grub_efi_device_path_t *) dp;
  374. ;
  375. p = GRUB_EFI_NEXT_DEVICE_PATH (p))
  376. {
  377. total_size += GRUB_EFI_DEVICE_PATH_LENGTH (p);
  378. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
  379. break;
  380. }
  381. p = grub_malloc (total_size);
  382. if (! p)
  383. return 0;
  384. grub_memcpy (p, dp, total_size);
  385. return p;
  386. }
  387. static void
  388. dump_vendor_path (const char *type, grub_efi_vendor_device_path_t *vendor)
  389. {
  390. grub_uint32_t vendor_data_len = vendor->header.length - sizeof (*vendor);
  391. grub_printf ("/%sVendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)[%x: ",
  392. type,
  393. (unsigned) vendor->vendor_guid.data1,
  394. (unsigned) vendor->vendor_guid.data2,
  395. (unsigned) vendor->vendor_guid.data3,
  396. (unsigned) vendor->vendor_guid.data4[0],
  397. (unsigned) vendor->vendor_guid.data4[1],
  398. (unsigned) vendor->vendor_guid.data4[2],
  399. (unsigned) vendor->vendor_guid.data4[3],
  400. (unsigned) vendor->vendor_guid.data4[4],
  401. (unsigned) vendor->vendor_guid.data4[5],
  402. (unsigned) vendor->vendor_guid.data4[6],
  403. (unsigned) vendor->vendor_guid.data4[7],
  404. vendor_data_len);
  405. if (vendor->header.length > sizeof (*vendor))
  406. {
  407. grub_uint32_t i;
  408. for (i = 0; i < vendor_data_len; i++)
  409. grub_printf ("%02x ", vendor->vendor_defined_data[i]);
  410. }
  411. grub_printf ("]");
  412. }
  413. /* Print the chain of Device Path nodes. This is mainly for debugging. */
  414. void
  415. grub_efi_print_device_path (grub_efi_device_path_t *dp)
  416. {
  417. while (1)
  418. {
  419. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  420. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  421. grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
  422. switch (type)
  423. {
  424. case GRUB_EFI_END_DEVICE_PATH_TYPE:
  425. switch (subtype)
  426. {
  427. case GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE:
  428. grub_printf ("/EndEntire\n");
  429. //grub_putchar ('\n');
  430. break;
  431. case GRUB_EFI_END_THIS_DEVICE_PATH_SUBTYPE:
  432. grub_printf ("/EndThis\n");
  433. //grub_putchar ('\n');
  434. break;
  435. default:
  436. grub_printf ("/EndUnknown(%x)\n", (unsigned) subtype);
  437. break;
  438. }
  439. break;
  440. case GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE:
  441. switch (subtype)
  442. {
  443. case GRUB_EFI_PCI_DEVICE_PATH_SUBTYPE:
  444. {
  445. grub_efi_pci_device_path_t *pci
  446. = (grub_efi_pci_device_path_t *) dp;
  447. grub_printf ("/PCI(%x,%x)",
  448. (unsigned) pci->function, (unsigned) pci->device);
  449. }
  450. break;
  451. case GRUB_EFI_PCCARD_DEVICE_PATH_SUBTYPE:
  452. {
  453. grub_efi_pccard_device_path_t *pccard
  454. = (grub_efi_pccard_device_path_t *) dp;
  455. grub_printf ("/PCCARD(%x)",
  456. (unsigned) pccard->function);
  457. }
  458. break;
  459. case GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE:
  460. {
  461. grub_efi_memory_mapped_device_path_t *mmapped
  462. = (grub_efi_memory_mapped_device_path_t *) dp;
  463. grub_printf ("/MMap(%x,%llx,%llx)",
  464. (unsigned) mmapped->memory_type,
  465. (unsigned long long) mmapped->start_address,
  466. (unsigned long long) mmapped->end_address);
  467. }
  468. break;
  469. case GRUB_EFI_VENDOR_DEVICE_PATH_SUBTYPE:
  470. dump_vendor_path ("Hardware",
  471. (grub_efi_vendor_device_path_t *) dp);
  472. break;
  473. case GRUB_EFI_CONTROLLER_DEVICE_PATH_SUBTYPE:
  474. {
  475. grub_efi_controller_device_path_t *controller
  476. = (grub_efi_controller_device_path_t *) dp;
  477. grub_printf ("/Ctrl(%x)",
  478. (unsigned) controller->controller_number);
  479. }
  480. break;
  481. default:
  482. grub_printf ("/UnknownHW(%x)", (unsigned) subtype);
  483. break;
  484. }
  485. break;
  486. case GRUB_EFI_ACPI_DEVICE_PATH_TYPE:
  487. switch (subtype)
  488. {
  489. case GRUB_EFI_ACPI_DEVICE_PATH_SUBTYPE:
  490. {
  491. grub_efi_acpi_device_path_t *acpi
  492. = (grub_efi_acpi_device_path_t *) dp;
  493. grub_printf ("/ACPI(%x,%x)",
  494. (unsigned) acpi->hid,
  495. (unsigned) acpi->uid);
  496. }
  497. break;
  498. case GRUB_EFI_EXPANDED_ACPI_DEVICE_PATH_SUBTYPE:
  499. {
  500. grub_efi_expanded_acpi_device_path_t *eacpi
  501. = (grub_efi_expanded_acpi_device_path_t *) dp;
  502. grub_printf ("/ACPI(");
  503. if (GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp)[0] == '\0')
  504. grub_printf ("%x,", (unsigned) eacpi->hid);
  505. else
  506. grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp));
  507. if (GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp)[0] == '\0')
  508. grub_printf ("%x,", (unsigned) eacpi->uid);
  509. else
  510. grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp));
  511. if (GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp)[0] == '\0')
  512. grub_printf ("%x)", (unsigned) eacpi->cid);
  513. else
  514. grub_printf ("%s)", GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp));
  515. }
  516. break;
  517. default:
  518. grub_printf ("/UnknownACPI(%x)", (unsigned) subtype);
  519. break;
  520. }
  521. break;
  522. case GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE:
  523. switch (subtype)
  524. {
  525. case GRUB_EFI_ATAPI_DEVICE_PATH_SUBTYPE:
  526. {
  527. grub_efi_atapi_device_path_t *atapi
  528. = (grub_efi_atapi_device_path_t *) dp;
  529. grub_printf ("/ATAPI(%x,%x,%x)",
  530. (unsigned) atapi->primary_secondary,
  531. (unsigned) atapi->slave_master,
  532. (unsigned) atapi->lun);
  533. }
  534. break;
  535. case GRUB_EFI_SCSI_DEVICE_PATH_SUBTYPE:
  536. {
  537. grub_efi_scsi_device_path_t *scsi
  538. = (grub_efi_scsi_device_path_t *) dp;
  539. grub_printf ("/SCSI(%x,%x)",
  540. (unsigned) scsi->pun,
  541. (unsigned) scsi->lun);
  542. }
  543. break;
  544. case GRUB_EFI_FIBRE_CHANNEL_DEVICE_PATH_SUBTYPE:
  545. {
  546. grub_efi_fibre_channel_device_path_t *fc
  547. = (grub_efi_fibre_channel_device_path_t *) dp;
  548. grub_printf ("/FibreChannel(%llx,%llx)",
  549. (unsigned long long) fc->wwn,
  550. (unsigned long long) fc->lun);
  551. }
  552. break;
  553. case GRUB_EFI_1394_DEVICE_PATH_SUBTYPE:
  554. {
  555. grub_efi_1394_device_path_t *firewire
  556. = (grub_efi_1394_device_path_t *) dp;
  557. grub_printf ("/1394(%llx)",
  558. (unsigned long long) firewire->guid);
  559. }
  560. break;
  561. case GRUB_EFI_USB_DEVICE_PATH_SUBTYPE:
  562. {
  563. grub_efi_usb_device_path_t *usb
  564. = (grub_efi_usb_device_path_t *) dp;
  565. grub_printf ("/USB(%x,%x)",
  566. (unsigned) usb->parent_port_number,
  567. (unsigned) usb->usb_interface);
  568. }
  569. break;
  570. case GRUB_EFI_USB_CLASS_DEVICE_PATH_SUBTYPE:
  571. {
  572. grub_efi_usb_class_device_path_t *usb_class
  573. = (grub_efi_usb_class_device_path_t *) dp;
  574. grub_printf ("/USBClass(%x,%x,%x,%x,%x)",
  575. (unsigned) usb_class->vendor_id,
  576. (unsigned) usb_class->product_id,
  577. (unsigned) usb_class->device_class,
  578. (unsigned) usb_class->device_subclass,
  579. (unsigned) usb_class->device_protocol);
  580. }
  581. break;
  582. case GRUB_EFI_I2O_DEVICE_PATH_SUBTYPE:
  583. {
  584. grub_efi_i2o_device_path_t *i2o
  585. = (grub_efi_i2o_device_path_t *) dp;
  586. grub_printf ("/I2O(%x)", (unsigned) i2o->tid);
  587. }
  588. break;
  589. case GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE:
  590. {
  591. grub_efi_mac_address_device_path_t *mac
  592. = (grub_efi_mac_address_device_path_t *) dp;
  593. grub_printf ("/MacAddr(%02x:%02x:%02x:%02x:%02x:%02x,%x)",
  594. (unsigned) mac->mac_address[0],
  595. (unsigned) mac->mac_address[1],
  596. (unsigned) mac->mac_address[2],
  597. (unsigned) mac->mac_address[3],
  598. (unsigned) mac->mac_address[4],
  599. (unsigned) mac->mac_address[5],
  600. (unsigned) mac->if_type);
  601. }
  602. break;
  603. case GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE:
  604. {
  605. grub_efi_ipv4_device_path_t *ipv4
  606. = (grub_efi_ipv4_device_path_t *) dp;
  607. grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
  608. (unsigned) ipv4->local_ip_address[0],
  609. (unsigned) ipv4->local_ip_address[1],
  610. (unsigned) ipv4->local_ip_address[2],
  611. (unsigned) ipv4->local_ip_address[3],
  612. (unsigned) ipv4->remote_ip_address[0],
  613. (unsigned) ipv4->remote_ip_address[1],
  614. (unsigned) ipv4->remote_ip_address[2],
  615. (unsigned) ipv4->remote_ip_address[3],
  616. (unsigned) ipv4->local_port,
  617. (unsigned) ipv4->remote_port,
  618. (unsigned) ipv4->protocol,
  619. (unsigned) ipv4->static_ip_address);
  620. }
  621. break;
  622. case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
  623. {
  624. grub_efi_ipv6_device_path_t *ipv6
  625. = (grub_efi_ipv6_device_path_t *) dp;
  626. grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
  627. (unsigned) ipv6->local_ip_address[0],
  628. (unsigned) ipv6->local_ip_address[1],
  629. (unsigned) ipv6->local_ip_address[2],
  630. (unsigned) ipv6->local_ip_address[3],
  631. (unsigned) ipv6->local_ip_address[4],
  632. (unsigned) ipv6->local_ip_address[5],
  633. (unsigned) ipv6->local_ip_address[6],
  634. (unsigned) ipv6->local_ip_address[7],
  635. (unsigned) ipv6->remote_ip_address[0],
  636. (unsigned) ipv6->remote_ip_address[1],
  637. (unsigned) ipv6->remote_ip_address[2],
  638. (unsigned) ipv6->remote_ip_address[3],
  639. (unsigned) ipv6->remote_ip_address[4],
  640. (unsigned) ipv6->remote_ip_address[5],
  641. (unsigned) ipv6->remote_ip_address[6],
  642. (unsigned) ipv6->remote_ip_address[7],
  643. (unsigned) ipv6->local_port,
  644. (unsigned) ipv6->remote_port,
  645. (unsigned) ipv6->protocol,
  646. (unsigned) ipv6->static_ip_address);
  647. }
  648. break;
  649. case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
  650. {
  651. grub_efi_infiniband_device_path_t *ib
  652. = (grub_efi_infiniband_device_path_t *) dp;
  653. grub_printf ("/InfiniBand(%x,%llx,%llx,%llx)",
  654. (unsigned) ib->port_gid[0], /* XXX */
  655. (unsigned long long) ib->remote_id,
  656. (unsigned long long) ib->target_port_id,
  657. (unsigned long long) ib->device_id);
  658. }
  659. break;
  660. case GRUB_EFI_UART_DEVICE_PATH_SUBTYPE:
  661. {
  662. grub_efi_uart_device_path_t *uart
  663. = (grub_efi_uart_device_path_t *) dp;
  664. grub_printf ("/UART(%llu,%u,%x,%x)",
  665. (unsigned long long) uart->baud_rate,
  666. uart->data_bits,
  667. uart->parity,
  668. uart->stop_bits);
  669. }
  670. break;
  671. case GRUB_EFI_SATA_DEVICE_PATH_SUBTYPE:
  672. {
  673. grub_efi_sata_device_path_t *sata;
  674. sata = (grub_efi_sata_device_path_t *) dp;
  675. grub_printf ("/Sata(%x,%x,%x)",
  676. sata->hba_port,
  677. sata->multiplier_port,
  678. sata->lun);
  679. }
  680. break;
  681. case GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE:
  682. dump_vendor_path ("Messaging",
  683. (grub_efi_vendor_device_path_t *) dp);
  684. break;
  685. default:
  686. grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
  687. break;
  688. }
  689. break;
  690. case GRUB_EFI_MEDIA_DEVICE_PATH_TYPE:
  691. switch (subtype)
  692. {
  693. case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
  694. {
  695. grub_efi_hard_drive_device_path_t *hd = (grub_efi_hard_drive_device_path_t *) dp;
  696. grub_printf ("/HD(%u,%llx,%llx,%02x%02x%02x%02x%02x%02x%02x%02x,%x,%x)",
  697. hd->partition_number,
  698. (unsigned long long) hd->partition_start,
  699. (unsigned long long) hd->partition_size,
  700. (unsigned) hd->partition_signature[0],
  701. (unsigned) hd->partition_signature[1],
  702. (unsigned) hd->partition_signature[2],
  703. (unsigned) hd->partition_signature[3],
  704. (unsigned) hd->partition_signature[4],
  705. (unsigned) hd->partition_signature[5],
  706. (unsigned) hd->partition_signature[6],
  707. (unsigned) hd->partition_signature[7],
  708. (unsigned) hd->partmap_type,
  709. (unsigned) hd->signature_type);
  710. }
  711. break;
  712. case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
  713. {
  714. grub_efi_cdrom_device_path_t *cd
  715. = (grub_efi_cdrom_device_path_t *) dp;
  716. grub_printf ("/CD(%u,%llx,%llx)",
  717. cd->boot_entry,
  718. (unsigned long long) cd->partition_start,
  719. (unsigned long long) cd->partition_size);
  720. }
  721. break;
  722. case GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE:
  723. dump_vendor_path ("Media",
  724. (grub_efi_vendor_device_path_t *) dp);
  725. break;
  726. case GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE:
  727. {
  728. grub_efi_file_path_device_path_t *fp;
  729. grub_uint8_t *buf;
  730. fp = (grub_efi_file_path_device_path_t *) dp;
  731. buf = grub_malloc ((len - 4) * 2 + 1);
  732. if (buf)
  733. {
  734. grub_efi_char16_t *dup_name = grub_malloc (len - 4);
  735. if (!dup_name)
  736. {
  737. grub_errno = GRUB_ERR_NONE;
  738. grub_printf ("/File((null))");
  739. grub_free (buf);
  740. break;
  741. }
  742. *grub_utf16_to_utf8 (buf, grub_memcpy (dup_name, fp->path_name, len - 4),
  743. (len - 4) / sizeof (grub_efi_char16_t))
  744. = '\0';
  745. grub_free (dup_name);
  746. }
  747. else
  748. grub_errno = GRUB_ERR_NONE;
  749. grub_printf ("/File(%s)", buf);
  750. grub_free (buf);
  751. }
  752. break;
  753. case GRUB_EFI_PROTOCOL_DEVICE_PATH_SUBTYPE:
  754. {
  755. grub_efi_protocol_device_path_t *proto
  756. = (grub_efi_protocol_device_path_t *) dp;
  757. grub_printf ("/Protocol(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
  758. (unsigned) proto->guid.data1,
  759. (unsigned) proto->guid.data2,
  760. (unsigned) proto->guid.data3,
  761. (unsigned) proto->guid.data4[0],
  762. (unsigned) proto->guid.data4[1],
  763. (unsigned) proto->guid.data4[2],
  764. (unsigned) proto->guid.data4[3],
  765. (unsigned) proto->guid.data4[4],
  766. (unsigned) proto->guid.data4[5],
  767. (unsigned) proto->guid.data4[6],
  768. (unsigned) proto->guid.data4[7]);
  769. }
  770. break;
  771. default:
  772. grub_printf ("/UnknownMedia(%x)", (unsigned) subtype);
  773. break;
  774. }
  775. break;
  776. case GRUB_EFI_BIOS_DEVICE_PATH_TYPE:
  777. switch (subtype)
  778. {
  779. case GRUB_EFI_BIOS_DEVICE_PATH_SUBTYPE:
  780. {
  781. grub_efi_bios_device_path_t *bios
  782. = (grub_efi_bios_device_path_t *) dp;
  783. grub_printf ("/BIOS(%x,%x,%s)",
  784. (unsigned) bios->device_type,
  785. (unsigned) bios->status_flags,
  786. (char *) (dp + 1));
  787. }
  788. break;
  789. default:
  790. grub_printf ("/UnknownBIOS(%x)", (unsigned) subtype);
  791. break;
  792. }
  793. break;
  794. default:
  795. grub_printf ("/UnknownType(%x,%x)\n",
  796. (unsigned) type,
  797. (unsigned) subtype);
  798. return;
  799. break;
  800. }
  801. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  802. break;
  803. dp = (grub_efi_device_path_t *) ((char *) dp + len);
  804. }
  805. }
  806. /* Compare device paths. */
  807. int
  808. grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
  809. const grub_efi_device_path_t *dp2)
  810. {
  811. if (! dp1 || ! dp2)
  812. /* Return non-zero. */
  813. return 1;
  814. while (1)
  815. {
  816. grub_efi_uint8_t type1, type2;
  817. grub_efi_uint8_t subtype1, subtype2;
  818. grub_efi_uint16_t len1, len2;
  819. int ret;
  820. type1 = GRUB_EFI_DEVICE_PATH_TYPE (dp1);
  821. type2 = GRUB_EFI_DEVICE_PATH_TYPE (dp2);
  822. if (type1 != type2)
  823. return (int) type2 - (int) type1;
  824. subtype1 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp1);
  825. subtype2 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp2);
  826. if (subtype1 != subtype2)
  827. return (int) subtype1 - (int) subtype2;
  828. len1 = GRUB_EFI_DEVICE_PATH_LENGTH (dp1);
  829. len2 = GRUB_EFI_DEVICE_PATH_LENGTH (dp2);
  830. if (len1 != len2)
  831. return (int) len1 - (int) len2;
  832. ret = grub_memcmp (dp1, dp2, len1);
  833. if (ret != 0)
  834. return ret;
  835. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp1))
  836. break;
  837. dp1 = (grub_efi_device_path_t *) ((char *) dp1 + len1);
  838. dp2 = (grub_efi_device_path_t *) ((char *) dp2 + len2);
  839. }
  840. return 0;
  841. }