efi_bootmgr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI boot manager
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #define LOG_CATEGORY LOGC_EFI
  8. #include <common.h>
  9. #include <charset.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <efi_default_filename.h>
  13. #include <efi_loader.h>
  14. #include <efi_variable.h>
  15. #include <asm/unaligned.h>
  16. static const struct efi_boot_services *bs;
  17. static const struct efi_runtime_services *rs;
  18. const efi_guid_t efi_guid_bootmenu_auto_generated =
  19. EFICONFIG_AUTO_GENERATED_ENTRY_GUID;
  20. /*
  21. * bootmgr implements the logic of trying to find a payload to boot
  22. * based on the BootOrder + BootXXXX variables, and then loading it.
  23. *
  24. * TODO detecting a special key held (f9?) and displaying a boot menu
  25. * like you would get on a PC would be clever.
  26. *
  27. * TODO if we had a way to write and persist variables after the OS
  28. * has started, we'd also want to check OsIndications to see if we
  29. * should do normal or recovery boot.
  30. */
  31. /**
  32. * expand_media_path() - expand a device path for default file name
  33. * @device_path: device path to check against
  34. *
  35. * If @device_path is a media or disk partition which houses a file
  36. * system, this function returns a full device path which contains
  37. * an architecture-specific default file name for removable media.
  38. *
  39. * Return: a newly allocated device path
  40. */
  41. static
  42. struct efi_device_path *expand_media_path(struct efi_device_path *device_path)
  43. {
  44. struct efi_device_path *rem, *full_path;
  45. efi_handle_t handle;
  46. if (!device_path)
  47. return NULL;
  48. /*
  49. * If device_path is a (removable) media or partition which provides
  50. * simple file system protocol, append a default file name to support
  51. * booting from removable media.
  52. */
  53. handle = efi_dp_find_obj(device_path,
  54. &efi_simple_file_system_protocol_guid, &rem);
  55. if (handle) {
  56. if (rem->type == DEVICE_PATH_TYPE_END) {
  57. full_path = efi_dp_from_file(device_path,
  58. "/EFI/BOOT/" BOOTEFI_NAME);
  59. } else {
  60. full_path = efi_dp_dup(device_path);
  61. }
  62. } else {
  63. full_path = efi_dp_dup(device_path);
  64. }
  65. return full_path;
  66. }
  67. /**
  68. * try_load_from_file_path() - try to load a file
  69. *
  70. * Given a file media path iterate through a list of handles and try to
  71. * to load the file from each of them until the first success.
  72. *
  73. * @fs_handles: array of handles with the simple file protocol
  74. * @num: number of handles in fs_handles
  75. * @fp: file path to open
  76. * @handle: on return pointer to handle for loaded image
  77. * @removable: if true only consider removable media, else only non-removable
  78. */
  79. static efi_status_t try_load_from_file_path(efi_handle_t *fs_handles,
  80. efi_uintn_t num,
  81. struct efi_device_path *fp,
  82. efi_handle_t *handle,
  83. bool removable)
  84. {
  85. struct efi_handler *handler;
  86. struct efi_device_path *dp;
  87. int i;
  88. efi_status_t ret;
  89. for (i = 0; i < num; i++) {
  90. if (removable != efi_disk_is_removable(fs_handles[i]))
  91. continue;
  92. ret = efi_search_protocol(fs_handles[i], &efi_guid_device_path,
  93. &handler);
  94. if (ret != EFI_SUCCESS)
  95. continue;
  96. dp = handler->protocol_interface;
  97. if (!dp)
  98. continue;
  99. dp = efi_dp_append(dp, fp);
  100. if (!dp)
  101. continue;
  102. ret = EFI_CALL(efi_load_image(true, efi_root, dp, NULL, 0,
  103. handle));
  104. efi_free_pool(dp);
  105. if (ret == EFI_SUCCESS)
  106. return ret;
  107. }
  108. return EFI_NOT_FOUND;
  109. }
  110. /**
  111. * try_load_from_short_path
  112. * @fp: file path
  113. * @handle: pointer to handle for newly installed image
  114. *
  115. * Enumerate all the devices which support file system operations,
  116. * prepend its media device path to the file path, @fp, and
  117. * try to load the file.
  118. * This function should be called when handling a short-form path
  119. * which is starting with a file device path.
  120. *
  121. * Return: status code
  122. */
  123. static efi_status_t try_load_from_short_path(struct efi_device_path *fp,
  124. efi_handle_t *handle)
  125. {
  126. efi_handle_t *fs_handles;
  127. efi_uintn_t num;
  128. efi_status_t ret;
  129. ret = EFI_CALL(efi_locate_handle_buffer(
  130. BY_PROTOCOL,
  131. &efi_simple_file_system_protocol_guid,
  132. NULL,
  133. &num, &fs_handles));
  134. if (ret != EFI_SUCCESS)
  135. return ret;
  136. if (!num)
  137. return EFI_NOT_FOUND;
  138. /* removable media first */
  139. ret = try_load_from_file_path(fs_handles, num, fp, handle, true);
  140. if (ret == EFI_SUCCESS)
  141. goto out;
  142. /* fixed media */
  143. ret = try_load_from_file_path(fs_handles, num, fp, handle, false);
  144. if (ret == EFI_SUCCESS)
  145. goto out;
  146. out:
  147. return ret;
  148. }
  149. /**
  150. * try_load_entry() - try to load image for boot option
  151. *
  152. * Attempt to load load-option number 'n', returning device_path and file_path
  153. * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
  154. * and that the specified file to boot exists.
  155. *
  156. * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
  157. * @handle: on return handle for the newly installed image
  158. * @load_options: load options set on the loaded image protocol
  159. * Return: status code
  160. */
  161. static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
  162. void **load_options)
  163. {
  164. struct efi_load_option lo;
  165. u16 varname[9];
  166. void *load_option;
  167. efi_uintn_t size;
  168. efi_status_t ret;
  169. efi_create_indexed_name(varname, sizeof(varname), "Boot", n);
  170. load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
  171. if (!load_option)
  172. return EFI_LOAD_ERROR;
  173. ret = efi_deserialize_load_option(&lo, load_option, &size);
  174. if (ret != EFI_SUCCESS) {
  175. log_warning("Invalid load option for %ls\n", varname);
  176. goto error;
  177. }
  178. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  179. struct efi_device_path *file_path;
  180. u32 attributes;
  181. log_debug("trying to load \"%ls\" from %pD\n", lo.label,
  182. lo.file_path);
  183. if (EFI_DP_TYPE(lo.file_path, MEDIA_DEVICE, FILE_PATH)) {
  184. /* file_path doesn't contain a device path */
  185. ret = try_load_from_short_path(lo.file_path, handle);
  186. } else {
  187. file_path = expand_media_path(lo.file_path);
  188. ret = EFI_CALL(efi_load_image(true, efi_root, file_path,
  189. NULL, 0, handle));
  190. efi_free_pool(file_path);
  191. }
  192. if (ret != EFI_SUCCESS) {
  193. log_warning("Loading %ls '%ls' failed\n",
  194. varname, lo.label);
  195. goto error;
  196. }
  197. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  198. EFI_VARIABLE_RUNTIME_ACCESS;
  199. ret = efi_set_variable_int(u"BootCurrent",
  200. &efi_global_variable_guid,
  201. attributes, sizeof(n), &n, false);
  202. if (ret != EFI_SUCCESS)
  203. goto unload;
  204. /* try to register load file2 for initrd's */
  205. if (IS_ENABLED(CONFIG_EFI_LOAD_FILE2_INITRD)) {
  206. ret = efi_initrd_register();
  207. if (ret != EFI_SUCCESS)
  208. goto unload;
  209. }
  210. log_info("Booting: %ls\n", lo.label);
  211. } else {
  212. ret = EFI_LOAD_ERROR;
  213. }
  214. /* Set load options */
  215. if (size >= sizeof(efi_guid_t) &&
  216. !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated))
  217. size = 0;
  218. if (size) {
  219. *load_options = malloc(size);
  220. if (!*load_options) {
  221. ret = EFI_OUT_OF_RESOURCES;
  222. goto error;
  223. }
  224. memcpy(*load_options, lo.optional_data, size);
  225. ret = efi_set_load_options(*handle, size, *load_options);
  226. } else {
  227. *load_options = NULL;
  228. }
  229. error:
  230. free(load_option);
  231. return ret;
  232. unload:
  233. if (EFI_CALL(efi_unload_image(*handle)) != EFI_SUCCESS)
  234. log_err("Unloading image failed\n");
  235. free(load_option);
  236. return ret;
  237. }
  238. /**
  239. * efi_bootmgr_load() - try to load from BootNext or BootOrder
  240. *
  241. * Attempt to load from BootNext or in the order specified by BootOrder
  242. * EFI variable, the available load-options, finding and returning
  243. * the first one that can be loaded successfully.
  244. *
  245. * @handle: on return handle for the newly installed image
  246. * @load_options: load options set on the loaded image protocol
  247. * Return: status code
  248. */
  249. efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
  250. {
  251. u16 bootnext, *bootorder;
  252. efi_uintn_t size;
  253. int i, num;
  254. efi_status_t ret;
  255. bs = systab.boottime;
  256. rs = systab.runtime;
  257. /* BootNext */
  258. size = sizeof(bootnext);
  259. ret = efi_get_variable_int(u"BootNext",
  260. &efi_global_variable_guid,
  261. NULL, &size, &bootnext, NULL);
  262. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  263. /* BootNext does exist here */
  264. if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
  265. log_err("BootNext must be 16-bit integer\n");
  266. /* delete BootNext */
  267. ret = efi_set_variable_int(u"BootNext",
  268. &efi_global_variable_guid,
  269. 0, 0, NULL, false);
  270. /* load BootNext */
  271. if (ret == EFI_SUCCESS) {
  272. if (size == sizeof(u16)) {
  273. ret = try_load_entry(bootnext, handle,
  274. load_options);
  275. if (ret == EFI_SUCCESS)
  276. return ret;
  277. log_warning(
  278. "Loading from BootNext failed, falling back to BootOrder\n");
  279. }
  280. } else {
  281. log_err("Deleting BootNext failed\n");
  282. }
  283. }
  284. /* BootOrder */
  285. bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
  286. if (!bootorder) {
  287. log_info("BootOrder not defined\n");
  288. ret = EFI_NOT_FOUND;
  289. goto error;
  290. }
  291. num = size / sizeof(uint16_t);
  292. for (i = 0; i < num; i++) {
  293. log_debug("trying to load Boot%04X\n", bootorder[i]);
  294. ret = try_load_entry(bootorder[i], handle, load_options);
  295. if (ret == EFI_SUCCESS)
  296. break;
  297. }
  298. free(bootorder);
  299. error:
  300. return ret;
  301. }
  302. /**
  303. * efi_bootmgr_enumerate_boot_option() - enumerate the possible bootable media
  304. *
  305. * @opt: pointer to the media boot option structure
  306. * @volume_handles: pointer to the efi handles
  307. * @count: number of efi handle
  308. * Return: status code
  309. */
  310. static efi_status_t efi_bootmgr_enumerate_boot_option(struct eficonfig_media_boot_option *opt,
  311. efi_handle_t *volume_handles,
  312. efi_status_t count)
  313. {
  314. u32 i;
  315. struct efi_handler *handler;
  316. efi_status_t ret = EFI_SUCCESS;
  317. for (i = 0; i < count; i++) {
  318. u16 *p;
  319. u16 dev_name[BOOTMENU_DEVICE_NAME_MAX];
  320. char *optional_data;
  321. struct efi_load_option lo;
  322. char buf[BOOTMENU_DEVICE_NAME_MAX];
  323. struct efi_device_path *device_path;
  324. struct efi_device_path *short_dp;
  325. ret = efi_search_protocol(volume_handles[i], &efi_guid_device_path, &handler);
  326. if (ret != EFI_SUCCESS)
  327. continue;
  328. ret = efi_protocol_open(handler, (void **)&device_path,
  329. efi_root, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  330. if (ret != EFI_SUCCESS)
  331. continue;
  332. ret = efi_disk_get_device_name(volume_handles[i], buf, BOOTMENU_DEVICE_NAME_MAX);
  333. if (ret != EFI_SUCCESS)
  334. continue;
  335. p = dev_name;
  336. utf8_utf16_strncpy(&p, buf, strlen(buf));
  337. /* prefer to short form device path */
  338. short_dp = efi_dp_shorten(device_path);
  339. if (short_dp)
  340. device_path = short_dp;
  341. lo.label = dev_name;
  342. lo.attributes = LOAD_OPTION_ACTIVE;
  343. lo.file_path = device_path;
  344. lo.file_path_length = efi_dp_size(device_path) + sizeof(END);
  345. /*
  346. * Set the dedicated guid to optional_data, it is used to identify
  347. * the boot option that automatically generated by the bootmenu.
  348. * efi_serialize_load_option() expects optional_data is null-terminated
  349. * utf8 string, so set the "1234567" string to allocate enough space
  350. * to store guid, instead of realloc the load_option.
  351. */
  352. lo.optional_data = "1234567";
  353. opt[i].size = efi_serialize_load_option(&lo, (u8 **)&opt[i].lo);
  354. if (!opt[i].size) {
  355. ret = EFI_OUT_OF_RESOURCES;
  356. goto out;
  357. }
  358. /* set the guid */
  359. optional_data = (char *)opt[i].lo + (opt[i].size - u16_strsize(u"1234567"));
  360. memcpy(optional_data, &efi_guid_bootmenu_auto_generated, sizeof(efi_guid_t));
  361. }
  362. out:
  363. return ret;
  364. }
  365. /**
  366. * efi_bootmgr_delete_invalid_boot_option() - delete non-existing boot option
  367. *
  368. * @opt: pointer to the media boot option structure
  369. * @count: number of media boot option structure
  370. * Return: status code
  371. */
  372. static efi_status_t efi_bootmgr_delete_invalid_boot_option(struct eficonfig_media_boot_option *opt,
  373. efi_status_t count)
  374. {
  375. efi_uintn_t size;
  376. void *load_option;
  377. u32 i, list_size = 0;
  378. struct efi_load_option lo;
  379. u16 *var_name16 = NULL;
  380. u16 varname[] = u"Boot####";
  381. efi_status_t ret = EFI_SUCCESS;
  382. u16 *delete_index_list = NULL, *p;
  383. efi_uintn_t buf_size;
  384. buf_size = 128;
  385. var_name16 = malloc(buf_size);
  386. if (!var_name16)
  387. return EFI_OUT_OF_RESOURCES;
  388. var_name16[0] = 0;
  389. for (;;) {
  390. int index;
  391. efi_guid_t guid;
  392. efi_uintn_t tmp;
  393. ret = efi_next_variable_name(&buf_size, &var_name16, &guid);
  394. if (ret == EFI_NOT_FOUND) {
  395. /*
  396. * EFI_NOT_FOUND indicates we retrieved all EFI variables.
  397. * This should be treated as success.
  398. */
  399. ret = EFI_SUCCESS;
  400. break;
  401. }
  402. if (ret != EFI_SUCCESS)
  403. goto out;
  404. if (!efi_varname_is_load_option(var_name16, &index))
  405. continue;
  406. efi_create_indexed_name(varname, sizeof(varname), "Boot", index);
  407. load_option = efi_get_var(varname, &efi_global_variable_guid, &size);
  408. if (!load_option)
  409. continue;
  410. tmp = size;
  411. ret = efi_deserialize_load_option(&lo, load_option, &size);
  412. if (ret != EFI_SUCCESS)
  413. goto next;
  414. if (size >= sizeof(efi_guid_bootmenu_auto_generated) &&
  415. !guidcmp(lo.optional_data, &efi_guid_bootmenu_auto_generated)) {
  416. for (i = 0; i < count; i++) {
  417. if (opt[i].size == tmp &&
  418. memcmp(opt[i].lo, load_option, tmp) == 0) {
  419. opt[i].exist = true;
  420. break;
  421. }
  422. }
  423. /*
  424. * The entire list of variables must be retrieved by
  425. * efi_get_next_variable_name_int() before deleting the invalid
  426. * boot option, just save the index here.
  427. */
  428. if (i == count) {
  429. p = realloc(delete_index_list, sizeof(u32) *
  430. (list_size + 1));
  431. if (!p) {
  432. ret = EFI_OUT_OF_RESOURCES;
  433. goto out;
  434. }
  435. delete_index_list = p;
  436. delete_index_list[list_size++] = index;
  437. }
  438. }
  439. next:
  440. free(load_option);
  441. }
  442. /* delete all invalid boot options */
  443. for (i = 0; i < list_size; i++) {
  444. ret = efi_bootmgr_delete_boot_option(delete_index_list[i]);
  445. if (ret != EFI_SUCCESS)
  446. goto out;
  447. }
  448. out:
  449. free(var_name16);
  450. free(delete_index_list);
  451. return ret;
  452. }
  453. /**
  454. * efi_bootmgr_get_unused_bootoption() - get unused "Boot####" index
  455. *
  456. * @buf: pointer to the buffer to store boot option variable name
  457. * @buf_size: buffer size
  458. * @index: pointer to store the index in the BootOrder variable
  459. * Return: status code
  460. */
  461. efi_status_t efi_bootmgr_get_unused_bootoption(u16 *buf, efi_uintn_t buf_size,
  462. unsigned int *index)
  463. {
  464. u32 i;
  465. efi_status_t ret;
  466. efi_uintn_t size;
  467. if (buf_size < u16_strsize(u"Boot####"))
  468. return EFI_BUFFER_TOO_SMALL;
  469. for (i = 0; i <= 0xFFFF; i++) {
  470. size = 0;
  471. efi_create_indexed_name(buf, buf_size, "Boot", i);
  472. ret = efi_get_variable_int(buf, &efi_global_variable_guid,
  473. NULL, &size, NULL, NULL);
  474. if (ret == EFI_BUFFER_TOO_SMALL)
  475. continue;
  476. else
  477. break;
  478. }
  479. if (i > 0xFFFF)
  480. return EFI_OUT_OF_RESOURCES;
  481. *index = i;
  482. return EFI_SUCCESS;
  483. }
  484. /**
  485. * efi_bootmgr_append_bootorder() - append new boot option in BootOrder variable
  486. *
  487. * @index: "Boot####" index to append to BootOrder variable
  488. * Return: status code
  489. */
  490. efi_status_t efi_bootmgr_append_bootorder(u16 index)
  491. {
  492. u16 *bootorder;
  493. efi_status_t ret;
  494. u16 *new_bootorder = NULL;
  495. efi_uintn_t last, size, new_size;
  496. /* append new boot option */
  497. bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
  498. last = size / sizeof(u16);
  499. new_size = size + sizeof(u16);
  500. new_bootorder = calloc(1, new_size);
  501. if (!new_bootorder) {
  502. ret = EFI_OUT_OF_RESOURCES;
  503. goto out;
  504. }
  505. memcpy(new_bootorder, bootorder, size);
  506. new_bootorder[last] = index;
  507. ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
  508. EFI_VARIABLE_NON_VOLATILE |
  509. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  510. EFI_VARIABLE_RUNTIME_ACCESS,
  511. new_size, new_bootorder, false);
  512. if (ret != EFI_SUCCESS)
  513. goto out;
  514. out:
  515. free(bootorder);
  516. free(new_bootorder);
  517. return ret;
  518. }
  519. /**
  520. * efi_bootmgr_delete_boot_option() - delete selected boot option
  521. *
  522. * @boot_index: boot option index to delete
  523. * Return: status code
  524. */
  525. efi_status_t efi_bootmgr_delete_boot_option(u16 boot_index)
  526. {
  527. u16 *bootorder;
  528. u16 varname[9];
  529. efi_status_t ret;
  530. unsigned int index;
  531. efi_uintn_t num, size;
  532. efi_create_indexed_name(varname, sizeof(varname),
  533. "Boot", boot_index);
  534. ret = efi_set_variable_int(varname, &efi_global_variable_guid,
  535. 0, 0, NULL, false);
  536. if (ret != EFI_SUCCESS) {
  537. log_err("delete boot option(%ls) failed\n", varname);
  538. return ret;
  539. }
  540. /* update BootOrder if necessary */
  541. bootorder = efi_get_var(u"BootOrder", &efi_global_variable_guid, &size);
  542. if (!bootorder)
  543. return EFI_SUCCESS;
  544. num = size / sizeof(u16);
  545. if (!efi_search_bootorder(bootorder, num, boot_index, &index))
  546. return EFI_SUCCESS;
  547. memmove(&bootorder[index], &bootorder[index + 1],
  548. (num - index - 1) * sizeof(u16));
  549. size -= sizeof(u16);
  550. ret = efi_set_variable_int(u"BootOrder", &efi_global_variable_guid,
  551. EFI_VARIABLE_NON_VOLATILE |
  552. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  553. EFI_VARIABLE_RUNTIME_ACCESS,
  554. size, bootorder, false);
  555. return ret;
  556. }
  557. /**
  558. * efi_bootmgr_update_media_device_boot_option() - generate the media device boot option
  559. *
  560. * This function enumerates all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
  561. * and generate the bootmenu entries.
  562. * This function also provide the BOOT#### variable maintenance for
  563. * the media device entries.
  564. * - Automatically create the BOOT#### variable for the newly detected device,
  565. * this BOOT#### variable is distinguished by the special GUID
  566. * stored in the EFI_LOAD_OPTION.optional_data
  567. * - If the device is not attached to the system, the associated BOOT#### variable
  568. * is automatically deleted.
  569. *
  570. * Return: status code
  571. */
  572. efi_status_t efi_bootmgr_update_media_device_boot_option(void)
  573. {
  574. u32 i;
  575. efi_status_t ret;
  576. efi_uintn_t count;
  577. efi_handle_t *volume_handles = NULL;
  578. struct eficonfig_media_boot_option *opt = NULL;
  579. ret = efi_locate_handle_buffer_int(BY_PROTOCOL,
  580. &efi_simple_file_system_protocol_guid,
  581. NULL, &count,
  582. (efi_handle_t **)&volume_handles);
  583. if (ret != EFI_SUCCESS)
  584. goto out;
  585. opt = calloc(count, sizeof(struct eficonfig_media_boot_option));
  586. if (!opt) {
  587. ret = EFI_OUT_OF_RESOURCES;
  588. goto out;
  589. }
  590. /* enumerate all devices supporting EFI_SIMPLE_FILE_SYSTEM_PROTOCOL */
  591. ret = efi_bootmgr_enumerate_boot_option(opt, volume_handles, count);
  592. if (ret != EFI_SUCCESS)
  593. goto out;
  594. /*
  595. * System hardware configuration may vary depending on the user setup.
  596. * The boot option is automatically added by the bootmenu.
  597. * If the device is not attached to the system, the boot option needs
  598. * to be deleted.
  599. */
  600. ret = efi_bootmgr_delete_invalid_boot_option(opt, count);
  601. if (ret != EFI_SUCCESS)
  602. goto out;
  603. /* add non-existent boot option */
  604. for (i = 0; i < count; i++) {
  605. u32 boot_index;
  606. u16 var_name[9];
  607. if (!opt[i].exist) {
  608. ret = efi_bootmgr_get_unused_bootoption(var_name, sizeof(var_name),
  609. &boot_index);
  610. if (ret != EFI_SUCCESS)
  611. goto out;
  612. ret = efi_set_variable_int(var_name, &efi_global_variable_guid,
  613. EFI_VARIABLE_NON_VOLATILE |
  614. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  615. EFI_VARIABLE_RUNTIME_ACCESS,
  616. opt[i].size, opt[i].lo, false);
  617. if (ret != EFI_SUCCESS)
  618. goto out;
  619. ret = efi_bootmgr_append_bootorder(boot_index);
  620. if (ret != EFI_SUCCESS) {
  621. efi_set_variable_int(var_name, &efi_global_variable_guid,
  622. 0, 0, NULL, false);
  623. goto out;
  624. }
  625. }
  626. }
  627. out:
  628. if (opt) {
  629. for (i = 0; i < count; i++)
  630. free(opt[i].lo);
  631. }
  632. free(opt);
  633. efi_free_pool(volume_handles);
  634. if (ret == EFI_NOT_FOUND)
  635. return EFI_SUCCESS;
  636. return ret;
  637. }