eboot.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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_pci(struct boot_params *params)
  204. {
  205. efi_pci_io_protocol *pci;
  206. efi_status_t status;
  207. void **pci_handle;
  208. efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
  209. unsigned long nr_pci, size = 0;
  210. int i;
  211. struct setup_data *data;
  212. data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
  213. while (data && data->next)
  214. data = (struct setup_data *)(unsigned long)data->next;
  215. status = efi_call_phys5(sys_table->boottime->locate_handle,
  216. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  217. NULL, &size, pci_handle);
  218. if (status == EFI_BUFFER_TOO_SMALL) {
  219. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  220. EFI_LOADER_DATA, size, &pci_handle);
  221. if (status != EFI_SUCCESS)
  222. return status;
  223. status = efi_call_phys5(sys_table->boottime->locate_handle,
  224. EFI_LOCATE_BY_PROTOCOL, &pci_proto,
  225. NULL, &size, pci_handle);
  226. }
  227. if (status != EFI_SUCCESS)
  228. goto free_handle;
  229. nr_pci = size / sizeof(void *);
  230. for (i = 0; i < nr_pci; i++) {
  231. void *h = pci_handle[i];
  232. uint64_t attributes;
  233. struct pci_setup_rom *rom;
  234. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  235. h, &pci_proto, &pci);
  236. if (status != EFI_SUCCESS)
  237. continue;
  238. if (!pci)
  239. continue;
  240. #ifdef CONFIG_X86_64
  241. status = efi_call_phys4(pci->attributes, pci,
  242. EfiPciIoAttributeOperationGet, 0,
  243. &attributes);
  244. #else
  245. status = efi_call_phys5(pci->attributes, pci,
  246. EfiPciIoAttributeOperationGet, 0, 0,
  247. &attributes);
  248. #endif
  249. if (status != EFI_SUCCESS)
  250. continue;
  251. if (!pci->romimage || !pci->romsize)
  252. continue;
  253. size = pci->romsize + sizeof(*rom);
  254. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  255. EFI_LOADER_DATA, size, &rom);
  256. if (status != EFI_SUCCESS)
  257. continue;
  258. rom->data.type = SETUP_PCI;
  259. rom->data.len = size - sizeof(struct setup_data);
  260. rom->data.next = 0;
  261. rom->pcilen = pci->romsize;
  262. status = efi_call_phys5(pci->pci.read, pci,
  263. EfiPciIoWidthUint16, PCI_VENDOR_ID,
  264. 1, &(rom->vendor));
  265. if (status != EFI_SUCCESS)
  266. goto free_struct;
  267. status = efi_call_phys5(pci->pci.read, pci,
  268. EfiPciIoWidthUint16, PCI_DEVICE_ID,
  269. 1, &(rom->devid));
  270. if (status != EFI_SUCCESS)
  271. goto free_struct;
  272. status = efi_call_phys5(pci->get_location, pci,
  273. &(rom->segment), &(rom->bus),
  274. &(rom->device), &(rom->function));
  275. if (status != EFI_SUCCESS)
  276. goto free_struct;
  277. memcpy(rom->romdata, pci->romimage, pci->romsize);
  278. if (data)
  279. data->next = (unsigned long)rom;
  280. else
  281. params->hdr.setup_data = (unsigned long)rom;
  282. data = (struct setup_data *)rom;
  283. continue;
  284. free_struct:
  285. efi_call_phys1(sys_table->boottime->free_pool, rom);
  286. }
  287. free_handle:
  288. efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
  289. return status;
  290. }
  291. /*
  292. * See if we have Graphics Output Protocol
  293. */
  294. static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
  295. unsigned long size)
  296. {
  297. struct efi_graphics_output_protocol *gop, *first_gop;
  298. struct efi_pixel_bitmask pixel_info;
  299. unsigned long nr_gops;
  300. efi_status_t status;
  301. void **gop_handle;
  302. u16 width, height;
  303. u32 fb_base, fb_size;
  304. u32 pixels_per_scan_line;
  305. int pixel_format;
  306. int i;
  307. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  308. EFI_LOADER_DATA, size, &gop_handle);
  309. if (status != EFI_SUCCESS)
  310. return status;
  311. status = efi_call_phys5(sys_table->boottime->locate_handle,
  312. EFI_LOCATE_BY_PROTOCOL, proto,
  313. NULL, &size, gop_handle);
  314. if (status != EFI_SUCCESS)
  315. goto free_handle;
  316. first_gop = NULL;
  317. nr_gops = size / sizeof(void *);
  318. for (i = 0; i < nr_gops; i++) {
  319. struct efi_graphics_output_mode_info *info;
  320. efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
  321. bool conout_found = false;
  322. void *dummy;
  323. void *h = gop_handle[i];
  324. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  325. h, proto, &gop);
  326. if (status != EFI_SUCCESS)
  327. continue;
  328. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  329. h, &conout_proto, &dummy);
  330. if (status == EFI_SUCCESS)
  331. conout_found = true;
  332. status = efi_call_phys4(gop->query_mode, gop,
  333. gop->mode->mode, &size, &info);
  334. if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
  335. /*
  336. * Systems that use the UEFI Console Splitter may
  337. * provide multiple GOP devices, not all of which are
  338. * backed by real hardware. The workaround is to search
  339. * for a GOP implementing the ConOut protocol, and if
  340. * one isn't found, to just fall back to the first GOP.
  341. */
  342. width = info->horizontal_resolution;
  343. height = info->vertical_resolution;
  344. fb_base = gop->mode->frame_buffer_base;
  345. fb_size = gop->mode->frame_buffer_size;
  346. pixel_format = info->pixel_format;
  347. pixel_info = info->pixel_information;
  348. pixels_per_scan_line = info->pixels_per_scan_line;
  349. /*
  350. * Once we've found a GOP supporting ConOut,
  351. * don't bother looking any further.
  352. */
  353. first_gop = gop;
  354. if (conout_found)
  355. break;
  356. }
  357. }
  358. /* Did we find any GOPs? */
  359. if (!first_gop)
  360. goto free_handle;
  361. /* EFI framebuffer */
  362. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  363. si->lfb_width = width;
  364. si->lfb_height = height;
  365. si->lfb_base = fb_base;
  366. si->pages = 1;
  367. if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
  368. si->lfb_depth = 32;
  369. si->lfb_linelength = pixels_per_scan_line * 4;
  370. si->red_size = 8;
  371. si->red_pos = 0;
  372. si->green_size = 8;
  373. si->green_pos = 8;
  374. si->blue_size = 8;
  375. si->blue_pos = 16;
  376. si->rsvd_size = 8;
  377. si->rsvd_pos = 24;
  378. } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
  379. si->lfb_depth = 32;
  380. si->lfb_linelength = pixels_per_scan_line * 4;
  381. si->red_size = 8;
  382. si->red_pos = 16;
  383. si->green_size = 8;
  384. si->green_pos = 8;
  385. si->blue_size = 8;
  386. si->blue_pos = 0;
  387. si->rsvd_size = 8;
  388. si->rsvd_pos = 24;
  389. } else if (pixel_format == PIXEL_BIT_MASK) {
  390. find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
  391. find_bits(pixel_info.green_mask, &si->green_pos,
  392. &si->green_size);
  393. find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
  394. find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
  395. &si->rsvd_size);
  396. si->lfb_depth = si->red_size + si->green_size +
  397. si->blue_size + si->rsvd_size;
  398. si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
  399. } else {
  400. si->lfb_depth = 4;
  401. si->lfb_linelength = si->lfb_width / 2;
  402. si->red_size = 0;
  403. si->red_pos = 0;
  404. si->green_size = 0;
  405. si->green_pos = 0;
  406. si->blue_size = 0;
  407. si->blue_pos = 0;
  408. si->rsvd_size = 0;
  409. si->rsvd_pos = 0;
  410. }
  411. si->lfb_size = si->lfb_linelength * si->lfb_height;
  412. si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
  413. free_handle:
  414. efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
  415. return status;
  416. }
  417. /*
  418. * See if we have Universal Graphics Adapter (UGA) protocol
  419. */
  420. static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
  421. unsigned long size)
  422. {
  423. struct efi_uga_draw_protocol *uga, *first_uga;
  424. unsigned long nr_ugas;
  425. efi_status_t status;
  426. u32 width, height;
  427. void **uga_handle = NULL;
  428. int i;
  429. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  430. EFI_LOADER_DATA, size, &uga_handle);
  431. if (status != EFI_SUCCESS)
  432. return status;
  433. status = efi_call_phys5(sys_table->boottime->locate_handle,
  434. EFI_LOCATE_BY_PROTOCOL, uga_proto,
  435. NULL, &size, uga_handle);
  436. if (status != EFI_SUCCESS)
  437. goto free_handle;
  438. first_uga = NULL;
  439. nr_ugas = size / sizeof(void *);
  440. for (i = 0; i < nr_ugas; i++) {
  441. efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
  442. void *handle = uga_handle[i];
  443. u32 w, h, depth, refresh;
  444. void *pciio;
  445. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  446. handle, uga_proto, &uga);
  447. if (status != EFI_SUCCESS)
  448. continue;
  449. efi_call_phys3(sys_table->boottime->handle_protocol,
  450. handle, &pciio_proto, &pciio);
  451. status = efi_call_phys5(uga->get_mode, uga, &w, &h,
  452. &depth, &refresh);
  453. if (status == EFI_SUCCESS && (!first_uga || pciio)) {
  454. width = w;
  455. height = h;
  456. /*
  457. * Once we've found a UGA supporting PCIIO,
  458. * don't bother looking any further.
  459. */
  460. if (pciio)
  461. break;
  462. first_uga = uga;
  463. }
  464. }
  465. if (!first_uga)
  466. goto free_handle;
  467. /* EFI framebuffer */
  468. si->orig_video_isVGA = VIDEO_TYPE_EFI;
  469. si->lfb_depth = 32;
  470. si->lfb_width = width;
  471. si->lfb_height = height;
  472. si->red_size = 8;
  473. si->red_pos = 16;
  474. si->green_size = 8;
  475. si->green_pos = 8;
  476. si->blue_size = 8;
  477. si->blue_pos = 0;
  478. si->rsvd_size = 8;
  479. si->rsvd_pos = 24;
  480. free_handle:
  481. efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
  482. return status;
  483. }
  484. void setup_graphics(struct boot_params *boot_params)
  485. {
  486. efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  487. struct screen_info *si;
  488. efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
  489. efi_status_t status;
  490. unsigned long size;
  491. void **gop_handle = NULL;
  492. void **uga_handle = NULL;
  493. si = &boot_params->screen_info;
  494. memset(si, 0, sizeof(*si));
  495. size = 0;
  496. status = efi_call_phys5(sys_table->boottime->locate_handle,
  497. EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
  498. NULL, &size, gop_handle);
  499. if (status == EFI_BUFFER_TOO_SMALL)
  500. status = setup_gop(si, &graphics_proto, size);
  501. if (status != EFI_SUCCESS) {
  502. size = 0;
  503. status = efi_call_phys5(sys_table->boottime->locate_handle,
  504. EFI_LOCATE_BY_PROTOCOL, &uga_proto,
  505. NULL, &size, uga_handle);
  506. if (status == EFI_BUFFER_TOO_SMALL)
  507. setup_uga(si, &uga_proto, size);
  508. }
  509. }
  510. struct initrd {
  511. efi_file_handle_t *handle;
  512. u64 size;
  513. };
  514. /*
  515. * Check the cmdline for a LILO-style initrd= arguments.
  516. *
  517. * We only support loading an initrd from the same filesystem as the
  518. * kernel image.
  519. */
  520. static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
  521. struct setup_header *hdr)
  522. {
  523. struct initrd *initrds;
  524. unsigned long initrd_addr;
  525. efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
  526. u64 initrd_total;
  527. efi_file_io_interface_t *io;
  528. efi_file_handle_t *fh;
  529. efi_status_t status;
  530. int nr_initrds;
  531. char *str;
  532. int i, j, k;
  533. initrd_addr = 0;
  534. initrd_total = 0;
  535. str = (char *)(unsigned long)hdr->cmd_line_ptr;
  536. j = 0; /* See close_handles */
  537. if (!str || !*str)
  538. return EFI_SUCCESS;
  539. for (nr_initrds = 0; *str; nr_initrds++) {
  540. str = strstr(str, "initrd=");
  541. if (!str)
  542. break;
  543. str += 7;
  544. /* Skip any leading slashes */
  545. while (*str == '/' || *str == '\\')
  546. str++;
  547. while (*str && *str != ' ' && *str != '\n')
  548. str++;
  549. }
  550. if (!nr_initrds)
  551. return EFI_SUCCESS;
  552. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  553. EFI_LOADER_DATA,
  554. nr_initrds * sizeof(*initrds),
  555. &initrds);
  556. if (status != EFI_SUCCESS) {
  557. efi_printk("Failed to alloc mem for initrds\n");
  558. goto fail;
  559. }
  560. str = (char *)(unsigned long)hdr->cmd_line_ptr;
  561. for (i = 0; i < nr_initrds; i++) {
  562. struct initrd *initrd;
  563. efi_file_handle_t *h;
  564. efi_file_info_t *info;
  565. efi_char16_t filename_16[256];
  566. unsigned long info_sz;
  567. efi_guid_t info_guid = EFI_FILE_INFO_ID;
  568. efi_char16_t *p;
  569. u64 file_sz;
  570. str = strstr(str, "initrd=");
  571. if (!str)
  572. break;
  573. str += 7;
  574. initrd = &initrds[i];
  575. p = filename_16;
  576. /* Skip any leading slashes */
  577. while (*str == '/' || *str == '\\')
  578. str++;
  579. while (*str && *str != ' ' && *str != '\n') {
  580. if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
  581. break;
  582. if (*str == '/') {
  583. *p++ = '\\';
  584. *str++;
  585. } else {
  586. *p++ = *str++;
  587. }
  588. }
  589. *p = '\0';
  590. /* Only open the volume once. */
  591. if (!i) {
  592. efi_boot_services_t *boottime;
  593. boottime = sys_table->boottime;
  594. status = efi_call_phys3(boottime->handle_protocol,
  595. image->device_handle, &fs_proto, &io);
  596. if (status != EFI_SUCCESS) {
  597. efi_printk("Failed to handle fs_proto\n");
  598. goto free_initrds;
  599. }
  600. status = efi_call_phys2(io->open_volume, io, &fh);
  601. if (status != EFI_SUCCESS) {
  602. efi_printk("Failed to open volume\n");
  603. goto free_initrds;
  604. }
  605. }
  606. status = efi_call_phys5(fh->open, fh, &h, filename_16,
  607. EFI_FILE_MODE_READ, (u64)0);
  608. if (status != EFI_SUCCESS) {
  609. efi_printk("Failed to open initrd file: ");
  610. efi_char16_printk(filename_16);
  611. efi_printk("\n");
  612. goto close_handles;
  613. }
  614. initrd->handle = h;
  615. info_sz = 0;
  616. status = efi_call_phys4(h->get_info, h, &info_guid,
  617. &info_sz, NULL);
  618. if (status != EFI_BUFFER_TOO_SMALL) {
  619. efi_printk("Failed to get initrd info size\n");
  620. goto close_handles;
  621. }
  622. grow:
  623. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  624. EFI_LOADER_DATA, info_sz, &info);
  625. if (status != EFI_SUCCESS) {
  626. efi_printk("Failed to alloc mem for initrd info\n");
  627. goto close_handles;
  628. }
  629. status = efi_call_phys4(h->get_info, h, &info_guid,
  630. &info_sz, info);
  631. if (status == EFI_BUFFER_TOO_SMALL) {
  632. efi_call_phys1(sys_table->boottime->free_pool, info);
  633. goto grow;
  634. }
  635. file_sz = info->file_size;
  636. efi_call_phys1(sys_table->boottime->free_pool, info);
  637. if (status != EFI_SUCCESS) {
  638. efi_printk("Failed to get initrd info\n");
  639. goto close_handles;
  640. }
  641. initrd->size = file_sz;
  642. initrd_total += file_sz;
  643. }
  644. if (initrd_total) {
  645. unsigned long addr;
  646. /*
  647. * Multiple initrd's need to be at consecutive
  648. * addresses in memory, so allocate enough memory for
  649. * all the initrd's.
  650. */
  651. status = high_alloc(initrd_total, 0x1000,
  652. &initrd_addr, hdr->initrd_addr_max);
  653. if (status != EFI_SUCCESS) {
  654. efi_printk("Failed to alloc highmem for initrds\n");
  655. goto close_handles;
  656. }
  657. /* We've run out of free low memory. */
  658. if (initrd_addr > hdr->initrd_addr_max) {
  659. efi_printk("We've run out of free low memory\n");
  660. status = EFI_INVALID_PARAMETER;
  661. goto free_initrd_total;
  662. }
  663. addr = initrd_addr;
  664. for (j = 0; j < nr_initrds; j++) {
  665. u64 size;
  666. size = initrds[j].size;
  667. while (size) {
  668. u64 chunksize;
  669. if (size > EFI_READ_CHUNK_SIZE)
  670. chunksize = EFI_READ_CHUNK_SIZE;
  671. else
  672. chunksize = size;
  673. status = efi_call_phys3(fh->read,
  674. initrds[j].handle,
  675. &chunksize, addr);
  676. if (status != EFI_SUCCESS) {
  677. efi_printk("Failed to read initrd\n");
  678. goto free_initrd_total;
  679. }
  680. addr += chunksize;
  681. size -= chunksize;
  682. }
  683. efi_call_phys1(fh->close, initrds[j].handle);
  684. }
  685. }
  686. efi_call_phys1(sys_table->boottime->free_pool, initrds);
  687. hdr->ramdisk_image = initrd_addr;
  688. hdr->ramdisk_size = initrd_total;
  689. return status;
  690. free_initrd_total:
  691. low_free(initrd_total, initrd_addr);
  692. close_handles:
  693. for (k = j; k < i; k++)
  694. efi_call_phys1(fh->close, initrds[k].handle);
  695. free_initrds:
  696. efi_call_phys1(sys_table->boottime->free_pool, initrds);
  697. fail:
  698. hdr->ramdisk_image = 0;
  699. hdr->ramdisk_size = 0;
  700. return status;
  701. }
  702. /*
  703. * Because the x86 boot code expects to be passed a boot_params we
  704. * need to create one ourselves (usually the bootloader would create
  705. * one for us).
  706. */
  707. struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
  708. {
  709. struct boot_params *boot_params;
  710. struct sys_desc_table *sdt;
  711. struct apm_bios_info *bi;
  712. struct setup_header *hdr;
  713. struct efi_info *efi;
  714. efi_loaded_image_t *image;
  715. void *options;
  716. u32 load_options_size;
  717. efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
  718. int options_size = 0;
  719. efi_status_t status;
  720. unsigned long cmdline;
  721. u16 *s2;
  722. u8 *s1;
  723. int i;
  724. sys_table = _table;
  725. /* Check if we were booted by the EFI firmware */
  726. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  727. return NULL;
  728. status = efi_call_phys3(sys_table->boottime->handle_protocol,
  729. handle, &proto, (void *)&image);
  730. if (status != EFI_SUCCESS) {
  731. efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
  732. return NULL;
  733. }
  734. status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
  735. if (status != EFI_SUCCESS) {
  736. efi_printk("Failed to alloc lowmem for boot params\n");
  737. return NULL;
  738. }
  739. memset(boot_params, 0x0, 0x4000);
  740. hdr = &boot_params->hdr;
  741. efi = &boot_params->efi_info;
  742. bi = &boot_params->apm_bios_info;
  743. sdt = &boot_params->sys_desc_table;
  744. /* Copy the second sector to boot_params */
  745. memcpy(&hdr->jump, image->image_base + 512, 512);
  746. /*
  747. * Fill out some of the header fields ourselves because the
  748. * EFI firmware loader doesn't load the first sector.
  749. */
  750. hdr->root_flags = 1;
  751. hdr->vid_mode = 0xffff;
  752. hdr->boot_flag = 0xAA55;
  753. hdr->code32_start = (__u64)(unsigned long)image->image_base;
  754. hdr->type_of_loader = 0x21;
  755. /* Convert unicode cmdline to ascii */
  756. options = image->load_options;
  757. load_options_size = image->load_options_size / 2; /* ASCII */
  758. cmdline = 0;
  759. s2 = (u16 *)options;
  760. if (s2) {
  761. while (*s2 && *s2 != '\n' && options_size < load_options_size) {
  762. s2++;
  763. options_size++;
  764. }
  765. if (options_size) {
  766. if (options_size > hdr->cmdline_size)
  767. options_size = hdr->cmdline_size;
  768. options_size++; /* NUL termination */
  769. status = low_alloc(options_size, 1, &cmdline);
  770. if (status != EFI_SUCCESS) {
  771. efi_printk("Failed to alloc mem for cmdline\n");
  772. goto fail;
  773. }
  774. s1 = (u8 *)(unsigned long)cmdline;
  775. s2 = (u16 *)options;
  776. for (i = 0; i < options_size - 1; i++)
  777. *s1++ = *s2++;
  778. *s1 = '\0';
  779. }
  780. }
  781. hdr->cmd_line_ptr = cmdline;
  782. hdr->ramdisk_image = 0;
  783. hdr->ramdisk_size = 0;
  784. /* Clear APM BIOS info */
  785. memset(bi, 0, sizeof(*bi));
  786. memset(sdt, 0, sizeof(*sdt));
  787. status = handle_ramdisks(image, hdr);
  788. if (status != EFI_SUCCESS)
  789. goto fail2;
  790. return boot_params;
  791. fail2:
  792. if (options_size)
  793. low_free(options_size, hdr->cmd_line_ptr);
  794. fail:
  795. low_free(0x4000, (unsigned long)boot_params);
  796. return NULL;
  797. }
  798. static efi_status_t exit_boot(struct boot_params *boot_params,
  799. void *handle)
  800. {
  801. struct efi_info *efi = &boot_params->efi_info;
  802. struct e820entry *e820_map = &boot_params->e820_map[0];
  803. struct e820entry *prev = NULL;
  804. unsigned long size, key, desc_size, _size;
  805. efi_memory_desc_t *mem_map;
  806. efi_status_t status;
  807. __u32 desc_version;
  808. u8 nr_entries;
  809. int i;
  810. size = sizeof(*mem_map) * 32;
  811. again:
  812. size += sizeof(*mem_map);
  813. _size = size;
  814. status = low_alloc(size, 1, (unsigned long *)&mem_map);
  815. if (status != EFI_SUCCESS)
  816. return status;
  817. status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
  818. mem_map, &key, &desc_size, &desc_version);
  819. if (status == EFI_BUFFER_TOO_SMALL) {
  820. low_free(_size, (unsigned long)mem_map);
  821. goto again;
  822. }
  823. if (status != EFI_SUCCESS)
  824. goto free_mem_map;
  825. memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
  826. efi->efi_systab = (unsigned long)sys_table;
  827. efi->efi_memdesc_size = desc_size;
  828. efi->efi_memdesc_version = desc_version;
  829. efi->efi_memmap = (unsigned long)mem_map;
  830. efi->efi_memmap_size = size;
  831. #ifdef CONFIG_X86_64
  832. efi->efi_systab_hi = (unsigned long)sys_table >> 32;
  833. efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
  834. #endif
  835. /* Might as well exit boot services now */
  836. status = efi_call_phys2(sys_table->boottime->exit_boot_services,
  837. handle, key);
  838. if (status != EFI_SUCCESS)
  839. goto free_mem_map;
  840. /* Historic? */
  841. boot_params->alt_mem_k = 32 * 1024;
  842. /*
  843. * Convert the EFI memory map to E820.
  844. */
  845. nr_entries = 0;
  846. for (i = 0; i < size / desc_size; i++) {
  847. efi_memory_desc_t *d;
  848. unsigned int e820_type = 0;
  849. unsigned long m = (unsigned long)mem_map;
  850. d = (efi_memory_desc_t *)(m + (i * desc_size));
  851. switch (d->type) {
  852. case EFI_RESERVED_TYPE:
  853. case EFI_RUNTIME_SERVICES_CODE:
  854. case EFI_RUNTIME_SERVICES_DATA:
  855. case EFI_MEMORY_MAPPED_IO:
  856. case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
  857. case EFI_PAL_CODE:
  858. e820_type = E820_RESERVED;
  859. break;
  860. case EFI_UNUSABLE_MEMORY:
  861. e820_type = E820_UNUSABLE;
  862. break;
  863. case EFI_ACPI_RECLAIM_MEMORY:
  864. e820_type = E820_ACPI;
  865. break;
  866. case EFI_LOADER_CODE:
  867. case EFI_LOADER_DATA:
  868. case EFI_BOOT_SERVICES_CODE:
  869. case EFI_BOOT_SERVICES_DATA:
  870. case EFI_CONVENTIONAL_MEMORY:
  871. e820_type = E820_RAM;
  872. break;
  873. case EFI_ACPI_MEMORY_NVS:
  874. e820_type = E820_NVS;
  875. break;
  876. default:
  877. continue;
  878. }
  879. /* Merge adjacent mappings */
  880. if (prev && prev->type == e820_type &&
  881. (prev->addr + prev->size) == d->phys_addr)
  882. prev->size += d->num_pages << 12;
  883. else {
  884. e820_map->addr = d->phys_addr;
  885. e820_map->size = d->num_pages << 12;
  886. e820_map->type = e820_type;
  887. prev = e820_map++;
  888. nr_entries++;
  889. }
  890. }
  891. boot_params->e820_entries = nr_entries;
  892. return EFI_SUCCESS;
  893. free_mem_map:
  894. low_free(_size, (unsigned long)mem_map);
  895. return status;
  896. }
  897. static efi_status_t relocate_kernel(struct setup_header *hdr)
  898. {
  899. unsigned long start, nr_pages;
  900. efi_status_t status;
  901. /*
  902. * The EFI firmware loader could have placed the kernel image
  903. * anywhere in memory, but the kernel has various restrictions
  904. * on the max physical address it can run at. Attempt to move
  905. * the kernel to boot_params.pref_address, or as low as
  906. * possible.
  907. */
  908. start = hdr->pref_address;
  909. nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
  910. status = efi_call_phys4(sys_table->boottime->allocate_pages,
  911. EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
  912. nr_pages, &start);
  913. if (status != EFI_SUCCESS) {
  914. status = low_alloc(hdr->init_size, hdr->kernel_alignment,
  915. &start);
  916. if (status != EFI_SUCCESS)
  917. efi_printk("Failed to alloc mem for kernel\n");
  918. }
  919. if (status == EFI_SUCCESS)
  920. memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
  921. hdr->init_size);
  922. hdr->pref_address = hdr->code32_start;
  923. hdr->code32_start = (__u32)start;
  924. return status;
  925. }
  926. /*
  927. * On success we return a pointer to a boot_params structure, and NULL
  928. * on failure.
  929. */
  930. struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
  931. struct boot_params *boot_params)
  932. {
  933. struct desc_ptr *gdt, *idt;
  934. efi_loaded_image_t *image;
  935. struct setup_header *hdr = &boot_params->hdr;
  936. efi_status_t status;
  937. struct desc_struct *desc;
  938. sys_table = _table;
  939. /* Check if we were booted by the EFI firmware */
  940. if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  941. goto fail;
  942. setup_graphics(boot_params);
  943. setup_efi_pci(boot_params);
  944. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  945. EFI_LOADER_DATA, sizeof(*gdt),
  946. (void **)&gdt);
  947. if (status != EFI_SUCCESS) {
  948. efi_printk("Failed to alloc mem for gdt structure\n");
  949. goto fail;
  950. }
  951. gdt->size = 0x800;
  952. status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
  953. if (status != EFI_SUCCESS) {
  954. efi_printk("Failed to alloc mem for gdt\n");
  955. goto fail;
  956. }
  957. status = efi_call_phys3(sys_table->boottime->allocate_pool,
  958. EFI_LOADER_DATA, sizeof(*idt),
  959. (void **)&idt);
  960. if (status != EFI_SUCCESS) {
  961. efi_printk("Failed to alloc mem for idt structure\n");
  962. goto fail;
  963. }
  964. idt->size = 0;
  965. idt->address = 0;
  966. /*
  967. * If the kernel isn't already loaded at the preferred load
  968. * address, relocate it.
  969. */
  970. if (hdr->pref_address != hdr->code32_start) {
  971. status = relocate_kernel(hdr);
  972. if (status != EFI_SUCCESS)
  973. goto fail;
  974. }
  975. status = exit_boot(boot_params, handle);
  976. if (status != EFI_SUCCESS)
  977. goto fail;
  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 ("lidt %0" : : "m" (*idt));
  1027. asm volatile ("lgdt %0" : : "m" (*gdt));
  1028. asm volatile("cli");
  1029. return boot_params;
  1030. fail:
  1031. return NULL;
  1032. }