eboot.c 37 KB

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