eboot.c 37 KB

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