linux.c 13 KB

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