linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* linux.c - boot Linux zImage or bzImage */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/loader.h>
  20. #include <grub/file.h>
  21. #include <grub/err.h>
  22. #include <grub/device.h>
  23. #include <grub/disk.h>
  24. #include <grub/misc.h>
  25. #include <grub/types.h>
  26. #include <grub/memory.h>
  27. #include <grub/dl.h>
  28. #include <grub/cpu/linux.h>
  29. #include <grub/command.h>
  30. #include <grub/i18n.h>
  31. #include <grub/mm.h>
  32. #include <grub/cpu/relocator.h>
  33. #include <grub/video.h>
  34. #include <grub/i386/floppy.h>
  35. #include <grub/lib/cmdline.h>
  36. #include <grub/linux.h>
  37. GRUB_MOD_LICENSE ("GPLv3+");
  38. #define GRUB_LINUX_CL_OFFSET 0x9000
  39. static grub_dl_t my_mod;
  40. static grub_size_t linux_mem_size;
  41. static int loaded;
  42. static struct grub_relocator *relocator = NULL;
  43. static grub_addr_t grub_linux_real_target;
  44. static char *grub_linux_real_chunk;
  45. static grub_size_t grub_linux16_prot_size;
  46. static grub_size_t maximal_cmdline_size;
  47. static grub_err_t
  48. grub_linux16_boot (void)
  49. {
  50. grub_uint16_t segment;
  51. struct grub_relocator16_state state;
  52. segment = grub_linux_real_target >> 4;
  53. state.gs = state.fs = state.es = state.ds = state.ss = segment;
  54. state.sp = GRUB_LINUX_SETUP_STACK;
  55. state.cs = segment + 0x20;
  56. state.ip = 0;
  57. state.a20 = 1;
  58. grub_video_set_mode ("text", 0, 0);
  59. grub_stop_floppy ();
  60. return grub_relocator16_boot (relocator, state);
  61. }
  62. static grub_err_t
  63. grub_linux_unload (void)
  64. {
  65. grub_dl_unref (my_mod);
  66. loaded = 0;
  67. grub_relocator_unload (relocator);
  68. relocator = NULL;
  69. return GRUB_ERR_NONE;
  70. }
  71. static int
  72. target_hook (grub_uint64_t addr, grub_uint64_t size, grub_memory_type_t type,
  73. void *data)
  74. {
  75. grub_uint64_t *result = data;
  76. grub_uint64_t candidate;
  77. if (type != GRUB_MEMORY_AVAILABLE)
  78. return 0;
  79. if (addr >= 0xa0000)
  80. return 0;
  81. if (addr + size >= 0xa0000)
  82. size = 0xa0000 - addr;
  83. /* Put the real mode part at as a high location as possible. */
  84. candidate = addr + size - (GRUB_LINUX_CL_OFFSET + maximal_cmdline_size);
  85. /* But it must not exceed the traditional area. */
  86. if (candidate > GRUB_LINUX_OLD_REAL_MODE_ADDR)
  87. candidate = GRUB_LINUX_OLD_REAL_MODE_ADDR;
  88. if (candidate < addr)
  89. return 0;
  90. if (candidate > *result || *result == (grub_uint64_t) -1)
  91. *result = candidate;
  92. return 0;
  93. }
  94. static grub_addr_t
  95. grub_find_real_target (void)
  96. {
  97. grub_uint64_t result = (grub_uint64_t) -1;
  98. grub_mmap_iterate (target_hook, &result);
  99. return result;
  100. }
  101. static grub_err_t
  102. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  103. int argc, char *argv[])
  104. {
  105. grub_file_t file = 0;
  106. struct linux_i386_kernel_header lh;
  107. grub_uint8_t setup_sects;
  108. grub_size_t real_size;
  109. grub_ssize_t len;
  110. int i;
  111. char *grub_linux_prot_chunk;
  112. int grub_linux_is_bzimage;
  113. grub_addr_t grub_linux_prot_target;
  114. grub_err_t err;
  115. grub_dl_ref (my_mod);
  116. if (argc == 0)
  117. {
  118. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  119. goto fail;
  120. }
  121. file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
  122. if (! file)
  123. goto fail;
  124. if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
  125. {
  126. if (!grub_errno)
  127. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  128. argv[0]);
  129. goto fail;
  130. }
  131. if (lh.boot_flag != grub_cpu_to_le16_compile_time (0xaa55))
  132. {
  133. grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
  134. goto fail;
  135. }
  136. if (lh.setup_sects > GRUB_LINUX_MAX_SETUP_SECTS)
  137. {
  138. grub_error (GRUB_ERR_BAD_OS, "too many setup sectors");
  139. goto fail;
  140. }
  141. grub_linux_is_bzimage = 0;
  142. setup_sects = lh.setup_sects;
  143. linux_mem_size = 0;
  144. maximal_cmdline_size = 256;
  145. if (lh.header == grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
  146. && grub_le_to_cpu16 (lh.version) >= 0x0200)
  147. {
  148. grub_linux_is_bzimage = (lh.loadflags & GRUB_LINUX_FLAG_BIG_KERNEL);
  149. lh.type_of_loader = GRUB_LINUX_BOOT_LOADER_TYPE;
  150. if (grub_le_to_cpu16 (lh.version) >= 0x0206)
  151. maximal_cmdline_size = grub_le_to_cpu32 (lh.cmdline_size) + 1;
  152. grub_linux_real_target = grub_find_real_target ();
  153. if (grub_linux_real_target == (grub_addr_t)-1)
  154. {
  155. grub_error (GRUB_ERR_OUT_OF_RANGE,
  156. "no appropriate low memory found");
  157. goto fail;
  158. }
  159. if (grub_le_to_cpu16 (lh.version) >= 0x0201)
  160. {
  161. lh.heap_end_ptr = grub_cpu_to_le16_compile_time (GRUB_LINUX_HEAP_END_OFFSET);
  162. lh.loadflags |= GRUB_LINUX_FLAG_CAN_USE_HEAP;
  163. }
  164. if (grub_le_to_cpu16 (lh.version) >= 0x0202)
  165. lh.cmd_line_ptr = grub_linux_real_target + GRUB_LINUX_CL_OFFSET;
  166. else
  167. {
  168. lh.cl_magic = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_MAGIC);
  169. lh.cl_offset = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET);
  170. lh.setup_move_size = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET
  171. + maximal_cmdline_size);
  172. }
  173. }
  174. else
  175. {
  176. /* Your kernel is quite old... */
  177. lh.cl_magic = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_MAGIC);
  178. lh.cl_offset = grub_cpu_to_le16_compile_time (GRUB_LINUX_CL_OFFSET);
  179. setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
  180. grub_linux_real_target = GRUB_LINUX_OLD_REAL_MODE_ADDR;
  181. }
  182. /* If SETUP_SECTS is not set, set it to the default (4). */
  183. if (! setup_sects)
  184. setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
  185. real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
  186. grub_linux16_prot_size = grub_file_size (file)
  187. - real_size - GRUB_DISK_SECTOR_SIZE;
  188. if (! grub_linux_is_bzimage
  189. && GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size
  190. > grub_linux_real_target)
  191. {
  192. grub_error (GRUB_ERR_BAD_OS, "too big zImage (0x%x > 0x%x), use bzImage instead",
  193. (char *) GRUB_LINUX_ZIMAGE_ADDR + grub_linux16_prot_size,
  194. (grub_size_t) grub_linux_real_target);
  195. goto fail;
  196. }
  197. grub_dprintf ("linux", "[Linux-%s, setup=0x%x, size=0x%x]\n",
  198. grub_linux_is_bzimage ? "bzImage" : "zImage",
  199. (unsigned) real_size,
  200. (unsigned) grub_linux16_prot_size);
  201. for (i = 1; i < argc; i++)
  202. if (grub_memcmp (argv[i], "vga=", 4) == 0)
  203. {
  204. /* Video mode selection support. */
  205. grub_uint16_t vid_mode;
  206. char *val = argv[i] + 4;
  207. if (grub_strcmp (val, "normal") == 0)
  208. vid_mode = GRUB_LINUX_VID_MODE_NORMAL;
  209. else if (grub_strcmp (val, "ext") == 0)
  210. vid_mode = GRUB_LINUX_VID_MODE_EXTENDED;
  211. else if (grub_strcmp (val, "ask") == 0)
  212. vid_mode = GRUB_LINUX_VID_MODE_ASK;
  213. else
  214. vid_mode = (grub_uint16_t) grub_strtoul (val, 0, 0);
  215. if (grub_errno)
  216. goto fail;
  217. lh.vid_mode = grub_cpu_to_le16 (vid_mode);
  218. }
  219. else if (grub_memcmp (argv[i], "mem=", 4) == 0)
  220. {
  221. char *val = argv[i] + 4;
  222. linux_mem_size = grub_strtoul (val, &val, 0);
  223. if (grub_errno)
  224. {
  225. grub_errno = GRUB_ERR_NONE;
  226. linux_mem_size = 0;
  227. }
  228. else
  229. {
  230. int shift = 0;
  231. switch (grub_tolower (val[0]))
  232. {
  233. case 'g':
  234. shift += 10;
  235. /* Fallthrough. */
  236. case 'm':
  237. shift += 10;
  238. /* Fallthrough. */
  239. case 'k':
  240. shift += 10;
  241. /* Fallthrough. */
  242. default:
  243. break;
  244. }
  245. /* Check an overflow. */
  246. if (linux_mem_size > (~0UL >> shift))
  247. linux_mem_size = 0;
  248. else
  249. linux_mem_size <<= shift;
  250. }
  251. }
  252. relocator = grub_relocator_new ();
  253. if (!relocator)
  254. goto fail;
  255. {
  256. grub_relocator_chunk_t ch;
  257. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  258. grub_linux_real_target,
  259. GRUB_LINUX_CL_OFFSET
  260. + maximal_cmdline_size);
  261. if (err)
  262. return err;
  263. grub_linux_real_chunk = get_virtual_current_address (ch);
  264. }
  265. /* Put the real mode code at the temporary address. */
  266. grub_memmove (grub_linux_real_chunk, &lh, sizeof (lh));
  267. len = real_size + GRUB_DISK_SECTOR_SIZE - sizeof (lh);
  268. if (grub_file_read (file, grub_linux_real_chunk + sizeof (lh), len) != len)
  269. {
  270. if (!grub_errno)
  271. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  272. argv[0]);
  273. goto fail;
  274. }
  275. if (lh.header != grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
  276. || grub_le_to_cpu16 (lh.version) < 0x0200)
  277. /* Clear the heap space. */
  278. grub_memset (grub_linux_real_chunk
  279. + ((setup_sects + 1) << GRUB_DISK_SECTOR_BITS),
  280. 0,
  281. ((GRUB_LINUX_MAX_SETUP_SECTS - setup_sects - 1)
  282. << GRUB_DISK_SECTOR_BITS));
  283. /* Create kernel command line. */
  284. grub_memcpy ((char *)grub_linux_real_chunk + GRUB_LINUX_CL_OFFSET,
  285. LINUX_IMAGE, sizeof (LINUX_IMAGE));
  286. err = grub_create_loader_cmdline (argc, argv,
  287. (char *)grub_linux_real_chunk
  288. + GRUB_LINUX_CL_OFFSET + sizeof (LINUX_IMAGE) - 1,
  289. maximal_cmdline_size
  290. - (sizeof (LINUX_IMAGE) - 1),
  291. GRUB_VERIFY_KERNEL_CMDLINE);
  292. if (err)
  293. goto fail;
  294. if (grub_linux_is_bzimage)
  295. grub_linux_prot_target = GRUB_LINUX_BZIMAGE_ADDR;
  296. else
  297. grub_linux_prot_target = GRUB_LINUX_ZIMAGE_ADDR;
  298. {
  299. grub_relocator_chunk_t ch;
  300. err = grub_relocator_alloc_chunk_addr (relocator, &ch,
  301. grub_linux_prot_target,
  302. grub_linux16_prot_size);
  303. if (err)
  304. return err;
  305. grub_linux_prot_chunk = get_virtual_current_address (ch);
  306. }
  307. len = grub_linux16_prot_size;
  308. if (grub_file_read (file, grub_linux_prot_chunk, len) != len && !grub_errno)
  309. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  310. argv[0]);
  311. if (grub_errno == GRUB_ERR_NONE)
  312. {
  313. grub_loader_set (grub_linux16_boot, grub_linux_unload, 0);
  314. loaded = 1;
  315. }
  316. fail:
  317. if (file)
  318. grub_file_close (file);
  319. if (grub_errno != GRUB_ERR_NONE)
  320. {
  321. grub_dl_unref (my_mod);
  322. loaded = 0;
  323. grub_relocator_unload (relocator);
  324. }
  325. return grub_errno;
  326. }
  327. static grub_err_t
  328. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  329. int argc, char *argv[])
  330. {
  331. grub_size_t size = 0;
  332. grub_addr_t addr_max, addr_min;
  333. struct linux_i386_kernel_header *lh;
  334. grub_uint8_t *initrd_chunk;
  335. grub_addr_t initrd_addr;
  336. grub_err_t err;
  337. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  338. if (argc == 0)
  339. {
  340. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  341. goto fail;
  342. }
  343. if (!loaded)
  344. {
  345. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("you need to load the kernel first"));
  346. goto fail;
  347. }
  348. lh = (struct linux_i386_kernel_header *) grub_linux_real_chunk;
  349. if (!(lh->header == grub_cpu_to_le32_compile_time (GRUB_LINUX_I386_MAGIC_SIGNATURE)
  350. && grub_le_to_cpu16 (lh->version) >= 0x0200))
  351. {
  352. grub_error (GRUB_ERR_BAD_OS, "the kernel is too old for initrd");
  353. goto fail;
  354. }
  355. /* Get the highest address available for the initrd. */
  356. if (grub_le_to_cpu16 (lh->version) >= 0x0203)
  357. {
  358. addr_max = grub_cpu_to_le32 (lh->initrd_addr_max);
  359. /* XXX in reality, Linux specifies a bogus value, so
  360. it is necessary to make sure that ADDR_MAX does not exceed
  361. 0x3fffffff. */
  362. if (addr_max > GRUB_LINUX_INITRD_MAX_ADDRESS)
  363. addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
  364. }
  365. else
  366. addr_max = GRUB_LINUX_INITRD_MAX_ADDRESS;
  367. if (linux_mem_size != 0 && linux_mem_size < addr_max)
  368. addr_max = linux_mem_size;
  369. /* Linux 2.3.xx has a bug in the memory range check, so avoid
  370. the last page.
  371. Linux 2.2.xx has a bug in the memory range check, which is
  372. worse than that of Linux 2.3.xx, so avoid the last 64kb. */
  373. addr_max -= 0x10000;
  374. addr_min = GRUB_LINUX_BZIMAGE_ADDR + grub_linux16_prot_size;
  375. if (grub_initrd_init (argc, argv, &initrd_ctx))
  376. goto fail;
  377. size = grub_get_initrd_size (&initrd_ctx);
  378. {
  379. grub_relocator_chunk_t ch;
  380. err = grub_relocator_alloc_chunk_align (relocator, &ch,
  381. addr_min, addr_max - size,
  382. size, 0x1000,
  383. GRUB_RELOCATOR_PREFERENCE_HIGH, 0);
  384. if (err)
  385. return err;
  386. initrd_chunk = get_virtual_current_address (ch);
  387. initrd_addr = get_physical_target_address (ch);
  388. }
  389. if (grub_initrd_load (&initrd_ctx, argv, initrd_chunk))
  390. goto fail;
  391. lh->ramdisk_image = initrd_addr;
  392. lh->ramdisk_size = size;
  393. fail:
  394. grub_initrd_close (&initrd_ctx);
  395. return grub_errno;
  396. }
  397. static grub_command_t cmd_linux, cmd_initrd;
  398. GRUB_MOD_INIT(linux16)
  399. {
  400. cmd_linux =
  401. grub_register_command ("linux16", grub_cmd_linux,
  402. 0, N_("Load Linux."));
  403. cmd_initrd =
  404. grub_register_command ("initrd16", grub_cmd_initrd,
  405. 0, N_("Load initrd."));
  406. my_mod = mod;
  407. }
  408. GRUB_MOD_FINI(linux16)
  409. {
  410. grub_unregister_command (cmd_linux);
  411. grub_unregister_command (cmd_initrd);
  412. }