eboot.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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. #undef memcpy /* Use memcpy from misc.c */
  15. #include "eboot.h"
  16. static efi_system_table_t *sys_table;
  17. static void efi_char16_printk(efi_char16_t *str)
  18. {
  19. struct efi_simple_text_output_protocol *out;
  20. out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
  21. efi_call_phys2(out->output_string, out, str);
  22. }
  23. static void efi_printk(char *str)
  24. {
  25. char *s8;
  26. for (s8 = str; *s8; s8++) {
  27. efi_char16_t ch[2] = { 0 };
  28. ch[0] = *s8;
  29. if (*s8 == '\n') {
  30. efi_char16_t nl[2] = { '\r', 0 };
  31. efi_char16_printk(nl);
  32. }
  33. efi_char16_printk(ch);
  34. }
  35. }
  36. static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
  37. unsigned long *desc_size)
  38. {
  39. efi_memory_desc_t *m = NULL;
  40. efi_status_t status;
  41. unsigned long key;
  42. u32 desc_version;
  43. *map_size = sizeof(*m) * 32;
  44. again:
  45. /*
  46. * Add an additional efi_memory_desc_t because we're doing an
  47. * allocation which may be in a new descriptor region.
  48. */
  49. *map_size += sizeof(*m);
  50. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  51. EFI_LOADER_DATA, *map_size, (void **)&m);
  52. if (status != EFI_SUCCESS)
  53. goto fail;
  54. status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
  55. m, &key, desc_size, &desc_version);
  56. if (status == EFI_BUFFER_TOO_SMALL) {
  57. efi_call_phys1(sys_table->boottime->free_pool, m);
  58. goto again;
  59. }
  60. if (status != EFI_SUCCESS)
  61. efi_call_phys1(sys_table->boottime->free_pool, m);
  62. fail:
  63. *map = m;
  64. return status;
  65. }
  66. /*
  67. * Allocate at the highest possible address that is not above 'max'.
  68. */
  69. static efi_status_t high_alloc(unsigned long size, unsigned long align,
  70. unsigned long *addr, unsigned long max)
  71. {
  72. unsigned long map_size, desc_size;
  73. efi_memory_desc_t *map;
  74. efi_status_t status;
  75. unsigned long nr_pages;
  76. u64 max_addr = 0;
  77. int i;
  78. status = __get_map(&map, &map_size, &desc_size);
  79. if (status != EFI_SUCCESS)
  80. goto fail;
  81. nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  82. again:
  83. for (i = 0; i < map_size / desc_size; i++) {
  84. efi_memory_desc_t *desc;
  85. unsigned long m = (unsigned long)map;
  86. u64 start, end;
  87. desc = (efi_memory_desc_t *)(m + (i * desc_size));
  88. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  89. continue;
  90. if (desc->num_pages < nr_pages)
  91. continue;
  92. start = desc->phys_addr;
  93. end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
  94. if ((start + size) > end || (start + size) > max)
  95. continue;
  96. if (end - size > max)
  97. end = max;
  98. if (round_down(end - size, align) < start)
  99. continue;
  100. start = round_down(end - size, align);
  101. /*
  102. * Don't allocate at 0x0. It will confuse code that
  103. * checks pointers against NULL.
  104. */
  105. if (start == 0x0)
  106. continue;
  107. if (start > max_addr)
  108. max_addr = start;
  109. }
  110. if (!max_addr)
  111. status = EFI_NOT_FOUND;
  112. else {
  113. status = efi_call_phys4(sys_table->boottime->allocate_pages,
  114. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  115. nr_pages, &max_addr);
  116. if (status != EFI_SUCCESS) {
  117. max = max_addr;
  118. max_addr = 0;
  119. goto again;
  120. }
  121. *addr = max_addr;
  122. }
  123. free_pool:
  124. efi_call_phys1(sys_table->boottime->free_pool, map);
  125. fail:
  126. return status;
  127. }
  128. /*
  129. * Allocate at the lowest possible address.
  130. */
  131. static efi_status_t low_alloc(unsigned long size, unsigned long align,
  132. unsigned long *addr)
  133. {
  134. unsigned long map_size, desc_size;
  135. efi_memory_desc_t *map;
  136. efi_status_t status;
  137. unsigned long nr_pages;
  138. int i;
  139. status = __get_map(&map, &map_size, &desc_size);
  140. if (status != EFI_SUCCESS)
  141. goto fail;
  142. nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  143. for (i = 0; i < map_size / desc_size; i++) {
  144. efi_memory_desc_t *desc;
  145. unsigned long m = (unsigned long)map;
  146. u64 start, end;
  147. desc = (efi_memory_desc_t *)(m + (i * desc_size));
  148. if (desc->type != EFI_CONVENTIONAL_MEMORY)
  149. continue;
  150. if (desc->num_pages < nr_pages)
  151. continue;
  152. start = desc->phys_addr;
  153. end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
  154. /*
  155. * Don't allocate at 0x0. It will confuse code that
  156. * checks pointers against NULL. Skip the first 8
  157. * bytes so we start at a nice even number.
  158. */
  159. if (start == 0x0)
  160. start += 8;
  161. start = round_up(start, align);
  162. if ((start + size) > end)
  163. continue;
  164. status = efi_call_phys4(sys_table->boottime->allocate_pages,
  165. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  166. nr_pages, &start);
  167. if (status == EFI_SUCCESS) {
  168. *addr = start;
  169. break;
  170. }
  171. }
  172. if (i == map_size / desc_size)
  173. status = EFI_NOT_FOUND;
  174. free_pool:
  175. efi_call_phys1(sys_table->boottime->free_pool, map);
  176. fail:
  177. return status;
  178. }
  179. static void low_free(unsigned long size, unsigned long addr)
  180. {
  181. unsigned long nr_pages;
  182. nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  183. efi_call_phys2(sys_table->boottime->free_pages, addr, size);
  184. }
  185. static void find_bits(unsigned long mask, u8 *pos, u8 *size)
  186. {
  187. u8 first, len;
  188. first = 0;
  189. len = 0;
  190. if (mask) {
  191. while (!(mask & 0x1)) {
  192. mask = mask >> 1;
  193. first++;
  194. }
  195. while (mask & 0x1) {
  196. mask = mask >> 1;
  197. len++;
  198. }
  199. }
  200. *pos = first;
  201. *size = len;
  202. }
  203. static efi_status_t setup_efi_vars(struct boot_params *params)
  204. {
  205. struct setup_data *data;
  206. struct efi_var_bootdata *efidata;
  207. u64 store_size, remaining_size, var_size;
  208. efi_status_t status;
  209. if (sys_table->runtime->hdr.revision < EFI_2_00_SYSTEM_TABLE_REVISION)
  210. return EFI_UNSUPPORTED;
  211. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  212. while (data && data->next)
  213. data = (struct setup_data *)(unsigned long)data->next;
  214. status = efi_call_phys4((void *)sys_table->runtime->query_variable_info,
  215. EFI_VARIABLE_NON_VOLATILE |
  216. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  217. EFI_VARIABLE_RUNTIME_ACCESS, &store_size,
  218. &remaining_size, &var_size);
  219. if (status != EFI_SUCCESS)
  220. return status;
  221. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  222. EFI_LOADER_DATA, sizeof(*efidata), &efidata);
  223. if (status != EFI_SUCCESS)
  224. return status;
  225. efidata->data.type = SETUP_EFI_VARS;
  226. efidata->data.len = sizeof(struct efi_var_bootdata) -
  227. sizeof(struct setup_data);
  228. efidata->data.next = 0;
  229. efidata->store_size = store_size;
  230. efidata->remaining_size = remaining_size;
  231. efidata->max_var_size = var_size;
  232. if (data)
  233. data->next = (unsigned long)efidata;
  234. else
  235. params->hdr.setup_data = (unsigned long)efidata;
  236. }
  237. static efi_status_t setup_efi_pci(struct boot_params *params)
  238. {
  239. efi_pci_io_protocol *pci;
  240. efi_status_t status;
  241. void **pci_handle;
  242. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  243. unsigned long nr_pci, size = 0;
  244. int i;
  245. struct setup_data *data;
  246. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  247. while (data && data->next)
  248. data = (struct setup_data *)(unsigned long)data->next;
  249. status = efi_call_phys5(sys_table->boottime->locate_handle,
  250. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  251. NULL, &size, pci_handle);
  252. if (status == EFI_BUFFER_TOO_SMALL) {
  253. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  254. EFI_LOADER_DATA, size, &pci_handle);
  255. if (status != EFI_SUCCESS)
  256. return status;
  257. status = efi_call_phys5(sys_table->boottime->locate_handle,
  258. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  259. NULL, &size, pci_handle);
  260. }
  261. if (status != EFI_SUCCESS)
  262. goto free_handle;
  263. nr_pci = size / sizeof(void *);
  264. for (i = 0; i < nr_pci; i++) {
  265. void *h = pci_handle[i];
  266. uint64_t attributes;
  267. struct pci_setup_rom *rom;
  268. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  269. h, &pci_proto, &pci);
  270. if (status != EFI_SUCCESS)
  271. continue;
  272. if (!pci)
  273. continue;
  274. #ifdef CONFIG_X86_64
  275. status = efi_call_phys4(pci->attributes, pci,
  276. EfiPciIoAttributeOperationGet, 0,
  277. &attributes);
  278. #else
  279. status = efi_call_phys5(pci->attributes, pci,
  280. EfiPciIoAttributeOperationGet, 0, 0,
  281. &attributes);
  282. #endif
  283. if (status != EFI_SUCCESS)
  284. continue;
  285. if (!pci->romimage || !pci->romsize)
  286. continue;
  287. size = pci->romsize + sizeof(*rom);
  288. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  289. EFI_LOADER_DATA, size, &rom);
  290. if (status != EFI_SUCCESS)
  291. continue;
  292. rom->data.type = SETUP_PCI;
  293. rom->data.len = size - sizeof(struct setup_data);
  294. rom->data.next = 0;
  295. rom->pcilen = pci->romsize;
  296. status = efi_call_phys5(pci->pci.read, pci,
  297. EfiPciIoWidthUint16, PCI_VENDOR_ID,
  298. 1, &(rom->vendor));
  299. if (status != EFI_SUCCESS)
  300. goto free_struct;
  301. status = efi_call_phys5(pci->pci.read, pci,
  302. EfiPciIoWidthUint16, PCI_DEVICE_ID,
  303. 1, &(rom->devid));
  304. if (status != EFI_SUCCESS)
  305. goto free_struct;
  306. status = efi_call_phys5(pci->get_location, pci,
  307. &(rom->segment), &(rom->bus),
  308. &(rom->device), &(rom->function));
  309. if (status != EFI_SUCCESS)
  310. goto free_struct;
  311. memcpy(rom->romdata, pci->romimage, pci->romsize);
  312. if (data)
  313. data->next = (unsigned long)rom;
  314. else
  315. params->hdr.setup_data = (unsigned long)rom;
  316. data = (struct setup_data *)rom;
  317. continue;
  318. free_struct:
  319. efi_call_phys1(sys_table->boottime->free_pool, rom);
  320. }
  321. free_handle:
  322. efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
  323. return status;
  324. }
  325. /*
  326. * See if we have Graphics Output Protocol
  327. */
  328. static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
  329. unsigned long size)
  330. {
  331. struct efi_graphics_output_protocol *gop, *first_gop;
  332. struct efi_pixel_bitmask pixel_info;
  333. unsigned long nr_gops;
  334. efi_status_t status;
  335. void **gop_handle;
  336. u16 width, height;
  337. u32 fb_base, fb_size;
  338. u32 pixels_per_scan_line;
  339. int pixel_format;
  340. int i;
  341. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  342. EFI_LOADER_DATA, size, &gop_handle);
  343. if (status != EFI_SUCCESS)
  344. return status;
  345. status = efi_call_phys5(sys_table->boottime->locate_handle,
  346. EFI_LOCATE_BY_PROTOCOL, proto,
  347. NULL, &size, gop_handle);
  348. if (status != EFI_SUCCESS)
  349. goto free_handle;
  350. first_gop = NULL;
  351. nr_gops = size / sizeof(void *);
  352. for (i = 0; i < nr_gops; i++) {
  353. struct efi_graphics_output_mode_info *info;
  354. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  355. bool conout_found = false;
  356. void *dummy;
  357. void *h = gop_handle[i];
  358. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  359. h, proto, &gop);
  360. if (status != EFI_SUCCESS)
  361. continue;
  362. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  363. h, &conout_proto, &dummy);
  364. if (status == EFI_SUCCESS)
  365. conout_found = true;
  366. status = efi_call_phys4(gop->query_mode, gop,
  367. gop->mode->mode, &size, &info);
  368. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  369. /*
  370. * Systems that use the UEFI Console Splitter may
  371. * provide multiple GOP devices, not all of which are
  372. * backed by real hardware. The workaround is to search
  373. * for a GOP implementing the ConOut protocol, and if
  374. * one isn't found, to just fall back to the first GOP.
  375. */
  376. width = info->horizontal_resolution;
  377. height = info->vertical_resolution;
  378. fb_base = gop->mode->frame_buffer_base;
  379. fb_size = gop->mode->frame_buffer_size;
  380. pixel_format = info->pixel_format;
  381. pixel_info = info->pixel_information;
  382. pixels_per_scan_line = info->pixels_per_scan_line;
  383. /*
  384. * Once we've found a GOP supporting ConOut,
  385. * don't bother looking any further.
  386. */
  387. first_gop = gop;
  388. if (conout_found)
  389. break;
  390. }
  391. }
  392. /* Did we find any GOPs? */
  393. if (!first_gop)
  394. goto free_handle;
  395. /* EFI framebuffer */
  396. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  397. si->lfb_width = width;
  398. si->lfb_height = height;
  399. si->lfb_base = fb_base;
  400. si->pages = 1;
  401. if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
  402. si->lfb_depth = 32;
  403. si->lfb_linelength = pixels_per_scan_line * 4;
  404. si->red_size = 8;
  405. si->red_pos = 0;
  406. si->green_size = 8;
  407. si->green_pos = 8;
  408. si->blue_size = 8;
  409. si->blue_pos = 16;
  410. si->rsvd_size = 8;
  411. si->rsvd_pos = 24;
  412. } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
  413. si->lfb_depth = 32;
  414. si->lfb_linelength = pixels_per_scan_line * 4;
  415. si->red_size = 8;
  416. si->red_pos = 16;
  417. si->green_size = 8;
  418. si->green_pos = 8;
  419. si->blue_size = 8;
  420. si->blue_pos = 0;
  421. si->rsvd_size = 8;
  422. si->rsvd_pos = 24;
  423. } else if (pixel_format == PIXEL_BIT_MASK) {
  424. find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
  425. find_bits(pixel_info.green_mask, &si->green_pos,
  426. &si->green_size);
  427. find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
  428. find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
  429. &si->rsvd_size);
  430. si->lfb_depth = si->red_size + si->green_size +
  431. si->blue_size + si->rsvd_size;
  432. si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
  433. } else {
  434. si->lfb_depth = 4;
  435. si->lfb_linelength = si->lfb_width / 2;
  436. si->red_size = 0;
  437. si->red_pos = 0;
  438. si->green_size = 0;
  439. si->green_pos = 0;
  440. si->blue_size = 0;
  441. si->blue_pos = 0;
  442. si->rsvd_size = 0;
  443. si->rsvd_pos = 0;
  444. }
  445. si->lfb_size = si->lfb_linelength * si->lfb_height;
  446. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  447. free_handle:
  448. efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
  449. return status;
  450. }
  451. /*
  452. * See if we have Universal Graphics Adapter (UGA) protocol
  453. */
  454. static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
  455. unsigned long size)
  456. {
  457. struct efi_uga_draw_protocol *uga, *first_uga;
  458. unsigned long nr_ugas;
  459. efi_status_t status;
  460. u32 width, height;
  461. void **uga_handle = NULL;
  462. int i;
  463. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  464. EFI_LOADER_DATA, size, &uga_handle);
  465. if (status != EFI_SUCCESS)
  466. return status;
  467. status = efi_call_phys5(sys_table->boottime->locate_handle,
  468. EFI_LOCATE_BY_PROTOCOL, uga_proto,
  469. NULL, &size, uga_handle);
  470. if (status != EFI_SUCCESS)
  471. goto free_handle;
  472. first_uga = NULL;
  473. nr_ugas = size / sizeof(void *);
  474. for (i = 0; i < nr_ugas; i++) {
  475. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  476. void *handle = uga_handle[i];
  477. u32 w, h, depth, refresh;
  478. void *pciio;
  479. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  480. handle, uga_proto, &uga);
  481. if (status != EFI_SUCCESS)
  482. continue;
  483. efi_call_phys3(sys_table->boottime->handle_protocol,
  484. handle, &pciio_proto, &pciio);
  485. status = efi_call_phys5(uga->get_mode, uga, &w, &h,
  486. &depth, &refresh);
  487. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  488. width = w;
  489. height = h;
  490. /*
  491. * Once we've found a UGA supporting PCIIO,
  492. * don't bother looking any further.
  493. */
  494. if (pciio)
  495. break;
  496. first_uga = uga;
  497. }
  498. }
  499. if (!first_uga)
  500. goto free_handle;
  501. /* EFI framebuffer */
  502. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  503. si->lfb_depth = 32;
  504. si->lfb_width = width;
  505. si->lfb_height = height;
  506. si->red_size = 8;
  507. si->red_pos = 16;
  508. si->green_size = 8;
  509. si->green_pos = 8;
  510. si->blue_size = 8;
  511. si->blue_pos = 0;
  512. si->rsvd_size = 8;
  513. si->rsvd_pos = 24;
  514. free_handle:
  515. efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
  516. return status;
  517. }
  518. void setup_graphics(struct boot_params *boot_params)
  519. {
  520. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  521. struct screen_info *si;
  522. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  523. efi_status_t status;
  524. unsigned long size;
  525. void **gop_handle = NULL;
  526. void **uga_handle = NULL;
  527. si = &boot_params->screen_info;
  528. memset(si, 0, sizeof(*si));
  529. size = 0;
  530. status = efi_call_phys5(sys_table->boottime->locate_handle,
  531. EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
  532. NULL, &size, gop_handle);
  533. if (status == EFI_BUFFER_TOO_SMALL)
  534. status = setup_gop(si, &graphics_proto, size);
  535. if (status != EFI_SUCCESS) {
  536. size = 0;
  537. status = efi_call_phys5(sys_table->boottime->locate_handle,
  538. EFI_LOCATE_BY_PROTOCOL, &uga_proto,
  539. NULL, &size, uga_handle);
  540. if (status == EFI_BUFFER_TOO_SMALL)
  541. setup_uga(si, &uga_proto, size);
  542. }
  543. }
  544. struct initrd {
  545. efi_file_handle_t *handle;
  546. u64 size;
  547. };
  548. /*
  549. * Check the cmdline for a LILO-style initrd= arguments.
  550. *
  551. * We only support loading an initrd from the same filesystem as the
  552. * kernel image.
  553. */
  554. static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
  555. struct setup_header *hdr)
  556. {
  557. struct initrd *initrds;
  558. unsigned long initrd_addr;
  559. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  560. u64 initrd_total;
  561. efi_file_io_interface_t *io;
  562. efi_file_handle_t *fh;
  563. efi_status_t status;
  564. int nr_initrds;
  565. char *str;
  566. int i, j, k;
  567. initrd_addr = 0;
  568. initrd_total = 0;
  569. str = (char *)(unsigned long)hdr->cmd_line_ptr;
  570. j = 0; /* See close_handles */
  571. if (!str || !*str)
  572. return EFI_SUCCESS;
  573. for (nr_initrds = 0; *str; nr_initrds++) {
  574. str = strstr(str, "initrd=");
  575. if (!str)
  576. break;
  577. str += 7;
  578. /* Skip any leading slashes */
  579. while (*str == '/' || *str == '\\')
  580. str++;
  581. while (*str && *str != ' ' && *str != '\n')
  582. str++;
  583. }
  584. if (!nr_initrds)
  585. return EFI_SUCCESS;
  586. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  587. EFI_LOADER_DATA,
  588. nr_initrds * sizeof(*initrds),
  589. &initrds);
  590. if (status != EFI_SUCCESS) {
  591. efi_printk("Failed to alloc mem for initrds\n");
  592. goto fail;
  593. }
  594. str = (char *)(unsigned long)hdr->cmd_line_ptr;
  595. for (i = 0; i < nr_initrds; i++) {
  596. struct initrd *initrd;
  597. efi_file_handle_t *h;
  598. efi_file_info_t *info;
  599. efi_char16_t filename_16[256];
  600. unsigned long info_sz;
  601. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  602. efi_char16_t *p;
  603. u64 file_sz;
  604. str = strstr(str, "initrd=");
  605. if (!str)
  606. break;
  607. str += 7;
  608. initrd = &initrds[i];
  609. p = filename_16;
  610. /* Skip any leading slashes */
  611. while (*str == '/' || *str == '\\')
  612. str++;
  613. while (*str && *str != ' ' && *str != '\n') {
  614. if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
  615. break;
  616. if (*str == '/') {
  617. *p++ = '\\';
  618. *str++;
  619. } else {
  620. *p++ = *str++;
  621. }
  622. }
  623. *p = '\0';
  624. /* Only open the volume once. */
  625. if (!i) {
  626. efi_boot_services_t *boottime;
  627. boottime = sys_table->boottime;
  628. status = efi_call_phys3(boottime->handle_protocol,
  629. image->device_handle, &fs_proto, &io);
  630. if (status != EFI_SUCCESS) {
  631. efi_printk("Failed to handle fs_proto\n");
  632. goto free_initrds;
  633. }
  634. status = efi_call_phys2(io->open_volume, io, &fh);
  635. if (status != EFI_SUCCESS) {
  636. efi_printk("Failed to open volume\n");
  637. goto free_initrds;
  638. }
  639. }
  640. status = efi_call_phys5(fh->open, fh, &h, filename_16,
  641. EFI_FILE_MODE_READ, (u64)0);
  642. if (status != EFI_SUCCESS) {
  643. efi_printk("Failed to open initrd file: ");
  644. efi_char16_printk(filename_16);
  645. efi_printk("\n");
  646. goto close_handles;
  647. }
  648. initrd->handle = h;
  649. info_sz = 0;
  650. status = efi_call_phys4(h->get_info, h, &info_guid,
  651. &info_sz, NULL);
  652. if (status != EFI_BUFFER_TOO_SMALL) {
  653. efi_printk("Failed to get initrd info size\n");
  654. goto close_handles;
  655. }
  656. grow:
  657. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  658. EFI_LOADER_DATA, info_sz, &info);
  659. if (status != EFI_SUCCESS) {
  660. efi_printk("Failed to alloc mem for initrd info\n");
  661. goto close_handles;
  662. }
  663. status = efi_call_phys4(h->get_info, h, &info_guid,
  664. &info_sz, info);
  665. if (status == EFI_BUFFER_TOO_SMALL) {
  666. efi_call_phys1(sys_table->boottime->free_pool, info);
  667. goto grow;
  668. }
  669. file_sz = info->file_size;
  670. efi_call_phys1(sys_table->boottime->free_pool, info);
  671. if (status != EFI_SUCCESS) {
  672. efi_printk("Failed to get initrd info\n");
  673. goto close_handles;
  674. }
  675. initrd->size = file_sz;
  676. initrd_total += file_sz;
  677. }
  678. if (initrd_total) {
  679. unsigned long addr;
  680. /*
  681. * Multiple initrd's need to be at consecutive
  682. * addresses in memory, so allocate enough memory for
  683. * all the initrd's.
  684. */
  685. status = high_alloc(initrd_total, 0x1000,
  686. &initrd_addr, hdr->initrd_addr_max);
  687. if (status != EFI_SUCCESS) {
  688. efi_printk("Failed to alloc highmem for initrds\n");
  689. goto close_handles;
  690. }
  691. /* We've run out of free low memory. */
  692. if (initrd_addr > hdr->initrd_addr_max) {
  693. efi_printk("We've run out of free low memory\n");
  694. status = EFI_INVALID_PARAMETER;
  695. goto free_initrd_total;
  696. }
  697. addr = initrd_addr;
  698. for (j = 0; j < nr_initrds; j++) {
  699. u64 size;
  700. size = initrds[j].size;
  701. while (size) {
  702. u64 chunksize;
  703. if (size > EFI_READ_CHUNK_SIZE)
  704. chunksize = EFI_READ_CHUNK_SIZE;
  705. else
  706. chunksize = size;
  707. status = efi_call_phys3(fh->read,
  708. initrds[j].handle,
  709. &chunksize, addr);
  710. if (status != EFI_SUCCESS) {
  711. efi_printk("Failed to read initrd\n");
  712. goto free_initrd_total;
  713. }
  714. addr += chunksize;
  715. size -= chunksize;
  716. }
  717. efi_call_phys1(fh->close, initrds[j].handle);
  718. }
  719. }
  720. efi_call_phys1(sys_table->boottime->free_pool, initrds);
  721. hdr->ramdisk_image = initrd_addr;
  722. hdr->ramdisk_size = initrd_total;
  723. return status;
  724. free_initrd_total:
  725. low_free(initrd_total, initrd_addr);
  726. close_handles:
  727. for (k = j; k < i; k++)
  728. efi_call_phys1(fh->close, initrds[k].handle);
  729. free_initrds:
  730. efi_call_phys1(sys_table->boottime->free_pool, initrds);
  731. fail:
  732. hdr->ramdisk_image = 0;
  733. hdr->ramdisk_size = 0;
  734. return status;
  735. }
  736. /*
  737. * Because the x86 boot code expects to be passed a boot_params we
  738. * need to create one ourselves (usually the bootloader would create
  739. * one for us).
  740. */
  741. struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
  742. {
  743. struct boot_params *boot_params;
  744. struct sys_desc_table *sdt;
  745. struct apm_bios_info *bi;
  746. struct setup_header *hdr;
  747. struct efi_info *efi;
  748. efi_loaded_image_t *image;
  749. void *options;
  750. u32 load_options_size;
  751. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  752. int options_size = 0;
  753. efi_status_t status;
  754. unsigned long cmdline;
  755. u16 *s2;
  756. u8 *s1;
  757. int i;
  758. sys_table = _table;
  759. /* Check if we were booted by the EFI firmware */
  760. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  761. return NULL;
  762. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  763. handle, &proto, (void *)&image);
  764. if (status != EFI_SUCCESS) {
  765. efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  766. return NULL;
  767. }
  768. status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
  769. if (status != EFI_SUCCESS) {
  770. efi_printk("Failed to alloc lowmem for boot params\n");
  771. return NULL;
  772. }
  773. memset(boot_params, 0x0, 0x4000);
  774. hdr = &boot_params->hdr;
  775. efi = &boot_params->efi_info;
  776. bi = &boot_params->apm_bios_info;
  777. sdt = &boot_params->sys_desc_table;
  778. /* Copy the second sector to boot_params */
  779. memcpy(&hdr->jump, image->image_base + 512, 512);
  780. /*
  781. * Fill out some of the header fields ourselves because the
  782. * EFI firmware loader doesn't load the first sector.
  783. */
  784. hdr->root_flags = 1;
  785. hdr->vid_mode = 0xffff;
  786. hdr->boot_flag = 0xAA55;
  787. hdr->code32_start = (__u64)(unsigned long)image->image_base;
  788. hdr->type_of_loader = 0x21;
  789. /* Convert unicode cmdline to ascii */
  790. options = image->load_options;
  791. load_options_size = image->load_options_size / 2; /* ASCII */
  792. cmdline = 0;
  793. s2 = (u16 *)options;
  794. if (s2) {
  795. while (*s2 && *s2 != '\n' && options_size < load_options_size) {
  796. s2++;
  797. options_size++;
  798. }
  799. if (options_size) {
  800. if (options_size > hdr->cmdline_size)
  801. options_size = hdr->cmdline_size;
  802. options_size++; /* NUL termination */
  803. status = low_alloc(options_size, 1, &cmdline);
  804. if (status != EFI_SUCCESS) {
  805. efi_printk("Failed to alloc mem for cmdline\n");
  806. goto fail;
  807. }
  808. s1 = (u8 *)(unsigned long)cmdline;
  809. s2 = (u16 *)options;
  810. for (i = 0; i < options_size - 1; i++)
  811. *s1++ = *s2++;
  812. *s1 = '\0';
  813. }
  814. }
  815. hdr->cmd_line_ptr = cmdline;
  816. hdr->ramdisk_image = 0;
  817. hdr->ramdisk_size = 0;
  818. /* Clear APM BIOS info */
  819. memset(bi, 0, sizeof(*bi));
  820. memset(sdt, 0, sizeof(*sdt));
  821. status = handle_ramdisks(image, hdr);
  822. if (status != EFI_SUCCESS)
  823. goto fail2;
  824. return boot_params;
  825. fail2:
  826. if (options_size)
  827. low_free(options_size, hdr->cmd_line_ptr);
  828. fail:
  829. low_free(0x4000, (unsigned long)boot_params);
  830. return NULL;
  831. }
  832. static efi_status_t exit_boot(struct boot_params *boot_params,
  833. void *handle)
  834. {
  835. struct efi_info *efi = &boot_params->efi_info;
  836. struct e820entry *e820_map = &boot_params->e820_map[0];
  837. struct e820entry *prev = NULL;
  838. unsigned long size, key, desc_size, _size;
  839. efi_memory_desc_t *mem_map;
  840. efi_status_t status;
  841. __u32 desc_version;
  842. u8 nr_entries;
  843. int i;
  844. size = sizeof(*mem_map) * 32;
  845. again:
  846. size += sizeof(*mem_map);
  847. _size = size;
  848. status = low_alloc(size, 1, (unsigned long *)&mem_map);
  849. if (status != EFI_SUCCESS)
  850. return status;
  851. status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
  852. mem_map, &key, &desc_size, &desc_version);
  853. if (status == EFI_BUFFER_TOO_SMALL) {
  854. low_free(_size, (unsigned long)mem_map);
  855. goto again;
  856. }
  857. if (status != EFI_SUCCESS)
  858. goto free_mem_map;
  859. memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
  860. efi->efi_systab = (unsigned long)sys_table;
  861. efi->efi_memdesc_size = desc_size;
  862. efi->efi_memdesc_version = desc_version;
  863. efi->efi_memmap = (unsigned long)mem_map;
  864. efi->efi_memmap_size = size;
  865. #ifdef CONFIG_X86_64
  866. efi->efi_systab_hi = (unsigned long)sys_table >> 32;
  867. efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
  868. #endif
  869. /* Might as well exit boot services now */
  870. status = efi_call_phys2(sys_table->boottime->exit_boot_services,
  871. handle, key);
  872. if (status != EFI_SUCCESS)
  873. goto free_mem_map;
  874. /* Historic? */
  875. boot_params->alt_mem_k = 32 * 1024;
  876. /*
  877. * Convert the EFI memory map to E820.
  878. */
  879. nr_entries = 0;
  880. for (i = 0; i < size / desc_size; i++) {
  881. efi_memory_desc_t *d;
  882. unsigned int e820_type = 0;
  883. unsigned long m = (unsigned long)mem_map;
  884. d = (efi_memory_desc_t *)(m + (i * desc_size));
  885. switch (d->type) {
  886. case EFI_RESERVED_TYPE:
  887. case EFI_RUNTIME_SERVICES_CODE:
  888. case EFI_RUNTIME_SERVICES_DATA:
  889. case EFI_MEMORY_MAPPED_IO:
  890. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  891. case EFI_PAL_CODE:
  892. e820_type = E820_RESERVED;
  893. break;
  894. case EFI_UNUSABLE_MEMORY:
  895. e820_type = E820_UNUSABLE;
  896. break;
  897. case EFI_ACPI_RECLAIM_MEMORY:
  898. e820_type = E820_ACPI;
  899. break;
  900. case EFI_LOADER_CODE:
  901. case EFI_LOADER_DATA:
  902. case EFI_BOOT_SERVICES_CODE:
  903. case EFI_BOOT_SERVICES_DATA:
  904. case EFI_CONVENTIONAL_MEMORY:
  905. e820_type = E820_RAM;
  906. break;
  907. case EFI_ACPI_MEMORY_NVS:
  908. e820_type = E820_NVS;
  909. break;
  910. default:
  911. continue;
  912. }
  913. /* Merge adjacent mappings */
  914. if (prev && prev->type == e820_type &&
  915. (prev->addr + prev->size) == d->phys_addr)
  916. prev->size += d->num_pages << 12;
  917. else {
  918. e820_map->addr = d->phys_addr;
  919. e820_map->size = d->num_pages << 12;
  920. e820_map->type = e820_type;
  921. prev = e820_map++;
  922. nr_entries++;
  923. }
  924. }
  925. boot_params->e820_entries = nr_entries;
  926. return EFI_SUCCESS;
  927. free_mem_map:
  928. low_free(_size, (unsigned long)mem_map);
  929. return status;
  930. }
  931. static efi_status_t relocate_kernel(struct setup_header *hdr)
  932. {
  933. unsigned long start, nr_pages;
  934. efi_status_t status;
  935. /*
  936. * The EFI firmware loader could have placed the kernel image
  937. * anywhere in memory, but the kernel has various restrictions
  938. * on the max physical address it can run at. Attempt to move
  939. * the kernel to boot_params.pref_address, or as low as
  940. * possible.
  941. */
  942. start = hdr->pref_address;
  943. nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  944. status = efi_call_phys4(sys_table->boottime->allocate_pages,
  945. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  946. nr_pages, &start);
  947. if (status != EFI_SUCCESS) {
  948. status = low_alloc(hdr->init_size, hdr->kernel_alignment,
  949. &start);
  950. if (status != EFI_SUCCESS)
  951. efi_printk("Failed to alloc mem for kernel\n");
  952. }
  953. if (status == EFI_SUCCESS)
  954. memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
  955. hdr->init_size);
  956. hdr->pref_address = hdr->code32_start;
  957. hdr->code32_start = (__u32)start;
  958. return status;
  959. }
  960. /*
  961. * On success we return a pointer to a boot_params structure, and NULL
  962. * on failure.
  963. */
  964. struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
  965. struct boot_params *boot_params)
  966. {
  967. struct desc_ptr *gdt, *idt;
  968. efi_loaded_image_t *image;
  969. struct setup_header *hdr = &boot_params->hdr;
  970. efi_status_t status;
  971. struct desc_struct *desc;
  972. sys_table = _table;
  973. /* Check if we were booted by the EFI firmware */
  974. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  975. goto fail;
  976. setup_graphics(boot_params);
  977. setup_efi_vars(boot_params);
  978. setup_efi_pci(boot_params);
  979. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  980. EFI_LOADER_DATA, sizeof(*gdt),
  981. (void **)&gdt);
  982. if (status != EFI_SUCCESS) {
  983. efi_printk("Failed to alloc mem for gdt structure\n");
  984. goto fail;
  985. }
  986. gdt->size = 0x800;
  987. status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
  988. if (status != EFI_SUCCESS) {
  989. efi_printk("Failed to alloc mem for gdt\n");
  990. goto fail;
  991. }
  992. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  993. EFI_LOADER_DATA, sizeof(*idt),
  994. (void **)&idt);
  995. if (status != EFI_SUCCESS) {
  996. efi_printk("Failed to alloc mem for idt structure\n");
  997. goto fail;
  998. }
  999. idt->size = 0;
  1000. idt->address = 0;
  1001. /*
  1002. * If the kernel isn't already loaded at the preferred load
  1003. * address, relocate it.
  1004. */
  1005. if (hdr->pref_address != hdr->code32_start) {
  1006. status = relocate_kernel(hdr);
  1007. if (status != EFI_SUCCESS)
  1008. goto fail;
  1009. }
  1010. status = exit_boot(boot_params, handle);
  1011. if (status != EFI_SUCCESS)
  1012. goto fail;
  1013. memset((char *)gdt->address, 0x0, gdt->size);
  1014. desc = (struct desc_struct *)gdt->address;
  1015. /* The first GDT is a dummy and the second is unused. */
  1016. desc += 2;
  1017. desc->limit0 = 0xffff;
  1018. desc->base0 = 0x0000;
  1019. desc->base1 = 0x0000;
  1020. desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
  1021. desc->s = DESC_TYPE_CODE_DATA;
  1022. desc->dpl = 0;
  1023. desc->p = 1;
  1024. desc->limit = 0xf;
  1025. desc->avl = 0;
  1026. desc->l = 0;
  1027. desc->d = SEG_OP_SIZE_32BIT;
  1028. desc->g = SEG_GRANULARITY_4KB;
  1029. desc->base2 = 0x00;
  1030. desc++;
  1031. desc->limit0 = 0xffff;
  1032. desc->base0 = 0x0000;
  1033. desc->base1 = 0x0000;
  1034. desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
  1035. desc->s = DESC_TYPE_CODE_DATA;
  1036. desc->dpl = 0;
  1037. desc->p = 1;
  1038. desc->limit = 0xf;
  1039. desc->avl = 0;
  1040. desc->l = 0;
  1041. desc->d = SEG_OP_SIZE_32BIT;
  1042. desc->g = SEG_GRANULARITY_4KB;
  1043. desc->base2 = 0x00;
  1044. #ifdef CONFIG_X86_64
  1045. /* Task segment value */
  1046. desc++;
  1047. desc->limit0 = 0x0000;
  1048. desc->base0 = 0x0000;
  1049. desc->base1 = 0x0000;
  1050. desc->type = SEG_TYPE_TSS;
  1051. desc->s = 0;
  1052. desc->dpl = 0;
  1053. desc->p = 1;
  1054. desc->limit = 0x0;
  1055. desc->avl = 0;
  1056. desc->l = 0;
  1057. desc->d = 0;
  1058. desc->g = SEG_GRANULARITY_4KB;
  1059. desc->base2 = 0x00;
  1060. #endif /* CONFIG_X86_64 */
  1061. asm volatile ("lidt %0" : : "m" (*idt));
  1062. asm volatile ("lgdt %0" : : "m" (*gdt));
  1063. asm volatile("cli");
  1064. return boot_params;
  1065. fail:
  1066. return NULL;
  1067. }