efi_bootmgr.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI boot manager
  4. *
  5. * Copyright (c) 2017 Rob Clark
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. #include <efi_loader.h>
  12. #include <asm/unaligned.h>
  13. static const struct efi_boot_services *bs;
  14. static const struct efi_runtime_services *rs;
  15. /*
  16. * bootmgr implements the logic of trying to find a payload to boot
  17. * based on the BootOrder + BootXXXX variables, and then loading it.
  18. *
  19. * TODO detecting a special key held (f9?) and displaying a boot menu
  20. * like you would get on a PC would be clever.
  21. *
  22. * TODO if we had a way to write and persist variables after the OS
  23. * has started, we'd also want to check OsIndications to see if we
  24. * should do normal or recovery boot.
  25. */
  26. /**
  27. * efi_deserialize_load_option() - parse serialized data
  28. *
  29. * Parse serialized data describing a load option and transform it to the
  30. * efi_load_option structure.
  31. *
  32. * @lo: pointer to target
  33. * @data: serialized data
  34. * @size: size of the load option, on return size of the optional data
  35. * Return: status code
  36. */
  37. efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
  38. efi_uintn_t *size)
  39. {
  40. efi_uintn_t len;
  41. len = sizeof(u32);
  42. if (*size < len + 2 * sizeof(u16))
  43. return EFI_INVALID_PARAMETER;
  44. lo->attributes = get_unaligned_le32(data);
  45. data += len;
  46. *size -= len;
  47. len = sizeof(u16);
  48. lo->file_path_length = get_unaligned_le16(data);
  49. data += len;
  50. *size -= len;
  51. lo->label = (u16 *)data;
  52. len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
  53. if (lo->label[len])
  54. return EFI_INVALID_PARAMETER;
  55. len = (len + 1) * sizeof(u16);
  56. if (*size < len)
  57. return EFI_INVALID_PARAMETER;
  58. data += len;
  59. *size -= len;
  60. len = lo->file_path_length;
  61. if (*size < len)
  62. return EFI_INVALID_PARAMETER;
  63. lo->file_path = (struct efi_device_path *)data;
  64. /*
  65. * TODO: validate device path. There should be an end node within
  66. * the indicated file_path_length.
  67. */
  68. data += len;
  69. *size -= len;
  70. lo->optional_data = data;
  71. return EFI_SUCCESS;
  72. }
  73. /**
  74. * efi_serialize_load_option() - serialize load option
  75. *
  76. * Serialize efi_load_option structure into byte stream for BootXXXX.
  77. *
  78. * @data: buffer for serialized data
  79. * @lo: load option
  80. * Return: size of allocated buffer
  81. */
  82. unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
  83. {
  84. unsigned long label_len;
  85. unsigned long size;
  86. u8 *p;
  87. label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
  88. /* total size */
  89. size = sizeof(lo->attributes);
  90. size += sizeof(lo->file_path_length);
  91. size += label_len;
  92. size += lo->file_path_length;
  93. if (lo->optional_data)
  94. size += (utf8_utf16_strlen((const char *)lo->optional_data)
  95. + 1) * sizeof(u16);
  96. p = malloc(size);
  97. if (!p)
  98. return 0;
  99. /* copy data */
  100. *data = p;
  101. memcpy(p, &lo->attributes, sizeof(lo->attributes));
  102. p += sizeof(lo->attributes);
  103. memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
  104. p += sizeof(lo->file_path_length);
  105. memcpy(p, lo->label, label_len);
  106. p += label_len;
  107. memcpy(p, lo->file_path, lo->file_path_length);
  108. p += lo->file_path_length;
  109. if (lo->optional_data) {
  110. utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
  111. p += sizeof(u16); /* size of trailing \0 */
  112. }
  113. return size;
  114. }
  115. /**
  116. * get_var() - get UEFI variable
  117. *
  118. * It is the caller's duty to free the returned buffer.
  119. *
  120. * @name: name of variable
  121. * @vendor: vendor GUID of variable
  122. * @size: size of allocated buffer
  123. * Return: buffer with variable data or NULL
  124. */
  125. static void *get_var(u16 *name, const efi_guid_t *vendor,
  126. efi_uintn_t *size)
  127. {
  128. efi_guid_t *v = (efi_guid_t *)vendor;
  129. efi_status_t ret;
  130. void *buf = NULL;
  131. *size = 0;
  132. EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
  133. if (ret == EFI_BUFFER_TOO_SMALL) {
  134. buf = malloc(*size);
  135. EFI_CALL(ret = rs->get_variable(name, v, NULL, size, buf));
  136. }
  137. if (ret != EFI_SUCCESS) {
  138. free(buf);
  139. *size = 0;
  140. return NULL;
  141. }
  142. return buf;
  143. }
  144. /**
  145. * try_load_entry() - try to load image for boot option
  146. *
  147. * Attempt to load load-option number 'n', returning device_path and file_path
  148. * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
  149. * and that the specified file to boot exists.
  150. *
  151. * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
  152. * @handle: on return handle for the newly installed image
  153. * Return: status code
  154. */
  155. static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
  156. {
  157. struct efi_load_option lo;
  158. u16 varname[] = L"Boot0000";
  159. u16 hexmap[] = L"0123456789ABCDEF";
  160. void *load_option;
  161. efi_uintn_t size;
  162. efi_status_t ret;
  163. varname[4] = hexmap[(n & 0xf000) >> 12];
  164. varname[5] = hexmap[(n & 0x0f00) >> 8];
  165. varname[6] = hexmap[(n & 0x00f0) >> 4];
  166. varname[7] = hexmap[(n & 0x000f) >> 0];
  167. load_option = get_var(varname, &efi_global_variable_guid, &size);
  168. if (!load_option)
  169. return EFI_LOAD_ERROR;
  170. ret = efi_deserialize_load_option(&lo, load_option, &size);
  171. if (ret != EFI_SUCCESS) {
  172. log_warning("Invalid load option for %ls\n", varname);
  173. goto error;
  174. }
  175. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  176. u32 attributes;
  177. debug("%s: trying to load \"%ls\" from %pD\n",
  178. __func__, lo.label, lo.file_path);
  179. ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
  180. NULL, 0, handle));
  181. if (ret != EFI_SUCCESS) {
  182. printf("Loading from Boot%04X '%ls' failed\n", n,
  183. lo.label);
  184. goto error;
  185. }
  186. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  187. EFI_VARIABLE_RUNTIME_ACCESS;
  188. size = sizeof(n);
  189. ret = EFI_CALL(efi_set_variable(
  190. L"BootCurrent",
  191. (efi_guid_t *)&efi_global_variable_guid,
  192. attributes, size, &n));
  193. if (ret != EFI_SUCCESS) {
  194. if (EFI_CALL(efi_unload_image(*handle))
  195. != EFI_SUCCESS)
  196. printf("Unloading image failed\n");
  197. goto error;
  198. }
  199. printf("Booting: %ls\n", lo.label);
  200. } else {
  201. ret = EFI_LOAD_ERROR;
  202. }
  203. error:
  204. free(load_option);
  205. return ret;
  206. }
  207. /**
  208. * efi_bootmgr_load() - try to load from BootNext or BootOrder
  209. *
  210. * Attempt to load from BootNext or in the order specified by BootOrder
  211. * EFI variable, the available load-options, finding and returning
  212. * the first one that can be loaded successfully.
  213. *
  214. * @handle: on return handle for the newly installed image
  215. * Return: status code
  216. */
  217. efi_status_t efi_bootmgr_load(efi_handle_t *handle)
  218. {
  219. u16 bootnext, *bootorder;
  220. efi_uintn_t size;
  221. int i, num;
  222. efi_status_t ret;
  223. bs = systab.boottime;
  224. rs = systab.runtime;
  225. /* BootNext */
  226. bootnext = 0;
  227. size = sizeof(bootnext);
  228. ret = EFI_CALL(efi_get_variable(L"BootNext",
  229. (efi_guid_t *)&efi_global_variable_guid,
  230. NULL, &size, &bootnext));
  231. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  232. /* BootNext does exist here */
  233. if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
  234. printf("BootNext must be 16-bit integer\n");
  235. /* delete BootNext */
  236. ret = EFI_CALL(efi_set_variable(
  237. L"BootNext",
  238. (efi_guid_t *)&efi_global_variable_guid,
  239. EFI_VARIABLE_NON_VOLATILE, 0,
  240. &bootnext));
  241. /* load BootNext */
  242. if (ret == EFI_SUCCESS) {
  243. if (size == sizeof(u16)) {
  244. ret = try_load_entry(bootnext, handle);
  245. if (ret == EFI_SUCCESS)
  246. return ret;
  247. printf("Loading from BootNext failed, falling back to BootOrder\n");
  248. }
  249. } else {
  250. printf("Deleting BootNext failed\n");
  251. }
  252. }
  253. /* BootOrder */
  254. bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
  255. if (!bootorder) {
  256. printf("BootOrder not defined\n");
  257. ret = EFI_NOT_FOUND;
  258. goto error;
  259. }
  260. num = size / sizeof(uint16_t);
  261. for (i = 0; i < num; i++) {
  262. debug("%s: trying to load Boot%04X\n", __func__, bootorder[i]);
  263. ret = try_load_entry(bootorder[i], handle);
  264. if (ret == EFI_SUCCESS)
  265. break;
  266. }
  267. free(bootorder);
  268. error:
  269. return ret;
  270. }