eboot.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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. static struct efi_config *efi_early;
  18. #define efi_call_early(f, ...) \
  19. efi_early->call(efi_early->f, __VA_ARGS__);
  20. #define BOOT_SERVICES(bits) \
  21. static void setup_boot_services##bits(struct efi_config *c) \
  22. { \
  23. efi_system_table_##bits##_t *table; \
  24. efi_boot_services_##bits##_t *bt; \
  25. \
  26. table = (typeof(table))sys_table; \
  27. \
  28. c->text_output = table->con_out; \
  29. \
  30. bt = (typeof(bt))(unsigned long)(table->boottime); \
  31. \
  32. c->allocate_pool = bt->allocate_pool; \
  33. c->allocate_pages = bt->allocate_pages; \
  34. c->get_memory_map = bt->get_memory_map; \
  35. c->free_pool = bt->free_pool; \
  36. c->free_pages = bt->free_pages; \
  37. c->locate_handle = bt->locate_handle; \
  38. c->handle_protocol = bt->handle_protocol; \
  39. c->exit_boot_services = bt->exit_boot_services; \
  40. }
  41. BOOT_SERVICES(32);
  42. BOOT_SERVICES(64);
  43. static void efi_printk(efi_system_table_t *, char *);
  44. static void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
  45. static efi_status_t
  46. __file_size32(void *__fh, efi_char16_t *filename_16,
  47. void **handle, u64 *file_sz)
  48. {
  49. efi_file_handle_32_t *h, *fh = __fh;
  50. efi_file_info_t *info;
  51. efi_status_t status;
  52. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  53. u32 info_sz;
  54. status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
  55. EFI_FILE_MODE_READ, (u64)0);
  56. if (status != EFI_SUCCESS) {
  57. efi_printk(sys_table, "Failed to open file: ");
  58. efi_char16_printk(sys_table, filename_16);
  59. efi_printk(sys_table, "\n");
  60. return status;
  61. }
  62. *handle = h;
  63. info_sz = 0;
  64. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  65. &info_sz, NULL);
  66. if (status != EFI_BUFFER_TOO_SMALL) {
  67. efi_printk(sys_table, "Failed to get file info size\n");
  68. return status;
  69. }
  70. grow:
  71. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  72. info_sz, (void **)&info);
  73. if (status != EFI_SUCCESS) {
  74. efi_printk(sys_table, "Failed to alloc mem for file info\n");
  75. return status;
  76. }
  77. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  78. &info_sz, info);
  79. if (status == EFI_BUFFER_TOO_SMALL) {
  80. efi_call_early(free_pool, info);
  81. goto grow;
  82. }
  83. *file_sz = info->file_size;
  84. efi_call_early(free_pool, info);
  85. if (status != EFI_SUCCESS)
  86. efi_printk(sys_table, "Failed to get initrd info\n");
  87. return status;
  88. }
  89. static efi_status_t
  90. __file_size64(void *__fh, efi_char16_t *filename_16,
  91. void **handle, u64 *file_sz)
  92. {
  93. efi_file_handle_64_t *h, *fh = __fh;
  94. efi_file_info_t *info;
  95. efi_status_t status;
  96. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  97. u64 info_sz;
  98. status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
  99. EFI_FILE_MODE_READ, (u64)0);
  100. if (status != EFI_SUCCESS) {
  101. efi_printk(sys_table, "Failed to open file: ");
  102. efi_char16_printk(sys_table, filename_16);
  103. efi_printk(sys_table, "\n");
  104. return status;
  105. }
  106. *handle = h;
  107. info_sz = 0;
  108. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  109. &info_sz, NULL);
  110. if (status != EFI_BUFFER_TOO_SMALL) {
  111. efi_printk(sys_table, "Failed to get file info size\n");
  112. return status;
  113. }
  114. grow:
  115. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  116. info_sz, (void **)&info);
  117. if (status != EFI_SUCCESS) {
  118. efi_printk(sys_table, "Failed to alloc mem for file info\n");
  119. return status;
  120. }
  121. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  122. &info_sz, info);
  123. if (status == EFI_BUFFER_TOO_SMALL) {
  124. efi_call_early(free_pool, info);
  125. goto grow;
  126. }
  127. *file_sz = info->file_size;
  128. efi_call_early(free_pool, info);
  129. if (status != EFI_SUCCESS)
  130. efi_printk(sys_table, "Failed to get initrd info\n");
  131. return status;
  132. }
  133. static efi_status_t
  134. efi_file_size(efi_system_table_t *sys_table, void *__fh,
  135. efi_char16_t *filename_16, void **handle, u64 *file_sz)
  136. {
  137. if (efi_early->is64)
  138. return __file_size64(__fh, filename_16, handle, file_sz);
  139. return __file_size32(__fh, filename_16, handle, file_sz);
  140. }
  141. static inline efi_status_t
  142. efi_file_read(void *handle, unsigned long *size, void *addr)
  143. {
  144. unsigned long func;
  145. if (efi_early->is64) {
  146. efi_file_handle_64_t *fh = handle;
  147. func = (unsigned long)fh->read;
  148. return efi_early->call(func, handle, size, addr);
  149. } else {
  150. efi_file_handle_32_t *fh = handle;
  151. func = (unsigned long)fh->read;
  152. return efi_early->call(func, handle, size, addr);
  153. }
  154. }
  155. static inline efi_status_t efi_file_close(void *handle)
  156. {
  157. if (efi_early->is64) {
  158. efi_file_handle_64_t *fh = handle;
  159. return efi_early->call((unsigned long)fh->close, handle);
  160. } else {
  161. efi_file_handle_32_t *fh = handle;
  162. return efi_early->call((unsigned long)fh->close, handle);
  163. }
  164. }
  165. static inline efi_status_t __open_volume32(void *__image, void **__fh)
  166. {
  167. efi_file_io_interface_t *io;
  168. efi_loaded_image_32_t *image = __image;
  169. efi_file_handle_32_t *fh;
  170. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  171. efi_status_t status;
  172. void *handle = (void *)(unsigned long)image->device_handle;
  173. unsigned long func;
  174. status = efi_call_early(handle_protocol, handle,
  175. &fs_proto, (void **)&io);
  176. if (status != EFI_SUCCESS) {
  177. efi_printk(sys_table, "Failed to handle fs_proto\n");
  178. return status;
  179. }
  180. func = (unsigned long)io->open_volume;
  181. status = efi_early->call(func, io, &fh);
  182. if (status != EFI_SUCCESS)
  183. efi_printk(sys_table, "Failed to open volume\n");
  184. *__fh = fh;
  185. return status;
  186. }
  187. static inline efi_status_t __open_volume64(void *__image, void **__fh)
  188. {
  189. efi_file_io_interface_t *io;
  190. efi_loaded_image_64_t *image = __image;
  191. efi_file_handle_64_t *fh;
  192. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  193. efi_status_t status;
  194. void *handle = (void *)(unsigned long)image->device_handle;
  195. unsigned long func;
  196. status = efi_call_early(handle_protocol, handle,
  197. &fs_proto, (void **)&io);
  198. if (status != EFI_SUCCESS) {
  199. efi_printk(sys_table, "Failed to handle fs_proto\n");
  200. return status;
  201. }
  202. func = (unsigned long)io->open_volume;
  203. status = efi_early->call(func, io, &fh);
  204. if (status != EFI_SUCCESS)
  205. efi_printk(sys_table, "Failed to open volume\n");
  206. *__fh = fh;
  207. return status;
  208. }
  209. static inline efi_status_t
  210. efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
  211. {
  212. if (efi_early->is64)
  213. return __open_volume64(__image, __fh);
  214. return __open_volume32(__image, __fh);
  215. }
  216. static void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
  217. {
  218. unsigned long output_string;
  219. size_t offset;
  220. if (efi_early->is64) {
  221. struct efi_simple_text_output_protocol_64 *out;
  222. u64 *func;
  223. offset = offsetof(typeof(*out), output_string);
  224. output_string = efi_early->text_output + offset;
  225. func = (u64 *)output_string;
  226. efi_early->call(*func, efi_early->text_output, str);
  227. } else {
  228. struct efi_simple_text_output_protocol_32 *out;
  229. u32 *func;
  230. offset = offsetof(typeof(*out), output_string);
  231. output_string = efi_early->text_output + offset;
  232. func = (u32 *)output_string;
  233. efi_early->call(*func, efi_early->text_output, str);
  234. }
  235. }
  236. #include "../../../../drivers/firmware/efi/efi-stub-helper.c"
  237. static void find_bits(unsigned long mask, u8 *pos, u8 *size)
  238. {
  239. u8 first, len;
  240. first = 0;
  241. len = 0;
  242. if (mask) {
  243. while (!(mask & 0x1)) {
  244. mask = mask >> 1;
  245. first++;
  246. }
  247. while (mask & 0x1) {
  248. mask = mask >> 1;
  249. len++;
  250. }
  251. }
  252. *pos = first;
  253. *size = len;
  254. }
  255. static efi_status_t
  256. __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
  257. {
  258. struct pci_setup_rom *rom = NULL;
  259. efi_status_t status;
  260. unsigned long size;
  261. uint64_t attributes;
  262. status = efi_early->call(pci->attributes, pci,
  263. EfiPciIoAttributeOperationGet, 0, 0,
  264. &attributes);
  265. if (status != EFI_SUCCESS)
  266. return status;
  267. if (!pci->romimage || !pci->romsize)
  268. return EFI_INVALID_PARAMETER;
  269. size = pci->romsize + sizeof(*rom);
  270. status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
  271. if (status != EFI_SUCCESS)
  272. return status;
  273. memset(rom, 0, sizeof(*rom));
  274. rom->data.type = SETUP_PCI;
  275. rom->data.len = size - sizeof(struct setup_data);
  276. rom->data.next = 0;
  277. rom->pcilen = pci->romsize;
  278. *__rom = rom;
  279. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  280. PCI_VENDOR_ID, 1, &(rom->vendor));
  281. if (status != EFI_SUCCESS)
  282. goto free_struct;
  283. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  284. PCI_DEVICE_ID, 1, &(rom->devid));
  285. if (status != EFI_SUCCESS)
  286. goto free_struct;
  287. status = efi_early->call(pci->get_location, pci, &(rom->segment),
  288. &(rom->bus), &(rom->device), &(rom->function));
  289. if (status != EFI_SUCCESS)
  290. goto free_struct;
  291. memcpy(rom->romdata, pci->romimage, pci->romsize);
  292. return status;
  293. free_struct:
  294. efi_call_early(free_pool, rom);
  295. return status;
  296. }
  297. static efi_status_t
  298. setup_efi_pci32(struct boot_params *params, void **pci_handle,
  299. unsigned long size)
  300. {
  301. efi_pci_io_protocol_32 *pci = NULL;
  302. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  303. u32 *handles = (u32 *)(unsigned long)pci_handle;
  304. efi_status_t status;
  305. unsigned long nr_pci;
  306. struct setup_data *data;
  307. int i;
  308. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  309. while (data && data->next)
  310. data = (struct setup_data *)(unsigned long)data->next;
  311. nr_pci = size / sizeof(u32);
  312. for (i = 0; i < nr_pci; i++) {
  313. struct pci_setup_rom *rom = NULL;
  314. u32 h = handles[i];
  315. status = efi_call_early(handle_protocol, h,
  316. &pci_proto, (void **)&pci);
  317. if (status != EFI_SUCCESS)
  318. continue;
  319. if (!pci)
  320. continue;
  321. status = __setup_efi_pci32(pci, &rom);
  322. if (status != EFI_SUCCESS)
  323. continue;
  324. if (data)
  325. data->next = (unsigned long)rom;
  326. else
  327. params->hdr.setup_data = (unsigned long)rom;
  328. data = (struct setup_data *)rom;
  329. }
  330. return status;
  331. }
  332. static efi_status_t
  333. __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
  334. {
  335. struct pci_setup_rom *rom;
  336. efi_status_t status;
  337. unsigned long size;
  338. uint64_t attributes;
  339. status = efi_early->call(pci->attributes, pci,
  340. EfiPciIoAttributeOperationGet, 0,
  341. &attributes);
  342. if (status != EFI_SUCCESS)
  343. return status;
  344. if (!pci->romimage || !pci->romsize)
  345. return EFI_INVALID_PARAMETER;
  346. size = pci->romsize + sizeof(*rom);
  347. status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
  348. if (status != EFI_SUCCESS)
  349. return status;
  350. rom->data.type = SETUP_PCI;
  351. rom->data.len = size - sizeof(struct setup_data);
  352. rom->data.next = 0;
  353. rom->pcilen = pci->romsize;
  354. *__rom = rom;
  355. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  356. PCI_VENDOR_ID, 1, &(rom->vendor));
  357. if (status != EFI_SUCCESS)
  358. goto free_struct;
  359. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  360. PCI_DEVICE_ID, 1, &(rom->devid));
  361. if (status != EFI_SUCCESS)
  362. goto free_struct;
  363. status = efi_early->call(pci->get_location, pci, &(rom->segment),
  364. &(rom->bus), &(rom->device), &(rom->function));
  365. if (status != EFI_SUCCESS)
  366. goto free_struct;
  367. memcpy(rom->romdata, pci->romimage, pci->romsize);
  368. return status;
  369. free_struct:
  370. efi_call_early(free_pool, rom);
  371. return status;
  372. }
  373. static efi_status_t
  374. setup_efi_pci64(struct boot_params *params, void **pci_handle,
  375. unsigned long size)
  376. {
  377. efi_pci_io_protocol_64 *pci = NULL;
  378. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  379. u64 *handles = (u64 *)(unsigned long)pci_handle;
  380. efi_status_t status;
  381. unsigned long nr_pci;
  382. struct setup_data *data;
  383. int i;
  384. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  385. while (data && data->next)
  386. data = (struct setup_data *)(unsigned long)data->next;
  387. nr_pci = size / sizeof(u64);
  388. for (i = 0; i < nr_pci; i++) {
  389. struct pci_setup_rom *rom = NULL;
  390. u64 h = handles[i];
  391. status = efi_call_early(handle_protocol, h,
  392. &pci_proto, (void **)&pci);
  393. if (status != EFI_SUCCESS)
  394. continue;
  395. if (!pci)
  396. continue;
  397. status = __setup_efi_pci64(pci, &rom);
  398. if (status != EFI_SUCCESS)
  399. continue;
  400. if (data)
  401. data->next = (unsigned long)rom;
  402. else
  403. params->hdr.setup_data = (unsigned long)rom;
  404. data = (struct setup_data *)rom;
  405. }
  406. return status;
  407. }
  408. static efi_status_t setup_efi_pci(struct boot_params *params)
  409. {
  410. efi_status_t status;
  411. void **pci_handle = NULL;
  412. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  413. unsigned long size = 0;
  414. status = efi_call_early(locate_handle,
  415. EFI_LOCATE_BY_PROTOCOL,
  416. &pci_proto, NULL, &size, pci_handle);
  417. if (status == EFI_BUFFER_TOO_SMALL) {
  418. status = efi_call_early(allocate_pool,
  419. EFI_LOADER_DATA,
  420. size, (void **)&pci_handle);
  421. if (status != EFI_SUCCESS)
  422. return status;
  423. status = efi_call_early(locate_handle,
  424. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  425. NULL, &size, pci_handle);
  426. }
  427. if (status != EFI_SUCCESS)
  428. goto free_handle;
  429. if (efi_early->is64)
  430. status = setup_efi_pci64(params, pci_handle, size);
  431. else
  432. status = setup_efi_pci32(params, pci_handle, size);
  433. free_handle:
  434. efi_call_early(free_pool, pci_handle);
  435. return status;
  436. }
  437. static void
  438. setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
  439. struct efi_pixel_bitmask pixel_info, int pixel_format)
  440. {
  441. if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
  442. si->lfb_depth = 32;
  443. si->lfb_linelength = pixels_per_scan_line * 4;
  444. si->red_size = 8;
  445. si->red_pos = 0;
  446. si->green_size = 8;
  447. si->green_pos = 8;
  448. si->blue_size = 8;
  449. si->blue_pos = 16;
  450. si->rsvd_size = 8;
  451. si->rsvd_pos = 24;
  452. } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
  453. si->lfb_depth = 32;
  454. si->lfb_linelength = pixels_per_scan_line * 4;
  455. si->red_size = 8;
  456. si->red_pos = 16;
  457. si->green_size = 8;
  458. si->green_pos = 8;
  459. si->blue_size = 8;
  460. si->blue_pos = 0;
  461. si->rsvd_size = 8;
  462. si->rsvd_pos = 24;
  463. } else if (pixel_format == PIXEL_BIT_MASK) {
  464. find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
  465. find_bits(pixel_info.green_mask, &si->green_pos,
  466. &si->green_size);
  467. find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
  468. find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
  469. &si->rsvd_size);
  470. si->lfb_depth = si->red_size + si->green_size +
  471. si->blue_size + si->rsvd_size;
  472. si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
  473. } else {
  474. si->lfb_depth = 4;
  475. si->lfb_linelength = si->lfb_width / 2;
  476. si->red_size = 0;
  477. si->red_pos = 0;
  478. si->green_size = 0;
  479. si->green_pos = 0;
  480. si->blue_size = 0;
  481. si->blue_pos = 0;
  482. si->rsvd_size = 0;
  483. si->rsvd_pos = 0;
  484. }
  485. }
  486. static efi_status_t
  487. __gop_query32(struct efi_graphics_output_protocol_32 *gop32,
  488. struct efi_graphics_output_mode_info **info,
  489. unsigned long *size, u32 *fb_base)
  490. {
  491. struct efi_graphics_output_protocol_mode_32 *mode;
  492. efi_status_t status;
  493. unsigned long m;
  494. m = gop32->mode;
  495. mode = (struct efi_graphics_output_protocol_mode_32 *)m;
  496. status = efi_early->call(gop32->query_mode, gop32,
  497. mode->mode, size, info);
  498. if (status != EFI_SUCCESS)
  499. return status;
  500. *fb_base = mode->frame_buffer_base;
  501. return status;
  502. }
  503. static efi_status_t
  504. setup_gop32(struct screen_info *si, efi_guid_t *proto,
  505. unsigned long size, void **gop_handle)
  506. {
  507. struct efi_graphics_output_protocol_32 *gop32, *first_gop;
  508. unsigned long nr_gops;
  509. u16 width, height;
  510. u32 pixels_per_scan_line;
  511. u32 fb_base;
  512. struct efi_pixel_bitmask pixel_info;
  513. int pixel_format;
  514. efi_status_t status;
  515. u32 *handles = (u32 *)(unsigned long)gop_handle;
  516. int i;
  517. first_gop = NULL;
  518. gop32 = NULL;
  519. nr_gops = size / sizeof(u32);
  520. for (i = 0; i < nr_gops; i++) {
  521. struct efi_graphics_output_mode_info *info = NULL;
  522. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  523. bool conout_found = false;
  524. void *dummy = NULL;
  525. u32 h = handles[i];
  526. status = efi_call_early(handle_protocol, h,
  527. proto, (void **)&gop32);
  528. if (status != EFI_SUCCESS)
  529. continue;
  530. status = efi_call_early(handle_protocol, h,
  531. &conout_proto, &dummy);
  532. if (status == EFI_SUCCESS)
  533. conout_found = true;
  534. status = __gop_query32(gop32, &info, &size, &fb_base);
  535. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  536. /*
  537. * Systems that use the UEFI Console Splitter may
  538. * provide multiple GOP devices, not all of which are
  539. * backed by real hardware. The workaround is to search
  540. * for a GOP implementing the ConOut protocol, and if
  541. * one isn't found, to just fall back to the first GOP.
  542. */
  543. width = info->horizontal_resolution;
  544. height = info->vertical_resolution;
  545. pixel_format = info->pixel_format;
  546. pixel_info = info->pixel_information;
  547. pixels_per_scan_line = info->pixels_per_scan_line;
  548. /*
  549. * Once we've found a GOP supporting ConOut,
  550. * don't bother looking any further.
  551. */
  552. first_gop = gop32;
  553. if (conout_found)
  554. break;
  555. }
  556. }
  557. /* Did we find any GOPs? */
  558. if (!first_gop)
  559. goto out;
  560. /* EFI framebuffer */
  561. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  562. si->lfb_width = width;
  563. si->lfb_height = height;
  564. si->lfb_base = fb_base;
  565. si->pages = 1;
  566. setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
  567. si->lfb_size = si->lfb_linelength * si->lfb_height;
  568. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  569. out:
  570. return status;
  571. }
  572. static efi_status_t
  573. __gop_query64(struct efi_graphics_output_protocol_64 *gop64,
  574. struct efi_graphics_output_mode_info **info,
  575. unsigned long *size, u32 *fb_base)
  576. {
  577. struct efi_graphics_output_protocol_mode_64 *mode;
  578. efi_status_t status;
  579. unsigned long m;
  580. m = gop64->mode;
  581. mode = (struct efi_graphics_output_protocol_mode_64 *)m;
  582. status = efi_early->call(gop64->query_mode, gop64,
  583. mode->mode, size, info);
  584. if (status != EFI_SUCCESS)
  585. return status;
  586. *fb_base = mode->frame_buffer_base;
  587. return status;
  588. }
  589. static efi_status_t
  590. setup_gop64(struct screen_info *si, efi_guid_t *proto,
  591. unsigned long size, void **gop_handle)
  592. {
  593. struct efi_graphics_output_protocol_64 *gop64, *first_gop;
  594. unsigned long nr_gops;
  595. u16 width, height;
  596. u32 pixels_per_scan_line;
  597. u32 fb_base;
  598. struct efi_pixel_bitmask pixel_info;
  599. int pixel_format;
  600. efi_status_t status;
  601. u64 *handles = (u64 *)(unsigned long)gop_handle;
  602. int i;
  603. first_gop = NULL;
  604. gop64 = NULL;
  605. nr_gops = size / sizeof(u64);
  606. for (i = 0; i < nr_gops; i++) {
  607. struct efi_graphics_output_mode_info *info = NULL;
  608. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  609. bool conout_found = false;
  610. void *dummy = NULL;
  611. u64 h = handles[i];
  612. status = efi_call_early(handle_protocol, h,
  613. proto, (void **)&gop64);
  614. if (status != EFI_SUCCESS)
  615. continue;
  616. status = efi_call_early(handle_protocol, h,
  617. &conout_proto, &dummy);
  618. if (status == EFI_SUCCESS)
  619. conout_found = true;
  620. status = __gop_query64(gop64, &info, &size, &fb_base);
  621. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  622. /*
  623. * Systems that use the UEFI Console Splitter may
  624. * provide multiple GOP devices, not all of which are
  625. * backed by real hardware. The workaround is to search
  626. * for a GOP implementing the ConOut protocol, and if
  627. * one isn't found, to just fall back to the first GOP.
  628. */
  629. width = info->horizontal_resolution;
  630. height = info->vertical_resolution;
  631. pixel_format = info->pixel_format;
  632. pixel_info = info->pixel_information;
  633. pixels_per_scan_line = info->pixels_per_scan_line;
  634. /*
  635. * Once we've found a GOP supporting ConOut,
  636. * don't bother looking any further.
  637. */
  638. first_gop = gop64;
  639. if (conout_found)
  640. break;
  641. }
  642. }
  643. /* Did we find any GOPs? */
  644. if (!first_gop)
  645. goto out;
  646. /* EFI framebuffer */
  647. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  648. si->lfb_width = width;
  649. si->lfb_height = height;
  650. si->lfb_base = fb_base;
  651. si->pages = 1;
  652. setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
  653. si->lfb_size = si->lfb_linelength * si->lfb_height;
  654. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  655. out:
  656. return status;
  657. }
  658. /*
  659. * See if we have Graphics Output Protocol
  660. */
  661. static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
  662. unsigned long size)
  663. {
  664. efi_status_t status;
  665. void **gop_handle = NULL;
  666. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  667. size, (void **)&gop_handle);
  668. if (status != EFI_SUCCESS)
  669. return status;
  670. status = efi_call_early(locate_handle,
  671. EFI_LOCATE_BY_PROTOCOL,
  672. proto, NULL, &size, gop_handle);
  673. if (status != EFI_SUCCESS)
  674. goto free_handle;
  675. if (efi_early->is64)
  676. status = setup_gop64(si, proto, size, gop_handle);
  677. else
  678. status = setup_gop32(si, proto, size, gop_handle);
  679. free_handle:
  680. efi_call_early(free_pool, gop_handle);
  681. return status;
  682. }
  683. static efi_status_t
  684. setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
  685. {
  686. struct efi_uga_draw_protocol *uga = NULL, *first_uga;
  687. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  688. unsigned long nr_ugas;
  689. u32 *handles = (u32 *)uga_handle;;
  690. efi_status_t status;
  691. int i;
  692. first_uga = NULL;
  693. nr_ugas = size / sizeof(u32);
  694. for (i = 0; i < nr_ugas; i++) {
  695. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  696. u32 w, h, depth, refresh;
  697. void *pciio;
  698. u32 handle = handles[i];
  699. status = efi_call_early(handle_protocol, handle,
  700. &uga_proto, (void **)&uga);
  701. if (status != EFI_SUCCESS)
  702. continue;
  703. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  704. status = efi_early->call((unsigned long)uga->get_mode, uga,
  705. &w, &h, &depth, &refresh);
  706. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  707. *width = w;
  708. *height = h;
  709. /*
  710. * Once we've found a UGA supporting PCIIO,
  711. * don't bother looking any further.
  712. */
  713. if (pciio)
  714. break;
  715. first_uga = uga;
  716. }
  717. }
  718. return status;
  719. }
  720. static efi_status_t
  721. setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
  722. {
  723. struct efi_uga_draw_protocol *uga = NULL, *first_uga;
  724. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  725. unsigned long nr_ugas;
  726. u64 *handles = (u64 *)uga_handle;;
  727. efi_status_t status;
  728. int i;
  729. first_uga = NULL;
  730. nr_ugas = size / sizeof(u64);
  731. for (i = 0; i < nr_ugas; i++) {
  732. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  733. u32 w, h, depth, refresh;
  734. void *pciio;
  735. u64 handle = handles[i];
  736. status = efi_call_early(handle_protocol, handle,
  737. &uga_proto, (void **)&uga);
  738. if (status != EFI_SUCCESS)
  739. continue;
  740. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  741. status = efi_early->call((unsigned long)uga->get_mode, uga,
  742. &w, &h, &depth, &refresh);
  743. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  744. *width = w;
  745. *height = h;
  746. /*
  747. * Once we've found a UGA supporting PCIIO,
  748. * don't bother looking any further.
  749. */
  750. if (pciio)
  751. break;
  752. first_uga = uga;
  753. }
  754. }
  755. return status;
  756. }
  757. /*
  758. * See if we have Universal Graphics Adapter (UGA) protocol
  759. */
  760. static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
  761. unsigned long size)
  762. {
  763. efi_status_t status;
  764. u32 width, height;
  765. void **uga_handle = NULL;
  766. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  767. size, (void **)&uga_handle);
  768. if (status != EFI_SUCCESS)
  769. return status;
  770. status = efi_call_early(locate_handle,
  771. EFI_LOCATE_BY_PROTOCOL,
  772. uga_proto, NULL, &size, uga_handle);
  773. if (status != EFI_SUCCESS)
  774. goto free_handle;
  775. height = 0;
  776. width = 0;
  777. if (efi_early->is64)
  778. status = setup_uga64(uga_handle, size, &width, &height);
  779. else
  780. status = setup_uga32(uga_handle, size, &width, &height);
  781. if (!width && !height)
  782. goto free_handle;
  783. /* EFI framebuffer */
  784. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  785. si->lfb_depth = 32;
  786. si->lfb_width = width;
  787. si->lfb_height = height;
  788. si->red_size = 8;
  789. si->red_pos = 16;
  790. si->green_size = 8;
  791. si->green_pos = 8;
  792. si->blue_size = 8;
  793. si->blue_pos = 0;
  794. si->rsvd_size = 8;
  795. si->rsvd_pos = 24;
  796. free_handle:
  797. efi_call_early(free_pool, uga_handle);
  798. return status;
  799. }
  800. void setup_graphics(struct boot_params *boot_params)
  801. {
  802. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  803. struct screen_info *si;
  804. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  805. efi_status_t status;
  806. unsigned long size;
  807. void **gop_handle = NULL;
  808. void **uga_handle = NULL;
  809. si = &boot_params->screen_info;
  810. memset(si, 0, sizeof(*si));
  811. size = 0;
  812. status = efi_call_early(locate_handle,
  813. EFI_LOCATE_BY_PROTOCOL,
  814. &graphics_proto, NULL, &size, gop_handle);
  815. if (status == EFI_BUFFER_TOO_SMALL)
  816. status = setup_gop(si, &graphics_proto, size);
  817. if (status != EFI_SUCCESS) {
  818. size = 0;
  819. status = efi_call_early(locate_handle,
  820. EFI_LOCATE_BY_PROTOCOL,
  821. &uga_proto, NULL, &size, uga_handle);
  822. if (status == EFI_BUFFER_TOO_SMALL)
  823. setup_uga(si, &uga_proto, size);
  824. }
  825. }
  826. /*
  827. * Because the x86 boot code expects to be passed a boot_params we
  828. * need to create one ourselves (usually the bootloader would create
  829. * one for us).
  830. *
  831. * The caller is responsible for filling out ->code32_start in the
  832. * returned boot_params.
  833. */
  834. struct boot_params *make_boot_params(struct efi_config *c)
  835. {
  836. struct boot_params *boot_params;
  837. struct sys_desc_table *sdt;
  838. struct apm_bios_info *bi;
  839. struct setup_header *hdr;
  840. struct efi_info *efi;
  841. efi_loaded_image_t *image;
  842. void *options, *handle;
  843. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  844. int options_size = 0;
  845. efi_status_t status;
  846. char *cmdline_ptr;
  847. u16 *s2;
  848. u8 *s1;
  849. int i;
  850. unsigned long ramdisk_addr;
  851. unsigned long ramdisk_size;
  852. efi_early = c;
  853. sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
  854. handle = (void *)(unsigned long)efi_early->image_handle;
  855. /* Check if we were booted by the EFI firmware */
  856. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  857. return NULL;
  858. if (efi_early->is64)
  859. setup_boot_services64(efi_early);
  860. else
  861. setup_boot_services32(efi_early);
  862. status = efi_call_early(handle_protocol, handle,
  863. &proto, (void *)&image);
  864. if (status != EFI_SUCCESS) {
  865. efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  866. return NULL;
  867. }
  868. status = efi_low_alloc(sys_table, 0x4000, 1,
  869. (unsigned long *)&boot_params);
  870. if (status != EFI_SUCCESS) {
  871. efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
  872. return NULL;
  873. }
  874. memset(boot_params, 0x0, 0x4000);
  875. hdr = &boot_params->hdr;
  876. efi = &boot_params->efi_info;
  877. bi = &boot_params->apm_bios_info;
  878. sdt = &boot_params->sys_desc_table;
  879. /* Copy the second sector to boot_params */
  880. memcpy(&hdr->jump, image->image_base + 512, 512);
  881. /*
  882. * Fill out some of the header fields ourselves because the
  883. * EFI firmware loader doesn't load the first sector.
  884. */
  885. hdr->root_flags = 1;
  886. hdr->vid_mode = 0xffff;
  887. hdr->boot_flag = 0xAA55;
  888. hdr->type_of_loader = 0x21;
  889. /* Convert unicode cmdline to ascii */
  890. cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
  891. if (!cmdline_ptr)
  892. goto fail;
  893. hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
  894. hdr->ramdisk_image = 0;
  895. hdr->ramdisk_size = 0;
  896. /* Clear APM BIOS info */
  897. memset(bi, 0, sizeof(*bi));
  898. memset(sdt, 0, sizeof(*sdt));
  899. status = handle_cmdline_files(sys_table, image,
  900. (char *)(unsigned long)hdr->cmd_line_ptr,
  901. "initrd=", hdr->initrd_addr_max,
  902. &ramdisk_addr, &ramdisk_size);
  903. if (status != EFI_SUCCESS)
  904. goto fail2;
  905. hdr->ramdisk_image = ramdisk_addr;
  906. hdr->ramdisk_size = ramdisk_size;
  907. return boot_params;
  908. fail2:
  909. efi_free(sys_table, options_size, hdr->cmd_line_ptr);
  910. fail:
  911. efi_free(sys_table, 0x4000, (unsigned long)boot_params);
  912. return NULL;
  913. }
  914. static void add_e820ext(struct boot_params *params,
  915. struct setup_data *e820ext, u32 nr_entries)
  916. {
  917. struct setup_data *data;
  918. efi_status_t status;
  919. unsigned long size;
  920. e820ext->type = SETUP_E820_EXT;
  921. e820ext->len = nr_entries * sizeof(struct e820entry);
  922. e820ext->next = 0;
  923. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  924. while (data && data->next)
  925. data = (struct setup_data *)(unsigned long)data->next;
  926. if (data)
  927. data->next = (unsigned long)e820ext;
  928. else
  929. params->hdr.setup_data = (unsigned long)e820ext;
  930. }
  931. static efi_status_t setup_e820(struct boot_params *params,
  932. struct setup_data *e820ext, u32 e820ext_size)
  933. {
  934. struct e820entry *e820_map = &params->e820_map[0];
  935. struct efi_info *efi = &params->efi_info;
  936. struct e820entry *prev = NULL;
  937. u32 nr_entries;
  938. u32 nr_desc;
  939. int i;
  940. nr_entries = 0;
  941. nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
  942. for (i = 0; i < nr_desc; i++) {
  943. efi_memory_desc_t *d;
  944. unsigned int e820_type = 0;
  945. unsigned long m = efi->efi_memmap;
  946. d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
  947. switch (d->type) {
  948. case EFI_RESERVED_TYPE:
  949. case EFI_RUNTIME_SERVICES_CODE:
  950. case EFI_RUNTIME_SERVICES_DATA:
  951. case EFI_MEMORY_MAPPED_IO:
  952. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  953. case EFI_PAL_CODE:
  954. e820_type = E820_RESERVED;
  955. break;
  956. case EFI_UNUSABLE_MEMORY:
  957. e820_type = E820_UNUSABLE;
  958. break;
  959. case EFI_ACPI_RECLAIM_MEMORY:
  960. e820_type = E820_ACPI;
  961. break;
  962. case EFI_LOADER_CODE:
  963. case EFI_LOADER_DATA:
  964. case EFI_BOOT_SERVICES_CODE:
  965. case EFI_BOOT_SERVICES_DATA:
  966. case EFI_CONVENTIONAL_MEMORY:
  967. e820_type = E820_RAM;
  968. break;
  969. case EFI_ACPI_MEMORY_NVS:
  970. e820_type = E820_NVS;
  971. break;
  972. default:
  973. continue;
  974. }
  975. /* Merge adjacent mappings */
  976. if (prev && prev->type == e820_type &&
  977. (prev->addr + prev->size) == d->phys_addr) {
  978. prev->size += d->num_pages << 12;
  979. continue;
  980. }
  981. if (nr_entries == ARRAY_SIZE(params->e820_map)) {
  982. u32 need = (nr_desc - i) * sizeof(struct e820entry) +
  983. sizeof(struct setup_data);
  984. if (!e820ext || e820ext_size < need)
  985. return EFI_BUFFER_TOO_SMALL;
  986. /* boot_params map full, switch to e820 extended */
  987. e820_map = (struct e820entry *)e820ext->data;
  988. }
  989. e820_map->addr = d->phys_addr;
  990. e820_map->size = d->num_pages << PAGE_SHIFT;
  991. e820_map->type = e820_type;
  992. prev = e820_map++;
  993. nr_entries++;
  994. }
  995. if (nr_entries > ARRAY_SIZE(params->e820_map)) {
  996. u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
  997. add_e820ext(params, e820ext, nr_e820ext);
  998. nr_entries -= nr_e820ext;
  999. }
  1000. params->e820_entries = (u8)nr_entries;
  1001. return EFI_SUCCESS;
  1002. }
  1003. static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
  1004. u32 *e820ext_size)
  1005. {
  1006. efi_status_t status;
  1007. unsigned long size;
  1008. size = sizeof(struct setup_data) +
  1009. sizeof(struct e820entry) * nr_desc;
  1010. if (*e820ext) {
  1011. efi_call_early(free_pool, *e820ext);
  1012. *e820ext = NULL;
  1013. *e820ext_size = 0;
  1014. }
  1015. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  1016. size, (void **)e820ext);
  1017. if (status == EFI_SUCCESS)
  1018. *e820ext_size = size;
  1019. return status;
  1020. }
  1021. static efi_status_t exit_boot(struct boot_params *boot_params,
  1022. void *handle, bool is64)
  1023. {
  1024. struct efi_info *efi = &boot_params->efi_info;
  1025. unsigned long map_sz, key, desc_size;
  1026. efi_memory_desc_t *mem_map;
  1027. struct setup_data *e820ext;
  1028. const char *signature;
  1029. __u32 e820ext_size;
  1030. __u32 nr_desc, prev_nr_desc;
  1031. efi_status_t status;
  1032. __u32 desc_version;
  1033. bool called_exit = false;
  1034. u8 nr_entries;
  1035. int i;
  1036. nr_desc = 0;
  1037. e820ext = NULL;
  1038. e820ext_size = 0;
  1039. get_map:
  1040. status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
  1041. &desc_version, &key);
  1042. if (status != EFI_SUCCESS)
  1043. return status;
  1044. prev_nr_desc = nr_desc;
  1045. nr_desc = map_sz / desc_size;
  1046. if (nr_desc > prev_nr_desc &&
  1047. nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
  1048. u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
  1049. status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
  1050. if (status != EFI_SUCCESS)
  1051. goto free_mem_map;
  1052. efi_call_early(free_pool, mem_map);
  1053. goto get_map; /* Allocated memory, get map again */
  1054. }
  1055. signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
  1056. memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
  1057. efi->efi_systab = (unsigned long)sys_table;
  1058. efi->efi_memdesc_size = desc_size;
  1059. efi->efi_memdesc_version = desc_version;
  1060. efi->efi_memmap = (unsigned long)mem_map;
  1061. efi->efi_memmap_size = map_sz;
  1062. #ifdef CONFIG_X86_64
  1063. efi->efi_systab_hi = (unsigned long)sys_table >> 32;
  1064. efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
  1065. #endif
  1066. /* Might as well exit boot services now */
  1067. status = efi_call_early(exit_boot_services, handle, key);
  1068. if (status != EFI_SUCCESS) {
  1069. /*
  1070. * ExitBootServices() will fail if any of the event
  1071. * handlers change the memory map. In which case, we
  1072. * must be prepared to retry, but only once so that
  1073. * we're guaranteed to exit on repeated failures instead
  1074. * of spinning forever.
  1075. */
  1076. if (called_exit)
  1077. goto free_mem_map;
  1078. called_exit = true;
  1079. efi_call_early(free_pool, mem_map);
  1080. goto get_map;
  1081. }
  1082. /* Historic? */
  1083. boot_params->alt_mem_k = 32 * 1024;
  1084. status = setup_e820(boot_params, e820ext, e820ext_size);
  1085. if (status != EFI_SUCCESS)
  1086. return status;
  1087. return EFI_SUCCESS;
  1088. free_mem_map:
  1089. efi_call_early(free_pool, mem_map);
  1090. return status;
  1091. }
  1092. /*
  1093. * On success we return a pointer to a boot_params structure, and NULL
  1094. * on failure.
  1095. */
  1096. struct boot_params *efi_main(struct efi_config *c,
  1097. struct boot_params *boot_params)
  1098. {
  1099. struct desc_ptr *gdt = NULL;
  1100. efi_loaded_image_t *image;
  1101. struct setup_header *hdr = &boot_params->hdr;
  1102. efi_status_t status;
  1103. struct desc_struct *desc;
  1104. void *handle;
  1105. efi_system_table_t *_table;
  1106. bool is64;
  1107. efi_early = c;
  1108. _table = (efi_system_table_t *)(unsigned long)efi_early->table;
  1109. handle = (void *)(unsigned long)efi_early->image_handle;
  1110. is64 = efi_early->is64;
  1111. sys_table = _table;
  1112. /* Check if we were booted by the EFI firmware */
  1113. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  1114. goto fail;
  1115. if (is64)
  1116. setup_boot_services64(efi_early);
  1117. else
  1118. setup_boot_services32(efi_early);
  1119. setup_graphics(boot_params);
  1120. setup_efi_pci(boot_params);
  1121. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  1122. sizeof(*gdt), (void **)&gdt);
  1123. if (status != EFI_SUCCESS) {
  1124. efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
  1125. goto fail;
  1126. }
  1127. gdt->size = 0x800;
  1128. status = efi_low_alloc(sys_table, gdt->size, 8,
  1129. (unsigned long *)&gdt->address);
  1130. if (status != EFI_SUCCESS) {
  1131. efi_printk(sys_table, "Failed to alloc mem for gdt\n");
  1132. goto fail;
  1133. }
  1134. /*
  1135. * If the kernel isn't already loaded at the preferred load
  1136. * address, relocate it.
  1137. */
  1138. if (hdr->pref_address != hdr->code32_start) {
  1139. unsigned long bzimage_addr = hdr->code32_start;
  1140. status = efi_relocate_kernel(sys_table, &bzimage_addr,
  1141. hdr->init_size, hdr->init_size,
  1142. hdr->pref_address,
  1143. hdr->kernel_alignment);
  1144. if (status != EFI_SUCCESS)
  1145. goto fail;
  1146. hdr->pref_address = hdr->code32_start;
  1147. hdr->code32_start = bzimage_addr;
  1148. }
  1149. status = exit_boot(boot_params, handle, is64);
  1150. if (status != EFI_SUCCESS)
  1151. goto fail;
  1152. memset((char *)gdt->address, 0x0, gdt->size);
  1153. desc = (struct desc_struct *)gdt->address;
  1154. /* The first GDT is a dummy and the second is unused. */
  1155. desc += 2;
  1156. desc->limit0 = 0xffff;
  1157. desc->base0 = 0x0000;
  1158. desc->base1 = 0x0000;
  1159. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  1160. desc->s = DESC_TYPE_CODE_DATA;
  1161. desc->dpl = 0;
  1162. desc->p = 1;
  1163. desc->limit = 0xf;
  1164. desc->avl = 0;
  1165. desc->l = 0;
  1166. desc->d = SEG_OP_SIZE_32BIT;
  1167. desc->g = SEG_GRANULARITY_4KB;
  1168. desc->base2 = 0x00;
  1169. desc++;
  1170. desc->limit0 = 0xffff;
  1171. desc->base0 = 0x0000;
  1172. desc->base1 = 0x0000;
  1173. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  1174. desc->s = DESC_TYPE_CODE_DATA;
  1175. desc->dpl = 0;
  1176. desc->p = 1;
  1177. desc->limit = 0xf;
  1178. desc->avl = 0;
  1179. desc->l = 0;
  1180. desc->d = SEG_OP_SIZE_32BIT;
  1181. desc->g = SEG_GRANULARITY_4KB;
  1182. desc->base2 = 0x00;
  1183. #ifdef CONFIG_X86_64
  1184. /* Task segment value */
  1185. desc++;
  1186. desc->limit0 = 0x0000;
  1187. desc->base0 = 0x0000;
  1188. desc->base1 = 0x0000;
  1189. desc->type = SEG_TYPE_TSS;
  1190. desc->s = 0;
  1191. desc->dpl = 0;
  1192. desc->p = 1;
  1193. desc->limit = 0x0;
  1194. desc->avl = 0;
  1195. desc->l = 0;
  1196. desc->d = 0;
  1197. desc->g = SEG_GRANULARITY_4KB;
  1198. desc->base2 = 0x00;
  1199. #endif /* CONFIG_X86_64 */
  1200. asm volatile("cli");
  1201. asm volatile ("lgdt %0" : : "m" (*gdt));
  1202. return boot_params;
  1203. fail:
  1204. return NULL;
  1205. }