eboot.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /* -----------------------------------------------------------------------
  2. *
  3. * Copyright 2011 Intel Corporation; author Matt Fleming
  4. *
  5. * This file is part of the Linux kernel, and is made available under
  6. * the terms of the GNU General Public License version 2.
  7. *
  8. * ----------------------------------------------------------------------- */
  9. #include <linux/efi.h>
  10. #include <linux/pci.h>
  11. #include <asm/efi.h>
  12. #include <asm/e820/types.h>
  13. #include <asm/setup.h>
  14. #include <asm/desc.h>
  15. #include "../string.h"
  16. #include "eboot.h"
  17. static efi_system_table_t *sys_table;
  18. static struct efi_config *efi_early;
  19. __pure const struct efi_config *__efi_early(void)
  20. {
  21. return efi_early;
  22. }
  23. #define BOOT_SERVICES(bits) \
  24. static void setup_boot_services##bits(struct efi_config *c) \
  25. { \
  26. efi_system_table_##bits##_t *table; \
  27. \
  28. table = (typeof(table))sys_table; \
  29. \
  30. c->runtime_services = table->runtime; \
  31. c->boot_services = table->boottime; \
  32. c->text_output = table->con_out; \
  33. }
  34. BOOT_SERVICES(32);
  35. BOOT_SERVICES(64);
  36. void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
  37. {
  38. efi_call_proto(efi_simple_text_output_protocol, output_string,
  39. efi_early->text_output, str);
  40. }
  41. static efi_status_t
  42. preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
  43. {
  44. struct pci_setup_rom *rom = NULL;
  45. efi_status_t status;
  46. unsigned long size;
  47. uint64_t romsize;
  48. void *romimage;
  49. /*
  50. * Some firmware images contain EFI function pointers at the place where
  51. * the romimage and romsize fields are supposed to be. Typically the EFI
  52. * code is mapped at high addresses, translating to an unrealistically
  53. * large romsize. The UEFI spec limits the size of option ROMs to 16
  54. * MiB so we reject any ROMs over 16 MiB in size to catch this.
  55. */
  56. romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
  57. romimage, pci);
  58. romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
  59. if (!romimage || !romsize || romsize > SZ_16M)
  60. return EFI_INVALID_PARAMETER;
  61. size = romsize + sizeof(*rom);
  62. status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
  63. if (status != EFI_SUCCESS) {
  64. efi_printk(sys_table, "Failed to allocate memory for 'rom'\n");
  65. return status;
  66. }
  67. memset(rom, 0, sizeof(*rom));
  68. rom->data.type = SETUP_PCI;
  69. rom->data.len = size - sizeof(struct setup_data);
  70. rom->data.next = 0;
  71. rom->pcilen = pci->romsize;
  72. *__rom = rom;
  73. status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
  74. EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
  75. &rom->vendor);
  76. if (status != EFI_SUCCESS) {
  77. efi_printk(sys_table, "Failed to read rom->vendor\n");
  78. goto free_struct;
  79. }
  80. status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
  81. EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
  82. &rom->devid);
  83. if (status != EFI_SUCCESS) {
  84. efi_printk(sys_table, "Failed to read rom->devid\n");
  85. goto free_struct;
  86. }
  87. status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
  88. &rom->segment, &rom->bus, &rom->device,
  89. &rom->function);
  90. if (status != EFI_SUCCESS)
  91. goto free_struct;
  92. memcpy(rom->romdata, romimage, romsize);
  93. return status;
  94. free_struct:
  95. efi_call_early(free_pool, rom);
  96. return status;
  97. }
  98. /*
  99. * There's no way to return an informative status from this function,
  100. * because any analysis (and printing of error messages) needs to be
  101. * done directly at the EFI function call-site.
  102. *
  103. * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
  104. * just didn't find any PCI devices, but there's no way to tell outside
  105. * the context of the call.
  106. */
  107. static void setup_efi_pci(struct boot_params *params)
  108. {
  109. efi_status_t status;
  110. void **pci_handle = NULL;
  111. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  112. unsigned long size = 0;
  113. unsigned long nr_pci;
  114. struct setup_data *data;
  115. int i;
  116. status = efi_call_early(locate_handle,
  117. EFI_LOCATE_BY_PROTOCOL,
  118. &pci_proto, NULL, &size, pci_handle);
  119. if (status == EFI_BUFFER_TOO_SMALL) {
  120. status = efi_call_early(allocate_pool,
  121. EFI_LOADER_DATA,
  122. size, (void **)&pci_handle);
  123. if (status != EFI_SUCCESS) {
  124. efi_printk(sys_table, "Failed to allocate memory for 'pci_handle'\n");
  125. return;
  126. }
  127. status = efi_call_early(locate_handle,
  128. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  129. NULL, &size, pci_handle);
  130. }
  131. if (status != EFI_SUCCESS)
  132. goto free_handle;
  133. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  134. while (data && data->next)
  135. data = (struct setup_data *)(unsigned long)data->next;
  136. nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
  137. for (i = 0; i < nr_pci; i++) {
  138. efi_pci_io_protocol_t *pci = NULL;
  139. struct pci_setup_rom *rom;
  140. status = efi_call_early(handle_protocol,
  141. efi_is_64bit() ? ((u64 *)pci_handle)[i]
  142. : ((u32 *)pci_handle)[i],
  143. &pci_proto, (void **)&pci);
  144. if (status != EFI_SUCCESS || !pci)
  145. continue;
  146. status = preserve_pci_rom_image(pci, &rom);
  147. if (status != EFI_SUCCESS)
  148. continue;
  149. if (data)
  150. data->next = (unsigned long)rom;
  151. else
  152. params->hdr.setup_data = (unsigned long)rom;
  153. data = (struct setup_data *)rom;
  154. }
  155. free_handle:
  156. efi_call_early(free_pool, pci_handle);
  157. }
  158. static void retrieve_apple_device_properties(struct boot_params *boot_params)
  159. {
  160. efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
  161. struct setup_data *data, *new;
  162. efi_status_t status;
  163. u32 size = 0;
  164. void *p;
  165. status = efi_call_early(locate_protocol, &guid, NULL, &p);
  166. if (status != EFI_SUCCESS)
  167. return;
  168. if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
  169. efi_printk(sys_table, "Unsupported properties proto version\n");
  170. return;
  171. }
  172. efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
  173. if (!size)
  174. return;
  175. do {
  176. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  177. size + sizeof(struct setup_data), &new);
  178. if (status != EFI_SUCCESS) {
  179. efi_printk(sys_table, "Failed to allocate memory for 'properties'\n");
  180. return;
  181. }
  182. status = efi_call_proto(apple_properties_protocol, get_all, p,
  183. new->data, &size);
  184. if (status == EFI_BUFFER_TOO_SMALL)
  185. efi_call_early(free_pool, new);
  186. } while (status == EFI_BUFFER_TOO_SMALL);
  187. new->type = SETUP_APPLE_PROPERTIES;
  188. new->len = size;
  189. new->next = 0;
  190. data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
  191. if (!data) {
  192. boot_params->hdr.setup_data = (unsigned long)new;
  193. } else {
  194. while (data->next)
  195. data = (struct setup_data *)(unsigned long)data->next;
  196. data->next = (unsigned long)new;
  197. }
  198. }
  199. static const efi_char16_t apple[] = L"Apple";
  200. static void setup_quirks(struct boot_params *boot_params)
  201. {
  202. efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
  203. efi_table_attr(efi_system_table, fw_vendor, sys_table);
  204. if (!memcmp(fw_vendor, apple, sizeof(apple))) {
  205. if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
  206. retrieve_apple_device_properties(boot_params);
  207. }
  208. }
  209. /*
  210. * See if we have Universal Graphics Adapter (UGA) protocol
  211. */
  212. static efi_status_t
  213. setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
  214. {
  215. efi_status_t status;
  216. u32 width, height;
  217. void **uga_handle = NULL;
  218. efi_uga_draw_protocol_t *uga = NULL, *first_uga;
  219. unsigned long nr_ugas;
  220. int i;
  221. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  222. size, (void **)&uga_handle);
  223. if (status != EFI_SUCCESS)
  224. return status;
  225. status = efi_call_early(locate_handle,
  226. EFI_LOCATE_BY_PROTOCOL,
  227. uga_proto, NULL, &size, uga_handle);
  228. if (status != EFI_SUCCESS)
  229. goto free_handle;
  230. height = 0;
  231. width = 0;
  232. first_uga = NULL;
  233. nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));
  234. for (i = 0; i < nr_ugas; i++) {
  235. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  236. u32 w, h, depth, refresh;
  237. void *pciio;
  238. unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
  239. : ((u32 *)uga_handle)[i];
  240. status = efi_call_early(handle_protocol, handle,
  241. uga_proto, (void **)&uga);
  242. if (status != EFI_SUCCESS)
  243. continue;
  244. pciio = NULL;
  245. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  246. status = efi_call_proto(efi_uga_draw_protocol, get_mode, uga,
  247. &w, &h, &depth, &refresh);
  248. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  249. width = w;
  250. height = h;
  251. /*
  252. * Once we've found a UGA supporting PCIIO,
  253. * don't bother looking any further.
  254. */
  255. if (pciio)
  256. break;
  257. first_uga = uga;
  258. }
  259. }
  260. if (!width && !height)
  261. goto free_handle;
  262. /* EFI framebuffer */
  263. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  264. si->lfb_depth = 32;
  265. si->lfb_width = width;
  266. si->lfb_height = height;
  267. si->red_size = 8;
  268. si->red_pos = 16;
  269. si->green_size = 8;
  270. si->green_pos = 8;
  271. si->blue_size = 8;
  272. si->blue_pos = 0;
  273. si->rsvd_size = 8;
  274. si->rsvd_pos = 24;
  275. free_handle:
  276. efi_call_early(free_pool, uga_handle);
  277. return status;
  278. }
  279. void setup_graphics(struct boot_params *boot_params)
  280. {
  281. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  282. struct screen_info *si;
  283. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  284. efi_status_t status;
  285. unsigned long size;
  286. void **gop_handle = NULL;
  287. void **uga_handle = NULL;
  288. si = &boot_params->screen_info;
  289. memset(si, 0, sizeof(*si));
  290. size = 0;
  291. status = efi_call_early(locate_handle,
  292. EFI_LOCATE_BY_PROTOCOL,
  293. &graphics_proto, NULL, &size, gop_handle);
  294. if (status == EFI_BUFFER_TOO_SMALL)
  295. status = efi_setup_gop(NULL, si, &graphics_proto, size);
  296. if (status != EFI_SUCCESS) {
  297. size = 0;
  298. status = efi_call_early(locate_handle,
  299. EFI_LOCATE_BY_PROTOCOL,
  300. &uga_proto, NULL, &size, uga_handle);
  301. if (status == EFI_BUFFER_TOO_SMALL)
  302. setup_uga(si, &uga_proto, size);
  303. }
  304. }
  305. /*
  306. * Because the x86 boot code expects to be passed a boot_params we
  307. * need to create one ourselves (usually the bootloader would create
  308. * one for us).
  309. *
  310. * The caller is responsible for filling out ->code32_start in the
  311. * returned boot_params.
  312. */
  313. struct boot_params *make_boot_params(struct efi_config *c)
  314. {
  315. struct boot_params *boot_params;
  316. struct apm_bios_info *bi;
  317. struct setup_header *hdr;
  318. efi_loaded_image_t *image;
  319. void *options, *handle;
  320. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  321. int options_size = 0;
  322. efi_status_t status;
  323. char *cmdline_ptr;
  324. u16 *s2;
  325. u8 *s1;
  326. int i;
  327. unsigned long ramdisk_addr;
  328. unsigned long ramdisk_size;
  329. efi_early = c;
  330. sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
  331. handle = (void *)(unsigned long)efi_early->image_handle;
  332. /* Check if we were booted by the EFI firmware */
  333. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  334. return NULL;
  335. if (efi_is_64bit())
  336. setup_boot_services64(efi_early);
  337. else
  338. setup_boot_services32(efi_early);
  339. status = efi_call_early(handle_protocol, handle,
  340. &proto, (void *)&image);
  341. if (status != EFI_SUCCESS) {
  342. efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  343. return NULL;
  344. }
  345. status = efi_low_alloc(sys_table, 0x4000, 1,
  346. (unsigned long *)&boot_params);
  347. if (status != EFI_SUCCESS) {
  348. efi_printk(sys_table, "Failed to allocate lowmem for boot params\n");
  349. return NULL;
  350. }
  351. memset(boot_params, 0x0, 0x4000);
  352. hdr = &boot_params->hdr;
  353. bi = &boot_params->apm_bios_info;
  354. /* Copy the second sector to boot_params */
  355. memcpy(&hdr->jump, image->image_base + 512, 512);
  356. /*
  357. * Fill out some of the header fields ourselves because the
  358. * EFI firmware loader doesn't load the first sector.
  359. */
  360. hdr->root_flags = 1;
  361. hdr->vid_mode = 0xffff;
  362. hdr->boot_flag = 0xAA55;
  363. hdr->type_of_loader = 0x21;
  364. /* Convert unicode cmdline to ascii */
  365. cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
  366. if (!cmdline_ptr)
  367. goto fail;
  368. hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
  369. /* Fill in upper bits of command line address, NOP on 32 bit */
  370. boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
  371. hdr->ramdisk_image = 0;
  372. hdr->ramdisk_size = 0;
  373. /* Clear APM BIOS info */
  374. memset(bi, 0, sizeof(*bi));
  375. status = efi_parse_options(cmdline_ptr);
  376. if (status != EFI_SUCCESS)
  377. goto fail2;
  378. status = handle_cmdline_files(sys_table, image,
  379. (char *)(unsigned long)hdr->cmd_line_ptr,
  380. "initrd=", hdr->initrd_addr_max,
  381. &ramdisk_addr, &ramdisk_size);
  382. if (status != EFI_SUCCESS &&
  383. hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
  384. efi_printk(sys_table, "Trying to load files to higher address\n");
  385. status = handle_cmdline_files(sys_table, image,
  386. (char *)(unsigned long)hdr->cmd_line_ptr,
  387. "initrd=", -1UL,
  388. &ramdisk_addr, &ramdisk_size);
  389. }
  390. if (status != EFI_SUCCESS)
  391. goto fail2;
  392. hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
  393. hdr->ramdisk_size = ramdisk_size & 0xffffffff;
  394. boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
  395. boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
  396. return boot_params;
  397. fail2:
  398. efi_free(sys_table, options_size, hdr->cmd_line_ptr);
  399. fail:
  400. efi_free(sys_table, 0x4000, (unsigned long)boot_params);
  401. return NULL;
  402. }
  403. static void add_e820ext(struct boot_params *params,
  404. struct setup_data *e820ext, u32 nr_entries)
  405. {
  406. struct setup_data *data;
  407. efi_status_t status;
  408. unsigned long size;
  409. e820ext->type = SETUP_E820_EXT;
  410. e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
  411. e820ext->next = 0;
  412. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  413. while (data && data->next)
  414. data = (struct setup_data *)(unsigned long)data->next;
  415. if (data)
  416. data->next = (unsigned long)e820ext;
  417. else
  418. params->hdr.setup_data = (unsigned long)e820ext;
  419. }
  420. static efi_status_t
  421. setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
  422. {
  423. struct boot_e820_entry *entry = params->e820_table;
  424. struct efi_info *efi = &params->efi_info;
  425. struct boot_e820_entry *prev = NULL;
  426. u32 nr_entries;
  427. u32 nr_desc;
  428. int i;
  429. nr_entries = 0;
  430. nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
  431. for (i = 0; i < nr_desc; i++) {
  432. efi_memory_desc_t *d;
  433. unsigned int e820_type = 0;
  434. unsigned long m = efi->efi_memmap;
  435. #ifdef CONFIG_X86_64
  436. m |= (u64)efi->efi_memmap_hi << 32;
  437. #endif
  438. d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
  439. switch (d->type) {
  440. case EFI_RESERVED_TYPE:
  441. case EFI_RUNTIME_SERVICES_CODE:
  442. case EFI_RUNTIME_SERVICES_DATA:
  443. case EFI_MEMORY_MAPPED_IO:
  444. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  445. case EFI_PAL_CODE:
  446. e820_type = E820_TYPE_RESERVED;
  447. break;
  448. case EFI_UNUSABLE_MEMORY:
  449. e820_type = E820_TYPE_UNUSABLE;
  450. break;
  451. case EFI_ACPI_RECLAIM_MEMORY:
  452. e820_type = E820_TYPE_ACPI;
  453. break;
  454. case EFI_LOADER_CODE:
  455. case EFI_LOADER_DATA:
  456. case EFI_BOOT_SERVICES_CODE:
  457. case EFI_BOOT_SERVICES_DATA:
  458. case EFI_CONVENTIONAL_MEMORY:
  459. e820_type = E820_TYPE_RAM;
  460. break;
  461. case EFI_ACPI_MEMORY_NVS:
  462. e820_type = E820_TYPE_NVS;
  463. break;
  464. case EFI_PERSISTENT_MEMORY:
  465. e820_type = E820_TYPE_PMEM;
  466. break;
  467. default:
  468. continue;
  469. }
  470. /* Merge adjacent mappings */
  471. if (prev && prev->type == e820_type &&
  472. (prev->addr + prev->size) == d->phys_addr) {
  473. prev->size += d->num_pages << 12;
  474. continue;
  475. }
  476. if (nr_entries == ARRAY_SIZE(params->e820_table)) {
  477. u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
  478. sizeof(struct setup_data);
  479. if (!e820ext || e820ext_size < need)
  480. return EFI_BUFFER_TOO_SMALL;
  481. /* boot_params map full, switch to e820 extended */
  482. entry = (struct boot_e820_entry *)e820ext->data;
  483. }
  484. entry->addr = d->phys_addr;
  485. entry->size = d->num_pages << PAGE_SHIFT;
  486. entry->type = e820_type;
  487. prev = entry++;
  488. nr_entries++;
  489. }
  490. if (nr_entries > ARRAY_SIZE(params->e820_table)) {
  491. u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
  492. add_e820ext(params, e820ext, nr_e820ext);
  493. nr_entries -= nr_e820ext;
  494. }
  495. params->e820_entries = (u8)nr_entries;
  496. return EFI_SUCCESS;
  497. }
  498. static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
  499. u32 *e820ext_size)
  500. {
  501. efi_status_t status;
  502. unsigned long size;
  503. size = sizeof(struct setup_data) +
  504. sizeof(struct e820_entry) * nr_desc;
  505. if (*e820ext) {
  506. efi_call_early(free_pool, *e820ext);
  507. *e820ext = NULL;
  508. *e820ext_size = 0;
  509. }
  510. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  511. size, (void **)e820ext);
  512. if (status == EFI_SUCCESS)
  513. *e820ext_size = size;
  514. return status;
  515. }
  516. static efi_status_t allocate_e820(struct boot_params *params,
  517. struct setup_data **e820ext,
  518. u32 *e820ext_size)
  519. {
  520. unsigned long map_size, desc_size, buff_size;
  521. struct efi_boot_memmap boot_map;
  522. efi_memory_desc_t *map;
  523. efi_status_t status;
  524. __u32 nr_desc;
  525. boot_map.map = &map;
  526. boot_map.map_size = &map_size;
  527. boot_map.desc_size = &desc_size;
  528. boot_map.desc_ver = NULL;
  529. boot_map.key_ptr = NULL;
  530. boot_map.buff_size = &buff_size;
  531. status = efi_get_memory_map(sys_table, &boot_map);
  532. if (status != EFI_SUCCESS)
  533. return status;
  534. nr_desc = buff_size / desc_size;
  535. if (nr_desc > ARRAY_SIZE(params->e820_table)) {
  536. u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
  537. status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
  538. if (status != EFI_SUCCESS)
  539. return status;
  540. }
  541. return EFI_SUCCESS;
  542. }
  543. struct exit_boot_struct {
  544. struct boot_params *boot_params;
  545. struct efi_info *efi;
  546. };
  547. static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
  548. struct efi_boot_memmap *map,
  549. void *priv)
  550. {
  551. const char *signature;
  552. __u32 nr_desc;
  553. efi_status_t status;
  554. struct exit_boot_struct *p = priv;
  555. signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
  556. : EFI32_LOADER_SIGNATURE;
  557. memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
  558. p->efi->efi_systab = (unsigned long)sys_table_arg;
  559. p->efi->efi_memdesc_size = *map->desc_size;
  560. p->efi->efi_memdesc_version = *map->desc_ver;
  561. p->efi->efi_memmap = (unsigned long)*map->map;
  562. p->efi->efi_memmap_size = *map->map_size;
  563. #ifdef CONFIG_X86_64
  564. p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
  565. p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
  566. #endif
  567. return EFI_SUCCESS;
  568. }
  569. static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
  570. {
  571. unsigned long map_sz, key, desc_size, buff_size;
  572. efi_memory_desc_t *mem_map;
  573. struct setup_data *e820ext = NULL;
  574. __u32 e820ext_size = 0;
  575. efi_status_t status;
  576. __u32 desc_version;
  577. struct efi_boot_memmap map;
  578. struct exit_boot_struct priv;
  579. map.map = &mem_map;
  580. map.map_size = &map_sz;
  581. map.desc_size = &desc_size;
  582. map.desc_ver = &desc_version;
  583. map.key_ptr = &key;
  584. map.buff_size = &buff_size;
  585. priv.boot_params = boot_params;
  586. priv.efi = &boot_params->efi_info;
  587. status = allocate_e820(boot_params, &e820ext, &e820ext_size);
  588. if (status != EFI_SUCCESS)
  589. return status;
  590. /* Might as well exit boot services now */
  591. status = efi_exit_boot_services(sys_table, handle, &map, &priv,
  592. exit_boot_func);
  593. if (status != EFI_SUCCESS)
  594. return status;
  595. /* Historic? */
  596. boot_params->alt_mem_k = 32 * 1024;
  597. status = setup_e820(boot_params, e820ext, e820ext_size);
  598. if (status != EFI_SUCCESS)
  599. return status;
  600. return EFI_SUCCESS;
  601. }
  602. /*
  603. * On success we return a pointer to a boot_params structure, and NULL
  604. * on failure.
  605. */
  606. struct boot_params *
  607. efi_main(struct efi_config *c, struct boot_params *boot_params)
  608. {
  609. struct desc_ptr *gdt = NULL;
  610. efi_loaded_image_t *image;
  611. struct setup_header *hdr = &boot_params->hdr;
  612. efi_status_t status;
  613. struct desc_struct *desc;
  614. void *handle;
  615. efi_system_table_t *_table;
  616. unsigned long cmdline_paddr;
  617. efi_early = c;
  618. _table = (efi_system_table_t *)(unsigned long)efi_early->table;
  619. handle = (void *)(unsigned long)efi_early->image_handle;
  620. sys_table = _table;
  621. /* Check if we were booted by the EFI firmware */
  622. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  623. goto fail;
  624. if (efi_is_64bit())
  625. setup_boot_services64(efi_early);
  626. else
  627. setup_boot_services32(efi_early);
  628. /*
  629. * make_boot_params() may have been called before efi_main(), in which
  630. * case this is the second time we parse the cmdline. This is ok,
  631. * parsing the cmdline multiple times does not have side-effects.
  632. */
  633. cmdline_paddr = ((u64)hdr->cmd_line_ptr |
  634. ((u64)boot_params->ext_cmd_line_ptr << 32));
  635. efi_parse_options((char *)cmdline_paddr);
  636. /*
  637. * If the boot loader gave us a value for secure_boot then we use that,
  638. * otherwise we ask the BIOS.
  639. */
  640. if (boot_params->secure_boot == efi_secureboot_mode_unset)
  641. boot_params->secure_boot = efi_get_secureboot(sys_table);
  642. /* Ask the firmware to clear memory on unclean shutdown */
  643. efi_enable_reset_attack_mitigation(sys_table);
  644. efi_retrieve_tpm2_eventlog(sys_table);
  645. setup_graphics(boot_params);
  646. setup_efi_pci(boot_params);
  647. setup_quirks(boot_params);
  648. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  649. sizeof(*gdt), (void **)&gdt);
  650. if (status != EFI_SUCCESS) {
  651. efi_printk(sys_table, "Failed to allocate memory for 'gdt' structure\n");
  652. goto fail;
  653. }
  654. gdt->size = 0x800;
  655. status = efi_low_alloc(sys_table, gdt->size, 8,
  656. (unsigned long *)&gdt->address);
  657. if (status != EFI_SUCCESS) {
  658. efi_printk(sys_table, "Failed to allocate memory for 'gdt'\n");
  659. goto fail;
  660. }
  661. /*
  662. * If the kernel isn't already loaded at the preferred load
  663. * address, relocate it.
  664. */
  665. if (hdr->pref_address != hdr->code32_start) {
  666. unsigned long bzimage_addr = hdr->code32_start;
  667. status = efi_relocate_kernel(sys_table, &bzimage_addr,
  668. hdr->init_size, hdr->init_size,
  669. hdr->pref_address,
  670. hdr->kernel_alignment);
  671. if (status != EFI_SUCCESS) {
  672. efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
  673. goto fail;
  674. }
  675. hdr->pref_address = hdr->code32_start;
  676. hdr->code32_start = bzimage_addr;
  677. }
  678. status = exit_boot(boot_params, handle);
  679. if (status != EFI_SUCCESS) {
  680. efi_printk(sys_table, "exit_boot() failed!\n");
  681. goto fail;
  682. }
  683. memset((char *)gdt->address, 0x0, gdt->size);
  684. desc = (struct desc_struct *)gdt->address;
  685. /* The first GDT is a dummy. */
  686. desc++;
  687. if (IS_ENABLED(CONFIG_X86_64)) {
  688. /* __KERNEL32_CS */
  689. desc->limit0 = 0xffff;
  690. desc->base0 = 0x0000;
  691. desc->base1 = 0x0000;
  692. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  693. desc->s = DESC_TYPE_CODE_DATA;
  694. desc->dpl = 0;
  695. desc->p = 1;
  696. desc->limit1 = 0xf;
  697. desc->avl = 0;
  698. desc->l = 0;
  699. desc->d = SEG_OP_SIZE_32BIT;
  700. desc->g = SEG_GRANULARITY_4KB;
  701. desc->base2 = 0x00;
  702. desc++;
  703. } else {
  704. /* Second entry is unused on 32-bit */
  705. desc++;
  706. }
  707. /* __KERNEL_CS */
  708. desc->limit0 = 0xffff;
  709. desc->base0 = 0x0000;
  710. desc->base1 = 0x0000;
  711. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  712. desc->s = DESC_TYPE_CODE_DATA;
  713. desc->dpl = 0;
  714. desc->p = 1;
  715. desc->limit1 = 0xf;
  716. desc->avl = 0;
  717. if (IS_ENABLED(CONFIG_X86_64)) {
  718. desc->l = 1;
  719. desc->d = 0;
  720. } else {
  721. desc->l = 0;
  722. desc->d = SEG_OP_SIZE_32BIT;
  723. }
  724. desc->g = SEG_GRANULARITY_4KB;
  725. desc->base2 = 0x00;
  726. desc++;
  727. /* __KERNEL_DS */
  728. desc->limit0 = 0xffff;
  729. desc->base0 = 0x0000;
  730. desc->base1 = 0x0000;
  731. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  732. desc->s = DESC_TYPE_CODE_DATA;
  733. desc->dpl = 0;
  734. desc->p = 1;
  735. desc->limit1 = 0xf;
  736. desc->avl = 0;
  737. desc->l = 0;
  738. desc->d = SEG_OP_SIZE_32BIT;
  739. desc->g = SEG_GRANULARITY_4KB;
  740. desc->base2 = 0x00;
  741. desc++;
  742. if (IS_ENABLED(CONFIG_X86_64)) {
  743. /* Task segment value */
  744. desc->limit0 = 0x0000;
  745. desc->base0 = 0x0000;
  746. desc->base1 = 0x0000;
  747. desc->type = SEG_TYPE_TSS;
  748. desc->s = 0;
  749. desc->dpl = 0;
  750. desc->p = 1;
  751. desc->limit1 = 0x0;
  752. desc->avl = 0;
  753. desc->l = 0;
  754. desc->d = 0;
  755. desc->g = SEG_GRANULARITY_4KB;
  756. desc->base2 = 0x00;
  757. desc++;
  758. }
  759. asm volatile("cli");
  760. asm volatile ("lgdt %0" : : "m" (*gdt));
  761. return boot_params;
  762. fail:
  763. efi_printk(sys_table, "efi_main() failed!\n");
  764. return NULL;
  765. }