eboot.c 29 KB

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