eboot.c 36 KB

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