linux.c 13 KB

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