eboot.c 37 KB

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