eboot.c 19 KB

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