x86-stub.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* -----------------------------------------------------------------------
  3. *
  4. * Copyright 2011 Intel Corporation; author Matt Fleming
  5. *
  6. * ----------------------------------------------------------------------- */
  7. #include <linux/efi.h>
  8. #include <linux/pci.h>
  9. #include <linux/stddef.h>
  10. #include <asm/efi.h>
  11. #include <asm/e820/types.h>
  12. #include <asm/setup.h>
  13. #include <asm/desc.h>
  14. #include <asm/boot.h>
  15. #include "efistub.h"
  16. /* Maximum physical address for 64-bit kernel with 4-level paging */
  17. #define MAXMEM_X86_64_4LEVEL (1ull << 46)
  18. const efi_system_table_t *efi_system_table;
  19. const efi_dxe_services_table_t *efi_dxe_table;
  20. extern u32 image_offset;
  21. static efi_loaded_image_t *image = NULL;
  22. static efi_status_t
  23. preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
  24. {
  25. struct pci_setup_rom *rom = NULL;
  26. efi_status_t status;
  27. unsigned long size;
  28. uint64_t romsize;
  29. void *romimage;
  30. /*
  31. * Some firmware images contain EFI function pointers at the place where
  32. * the romimage and romsize fields are supposed to be. Typically the EFI
  33. * code is mapped at high addresses, translating to an unrealistically
  34. * large romsize. The UEFI spec limits the size of option ROMs to 16
  35. * MiB so we reject any ROMs over 16 MiB in size to catch this.
  36. */
  37. romimage = efi_table_attr(pci, romimage);
  38. romsize = efi_table_attr(pci, romsize);
  39. if (!romimage || !romsize || romsize > SZ_16M)
  40. return EFI_INVALID_PARAMETER;
  41. size = romsize + sizeof(*rom);
  42. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  43. (void **)&rom);
  44. if (status != EFI_SUCCESS) {
  45. efi_err("Failed to allocate memory for 'rom'\n");
  46. return status;
  47. }
  48. memset(rom, 0, sizeof(*rom));
  49. rom->data.type = SETUP_PCI;
  50. rom->data.len = size - sizeof(struct setup_data);
  51. rom->data.next = 0;
  52. rom->pcilen = romsize;
  53. *__rom = rom;
  54. status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
  55. PCI_VENDOR_ID, 1, &rom->vendor);
  56. if (status != EFI_SUCCESS) {
  57. efi_err("Failed to read rom->vendor\n");
  58. goto free_struct;
  59. }
  60. status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
  61. PCI_DEVICE_ID, 1, &rom->devid);
  62. if (status != EFI_SUCCESS) {
  63. efi_err("Failed to read rom->devid\n");
  64. goto free_struct;
  65. }
  66. status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
  67. &rom->device, &rom->function);
  68. if (status != EFI_SUCCESS)
  69. goto free_struct;
  70. memcpy(rom->romdata, romimage, romsize);
  71. return status;
  72. free_struct:
  73. efi_bs_call(free_pool, rom);
  74. return status;
  75. }
  76. /*
  77. * There's no way to return an informative status from this function,
  78. * because any analysis (and printing of error messages) needs to be
  79. * done directly at the EFI function call-site.
  80. *
  81. * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
  82. * just didn't find any PCI devices, but there's no way to tell outside
  83. * the context of the call.
  84. */
  85. static void setup_efi_pci(struct boot_params *params)
  86. {
  87. efi_status_t status;
  88. void **pci_handle = NULL;
  89. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  90. unsigned long size = 0;
  91. struct setup_data *data;
  92. efi_handle_t h;
  93. int i;
  94. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  95. &pci_proto, NULL, &size, pci_handle);
  96. if (status == EFI_BUFFER_TOO_SMALL) {
  97. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  98. (void **)&pci_handle);
  99. if (status != EFI_SUCCESS) {
  100. efi_err("Failed to allocate memory for 'pci_handle'\n");
  101. return;
  102. }
  103. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  104. &pci_proto, NULL, &size, pci_handle);
  105. }
  106. if (status != EFI_SUCCESS)
  107. goto free_handle;
  108. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  109. while (data && data->next)
  110. data = (struct setup_data *)(unsigned long)data->next;
  111. for_each_efi_handle(h, pci_handle, size, i) {
  112. efi_pci_io_protocol_t *pci = NULL;
  113. struct pci_setup_rom *rom;
  114. status = efi_bs_call(handle_protocol, h, &pci_proto,
  115. (void **)&pci);
  116. if (status != EFI_SUCCESS || !pci)
  117. continue;
  118. status = preserve_pci_rom_image(pci, &rom);
  119. if (status != EFI_SUCCESS)
  120. continue;
  121. if (data)
  122. data->next = (unsigned long)rom;
  123. else
  124. params->hdr.setup_data = (unsigned long)rom;
  125. data = (struct setup_data *)rom;
  126. }
  127. free_handle:
  128. efi_bs_call(free_pool, pci_handle);
  129. }
  130. static void retrieve_apple_device_properties(struct boot_params *boot_params)
  131. {
  132. efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
  133. struct setup_data *data, *new;
  134. efi_status_t status;
  135. u32 size = 0;
  136. apple_properties_protocol_t *p;
  137. status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
  138. if (status != EFI_SUCCESS)
  139. return;
  140. if (efi_table_attr(p, version) != 0x10000) {
  141. efi_err("Unsupported properties proto version\n");
  142. return;
  143. }
  144. efi_call_proto(p, get_all, NULL, &size);
  145. if (!size)
  146. return;
  147. do {
  148. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
  149. size + sizeof(struct setup_data),
  150. (void **)&new);
  151. if (status != EFI_SUCCESS) {
  152. efi_err("Failed to allocate memory for 'properties'\n");
  153. return;
  154. }
  155. status = efi_call_proto(p, get_all, new->data, &size);
  156. if (status == EFI_BUFFER_TOO_SMALL)
  157. efi_bs_call(free_pool, new);
  158. } while (status == EFI_BUFFER_TOO_SMALL);
  159. new->type = SETUP_APPLE_PROPERTIES;
  160. new->len = size;
  161. new->next = 0;
  162. data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
  163. if (!data) {
  164. boot_params->hdr.setup_data = (unsigned long)new;
  165. } else {
  166. while (data->next)
  167. data = (struct setup_data *)(unsigned long)data->next;
  168. data->next = (unsigned long)new;
  169. }
  170. }
  171. static void
  172. adjust_memory_range_protection(unsigned long start, unsigned long size)
  173. {
  174. efi_status_t status;
  175. efi_gcd_memory_space_desc_t desc;
  176. unsigned long end, next;
  177. unsigned long rounded_start, rounded_end;
  178. unsigned long unprotect_start, unprotect_size;
  179. if (efi_dxe_table == NULL)
  180. return;
  181. rounded_start = rounddown(start, EFI_PAGE_SIZE);
  182. rounded_end = roundup(start + size, EFI_PAGE_SIZE);
  183. /*
  184. * Don't modify memory region attributes, they are
  185. * already suitable, to lower the possibility to
  186. * encounter firmware bugs.
  187. */
  188. for (end = start + size; start < end; start = next) {
  189. status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
  190. if (status != EFI_SUCCESS)
  191. return;
  192. next = desc.base_address + desc.length;
  193. /*
  194. * Only system memory is suitable for trampoline/kernel image placement,
  195. * so only this type of memory needs its attributes to be modified.
  196. */
  197. if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
  198. (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
  199. continue;
  200. unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
  201. unprotect_size = min(rounded_end, next) - unprotect_start;
  202. status = efi_dxe_call(set_memory_space_attributes,
  203. unprotect_start, unprotect_size,
  204. EFI_MEMORY_WB);
  205. if (status != EFI_SUCCESS) {
  206. efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
  207. unprotect_start,
  208. unprotect_start + unprotect_size,
  209. status);
  210. }
  211. }
  212. }
  213. /*
  214. * Trampoline takes 2 pages and can be loaded in first megabyte of memory
  215. * with its end placed between 128k and 640k where BIOS might start.
  216. * (see arch/x86/boot/compressed/pgtable_64.c)
  217. *
  218. * We cannot find exact trampoline placement since memory map
  219. * can be modified by UEFI, and it can alter the computed address.
  220. */
  221. #define TRAMPOLINE_PLACEMENT_BASE ((128 - 8)*1024)
  222. #define TRAMPOLINE_PLACEMENT_SIZE (640*1024 - (128 - 8)*1024)
  223. void startup_32(struct boot_params *boot_params);
  224. static void
  225. setup_memory_protection(unsigned long image_base, unsigned long image_size)
  226. {
  227. /*
  228. * Allow execution of possible trampoline used
  229. * for switching between 4- and 5-level page tables
  230. * and relocated kernel image.
  231. */
  232. adjust_memory_range_protection(TRAMPOLINE_PLACEMENT_BASE,
  233. TRAMPOLINE_PLACEMENT_SIZE);
  234. #ifdef CONFIG_64BIT
  235. if (image_base != (unsigned long)startup_32)
  236. adjust_memory_range_protection(image_base, image_size);
  237. #else
  238. /*
  239. * Clear protection flags on a whole range of possible
  240. * addresses used for KASLR. We don't need to do that
  241. * on x86_64, since KASLR/extraction is performed after
  242. * dedicated identity page tables are built and we only
  243. * need to remove possible protection on relocated image
  244. * itself disregarding further relocations.
  245. */
  246. adjust_memory_range_protection(LOAD_PHYSICAL_ADDR,
  247. KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR);
  248. #endif
  249. }
  250. static const efi_char16_t apple[] = L"Apple";
  251. static void setup_quirks(struct boot_params *boot_params,
  252. unsigned long image_base,
  253. unsigned long image_size)
  254. {
  255. efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
  256. efi_table_attr(efi_system_table, fw_vendor);
  257. if (!memcmp(fw_vendor, apple, sizeof(apple))) {
  258. if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
  259. retrieve_apple_device_properties(boot_params);
  260. }
  261. if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES))
  262. setup_memory_protection(image_base, image_size);
  263. }
  264. /*
  265. * See if we have Universal Graphics Adapter (UGA) protocol
  266. */
  267. static efi_status_t
  268. setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
  269. {
  270. efi_status_t status;
  271. u32 width, height;
  272. void **uga_handle = NULL;
  273. efi_uga_draw_protocol_t *uga = NULL, *first_uga;
  274. efi_handle_t handle;
  275. int i;
  276. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  277. (void **)&uga_handle);
  278. if (status != EFI_SUCCESS)
  279. return status;
  280. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  281. uga_proto, NULL, &size, uga_handle);
  282. if (status != EFI_SUCCESS)
  283. goto free_handle;
  284. height = 0;
  285. width = 0;
  286. first_uga = NULL;
  287. for_each_efi_handle(handle, uga_handle, size, i) {
  288. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  289. u32 w, h, depth, refresh;
  290. void *pciio;
  291. status = efi_bs_call(handle_protocol, handle, uga_proto,
  292. (void **)&uga);
  293. if (status != EFI_SUCCESS)
  294. continue;
  295. pciio = NULL;
  296. efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
  297. status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
  298. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  299. width = w;
  300. height = h;
  301. /*
  302. * Once we've found a UGA supporting PCIIO,
  303. * don't bother looking any further.
  304. */
  305. if (pciio)
  306. break;
  307. first_uga = uga;
  308. }
  309. }
  310. if (!width && !height)
  311. goto free_handle;
  312. /* EFI framebuffer */
  313. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  314. si->lfb_depth = 32;
  315. si->lfb_width = width;
  316. si->lfb_height = height;
  317. si->red_size = 8;
  318. si->red_pos = 16;
  319. si->green_size = 8;
  320. si->green_pos = 8;
  321. si->blue_size = 8;
  322. si->blue_pos = 0;
  323. si->rsvd_size = 8;
  324. si->rsvd_pos = 24;
  325. free_handle:
  326. efi_bs_call(free_pool, uga_handle);
  327. return status;
  328. }
  329. static void setup_graphics(struct boot_params *boot_params)
  330. {
  331. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  332. struct screen_info *si;
  333. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  334. efi_status_t status;
  335. unsigned long size;
  336. void **gop_handle = NULL;
  337. void **uga_handle = NULL;
  338. si = &boot_params->screen_info;
  339. memset(si, 0, sizeof(*si));
  340. size = 0;
  341. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  342. &graphics_proto, NULL, &size, gop_handle);
  343. if (status == EFI_BUFFER_TOO_SMALL)
  344. status = efi_setup_gop(si, &graphics_proto, size);
  345. if (status != EFI_SUCCESS) {
  346. size = 0;
  347. status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
  348. &uga_proto, NULL, &size, uga_handle);
  349. if (status == EFI_BUFFER_TOO_SMALL)
  350. setup_uga(si, &uga_proto, size);
  351. }
  352. }
  353. static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
  354. {
  355. efi_bs_call(exit, handle, status, 0, NULL);
  356. for(;;)
  357. asm("hlt");
  358. }
  359. void __noreturn efi_stub_entry(efi_handle_t handle,
  360. efi_system_table_t *sys_table_arg,
  361. struct boot_params *boot_params);
  362. /*
  363. * Because the x86 boot code expects to be passed a boot_params we
  364. * need to create one ourselves (usually the bootloader would create
  365. * one for us).
  366. */
  367. efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
  368. efi_system_table_t *sys_table_arg)
  369. {
  370. struct boot_params *boot_params;
  371. struct setup_header *hdr;
  372. void *image_base;
  373. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  374. int options_size = 0;
  375. efi_status_t status;
  376. char *cmdline_ptr;
  377. efi_system_table = sys_table_arg;
  378. /* Check if we were booted by the EFI firmware */
  379. if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  380. efi_exit(handle, EFI_INVALID_PARAMETER);
  381. status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
  382. if (status != EFI_SUCCESS) {
  383. efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  384. efi_exit(handle, status);
  385. }
  386. image_base = efi_table_attr(image, image_base);
  387. image_offset = (void *)startup_32 - image_base;
  388. status = efi_allocate_pages(sizeof(struct boot_params),
  389. (unsigned long *)&boot_params, ULONG_MAX);
  390. if (status != EFI_SUCCESS) {
  391. efi_err("Failed to allocate lowmem for boot params\n");
  392. efi_exit(handle, status);
  393. }
  394. memset(boot_params, 0x0, sizeof(struct boot_params));
  395. hdr = &boot_params->hdr;
  396. /* Copy the setup header from the second sector to boot_params */
  397. memcpy(&hdr->jump, image_base + 512,
  398. sizeof(struct setup_header) - offsetof(struct setup_header, jump));
  399. /*
  400. * Fill out some of the header fields ourselves because the
  401. * EFI firmware loader doesn't load the first sector.
  402. */
  403. hdr->root_flags = 1;
  404. hdr->vid_mode = 0xffff;
  405. hdr->boot_flag = 0xAA55;
  406. hdr->type_of_loader = 0x21;
  407. /* Convert unicode cmdline to ascii */
  408. cmdline_ptr = efi_convert_cmdline(image, &options_size);
  409. if (!cmdline_ptr)
  410. goto fail;
  411. efi_set_u64_split((unsigned long)cmdline_ptr,
  412. &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
  413. hdr->ramdisk_image = 0;
  414. hdr->ramdisk_size = 0;
  415. /*
  416. * Disregard any setup data that was provided by the bootloader:
  417. * setup_data could be pointing anywhere, and we have no way of
  418. * authenticating or validating the payload.
  419. */
  420. hdr->setup_data = 0;
  421. efi_stub_entry(handle, sys_table_arg, boot_params);
  422. /* not reached */
  423. fail:
  424. efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
  425. efi_exit(handle, status);
  426. }
  427. static void add_e820ext(struct boot_params *params,
  428. struct setup_data *e820ext, u32 nr_entries)
  429. {
  430. struct setup_data *data;
  431. e820ext->type = SETUP_E820_EXT;
  432. e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
  433. e820ext->next = 0;
  434. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  435. while (data && data->next)
  436. data = (struct setup_data *)(unsigned long)data->next;
  437. if (data)
  438. data->next = (unsigned long)e820ext;
  439. else
  440. params->hdr.setup_data = (unsigned long)e820ext;
  441. }
  442. static efi_status_t
  443. setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
  444. {
  445. struct boot_e820_entry *entry = params->e820_table;
  446. struct efi_info *efi = &params->efi_info;
  447. struct boot_e820_entry *prev = NULL;
  448. u32 nr_entries;
  449. u32 nr_desc;
  450. int i;
  451. nr_entries = 0;
  452. nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
  453. for (i = 0; i < nr_desc; i++) {
  454. efi_memory_desc_t *d;
  455. unsigned int e820_type = 0;
  456. unsigned long m = efi->efi_memmap;
  457. #ifdef CONFIG_X86_64
  458. m |= (u64)efi->efi_memmap_hi << 32;
  459. #endif
  460. d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
  461. switch (d->type) {
  462. case EFI_RESERVED_TYPE:
  463. case EFI_RUNTIME_SERVICES_CODE:
  464. case EFI_RUNTIME_SERVICES_DATA:
  465. case EFI_MEMORY_MAPPED_IO:
  466. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  467. case EFI_PAL_CODE:
  468. e820_type = E820_TYPE_RESERVED;
  469. break;
  470. case EFI_UNUSABLE_MEMORY:
  471. e820_type = E820_TYPE_UNUSABLE;
  472. break;
  473. case EFI_ACPI_RECLAIM_MEMORY:
  474. e820_type = E820_TYPE_ACPI;
  475. break;
  476. case EFI_LOADER_CODE:
  477. case EFI_LOADER_DATA:
  478. case EFI_BOOT_SERVICES_CODE:
  479. case EFI_BOOT_SERVICES_DATA:
  480. case EFI_CONVENTIONAL_MEMORY:
  481. if (efi_soft_reserve_enabled() &&
  482. (d->attribute & EFI_MEMORY_SP))
  483. e820_type = E820_TYPE_SOFT_RESERVED;
  484. else
  485. e820_type = E820_TYPE_RAM;
  486. break;
  487. case EFI_ACPI_MEMORY_NVS:
  488. e820_type = E820_TYPE_NVS;
  489. break;
  490. case EFI_PERSISTENT_MEMORY:
  491. e820_type = E820_TYPE_PMEM;
  492. break;
  493. default:
  494. continue;
  495. }
  496. /* Merge adjacent mappings */
  497. if (prev && prev->type == e820_type &&
  498. (prev->addr + prev->size) == d->phys_addr) {
  499. prev->size += d->num_pages << 12;
  500. continue;
  501. }
  502. if (nr_entries == ARRAY_SIZE(params->e820_table)) {
  503. u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
  504. sizeof(struct setup_data);
  505. if (!e820ext || e820ext_size < need)
  506. return EFI_BUFFER_TOO_SMALL;
  507. /* boot_params map full, switch to e820 extended */
  508. entry = (struct boot_e820_entry *)e820ext->data;
  509. }
  510. entry->addr = d->phys_addr;
  511. entry->size = d->num_pages << PAGE_SHIFT;
  512. entry->type = e820_type;
  513. prev = entry++;
  514. nr_entries++;
  515. }
  516. if (nr_entries > ARRAY_SIZE(params->e820_table)) {
  517. u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
  518. add_e820ext(params, e820ext, nr_e820ext);
  519. nr_entries -= nr_e820ext;
  520. }
  521. params->e820_entries = (u8)nr_entries;
  522. return EFI_SUCCESS;
  523. }
  524. static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
  525. u32 *e820ext_size)
  526. {
  527. efi_status_t status;
  528. unsigned long size;
  529. size = sizeof(struct setup_data) +
  530. sizeof(struct e820_entry) * nr_desc;
  531. if (*e820ext) {
  532. efi_bs_call(free_pool, *e820ext);
  533. *e820ext = NULL;
  534. *e820ext_size = 0;
  535. }
  536. status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
  537. (void **)e820ext);
  538. if (status == EFI_SUCCESS)
  539. *e820ext_size = size;
  540. return status;
  541. }
  542. static efi_status_t allocate_e820(struct boot_params *params,
  543. struct setup_data **e820ext,
  544. u32 *e820ext_size)
  545. {
  546. unsigned long map_size, desc_size, map_key;
  547. efi_status_t status;
  548. __u32 nr_desc, desc_version;
  549. /* Only need the size of the mem map and size of each mem descriptor */
  550. map_size = 0;
  551. status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
  552. &desc_size, &desc_version);
  553. if (status != EFI_BUFFER_TOO_SMALL)
  554. return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
  555. nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
  556. if (nr_desc > ARRAY_SIZE(params->e820_table)) {
  557. u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
  558. status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
  559. if (status != EFI_SUCCESS)
  560. return status;
  561. }
  562. return EFI_SUCCESS;
  563. }
  564. struct exit_boot_struct {
  565. struct boot_params *boot_params;
  566. struct efi_info *efi;
  567. };
  568. static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
  569. void *priv)
  570. {
  571. const char *signature;
  572. struct exit_boot_struct *p = priv;
  573. signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
  574. : EFI32_LOADER_SIGNATURE;
  575. memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
  576. efi_set_u64_split((unsigned long)efi_system_table,
  577. &p->efi->efi_systab, &p->efi->efi_systab_hi);
  578. p->efi->efi_memdesc_size = map->desc_size;
  579. p->efi->efi_memdesc_version = map->desc_ver;
  580. efi_set_u64_split((unsigned long)map->map,
  581. &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
  582. p->efi->efi_memmap_size = map->map_size;
  583. return EFI_SUCCESS;
  584. }
  585. static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
  586. {
  587. struct setup_data *e820ext = NULL;
  588. __u32 e820ext_size = 0;
  589. efi_status_t status;
  590. struct exit_boot_struct priv;
  591. priv.boot_params = boot_params;
  592. priv.efi = &boot_params->efi_info;
  593. status = allocate_e820(boot_params, &e820ext, &e820ext_size);
  594. if (status != EFI_SUCCESS)
  595. return status;
  596. /* Might as well exit boot services now */
  597. status = efi_exit_boot_services(handle, &priv, exit_boot_func);
  598. if (status != EFI_SUCCESS)
  599. return status;
  600. /* Historic? */
  601. boot_params->alt_mem_k = 32 * 1024;
  602. status = setup_e820(boot_params, e820ext, e820ext_size);
  603. if (status != EFI_SUCCESS)
  604. return status;
  605. return EFI_SUCCESS;
  606. }
  607. /*
  608. * On success, we return the address of startup_32, which has potentially been
  609. * relocated by efi_relocate_kernel.
  610. * On failure, we exit to the firmware via efi_exit instead of returning.
  611. */
  612. asmlinkage unsigned long efi_main(efi_handle_t handle,
  613. efi_system_table_t *sys_table_arg,
  614. struct boot_params *boot_params)
  615. {
  616. unsigned long bzimage_addr = (unsigned long)startup_32;
  617. unsigned long buffer_start, buffer_end;
  618. struct setup_header *hdr = &boot_params->hdr;
  619. const struct linux_efi_initrd *initrd = NULL;
  620. efi_status_t status;
  621. efi_system_table = sys_table_arg;
  622. /* Check if we were booted by the EFI firmware */
  623. if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  624. efi_exit(handle, EFI_INVALID_PARAMETER);
  625. efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
  626. if (efi_dxe_table &&
  627. efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
  628. efi_warn("Ignoring DXE services table: invalid signature\n");
  629. efi_dxe_table = NULL;
  630. }
  631. /*
  632. * If the kernel isn't already loaded at a suitable address,
  633. * relocate it.
  634. *
  635. * It must be loaded above LOAD_PHYSICAL_ADDR.
  636. *
  637. * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
  638. * is defined as the macro MAXMEM, but unfortunately that is not a
  639. * compile-time constant if 5-level paging is configured, so we instead
  640. * define our own macro for use here.
  641. *
  642. * For 32-bit, the maximum address is complicated to figure out, for
  643. * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
  644. * KASLR uses.
  645. *
  646. * Also relocate it if image_offset is zero, i.e. the kernel wasn't
  647. * loaded by LoadImage, but rather by a bootloader that called the
  648. * handover entry. The reason we must always relocate in this case is
  649. * to handle the case of systemd-boot booting a unified kernel image,
  650. * which is a PE executable that contains the bzImage and an initrd as
  651. * COFF sections. The initrd section is placed after the bzImage
  652. * without ensuring that there are at least init_size bytes available
  653. * for the bzImage, and thus the compressed kernel's startup code may
  654. * overwrite the initrd unless it is moved out of the way.
  655. */
  656. buffer_start = ALIGN(bzimage_addr - image_offset,
  657. hdr->kernel_alignment);
  658. buffer_end = buffer_start + hdr->init_size;
  659. if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
  660. (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
  661. (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
  662. (image_offset == 0)) {
  663. extern char _bss[];
  664. status = efi_relocate_kernel(&bzimage_addr,
  665. (unsigned long)_bss - bzimage_addr,
  666. hdr->init_size,
  667. hdr->pref_address,
  668. hdr->kernel_alignment,
  669. LOAD_PHYSICAL_ADDR);
  670. if (status != EFI_SUCCESS) {
  671. efi_err("efi_relocate_kernel() failed!\n");
  672. goto fail;
  673. }
  674. /*
  675. * Now that we've copied the kernel elsewhere, we no longer
  676. * have a set up block before startup_32(), so reset image_offset
  677. * to zero in case it was set earlier.
  678. */
  679. image_offset = 0;
  680. }
  681. #ifdef CONFIG_CMDLINE_BOOL
  682. status = efi_parse_options(CONFIG_CMDLINE);
  683. if (status != EFI_SUCCESS) {
  684. efi_err("Failed to parse options\n");
  685. goto fail;
  686. }
  687. #endif
  688. if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
  689. unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
  690. ((u64)boot_params->ext_cmd_line_ptr << 32));
  691. status = efi_parse_options((char *)cmdline_paddr);
  692. if (status != EFI_SUCCESS) {
  693. efi_err("Failed to parse options\n");
  694. goto fail;
  695. }
  696. }
  697. /*
  698. * At this point, an initrd may already have been loaded by the
  699. * bootloader and passed via bootparams. We permit an initrd loaded
  700. * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
  701. *
  702. * If the device path is not present, any command-line initrd=
  703. * arguments will be processed only if image is not NULL, which will be
  704. * the case only if we were loaded via the PE entry point.
  705. */
  706. status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
  707. &initrd);
  708. if (status != EFI_SUCCESS)
  709. goto fail;
  710. if (initrd && initrd->size > 0) {
  711. efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
  712. &boot_params->ext_ramdisk_image);
  713. efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
  714. &boot_params->ext_ramdisk_size);
  715. }
  716. /*
  717. * If the boot loader gave us a value for secure_boot then we use that,
  718. * otherwise we ask the BIOS.
  719. */
  720. if (boot_params->secure_boot == efi_secureboot_mode_unset)
  721. boot_params->secure_boot = efi_get_secureboot();
  722. /* Ask the firmware to clear memory on unclean shutdown */
  723. efi_enable_reset_attack_mitigation();
  724. efi_random_get_seed();
  725. efi_retrieve_tpm2_eventlog();
  726. setup_graphics(boot_params);
  727. setup_efi_pci(boot_params);
  728. setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start);
  729. status = exit_boot(boot_params, handle);
  730. if (status != EFI_SUCCESS) {
  731. efi_err("exit_boot() failed!\n");
  732. goto fail;
  733. }
  734. return bzimage_addr;
  735. fail:
  736. efi_err("efi_main() failed!\n");
  737. efi_exit(handle, status);
  738. }