eboot.c 29 KB

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