eboot.c 36 KB

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