eboot.c 30 KB

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