eboot.c 29 KB

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