eboot.c 31 KB

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