linux.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/loader.h>
  19. #include <grub/memory.h>
  20. #include <grub/normal.h>
  21. #include <grub/file.h>
  22. #include <grub/disk.h>
  23. #include <grub/err.h>
  24. #include <grub/misc.h>
  25. #include <grub/types.h>
  26. #include <grub/dl.h>
  27. #include <grub/mm.h>
  28. #include <grub/term.h>
  29. #include <grub/cpu/linux.h>
  30. #include <grub/video.h>
  31. #include <grub/video_fb.h>
  32. #include <grub/command.h>
  33. #include <grub/i386/relocator.h>
  34. #include <grub/i18n.h>
  35. #include <grub/lib/cmdline.h>
  36. #include <grub/linux.h>
  37. GRUB_MOD_LICENSE ("GPLv3+");
  38. #ifdef GRUB_MACHINE_PCBIOS
  39. #include <grub/i386/pc/vesa_modes_table.h>
  40. #endif
  41. #ifdef GRUB_MACHINE_EFI
  42. #include <grub/efi/efi.h>
  43. #define HAS_VGA_TEXT 0
  44. #define DEFAULT_VIDEO_MODE "auto"
  45. #define ACCEPTS_PURE_TEXT 0
  46. #elif defined (GRUB_MACHINE_IEEE1275)
  47. #include <grub/ieee1275/ieee1275.h>
  48. #define HAS_VGA_TEXT 0
  49. #define DEFAULT_VIDEO_MODE "text"
  50. #define ACCEPTS_PURE_TEXT 1
  51. #else
  52. #include <grub/i386/pc/vbe.h>
  53. #include <grub/i386/pc/console.h>
  54. #define HAS_VGA_TEXT 1
  55. #define DEFAULT_VIDEO_MODE "text"
  56. #define ACCEPTS_PURE_TEXT 1
  57. #endif
  58. static grub_dl_t my_mod;
  59. static grub_size_t linux_mem_size;
  60. static int loaded;
  61. static void *prot_mode_mem;
  62. static grub_addr_t prot_mode_target;
  63. static void *initrd_mem;
  64. static grub_addr_t initrd_mem_target;
  65. static grub_size_t prot_init_space;
  66. static struct grub_relocator *relocator = NULL;
  67. static void *efi_mmap_buf;
  68. static grub_size_t maximal_cmdline_size;
  69. static struct linux_kernel_params linux_params;
  70. static char *linux_cmdline;
  71. #ifdef GRUB_MACHINE_EFI
  72. static grub_efi_uintn_t efi_mmap_size;
  73. #else
  74. static const grub_size_t efi_mmap_size = 0;
  75. #endif
  76. /* FIXME */
  77. #if 0
  78. struct idt_descriptor
  79. {
  80. grub_uint16_t limit;
  81. void *base;
  82. } GRUB_PACKED;
  83. static struct idt_descriptor idt_desc =
  84. {
  85. 0,
  86. 0
  87. };
  88. #endif
  89. static inline grub_size_t
  90. page_align (grub_size_t size)
  91. {
  92. return (size + (1 << 12) - 1) & (~((1 << 12) - 1));
  93. }
  94. #ifdef GRUB_MACHINE_EFI
  95. /* Find the optimal number of pages for the memory map. Is it better to
  96. move this code to efi/mm.c? */
  97. static grub_efi_uintn_t
  98. find_efi_mmap_size (void)
  99. {
  100. static grub_efi_uintn_t mmap_size = 0;
  101. if (mmap_size != 0)
  102. return mmap_size;
  103. mmap_size = (1 << 12);
  104. while (1)
  105. {
  106. int ret;
  107. grub_efi_memory_descriptor_t *mmap;
  108. grub_efi_uintn_t desc_size;
  109. grub_efi_uintn_t cur_mmap_size = mmap_size;
  110. mmap = grub_malloc (cur_mmap_size);
  111. if (! mmap)
  112. return 0;
  113. ret = grub_efi_get_memory_map (&cur_mmap_size, mmap, 0, &desc_size, 0);
  114. grub_free (mmap);
  115. if (ret < 0)
  116. {
  117. grub_error (GRUB_ERR_IO, "cannot get memory map");
  118. return 0;
  119. }
  120. else if (ret > 0)
  121. break;
  122. if (mmap_size < cur_mmap_size)
  123. mmap_size = cur_mmap_size;
  124. mmap_size += (1 << 12);
  125. }
  126. /* Increase the size a bit for safety, because GRUB allocates more on
  127. later, and EFI itself may allocate more. */
  128. mmap_size += (3 << 12);
  129. mmap_size = page_align (mmap_size);
  130. return mmap_size;
  131. }
  132. #endif
  133. /* Helper for find_mmap_size. */
  134. static int
  135. count_hook (grub_uint64_t addr __attribute__ ((unused)),
  136. grub_uint64_t size __attribute__ ((unused)),
  137. grub_memory_type_t type __attribute__ ((unused)), void *data)
  138. {
  139. grub_size_t *count = data;
  140. (*count)++;
  141. return 0;
  142. }
  143. /* Find the optimal number of pages for the memory map. */
  144. static grub_size_t
  145. find_mmap_size (void)
  146. {
  147. grub_size_t count = 0, mmap_size;
  148. grub_mmap_iterate (count_hook, &count);
  149. mmap_size = count * sizeof (struct grub_e820_mmap);
  150. /* Increase the size a bit for safety, because GRUB allocates more on
  151. later. */
  152. mmap_size += (1 << 12);
  153. return page_align (mmap_size);
  154. }
  155. static void
  156. free_pages (void)
  157. {
  158. grub_relocator_unload (relocator);
  159. relocator = NULL;
  160. prot_mode_mem = initrd_mem = 0;
  161. prot_mode_target = initrd_mem_target = 0;
  162. }
  163. /* Allocate pages for the real mode code and the protected mode code
  164. for linux as well as a memory map buffer. */
  165. static grub_err_t
  166. allocate_pages (grub_size_t prot_size, grub_size_t *align,
  167. grub_size_t min_align, int relocatable,
  168. grub_uint64_t preferred_address)
  169. {
  170. grub_err_t err;
  171. if (prot_size == 0)
  172. prot_size = 1;
  173. prot_size = page_align (prot_size);
  174. /* Initialize the memory pointers with NULL for convenience. */
  175. free_pages ();
  176. relocator = grub_relocator_new ();
  177. if (!relocator)
  178. {
  179. err = grub_errno;
  180. goto fail;
  181. }
  182. /* FIXME: Should request low memory from the heap when this feature is
  183. implemented. */
  184. {
  185. grub_relocator_chunk_t ch;
  186. if (relocatable)
  187. {
  188. err = grub_relocator_alloc_chunk_align (relocator, &ch,
  189. preferred_address,
  190. preferred_address,
  191. prot_size, 1,
  192. GRUB_RELOCATOR_PREFERENCE_LOW,
  193. 1);
  194. for (; err && *align + 1 > min_align; (*align)--)
  195. {
  196. grub_errno = GRUB_ERR_NONE;
  197. err = grub_relocator_alloc_chunk_align (relocator, &ch,
  198. 0x1000000,
  199. 0xffffffff & ~prot_size,
  200. prot_size, 1 << *align,
  201. GRUB_RELOCATOR_PREFERENCE_LOW,
  202. 1);
  203. }
  204. if (err)
  205. goto fail;
  206. }
  207. else
  208. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  209. preferred_address,
  210. prot_size);
  211. if (err)
  212. goto fail;
  213. prot_mode_mem = get_virtual_current_address (ch);
  214. prot_mode_target = get_physical_target_address (ch);
  215. }
  216. grub_dprintf ("linux", "prot_mode_mem = %p, prot_mode_target = %lx, prot_size = %x\n",
  217. prot_mode_mem, (unsigned long) prot_mode_target,
  218. (unsigned) prot_size);
  219. return GRUB_ERR_NONE;
  220. fail:
  221. free_pages ();
  222. return err;
  223. }
  224. static grub_err_t
  225. grub_e820_add_region (struct grub_e820_mmap *e820_map, int *e820_num,
  226. grub_uint64_t start, grub_uint64_t size,
  227. grub_uint32_t type)
  228. {
  229. int n = *e820_num;
  230. if ((n > 0) && (e820_map[n - 1].addr + e820_map[n - 1].size == start) &&
  231. (e820_map[n - 1].type == type))
  232. e820_map[n - 1].size += size;
  233. else
  234. {
  235. e820_map[n].addr = start;
  236. e820_map[n].size = size;
  237. e820_map[n].type = type;
  238. (*e820_num)++;
  239. }
  240. return GRUB_ERR_NONE;
  241. }
  242. static grub_err_t
  243. grub_linux_setup_video (struct linux_kernel_params *params)
  244. {
  245. struct grub_video_mode_info mode_info;
  246. void *framebuffer;
  247. grub_err_t err;
  248. grub_video_driver_id_t driver_id;
  249. const char *gfxlfbvar = grub_env_get ("gfxpayloadforcelfb");
  250. driver_id = grub_video_get_driver_id ();
  251. if (driver_id == GRUB_VIDEO_DRIVER_NONE)
  252. return 1;
  253. err = grub_video_get_info_and_fini (&mode_info, &framebuffer);
  254. if (err)
  255. {
  256. grub_errno = GRUB_ERR_NONE;
  257. return 1;
  258. }
  259. params->lfb_width = mode_info.width;
  260. params->lfb_height = mode_info.height;
  261. params->lfb_depth = mode_info.bpp;
  262. params->lfb_line_len = mode_info.pitch;
  263. params->lfb_base = (grub_size_t) framebuffer;
  264. params->lfb_size = ALIGN_UP (params->lfb_line_len * params->lfb_height, 65536);
  265. params->red_mask_size = mode_info.red_mask_size;
  266. params->red_field_pos = mode_info.red_field_pos;
  267. params->green_mask_size = mode_info.green_mask_size;
  268. params->green_field_pos = mode_info.green_field_pos;
  269. params->blue_mask_size = mode_info.blue_mask_size;
  270. params->blue_field_pos = mode_info.blue_field_pos;
  271. params->reserved_mask_size = mode_info.reserved_mask_size;
  272. params->reserved_field_pos = mode_info.reserved_field_pos;
  273. if (gfxlfbvar && (gfxlfbvar[0] == '1' || gfxlfbvar[0] == 'y'))
  274. params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
  275. else
  276. {
  277. switch (driver_id)
  278. {
  279. case GRUB_VIDEO_DRIVER_VBE:
  280. params->lfb_size >>= 16;
  281. params->have_vga = GRUB_VIDEO_LINUX_TYPE_VESA;
  282. break;
  283. case GRUB_VIDEO_DRIVER_EFI_UGA:
  284. case GRUB_VIDEO_DRIVER_EFI_GOP:
  285. params->have_vga = GRUB_VIDEO_LINUX_TYPE_EFIFB;
  286. break;
  287. /* FIXME: check if better id is available. */
  288. case GRUB_VIDEO_DRIVER_SM712:
  289. case GRUB_VIDEO_DRIVER_SIS315PRO:
  290. case GRUB_VIDEO_DRIVER_VGA:
  291. case GRUB_VIDEO_DRIVER_CIRRUS:
  292. case GRUB_VIDEO_DRIVER_BOCHS:
  293. case GRUB_VIDEO_DRIVER_RADEON_FULOONG2E:
  294. case GRUB_VIDEO_DRIVER_RADEON_YEELOONG3A:
  295. case GRUB_VIDEO_DRIVER_IEEE1275:
  296. case GRUB_VIDEO_DRIVER_COREBOOT:
  297. /* Make gcc happy. */
  298. case GRUB_VIDEO_DRIVER_XEN:
  299. case GRUB_VIDEO_DRIVER_SDL:
  300. case GRUB_VIDEO_DRIVER_NONE:
  301. case GRUB_VIDEO_ADAPTER_CAPTURE:
  302. params->have_vga = GRUB_VIDEO_LINUX_TYPE_SIMPLE;
  303. break;
  304. }
  305. }
  306. #ifdef GRUB_MACHINE_PCBIOS
  307. /* VESA packed modes may come with zeroed mask sizes, which need
  308. to be set here according to DAC Palette width. If we don't,
  309. this results in Linux displaying a black screen. */
  310. if (driver_id == GRUB_VIDEO_DRIVER_VBE && mode_info.bpp <= 8)
  311. {
  312. struct grub_vbe_info_block controller_info;
  313. int status;
  314. int width = 8;
  315. status = grub_vbe_bios_get_controller_info (&controller_info);
  316. if (status == GRUB_VBE_STATUS_OK &&
  317. (controller_info.capabilities & GRUB_VBE_CAPABILITY_DACWIDTH))
  318. status = grub_vbe_bios_set_dac_palette_width (&width);
  319. if (status != GRUB_VBE_STATUS_OK)
  320. /* 6 is default after mode reset. */
  321. width = 6;
  322. params->red_mask_size = params->green_mask_size
  323. = params->blue_mask_size = width;
  324. params->reserved_mask_size = 0;
  325. }
  326. #endif
  327. return GRUB_ERR_NONE;
  328. }
  329. /* Context for grub_linux_boot. */
  330. struct grub_linux_boot_ctx
  331. {
  332. grub_addr_t real_mode_target;
  333. grub_size_t real_size;
  334. struct linux_kernel_params *params;
  335. int e820_num;
  336. };
  337. /* Helper for grub_linux_boot. */
  338. static int
  339. grub_linux_boot_mmap_find (grub_uint64_t addr, grub_uint64_t size,
  340. grub_memory_type_t type, void *data)
  341. {
  342. struct grub_linux_boot_ctx *ctx = data;
  343. /* We must put real mode code in the traditional space. */
  344. if (type != GRUB_MEMORY_AVAILABLE || addr > 0x90000)
  345. return 0;
  346. if (addr + size < 0x10000)
  347. return 0;
  348. if (addr < 0x10000)
  349. {
  350. size += addr - 0x10000;
  351. addr = 0x10000;
  352. }
  353. if (addr + size > 0x90000)
  354. size = 0x90000 - addr;
  355. if (ctx->real_size + efi_mmap_size > size)
  356. return 0;
  357. grub_dprintf ("linux", "addr = %lx, size = %x, need_size = %x\n",
  358. (unsigned long) addr,
  359. (unsigned) size,
  360. (unsigned) (ctx->real_size + efi_mmap_size));
  361. ctx->real_mode_target = ((addr + size) - (ctx->real_size + efi_mmap_size));
  362. return 1;
  363. }
  364. /* GRUB types conveniently match E820 types. */
  365. static int
  366. grub_linux_boot_mmap_fill (grub_uint64_t addr, grub_uint64_t size,
  367. grub_memory_type_t type, void *data)
  368. {
  369. struct grub_linux_boot_ctx *ctx = data;
  370. if (grub_e820_add_region (ctx->params->e820_map, &ctx->e820_num,
  371. addr, size, type))
  372. return 1;
  373. return 0;
  374. }
  375. static grub_err_t
  376. grub_linux_boot (void)
  377. {
  378. grub_err_t err = 0;
  379. const char *modevar;
  380. char *tmp;
  381. struct grub_relocator32_state state;
  382. void *real_mode_mem;
  383. struct grub_linux_boot_ctx ctx = {
  384. .real_mode_target = 0
  385. };
  386. grub_size_t mmap_size;
  387. grub_size_t cl_offset;
  388. #ifdef GRUB_MACHINE_IEEE1275
  389. {
  390. const char *bootpath;
  391. grub_ssize_t len;
  392. bootpath = grub_env_get ("root");
  393. if (bootpath)
  394. grub_ieee1275_set_property (grub_ieee1275_chosen,
  395. "bootpath", bootpath,
  396. grub_strlen (bootpath) + 1,
  397. &len);
  398. linux_params.ofw_signature = GRUB_LINUX_OFW_SIGNATURE;
  399. linux_params.ofw_num_items = 1;
  400. linux_params.ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn;
  401. linux_params.ofw_idt = 0;
  402. }
  403. #endif
  404. modevar = grub_env_get ("gfxpayload");
  405. /* Now all graphical modes are acceptable.
  406. May change in future if we have modes without framebuffer. */
  407. if (modevar && *modevar != 0)
  408. {
  409. tmp = grub_xasprintf ("%s;" DEFAULT_VIDEO_MODE, modevar);
  410. if (! tmp)
  411. return grub_errno;
  412. #if ACCEPTS_PURE_TEXT
  413. err = grub_video_set_mode (tmp, 0, 0);
  414. #else
  415. err = grub_video_set_mode (tmp, GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
  416. #endif
  417. grub_free (tmp);
  418. }
  419. else /* We can't go back to text mode from coreboot fb. */
  420. #ifdef GRUB_MACHINE_COREBOOT
  421. if (grub_video_get_driver_id () == GRUB_VIDEO_DRIVER_COREBOOT)
  422. err = GRUB_ERR_NONE;
  423. else
  424. #endif
  425. {
  426. #if ACCEPTS_PURE_TEXT
  427. err = grub_video_set_mode (DEFAULT_VIDEO_MODE, 0, 0);
  428. #else
  429. err = grub_video_set_mode (DEFAULT_VIDEO_MODE,
  430. GRUB_VIDEO_MODE_TYPE_PURE_TEXT, 0);
  431. #endif
  432. }
  433. if (err)
  434. {
  435. grub_print_error ();
  436. grub_puts_ (N_("Booting in blind mode"));
  437. grub_errno = GRUB_ERR_NONE;
  438. }
  439. if (grub_linux_setup_video (&linux_params))
  440. {
  441. #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU)
  442. linux_params.have_vga = GRUB_VIDEO_LINUX_TYPE_TEXT;
  443. linux_params.video_mode = 0x3;
  444. #else
  445. linux_params.have_vga = 0;
  446. linux_params.video_mode = 0;
  447. linux_params.video_width = 0;
  448. linux_params.video_height = 0;
  449. #endif
  450. }
  451. #ifndef GRUB_MACHINE_IEEE1275
  452. if (linux_params.have_vga == GRUB_VIDEO_LINUX_TYPE_TEXT)
  453. #endif
  454. {
  455. grub_term_output_t term;
  456. int found = 0;
  457. FOR_ACTIVE_TERM_OUTPUTS(term)
  458. if (grub_strcmp (term->name, "vga_text") == 0
  459. || grub_strcmp (term->name, "console") == 0
  460. || grub_strcmp (term->name, "ofconsole") == 0)
  461. {
  462. struct grub_term_coordinate pos = grub_term_getxy (term);
  463. linux_params.video_cursor_x = pos.x;
  464. linux_params.video_cursor_y = pos.y;
  465. linux_params.video_width = grub_term_width (term);
  466. linux_params.video_height = grub_term_height (term);
  467. found = 1;
  468. break;
  469. }
  470. if (!found)
  471. {
  472. linux_params.video_cursor_x = 0;
  473. linux_params.video_cursor_y = 0;
  474. linux_params.video_width = 80;
  475. linux_params.video_height = 25;
  476. }
  477. }
  478. mmap_size = find_mmap_size ();
  479. /* Make sure that each size is aligned to a page boundary. */
  480. cl_offset = ALIGN_UP (mmap_size + sizeof (linux_params), 4096);
  481. if (cl_offset < ((grub_size_t) linux_params.setup_sects << GRUB_DISK_SECTOR_BITS))
  482. cl_offset = ALIGN_UP ((grub_size_t) (linux_params.setup_sects
  483. << GRUB_DISK_SECTOR_BITS), 4096);
  484. ctx.real_size = ALIGN_UP (cl_offset + maximal_cmdline_size, 4096);
  485. #ifdef GRUB_MACHINE_EFI
  486. efi_mmap_size = find_efi_mmap_size ();
  487. if (efi_mmap_size == 0)
  488. return grub_errno;
  489. #endif
  490. grub_dprintf ("linux", "real_size = %x, mmap_size = %x\n",
  491. (unsigned) ctx.real_size, (unsigned) mmap_size);
  492. #ifdef GRUB_MACHINE_EFI
  493. grub_efi_mmap_iterate (grub_linux_boot_mmap_find, &ctx, 1);
  494. if (! ctx.real_mode_target)
  495. grub_efi_mmap_iterate (grub_linux_boot_mmap_find, &ctx, 0);
  496. #else
  497. grub_mmap_iterate (grub_linux_boot_mmap_find, &ctx);
  498. #endif
  499. grub_dprintf ("linux", "real_mode_target = %lx, real_size = %x, efi_mmap_size = %x\n",
  500. (unsigned long) ctx.real_mode_target,
  501. (unsigned) ctx.real_size,
  502. (unsigned) efi_mmap_size);
  503. if (! ctx.real_mode_target)
  504. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "cannot allocate real mode pages");
  505. {
  506. grub_relocator_chunk_t ch;
  507. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  508. ctx.real_mode_target,
  509. (ctx.real_size + efi_mmap_size));
  510. if (err)
  511. return err;
  512. real_mode_mem = get_virtual_current_address (ch);
  513. }
  514. efi_mmap_buf = (grub_uint8_t *) real_mode_mem + ctx.real_size;
  515. grub_dprintf ("linux", "real_mode_mem = %p\n",
  516. real_mode_mem);
  517. ctx.params = real_mode_mem;
  518. *ctx.params = linux_params;
  519. ctx.params->cmd_line_ptr = ctx.real_mode_target + cl_offset;
  520. grub_memcpy ((char *) ctx.params + cl_offset, linux_cmdline,
  521. maximal_cmdline_size);
  522. grub_dprintf ("linux", "code32_start = %x\n",
  523. (unsigned) ctx.params->code32_start);
  524. ctx.e820_num = 0;
  525. if (grub_mmap_iterate (grub_linux_boot_mmap_fill, &ctx))
  526. return grub_errno;
  527. ctx.params->mmap_size = ctx.e820_num;
  528. #ifdef GRUB_MACHINE_EFI
  529. {
  530. grub_efi_uintn_t efi_desc_size;
  531. grub_size_t efi_mmap_target;
  532. grub_efi_uint32_t efi_desc_version;
  533. err = grub_efi_finish_boot_services (&efi_mmap_size, efi_mmap_buf, NULL,
  534. &efi_desc_size, &efi_desc_version);
  535. if (err)
  536. return err;
  537. /* Note that no boot services are available from here. */
  538. efi_mmap_target = ctx.real_mode_target
  539. + ((grub_uint8_t *) efi_mmap_buf - (grub_uint8_t *) real_mode_mem);
  540. /* Pass EFI parameters. */
  541. if (grub_le_to_cpu16 (ctx.params->version) >= 0x0208)
  542. {
  543. ctx.params->v0208.efi_mem_desc_size = efi_desc_size;
  544. ctx.params->v0208.efi_mem_desc_version = efi_desc_version;
  545. ctx.params->v0208.efi_mmap = efi_mmap_target;
  546. ctx.params->v0208.efi_mmap_size = efi_mmap_size;
  547. #ifdef __x86_64__
  548. ctx.params->v0208.efi_mmap_hi = (efi_mmap_target >> 32);
  549. #endif
  550. }
  551. else if (grub_le_to_cpu16 (ctx.params->version) >= 0x0206)
  552. {
  553. ctx.params->v0206.efi_mem_desc_size = efi_desc_size;
  554. ctx.params->v0206.efi_mem_desc_version = efi_desc_version;
  555. ctx.params->v0206.efi_mmap = efi_mmap_target;
  556. ctx.params->v0206.efi_mmap_size = efi_mmap_size;
  557. }
  558. else if (grub_le_to_cpu16 (ctx.params->version) >= 0x0204)
  559. {
  560. ctx.params->v0204.efi_mem_desc_size = efi_desc_size;
  561. ctx.params->v0204.efi_mem_desc_version = efi_desc_version;
  562. ctx.params->v0204.efi_mmap = efi_mmap_target;
  563. ctx.params->v0204.efi_mmap_size = efi_mmap_size;
  564. }
  565. }
  566. #endif
  567. /* FIXME. */
  568. /* asm volatile ("lidt %0" : : "m" (idt_desc)); */
  569. state.ebp = state.edi = state.ebx = 0;
  570. state.esi = ctx.real_mode_target;
  571. state.esp = ctx.real_mode_target;
  572. state.eip = ctx.params->code32_start;
  573. return grub_relocator32_boot (relocator, state, 0);
  574. }
  575. static grub_err_t
  576. grub_linux_unload (void)
  577. {
  578. grub_dl_unref (my_mod);
  579. loaded = 0;
  580. grub_free (linux_cmdline);
  581. linux_cmdline = 0;
  582. return GRUB_ERR_NONE;
  583. }
  584. static grub_err_t
  585. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  586. int argc, char *argv[])
  587. {
  588. grub_file_t file = 0;
  589. struct linux_kernel_header lh;
  590. grub_uint8_t setup_sects;
  591. grub_size_t real_size, prot_size, prot_file_size;
  592. grub_ssize_t len;
  593. int i;
  594. grub_size_t align, min_align;
  595. int relocatable;
  596. grub_uint64_t preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
  597. grub_dl_ref (my_mod);
  598. if (argc == 0)
  599. {
  600. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  601. goto fail;
  602. }
  603. file = grub_file_open (argv[0]);
  604. if (! file)
  605. goto fail;
  606. if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
  607. {
  608. if (!grub_errno)
  609. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  610. argv[0]);
  611. goto fail;
  612. }
  613. if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
  614. {
  615. grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
  616. goto fail;
  617. }
  618. if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
  619. {
  620. grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
  621. goto fail;
  622. }
  623. /* FIXME: 2.03 is not always good enough (Linux 2.4 can be 2.03 and
  624. still not support 32-bit boot. */
  625. if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_MAGIC_SIGNATURE)
  626. || grub_le_to_cpu16 (lh.version) < 0x0203)
  627. {
  628. grub_error (GRUB_ERR_BAD_OS, "version too old for 32-bit boot"
  629. #ifdef GRUB_MACHINE_PCBIOS
  630. " (try with `linux16')"
  631. #endif
  632. );
  633. goto fail;
  634. }
  635. if (! (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL))
  636. {
  637. grub_error (GRUB_ERR_BAD_OS, "zImage doesn't support 32-bit boot"
  638. #ifdef GRUB_MACHINE_PCBIOS
  639. " (try with `linux16')"
  640. #endif
  641. );
  642. goto fail;
  643. }
  644. if (grub_le_to_cpu16 (lh.version) >= 0x0206)
  645. maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
  646. else
  647. maximal_cmdline_size = 256;
  648. if (maximal_cmdline_size < 128)
  649. maximal_cmdline_size = 128;
  650. setup_sects = lh.setup_sects;
  651. /* If SETUP_SECTS is not set, set it to the default (4). */
  652. if (! setup_sects)
  653. setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
  654. real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
  655. prot_file_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
  656. if (grub_le_to_cpu16 (lh.version) >= 0x205
  657. && lh.kernel_alignment != 0
  658. && ((lh.kernel_alignment - 1) & lh.kernel_alignment) == 0)
  659. {
  660. for (align = 0; align < 32; align++)
  661. if (grub_le_to_cpu32 (lh.kernel_alignment) & (1 << align))
  662. break;
  663. relocatable = grub_le_to_cpu32 (lh.relocatable);
  664. }
  665. else
  666. {
  667. align = 0;
  668. relocatable = 0;
  669. }
  670. if (grub_le_to_cpu16 (lh.version) >= 0x020a)
  671. {
  672. min_align = lh.min_alignment;
  673. prot_size = grub_le_to_cpu32 (lh.init_size);
  674. prot_init_space = page_align (prot_size);
  675. if (relocatable)
  676. preferred_address = grub_le_to_cpu64 (lh.pref_address);
  677. else
  678. preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
  679. }
  680. else
  681. {
  682. min_align = align;
  683. prot_size = prot_file_size;
  684. preferred_address = GRUB_LINUX_BZIMAGE_ADDR;
  685. /* Usually, the compression ratio is about 50%. */
  686. prot_init_space = page_align (prot_size) * 3;
  687. }
  688. if (allocate_pages (prot_size, &align,
  689. min_align, relocatable,
  690. preferred_address))
  691. goto fail;
  692. grub_memset (&linux_params, 0, sizeof (linux_params));
  693. grub_memcpy (&linux_params.setup_sects, &lh.setup_sects, sizeof (lh) - 0x1F1);
  694. linux_params.code32_start = prot_mode_target + lh.code32_start - GRUB_LINUX_BZIMAGE_ADDR;
  695. linux_params.kernel_alignment = (1 << align);
  696. linux_params.ps_mouse = linux_params.padding10 = 0;
  697. len = sizeof (linux_params) - sizeof (lh);
  698. if (grub_file_read (file, (char *) &linux_params + sizeof (lh), len) != len)
  699. {
  700. if (!grub_errno)
  701. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  702. argv[0]);
  703. goto fail;
  704. }
  705. linux_params.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
  706. /* These two are used (instead of cmd_line_ptr) by older versions of Linux,
  707. and otherwise ignored. */
  708. linux_params.cl_magic = GRUB_LINUX_CL_MAGIC;
  709. linux_params.cl_offset = 0x1000;
  710. linux_params.ramdisk_image = 0;
  711. linux_params.ramdisk_size = 0;
  712. linux_params.heap_end_ptr = GRUB_LINUX_HEAP_END_OFFSET;
  713. linux_params.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
  714. /* These are not needed to be precise, because Linux uses these values
  715. only to raise an error when the decompression code cannot find good
  716. space. */
  717. linux_params.ext_mem = ((32 * 0x100000) >> 10);
  718. linux_params.alt_mem = ((32 * 0x100000) >> 10);
  719. /* Ignored by Linux. */
  720. linux_params.video_page = 0;
  721. /* Only used when `video_mode == 0x7', otherwise ignored. */
  722. linux_params.video_ega_bx = 0;
  723. linux_params.font_size = 16; /* XXX */
  724. #ifdef GRUB_MACHINE_EFI
  725. #ifdef __x86_64__
  726. if (grub_le_to_cpu16 (linux_params.version) < 0x0208 &&
  727. ((grub_addr_t) grub_efi_system_table >> 32) != 0)
  728. return grub_error(GRUB_ERR_BAD_OS,
  729. "kernel does not support 64-bit addressing");
  730. #endif
  731. if (grub_le_to_cpu16 (linux_params.version) >= 0x0208)
  732. {
  733. linux_params.v0208.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
  734. linux_params.v0208.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
  735. #ifdef __x86_64__
  736. linux_params.v0208.efi_system_table_hi = (grub_uint32_t) ((grub_uint64_t) grub_efi_system_table >> 32);
  737. #endif
  738. }
  739. else if (grub_le_to_cpu16 (linux_params.version) >= 0x0206)
  740. {
  741. linux_params.v0206.efi_signature = GRUB_LINUX_EFI_SIGNATURE;
  742. linux_params.v0206.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
  743. }
  744. else if (grub_le_to_cpu16 (linux_params.version) >= 0x0204)
  745. {
  746. linux_params.v0204.efi_signature = GRUB_LINUX_EFI_SIGNATURE_0204;
  747. linux_params.v0204.efi_system_table = (grub_uint32_t) (grub_addr_t) grub_efi_system_table;
  748. }
  749. #endif
  750. /* The other parameters are filled when booting. */
  751. grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
  752. grub_dprintf ("linux", "bzImage, setup=0x%x, size=0x%x\n",
  753. (unsigned) real_size, (unsigned) prot_size);
  754. /* Look for memory size and video mode specified on the command line. */
  755. linux_mem_size = 0;
  756. for (i = 1; i < argc; i++)
  757. #ifdef GRUB_MACHINE_PCBIOS
  758. if (grub_memcmp (argv[i], "vga=", 4) == 0)
  759. {
  760. /* Video mode selection support. */
  761. char *val = argv[i] + 4;
  762. unsigned vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
  763. struct grub_vesa_mode_table_entry *linux_mode;
  764. grub_err_t err;
  765. char *buf;
  766. grub_dl_load ("vbe");
  767. if (grub_strcmp (val, "normal") == 0)
  768. vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
  769. else if (grub_strcmp (val, "ext") == 0)
  770. vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
  771. else if (grub_strcmp (val, "ask") == 0)
  772. {
  773. grub_puts_ (N_("Legacy `ask' parameter no longer supported."));
  774. /* We usually would never do this in a loader, but "vga=ask" means user
  775. requested interaction, so it can't hurt to request keyboard input. */
  776. grub_wait_after_message ();
  777. goto fail;
  778. }
  779. else
  780. vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
  781. switch (vid_mode)
  782. {
  783. case 0:
  784. case GRUB_LINUX_VID_MODE_NORMAL:
  785. grub_env_set ("gfxpayload", "text");
  786. grub_printf_ (N_("%s is deprecated. "
  787. "Use set gfxpayload=%s before "
  788. "linux command instead.\n"),
  789. argv[i], "text");
  790. break;
  791. case 1:
  792. case GRUB_LINUX_VID_MODE_EXTENDED:
  793. /* FIXME: support 80x50 text. */
  794. grub_env_set ("gfxpayload", "text");
  795. grub_printf_ (N_("%s is deprecated. "
  796. "Use set gfxpayload=%s before "
  797. "linux command instead.\n"),
  798. argv[i], "text");
  799. break;
  800. default:
  801. /* Ignore invalid values. */
  802. if (vid_mode < GRUB_VESA_MODE_TABLE_START ||
  803. vid_mode > GRUB_VESA_MODE_TABLE_END)
  804. {
  805. grub_env_set ("gfxpayload", "text");
  806. /* TRANSLATORS: "x" has to be entered in, like an identifier,
  807. so please don't use better Unicode codepoints. */
  808. grub_printf_ (N_("%s is deprecated. VGA mode %d isn't recognized. "
  809. "Use set gfxpayload=WIDTHxHEIGHT[xDEPTH] "
  810. "before linux command instead.\n"),
  811. argv[i], vid_mode);
  812. break;
  813. }
  814. linux_mode = &grub_vesa_mode_table[vid_mode
  815. - GRUB_VESA_MODE_TABLE_START];
  816. buf = grub_xasprintf ("%ux%ux%u,%ux%u",
  817. linux_mode->width, linux_mode->height,
  818. linux_mode->depth,
  819. linux_mode->width, linux_mode->height);
  820. if (! buf)
  821. goto fail;
  822. grub_printf_ (N_("%s is deprecated. "
  823. "Use set gfxpayload=%s before "
  824. "linux command instead.\n"),
  825. argv[i], buf);
  826. err = grub_env_set ("gfxpayload", buf);
  827. grub_free (buf);
  828. if (err)
  829. goto fail;
  830. }
  831. }
  832. else
  833. #endif /* GRUB_MACHINE_PCBIOS */
  834. if (grub_memcmp (argv[i], "mem=", 4) == 0)
  835. {
  836. char *val = argv[i] + 4;
  837. linux_mem_size = grub_strtoul (val, &val, 0);
  838. if (grub_errno)
  839. {
  840. grub_errno = GRUB_ERR_NONE;
  841. linux_mem_size = 0;
  842. }
  843. else
  844. {
  845. int shift = 0;
  846. switch (grub_tolower (val[0]))
  847. {
  848. case 'g':
  849. shift += 10;
  850. /* FALLTHROUGH */
  851. case 'm':
  852. shift += 10;
  853. /* FALLTHROUGH */
  854. case 'k':
  855. shift += 10;
  856. /* FALLTHROUGH */
  857. default:
  858. break;
  859. }
  860. /* Check an overflow. */
  861. if (linux_mem_size > (~0UL >> shift))
  862. linux_mem_size = 0;
  863. else
  864. linux_mem_size <<= shift;
  865. }
  866. }
  867. else if (grub_memcmp (argv[i], "quiet", sizeof ("quiet") - 1) == 0)
  868. {
  869. linux_params.loadflags |= GRUB_LINUX_FLAG_QUIET;
  870. }
  871. /* Create kernel command line. */
  872. linux_cmdline = grub_zalloc (maximal_cmdline_size + 1);
  873. if (!linux_cmdline)
  874. goto fail;
  875. grub_memcpy (linux_cmdline, LINUX_IMAGE, sizeof (LINUX_IMAGE));
  876. grub_create_loader_cmdline (argc, argv,
  877. linux_cmdline
  878. + sizeof (LINUX_IMAGE) - 1,
  879. maximal_cmdline_size
  880. - (sizeof (LINUX_IMAGE) - 1));
  881. len = prot_file_size;
  882. if (grub_file_read (file, prot_mode_mem, len) != len && !grub_errno)
  883. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  884. argv[0]);
  885. if (grub_errno == GRUB_ERR_NONE)
  886. {
  887. grub_loader_set (grub_linux_boot, grub_linux_unload,
  888. 0 /* set noreturn=0 in order to avoid grub_console_fini() */);
  889. loaded = 1;
  890. }
  891. fail:
  892. if (file)
  893. grub_file_close (file);
  894. if (grub_errno != GRUB_ERR_NONE)
  895. {
  896. grub_dl_unref (my_mod);
  897. loaded = 0;
  898. }
  899. return grub_errno;
  900. }
  901. static grub_err_t
  902. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  903. int argc, char *argv[])
  904. {
  905. grub_size_t size = 0, aligned_size = 0;
  906. grub_addr_t addr_min, addr_max;
  907. grub_addr_t addr;
  908. grub_err_t err;
  909. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  910. if (argc == 0)
  911. {
  912. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  913. goto fail;
  914. }
  915. if (! loaded)
  916. {
  917. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
  918. goto fail;
  919. }
  920. if (grub_initrd_init (argc, argv, &initrd_ctx))
  921. goto fail;
  922. size = grub_get_initrd_size (&initrd_ctx);
  923. aligned_size = ALIGN_UP (size, 4096);
  924. /* Get the highest address available for the initrd. */
  925. if (grub_le_to_cpu16 (linux_params.version) >= 0x0203)
  926. {
  927. addr_max = grub_cpu_to_le32 (linux_params.initrd_addr_max);
  928. /* XXX in reality, Linux specifies a bogus value, so
  929. it is necessary to make sure that ADDR_MAX does not exceed
  930. 0x3fffffff. */
  931. if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
  932. addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
  933. }
  934. else
  935. addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
  936. if (linux_mem_size != 0 && linux_mem_size < addr_max)
  937. addr_max = linux_mem_size;
  938. /* Linux 2.3.xx has a bug in the memory range check, so avoid
  939. the last page.
  940. Linux 2.2.xx has a bug in the memory range check, which is
  941. worse than that of Linux 2.3.xx, so avoid the last 64kb. */
  942. addr_max -= 0x10000;
  943. addr_min = (grub_addr_t) prot_mode_target + prot_init_space;
  944. /* Put the initrd as high as possible, 4KiB aligned. */
  945. addr = (addr_max - aligned_size) & ~0xFFF;
  946. if (addr < addr_min)
  947. {
  948. grub_error (GRUB_ERR_OUT_OF_RANGE, "the initrd is too big");
  949. goto fail;
  950. }
  951. {
  952. grub_relocator_chunk_t ch;
  953. err = grub_relocator_alloc_chunk_align (relocator, &ch,
  954. addr_min, addr, aligned_size,
  955. 0x1000,
  956. GRUB_RELOCATOR_PREFERENCE_HIGH,
  957. 1);
  958. if (err)
  959. return err;
  960. initrd_mem = get_virtual_current_address (ch);
  961. initrd_mem_target = get_physical_target_address (ch);
  962. }
  963. if (grub_initrd_load (&initrd_ctx, argv, initrd_mem))
  964. goto fail;
  965. grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n",
  966. (unsigned) addr, (unsigned) size);
  967. linux_params.ramdisk_image = initrd_mem_target;
  968. linux_params.ramdisk_size = size;
  969. linux_params.root_dev = 0x0100; /* XXX */
  970. fail:
  971. grub_initrd_close (&initrd_ctx);
  972. return grub_errno;
  973. }
  974. static grub_command_t cmd_linux, cmd_initrd;
  975. GRUB_MOD_INIT(linux)
  976. {
  977. cmd_linux = grub_register_command ("linux", grub_cmd_linux,
  978. 0, N_("Load Linux."));
  979. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  980. 0, N_("Load initrd."));
  981. my_mod = mod;
  982. }
  983. GRUB_MOD_FINI(linux)
  984. {
  985. grub_unregister_command (cmd_linux);
  986. grub_unregister_command (cmd_initrd);
  987. }