eboot.c 29 KB

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