eboot.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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, u64 *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 ext_lfb_base;
  534. u64 fb_base;
  535. struct efi_pixel_bitmask pixel_info;
  536. int pixel_format;
  537. efi_status_t status;
  538. u32 *handles = (u32 *)(unsigned long)gop_handle;
  539. int i;
  540. first_gop = NULL;
  541. gop32 = NULL;
  542. nr_gops = size / sizeof(u32);
  543. for (i = 0; i < nr_gops; i++) {
  544. struct efi_graphics_output_mode_info *info = NULL;
  545. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  546. bool conout_found = false;
  547. void *dummy = NULL;
  548. u32 h = handles[i];
  549. u64 current_fb_base;
  550. status = efi_call_early(handle_protocol, h,
  551. proto, (void **)&gop32);
  552. if (status != EFI_SUCCESS)
  553. continue;
  554. status = efi_call_early(handle_protocol, h,
  555. &conout_proto, &dummy);
  556. if (status == EFI_SUCCESS)
  557. conout_found = true;
  558. status = __gop_query32(gop32, &info, &size, &current_fb_base);
  559. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  560. /*
  561. * Systems that use the UEFI Console Splitter may
  562. * provide multiple GOP devices, not all of which are
  563. * backed by real hardware. The workaround is to search
  564. * for a GOP implementing the ConOut protocol, and if
  565. * one isn't found, to just fall back to the first GOP.
  566. */
  567. width = info->horizontal_resolution;
  568. height = info->vertical_resolution;
  569. pixel_format = info->pixel_format;
  570. pixel_info = info->pixel_information;
  571. pixels_per_scan_line = info->pixels_per_scan_line;
  572. fb_base = current_fb_base;
  573. /*
  574. * Once we've found a GOP supporting ConOut,
  575. * don't bother looking any further.
  576. */
  577. first_gop = gop32;
  578. if (conout_found)
  579. break;
  580. }
  581. }
  582. /* Did we find any GOPs? */
  583. if (!first_gop)
  584. goto out;
  585. /* EFI framebuffer */
  586. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  587. si->lfb_width = width;
  588. si->lfb_height = height;
  589. si->lfb_base = fb_base;
  590. ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
  591. if (ext_lfb_base) {
  592. si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
  593. si->ext_lfb_base = ext_lfb_base;
  594. }
  595. si->pages = 1;
  596. setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
  597. si->lfb_size = si->lfb_linelength * si->lfb_height;
  598. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  599. out:
  600. return status;
  601. }
  602. static efi_status_t
  603. __gop_query64(struct efi_graphics_output_protocol_64 *gop64,
  604. struct efi_graphics_output_mode_info **info,
  605. unsigned long *size, u64 *fb_base)
  606. {
  607. struct efi_graphics_output_protocol_mode_64 *mode;
  608. efi_status_t status;
  609. unsigned long m;
  610. m = gop64->mode;
  611. mode = (struct efi_graphics_output_protocol_mode_64 *)m;
  612. status = efi_early->call(gop64->query_mode, gop64,
  613. mode->mode, size, info);
  614. if (status != EFI_SUCCESS)
  615. return status;
  616. *fb_base = mode->frame_buffer_base;
  617. return status;
  618. }
  619. static efi_status_t
  620. setup_gop64(struct screen_info *si, efi_guid_t *proto,
  621. unsigned long size, void **gop_handle)
  622. {
  623. struct efi_graphics_output_protocol_64 *gop64, *first_gop;
  624. unsigned long nr_gops;
  625. u16 width, height;
  626. u32 pixels_per_scan_line;
  627. u32 ext_lfb_base;
  628. u64 fb_base;
  629. struct efi_pixel_bitmask pixel_info;
  630. int pixel_format;
  631. efi_status_t status;
  632. u64 *handles = (u64 *)(unsigned long)gop_handle;
  633. int i;
  634. first_gop = NULL;
  635. gop64 = NULL;
  636. nr_gops = size / sizeof(u64);
  637. for (i = 0; i < nr_gops; i++) {
  638. struct efi_graphics_output_mode_info *info = NULL;
  639. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  640. bool conout_found = false;
  641. void *dummy = NULL;
  642. u64 h = handles[i];
  643. u64 current_fb_base;
  644. status = efi_call_early(handle_protocol, h,
  645. proto, (void **)&gop64);
  646. if (status != EFI_SUCCESS)
  647. continue;
  648. status = efi_call_early(handle_protocol, h,
  649. &conout_proto, &dummy);
  650. if (status == EFI_SUCCESS)
  651. conout_found = true;
  652. status = __gop_query64(gop64, &info, &size, &current_fb_base);
  653. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  654. /*
  655. * Systems that use the UEFI Console Splitter may
  656. * provide multiple GOP devices, not all of which are
  657. * backed by real hardware. The workaround is to search
  658. * for a GOP implementing the ConOut protocol, and if
  659. * one isn't found, to just fall back to the first GOP.
  660. */
  661. width = info->horizontal_resolution;
  662. height = info->vertical_resolution;
  663. pixel_format = info->pixel_format;
  664. pixel_info = info->pixel_information;
  665. pixels_per_scan_line = info->pixels_per_scan_line;
  666. fb_base = current_fb_base;
  667. /*
  668. * Once we've found a GOP supporting ConOut,
  669. * don't bother looking any further.
  670. */
  671. first_gop = gop64;
  672. if (conout_found)
  673. break;
  674. }
  675. }
  676. /* Did we find any GOPs? */
  677. if (!first_gop)
  678. goto out;
  679. /* EFI framebuffer */
  680. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  681. si->lfb_width = width;
  682. si->lfb_height = height;
  683. si->lfb_base = fb_base;
  684. ext_lfb_base = (u64)(unsigned long)fb_base >> 32;
  685. if (ext_lfb_base) {
  686. si->capabilities |= VIDEO_CAPABILITY_64BIT_BASE;
  687. si->ext_lfb_base = ext_lfb_base;
  688. }
  689. si->pages = 1;
  690. setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
  691. si->lfb_size = si->lfb_linelength * si->lfb_height;
  692. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  693. out:
  694. return status;
  695. }
  696. /*
  697. * See if we have Graphics Output Protocol
  698. */
  699. static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
  700. unsigned long size)
  701. {
  702. efi_status_t status;
  703. void **gop_handle = NULL;
  704. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  705. size, (void **)&gop_handle);
  706. if (status != EFI_SUCCESS)
  707. return status;
  708. status = efi_call_early(locate_handle,
  709. EFI_LOCATE_BY_PROTOCOL,
  710. proto, NULL, &size, gop_handle);
  711. if (status != EFI_SUCCESS)
  712. goto free_handle;
  713. if (efi_early->is64)
  714. status = setup_gop64(si, proto, size, gop_handle);
  715. else
  716. status = setup_gop32(si, proto, size, gop_handle);
  717. free_handle:
  718. efi_call_early(free_pool, gop_handle);
  719. return status;
  720. }
  721. static efi_status_t
  722. setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
  723. {
  724. struct efi_uga_draw_protocol *uga = NULL, *first_uga;
  725. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  726. unsigned long nr_ugas;
  727. u32 *handles = (u32 *)uga_handle;;
  728. efi_status_t status;
  729. int i;
  730. first_uga = NULL;
  731. nr_ugas = size / sizeof(u32);
  732. for (i = 0; i < nr_ugas; i++) {
  733. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  734. u32 w, h, depth, refresh;
  735. void *pciio;
  736. u32 handle = handles[i];
  737. status = efi_call_early(handle_protocol, handle,
  738. &uga_proto, (void **)&uga);
  739. if (status != EFI_SUCCESS)
  740. continue;
  741. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  742. status = efi_early->call((unsigned long)uga->get_mode, uga,
  743. &w, &h, &depth, &refresh);
  744. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  745. *width = w;
  746. *height = h;
  747. /*
  748. * Once we've found a UGA supporting PCIIO,
  749. * don't bother looking any further.
  750. */
  751. if (pciio)
  752. break;
  753. first_uga = uga;
  754. }
  755. }
  756. return status;
  757. }
  758. static efi_status_t
  759. setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
  760. {
  761. struct efi_uga_draw_protocol *uga = NULL, *first_uga;
  762. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  763. unsigned long nr_ugas;
  764. u64 *handles = (u64 *)uga_handle;;
  765. efi_status_t status;
  766. int i;
  767. first_uga = NULL;
  768. nr_ugas = size / sizeof(u64);
  769. for (i = 0; i < nr_ugas; i++) {
  770. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  771. u32 w, h, depth, refresh;
  772. void *pciio;
  773. u64 handle = handles[i];
  774. status = efi_call_early(handle_protocol, handle,
  775. &uga_proto, (void **)&uga);
  776. if (status != EFI_SUCCESS)
  777. continue;
  778. efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
  779. status = efi_early->call((unsigned long)uga->get_mode, uga,
  780. &w, &h, &depth, &refresh);
  781. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  782. *width = w;
  783. *height = h;
  784. /*
  785. * Once we've found a UGA supporting PCIIO,
  786. * don't bother looking any further.
  787. */
  788. if (pciio)
  789. break;
  790. first_uga = uga;
  791. }
  792. }
  793. return status;
  794. }
  795. /*
  796. * See if we have Universal Graphics Adapter (UGA) protocol
  797. */
  798. static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
  799. unsigned long size)
  800. {
  801. efi_status_t status;
  802. u32 width, height;
  803. void **uga_handle = NULL;
  804. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  805. size, (void **)&uga_handle);
  806. if (status != EFI_SUCCESS)
  807. return status;
  808. status = efi_call_early(locate_handle,
  809. EFI_LOCATE_BY_PROTOCOL,
  810. uga_proto, NULL, &size, uga_handle);
  811. if (status != EFI_SUCCESS)
  812. goto free_handle;
  813. height = 0;
  814. width = 0;
  815. if (efi_early->is64)
  816. status = setup_uga64(uga_handle, size, &width, &height);
  817. else
  818. status = setup_uga32(uga_handle, size, &width, &height);
  819. if (!width && !height)
  820. goto free_handle;
  821. /* EFI framebuffer */
  822. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  823. si->lfb_depth = 32;
  824. si->lfb_width = width;
  825. si->lfb_height = height;
  826. si->red_size = 8;
  827. si->red_pos = 16;
  828. si->green_size = 8;
  829. si->green_pos = 8;
  830. si->blue_size = 8;
  831. si->blue_pos = 0;
  832. si->rsvd_size = 8;
  833. si->rsvd_pos = 24;
  834. free_handle:
  835. efi_call_early(free_pool, uga_handle);
  836. return status;
  837. }
  838. void setup_graphics(struct boot_params *boot_params)
  839. {
  840. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  841. struct screen_info *si;
  842. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  843. efi_status_t status;
  844. unsigned long size;
  845. void **gop_handle = NULL;
  846. void **uga_handle = NULL;
  847. si = &boot_params->screen_info;
  848. memset(si, 0, sizeof(*si));
  849. size = 0;
  850. status = efi_call_early(locate_handle,
  851. EFI_LOCATE_BY_PROTOCOL,
  852. &graphics_proto, NULL, &size, gop_handle);
  853. if (status == EFI_BUFFER_TOO_SMALL)
  854. status = setup_gop(si, &graphics_proto, size);
  855. if (status != EFI_SUCCESS) {
  856. size = 0;
  857. status = efi_call_early(locate_handle,
  858. EFI_LOCATE_BY_PROTOCOL,
  859. &uga_proto, NULL, &size, uga_handle);
  860. if (status == EFI_BUFFER_TOO_SMALL)
  861. setup_uga(si, &uga_proto, size);
  862. }
  863. }
  864. /*
  865. * Because the x86 boot code expects to be passed a boot_params we
  866. * need to create one ourselves (usually the bootloader would create
  867. * one for us).
  868. *
  869. * The caller is responsible for filling out ->code32_start in the
  870. * returned boot_params.
  871. */
  872. struct boot_params *make_boot_params(struct efi_config *c)
  873. {
  874. struct boot_params *boot_params;
  875. struct apm_bios_info *bi;
  876. struct setup_header *hdr;
  877. struct efi_info *efi;
  878. efi_loaded_image_t *image;
  879. void *options, *handle;
  880. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  881. int options_size = 0;
  882. efi_status_t status;
  883. char *cmdline_ptr;
  884. u16 *s2;
  885. u8 *s1;
  886. int i;
  887. unsigned long ramdisk_addr;
  888. unsigned long ramdisk_size;
  889. efi_early = c;
  890. sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
  891. handle = (void *)(unsigned long)efi_early->image_handle;
  892. /* Check if we were booted by the EFI firmware */
  893. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  894. return NULL;
  895. if (efi_early->is64)
  896. setup_boot_services64(efi_early);
  897. else
  898. setup_boot_services32(efi_early);
  899. status = efi_call_early(handle_protocol, handle,
  900. &proto, (void *)&image);
  901. if (status != EFI_SUCCESS) {
  902. efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  903. return NULL;
  904. }
  905. status = efi_low_alloc(sys_table, 0x4000, 1,
  906. (unsigned long *)&boot_params);
  907. if (status != EFI_SUCCESS) {
  908. efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
  909. return NULL;
  910. }
  911. memset(boot_params, 0x0, 0x4000);
  912. hdr = &boot_params->hdr;
  913. efi = &boot_params->efi_info;
  914. bi = &boot_params->apm_bios_info;
  915. /* Copy the second sector to boot_params */
  916. memcpy(&hdr->jump, image->image_base + 512, 512);
  917. /*
  918. * Fill out some of the header fields ourselves because the
  919. * EFI firmware loader doesn't load the first sector.
  920. */
  921. hdr->root_flags = 1;
  922. hdr->vid_mode = 0xffff;
  923. hdr->boot_flag = 0xAA55;
  924. hdr->type_of_loader = 0x21;
  925. /* Convert unicode cmdline to ascii */
  926. cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
  927. if (!cmdline_ptr)
  928. goto fail;
  929. hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
  930. /* Fill in upper bits of command line address, NOP on 32 bit */
  931. boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
  932. hdr->ramdisk_image = 0;
  933. hdr->ramdisk_size = 0;
  934. /* Clear APM BIOS info */
  935. memset(bi, 0, sizeof(*bi));
  936. status = efi_parse_options(cmdline_ptr);
  937. if (status != EFI_SUCCESS)
  938. goto fail2;
  939. status = handle_cmdline_files(sys_table, image,
  940. (char *)(unsigned long)hdr->cmd_line_ptr,
  941. "initrd=", hdr->initrd_addr_max,
  942. &ramdisk_addr, &ramdisk_size);
  943. if (status != EFI_SUCCESS &&
  944. hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
  945. efi_printk(sys_table, "Trying to load files to higher address\n");
  946. status = handle_cmdline_files(sys_table, image,
  947. (char *)(unsigned long)hdr->cmd_line_ptr,
  948. "initrd=", -1UL,
  949. &ramdisk_addr, &ramdisk_size);
  950. }
  951. if (status != EFI_SUCCESS)
  952. goto fail2;
  953. hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
  954. hdr->ramdisk_size = ramdisk_size & 0xffffffff;
  955. boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
  956. boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32;
  957. return boot_params;
  958. fail2:
  959. efi_free(sys_table, options_size, hdr->cmd_line_ptr);
  960. fail:
  961. efi_free(sys_table, 0x4000, (unsigned long)boot_params);
  962. return NULL;
  963. }
  964. static void add_e820ext(struct boot_params *params,
  965. struct setup_data *e820ext, u32 nr_entries)
  966. {
  967. struct setup_data *data;
  968. efi_status_t status;
  969. unsigned long size;
  970. e820ext->type = SETUP_E820_EXT;
  971. e820ext->len = nr_entries * sizeof(struct e820entry);
  972. e820ext->next = 0;
  973. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  974. while (data && data->next)
  975. data = (struct setup_data *)(unsigned long)data->next;
  976. if (data)
  977. data->next = (unsigned long)e820ext;
  978. else
  979. params->hdr.setup_data = (unsigned long)e820ext;
  980. }
  981. static efi_status_t setup_e820(struct boot_params *params,
  982. struct setup_data *e820ext, u32 e820ext_size)
  983. {
  984. struct e820entry *e820_map = &params->e820_map[0];
  985. struct efi_info *efi = &params->efi_info;
  986. struct e820entry *prev = NULL;
  987. u32 nr_entries;
  988. u32 nr_desc;
  989. int i;
  990. nr_entries = 0;
  991. nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
  992. for (i = 0; i < nr_desc; i++) {
  993. efi_memory_desc_t *d;
  994. unsigned int e820_type = 0;
  995. unsigned long m = efi->efi_memmap;
  996. #ifdef CONFIG_X86_64
  997. m |= (u64)efi->efi_memmap_hi << 32;
  998. #endif
  999. d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
  1000. switch (d->type) {
  1001. case EFI_RESERVED_TYPE:
  1002. case EFI_RUNTIME_SERVICES_CODE:
  1003. case EFI_RUNTIME_SERVICES_DATA:
  1004. case EFI_MEMORY_MAPPED_IO:
  1005. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  1006. case EFI_PAL_CODE:
  1007. e820_type = E820_RESERVED;
  1008. break;
  1009. case EFI_UNUSABLE_MEMORY:
  1010. e820_type = E820_UNUSABLE;
  1011. break;
  1012. case EFI_ACPI_RECLAIM_MEMORY:
  1013. e820_type = E820_ACPI;
  1014. break;
  1015. case EFI_LOADER_CODE:
  1016. case EFI_LOADER_DATA:
  1017. case EFI_BOOT_SERVICES_CODE:
  1018. case EFI_BOOT_SERVICES_DATA:
  1019. case EFI_CONVENTIONAL_MEMORY:
  1020. e820_type = E820_RAM;
  1021. break;
  1022. case EFI_ACPI_MEMORY_NVS:
  1023. e820_type = E820_NVS;
  1024. break;
  1025. case EFI_PERSISTENT_MEMORY:
  1026. e820_type = E820_PMEM;
  1027. break;
  1028. default:
  1029. continue;
  1030. }
  1031. /* Merge adjacent mappings */
  1032. if (prev && prev->type == e820_type &&
  1033. (prev->addr + prev->size) == d->phys_addr) {
  1034. prev->size += d->num_pages << 12;
  1035. continue;
  1036. }
  1037. if (nr_entries == ARRAY_SIZE(params->e820_map)) {
  1038. u32 need = (nr_desc - i) * sizeof(struct e820entry) +
  1039. sizeof(struct setup_data);
  1040. if (!e820ext || e820ext_size < need)
  1041. return EFI_BUFFER_TOO_SMALL;
  1042. /* boot_params map full, switch to e820 extended */
  1043. e820_map = (struct e820entry *)e820ext->data;
  1044. }
  1045. e820_map->addr = d->phys_addr;
  1046. e820_map->size = d->num_pages << PAGE_SHIFT;
  1047. e820_map->type = e820_type;
  1048. prev = e820_map++;
  1049. nr_entries++;
  1050. }
  1051. if (nr_entries > ARRAY_SIZE(params->e820_map)) {
  1052. u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
  1053. add_e820ext(params, e820ext, nr_e820ext);
  1054. nr_entries -= nr_e820ext;
  1055. }
  1056. params->e820_entries = (u8)nr_entries;
  1057. return EFI_SUCCESS;
  1058. }
  1059. static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
  1060. u32 *e820ext_size)
  1061. {
  1062. efi_status_t status;
  1063. unsigned long size;
  1064. size = sizeof(struct setup_data) +
  1065. sizeof(struct e820entry) * nr_desc;
  1066. if (*e820ext) {
  1067. efi_call_early(free_pool, *e820ext);
  1068. *e820ext = NULL;
  1069. *e820ext_size = 0;
  1070. }
  1071. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  1072. size, (void **)e820ext);
  1073. if (status == EFI_SUCCESS)
  1074. *e820ext_size = size;
  1075. return status;
  1076. }
  1077. static efi_status_t exit_boot(struct boot_params *boot_params,
  1078. void *handle, bool is64)
  1079. {
  1080. struct efi_info *efi = &boot_params->efi_info;
  1081. unsigned long map_sz, key, desc_size;
  1082. efi_memory_desc_t *mem_map;
  1083. struct setup_data *e820ext;
  1084. const char *signature;
  1085. __u32 e820ext_size;
  1086. __u32 nr_desc, prev_nr_desc;
  1087. efi_status_t status;
  1088. __u32 desc_version;
  1089. bool called_exit = false;
  1090. u8 nr_entries;
  1091. int i;
  1092. nr_desc = 0;
  1093. e820ext = NULL;
  1094. e820ext_size = 0;
  1095. get_map:
  1096. status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
  1097. &desc_version, &key);
  1098. if (status != EFI_SUCCESS)
  1099. return status;
  1100. prev_nr_desc = nr_desc;
  1101. nr_desc = map_sz / desc_size;
  1102. if (nr_desc > prev_nr_desc &&
  1103. nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
  1104. u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
  1105. status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
  1106. if (status != EFI_SUCCESS)
  1107. goto free_mem_map;
  1108. efi_call_early(free_pool, mem_map);
  1109. goto get_map; /* Allocated memory, get map again */
  1110. }
  1111. signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
  1112. memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
  1113. efi->efi_systab = (unsigned long)sys_table;
  1114. efi->efi_memdesc_size = desc_size;
  1115. efi->efi_memdesc_version = desc_version;
  1116. efi->efi_memmap = (unsigned long)mem_map;
  1117. efi->efi_memmap_size = map_sz;
  1118. #ifdef CONFIG_X86_64
  1119. efi->efi_systab_hi = (unsigned long)sys_table >> 32;
  1120. efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
  1121. #endif
  1122. /* Might as well exit boot services now */
  1123. status = efi_call_early(exit_boot_services, handle, key);
  1124. if (status != EFI_SUCCESS) {
  1125. /*
  1126. * ExitBootServices() will fail if any of the event
  1127. * handlers change the memory map. In which case, we
  1128. * must be prepared to retry, but only once so that
  1129. * we're guaranteed to exit on repeated failures instead
  1130. * of spinning forever.
  1131. */
  1132. if (called_exit)
  1133. goto free_mem_map;
  1134. called_exit = true;
  1135. efi_call_early(free_pool, mem_map);
  1136. goto get_map;
  1137. }
  1138. /* Historic? */
  1139. boot_params->alt_mem_k = 32 * 1024;
  1140. status = setup_e820(boot_params, e820ext, e820ext_size);
  1141. if (status != EFI_SUCCESS)
  1142. return status;
  1143. return EFI_SUCCESS;
  1144. free_mem_map:
  1145. efi_call_early(free_pool, mem_map);
  1146. return status;
  1147. }
  1148. /*
  1149. * On success we return a pointer to a boot_params structure, and NULL
  1150. * on failure.
  1151. */
  1152. struct boot_params *efi_main(struct efi_config *c,
  1153. struct boot_params *boot_params)
  1154. {
  1155. struct desc_ptr *gdt = NULL;
  1156. efi_loaded_image_t *image;
  1157. struct setup_header *hdr = &boot_params->hdr;
  1158. efi_status_t status;
  1159. struct desc_struct *desc;
  1160. void *handle;
  1161. efi_system_table_t *_table;
  1162. bool is64;
  1163. efi_early = c;
  1164. _table = (efi_system_table_t *)(unsigned long)efi_early->table;
  1165. handle = (void *)(unsigned long)efi_early->image_handle;
  1166. is64 = efi_early->is64;
  1167. sys_table = _table;
  1168. /* Check if we were booted by the EFI firmware */
  1169. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  1170. goto fail;
  1171. if (is64)
  1172. setup_boot_services64(efi_early);
  1173. else
  1174. setup_boot_services32(efi_early);
  1175. setup_graphics(boot_params);
  1176. setup_efi_pci(boot_params);
  1177. status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
  1178. sizeof(*gdt), (void **)&gdt);
  1179. if (status != EFI_SUCCESS) {
  1180. efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
  1181. goto fail;
  1182. }
  1183. gdt->size = 0x800;
  1184. status = efi_low_alloc(sys_table, gdt->size, 8,
  1185. (unsigned long *)&gdt->address);
  1186. if (status != EFI_SUCCESS) {
  1187. efi_printk(sys_table, "Failed to alloc mem for gdt\n");
  1188. goto fail;
  1189. }
  1190. /*
  1191. * If the kernel isn't already loaded at the preferred load
  1192. * address, relocate it.
  1193. */
  1194. if (hdr->pref_address != hdr->code32_start) {
  1195. unsigned long bzimage_addr = hdr->code32_start;
  1196. status = efi_relocate_kernel(sys_table, &bzimage_addr,
  1197. hdr->init_size, hdr->init_size,
  1198. hdr->pref_address,
  1199. hdr->kernel_alignment);
  1200. if (status != EFI_SUCCESS) {
  1201. efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
  1202. goto fail;
  1203. }
  1204. hdr->pref_address = hdr->code32_start;
  1205. hdr->code32_start = bzimage_addr;
  1206. }
  1207. status = exit_boot(boot_params, handle, is64);
  1208. if (status != EFI_SUCCESS) {
  1209. efi_printk(sys_table, "exit_boot() failed!\n");
  1210. goto fail;
  1211. }
  1212. memset((char *)gdt->address, 0x0, gdt->size);
  1213. desc = (struct desc_struct *)gdt->address;
  1214. /* The first GDT is a dummy and the second is unused. */
  1215. desc += 2;
  1216. desc->limit0 = 0xffff;
  1217. desc->base0 = 0x0000;
  1218. desc->base1 = 0x0000;
  1219. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  1220. desc->s = DESC_TYPE_CODE_DATA;
  1221. desc->dpl = 0;
  1222. desc->p = 1;
  1223. desc->limit = 0xf;
  1224. desc->avl = 0;
  1225. desc->l = 0;
  1226. desc->d = SEG_OP_SIZE_32BIT;
  1227. desc->g = SEG_GRANULARITY_4KB;
  1228. desc->base2 = 0x00;
  1229. desc++;
  1230. desc->limit0 = 0xffff;
  1231. desc->base0 = 0x0000;
  1232. desc->base1 = 0x0000;
  1233. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  1234. desc->s = DESC_TYPE_CODE_DATA;
  1235. desc->dpl = 0;
  1236. desc->p = 1;
  1237. desc->limit = 0xf;
  1238. desc->avl = 0;
  1239. desc->l = 0;
  1240. desc->d = SEG_OP_SIZE_32BIT;
  1241. desc->g = SEG_GRANULARITY_4KB;
  1242. desc->base2 = 0x00;
  1243. #ifdef CONFIG_X86_64
  1244. /* Task segment value */
  1245. desc++;
  1246. desc->limit0 = 0x0000;
  1247. desc->base0 = 0x0000;
  1248. desc->base1 = 0x0000;
  1249. desc->type = SEG_TYPE_TSS;
  1250. desc->s = 0;
  1251. desc->dpl = 0;
  1252. desc->p = 1;
  1253. desc->limit = 0x0;
  1254. desc->avl = 0;
  1255. desc->l = 0;
  1256. desc->d = 0;
  1257. desc->g = SEG_GRANULARITY_4KB;
  1258. desc->base2 = 0x00;
  1259. #endif /* CONFIG_X86_64 */
  1260. asm volatile("cli");
  1261. asm volatile ("lgdt %0" : : "m" (*gdt));
  1262. return boot_params;
  1263. fail:
  1264. efi_printk(sys_table, "efi_main() failed!\n");
  1265. return NULL;
  1266. }