linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* linux.c - boot Linux */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 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/dl.h>
  20. #include <grub/fdt.h>
  21. #include <grub/file.h>
  22. #include <grub/loader.h>
  23. #include <grub/mm.h>
  24. #include <grub/misc.h>
  25. #include <grub/command.h>
  26. #include <grub/cache.h>
  27. #include <grub/cpu/linux.h>
  28. #include <grub/efi/efi.h>
  29. #include <grub/lib/cmdline.h>
  30. #include <grub/linux.h>
  31. #include <grub/verify.h>
  32. GRUB_MOD_LICENSE ("GPLv3+");
  33. static grub_dl_t my_mod;
  34. static grub_addr_t initrd_start;
  35. static grub_addr_t initrd_end;
  36. static grub_addr_t linux_addr;
  37. static grub_size_t linux_size;
  38. static char *linux_args;
  39. static grub_uint32_t machine_type;
  40. static const void *current_fdt;
  41. typedef void (*kernel_entry_t) (int, unsigned long, void *);
  42. #define LINUX_PHYS_OFFSET (0x00008000)
  43. #define LINUX_INITRD_PHYS_OFFSET (LINUX_PHYS_OFFSET + 0x03000000)
  44. #define LINUX_FDT_PHYS_OFFSET (LINUX_INITRD_PHYS_OFFSET - 0x10000)
  45. static grub_size_t
  46. get_atag_size (const grub_uint32_t *atag)
  47. {
  48. const grub_uint32_t *atag0 = atag;
  49. while (atag[0] && atag[1])
  50. atag += atag[0];
  51. return atag - atag0;
  52. }
  53. /*
  54. * linux_prepare_fdt():
  55. * Prepares a loaded FDT for being passed to Linux.
  56. * Merges in command line parameters and sets up initrd addresses.
  57. */
  58. static grub_err_t
  59. linux_prepare_atag (void *target_atag)
  60. {
  61. const grub_uint32_t *atag_orig = (const grub_uint32_t *) current_fdt;
  62. grub_uint32_t *tmp_atag, *to;
  63. const grub_uint32_t *from;
  64. grub_size_t tmp_size;
  65. grub_size_t arg_size = grub_strlen (linux_args);
  66. char *cmdline_orig = NULL;
  67. grub_size_t cmdline_orig_len = 0;
  68. /* some place for cmdline, initrd and terminator. */
  69. tmp_size = get_atag_size (atag_orig) + 20 + (arg_size) / 4;
  70. tmp_atag = grub_calloc (tmp_size, sizeof (grub_uint32_t));
  71. if (!tmp_atag)
  72. return grub_errno;
  73. for (from = atag_orig, to = tmp_atag; from[0] && from[1];
  74. from += from[0])
  75. switch (from[1])
  76. {
  77. case 0x54410004:
  78. case 0x54410005:
  79. case 0x54420005:
  80. break;
  81. case 0x54410009:
  82. if (*(char *) (from + 2))
  83. {
  84. cmdline_orig = (char *) (from + 2);
  85. cmdline_orig_len = grub_strlen (cmdline_orig) + 1;
  86. }
  87. break;
  88. default:
  89. grub_memcpy (to, from, sizeof (grub_uint32_t) * from[0]);
  90. to += from[0];
  91. break;
  92. }
  93. grub_dprintf ("linux", "linux inherited args: '%s'\n",
  94. cmdline_orig ? : "");
  95. grub_dprintf ("linux", "linux_args: '%s'\n", linux_args);
  96. /* Generate and set command line */
  97. to[0] = 3 + (arg_size + cmdline_orig_len) / 4;
  98. to[1] = 0x54410009;
  99. if (cmdline_orig)
  100. {
  101. grub_memcpy ((char *) to + 8, cmdline_orig, cmdline_orig_len - 1);
  102. *((char *) to + 8 + cmdline_orig_len - 1) = ' ';
  103. }
  104. grub_memcpy ((char *) to + 8 + cmdline_orig_len, linux_args, arg_size);
  105. grub_memset ((char *) to + 8 + cmdline_orig_len + arg_size, 0,
  106. 4 - ((arg_size + cmdline_orig_len) & 3));
  107. to += to[0];
  108. if (initrd_start && initrd_end)
  109. {
  110. /*
  111. * We're using physical addresses, so even if we have LPAE, we're
  112. * restricted to a 32-bit address space.
  113. */
  114. grub_dprintf ("loader", "Initrd @ 0x%08x-0x%08x\n",
  115. initrd_start, initrd_end);
  116. to[0] = 4;
  117. to[1] = 0x54420005;
  118. to[2] = initrd_start;
  119. to[3] = initrd_end - initrd_start;
  120. to += 4;
  121. }
  122. to[0] = 0;
  123. to[1] = 0;
  124. to += 2;
  125. /* Copy updated FDT to its launch location */
  126. grub_memcpy (target_atag, tmp_atag, sizeof (grub_uint32_t) * (to - tmp_atag));
  127. grub_free (tmp_atag);
  128. grub_dprintf ("loader", "ATAG updated for Linux boot\n");
  129. return GRUB_ERR_NONE;
  130. }
  131. /*
  132. * linux_prepare_fdt():
  133. * Prepares a loaded FDT for being passed to Linux.
  134. * Merges in command line parameters and sets up initrd addresses.
  135. */
  136. static grub_err_t
  137. linux_prepare_fdt (void *target_fdt)
  138. {
  139. int node;
  140. int retval;
  141. int tmp_size;
  142. void *tmp_fdt;
  143. tmp_size = grub_fdt_get_totalsize (current_fdt) + 0x100 + grub_strlen (linux_args);
  144. tmp_fdt = grub_malloc (tmp_size);
  145. if (!tmp_fdt)
  146. return grub_errno;
  147. grub_memcpy (tmp_fdt, current_fdt, grub_fdt_get_totalsize (current_fdt));
  148. grub_fdt_set_totalsize (tmp_fdt, tmp_size);
  149. /* Find or create '/chosen' node */
  150. node = grub_fdt_find_subnode (tmp_fdt, 0, "chosen");
  151. if (node < 0)
  152. {
  153. grub_dprintf ("linux", "No 'chosen' node in FDT - creating.\n");
  154. node = grub_fdt_add_subnode (tmp_fdt, 0, "chosen");
  155. if (node < 0)
  156. goto failure;
  157. }
  158. grub_dprintf ("linux", "linux_args: '%s'\n", linux_args);
  159. /* Generate and set command line */
  160. retval = grub_fdt_set_prop (tmp_fdt, node, "bootargs", linux_args,
  161. grub_strlen (linux_args) + 1);
  162. if (retval)
  163. goto failure;
  164. if (initrd_start && initrd_end)
  165. {
  166. /*
  167. * We're using physical addresses, so even if we have LPAE, we're
  168. * restricted to a 32-bit address space.
  169. */
  170. grub_dprintf ("loader", "Initrd @ 0x%08x-0x%08x\n",
  171. initrd_start, initrd_end);
  172. retval = grub_fdt_set_prop32 (tmp_fdt, node, "linux,initrd-start",
  173. initrd_start);
  174. if (retval)
  175. goto failure;
  176. retval = grub_fdt_set_prop32 (tmp_fdt, node, "linux,initrd-end",
  177. initrd_end);
  178. if (retval)
  179. goto failure;
  180. }
  181. /* Copy updated FDT to its launch location */
  182. grub_memcpy (target_fdt, tmp_fdt, tmp_size);
  183. grub_free (tmp_fdt);
  184. grub_dprintf ("loader", "FDT updated for Linux boot\n");
  185. return GRUB_ERR_NONE;
  186. failure:
  187. grub_free (tmp_fdt);
  188. return grub_error (GRUB_ERR_BAD_ARGUMENT, "unable to prepare FDT");
  189. }
  190. static grub_err_t
  191. linux_boot (void)
  192. {
  193. kernel_entry_t linuxmain;
  194. int fdt_valid, atag_valid;
  195. void *target_fdt = 0;
  196. fdt_valid = (current_fdt && grub_fdt_check_header_nosize (current_fdt) == 0);
  197. atag_valid = ((((const grub_uint16_t *) current_fdt)[3] & ~3) == 0x5440
  198. && *((const grub_uint32_t *) current_fdt));
  199. grub_dprintf ("loader", "atag: %p, %x, %x, %s, %s\n",
  200. current_fdt,
  201. ((const grub_uint16_t *) current_fdt)[3],
  202. *((const grub_uint32_t *) current_fdt),
  203. (const char *) current_fdt,
  204. (const char *) current_fdt + 1);
  205. if (!fdt_valid && machine_type == GRUB_ARM_MACHINE_TYPE_FDT)
  206. return grub_error (GRUB_ERR_FILE_NOT_FOUND,
  207. N_("device tree must be supplied (see `devicetree' command)"));
  208. grub_arch_sync_caches ((void *) linux_addr, linux_size);
  209. grub_dprintf ("loader", "Kernel at: 0x%x\n", linux_addr);
  210. if (fdt_valid || atag_valid)
  211. {
  212. #ifdef GRUB_MACHINE_EFI
  213. grub_size_t size;
  214. if (fdt_valid)
  215. size = grub_fdt_get_totalsize (current_fdt);
  216. else
  217. size = 4 * get_atag_size (current_fdt);
  218. size += grub_strlen (linux_args) + 256;
  219. target_fdt = grub_efi_allocate_loader_memory (LINUX_FDT_PHYS_OFFSET, size);
  220. if (!target_fdt)
  221. return grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  222. #else
  223. target_fdt = (void *) LINUX_FDT_ADDRESS;
  224. #endif
  225. }
  226. if (fdt_valid)
  227. {
  228. grub_err_t err;
  229. err = linux_prepare_fdt (target_fdt);
  230. if (err)
  231. return err;
  232. grub_dprintf ("loader", "FDT @ %p\n", target_fdt);
  233. }
  234. else if (atag_valid)
  235. {
  236. grub_err_t err;
  237. err = linux_prepare_atag (target_fdt);
  238. if (err)
  239. return err;
  240. grub_dprintf ("loader", "ATAG @ %p\n", target_fdt);
  241. }
  242. grub_dprintf ("loader", "Jumping to Linux...\n");
  243. /* Boot the kernel.
  244. * Arguments to kernel:
  245. * r0 - 0
  246. * r1 - machine type
  247. * r2 - address of DTB
  248. */
  249. linuxmain = (kernel_entry_t) linux_addr;
  250. grub_arm_disable_caches_mmu ();
  251. linuxmain (0, machine_type, target_fdt);
  252. return grub_error (GRUB_ERR_BAD_OS, "Linux call returned");
  253. }
  254. /*
  255. * Only support zImage, so no relocations necessary
  256. */
  257. static grub_err_t
  258. linux_load (const char *filename, grub_file_t file)
  259. {
  260. struct linux_arch_kernel_header *lh;
  261. int size;
  262. size = grub_file_size (file);
  263. linux_addr = LINUX_ADDRESS;
  264. grub_dprintf ("loader", "Loading Linux to 0x%08x\n",
  265. (grub_addr_t) linux_addr);
  266. if (grub_file_read (file, (void *) linux_addr, size) != size)
  267. {
  268. if (!grub_errno)
  269. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  270. filename);
  271. return grub_errno;
  272. }
  273. lh = (void *) linux_addr;
  274. if ((grub_size_t) size > sizeof (*lh) &&
  275. lh->magic == GRUB_LINUX_ARM_MAGIC_SIGNATURE)
  276. ;
  277. else if (size > 0x8000 && *(grub_uint32_t *) (linux_addr) == 0xea000006
  278. && machine_type == GRUB_ARM_MACHINE_TYPE_RASPBERRY_PI)
  279. grub_memmove ((void *) linux_addr, (void *) (linux_addr + 0x8000),
  280. size - 0x8000);
  281. else
  282. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("invalid zImage"));
  283. linux_size = size;
  284. return GRUB_ERR_NONE;
  285. }
  286. static grub_err_t
  287. linux_unload (void)
  288. {
  289. grub_dl_unref (my_mod);
  290. grub_free (linux_args);
  291. linux_args = NULL;
  292. initrd_start = initrd_end = 0;
  293. return GRUB_ERR_NONE;
  294. }
  295. static grub_err_t
  296. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  297. int argc, char *argv[])
  298. {
  299. int size;
  300. grub_err_t err;
  301. grub_file_t file;
  302. grub_dl_ref (my_mod);
  303. if (argc == 0)
  304. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  305. file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_KERNEL);
  306. if (!file)
  307. goto fail;
  308. err = linux_load (argv[0], file);
  309. grub_file_close (file);
  310. if (err)
  311. goto fail;
  312. grub_loader_set (linux_boot, linux_unload, 0);
  313. size = grub_loader_cmdline_size (argc, argv);
  314. linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
  315. if (!linux_args)
  316. {
  317. grub_loader_unset();
  318. goto fail;
  319. }
  320. /* Create kernel command line. */
  321. grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
  322. err = grub_create_loader_cmdline (argc, argv,
  323. linux_args + sizeof (LINUX_IMAGE) - 1, size,
  324. GRUB_VERIFY_KERNEL_CMDLINE);
  325. if (err)
  326. goto fail;
  327. return GRUB_ERR_NONE;
  328. fail:
  329. grub_dl_unref (my_mod);
  330. return grub_errno;
  331. }
  332. static grub_err_t
  333. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  334. int argc, char *argv[])
  335. {
  336. grub_file_t file;
  337. grub_size_t size = 0;
  338. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  339. if (argc == 0)
  340. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  341. file = grub_file_open (argv[0], GRUB_FILE_TYPE_LINUX_INITRD);
  342. if (!file)
  343. return grub_errno;
  344. if (grub_initrd_init (argc, argv, &initrd_ctx))
  345. goto fail;
  346. size = grub_get_initrd_size (&initrd_ctx);
  347. initrd_start = LINUX_INITRD_ADDRESS;
  348. grub_dprintf ("loader", "Loading initrd to 0x%08x\n",
  349. (grub_addr_t) initrd_start);
  350. if (grub_initrd_load (&initrd_ctx, (void *) initrd_start))
  351. goto fail;
  352. initrd_end = initrd_start + size;
  353. return GRUB_ERR_NONE;
  354. fail:
  355. grub_file_close (file);
  356. return grub_errno;
  357. }
  358. static grub_err_t
  359. load_dtb (grub_file_t dtb, int size)
  360. {
  361. void *new_fdt = grub_zalloc (size);
  362. if (!new_fdt)
  363. return grub_errno;
  364. grub_dprintf ("loader", "Loading device tree to %p\n",
  365. new_fdt);
  366. if ((grub_file_read (dtb, new_fdt, size) != size)
  367. || (grub_fdt_check_header (new_fdt, size) != 0))
  368. {
  369. grub_free (new_fdt);
  370. return grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
  371. }
  372. grub_fdt_set_totalsize (new_fdt, size);
  373. current_fdt = new_fdt;
  374. /*
  375. * We've successfully loaded an FDT, so any machine type passed
  376. * from firmware is now obsolete.
  377. */
  378. machine_type = GRUB_ARM_MACHINE_TYPE_FDT;
  379. return GRUB_ERR_NONE;
  380. }
  381. static grub_err_t
  382. grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
  383. int argc, char *argv[])
  384. {
  385. grub_file_t dtb;
  386. int size;
  387. if (argc != 1)
  388. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  389. dtb = grub_file_open (argv[0], GRUB_FILE_TYPE_DEVICE_TREE_IMAGE);
  390. if (!dtb)
  391. return grub_errno;
  392. size = grub_file_size (dtb);
  393. if (size == 0)
  394. grub_error (GRUB_ERR_BAD_OS, "empty file");
  395. else
  396. load_dtb (dtb, size);
  397. grub_file_close (dtb);
  398. return grub_errno;
  399. }
  400. static grub_command_t cmd_linux, cmd_initrd, cmd_devicetree;
  401. GRUB_MOD_INIT (linux)
  402. {
  403. cmd_linux = grub_register_command ("linux", grub_cmd_linux,
  404. 0, N_("Load Linux."));
  405. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  406. 0, N_("Load initrd."));
  407. cmd_devicetree = grub_register_command_lockdown ("devicetree", grub_cmd_devicetree,
  408. /* TRANSLATORS: DTB stands for device tree blob. */
  409. 0, N_("Load DTB file."));
  410. my_mod = mod;
  411. current_fdt = (const void *) grub_arm_firmware_get_boot_data ();
  412. machine_type = grub_arm_firmware_get_machine_type ();
  413. }
  414. GRUB_MOD_FINI (linux)
  415. {
  416. grub_unregister_command (cmd_linux);
  417. grub_unregister_command (cmd_initrd);
  418. grub_unregister_command (cmd_devicetree);
  419. }