eboot.c 29 KB

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