efi_bootmgr.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_deserialize_load_option() - parse serialized data
  30. *
  31. * Parse serialized data describing a load option and transform it to the
  32. * efi_load_option structure.
  33. *
  34. * @lo: pointer to target
  35. * @data: serialized data
  36. * @size: size of the load option, on return size of the optional data
  37. * Return: status code
  38. */
  39. efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
  40. efi_uintn_t *size)
  41. {
  42. efi_uintn_t len;
  43. len = sizeof(u32);
  44. if (*size < len + 2 * sizeof(u16))
  45. return EFI_INVALID_PARAMETER;
  46. lo->attributes = get_unaligned_le32(data);
  47. data += len;
  48. *size -= len;
  49. len = sizeof(u16);
  50. lo->file_path_length = get_unaligned_le16(data);
  51. data += len;
  52. *size -= len;
  53. lo->label = (u16 *)data;
  54. len = u16_strnlen(lo->label, *size / sizeof(u16) - 1);
  55. if (lo->label[len])
  56. return EFI_INVALID_PARAMETER;
  57. len = (len + 1) * sizeof(u16);
  58. if (*size < len)
  59. return EFI_INVALID_PARAMETER;
  60. data += len;
  61. *size -= len;
  62. len = lo->file_path_length;
  63. if (*size < len)
  64. return EFI_INVALID_PARAMETER;
  65. lo->file_path = (struct efi_device_path *)data;
  66. /*
  67. * TODO: validate device path. There should be an end node within
  68. * the indicated file_path_length.
  69. */
  70. data += len;
  71. *size -= len;
  72. lo->optional_data = data;
  73. return EFI_SUCCESS;
  74. }
  75. /**
  76. * efi_serialize_load_option() - serialize load option
  77. *
  78. * Serialize efi_load_option structure into byte stream for BootXXXX.
  79. *
  80. * @data: buffer for serialized data
  81. * @lo: load option
  82. * Return: size of allocated buffer
  83. */
  84. unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data)
  85. {
  86. unsigned long label_len;
  87. unsigned long size;
  88. u8 *p;
  89. label_len = (u16_strlen(lo->label) + 1) * sizeof(u16);
  90. /* total size */
  91. size = sizeof(lo->attributes);
  92. size += sizeof(lo->file_path_length);
  93. size += label_len;
  94. size += lo->file_path_length;
  95. if (lo->optional_data)
  96. size += (utf8_utf16_strlen((const char *)lo->optional_data)
  97. + 1) * sizeof(u16);
  98. p = malloc(size);
  99. if (!p)
  100. return 0;
  101. /* copy data */
  102. *data = p;
  103. memcpy(p, &lo->attributes, sizeof(lo->attributes));
  104. p += sizeof(lo->attributes);
  105. memcpy(p, &lo->file_path_length, sizeof(lo->file_path_length));
  106. p += sizeof(lo->file_path_length);
  107. memcpy(p, lo->label, label_len);
  108. p += label_len;
  109. memcpy(p, lo->file_path, lo->file_path_length);
  110. p += lo->file_path_length;
  111. if (lo->optional_data) {
  112. utf8_utf16_strcpy((u16 **)&p, (const char *)lo->optional_data);
  113. p += sizeof(u16); /* size of trailing \0 */
  114. }
  115. return size;
  116. }
  117. /**
  118. * get_var() - get UEFI variable
  119. *
  120. * It is the caller's duty to free the returned buffer.
  121. *
  122. * @name: name of variable
  123. * @vendor: vendor GUID of variable
  124. * @size: size of allocated buffer
  125. * Return: buffer with variable data or NULL
  126. */
  127. static void *get_var(u16 *name, const efi_guid_t *vendor,
  128. efi_uintn_t *size)
  129. {
  130. efi_status_t ret;
  131. void *buf = NULL;
  132. *size = 0;
  133. ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
  134. if (ret == EFI_BUFFER_TOO_SMALL) {
  135. buf = malloc(*size);
  136. ret = efi_get_variable_int(name, vendor, NULL, size, buf, NULL);
  137. }
  138. if (ret != EFI_SUCCESS) {
  139. free(buf);
  140. *size = 0;
  141. return NULL;
  142. }
  143. return buf;
  144. }
  145. /**
  146. * try_load_entry() - try to load image for boot option
  147. *
  148. * Attempt to load load-option number 'n', returning device_path and file_path
  149. * if successful. This checks that the EFI_LOAD_OPTION is active (enabled)
  150. * and that the specified file to boot exists.
  151. *
  152. * @n: number of the boot option, e.g. 0x0a13 for Boot0A13
  153. * @handle: on return handle for the newly installed image
  154. * Return: status code
  155. */
  156. static efi_status_t try_load_entry(u16 n, efi_handle_t *handle)
  157. {
  158. struct efi_load_option lo;
  159. u16 varname[] = L"Boot0000";
  160. u16 hexmap[] = L"0123456789ABCDEF";
  161. void *load_option;
  162. efi_uintn_t size;
  163. efi_status_t ret;
  164. varname[4] = hexmap[(n & 0xf000) >> 12];
  165. varname[5] = hexmap[(n & 0x0f00) >> 8];
  166. varname[6] = hexmap[(n & 0x00f0) >> 4];
  167. varname[7] = hexmap[(n & 0x000f) >> 0];
  168. load_option = get_var(varname, &efi_global_variable_guid, &size);
  169. if (!load_option)
  170. return EFI_LOAD_ERROR;
  171. ret = efi_deserialize_load_option(&lo, load_option, &size);
  172. if (ret != EFI_SUCCESS) {
  173. log_warning("Invalid load option for %ls\n", varname);
  174. goto error;
  175. }
  176. if (lo.attributes & LOAD_OPTION_ACTIVE) {
  177. u32 attributes;
  178. log_debug("%s: trying to load \"%ls\" from %pD\n",
  179. __func__, lo.label, lo.file_path);
  180. ret = EFI_CALL(efi_load_image(true, efi_root, lo.file_path,
  181. NULL, 0, handle));
  182. if (ret != EFI_SUCCESS) {
  183. log_warning("Loading %ls '%ls' failed\n",
  184. varname, lo.label);
  185. goto error;
  186. }
  187. attributes = EFI_VARIABLE_BOOTSERVICE_ACCESS |
  188. EFI_VARIABLE_RUNTIME_ACCESS;
  189. size = sizeof(n);
  190. ret = efi_set_variable_int(L"BootCurrent",
  191. &efi_global_variable_guid,
  192. attributes, size, &n, false);
  193. if (ret != EFI_SUCCESS) {
  194. if (EFI_CALL(efi_unload_image(*handle))
  195. != EFI_SUCCESS)
  196. log_err("Unloading image failed\n");
  197. goto error;
  198. }
  199. log_info("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. size = sizeof(bootnext);
  227. ret = efi_get_variable_int(L"BootNext",
  228. &efi_global_variable_guid,
  229. NULL, &size, &bootnext, NULL);
  230. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  231. /* BootNext does exist here */
  232. if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16))
  233. log_err("BootNext must be 16-bit integer\n");
  234. /* delete BootNext */
  235. ret = efi_set_variable_int(L"BootNext",
  236. &efi_global_variable_guid,
  237. 0, 0, NULL, false);
  238. /* load BootNext */
  239. if (ret == EFI_SUCCESS) {
  240. if (size == sizeof(u16)) {
  241. ret = try_load_entry(bootnext, handle);
  242. if (ret == EFI_SUCCESS)
  243. return ret;
  244. log_warning(
  245. "Loading from BootNext failed, falling back to BootOrder\n");
  246. }
  247. } else {
  248. log_err("Deleting BootNext failed\n");
  249. }
  250. }
  251. /* BootOrder */
  252. bootorder = get_var(L"BootOrder", &efi_global_variable_guid, &size);
  253. if (!bootorder) {
  254. log_info("BootOrder not defined\n");
  255. ret = EFI_NOT_FOUND;
  256. goto error;
  257. }
  258. num = size / sizeof(uint16_t);
  259. for (i = 0; i < num; i++) {
  260. log_debug("%s trying to load Boot%04X\n", __func__,
  261. bootorder[i]);
  262. ret = try_load_entry(bootorder[i], handle);
  263. if (ret == EFI_SUCCESS)
  264. break;
  265. }
  266. free(bootorder);
  267. error:
  268. return ret;
  269. }