eboot.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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. \
  27. table = (typeof(table))sys_table; \
  28. \
  29. c->boot_services = table->boottime; \
  30. c->text_output = table->con_out; \
  31. }
  32. BOOT_SERVICES(32);
  33. BOOT_SERVICES(64);
  34. void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
  35. static efi_status_t
  36. __file_size32(void *__fh, efi_char16_t *filename_16,
  37. void **handle, u64 *file_sz)
  38. {
  39. efi_file_handle_32_t *h, *fh = __fh;
  40. efi_file_info_t *info;
  41. efi_status_t status;
  42. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  43. u32 info_sz;
  44. status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
  45. EFI_FILE_MODE_READ, (u64)0);
  46. if (status != EFI_SUCCESS) {
  47. efi_printk(sys_table, "Failed to open file: ");
  48. efi_char16_printk(sys_table, filename_16);
  49. efi_printk(sys_table, "\n");
  50. return status;
  51. }
  52. *handle = h;
  53. info_sz = 0;
  54. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  55. &info_sz, NULL);
  56. if (status != EFI_BUFFER_TOO_SMALL) {
  57. efi_printk(sys_table, "Failed to get file info size\n");
  58. return status;
  59. }
  60. grow:
  61. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  62. info_sz, (void **)&info);
  63. if (status != EFI_SUCCESS) {
  64. efi_printk(sys_table, "Failed to alloc mem for file info\n");
  65. return status;
  66. }
  67. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  68. &info_sz, info);
  69. if (status == EFI_BUFFER_TOO_SMALL) {
  70. efi_call_early(free_pool, info);
  71. goto grow;
  72. }
  73. *file_sz = info->file_size;
  74. efi_call_early(free_pool, info);
  75. if (status != EFI_SUCCESS)
  76. efi_printk(sys_table, "Failed to get initrd info\n");
  77. return status;
  78. }
  79. static efi_status_t
  80. __file_size64(void *__fh, efi_char16_t *filename_16,
  81. void **handle, u64 *file_sz)
  82. {
  83. efi_file_handle_64_t *h, *fh = __fh;
  84. efi_file_info_t *info;
  85. efi_status_t status;
  86. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  87. u64 info_sz;
  88. status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
  89. EFI_FILE_MODE_READ, (u64)0);
  90. if (status != EFI_SUCCESS) {
  91. efi_printk(sys_table, "Failed to open file: ");
  92. efi_char16_printk(sys_table, filename_16);
  93. efi_printk(sys_table, "\n");
  94. return status;
  95. }
  96. *handle = h;
  97. info_sz = 0;
  98. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  99. &info_sz, NULL);
  100. if (status != EFI_BUFFER_TOO_SMALL) {
  101. efi_printk(sys_table, "Failed to get file info size\n");
  102. return status;
  103. }
  104. grow:
  105. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  106. info_sz, (void **)&info);
  107. if (status != EFI_SUCCESS) {
  108. efi_printk(sys_table, "Failed to alloc mem for file info\n");
  109. return status;
  110. }
  111. status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
  112. &info_sz, info);
  113. if (status == EFI_BUFFER_TOO_SMALL) {
  114. efi_call_early(free_pool, info);
  115. goto grow;
  116. }
  117. *file_sz = info->file_size;
  118. efi_call_early(free_pool, info);
  119. if (status != EFI_SUCCESS)
  120. efi_printk(sys_table, "Failed to get initrd info\n");
  121. return status;
  122. }
  123. efi_status_t
  124. efi_file_size(efi_system_table_t *sys_table, void *__fh,
  125. efi_char16_t *filename_16, void **handle, u64 *file_sz)
  126. {
  127. if (efi_early->is64)
  128. return __file_size64(__fh, filename_16, handle, file_sz);
  129. return __file_size32(__fh, filename_16, handle, file_sz);
  130. }
  131. efi_status_t
  132. efi_file_read(void *handle, unsigned long *size, void *addr)
  133. {
  134. unsigned long func;
  135. if (efi_early->is64) {
  136. efi_file_handle_64_t *fh = handle;
  137. func = (unsigned long)fh->read;
  138. return efi_early->call(func, handle, size, addr);
  139. } else {
  140. efi_file_handle_32_t *fh = handle;
  141. func = (unsigned long)fh->read;
  142. return efi_early->call(func, handle, size, addr);
  143. }
  144. }
  145. efi_status_t efi_file_close(void *handle)
  146. {
  147. if (efi_early->is64) {
  148. efi_file_handle_64_t *fh = handle;
  149. return efi_early->call((unsigned long)fh->close, handle);
  150. } else {
  151. efi_file_handle_32_t *fh = handle;
  152. return efi_early->call((unsigned long)fh->close, handle);
  153. }
  154. }
  155. static inline efi_status_t __open_volume32(void *__image, void **__fh)
  156. {
  157. efi_file_io_interface_t *io;
  158. efi_loaded_image_32_t *image = __image;
  159. efi_file_handle_32_t *fh;
  160. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  161. efi_status_t status;
  162. void *handle = (void *)(unsigned long)image->device_handle;
  163. unsigned long func;
  164. status = efi_call_early(handle_protocol, handle,
  165. &fs_proto, (void **)&io);
  166. if (status != EFI_SUCCESS) {
  167. efi_printk(sys_table, "Failed to handle fs_proto\n");
  168. return status;
  169. }
  170. func = (unsigned long)io->open_volume;
  171. status = efi_early->call(func, io, &fh);
  172. if (status != EFI_SUCCESS)
  173. efi_printk(sys_table, "Failed to open volume\n");
  174. *__fh = fh;
  175. return status;
  176. }
  177. static inline efi_status_t __open_volume64(void *__image, void **__fh)
  178. {
  179. efi_file_io_interface_t *io;
  180. efi_loaded_image_64_t *image = __image;
  181. efi_file_handle_64_t *fh;
  182. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  183. efi_status_t status;
  184. void *handle = (void *)(unsigned long)image->device_handle;
  185. unsigned long func;
  186. status = efi_call_early(handle_protocol, handle,
  187. &fs_proto, (void **)&io);
  188. if (status != EFI_SUCCESS) {
  189. efi_printk(sys_table, "Failed to handle fs_proto\n");
  190. return status;
  191. }
  192. func = (unsigned long)io->open_volume;
  193. status = efi_early->call(func, io, &fh);
  194. if (status != EFI_SUCCESS)
  195. efi_printk(sys_table, "Failed to open volume\n");
  196. *__fh = fh;
  197. return status;
  198. }
  199. efi_status_t
  200. efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
  201. {
  202. if (efi_early->is64)
  203. return __open_volume64(__image, __fh);
  204. return __open_volume32(__image, __fh);
  205. }
  206. void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
  207. {
  208. unsigned long output_string;
  209. size_t offset;
  210. if (efi_early->is64) {
  211. struct efi_simple_text_output_protocol_64 *out;
  212. u64 *func;
  213. offset = offsetof(typeof(*out), output_string);
  214. output_string = efi_early->text_output + offset;
  215. out = (typeof(out))(unsigned long)efi_early->text_output;
  216. func = (u64 *)output_string;
  217. efi_early->call(*func, out, str);
  218. } else {
  219. struct efi_simple_text_output_protocol_32 *out;
  220. u32 *func;
  221. offset = offsetof(typeof(*out), output_string);
  222. output_string = efi_early->text_output + offset;
  223. out = (typeof(out))(unsigned long)efi_early->text_output;
  224. func = (u32 *)output_string;
  225. efi_early->call(*func, out, str);
  226. }
  227. }
  228. static efi_status_t
  229. __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
  230. {
  231. struct pci_setup_rom *rom = NULL;
  232. efi_status_t status;
  233. unsigned long size;
  234. uint64_t attributes;
  235. status = efi_early->call(pci->attributes, pci,
  236. EfiPciIoAttributeOperationGet, 0, 0,
  237. &attributes);
  238. if (status != EFI_SUCCESS)
  239. return status;
  240. if (!pci->romimage || !pci->romsize)
  241. return EFI_INVALID_PARAMETER;
  242. size = pci->romsize + sizeof(*rom);
  243. status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
  244. if (status != EFI_SUCCESS) {
  245. efi_printk(sys_table, "Failed to alloc mem for rom\n");
  246. return status;
  247. }
  248. memset(rom, 0, sizeof(*rom));
  249. rom->data.type = SETUP_PCI;
  250. rom->data.len = size - sizeof(struct setup_data);
  251. rom->data.next = 0;
  252. rom->pcilen = pci->romsize;
  253. *__rom = rom;
  254. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  255. PCI_VENDOR_ID, 1, &(rom->vendor));
  256. if (status != EFI_SUCCESS) {
  257. efi_printk(sys_table, "Failed to read rom->vendor\n");
  258. goto free_struct;
  259. }
  260. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  261. PCI_DEVICE_ID, 1, &(rom->devid));
  262. if (status != EFI_SUCCESS) {
  263. efi_printk(sys_table, "Failed to read rom->devid\n");
  264. goto free_struct;
  265. }
  266. status = efi_early->call(pci->get_location, pci, &(rom->segment),
  267. &(rom->bus), &(rom->device), &(rom->function));
  268. if (status != EFI_SUCCESS)
  269. goto free_struct;
  270. memcpy(rom->romdata, (void *)(unsigned long)pci->romimage,
  271. pci->romsize);
  272. return status;
  273. free_struct:
  274. efi_call_early(free_pool, rom);
  275. return status;
  276. }
  277. static void
  278. setup_efi_pci32(struct boot_params *params, void **pci_handle,
  279. unsigned long size)
  280. {
  281. efi_pci_io_protocol_32 *pci = NULL;
  282. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  283. u32 *handles = (u32 *)(unsigned long)pci_handle;
  284. efi_status_t status;
  285. unsigned long nr_pci;
  286. struct setup_data *data;
  287. int i;
  288. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  289. while (data && data->next)
  290. data = (struct setup_data *)(unsigned long)data->next;
  291. nr_pci = size / sizeof(u32);
  292. for (i = 0; i < nr_pci; i++) {
  293. struct pci_setup_rom *rom = NULL;
  294. u32 h = handles[i];
  295. status = efi_call_early(handle_protocol, h,
  296. &pci_proto, (void **)&pci);
  297. if (status != EFI_SUCCESS)
  298. continue;
  299. if (!pci)
  300. continue;
  301. status = __setup_efi_pci32(pci, &rom);
  302. if (status != EFI_SUCCESS)
  303. continue;
  304. if (data)
  305. data->next = (unsigned long)rom;
  306. else
  307. params->hdr.setup_data = (unsigned long)rom;
  308. data = (struct setup_data *)rom;
  309. }
  310. }
  311. static efi_status_t
  312. __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
  313. {
  314. struct pci_setup_rom *rom;
  315. efi_status_t status;
  316. unsigned long size;
  317. uint64_t attributes;
  318. status = efi_early->call(pci->attributes, pci,
  319. EfiPciIoAttributeOperationGet, 0,
  320. &attributes);
  321. if (status != EFI_SUCCESS)
  322. return status;
  323. if (!pci->romimage || !pci->romsize)
  324. return EFI_INVALID_PARAMETER;
  325. size = pci->romsize + sizeof(*rom);
  326. status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
  327. if (status != EFI_SUCCESS) {
  328. efi_printk(sys_table, "Failed to alloc mem for rom\n");
  329. return status;
  330. }
  331. rom->data.type = SETUP_PCI;
  332. rom->data.len = size - sizeof(struct setup_data);
  333. rom->data.next = 0;
  334. rom->pcilen = pci->romsize;
  335. *__rom = rom;
  336. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  337. PCI_VENDOR_ID, 1, &(rom->vendor));
  338. if (status != EFI_SUCCESS) {
  339. efi_printk(sys_table, "Failed to read rom->vendor\n");
  340. goto free_struct;
  341. }
  342. status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
  343. PCI_DEVICE_ID, 1, &(rom->devid));
  344. if (status != EFI_SUCCESS) {
  345. efi_printk(sys_table, "Failed to read rom->devid\n");
  346. goto free_struct;
  347. }
  348. status = efi_early->call(pci->get_location, pci, &(rom->segment),
  349. &(rom->bus), &(rom->device), &(rom->function));
  350. if (status != EFI_SUCCESS)
  351. goto free_struct;
  352. memcpy(rom->romdata, (void *)(unsigned long)pci->romimage,
  353. pci->romsize);
  354. return status;
  355. free_struct:
  356. efi_call_early(free_pool, rom);
  357. return status;
  358. }
  359. static void
  360. setup_efi_pci64(struct boot_params *params, void **pci_handle,
  361. unsigned long size)
  362. {
  363. efi_pci_io_protocol_64 *pci = NULL;
  364. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  365. u64 *handles = (u64 *)(unsigned long)pci_handle;
  366. efi_status_t status;
  367. unsigned long nr_pci;
  368. struct setup_data *data;
  369. int i;
  370. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  371. while (data && data->next)
  372. data = (struct setup_data *)(unsigned long)data->next;
  373. nr_pci = size / sizeof(u64);
  374. for (i = 0; i < nr_pci; i++) {
  375. struct pci_setup_rom *rom = NULL;
  376. u64 h = handles[i];
  377. status = efi_call_early(handle_protocol, h,
  378. &pci_proto, (void **)&pci);
  379. if (status != EFI_SUCCESS)
  380. continue;
  381. if (!pci)
  382. continue;
  383. status = __setup_efi_pci64(pci, &rom);
  384. if (status != EFI_SUCCESS)
  385. continue;
  386. if (data)
  387. data->next = (unsigned long)rom;
  388. else
  389. params->hdr.setup_data = (unsigned long)rom;
  390. data = (struct setup_data *)rom;
  391. }
  392. }
  393. /*
  394. * There's no way to return an informative status from this function,
  395. * because any analysis (and printing of error messages) needs to be
  396. * done directly at the EFI function call-site.
  397. *
  398. * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
  399. * just didn't find any PCI devices, but there's no way to tell outside
  400. * the context of the call.
  401. */
  402. static void setup_efi_pci(struct boot_params *params)
  403. {
  404. efi_status_t status;
  405. void **pci_handle = NULL;
  406. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  407. unsigned long size = 0;
  408. status = efi_call_early(locate_handle,
  409. EFI_LOCATE_BY_PROTOCOL,
  410. &pci_proto, NULL, &size, pci_handle);
  411. if (status == EFI_BUFFER_TOO_SMALL) {
  412. status = efi_call_early(allocate_pool,
  413. EFI_LOADER_DATA,
  414. size, (void **)&pci_handle);
  415. if (status != EFI_SUCCESS) {
  416. efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
  417. return;
  418. }
  419. status = efi_call_early(locate_handle,
  420. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  421. NULL, &size, pci_handle);
  422. }
  423. if (status != EFI_SUCCESS)
  424. goto free_handle;
  425. if (efi_early->is64)
  426. setup_efi_pci64(params, pci_handle, size);
  427. else
  428. setup_efi_pci32(params, pci_handle, size);
  429. free_handle:
  430. efi_call_early(free_pool, pci_handle);
  431. }
  432. static efi_status_t
  433. setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
  434. {
  435. struct efi_uga_draw_protocol *uga = NULL, *first_uga;
  436. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  437. unsigned long nr_ugas;
  438. u32 *handles = (u32 *)uga_handle;;
  439. efi_status_t status = EFI_INVALID_PARAMETER;
  440. int i;
  441. first_uga = NULL;
  442. nr_ugas = size / sizeof(u32);
  443. for (i = 0; i < nr_ugas; i++) {
  444. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  445. u32 w, h, depth, refresh;
  446. void *pciio;
  447. u32 handle = handles[i];
  448. status = efi_call_early(handle_protocol, handle,
  449. &uga_proto, (void **)&uga);
  450. if (status != EFI_SUCCESS)
  451. continue;
  452. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  453. status = efi_early->call((unsigned long)uga->get_mode, uga,
  454. &w, &h, &depth, &refresh);
  455. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  456. *width = w;
  457. *height = h;
  458. /*
  459. * Once we've found a UGA supporting PCIIO,
  460. * don't bother looking any further.
  461. */
  462. if (pciio)
  463. break;
  464. first_uga = uga;
  465. }
  466. }
  467. return status;
  468. }
  469. static efi_status_t
  470. setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
  471. {
  472. struct efi_uga_draw_protocol *uga = NULL, *first_uga;
  473. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  474. unsigned long nr_ugas;
  475. u64 *handles = (u64 *)uga_handle;;
  476. efi_status_t status = EFI_INVALID_PARAMETER;
  477. int i;
  478. first_uga = NULL;
  479. nr_ugas = size / sizeof(u64);
  480. for (i = 0; i < nr_ugas; i++) {
  481. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  482. u32 w, h, depth, refresh;
  483. void *pciio;
  484. u64 handle = handles[i];
  485. status = efi_call_early(handle_protocol, handle,
  486. &uga_proto, (void **)&uga);
  487. if (status != EFI_SUCCESS)
  488. continue;
  489. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  490. status = efi_early->call((unsigned long)uga->get_mode, uga,
  491. &w, &h, &depth, &refresh);
  492. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  493. *width = w;
  494. *height = h;
  495. /*
  496. * Once we've found a UGA supporting PCIIO,
  497. * don't bother looking any further.
  498. */
  499. if (pciio)
  500. break;
  501. first_uga = uga;
  502. }
  503. }
  504. return status;
  505. }
  506. /*
  507. * See if we have Universal Graphics Adapter (UGA) protocol
  508. */
  509. static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
  510. unsigned long size)
  511. {
  512. efi_status_t status;
  513. u32 width, height;
  514. void **uga_handle = NULL;
  515. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  516. size, (void **)&uga_handle);
  517. if (status != EFI_SUCCESS)
  518. return status;
  519. status = efi_call_early(locate_handle,
  520. EFI_LOCATE_BY_PROTOCOL,
  521. uga_proto, NULL, &size, uga_handle);
  522. if (status != EFI_SUCCESS)
  523. goto free_handle;
  524. height = 0;
  525. width = 0;
  526. if (efi_early->is64)
  527. status = setup_uga64(uga_handle, size, &width, &height);
  528. else
  529. status = setup_uga32(uga_handle, size, &width, &height);
  530. if (!width && !height)
  531. goto free_handle;
  532. /* EFI framebuffer */
  533. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  534. si->lfb_depth = 32;
  535. si->lfb_width = width;
  536. si->lfb_height = height;
  537. si->red_size = 8;
  538. si->red_pos = 16;
  539. si->green_size = 8;
  540. si->green_pos = 8;
  541. si->blue_size = 8;
  542. si->blue_pos = 0;
  543. si->rsvd_size = 8;
  544. si->rsvd_pos = 24;
  545. free_handle:
  546. efi_call_early(free_pool, uga_handle);
  547. return status;
  548. }
  549. void setup_graphics(struct boot_params *boot_params)
  550. {
  551. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  552. struct screen_info *si;
  553. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  554. efi_status_t status;
  555. unsigned long size;
  556. void **gop_handle = NULL;
  557. void **uga_handle = NULL;
  558. si = &boot_params->screen_info;
  559. memset(si, 0, sizeof(*si));
  560. size = 0;
  561. status = efi_call_early(locate_handle,
  562. EFI_LOCATE_BY_PROTOCOL,
  563. &graphics_proto, NULL, &size, gop_handle);
  564. if (status == EFI_BUFFER_TOO_SMALL)
  565. status = efi_setup_gop(NULL, si, &graphics_proto, size);
  566. if (status != EFI_SUCCESS) {
  567. size = 0;
  568. status = efi_call_early(locate_handle,
  569. EFI_LOCATE_BY_PROTOCOL,
  570. &uga_proto, NULL, &size, uga_handle);
  571. if (status == EFI_BUFFER_TOO_SMALL)
  572. setup_uga(si, &uga_proto, size);
  573. }
  574. }
  575. /*
  576. * Because the x86 boot code expects to be passed a boot_params we
  577. * need to create one ourselves (usually the bootloader would create
  578. * one for us).
  579. *
  580. * The caller is responsible for filling out ->code32_start in the
  581. * returned boot_params.
  582. */
  583. struct boot_params *make_boot_params(struct efi_config *c)
  584. {
  585. struct boot_params *boot_params;
  586. struct apm_bios_info *bi;
  587. struct setup_header *hdr;
  588. efi_loaded_image_t *image;
  589. void *options, *handle;
  590. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  591. int options_size = 0;
  592. efi_status_t status;
  593. char *cmdline_ptr;
  594. u16 *s2;
  595. u8 *s1;
  596. int i;
  597. unsigned long ramdisk_addr;
  598. unsigned long ramdisk_size;
  599. efi_early = c;
  600. sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
  601. handle = (void *)(unsigned long)efi_early->image_handle;
  602. /* Check if we were booted by the EFI firmware */
  603. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  604. return NULL;
  605. if (efi_early->is64)
  606. setup_boot_services64(efi_early);
  607. else
  608. setup_boot_services32(efi_early);
  609. status = efi_call_early(handle_protocol, handle,
  610. &proto, (void *)&image);
  611. if (status != EFI_SUCCESS) {
  612. efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  613. return NULL;
  614. }
  615. status = efi_low_alloc(sys_table, 0x4000, 1,
  616. (unsigned long *)&boot_params);
  617. if (status != EFI_SUCCESS) {
  618. efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
  619. return NULL;
  620. }
  621. memset(boot_params, 0x0, 0x4000);
  622. hdr = &boot_params->hdr;
  623. bi = &boot_params->apm_bios_info;
  624. /* Copy the second sector to boot_params */
  625. memcpy(&hdr->jump, image->image_base + 512, 512);
  626. /*
  627. * Fill out some of the header fields ourselves because the
  628. * EFI firmware loader doesn't load the first sector.
  629. */
  630. hdr->root_flags = 1;
  631. hdr->vid_mode = 0xffff;
  632. hdr->boot_flag = 0xAA55;
  633. hdr->type_of_loader = 0x21;
  634. /* Convert unicode cmdline to ascii */
  635. cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
  636. if (!cmdline_ptr)
  637. goto fail;
  638. hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
  639. /* Fill in upper bits of command line address, NOP on 32 bit */
  640. boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
  641. hdr->ramdisk_image = 0;
  642. hdr->ramdisk_size = 0;
  643. /* Clear APM BIOS info */
  644. memset(bi, 0, sizeof(*bi));
  645. status = efi_parse_options(cmdline_ptr);
  646. if (status != EFI_SUCCESS)
  647. goto fail2;
  648. status = handle_cmdline_files(sys_table, image,
  649. (char *)(unsigned long)hdr->cmd_line_ptr,
  650. "initrd=", hdr->initrd_addr_max,
  651. &ramdisk_addr, &ramdisk_size);
  652. if (status != EFI_SUCCESS &&
  653. hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
  654. efi_printk(sys_table, "Trying to load files to higher address\n");
  655. status = handle_cmdline_files(sys_table, image,
  656. (char *)(unsigned long)hdr->cmd_line_ptr,
  657. "initrd=", -1UL,
  658. &ramdisk_addr, &ramdisk_size);
  659. }
  660. if (status != EFI_SUCCESS)
  661. goto fail2;
  662. hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
  663. hdr->ramdisk_size = ramdisk_size & 0xffffffff;
  664. boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
  665. boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
  666. return boot_params;
  667. fail2:
  668. efi_free(sys_table, options_size, hdr->cmd_line_ptr);
  669. fail:
  670. efi_free(sys_table, 0x4000, (unsigned long)boot_params);
  671. return NULL;
  672. }
  673. static void add_e820ext(struct boot_params *params,
  674. struct setup_data *e820ext, u32 nr_entries)
  675. {
  676. struct setup_data *data;
  677. efi_status_t status;
  678. unsigned long size;
  679. e820ext->type = SETUP_E820_EXT;
  680. e820ext->len = nr_entries * sizeof(struct e820entry);
  681. e820ext->next = 0;
  682. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  683. while (data && data->next)
  684. data = (struct setup_data *)(unsigned long)data->next;
  685. if (data)
  686. data->next = (unsigned long)e820ext;
  687. else
  688. params->hdr.setup_data = (unsigned long)e820ext;
  689. }
  690. static efi_status_t setup_e820(struct boot_params *params,
  691. struct setup_data *e820ext, u32 e820ext_size)
  692. {
  693. struct e820entry *e820_map = &params->e820_map[0];
  694. struct efi_info *efi = &params->efi_info;
  695. struct e820entry *prev = NULL;
  696. u32 nr_entries;
  697. u32 nr_desc;
  698. int i;
  699. nr_entries = 0;
  700. nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
  701. for (i = 0; i < nr_desc; i++) {
  702. efi_memory_desc_t *d;
  703. unsigned int e820_type = 0;
  704. unsigned long m = efi->efi_memmap;
  705. #ifdef CONFIG_X86_64
  706. m |= (u64)efi->efi_memmap_hi << 32;
  707. #endif
  708. d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
  709. switch (d->type) {
  710. case EFI_RESERVED_TYPE:
  711. case EFI_RUNTIME_SERVICES_CODE:
  712. case EFI_RUNTIME_SERVICES_DATA:
  713. case EFI_MEMORY_MAPPED_IO:
  714. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  715. case EFI_PAL_CODE:
  716. e820_type = E820_RESERVED;
  717. break;
  718. case EFI_UNUSABLE_MEMORY:
  719. e820_type = E820_UNUSABLE;
  720. break;
  721. case EFI_ACPI_RECLAIM_MEMORY:
  722. e820_type = E820_ACPI;
  723. break;
  724. case EFI_LOADER_CODE:
  725. case EFI_LOADER_DATA:
  726. case EFI_BOOT_SERVICES_CODE:
  727. case EFI_BOOT_SERVICES_DATA:
  728. case EFI_CONVENTIONAL_MEMORY:
  729. e820_type = E820_RAM;
  730. break;
  731. case EFI_ACPI_MEMORY_NVS:
  732. e820_type = E820_NVS;
  733. break;
  734. case EFI_PERSISTENT_MEMORY:
  735. e820_type = E820_PMEM;
  736. break;
  737. default:
  738. continue;
  739. }
  740. /* Merge adjacent mappings */
  741. if (prev && prev->type == e820_type &&
  742. (prev->addr + prev->size) == d->phys_addr) {
  743. prev->size += d->num_pages << 12;
  744. continue;
  745. }
  746. if (nr_entries == ARRAY_SIZE(params->e820_map)) {
  747. u32 need = (nr_desc - i) * sizeof(struct e820entry) +
  748. sizeof(struct setup_data);
  749. if (!e820ext || e820ext_size < need)
  750. return EFI_BUFFER_TOO_SMALL;
  751. /* boot_params map full, switch to e820 extended */
  752. e820_map = (struct e820entry *)e820ext->data;
  753. }
  754. e820_map->addr = d->phys_addr;
  755. e820_map->size = d->num_pages << PAGE_SHIFT;
  756. e820_map->type = e820_type;
  757. prev = e820_map++;
  758. nr_entries++;
  759. }
  760. if (nr_entries > ARRAY_SIZE(params->e820_map)) {
  761. u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
  762. add_e820ext(params, e820ext, nr_e820ext);
  763. nr_entries -= nr_e820ext;
  764. }
  765. params->e820_entries = (u8)nr_entries;
  766. return EFI_SUCCESS;
  767. }
  768. static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
  769. u32 *e820ext_size)
  770. {
  771. efi_status_t status;
  772. unsigned long size;
  773. size = sizeof(struct setup_data) +
  774. sizeof(struct e820entry) * nr_desc;
  775. if (*e820ext) {
  776. efi_call_early(free_pool, *e820ext);
  777. *e820ext = NULL;
  778. *e820ext_size = 0;
  779. }
  780. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  781. size, (void **)e820ext);
  782. if (status == EFI_SUCCESS)
  783. *e820ext_size = size;
  784. return status;
  785. }
  786. struct exit_boot_struct {
  787. struct boot_params *boot_params;
  788. struct efi_info *efi;
  789. struct setup_data *e820ext;
  790. __u32 e820ext_size;
  791. bool is64;
  792. };
  793. static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
  794. struct efi_boot_memmap *map,
  795. void *priv)
  796. {
  797. static bool first = true;
  798. const char *signature;
  799. __u32 nr_desc;
  800. efi_status_t status;
  801. struct exit_boot_struct *p = priv;
  802. if (first) {
  803. nr_desc = *map->buff_size / *map->desc_size;
  804. if (nr_desc > ARRAY_SIZE(p->boot_params->e820_map)) {
  805. u32 nr_e820ext = nr_desc -
  806. ARRAY_SIZE(p->boot_params->e820_map);
  807. status = alloc_e820ext(nr_e820ext, &p->e820ext,
  808. &p->e820ext_size);
  809. if (status != EFI_SUCCESS)
  810. return status;
  811. }
  812. first = false;
  813. }
  814. signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
  815. memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
  816. p->efi->efi_systab = (unsigned long)sys_table_arg;
  817. p->efi->efi_memdesc_size = *map->desc_size;
  818. p->efi->efi_memdesc_version = *map->desc_ver;
  819. p->efi->efi_memmap = (unsigned long)*map->map;
  820. p->efi->efi_memmap_size = *map->map_size;
  821. #ifdef CONFIG_X86_64
  822. p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
  823. p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
  824. #endif
  825. return EFI_SUCCESS;
  826. }
  827. static efi_status_t exit_boot(struct boot_params *boot_params,
  828. void *handle, bool is64)
  829. {
  830. unsigned long map_sz, key, desc_size, buff_size;
  831. efi_memory_desc_t *mem_map;
  832. struct setup_data *e820ext;
  833. __u32 e820ext_size;
  834. efi_status_t status;
  835. __u32 desc_version;
  836. struct efi_boot_memmap map;
  837. struct exit_boot_struct priv;
  838. map.map = &mem_map;
  839. map.map_size = &map_sz;
  840. map.desc_size = &desc_size;
  841. map.desc_ver = &desc_version;
  842. map.key_ptr = &key;
  843. map.buff_size = &buff_size;
  844. priv.boot_params = boot_params;
  845. priv.efi = &boot_params->efi_info;
  846. priv.e820ext = NULL;
  847. priv.e820ext_size = 0;
  848. priv.is64 = is64;
  849. /* Might as well exit boot services now */
  850. status = efi_exit_boot_services(sys_table, handle, &map, &priv,
  851. exit_boot_func);
  852. if (status != EFI_SUCCESS)
  853. return status;
  854. e820ext = priv.e820ext;
  855. e820ext_size = priv.e820ext_size;
  856. /* Historic? */
  857. boot_params->alt_mem_k = 32 * 1024;
  858. status = setup_e820(boot_params, e820ext, e820ext_size);
  859. if (status != EFI_SUCCESS)
  860. return status;
  861. return EFI_SUCCESS;
  862. }
  863. /*
  864. * On success we return a pointer to a boot_params structure, and NULL
  865. * on failure.
  866. */
  867. struct boot_params *efi_main(struct efi_config *c,
  868. struct boot_params *boot_params)
  869. {
  870. struct desc_ptr *gdt = NULL;
  871. efi_loaded_image_t *image;
  872. struct setup_header *hdr = &boot_params->hdr;
  873. efi_status_t status;
  874. struct desc_struct *desc;
  875. void *handle;
  876. efi_system_table_t *_table;
  877. bool is64;
  878. efi_early = c;
  879. _table = (efi_system_table_t *)(unsigned long)efi_early->table;
  880. handle = (void *)(unsigned long)efi_early->image_handle;
  881. is64 = efi_early->is64;
  882. sys_table = _table;
  883. /* Check if we were booted by the EFI firmware */
  884. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  885. goto fail;
  886. if (is64)
  887. setup_boot_services64(efi_early);
  888. else
  889. setup_boot_services32(efi_early);
  890. setup_graphics(boot_params);
  891. setup_efi_pci(boot_params);
  892. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  893. sizeof(*gdt), (void **)&gdt);
  894. if (status != EFI_SUCCESS) {
  895. efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
  896. goto fail;
  897. }
  898. gdt->size = 0x800;
  899. status = efi_low_alloc(sys_table, gdt->size, 8,
  900. (unsigned long *)&gdt->address);
  901. if (status != EFI_SUCCESS) {
  902. efi_printk(sys_table, "Failed to alloc mem for gdt\n");
  903. goto fail;
  904. }
  905. /*
  906. * If the kernel isn't already loaded at the preferred load
  907. * address, relocate it.
  908. */
  909. if (hdr->pref_address != hdr->code32_start) {
  910. unsigned long bzimage_addr = hdr->code32_start;
  911. status = efi_relocate_kernel(sys_table, &bzimage_addr,
  912. hdr->init_size, hdr->init_size,
  913. hdr->pref_address,
  914. hdr->kernel_alignment);
  915. if (status != EFI_SUCCESS) {
  916. efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
  917. goto fail;
  918. }
  919. hdr->pref_address = hdr->code32_start;
  920. hdr->code32_start = bzimage_addr;
  921. }
  922. status = exit_boot(boot_params, handle, is64);
  923. if (status != EFI_SUCCESS) {
  924. efi_printk(sys_table, "exit_boot() failed!\n");
  925. goto fail;
  926. }
  927. memset((char *)gdt->address, 0x0, gdt->size);
  928. desc = (struct desc_struct *)gdt->address;
  929. /* The first GDT is a dummy and the second is unused. */
  930. desc += 2;
  931. desc->limit0 = 0xffff;
  932. desc->base0 = 0x0000;
  933. desc->base1 = 0x0000;
  934. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  935. desc->s = DESC_TYPE_CODE_DATA;
  936. desc->dpl = 0;
  937. desc->p = 1;
  938. desc->limit = 0xf;
  939. desc->avl = 0;
  940. desc->l = 0;
  941. desc->d = SEG_OP_SIZE_32BIT;
  942. desc->g = SEG_GRANULARITY_4KB;
  943. desc->base2 = 0x00;
  944. desc++;
  945. desc->limit0 = 0xffff;
  946. desc->base0 = 0x0000;
  947. desc->base1 = 0x0000;
  948. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  949. desc->s = DESC_TYPE_CODE_DATA;
  950. desc->dpl = 0;
  951. desc->p = 1;
  952. desc->limit = 0xf;
  953. desc->avl = 0;
  954. desc->l = 0;
  955. desc->d = SEG_OP_SIZE_32BIT;
  956. desc->g = SEG_GRANULARITY_4KB;
  957. desc->base2 = 0x00;
  958. #ifdef CONFIG_X86_64
  959. /* Task segment value */
  960. desc++;
  961. desc->limit0 = 0x0000;
  962. desc->base0 = 0x0000;
  963. desc->base1 = 0x0000;
  964. desc->type = SEG_TYPE_TSS;
  965. desc->s = 0;
  966. desc->dpl = 0;
  967. desc->p = 1;
  968. desc->limit = 0x0;
  969. desc->avl = 0;
  970. desc->l = 0;
  971. desc->d = 0;
  972. desc->g = SEG_GRANULARITY_4KB;
  973. desc->base2 = 0x00;
  974. #endif /* CONFIG_X86_64 */
  975. asm volatile("cli");
  976. asm volatile ("lgdt %0" : : "m" (*gdt));
  977. return boot_params;
  978. fail:
  979. efi_printk(sys_table, "efi_main() failed!\n");
  980. return NULL;
  981. }