eboot.c 26 KB

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