eboot.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. u32 load_options_size;
  358. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  359. int options_size = 0;
  360. efi_status_t status;
  361. unsigned long cmdline;
  362. u16 *s2;
  363. u8 *s1;
  364. int i;
  365. sys_table = _table;
  366. /* Check if we were booted by the EFI firmware */
  367. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  368. return NULL;
  369. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  370. handle, &proto, (void *)&image);
  371. if (status != EFI_SUCCESS) {
  372. efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  373. return NULL;
  374. }
  375. status = efi_low_alloc(sys_table, 0x4000, 1,
  376. (unsigned long *)&boot_params);
  377. if (status != EFI_SUCCESS) {
  378. efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
  379. return NULL;
  380. }
  381. memset(boot_params, 0x0, 0x4000);
  382. hdr = &boot_params->hdr;
  383. efi = &boot_params->efi_info;
  384. bi = &boot_params->apm_bios_info;
  385. sdt = &boot_params->sys_desc_table;
  386. /* Copy the second sector to boot_params */
  387. memcpy(&hdr->jump, image->image_base + 512, 512);
  388. /*
  389. * Fill out some of the header fields ourselves because the
  390. * EFI firmware loader doesn't load the first sector.
  391. */
  392. hdr->root_flags = 1;
  393. hdr->vid_mode = 0xffff;
  394. hdr->boot_flag = 0xAA55;
  395. hdr->code32_start = (__u64)(unsigned long)image->image_base;
  396. hdr->type_of_loader = 0x21;
  397. /* Convert unicode cmdline to ascii */
  398. options = image->load_options;
  399. load_options_size = image->load_options_size / 2; /* ASCII */
  400. cmdline = 0;
  401. s2 = (u16 *)options;
  402. if (s2) {
  403. while (*s2 && *s2 != '\n' && options_size < load_options_size) {
  404. s2++;
  405. options_size++;
  406. }
  407. if (options_size) {
  408. if (options_size > hdr->cmdline_size)
  409. options_size = hdr->cmdline_size;
  410. options_size++; /* NUL termination */
  411. status = efi_low_alloc(sys_table, options_size, 1,
  412. &cmdline);
  413. if (status != EFI_SUCCESS) {
  414. efi_printk(sys_table, "Failed to alloc mem for cmdline\n");
  415. goto fail;
  416. }
  417. s1 = (u8 *)(unsigned long)cmdline;
  418. s2 = (u16 *)options;
  419. for (i = 0; i < options_size - 1; i++)
  420. *s1++ = *s2++;
  421. *s1 = '\0';
  422. }
  423. }
  424. hdr->cmd_line_ptr = cmdline;
  425. hdr->ramdisk_image = 0;
  426. hdr->ramdisk_size = 0;
  427. /* Clear APM BIOS info */
  428. memset(bi, 0, sizeof(*bi));
  429. memset(sdt, 0, sizeof(*sdt));
  430. status = handle_ramdisks(sys_table, image, hdr);
  431. if (status != EFI_SUCCESS)
  432. goto fail2;
  433. return boot_params;
  434. fail2:
  435. if (options_size)
  436. efi_free(sys_table, options_size, hdr->cmd_line_ptr);
  437. fail:
  438. efi_free(sys_table, 0x4000, (unsigned long)boot_params);
  439. return NULL;
  440. }
  441. static efi_status_t exit_boot(struct boot_params *boot_params,
  442. void *handle)
  443. {
  444. struct efi_info *efi = &boot_params->efi_info;
  445. struct e820entry *e820_map = &boot_params->e820_map[0];
  446. struct e820entry *prev = NULL;
  447. unsigned long size, key, desc_size, _size;
  448. efi_memory_desc_t *mem_map;
  449. efi_status_t status;
  450. __u32 desc_version;
  451. bool called_exit = false;
  452. u8 nr_entries;
  453. int i;
  454. size = sizeof(*mem_map) * 32;
  455. again:
  456. size += sizeof(*mem_map) * 2;
  457. _size = size;
  458. status = efi_low_alloc(sys_table, size, 1, (unsigned long *)&mem_map);
  459. if (status != EFI_SUCCESS)
  460. return status;
  461. get_map:
  462. status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
  463. mem_map, &key, &desc_size, &desc_version);
  464. if (status == EFI_BUFFER_TOO_SMALL) {
  465. efi_free(sys_table, _size, (unsigned long)mem_map);
  466. goto again;
  467. }
  468. if (status != EFI_SUCCESS)
  469. goto free_mem_map;
  470. memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
  471. efi->efi_systab = (unsigned long)sys_table;
  472. efi->efi_memdesc_size = desc_size;
  473. efi->efi_memdesc_version = desc_version;
  474. efi->efi_memmap = (unsigned long)mem_map;
  475. efi->efi_memmap_size = size;
  476. #ifdef CONFIG_X86_64
  477. efi->efi_systab_hi = (unsigned long)sys_table >> 32;
  478. efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
  479. #endif
  480. /* Might as well exit boot services now */
  481. status = efi_call_phys2(sys_table->boottime->exit_boot_services,
  482. handle, key);
  483. if (status != EFI_SUCCESS) {
  484. /*
  485. * ExitBootServices() will fail if any of the event
  486. * handlers change the memory map. In which case, we
  487. * must be prepared to retry, but only once so that
  488. * we're guaranteed to exit on repeated failures instead
  489. * of spinning forever.
  490. */
  491. if (called_exit)
  492. goto free_mem_map;
  493. called_exit = true;
  494. goto get_map;
  495. }
  496. /* Historic? */
  497. boot_params->alt_mem_k = 32 * 1024;
  498. /*
  499. * Convert the EFI memory map to E820.
  500. */
  501. nr_entries = 0;
  502. for (i = 0; i < size / desc_size; i++) {
  503. efi_memory_desc_t *d;
  504. unsigned int e820_type = 0;
  505. unsigned long m = (unsigned long)mem_map;
  506. d = (efi_memory_desc_t *)(m + (i * desc_size));
  507. switch (d->type) {
  508. case EFI_RESERVED_TYPE:
  509. case EFI_RUNTIME_SERVICES_CODE:
  510. case EFI_RUNTIME_SERVICES_DATA:
  511. case EFI_MEMORY_MAPPED_IO:
  512. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  513. case EFI_PAL_CODE:
  514. e820_type = E820_RESERVED;
  515. break;
  516. case EFI_UNUSABLE_MEMORY:
  517. e820_type = E820_UNUSABLE;
  518. break;
  519. case EFI_ACPI_RECLAIM_MEMORY:
  520. e820_type = E820_ACPI;
  521. break;
  522. case EFI_LOADER_CODE:
  523. case EFI_LOADER_DATA:
  524. case EFI_BOOT_SERVICES_CODE:
  525. case EFI_BOOT_SERVICES_DATA:
  526. case EFI_CONVENTIONAL_MEMORY:
  527. e820_type = E820_RAM;
  528. break;
  529. case EFI_ACPI_MEMORY_NVS:
  530. e820_type = E820_NVS;
  531. break;
  532. default:
  533. continue;
  534. }
  535. /* Merge adjacent mappings */
  536. if (prev && prev->type == e820_type &&
  537. (prev->addr + prev->size) == d->phys_addr)
  538. prev->size += d->num_pages << 12;
  539. else {
  540. e820_map->addr = d->phys_addr;
  541. e820_map->size = d->num_pages << 12;
  542. e820_map->type = e820_type;
  543. prev = e820_map++;
  544. nr_entries++;
  545. }
  546. }
  547. boot_params->e820_entries = nr_entries;
  548. return EFI_SUCCESS;
  549. free_mem_map:
  550. efi_free(sys_table, _size, (unsigned long)mem_map);
  551. return status;
  552. }
  553. static efi_status_t relocate_kernel(struct setup_header *hdr)
  554. {
  555. unsigned long start, nr_pages;
  556. efi_status_t status;
  557. /*
  558. * The EFI firmware loader could have placed the kernel image
  559. * anywhere in memory, but the kernel has various restrictions
  560. * on the max physical address it can run at. Attempt to move
  561. * the kernel to boot_params.pref_address, or as low as
  562. * possible.
  563. */
  564. start = hdr->pref_address;
  565. nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  566. status = efi_call_phys4(sys_table->boottime->allocate_pages,
  567. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  568. nr_pages, &start);
  569. if (status != EFI_SUCCESS) {
  570. status = efi_low_alloc(sys_table, hdr->init_size,
  571. hdr->kernel_alignment, &start);
  572. if (status != EFI_SUCCESS)
  573. efi_printk(sys_table, "Failed to alloc mem for kernel\n");
  574. }
  575. if (status == EFI_SUCCESS)
  576. memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
  577. hdr->init_size);
  578. hdr->pref_address = hdr->code32_start;
  579. hdr->code32_start = (__u32)start;
  580. return status;
  581. }
  582. /*
  583. * On success we return a pointer to a boot_params structure, and NULL
  584. * on failure.
  585. */
  586. struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
  587. struct boot_params *boot_params)
  588. {
  589. struct desc_ptr *gdt, *idt;
  590. efi_loaded_image_t *image;
  591. struct setup_header *hdr = &boot_params->hdr;
  592. efi_status_t status;
  593. struct desc_struct *desc;
  594. sys_table = _table;
  595. /* Check if we were booted by the EFI firmware */
  596. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  597. goto fail;
  598. setup_graphics(boot_params);
  599. setup_efi_pci(boot_params);
  600. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  601. EFI_LOADER_DATA, sizeof(*gdt),
  602. (void **)&gdt);
  603. if (status != EFI_SUCCESS) {
  604. efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
  605. goto fail;
  606. }
  607. gdt->size = 0x800;
  608. status = efi_low_alloc(sys_table, gdt->size, 8,
  609. (unsigned long *)&gdt->address);
  610. if (status != EFI_SUCCESS) {
  611. efi_printk(sys_table, "Failed to alloc mem for gdt\n");
  612. goto fail;
  613. }
  614. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  615. EFI_LOADER_DATA, sizeof(*idt),
  616. (void **)&idt);
  617. if (status != EFI_SUCCESS) {
  618. efi_printk(sys_table, "Failed to alloc mem for idt structure\n");
  619. goto fail;
  620. }
  621. idt->size = 0;
  622. idt->address = 0;
  623. /*
  624. * If the kernel isn't already loaded at the preferred load
  625. * address, relocate it.
  626. */
  627. if (hdr->pref_address != hdr->code32_start) {
  628. status = relocate_kernel(hdr);
  629. if (status != EFI_SUCCESS)
  630. goto fail;
  631. }
  632. status = exit_boot(boot_params, handle);
  633. if (status != EFI_SUCCESS)
  634. goto fail;
  635. memset((char *)gdt->address, 0x0, gdt->size);
  636. desc = (struct desc_struct *)gdt->address;
  637. /* The first GDT is a dummy and the second is unused. */
  638. desc += 2;
  639. desc->limit0 = 0xffff;
  640. desc->base0 = 0x0000;
  641. desc->base1 = 0x0000;
  642. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  643. desc->s = DESC_TYPE_CODE_DATA;
  644. desc->dpl = 0;
  645. desc->p = 1;
  646. desc->limit = 0xf;
  647. desc->avl = 0;
  648. desc->l = 0;
  649. desc->d = SEG_OP_SIZE_32BIT;
  650. desc->g = SEG_GRANULARITY_4KB;
  651. desc->base2 = 0x00;
  652. desc++;
  653. desc->limit0 = 0xffff;
  654. desc->base0 = 0x0000;
  655. desc->base1 = 0x0000;
  656. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  657. desc->s = DESC_TYPE_CODE_DATA;
  658. desc->dpl = 0;
  659. desc->p = 1;
  660. desc->limit = 0xf;
  661. desc->avl = 0;
  662. desc->l = 0;
  663. desc->d = SEG_OP_SIZE_32BIT;
  664. desc->g = SEG_GRANULARITY_4KB;
  665. desc->base2 = 0x00;
  666. #ifdef CONFIG_X86_64
  667. /* Task segment value */
  668. desc++;
  669. desc->limit0 = 0x0000;
  670. desc->base0 = 0x0000;
  671. desc->base1 = 0x0000;
  672. desc->type = SEG_TYPE_TSS;
  673. desc->s = 0;
  674. desc->dpl = 0;
  675. desc->p = 1;
  676. desc->limit = 0x0;
  677. desc->avl = 0;
  678. desc->l = 0;
  679. desc->d = 0;
  680. desc->g = SEG_GRANULARITY_4KB;
  681. desc->base2 = 0x00;
  682. #endif /* CONFIG_X86_64 */
  683. asm volatile ("lidt %0" : : "m" (*idt));
  684. asm volatile ("lgdt %0" : : "m" (*gdt));
  685. asm volatile("cli");
  686. return boot_params;
  687. fail:
  688. return NULL;
  689. }