eboot.c 27 KB

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