eboot.c 29 KB

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