eboot.c 31 KB

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