efi_bootmgr.c 4.2 KB

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