eboot.c 37 KB

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