eboot.c 35 KB

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