eboot.c 26 KB

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