eboot.c 23 KB

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