eboot.c 26 KB

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