efi_bootmgr.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_loader.h>
  13. #include <efi_variable.h>
  14. #include <asm/unaligned.h>
  15. static const struct efi_boot_services *bs;
  16. static const struct efi_runtime_services *rs;
  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. * efi_set_load_options() - set the load options of a loaded image
  30. *
  31. * @handle: the image handle
  32. * @load_options_size: size of load options
  33. * @load_options: pointer to load options
  34. * Return: status code
  35. */
  36. efi_status_t efi_set_load_options(efi_handle_t handle,
  37. efi_uintn_t load_options_size,
  38. void *load_options)
  39. {
  40. struct efi_loaded_image *loaded_image_info;
  41. efi_status_t ret;
  42. ret = EFI_CALL(systab.boottime->open_protocol(
  43. handle,
  44. &efi_guid_loaded_image,
  45. (void **)&loaded_image_info,
  46. efi_root, NULL,
  47. EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL));
  48. if (ret != EFI_SUCCESS)
  49. return EFI_INVALID_PARAMETER;
  50. loaded_image_info->load_options = load_options;
  51. loaded_image_info->load_options_size = load_options_size;
  52. return EFI_CALL(systab.boottime->close_protocol(handle,
  53. &efi_guid_loaded_image,
  54. efi_root, NULL));
  55. }
  56. /**
  57. * efi_deserialize_load_option() - parse serialized data
  58. *
  59. * Parse serialized data describing a load option and transform it to the
  60. * efi_load_option structure.
  61. *
  62. * @lo: pointer to target
  63. * @data: serialized data
  64. * @size: size of the load option, on return size of the optional data
  65. * Return: status code
  66. */
  67. efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
  68. efi_uintn_t *size)
  69. {
  70. efi_uintn_t len;
  71. len = sizeof(u32);
  72. if (*size < len + 2 * sizeof(u16))
  73. return EFI_INVALID_PARAMETER;
  74. lo->attributes = get_unaligned_le32(data);
  75. data += len;
  76. *size -= len;
  77. len = sizeof(u16);
  78. lo->file_path_length = get_unaligned_le16(data);
  79. data += len;
  80. *size -= len;
  81. lo->label = (u16 *)data;
  82. len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
  83. if (lo->label[len])
  84. return EFI_INVALID_PARAMETER;
  85. len = (len + 1) * sizeof(u16);
  86. if (*size < len)
  87. return EFI_INVALID_PARAMETER;
  88. data += len;
  89. *size -= len;
  90. len = lo->file_path_length;
  91. if (*size < len)
  92. return EFI_INVALID_PARAMETER;
  93. lo->file_path = (struct efi_device_path *)data;
  94. if (efi_dp_check_length(lo->file_path, len) < 0)
  95. return EFI_INVALID_PARAMETER;
  96. data += len;
  97. *size -= len;
  98. lo->optional_data = data;
  99. return EFI_SUCCESS;
  100. }
  101. /**
  102. * efi_serialize_load_option() - serialize load option
  103. *
  104. * Serialize efi_load_option structure into byte stream for BootXXXX.
  105. *
  106. * @data: buffer for serialized data
  107. * @lo: load option
  108. * Return: size of allocated buffer
  109. */
  110. unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
  111. {
  112. unsigned long label_len;
  113. unsigned long size;
  114. u8 *p;
  115. label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
  116. /* total size */
  117. size = sizeof(lo->attributes);
  118. size += sizeof(lo->file_path_length);
  119. size += label_len;
  120. size += lo->file_path_length;
  121. if (lo->optional_data)
  122. size += (utf8_utf16_strlen((const char *)lo->optional_data)
  123. + 1) * sizeof(u16);
  124. p = malloc(size);
  125. if (!p)
  126. return 0;
  127. /* copy data */
  128. *data = p;
  129. memcpy(p, &lo->attributes, sizeof(lo->attributes));
  130. p += sizeof(lo->attributes);
  131. memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
  132. p += sizeof(lo->file_path_length);
  133. memcpy(p, lo->label, label_len);
  134. p += label_len;
  135. memcpy(p, lo->file_path, lo->file_path_length);
  136. p += lo->file_path_length;
  137. if (lo->optional_data) {
  138. utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
  139. p += sizeof(u16); /* size of trailing \0 */
  140. }
  141. return size;
  142. }
  143. /**
  144. * get_var() - get UEFI variable
  145. *
  146. * It is the caller's duty to free the returned buffer.
  147. *
  148. * @name: name of variable
  149. * @vendor: vendor GUID of variable
  150. * @size: size of allocated buffer
  151. * Return: buffer with variable data or NULL
  152. */
  153. static void *get_var(u16 *name, const efi_guid_t *vendor,
  154. efi_uintn_t *size)
  155. {
  156. efi_status_t ret;
  157. void *buf = NULL;
  158. *size = 0;
  159. ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
  160. if (ret == EFI_BUFFER_TOO_SMALL) {
  161. buf = malloc(*size);
  162. ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
  163. }
  164. if (ret != EFI_SUCCESS) {
  165. free(buf);
  166. *size = 0;
  167. return NULL;
  168. }
  169. return buf;
  170. }
  171. /**
  172. * try_load_entry() - try to load image for boot option
  173. *
  174. * Attempt to load load-option number 'n', returning device_path and file_path
  175. * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
  176. * and that the specified file to boot exists.
  177. *
  178. * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
  179. * @handle: on return handle for the newly installed image
  180. * @load_options: load options set on the loaded image protocol
  181. * Return: status code
  182. */
  183. static efi_status_t try_load_entry(u16 n, efi_handle_t *handle,
  184. void **load_options)
  185. {
  186. struct efi_load_option lo;
  187. u16 varname[] = L"Boot0000";
  188. u16 hexmap[] = L"0123456789ABCDEF";
  189. void *load_option;
  190. efi_uintn_t size;
  191. efi_status_t ret;
  192. varname[4] = hexmap[(n & 0xf000) >> 12];
  193. varname[5] = hexmap[(n & 0x0f00) >> 8];
  194. varname[6] = hexmap[(n & 0x00f0) >> 4];
  195. varname[7] = hexmap[(n & 0x000f) >> 0];
  196. load_option = get_var(varname, &efi_global_variable_guid, &size);
  197. if (!load_option)
  198. return EFI_LOAD_ERROR;
  199. ret = efi_deserialize_load_option(&lo, load_option, &size);
  200. if (ret != EFI_SUCCESS) {
  201. log_warning("Invalid load option for %ls\n", varname);
  202. goto error;
  203. }
  204. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  205. u32 attributes;
  206. log_debug("%s: trying to load \"%ls\" from %pD\n",
  207. __func__, lo.label, lo.file_path);
  208. ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
  209. NULL, 0, handle));
  210. if (ret != EFI_SUCCESS) {
  211. log_warning("Loading %ls '%ls' failed\n",
  212. varname, lo.label);
  213. goto error;
  214. }
  215. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  216. EFI_VARIABLE_RUNTIME_ACCESS;
  217. ret = efi_set_variable_int(L"BootCurrent",
  218. &efi_global_variable_guid,
  219. attributes, sizeof(n), &n, false);
  220. if (ret != EFI_SUCCESS) {
  221. if (EFI_CALL(efi_unload_image(*handle))
  222. != EFI_SUCCESS)
  223. log_err("Unloading image failed\n");
  224. goto error;
  225. }
  226. log_info("Booting: %ls\n", lo.label);
  227. } else {
  228. ret = EFI_LOAD_ERROR;
  229. }
  230. /* Set load options */
  231. if (size) {
  232. *load_options = malloc(size);
  233. if (!*load_options) {
  234. ret = EFI_OUT_OF_RESOURCES;
  235. goto error;
  236. }
  237. memcpy(*load_options, lo.optional_data, size);
  238. ret = efi_set_load_options(*handle, size, *load_options);
  239. } else {
  240. load_options = NULL;
  241. }
  242. error:
  243. free(load_option);
  244. return ret;
  245. }
  246. /**
  247. * efi_bootmgr_load() - try to load from BootNext or BootOrder
  248. *
  249. * Attempt to load from BootNext or in the order specified by BootOrder
  250. * EFI variable, the available load-options, finding and returning
  251. * the first one that can be loaded successfully.
  252. *
  253. * @handle: on return handle for the newly installed image
  254. * @load_options: load options set on the loaded image protocol
  255. * Return: status code
  256. */
  257. efi_status_t efi_bootmgr_load(efi_handle_t *handle, void **load_options)
  258. {
  259. u16 bootnext, *bootorder;
  260. efi_uintn_t size;
  261. int i, num;
  262. efi_status_t ret;
  263. bs = systab.boottime;
  264. rs = systab.runtime;
  265. /* BootNext */
  266. size = sizeof(bootnext);
  267. ret = efi_get_variable_int(L"BootNext",
  268. &efi_global_variable_guid,
  269. NULL, &size, &bootnext, NULL);
  270. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  271. /* BootNext does exist here */
  272. if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
  273. log_err("BootNext must be 16-bit integer\n");
  274. /* delete BootNext */
  275. ret = efi_set_variable_int(L"BootNext",
  276. &efi_global_variable_guid,
  277. 0, 0, NULL, false);
  278. /* load BootNext */
  279. if (ret == EFI_SUCCESS) {
  280. if (size == sizeof(u16)) {
  281. ret = try_load_entry(bootnext, handle,
  282. load_options);
  283. if (ret == EFI_SUCCESS)
  284. return ret;
  285. log_warning(
  286. "Loading from BootNext failed, falling back to BootOrder\n");
  287. }
  288. } else {
  289. log_err("Deleting BootNext failed\n");
  290. }
  291. }
  292. /* BootOrder */
  293. bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
  294. if (!bootorder) {
  295. log_info("BootOrder not defined\n");
  296. ret = EFI_NOT_FOUND;
  297. goto error;
  298. }
  299. num = size / sizeof(uint16_t);
  300. for (i = 0; i < num; i++) {
  301. log_debug("%s trying to load Boot%04X\n", __func__,
  302. bootorder[i]);
  303. ret = try_load_entry(bootorder[i], handle, load_options);
  304. if (ret == EFI_SUCCESS)
  305. break;
  306. }
  307. free(bootorder);
  308. error:
  309. return ret;
  310. }