efi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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_exit (void)
  135. {
  136. grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
  137. efi_call_4 (grub_efi_system_table->boot_services->exit,
  138. grub_efi_image_handle, GRUB_EFI_SUCCESS, 0, 0);
  139. for (;;) ;
  140. }
  141. grub_err_t
  142. grub_efi_set_virtual_address_map (grub_efi_uintn_t memory_map_size,
  143. grub_efi_uintn_t descriptor_size,
  144. grub_efi_uint32_t descriptor_version,
  145. grub_efi_memory_descriptor_t *virtual_map)
  146. {
  147. grub_efi_runtime_services_t *r;
  148. grub_efi_status_t status;
  149. r = grub_efi_system_table->runtime_services;
  150. status = efi_call_4 (r->set_virtual_address_map, memory_map_size,
  151. descriptor_size, descriptor_version, virtual_map);
  152. if (status == GRUB_EFI_SUCCESS)
  153. return GRUB_ERR_NONE;
  154. return grub_error (GRUB_ERR_IO, "set_virtual_address_map failed");
  155. }
  156. grub_err_t
  157. grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
  158. void *data, grub_size_t datasize)
  159. {
  160. grub_efi_status_t status;
  161. grub_efi_runtime_services_t *r;
  162. grub_efi_char16_t *var16;
  163. grub_size_t len, len16;
  164. len = grub_strlen (var);
  165. len16 = len * GRUB_MAX_UTF16_PER_UTF8;
  166. var16 = grub_malloc ((len16 + 1) * sizeof (var16[0]));
  167. if (!var16)
  168. return grub_errno;
  169. len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
  170. var16[len16] = 0;
  171. r = grub_efi_system_table->runtime_services;
  172. status = efi_call_5 (r->set_variable, var16, guid,
  173. (GRUB_EFI_VARIABLE_NON_VOLATILE
  174. | GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS
  175. | GRUB_EFI_VARIABLE_RUNTIME_ACCESS),
  176. datasize, data);
  177. grub_free (var16);
  178. if (status == GRUB_EFI_SUCCESS)
  179. return GRUB_ERR_NONE;
  180. return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
  181. }
  182. void *
  183. grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
  184. grub_size_t *datasize_out)
  185. {
  186. grub_efi_status_t status;
  187. grub_efi_uintn_t datasize = 0;
  188. grub_efi_runtime_services_t *r;
  189. grub_efi_char16_t *var16;
  190. void *data;
  191. grub_size_t len, len16;
  192. *datasize_out = 0;
  193. len = grub_strlen (var);
  194. len16 = len * GRUB_MAX_UTF16_PER_UTF8;
  195. var16 = grub_malloc ((len16 + 1) * sizeof (var16[0]));
  196. if (!var16)
  197. return NULL;
  198. len16 = grub_utf8_to_utf16 (var16, len16, (grub_uint8_t *) var, len, NULL);
  199. var16[len16] = 0;
  200. r = grub_efi_system_table->runtime_services;
  201. status = efi_call_5 (r->get_variable, var16, guid, NULL, &datasize, NULL);
  202. if (status != GRUB_EFI_BUFFER_TOO_SMALL || !datasize)
  203. {
  204. grub_free (var16);
  205. return NULL;
  206. }
  207. data = grub_malloc (datasize);
  208. if (!data)
  209. {
  210. grub_free (var16);
  211. return NULL;
  212. }
  213. status = efi_call_5 (r->get_variable, var16, guid, NULL, &datasize, data);
  214. grub_free (var16);
  215. if (status == GRUB_EFI_SUCCESS)
  216. {
  217. *datasize_out = datasize;
  218. return data;
  219. }
  220. grub_free (data);
  221. return NULL;
  222. }
  223. #pragma GCC diagnostic ignored "-Wcast-align"
  224. /* Search the mods section from the PE32/PE32+ image. This code uses
  225. a PE32 header, but should work with PE32+ as well. */
  226. grub_addr_t
  227. grub_efi_modules_addr (void)
  228. {
  229. grub_efi_loaded_image_t *image;
  230. struct grub_pe32_header *header;
  231. struct grub_pe32_coff_header *coff_header;
  232. struct grub_pe32_section_table *sections;
  233. struct grub_pe32_section_table *section;
  234. struct grub_module_info *info;
  235. grub_uint16_t i;
  236. image = grub_efi_get_loaded_image (grub_efi_image_handle);
  237. if (! image)
  238. return 0;
  239. header = image->image_base;
  240. coff_header = &(header->coff_header);
  241. sections
  242. = (struct grub_pe32_section_table *) ((char *) coff_header
  243. + sizeof (*coff_header)
  244. + coff_header->optional_header_size);
  245. for (i = 0, section = sections;
  246. i < coff_header->num_sections;
  247. i++, section++)
  248. {
  249. if (grub_strcmp (section->name, "mods") == 0)
  250. break;
  251. }
  252. if (i == coff_header->num_sections)
  253. return 0;
  254. info = (struct grub_module_info *) ((char *) image->image_base
  255. + section->virtual_address);
  256. if (info->magic != GRUB_MODULE_MAGIC)
  257. return 0;
  258. return (grub_addr_t) info;
  259. }
  260. #pragma GCC diagnostic error "-Wcast-align"
  261. char *
  262. grub_efi_get_filename (grub_efi_device_path_t *dp0)
  263. {
  264. char *name = 0, *p, *pi;
  265. grub_size_t filesize = 0;
  266. grub_efi_device_path_t *dp;
  267. dp = dp0;
  268. while (1)
  269. {
  270. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  271. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  272. if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
  273. break;
  274. if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
  275. && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
  276. {
  277. grub_efi_uint16_t len;
  278. len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
  279. / sizeof (grub_efi_char16_t));
  280. filesize += GRUB_MAX_UTF8_PER_UTF16 * len + 2;
  281. }
  282. dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
  283. }
  284. if (!filesize)
  285. return NULL;
  286. dp = dp0;
  287. p = name = grub_malloc (filesize);
  288. if (!name)
  289. return NULL;
  290. while (1)
  291. {
  292. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  293. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  294. if (type == GRUB_EFI_END_DEVICE_PATH_TYPE)
  295. break;
  296. else if (type == GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
  297. && subtype == GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE)
  298. {
  299. grub_efi_file_path_device_path_t *fp;
  300. grub_efi_uint16_t len;
  301. *p++ = '/';
  302. len = ((GRUB_EFI_DEVICE_PATH_LENGTH (dp) - 4)
  303. / sizeof (grub_efi_char16_t));
  304. fp = (grub_efi_file_path_device_path_t *) dp;
  305. p = (char *) grub_utf16_to_utf8 ((unsigned char *) p, fp->path_name, len);
  306. }
  307. dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
  308. }
  309. *p = '\0';
  310. for (pi = name, p = name; *pi;)
  311. {
  312. /* EFI breaks paths with backslashes. */
  313. if (*pi == '\\' || *pi == '/')
  314. {
  315. *p++ = '/';
  316. while (*pi == '\\' || *pi == '/')
  317. pi++;
  318. continue;
  319. }
  320. *p++ = *pi++;
  321. }
  322. *p = '\0';
  323. return name;
  324. }
  325. grub_efi_device_path_t *
  326. grub_efi_get_device_path (grub_efi_handle_t handle)
  327. {
  328. return grub_efi_open_protocol (handle, &device_path_guid,
  329. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  330. }
  331. /* Return the device path node right before the end node. */
  332. grub_efi_device_path_t *
  333. grub_efi_find_last_device_path (const grub_efi_device_path_t *dp)
  334. {
  335. grub_efi_device_path_t *next, *p;
  336. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  337. return 0;
  338. for (p = (grub_efi_device_path_t *) dp, next = GRUB_EFI_NEXT_DEVICE_PATH (p);
  339. ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (next);
  340. p = next, next = GRUB_EFI_NEXT_DEVICE_PATH (next))
  341. ;
  342. return p;
  343. }
  344. /* Duplicate a device path. */
  345. grub_efi_device_path_t *
  346. grub_efi_duplicate_device_path (const grub_efi_device_path_t *dp)
  347. {
  348. grub_efi_device_path_t *p;
  349. grub_size_t total_size = 0;
  350. for (p = (grub_efi_device_path_t *) dp;
  351. ;
  352. p = GRUB_EFI_NEXT_DEVICE_PATH (p))
  353. {
  354. total_size += GRUB_EFI_DEVICE_PATH_LENGTH (p);
  355. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
  356. break;
  357. }
  358. p = grub_malloc (total_size);
  359. if (! p)
  360. return 0;
  361. grub_memcpy (p, dp, total_size);
  362. return p;
  363. }
  364. static void
  365. dump_vendor_path (const char *type, grub_efi_vendor_device_path_t *vendor)
  366. {
  367. grub_uint32_t vendor_data_len = vendor->header.length - sizeof (*vendor);
  368. grub_printf ("/%sVendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)[%x: ",
  369. type,
  370. (unsigned) vendor->vendor_guid.data1,
  371. (unsigned) vendor->vendor_guid.data2,
  372. (unsigned) vendor->vendor_guid.data3,
  373. (unsigned) vendor->vendor_guid.data4[0],
  374. (unsigned) vendor->vendor_guid.data4[1],
  375. (unsigned) vendor->vendor_guid.data4[2],
  376. (unsigned) vendor->vendor_guid.data4[3],
  377. (unsigned) vendor->vendor_guid.data4[4],
  378. (unsigned) vendor->vendor_guid.data4[5],
  379. (unsigned) vendor->vendor_guid.data4[6],
  380. (unsigned) vendor->vendor_guid.data4[7],
  381. vendor_data_len);
  382. if (vendor->header.length > sizeof (*vendor))
  383. {
  384. grub_uint32_t i;
  385. for (i = 0; i < vendor_data_len; i++)
  386. grub_printf ("%02x ", vendor->vendor_defined_data[i]);
  387. }
  388. grub_printf ("]");
  389. }
  390. /* Print the chain of Device Path nodes. This is mainly for debugging. */
  391. void
  392. grub_efi_print_device_path (grub_efi_device_path_t *dp)
  393. {
  394. while (1)
  395. {
  396. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  397. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  398. grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
  399. switch (type)
  400. {
  401. case GRUB_EFI_END_DEVICE_PATH_TYPE:
  402. switch (subtype)
  403. {
  404. case GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE:
  405. grub_printf ("/EndEntire\n");
  406. //grub_putchar ('\n');
  407. break;
  408. case GRUB_EFI_END_THIS_DEVICE_PATH_SUBTYPE:
  409. grub_printf ("/EndThis\n");
  410. //grub_putchar ('\n');
  411. break;
  412. default:
  413. grub_printf ("/EndUnknown(%x)\n", (unsigned) subtype);
  414. break;
  415. }
  416. break;
  417. case GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE:
  418. switch (subtype)
  419. {
  420. case GRUB_EFI_PCI_DEVICE_PATH_SUBTYPE:
  421. {
  422. grub_efi_pci_device_path_t *pci
  423. = (grub_efi_pci_device_path_t *) dp;
  424. grub_printf ("/PCI(%x,%x)",
  425. (unsigned) pci->function, (unsigned) pci->device);
  426. }
  427. break;
  428. case GRUB_EFI_PCCARD_DEVICE_PATH_SUBTYPE:
  429. {
  430. grub_efi_pccard_device_path_t *pccard
  431. = (grub_efi_pccard_device_path_t *) dp;
  432. grub_printf ("/PCCARD(%x)",
  433. (unsigned) pccard->function);
  434. }
  435. break;
  436. case GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE:
  437. {
  438. grub_efi_memory_mapped_device_path_t *mmapped
  439. = (grub_efi_memory_mapped_device_path_t *) dp;
  440. grub_printf ("/MMap(%x,%llx,%llx)",
  441. (unsigned) mmapped->memory_type,
  442. (unsigned long long) mmapped->start_address,
  443. (unsigned long long) mmapped->end_address);
  444. }
  445. break;
  446. case GRUB_EFI_VENDOR_DEVICE_PATH_SUBTYPE:
  447. dump_vendor_path ("Hardware",
  448. (grub_efi_vendor_device_path_t *) dp);
  449. break;
  450. case GRUB_EFI_CONTROLLER_DEVICE_PATH_SUBTYPE:
  451. {
  452. grub_efi_controller_device_path_t *controller
  453. = (grub_efi_controller_device_path_t *) dp;
  454. grub_printf ("/Ctrl(%x)",
  455. (unsigned) controller->controller_number);
  456. }
  457. break;
  458. default:
  459. grub_printf ("/UnknownHW(%x)", (unsigned) subtype);
  460. break;
  461. }
  462. break;
  463. case GRUB_EFI_ACPI_DEVICE_PATH_TYPE:
  464. switch (subtype)
  465. {
  466. case GRUB_EFI_ACPI_DEVICE_PATH_SUBTYPE:
  467. {
  468. grub_efi_acpi_device_path_t *acpi
  469. = (grub_efi_acpi_device_path_t *) dp;
  470. grub_printf ("/ACPI(%x,%x)",
  471. (unsigned) acpi->hid,
  472. (unsigned) acpi->uid);
  473. }
  474. break;
  475. case GRUB_EFI_EXPANDED_ACPI_DEVICE_PATH_SUBTYPE:
  476. {
  477. grub_efi_expanded_acpi_device_path_t *eacpi
  478. = (grub_efi_expanded_acpi_device_path_t *) dp;
  479. grub_printf ("/ACPI(");
  480. if (GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp)[0] == '\0')
  481. grub_printf ("%x,", (unsigned) eacpi->hid);
  482. else
  483. grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp));
  484. if (GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp)[0] == '\0')
  485. grub_printf ("%x,", (unsigned) eacpi->uid);
  486. else
  487. grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp));
  488. if (GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp)[0] == '\0')
  489. grub_printf ("%x)", (unsigned) eacpi->cid);
  490. else
  491. grub_printf ("%s)", GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp));
  492. }
  493. break;
  494. default:
  495. grub_printf ("/UnknownACPI(%x)", (unsigned) subtype);
  496. break;
  497. }
  498. break;
  499. case GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE:
  500. switch (subtype)
  501. {
  502. case GRUB_EFI_ATAPI_DEVICE_PATH_SUBTYPE:
  503. {
  504. grub_efi_atapi_device_path_t *atapi
  505. = (grub_efi_atapi_device_path_t *) dp;
  506. grub_printf ("/ATAPI(%x,%x,%x)",
  507. (unsigned) atapi->primary_secondary,
  508. (unsigned) atapi->slave_master,
  509. (unsigned) atapi->lun);
  510. }
  511. break;
  512. case GRUB_EFI_SCSI_DEVICE_PATH_SUBTYPE:
  513. {
  514. grub_efi_scsi_device_path_t *scsi
  515. = (grub_efi_scsi_device_path_t *) dp;
  516. grub_printf ("/SCSI(%x,%x)",
  517. (unsigned) scsi->pun,
  518. (unsigned) scsi->lun);
  519. }
  520. break;
  521. case GRUB_EFI_FIBRE_CHANNEL_DEVICE_PATH_SUBTYPE:
  522. {
  523. grub_efi_fibre_channel_device_path_t *fc
  524. = (grub_efi_fibre_channel_device_path_t *) dp;
  525. grub_printf ("/FibreChannel(%llx,%llx)",
  526. (unsigned long long) fc->wwn,
  527. (unsigned long long) fc->lun);
  528. }
  529. break;
  530. case GRUB_EFI_1394_DEVICE_PATH_SUBTYPE:
  531. {
  532. grub_efi_1394_device_path_t *firewire
  533. = (grub_efi_1394_device_path_t *) dp;
  534. grub_printf ("/1394(%llx)",
  535. (unsigned long long) firewire->guid);
  536. }
  537. break;
  538. case GRUB_EFI_USB_DEVICE_PATH_SUBTYPE:
  539. {
  540. grub_efi_usb_device_path_t *usb
  541. = (grub_efi_usb_device_path_t *) dp;
  542. grub_printf ("/USB(%x,%x)",
  543. (unsigned) usb->parent_port_number,
  544. (unsigned) usb->usb_interface);
  545. }
  546. break;
  547. case GRUB_EFI_USB_CLASS_DEVICE_PATH_SUBTYPE:
  548. {
  549. grub_efi_usb_class_device_path_t *usb_class
  550. = (grub_efi_usb_class_device_path_t *) dp;
  551. grub_printf ("/USBClass(%x,%x,%x,%x,%x)",
  552. (unsigned) usb_class->vendor_id,
  553. (unsigned) usb_class->product_id,
  554. (unsigned) usb_class->device_class,
  555. (unsigned) usb_class->device_subclass,
  556. (unsigned) usb_class->device_protocol);
  557. }
  558. break;
  559. case GRUB_EFI_I2O_DEVICE_PATH_SUBTYPE:
  560. {
  561. grub_efi_i2o_device_path_t *i2o
  562. = (grub_efi_i2o_device_path_t *) dp;
  563. grub_printf ("/I2O(%x)", (unsigned) i2o->tid);
  564. }
  565. break;
  566. case GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE:
  567. {
  568. grub_efi_mac_address_device_path_t *mac
  569. = (grub_efi_mac_address_device_path_t *) dp;
  570. grub_printf ("/MacAddr(%02x:%02x:%02x:%02x:%02x:%02x,%x)",
  571. (unsigned) mac->mac_address[0],
  572. (unsigned) mac->mac_address[1],
  573. (unsigned) mac->mac_address[2],
  574. (unsigned) mac->mac_address[3],
  575. (unsigned) mac->mac_address[4],
  576. (unsigned) mac->mac_address[5],
  577. (unsigned) mac->if_type);
  578. }
  579. break;
  580. case GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE:
  581. {
  582. grub_efi_ipv4_device_path_t *ipv4
  583. = (grub_efi_ipv4_device_path_t *) dp;
  584. grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
  585. (unsigned) ipv4->local_ip_address[0],
  586. (unsigned) ipv4->local_ip_address[1],
  587. (unsigned) ipv4->local_ip_address[2],
  588. (unsigned) ipv4->local_ip_address[3],
  589. (unsigned) ipv4->remote_ip_address[0],
  590. (unsigned) ipv4->remote_ip_address[1],
  591. (unsigned) ipv4->remote_ip_address[2],
  592. (unsigned) ipv4->remote_ip_address[3],
  593. (unsigned) ipv4->local_port,
  594. (unsigned) ipv4->remote_port,
  595. (unsigned) ipv4->protocol,
  596. (unsigned) ipv4->static_ip_address);
  597. }
  598. break;
  599. case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
  600. {
  601. grub_efi_ipv6_device_path_t *ipv6
  602. = (grub_efi_ipv6_device_path_t *) dp;
  603. grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
  604. (unsigned) ipv6->local_ip_address[0],
  605. (unsigned) ipv6->local_ip_address[1],
  606. (unsigned) ipv6->local_ip_address[2],
  607. (unsigned) ipv6->local_ip_address[3],
  608. (unsigned) ipv6->local_ip_address[4],
  609. (unsigned) ipv6->local_ip_address[5],
  610. (unsigned) ipv6->local_ip_address[6],
  611. (unsigned) ipv6->local_ip_address[7],
  612. (unsigned) ipv6->remote_ip_address[0],
  613. (unsigned) ipv6->remote_ip_address[1],
  614. (unsigned) ipv6->remote_ip_address[2],
  615. (unsigned) ipv6->remote_ip_address[3],
  616. (unsigned) ipv6->remote_ip_address[4],
  617. (unsigned) ipv6->remote_ip_address[5],
  618. (unsigned) ipv6->remote_ip_address[6],
  619. (unsigned) ipv6->remote_ip_address[7],
  620. (unsigned) ipv6->local_port,
  621. (unsigned) ipv6->remote_port,
  622. (unsigned) ipv6->protocol,
  623. (unsigned) ipv6->static_ip_address);
  624. }
  625. break;
  626. case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
  627. {
  628. grub_efi_infiniband_device_path_t *ib
  629. = (grub_efi_infiniband_device_path_t *) dp;
  630. grub_printf ("/InfiniBand(%x,%llx,%llx,%llx)",
  631. (unsigned) ib->port_gid[0], /* XXX */
  632. (unsigned long long) ib->remote_id,
  633. (unsigned long long) ib->target_port_id,
  634. (unsigned long long) ib->device_id);
  635. }
  636. break;
  637. case GRUB_EFI_UART_DEVICE_PATH_SUBTYPE:
  638. {
  639. grub_efi_uart_device_path_t *uart
  640. = (grub_efi_uart_device_path_t *) dp;
  641. grub_printf ("/UART(%llu,%u,%x,%x)",
  642. (unsigned long long) uart->baud_rate,
  643. uart->data_bits,
  644. uart->parity,
  645. uart->stop_bits);
  646. }
  647. break;
  648. case GRUB_EFI_SATA_DEVICE_PATH_SUBTYPE:
  649. {
  650. grub_efi_sata_device_path_t *sata;
  651. sata = (grub_efi_sata_device_path_t *) dp;
  652. grub_printf ("/Sata(%x,%x,%x)",
  653. sata->hba_port,
  654. sata->multiplier_port,
  655. sata->lun);
  656. }
  657. break;
  658. case GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE:
  659. dump_vendor_path ("Messaging",
  660. (grub_efi_vendor_device_path_t *) dp);
  661. break;
  662. default:
  663. grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
  664. break;
  665. }
  666. break;
  667. case GRUB_EFI_MEDIA_DEVICE_PATH_TYPE:
  668. switch (subtype)
  669. {
  670. case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
  671. {
  672. grub_efi_hard_drive_device_path_t *hd = (grub_efi_hard_drive_device_path_t *) dp;
  673. grub_printf ("/HD(%u,%llx,%llx,%02x%02x%02x%02x%02x%02x%02x%02x,%x,%x)",
  674. hd->partition_number,
  675. (unsigned long long) hd->partition_start,
  676. (unsigned long long) hd->partition_size,
  677. (unsigned) hd->partition_signature[0],
  678. (unsigned) hd->partition_signature[1],
  679. (unsigned) hd->partition_signature[2],
  680. (unsigned) hd->partition_signature[3],
  681. (unsigned) hd->partition_signature[4],
  682. (unsigned) hd->partition_signature[5],
  683. (unsigned) hd->partition_signature[6],
  684. (unsigned) hd->partition_signature[7],
  685. (unsigned) hd->partmap_type,
  686. (unsigned) hd->signature_type);
  687. }
  688. break;
  689. case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
  690. {
  691. grub_efi_cdrom_device_path_t *cd
  692. = (grub_efi_cdrom_device_path_t *) dp;
  693. grub_printf ("/CD(%u,%llx,%llx)",
  694. cd->boot_entry,
  695. (unsigned long long) cd->partition_start,
  696. (unsigned long long) cd->partition_size);
  697. }
  698. break;
  699. case GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE:
  700. dump_vendor_path ("Media",
  701. (grub_efi_vendor_device_path_t *) dp);
  702. break;
  703. case GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE:
  704. {
  705. grub_efi_file_path_device_path_t *fp;
  706. grub_uint8_t *buf;
  707. fp = (grub_efi_file_path_device_path_t *) dp;
  708. buf = grub_malloc ((len - 4) * 2 + 1);
  709. if (buf)
  710. *grub_utf16_to_utf8 (buf, fp->path_name,
  711. (len - 4) / sizeof (grub_efi_char16_t))
  712. = '\0';
  713. else
  714. grub_errno = GRUB_ERR_NONE;
  715. grub_printf ("/File(%s)", buf);
  716. grub_free (buf);
  717. }
  718. break;
  719. case GRUB_EFI_PROTOCOL_DEVICE_PATH_SUBTYPE:
  720. {
  721. grub_efi_protocol_device_path_t *proto
  722. = (grub_efi_protocol_device_path_t *) dp;
  723. grub_printf ("/Protocol(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
  724. (unsigned) proto->guid.data1,
  725. (unsigned) proto->guid.data2,
  726. (unsigned) proto->guid.data3,
  727. (unsigned) proto->guid.data4[0],
  728. (unsigned) proto->guid.data4[1],
  729. (unsigned) proto->guid.data4[2],
  730. (unsigned) proto->guid.data4[3],
  731. (unsigned) proto->guid.data4[4],
  732. (unsigned) proto->guid.data4[5],
  733. (unsigned) proto->guid.data4[6],
  734. (unsigned) proto->guid.data4[7]);
  735. }
  736. break;
  737. default:
  738. grub_printf ("/UnknownMedia(%x)", (unsigned) subtype);
  739. break;
  740. }
  741. break;
  742. case GRUB_EFI_BIOS_DEVICE_PATH_TYPE:
  743. switch (subtype)
  744. {
  745. case GRUB_EFI_BIOS_DEVICE_PATH_SUBTYPE:
  746. {
  747. grub_efi_bios_device_path_t *bios
  748. = (grub_efi_bios_device_path_t *) dp;
  749. grub_printf ("/BIOS(%x,%x,%s)",
  750. (unsigned) bios->device_type,
  751. (unsigned) bios->status_flags,
  752. (char *) (dp + 1));
  753. }
  754. break;
  755. default:
  756. grub_printf ("/UnknownBIOS(%x)", (unsigned) subtype);
  757. break;
  758. }
  759. break;
  760. default:
  761. grub_printf ("/UnknownType(%x,%x)\n",
  762. (unsigned) type,
  763. (unsigned) subtype);
  764. return;
  765. break;
  766. }
  767. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  768. break;
  769. dp = (grub_efi_device_path_t *) ((char *) dp + len);
  770. }
  771. }
  772. /* Compare device paths. */
  773. int
  774. grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
  775. const grub_efi_device_path_t *dp2)
  776. {
  777. if (! dp1 || ! dp2)
  778. /* Return non-zero. */
  779. return 1;
  780. while (1)
  781. {
  782. grub_efi_uint8_t type1, type2;
  783. grub_efi_uint8_t subtype1, subtype2;
  784. grub_efi_uint16_t len1, len2;
  785. int ret;
  786. type1 = GRUB_EFI_DEVICE_PATH_TYPE (dp1);
  787. type2 = GRUB_EFI_DEVICE_PATH_TYPE (dp2);
  788. if (type1 != type2)
  789. return (int) type2 - (int) type1;
  790. subtype1 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp1);
  791. subtype2 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp2);
  792. if (subtype1 != subtype2)
  793. return (int) subtype1 - (int) subtype2;
  794. len1 = GRUB_EFI_DEVICE_PATH_LENGTH (dp1);
  795. len2 = GRUB_EFI_DEVICE_PATH_LENGTH (dp2);
  796. if (len1 != len2)
  797. return (int) len1 - (int) len2;
  798. ret = grub_memcmp (dp1, dp2, len1);
  799. if (ret != 0)
  800. return ret;
  801. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp1))
  802. break;
  803. dp1 = (grub_efi_device_path_t *) ((char *) dp1 + len1);
  804. dp2 = (grub_efi_device_path_t *) ((char *) dp2 + len2);
  805. }
  806. return 0;
  807. }