eboot.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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/setup.h>
  13. #include <asm/desc.h>
  14. #undef memcpy /* Use memcpy from misc.c */
  15. #include "eboot.h"
  16. static efi_system_table_t *sys_table;
  17. #include "../../../../drivers/firmware/efi/efi-stub-helper.c"
  18. static void find_bits(unsigned long mask, u8 *pos, u8 *size)
  19. {
  20. u8 first, len;
  21. first = 0;
  22. len = 0;
  23. if (mask) {
  24. while (!(mask & 0x1)) {
  25. mask = mask >> 1;
  26. first++;
  27. }
  28. while (mask & 0x1) {
  29. mask = mask >> 1;
  30. len++;
  31. }
  32. }
  33. *pos = first;
  34. *size = len;
  35. }
  36. static efi_status_t setup_efi_pci(struct boot_params *params)
  37. {
  38. efi_pci_io_protocol *pci;
  39. efi_status_t status;
  40. void **pci_handle;
  41. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  42. unsigned long nr_pci, size = 0;
  43. int i;
  44. struct setup_data *data;
  45. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  46. while (data && data->next)
  47. data = (struct setup_data *)(unsigned long)data->next;
  48. status = efi_call_phys5(sys_table->boottime->locate_handle,
  49. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  50. NULL, &size, pci_handle);
  51. if (status == EFI_BUFFER_TOO_SMALL) {
  52. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  53. EFI_LOADER_DATA, size, &pci_handle);
  54. if (status != EFI_SUCCESS)
  55. return status;
  56. status = efi_call_phys5(sys_table->boottime->locate_handle,
  57. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  58. NULL, &size, pci_handle);
  59. }
  60. if (status != EFI_SUCCESS)
  61. goto free_handle;
  62. nr_pci = size / sizeof(void *);
  63. for (i = 0; i < nr_pci; i++) {
  64. void *h = pci_handle[i];
  65. uint64_t attributes;
  66. struct pci_setup_rom *rom;
  67. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  68. h, &pci_proto, &pci);
  69. if (status != EFI_SUCCESS)
  70. continue;
  71. if (!pci)
  72. continue;
  73. #ifdef CONFIG_X86_64
  74. status = efi_call_phys4(pci->attributes, pci,
  75. EfiPciIoAttributeOperationGet, 0,
  76. &attributes);
  77. #else
  78. status = efi_call_phys5(pci->attributes, pci,
  79. EfiPciIoAttributeOperationGet, 0, 0,
  80. &attributes);
  81. #endif
  82. if (status != EFI_SUCCESS)
  83. continue;
  84. if (!pci->romimage || !pci->romsize)
  85. continue;
  86. size = pci->romsize + sizeof(*rom);
  87. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  88. EFI_LOADER_DATA, size, &rom);
  89. if (status != EFI_SUCCESS)
  90. continue;
  91. rom->data.type = SETUP_PCI;
  92. rom->data.len = size - sizeof(struct setup_data);
  93. rom->data.next = 0;
  94. rom->pcilen = pci->romsize;
  95. status = efi_call_phys5(pci->pci.read, pci,
  96. EfiPciIoWidthUint16, PCI_VENDOR_ID,
  97. 1, &(rom->vendor));
  98. if (status != EFI_SUCCESS)
  99. goto free_struct;
  100. status = efi_call_phys5(pci->pci.read, pci,
  101. EfiPciIoWidthUint16, PCI_DEVICE_ID,
  102. 1, &(rom->devid));
  103. if (status != EFI_SUCCESS)
  104. goto free_struct;
  105. status = efi_call_phys5(pci->get_location, pci,
  106. &(rom->segment), &(rom->bus),
  107. &(rom->device), &(rom->function));
  108. if (status != EFI_SUCCESS)
  109. goto free_struct;
  110. memcpy(rom->romdata, pci->romimage, pci->romsize);
  111. if (data)
  112. data->next = (unsigned long)rom;
  113. else
  114. params->hdr.setup_data = (unsigned long)rom;
  115. data = (struct setup_data *)rom;
  116. continue;
  117. free_struct:
  118. efi_call_phys1(sys_table->boottime->free_pool, rom);
  119. }
  120. free_handle:
  121. efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
  122. return status;
  123. }
  124. /*
  125. * See if we have Graphics Output Protocol
  126. */
  127. static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
  128. unsigned long size)
  129. {
  130. struct efi_graphics_output_protocol *gop, *first_gop;
  131. struct efi_pixel_bitmask pixel_info;
  132. unsigned long nr_gops;
  133. efi_status_t status;
  134. void **gop_handle;
  135. u16 width, height;
  136. u32 fb_base, fb_size;
  137. u32 pixels_per_scan_line;
  138. int pixel_format;
  139. int i;
  140. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  141. EFI_LOADER_DATA, size, &gop_handle);
  142. if (status != EFI_SUCCESS)
  143. return status;
  144. status = efi_call_phys5(sys_table->boottime->locate_handle,
  145. EFI_LOCATE_BY_PROTOCOL, proto,
  146. NULL, &size, gop_handle);
  147. if (status != EFI_SUCCESS)
  148. goto free_handle;
  149. first_gop = NULL;
  150. nr_gops = size / sizeof(void *);
  151. for (i = 0; i < nr_gops; i++) {
  152. struct efi_graphics_output_mode_info *info;
  153. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  154. bool conout_found = false;
  155. void *dummy;
  156. void *h = gop_handle[i];
  157. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  158. h, proto, &gop);
  159. if (status != EFI_SUCCESS)
  160. continue;
  161. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  162. h, &conout_proto, &dummy);
  163. if (status == EFI_SUCCESS)
  164. conout_found = true;
  165. status = efi_call_phys4(gop->query_mode, gop,
  166. gop->mode->mode, &size, &info);
  167. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  168. /*
  169. * Systems that use the UEFI Console Splitter may
  170. * provide multiple GOP devices, not all of which are
  171. * backed by real hardware. The workaround is to search
  172. * for a GOP implementing the ConOut protocol, and if
  173. * one isn't found, to just fall back to the first GOP.
  174. */
  175. width = info->horizontal_resolution;
  176. height = info->vertical_resolution;
  177. fb_base = gop->mode->frame_buffer_base;
  178. fb_size = gop->mode->frame_buffer_size;
  179. pixel_format = info->pixel_format;
  180. pixel_info = info->pixel_information;
  181. pixels_per_scan_line = info->pixels_per_scan_line;
  182. /*
  183. * Once we've found a GOP supporting ConOut,
  184. * don't bother looking any further.
  185. */
  186. first_gop = gop;
  187. if (conout_found)
  188. break;
  189. }
  190. }
  191. /* Did we find any GOPs? */
  192. if (!first_gop)
  193. goto free_handle;
  194. /* EFI framebuffer */
  195. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  196. si->lfb_width = width;
  197. si->lfb_height = height;
  198. si->lfb_base = fb_base;
  199. si->pages = 1;
  200. if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
  201. si->lfb_depth = 32;
  202. si->lfb_linelength = pixels_per_scan_line * 4;
  203. si->red_size = 8;
  204. si->red_pos = 0;
  205. si->green_size = 8;
  206. si->green_pos = 8;
  207. si->blue_size = 8;
  208. si->blue_pos = 16;
  209. si->rsvd_size = 8;
  210. si->rsvd_pos = 24;
  211. } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
  212. si->lfb_depth = 32;
  213. si->lfb_linelength = pixels_per_scan_line * 4;
  214. si->red_size = 8;
  215. si->red_pos = 16;
  216. si->green_size = 8;
  217. si->green_pos = 8;
  218. si->blue_size = 8;
  219. si->blue_pos = 0;
  220. si->rsvd_size = 8;
  221. si->rsvd_pos = 24;
  222. } else if (pixel_format == PIXEL_BIT_MASK) {
  223. find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
  224. find_bits(pixel_info.green_mask, &si->green_pos,
  225. &si->green_size);
  226. find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
  227. find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
  228. &si->rsvd_size);
  229. si->lfb_depth = si->red_size + si->green_size +
  230. si->blue_size + si->rsvd_size;
  231. si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
  232. } else {
  233. si->lfb_depth = 4;
  234. si->lfb_linelength = si->lfb_width / 2;
  235. si->red_size = 0;
  236. si->red_pos = 0;
  237. si->green_size = 0;
  238. si->green_pos = 0;
  239. si->blue_size = 0;
  240. si->blue_pos = 0;
  241. si->rsvd_size = 0;
  242. si->rsvd_pos = 0;
  243. }
  244. si->lfb_size = si->lfb_linelength * si->lfb_height;
  245. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  246. free_handle:
  247. efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
  248. return status;
  249. }
  250. /*
  251. * See if we have Universal Graphics Adapter (UGA) protocol
  252. */
  253. static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
  254. unsigned long size)
  255. {
  256. struct efi_uga_draw_protocol *uga, *first_uga;
  257. unsigned long nr_ugas;
  258. efi_status_t status;
  259. u32 width, height;
  260. void **uga_handle = NULL;
  261. int i;
  262. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  263. EFI_LOADER_DATA, size, &uga_handle);
  264. if (status != EFI_SUCCESS)
  265. return status;
  266. status = efi_call_phys5(sys_table->boottime->locate_handle,
  267. EFI_LOCATE_BY_PROTOCOL, uga_proto,
  268. NULL, &size, uga_handle);
  269. if (status != EFI_SUCCESS)
  270. goto free_handle;
  271. first_uga = NULL;
  272. nr_ugas = size / sizeof(void *);
  273. for (i = 0; i < nr_ugas; i++) {
  274. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  275. void *handle = uga_handle[i];
  276. u32 w, h, depth, refresh;
  277. void *pciio;
  278. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  279. handle, uga_proto, &uga);
  280. if (status != EFI_SUCCESS)
  281. continue;
  282. efi_call_phys3(sys_table->boottime->handle_protocol,
  283. handle, &pciio_proto, &pciio);
  284. status = efi_call_phys5(uga->get_mode, uga, &w, &h,
  285. &depth, &refresh);
  286. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  287. width = w;
  288. height = h;
  289. /*
  290. * Once we've found a UGA supporting PCIIO,
  291. * don't bother looking any further.
  292. */
  293. if (pciio)
  294. break;
  295. first_uga = uga;
  296. }
  297. }
  298. if (!first_uga)
  299. goto free_handle;
  300. /* EFI framebuffer */
  301. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  302. si->lfb_depth = 32;
  303. si->lfb_width = width;
  304. si->lfb_height = height;
  305. si->red_size = 8;
  306. si->red_pos = 16;
  307. si->green_size = 8;
  308. si->green_pos = 8;
  309. si->blue_size = 8;
  310. si->blue_pos = 0;
  311. si->rsvd_size = 8;
  312. si->rsvd_pos = 24;
  313. free_handle:
  314. efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
  315. return status;
  316. }
  317. void setup_graphics(struct boot_params *boot_params)
  318. {
  319. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  320. struct screen_info *si;
  321. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  322. efi_status_t status;
  323. unsigned long size;
  324. void **gop_handle = NULL;
  325. void **uga_handle = NULL;
  326. si = &boot_params->screen_info;
  327. memset(si, 0, sizeof(*si));
  328. size = 0;
  329. status = efi_call_phys5(sys_table->boottime->locate_handle,
  330. EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
  331. NULL, &size, gop_handle);
  332. if (status == EFI_BUFFER_TOO_SMALL)
  333. status = setup_gop(si, &graphics_proto, size);
  334. if (status != EFI_SUCCESS) {
  335. size = 0;
  336. status = efi_call_phys5(sys_table->boottime->locate_handle,
  337. EFI_LOCATE_BY_PROTOCOL, &uga_proto,
  338. NULL, &size, uga_handle);
  339. if (status == EFI_BUFFER_TOO_SMALL)
  340. setup_uga(si, &uga_proto, size);
  341. }
  342. }
  343. /*
  344. * Because the x86 boot code expects to be passed a boot_params we
  345. * need to create one ourselves (usually the bootloader would create
  346. * one for us).
  347. */
  348. struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
  349. {
  350. struct boot_params *boot_params;
  351. struct sys_desc_table *sdt;
  352. struct apm_bios_info *bi;
  353. struct setup_header *hdr;
  354. struct efi_info *efi;
  355. efi_loaded_image_t *image;
  356. void *options;
  357. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  358. int options_size = 0;
  359. efi_status_t status;
  360. char *cmdline_ptr;
  361. u16 *s2;
  362. u8 *s1;
  363. int i;
  364. unsigned long ramdisk_addr;
  365. unsigned long ramdisk_size;
  366. sys_table = _table;
  367. /* Check if we were booted by the EFI firmware */
  368. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  369. return NULL;
  370. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  371. handle, &proto, (void *)&image);
  372. if (status != EFI_SUCCESS) {
  373. efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  374. return NULL;
  375. }
  376. status = efi_low_alloc(sys_table, 0x4000, 1,
  377. (unsigned long *)&boot_params);
  378. if (status != EFI_SUCCESS) {
  379. efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
  380. return NULL;
  381. }
  382. memset(boot_params, 0x0, 0x4000);
  383. hdr = &boot_params->hdr;
  384. efi = &boot_params->efi_info;
  385. bi = &boot_params->apm_bios_info;
  386. sdt = &boot_params->sys_desc_table;
  387. /* Copy the second sector to boot_params */
  388. memcpy(&hdr->jump, image->image_base + 512, 512);
  389. /*
  390. * Fill out some of the header fields ourselves because the
  391. * EFI firmware loader doesn't load the first sector.
  392. */
  393. hdr->root_flags = 1;
  394. hdr->vid_mode = 0xffff;
  395. hdr->boot_flag = 0xAA55;
  396. hdr->code32_start = (__u64)(unsigned long)image->image_base;
  397. hdr->type_of_loader = 0x21;
  398. /* Convert unicode cmdline to ascii */
  399. cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image,
  400. &options_size);
  401. if (!cmdline_ptr)
  402. goto fail;
  403. hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
  404. hdr->ramdisk_image = 0;
  405. hdr->ramdisk_size = 0;
  406. /* Clear APM BIOS info */
  407. memset(bi, 0, sizeof(*bi));
  408. memset(sdt, 0, sizeof(*sdt));
  409. status = handle_cmdline_files(sys_table, image,
  410. (char *)(unsigned long)hdr->cmd_line_ptr,
  411. "initrd=", hdr->initrd_addr_max,
  412. &ramdisk_addr, &ramdisk_size);
  413. if (status != EFI_SUCCESS)
  414. goto fail2;
  415. hdr->ramdisk_image = ramdisk_addr;
  416. hdr->ramdisk_size = ramdisk_size;
  417. return boot_params;
  418. fail2:
  419. efi_free(sys_table, options_size, hdr->cmd_line_ptr);
  420. fail:
  421. efi_free(sys_table, 0x4000, (unsigned long)boot_params);
  422. return NULL;
  423. }
  424. static void add_e820ext(struct boot_params *params,
  425. struct setup_data *e820ext, u32 nr_entries)
  426. {
  427. struct setup_data *data;
  428. efi_status_t status;
  429. unsigned long size;
  430. e820ext->type = SETUP_E820_EXT;
  431. e820ext->len = nr_entries * sizeof(struct e820entry);
  432. e820ext->next = 0;
  433. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  434. while (data && data->next)
  435. data = (struct setup_data *)(unsigned long)data->next;
  436. if (data)
  437. data->next = (unsigned long)e820ext;
  438. else
  439. params->hdr.setup_data = (unsigned long)e820ext;
  440. }
  441. static efi_status_t setup_e820(struct boot_params *params,
  442. struct setup_data *e820ext, u32 e820ext_size)
  443. {
  444. struct e820entry *e820_map = &params->e820_map[0];
  445. struct efi_info *efi = &params->efi_info;
  446. struct e820entry *prev = NULL;
  447. u32 nr_entries;
  448. u32 nr_desc;
  449. int i;
  450. nr_entries = 0;
  451. nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
  452. for (i = 0; i < nr_desc; i++) {
  453. efi_memory_desc_t *d;
  454. unsigned int e820_type = 0;
  455. unsigned long m = efi->efi_memmap;
  456. d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
  457. switch (d->type) {
  458. case EFI_RESERVED_TYPE:
  459. case EFI_RUNTIME_SERVICES_CODE:
  460. case EFI_RUNTIME_SERVICES_DATA:
  461. case EFI_MEMORY_MAPPED_IO:
  462. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  463. case EFI_PAL_CODE:
  464. e820_type = E820_RESERVED;
  465. break;
  466. case EFI_UNUSABLE_MEMORY:
  467. e820_type = E820_UNUSABLE;
  468. break;
  469. case EFI_ACPI_RECLAIM_MEMORY:
  470. e820_type = E820_ACPI;
  471. break;
  472. case EFI_LOADER_CODE:
  473. case EFI_LOADER_DATA:
  474. case EFI_BOOT_SERVICES_CODE:
  475. case EFI_BOOT_SERVICES_DATA:
  476. case EFI_CONVENTIONAL_MEMORY:
  477. e820_type = E820_RAM;
  478. break;
  479. case EFI_ACPI_MEMORY_NVS:
  480. e820_type = E820_NVS;
  481. break;
  482. default:
  483. continue;
  484. }
  485. /* Merge adjacent mappings */
  486. if (prev && prev->type == e820_type &&
  487. (prev->addr + prev->size) == d->phys_addr) {
  488. prev->size += d->num_pages << 12;
  489. continue;
  490. }
  491. if (nr_entries == ARRAY_SIZE(params->e820_map)) {
  492. u32 need = (nr_desc - i) * sizeof(struct e820entry) +
  493. sizeof(struct setup_data);
  494. if (!e820ext || e820ext_size < need)
  495. return EFI_BUFFER_TOO_SMALL;
  496. /* boot_params map full, switch to e820 extended */
  497. e820_map = (struct e820entry *)e820ext->data;
  498. }
  499. e820_map->addr = d->phys_addr;
  500. e820_map->size = d->num_pages << PAGE_SHIFT;
  501. e820_map->type = e820_type;
  502. prev = e820_map++;
  503. nr_entries++;
  504. }
  505. if (nr_entries > ARRAY_SIZE(params->e820_map)) {
  506. u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
  507. add_e820ext(params, e820ext, nr_e820ext);
  508. nr_entries -= nr_e820ext;
  509. }
  510. params->e820_entries = (u8)nr_entries;
  511. return EFI_SUCCESS;
  512. }
  513. static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
  514. u32 *e820ext_size)
  515. {
  516. efi_status_t status;
  517. unsigned long size;
  518. size = sizeof(struct setup_data) +
  519. sizeof(struct e820entry) * nr_desc;
  520. if (*e820ext) {
  521. efi_call_phys1(sys_table->boottime->free_pool, *e820ext);
  522. *e820ext = NULL;
  523. *e820ext_size = 0;
  524. }
  525. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  526. EFI_LOADER_DATA, size, e820ext);
  527. if (status == EFI_SUCCESS)
  528. *e820ext_size = size;
  529. return status;
  530. }
  531. static efi_status_t exit_boot(struct boot_params *boot_params,
  532. void *handle)
  533. {
  534. struct efi_info *efi = &boot_params->efi_info;
  535. unsigned long map_sz, key, desc_size;
  536. efi_memory_desc_t *mem_map;
  537. struct setup_data *e820ext;
  538. __u32 e820ext_size;
  539. __u32 nr_desc, prev_nr_desc;
  540. efi_status_t status;
  541. __u32 desc_version;
  542. bool called_exit = false;
  543. u8 nr_entries;
  544. int i;
  545. nr_desc = 0;
  546. e820ext = NULL;
  547. e820ext_size = 0;
  548. get_map:
  549. status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
  550. &desc_version, &key);
  551. if (status != EFI_SUCCESS)
  552. return status;
  553. prev_nr_desc = nr_desc;
  554. nr_desc = map_sz / desc_size;
  555. if (nr_desc > prev_nr_desc &&
  556. nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
  557. u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
  558. status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
  559. if (status != EFI_SUCCESS)
  560. goto free_mem_map;
  561. efi_call_phys1(sys_table->boottime->free_pool, mem_map);
  562. goto get_map; /* Allocated memory, get map again */
  563. }
  564. memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
  565. efi->efi_systab = (unsigned long)sys_table;
  566. efi->efi_memdesc_size = desc_size;
  567. efi->efi_memdesc_version = desc_version;
  568. efi->efi_memmap = (unsigned long)mem_map;
  569. efi->efi_memmap_size = map_sz;
  570. #ifdef CONFIG_X86_64
  571. efi->efi_systab_hi = (unsigned long)sys_table >> 32;
  572. efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
  573. #endif
  574. /* Might as well exit boot services now */
  575. status = efi_call_phys2(sys_table->boottime->exit_boot_services,
  576. handle, key);
  577. if (status != EFI_SUCCESS) {
  578. /*
  579. * ExitBootServices() will fail if any of the event
  580. * handlers change the memory map. In which case, we
  581. * must be prepared to retry, but only once so that
  582. * we're guaranteed to exit on repeated failures instead
  583. * of spinning forever.
  584. */
  585. if (called_exit)
  586. goto free_mem_map;
  587. called_exit = true;
  588. efi_call_phys1(sys_table->boottime->free_pool, mem_map);
  589. goto get_map;
  590. }
  591. /* Historic? */
  592. boot_params->alt_mem_k = 32 * 1024;
  593. status = setup_e820(boot_params, e820ext, e820ext_size);
  594. if (status != EFI_SUCCESS)
  595. return status;
  596. return EFI_SUCCESS;
  597. free_mem_map:
  598. efi_call_phys1(sys_table->boottime->free_pool, mem_map);
  599. return status;
  600. }
  601. /*
  602. * On success we return a pointer to a boot_params structure, and NULL
  603. * on failure.
  604. */
  605. struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
  606. struct boot_params *boot_params)
  607. {
  608. struct desc_ptr *gdt;
  609. efi_loaded_image_t *image;
  610. struct setup_header *hdr = &boot_params->hdr;
  611. efi_status_t status;
  612. struct desc_struct *desc;
  613. sys_table = _table;
  614. /* Check if we were booted by the EFI firmware */
  615. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  616. goto fail;
  617. setup_graphics(boot_params);
  618. setup_efi_pci(boot_params);
  619. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  620. EFI_LOADER_DATA, sizeof(*gdt),
  621. (void **)&gdt);
  622. if (status != EFI_SUCCESS) {
  623. efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
  624. goto fail;
  625. }
  626. gdt->size = 0x800;
  627. status = efi_low_alloc(sys_table, gdt->size, 8,
  628. (unsigned long *)&gdt->address);
  629. if (status != EFI_SUCCESS) {
  630. efi_printk(sys_table, "Failed to alloc mem for gdt\n");
  631. goto fail;
  632. }
  633. /*
  634. * If the kernel isn't already loaded at the preferred load
  635. * address, relocate it.
  636. */
  637. if (hdr->pref_address != hdr->code32_start) {
  638. unsigned long bzimage_addr = hdr->code32_start;
  639. status = efi_relocate_kernel(sys_table, &bzimage_addr,
  640. hdr->init_size, hdr->init_size,
  641. hdr->pref_address,
  642. hdr->kernel_alignment);
  643. if (status != EFI_SUCCESS)
  644. goto fail;
  645. hdr->pref_address = hdr->code32_start;
  646. hdr->code32_start = bzimage_addr;
  647. }
  648. status = exit_boot(boot_params, handle);
  649. if (status != EFI_SUCCESS)
  650. goto fail;
  651. memset((char *)gdt->address, 0x0, gdt->size);
  652. desc = (struct desc_struct *)gdt->address;
  653. /* The first GDT is a dummy and the second is unused. */
  654. desc += 2;
  655. desc->limit0 = 0xffff;
  656. desc->base0 = 0x0000;
  657. desc->base1 = 0x0000;
  658. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  659. desc->s = DESC_TYPE_CODE_DATA;
  660. desc->dpl = 0;
  661. desc->p = 1;
  662. desc->limit = 0xf;
  663. desc->avl = 0;
  664. desc->l = 0;
  665. desc->d = SEG_OP_SIZE_32BIT;
  666. desc->g = SEG_GRANULARITY_4KB;
  667. desc->base2 = 0x00;
  668. desc++;
  669. desc->limit0 = 0xffff;
  670. desc->base0 = 0x0000;
  671. desc->base1 = 0x0000;
  672. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  673. desc->s = DESC_TYPE_CODE_DATA;
  674. desc->dpl = 0;
  675. desc->p = 1;
  676. desc->limit = 0xf;
  677. desc->avl = 0;
  678. desc->l = 0;
  679. desc->d = SEG_OP_SIZE_32BIT;
  680. desc->g = SEG_GRANULARITY_4KB;
  681. desc->base2 = 0x00;
  682. #ifdef CONFIG_X86_64
  683. /* Task segment value */
  684. desc++;
  685. desc->limit0 = 0x0000;
  686. desc->base0 = 0x0000;
  687. desc->base1 = 0x0000;
  688. desc->type = SEG_TYPE_TSS;
  689. desc->s = 0;
  690. desc->dpl = 0;
  691. desc->p = 1;
  692. desc->limit = 0x0;
  693. desc->avl = 0;
  694. desc->l = 0;
  695. desc->d = 0;
  696. desc->g = SEG_GRANULARITY_4KB;
  697. desc->base2 = 0x00;
  698. #endif /* CONFIG_X86_64 */
  699. asm volatile("cli");
  700. asm volatile ("lgdt %0" : : "m" (*gdt));
  701. return boot_params;
  702. fail:
  703. return NULL;
  704. }