eboot.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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. first_gop = gop;
  346. if (conout_found)
  347. break;
  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. }