efi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. /* According to EFI spec Path Name is NULL terminated */
  306. while (len > 0 && fp->path_name[len - 1] == 0)
  307. len--;
  308. p = (char *) grub_utf16_to_utf8 ((unsigned char *) p, fp->path_name, len);
  309. }
  310. dp = GRUB_EFI_NEXT_DEVICE_PATH (dp);
  311. }
  312. *p = '\0';
  313. for (pi = name, p = name; *pi;)
  314. {
  315. /* EFI breaks paths with backslashes. */
  316. if (*pi == '\\' || *pi == '/')
  317. {
  318. *p++ = '/';
  319. while (*pi == '\\' || *pi == '/')
  320. pi++;
  321. continue;
  322. }
  323. *p++ = *pi++;
  324. }
  325. *p = '\0';
  326. return name;
  327. }
  328. grub_efi_device_path_t *
  329. grub_efi_get_device_path (grub_efi_handle_t handle)
  330. {
  331. return grub_efi_open_protocol (handle, &device_path_guid,
  332. GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  333. }
  334. /* Return the device path node right before the end node. */
  335. grub_efi_device_path_t *
  336. grub_efi_find_last_device_path (const grub_efi_device_path_t *dp)
  337. {
  338. grub_efi_device_path_t *next, *p;
  339. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  340. return 0;
  341. for (p = (grub_efi_device_path_t *) dp, next = GRUB_EFI_NEXT_DEVICE_PATH (p);
  342. ! GRUB_EFI_END_ENTIRE_DEVICE_PATH (next);
  343. p = next, next = GRUB_EFI_NEXT_DEVICE_PATH (next))
  344. ;
  345. return p;
  346. }
  347. /* Duplicate a device path. */
  348. grub_efi_device_path_t *
  349. grub_efi_duplicate_device_path (const grub_efi_device_path_t *dp)
  350. {
  351. grub_efi_device_path_t *p;
  352. grub_size_t total_size = 0;
  353. for (p = (grub_efi_device_path_t *) dp;
  354. ;
  355. p = GRUB_EFI_NEXT_DEVICE_PATH (p))
  356. {
  357. total_size += GRUB_EFI_DEVICE_PATH_LENGTH (p);
  358. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (p))
  359. break;
  360. }
  361. p = grub_malloc (total_size);
  362. if (! p)
  363. return 0;
  364. grub_memcpy (p, dp, total_size);
  365. return p;
  366. }
  367. static void
  368. dump_vendor_path (const char *type, grub_efi_vendor_device_path_t *vendor)
  369. {
  370. grub_uint32_t vendor_data_len = vendor->header.length - sizeof (*vendor);
  371. grub_printf ("/%sVendor(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)[%x: ",
  372. type,
  373. (unsigned) vendor->vendor_guid.data1,
  374. (unsigned) vendor->vendor_guid.data2,
  375. (unsigned) vendor->vendor_guid.data3,
  376. (unsigned) vendor->vendor_guid.data4[0],
  377. (unsigned) vendor->vendor_guid.data4[1],
  378. (unsigned) vendor->vendor_guid.data4[2],
  379. (unsigned) vendor->vendor_guid.data4[3],
  380. (unsigned) vendor->vendor_guid.data4[4],
  381. (unsigned) vendor->vendor_guid.data4[5],
  382. (unsigned) vendor->vendor_guid.data4[6],
  383. (unsigned) vendor->vendor_guid.data4[7],
  384. vendor_data_len);
  385. if (vendor->header.length > sizeof (*vendor))
  386. {
  387. grub_uint32_t i;
  388. for (i = 0; i < vendor_data_len; i++)
  389. grub_printf ("%02x ", vendor->vendor_defined_data[i]);
  390. }
  391. grub_printf ("]");
  392. }
  393. /* Print the chain of Device Path nodes. This is mainly for debugging. */
  394. void
  395. grub_efi_print_device_path (grub_efi_device_path_t *dp)
  396. {
  397. while (1)
  398. {
  399. grub_efi_uint8_t type = GRUB_EFI_DEVICE_PATH_TYPE (dp);
  400. grub_efi_uint8_t subtype = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp);
  401. grub_efi_uint16_t len = GRUB_EFI_DEVICE_PATH_LENGTH (dp);
  402. switch (type)
  403. {
  404. case GRUB_EFI_END_DEVICE_PATH_TYPE:
  405. switch (subtype)
  406. {
  407. case GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE:
  408. grub_printf ("/EndEntire\n");
  409. //grub_putchar ('\n');
  410. break;
  411. case GRUB_EFI_END_THIS_DEVICE_PATH_SUBTYPE:
  412. grub_printf ("/EndThis\n");
  413. //grub_putchar ('\n');
  414. break;
  415. default:
  416. grub_printf ("/EndUnknown(%x)\n", (unsigned) subtype);
  417. break;
  418. }
  419. break;
  420. case GRUB_EFI_HARDWARE_DEVICE_PATH_TYPE:
  421. switch (subtype)
  422. {
  423. case GRUB_EFI_PCI_DEVICE_PATH_SUBTYPE:
  424. {
  425. grub_efi_pci_device_path_t *pci
  426. = (grub_efi_pci_device_path_t *) dp;
  427. grub_printf ("/PCI(%x,%x)",
  428. (unsigned) pci->function, (unsigned) pci->device);
  429. }
  430. break;
  431. case GRUB_EFI_PCCARD_DEVICE_PATH_SUBTYPE:
  432. {
  433. grub_efi_pccard_device_path_t *pccard
  434. = (grub_efi_pccard_device_path_t *) dp;
  435. grub_printf ("/PCCARD(%x)",
  436. (unsigned) pccard->function);
  437. }
  438. break;
  439. case GRUB_EFI_MEMORY_MAPPED_DEVICE_PATH_SUBTYPE:
  440. {
  441. grub_efi_memory_mapped_device_path_t *mmapped
  442. = (grub_efi_memory_mapped_device_path_t *) dp;
  443. grub_printf ("/MMap(%x,%llx,%llx)",
  444. (unsigned) mmapped->memory_type,
  445. (unsigned long long) mmapped->start_address,
  446. (unsigned long long) mmapped->end_address);
  447. }
  448. break;
  449. case GRUB_EFI_VENDOR_DEVICE_PATH_SUBTYPE:
  450. dump_vendor_path ("Hardware",
  451. (grub_efi_vendor_device_path_t *) dp);
  452. break;
  453. case GRUB_EFI_CONTROLLER_DEVICE_PATH_SUBTYPE:
  454. {
  455. grub_efi_controller_device_path_t *controller
  456. = (grub_efi_controller_device_path_t *) dp;
  457. grub_printf ("/Ctrl(%x)",
  458. (unsigned) controller->controller_number);
  459. }
  460. break;
  461. default:
  462. grub_printf ("/UnknownHW(%x)", (unsigned) subtype);
  463. break;
  464. }
  465. break;
  466. case GRUB_EFI_ACPI_DEVICE_PATH_TYPE:
  467. switch (subtype)
  468. {
  469. case GRUB_EFI_ACPI_DEVICE_PATH_SUBTYPE:
  470. {
  471. grub_efi_acpi_device_path_t *acpi
  472. = (grub_efi_acpi_device_path_t *) dp;
  473. grub_printf ("/ACPI(%x,%x)",
  474. (unsigned) acpi->hid,
  475. (unsigned) acpi->uid);
  476. }
  477. break;
  478. case GRUB_EFI_EXPANDED_ACPI_DEVICE_PATH_SUBTYPE:
  479. {
  480. grub_efi_expanded_acpi_device_path_t *eacpi
  481. = (grub_efi_expanded_acpi_device_path_t *) dp;
  482. grub_printf ("/ACPI(");
  483. if (GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp)[0] == '\0')
  484. grub_printf ("%x,", (unsigned) eacpi->hid);
  485. else
  486. grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_HIDSTR (dp));
  487. if (GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp)[0] == '\0')
  488. grub_printf ("%x,", (unsigned) eacpi->uid);
  489. else
  490. grub_printf ("%s,", GRUB_EFI_EXPANDED_ACPI_UIDSTR (dp));
  491. if (GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp)[0] == '\0')
  492. grub_printf ("%x)", (unsigned) eacpi->cid);
  493. else
  494. grub_printf ("%s)", GRUB_EFI_EXPANDED_ACPI_CIDSTR (dp));
  495. }
  496. break;
  497. default:
  498. grub_printf ("/UnknownACPI(%x)", (unsigned) subtype);
  499. break;
  500. }
  501. break;
  502. case GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE:
  503. switch (subtype)
  504. {
  505. case GRUB_EFI_ATAPI_DEVICE_PATH_SUBTYPE:
  506. {
  507. grub_efi_atapi_device_path_t *atapi
  508. = (grub_efi_atapi_device_path_t *) dp;
  509. grub_printf ("/ATAPI(%x,%x,%x)",
  510. (unsigned) atapi->primary_secondary,
  511. (unsigned) atapi->slave_master,
  512. (unsigned) atapi->lun);
  513. }
  514. break;
  515. case GRUB_EFI_SCSI_DEVICE_PATH_SUBTYPE:
  516. {
  517. grub_efi_scsi_device_path_t *scsi
  518. = (grub_efi_scsi_device_path_t *) dp;
  519. grub_printf ("/SCSI(%x,%x)",
  520. (unsigned) scsi->pun,
  521. (unsigned) scsi->lun);
  522. }
  523. break;
  524. case GRUB_EFI_FIBRE_CHANNEL_DEVICE_PATH_SUBTYPE:
  525. {
  526. grub_efi_fibre_channel_device_path_t *fc
  527. = (grub_efi_fibre_channel_device_path_t *) dp;
  528. grub_printf ("/FibreChannel(%llx,%llx)",
  529. (unsigned long long) fc->wwn,
  530. (unsigned long long) fc->lun);
  531. }
  532. break;
  533. case GRUB_EFI_1394_DEVICE_PATH_SUBTYPE:
  534. {
  535. grub_efi_1394_device_path_t *firewire
  536. = (grub_efi_1394_device_path_t *) dp;
  537. grub_printf ("/1394(%llx)",
  538. (unsigned long long) firewire->guid);
  539. }
  540. break;
  541. case GRUB_EFI_USB_DEVICE_PATH_SUBTYPE:
  542. {
  543. grub_efi_usb_device_path_t *usb
  544. = (grub_efi_usb_device_path_t *) dp;
  545. grub_printf ("/USB(%x,%x)",
  546. (unsigned) usb->parent_port_number,
  547. (unsigned) usb->usb_interface);
  548. }
  549. break;
  550. case GRUB_EFI_USB_CLASS_DEVICE_PATH_SUBTYPE:
  551. {
  552. grub_efi_usb_class_device_path_t *usb_class
  553. = (grub_efi_usb_class_device_path_t *) dp;
  554. grub_printf ("/USBClass(%x,%x,%x,%x,%x)",
  555. (unsigned) usb_class->vendor_id,
  556. (unsigned) usb_class->product_id,
  557. (unsigned) usb_class->device_class,
  558. (unsigned) usb_class->device_subclass,
  559. (unsigned) usb_class->device_protocol);
  560. }
  561. break;
  562. case GRUB_EFI_I2O_DEVICE_PATH_SUBTYPE:
  563. {
  564. grub_efi_i2o_device_path_t *i2o
  565. = (grub_efi_i2o_device_path_t *) dp;
  566. grub_printf ("/I2O(%x)", (unsigned) i2o->tid);
  567. }
  568. break;
  569. case GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE:
  570. {
  571. grub_efi_mac_address_device_path_t *mac
  572. = (grub_efi_mac_address_device_path_t *) dp;
  573. grub_printf ("/MacAddr(%02x:%02x:%02x:%02x:%02x:%02x,%x)",
  574. (unsigned) mac->mac_address[0],
  575. (unsigned) mac->mac_address[1],
  576. (unsigned) mac->mac_address[2],
  577. (unsigned) mac->mac_address[3],
  578. (unsigned) mac->mac_address[4],
  579. (unsigned) mac->mac_address[5],
  580. (unsigned) mac->if_type);
  581. }
  582. break;
  583. case GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE:
  584. {
  585. grub_efi_ipv4_device_path_t *ipv4
  586. = (grub_efi_ipv4_device_path_t *) dp;
  587. grub_printf ("/IPv4(%u.%u.%u.%u,%u.%u.%u.%u,%u,%u,%x,%x)",
  588. (unsigned) ipv4->local_ip_address[0],
  589. (unsigned) ipv4->local_ip_address[1],
  590. (unsigned) ipv4->local_ip_address[2],
  591. (unsigned) ipv4->local_ip_address[3],
  592. (unsigned) ipv4->remote_ip_address[0],
  593. (unsigned) ipv4->remote_ip_address[1],
  594. (unsigned) ipv4->remote_ip_address[2],
  595. (unsigned) ipv4->remote_ip_address[3],
  596. (unsigned) ipv4->local_port,
  597. (unsigned) ipv4->remote_port,
  598. (unsigned) ipv4->protocol,
  599. (unsigned) ipv4->static_ip_address);
  600. }
  601. break;
  602. case GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE:
  603. {
  604. grub_efi_ipv6_device_path_t *ipv6
  605. = (grub_efi_ipv6_device_path_t *) dp;
  606. grub_printf ("/IPv6(%x:%x:%x:%x:%x:%x:%x:%x,%x:%x:%x:%x:%x:%x:%x:%x,%u,%u,%x,%x)",
  607. (unsigned) ipv6->local_ip_address[0],
  608. (unsigned) ipv6->local_ip_address[1],
  609. (unsigned) ipv6->local_ip_address[2],
  610. (unsigned) ipv6->local_ip_address[3],
  611. (unsigned) ipv6->local_ip_address[4],
  612. (unsigned) ipv6->local_ip_address[5],
  613. (unsigned) ipv6->local_ip_address[6],
  614. (unsigned) ipv6->local_ip_address[7],
  615. (unsigned) ipv6->remote_ip_address[0],
  616. (unsigned) ipv6->remote_ip_address[1],
  617. (unsigned) ipv6->remote_ip_address[2],
  618. (unsigned) ipv6->remote_ip_address[3],
  619. (unsigned) ipv6->remote_ip_address[4],
  620. (unsigned) ipv6->remote_ip_address[5],
  621. (unsigned) ipv6->remote_ip_address[6],
  622. (unsigned) ipv6->remote_ip_address[7],
  623. (unsigned) ipv6->local_port,
  624. (unsigned) ipv6->remote_port,
  625. (unsigned) ipv6->protocol,
  626. (unsigned) ipv6->static_ip_address);
  627. }
  628. break;
  629. case GRUB_EFI_INFINIBAND_DEVICE_PATH_SUBTYPE:
  630. {
  631. grub_efi_infiniband_device_path_t *ib
  632. = (grub_efi_infiniband_device_path_t *) dp;
  633. grub_printf ("/InfiniBand(%x,%llx,%llx,%llx)",
  634. (unsigned) ib->port_gid[0], /* XXX */
  635. (unsigned long long) ib->remote_id,
  636. (unsigned long long) ib->target_port_id,
  637. (unsigned long long) ib->device_id);
  638. }
  639. break;
  640. case GRUB_EFI_UART_DEVICE_PATH_SUBTYPE:
  641. {
  642. grub_efi_uart_device_path_t *uart
  643. = (grub_efi_uart_device_path_t *) dp;
  644. grub_printf ("/UART(%llu,%u,%x,%x)",
  645. (unsigned long long) uart->baud_rate,
  646. uart->data_bits,
  647. uart->parity,
  648. uart->stop_bits);
  649. }
  650. break;
  651. case GRUB_EFI_SATA_DEVICE_PATH_SUBTYPE:
  652. {
  653. grub_efi_sata_device_path_t *sata;
  654. sata = (grub_efi_sata_device_path_t *) dp;
  655. grub_printf ("/Sata(%x,%x,%x)",
  656. sata->hba_port,
  657. sata->multiplier_port,
  658. sata->lun);
  659. }
  660. break;
  661. case GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE:
  662. dump_vendor_path ("Messaging",
  663. (grub_efi_vendor_device_path_t *) dp);
  664. break;
  665. default:
  666. grub_printf ("/UnknownMessaging(%x)", (unsigned) subtype);
  667. break;
  668. }
  669. break;
  670. case GRUB_EFI_MEDIA_DEVICE_PATH_TYPE:
  671. switch (subtype)
  672. {
  673. case GRUB_EFI_HARD_DRIVE_DEVICE_PATH_SUBTYPE:
  674. {
  675. grub_efi_hard_drive_device_path_t *hd = (grub_efi_hard_drive_device_path_t *) dp;
  676. grub_printf ("/HD(%u,%llx,%llx,%02x%02x%02x%02x%02x%02x%02x%02x,%x,%x)",
  677. hd->partition_number,
  678. (unsigned long long) hd->partition_start,
  679. (unsigned long long) hd->partition_size,
  680. (unsigned) hd->partition_signature[0],
  681. (unsigned) hd->partition_signature[1],
  682. (unsigned) hd->partition_signature[2],
  683. (unsigned) hd->partition_signature[3],
  684. (unsigned) hd->partition_signature[4],
  685. (unsigned) hd->partition_signature[5],
  686. (unsigned) hd->partition_signature[6],
  687. (unsigned) hd->partition_signature[7],
  688. (unsigned) hd->partmap_type,
  689. (unsigned) hd->signature_type);
  690. }
  691. break;
  692. case GRUB_EFI_CDROM_DEVICE_PATH_SUBTYPE:
  693. {
  694. grub_efi_cdrom_device_path_t *cd
  695. = (grub_efi_cdrom_device_path_t *) dp;
  696. grub_printf ("/CD(%u,%llx,%llx)",
  697. cd->boot_entry,
  698. (unsigned long long) cd->partition_start,
  699. (unsigned long long) cd->partition_size);
  700. }
  701. break;
  702. case GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE:
  703. dump_vendor_path ("Media",
  704. (grub_efi_vendor_device_path_t *) dp);
  705. break;
  706. case GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE:
  707. {
  708. grub_efi_file_path_device_path_t *fp;
  709. grub_uint8_t *buf;
  710. fp = (grub_efi_file_path_device_path_t *) dp;
  711. buf = grub_malloc ((len - 4) * 2 + 1);
  712. if (buf)
  713. *grub_utf16_to_utf8 (buf, fp->path_name,
  714. (len - 4) / sizeof (grub_efi_char16_t))
  715. = '\0';
  716. else
  717. grub_errno = GRUB_ERR_NONE;
  718. grub_printf ("/File(%s)", buf);
  719. grub_free (buf);
  720. }
  721. break;
  722. case GRUB_EFI_PROTOCOL_DEVICE_PATH_SUBTYPE:
  723. {
  724. grub_efi_protocol_device_path_t *proto
  725. = (grub_efi_protocol_device_path_t *) dp;
  726. grub_printf ("/Protocol(%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x)",
  727. (unsigned) proto->guid.data1,
  728. (unsigned) proto->guid.data2,
  729. (unsigned) proto->guid.data3,
  730. (unsigned) proto->guid.data4[0],
  731. (unsigned) proto->guid.data4[1],
  732. (unsigned) proto->guid.data4[2],
  733. (unsigned) proto->guid.data4[3],
  734. (unsigned) proto->guid.data4[4],
  735. (unsigned) proto->guid.data4[5],
  736. (unsigned) proto->guid.data4[6],
  737. (unsigned) proto->guid.data4[7]);
  738. }
  739. break;
  740. default:
  741. grub_printf ("/UnknownMedia(%x)", (unsigned) subtype);
  742. break;
  743. }
  744. break;
  745. case GRUB_EFI_BIOS_DEVICE_PATH_TYPE:
  746. switch (subtype)
  747. {
  748. case GRUB_EFI_BIOS_DEVICE_PATH_SUBTYPE:
  749. {
  750. grub_efi_bios_device_path_t *bios
  751. = (grub_efi_bios_device_path_t *) dp;
  752. grub_printf ("/BIOS(%x,%x,%s)",
  753. (unsigned) bios->device_type,
  754. (unsigned) bios->status_flags,
  755. (char *) (dp + 1));
  756. }
  757. break;
  758. default:
  759. grub_printf ("/UnknownBIOS(%x)", (unsigned) subtype);
  760. break;
  761. }
  762. break;
  763. default:
  764. grub_printf ("/UnknownType(%x,%x)\n",
  765. (unsigned) type,
  766. (unsigned) subtype);
  767. return;
  768. break;
  769. }
  770. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp))
  771. break;
  772. dp = (grub_efi_device_path_t *) ((char *) dp + len);
  773. }
  774. }
  775. /* Compare device paths. */
  776. int
  777. grub_efi_compare_device_paths (const grub_efi_device_path_t *dp1,
  778. const grub_efi_device_path_t *dp2)
  779. {
  780. if (! dp1 || ! dp2)
  781. /* Return non-zero. */
  782. return 1;
  783. while (1)
  784. {
  785. grub_efi_uint8_t type1, type2;
  786. grub_efi_uint8_t subtype1, subtype2;
  787. grub_efi_uint16_t len1, len2;
  788. int ret;
  789. type1 = GRUB_EFI_DEVICE_PATH_TYPE (dp1);
  790. type2 = GRUB_EFI_DEVICE_PATH_TYPE (dp2);
  791. if (type1 != type2)
  792. return (int) type2 - (int) type1;
  793. subtype1 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp1);
  794. subtype2 = GRUB_EFI_DEVICE_PATH_SUBTYPE (dp2);
  795. if (subtype1 != subtype2)
  796. return (int) subtype1 - (int) subtype2;
  797. len1 = GRUB_EFI_DEVICE_PATH_LENGTH (dp1);
  798. len2 = GRUB_EFI_DEVICE_PATH_LENGTH (dp2);
  799. if (len1 != len2)
  800. return (int) len1 - (int) len2;
  801. ret = grub_memcmp (dp1, dp2, len1);
  802. if (ret != 0)
  803. return ret;
  804. if (GRUB_EFI_END_ENTIRE_DEVICE_PATH (dp1))
  805. break;
  806. dp1 = (grub_efi_device_path_t *) ((char *) dp1 + len1);
  807. dp2 = (grub_efi_device_path_t *) ((char *) dp2 + len2);
  808. }
  809. return 0;
  810. }