eboot.c 23 KB

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