efi_bootmgr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * EFI utils
  3. *
  4. * Copyright (c) 2017 Rob Clark
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <charset.h>
  10. #include <malloc.h>
  11. #include <efi_loader.h>
  12. static const struct efi_boot_services *bs;
  13. static const struct efi_runtime_services *rs;
  14. #define LOAD_OPTION_ACTIVE 0x00000001
  15. #define LOAD_OPTION_FORCE_RECONNECT 0x00000002
  16. #define LOAD_OPTION_HIDDEN 0x00000008
  17. /*
  18. * bootmgr implements the logic of trying to find a payload to boot
  19. * based on the BootOrder + BootXXXX variables, and then loading it.
  20. *
  21. * TODO detecting a special key held (f9?) and displaying a boot menu
  22. * like you would get on a PC would be clever.
  23. *
  24. * TODO if we had a way to write and persist variables after the OS
  25. * has started, we'd also want to check OsIndications to see if we
  26. * should do normal or recovery boot.
  27. */
  28. /*
  29. * See section 3.1.3 in the v2.7 UEFI spec for more details on
  30. * the layout of EFI_LOAD_OPTION. In short it is:
  31. *
  32. * typedef struct _EFI_LOAD_OPTION {
  33. * UINT32 Attributes;
  34. * UINT16 FilePathListLength;
  35. * // CHAR16 Description[]; <-- variable length, NULL terminated
  36. * // EFI_DEVICE_PATH_PROTOCOL FilePathList[]; <-- FilePathListLength bytes
  37. * // UINT8 OptionalData[];
  38. * } EFI_LOAD_OPTION;
  39. */
  40. struct load_option {
  41. u32 attributes;
  42. u16 file_path_length;
  43. u16 *label;
  44. struct efi_device_path *file_path;
  45. u8 *optional_data;
  46. };
  47. /* parse an EFI_LOAD_OPTION, as described above */
  48. static void parse_load_option(struct load_option *lo, void *ptr)
  49. {
  50. lo->attributes = *(u32 *)ptr;
  51. ptr += sizeof(u32);
  52. lo->file_path_length = *(u16 *)ptr;
  53. ptr += sizeof(u16);
  54. lo->label = ptr;
  55. ptr += (utf16_strlen(lo->label) + 1) * 2;
  56. lo->file_path = ptr;
  57. ptr += lo->file_path_length;
  58. lo->optional_data = ptr;
  59. }
  60. /* free() the result */
  61. static void *get_var(u16 *name, const efi_guid_t *vendor,
  62. unsigned long *size)
  63. {
  64. efi_guid_t *v = (efi_guid_t *)vendor;
  65. efi_status_t ret;
  66. void *buf = NULL;
  67. *size = 0;
  68. EFI_CALL(ret = rs->get_variable((s16 *)name, v, NULL, size, buf));
  69. if (ret == EFI_BUFFER_TOO_SMALL) {
  70. buf = malloc(*size);
  71. EFI_CALL(ret = rs->get_variable((s16 *)name, v, NULL, size, buf));
  72. }
  73. if (ret != EFI_SUCCESS) {
  74. free(buf);
  75. *size = 0;
  76. return NULL;
  77. }
  78. return buf;
  79. }
  80. /*
  81. * Attempt to load load-option number 'n', returning device_path and file_path
  82. * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
  83. * and that the specified file to boot exists.
  84. */
  85. static void *try_load_entry(uint16_t n, struct efi_device_path **device_path,
  86. struct efi_device_path **file_path)
  87. {
  88. struct load_option lo;
  89. u16 varname[] = L"Boot0000";
  90. u16 hexmap[] = L"0123456789ABCDEF";
  91. void *load_option, *image = NULL;
  92. unsigned long size;
  93. varname[4] = hexmap[(n & 0xf000) >> 12];
  94. varname[5] = hexmap[(n & 0x0f00) >> 8];
  95. varname[6] = hexmap[(n & 0x00f0) >> 4];
  96. varname[7] = hexmap[(n & 0x000f) >> 0];
  97. load_option = get_var(varname, &efi_global_variable_guid, &size);
  98. if (!load_option)
  99. return NULL;
  100. parse_load_option(&lo, load_option);
  101. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  102. efi_status_t ret;
  103. u16 *str = NULL;
  104. debug("%s: trying to load \"%ls\" from: %ls\n", __func__,
  105. lo.label, (str = efi_dp_str(lo.file_path)));
  106. efi_free_pool(str);
  107. ret = efi_load_image_from_path(lo.file_path, &image);
  108. if (ret != EFI_SUCCESS)
  109. goto error;
  110. printf("Booting: %ls\n", lo.label);
  111. efi_dp_split_file_path(lo.file_path, device_path, file_path);
  112. }
  113. error:
  114. free(load_option);
  115. return image;
  116. }
  117. /*
  118. * Attempt to load, in the order specified by BootOrder EFI variable, the
  119. * available load-options, finding and returning the first one that can
  120. * be loaded successfully.
  121. */
  122. void *efi_bootmgr_load(struct efi_device_path **device_path,
  123. struct efi_device_path **file_path)
  124. {
  125. uint16_t *bootorder;
  126. unsigned long size;
  127. void *image = NULL;
  128. int i, num;
  129. __efi_entry_check();
  130. bs = systab.boottime;
  131. rs = systab.runtime;
  132. bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
  133. if (!bootorder)
  134. goto error;
  135. num = size / sizeof(uint16_t);
  136. for (i = 0; i < num; i++) {
  137. debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
  138. image = try_load_entry(bootorder[i], device_path, file_path);
  139. if (image)
  140. break;
  141. }
  142. free(bootorder);
  143. error:
  144. __efi_exit_check();
  145. return image;
  146. }