efi_bootmgr.c 6.8 KB

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