linux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. GRUB_MOD_LICENSE ("GPLv3+");
  31. static grub_dl_t my_mod;
  32. static grub_addr_t initrd_start;
  33. static grub_addr_t initrd_end;
  34. static grub_addr_t linux_addr;
  35. static grub_size_t linux_size;
  36. static char *linux_args;
  37. static grub_uint32_t machine_type;
  38. static const void *current_fdt;
  39. typedef void (*kernel_entry_t) (int, unsigned long, void *);
  40. #define LINUX_ZIMAGE_OFFSET 0x24
  41. #define LINUX_ZIMAGE_MAGIC 0x016f2818
  42. #define LINUX_PHYS_OFFSET (0x00008000)
  43. #define LINUX_INITRD_PHYS_OFFSET (LINUX_PHYS_OFFSET + 0x02000000)
  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_malloc (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 (fdt_addr);
  216. else
  217. size = 4 * get_atag_size (atag_orig);
  218. size += grub_strlen (linux_args) + 256;
  219. target_fdt = grub_efi_allocate_loader_memory (LINUX_FDT_PHYS_OFFSET, size);
  220. if (!fdt_addr)
  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. #ifdef GRUB_MACHINE_EFI
  251. {
  252. grub_err_t err;
  253. err = grub_efi_prepare_platform();
  254. if (err != GRUB_ERR_NONE)
  255. return err;
  256. }
  257. #endif
  258. grub_arm_disable_caches_mmu ();
  259. linuxmain (0, machine_type, target_fdt);
  260. return grub_error (GRUB_ERR_BAD_OS, "Linux call returned");
  261. }
  262. /*
  263. * Only support zImage, so no relocations necessary
  264. */
  265. static grub_err_t
  266. linux_load (const char *filename, grub_file_t file)
  267. {
  268. int size;
  269. size = grub_file_size (file);
  270. #ifdef GRUB_MACHINE_EFI
  271. linux_addr = (grub_addr_t) grub_efi_allocate_loader_memory (LINUX_PHYS_OFFSET, size);
  272. if (!linux_addr)
  273. return grub_errno;
  274. #else
  275. linux_addr = LINUX_ADDRESS;
  276. #endif
  277. grub_dprintf ("loader", "Loading Linux to 0x%08x\n",
  278. (grub_addr_t) linux_addr);
  279. if (grub_file_read (file, (void *) linux_addr, size) != size)
  280. {
  281. if (!grub_errno)
  282. grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"),
  283. filename);
  284. return grub_errno;
  285. }
  286. if (size > LINUX_ZIMAGE_OFFSET + 4
  287. && *(grub_uint32_t *) (linux_addr + LINUX_ZIMAGE_OFFSET)
  288. == LINUX_ZIMAGE_MAGIC)
  289. ;
  290. else if (size > 0x8000 && *(grub_uint32_t *) (linux_addr) == 0xea000006
  291. && machine_type == GRUB_ARM_MACHINE_TYPE_RASPBERRY_PI)
  292. grub_memmove ((void *) linux_addr, (void *) (linux_addr + 0x8000),
  293. size - 0x8000);
  294. else
  295. return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("invalid zImage"));
  296. linux_size = size;
  297. return GRUB_ERR_NONE;
  298. }
  299. static grub_err_t
  300. linux_unload (void)
  301. {
  302. grub_dl_unref (my_mod);
  303. grub_free (linux_args);
  304. linux_args = NULL;
  305. initrd_start = initrd_end = 0;
  306. return GRUB_ERR_NONE;
  307. }
  308. static grub_err_t
  309. grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
  310. int argc, char *argv[])
  311. {
  312. int size;
  313. grub_err_t err;
  314. grub_file_t file;
  315. grub_dl_ref (my_mod);
  316. if (argc == 0)
  317. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  318. file = grub_file_open (argv[0]);
  319. if (!file)
  320. goto fail;
  321. err = linux_load (argv[0], file);
  322. grub_file_close (file);
  323. if (err)
  324. goto fail;
  325. grub_loader_set (linux_boot, linux_unload, 0);
  326. size = grub_loader_cmdline_size (argc, argv);
  327. linux_args = grub_malloc (size + sizeof (LINUX_IMAGE));
  328. if (!linux_args)
  329. {
  330. grub_loader_unset();
  331. goto fail;
  332. }
  333. /* Create kernel command line. */
  334. grub_memcpy (linux_args, LINUX_IMAGE, sizeof (LINUX_IMAGE));
  335. grub_create_loader_cmdline (argc, argv,
  336. linux_args + sizeof (LINUX_IMAGE) - 1, size);
  337. return GRUB_ERR_NONE;
  338. fail:
  339. grub_dl_unref (my_mod);
  340. return grub_errno;
  341. }
  342. static grub_err_t
  343. grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
  344. int argc, char *argv[])
  345. {
  346. grub_file_t file;
  347. grub_size_t size = 0;
  348. struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 };
  349. if (argc == 0)
  350. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  351. file = grub_file_open (argv[0]);
  352. if (!file)
  353. return grub_errno;
  354. if (grub_initrd_init (argc, argv, &initrd_ctx))
  355. goto fail;
  356. size = grub_get_initrd_size (&initrd_ctx);
  357. #ifdef GRUB_MACHINE_EFI
  358. if (initrd_start)
  359. grub_efi_free_pages (initrd_start,
  360. (initrd_end - initrd_start + 0xfff) >> 12);
  361. initrd_start = (grub_addr_t) grub_efi_allocate_loader_memory (LINUX_INITRD_PHYS_OFFSET, size);
  362. if (!initrd_start)
  363. {
  364. grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
  365. goto fail;
  366. }
  367. #else
  368. initrd_start = LINUX_INITRD_ADDRESS;
  369. #endif
  370. grub_dprintf ("loader", "Loading initrd to 0x%08x\n",
  371. (grub_addr_t) initrd_start);
  372. if (grub_initrd_load (&initrd_ctx, argv, (void *) initrd_start))
  373. goto fail;
  374. initrd_end = initrd_start + size;
  375. return GRUB_ERR_NONE;
  376. fail:
  377. grub_file_close (file);
  378. return grub_errno;
  379. }
  380. static grub_err_t
  381. load_dtb (grub_file_t dtb, int size)
  382. {
  383. void *new_fdt = grub_zalloc (size);
  384. if (!new_fdt)
  385. return grub_errno;
  386. grub_dprintf ("loader", "Loading device tree to %p\n",
  387. new_fdt);
  388. if ((grub_file_read (dtb, new_fdt, size) != size)
  389. || (grub_fdt_check_header (new_fdt, size) != 0))
  390. {
  391. grub_free (new_fdt);
  392. return grub_error (GRUB_ERR_BAD_OS, N_("invalid device tree"));
  393. }
  394. grub_fdt_set_totalsize (new_fdt, size);
  395. current_fdt = new_fdt;
  396. /*
  397. * We've successfully loaded an FDT, so any machine type passed
  398. * from firmware is now obsolete.
  399. */
  400. machine_type = GRUB_ARM_MACHINE_TYPE_FDT;
  401. return GRUB_ERR_NONE;
  402. }
  403. static grub_err_t
  404. grub_cmd_devicetree (grub_command_t cmd __attribute__ ((unused)),
  405. int argc, char *argv[])
  406. {
  407. grub_file_t dtb;
  408. int size;
  409. if (argc != 1)
  410. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  411. dtb = grub_file_open (argv[0]);
  412. if (!dtb)
  413. return grub_errno;
  414. size = grub_file_size (dtb);
  415. if (size == 0)
  416. grub_error (GRUB_ERR_BAD_OS, "empty file");
  417. else
  418. load_dtb (dtb, size);
  419. grub_file_close (dtb);
  420. return grub_errno;
  421. }
  422. static grub_command_t cmd_linux, cmd_initrd, cmd_devicetree;
  423. GRUB_MOD_INIT (linux)
  424. {
  425. cmd_linux = grub_register_command ("linux", grub_cmd_linux,
  426. 0, N_("Load Linux."));
  427. cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
  428. 0, N_("Load initrd."));
  429. cmd_devicetree = grub_register_command ("devicetree", grub_cmd_devicetree,
  430. /* TRANSLATORS: DTB stands for device tree blob. */
  431. 0, N_("Load DTB file."));
  432. my_mod = mod;
  433. current_fdt = grub_arm_firmware_get_boot_data ();
  434. machine_type = grub_arm_firmware_get_machine_type ();
  435. }
  436. GRUB_MOD_FINI (linux)
  437. {
  438. grub_unregister_command (cmd_linux);
  439. grub_unregister_command (cmd_initrd);
  440. grub_unregister_command (cmd_devicetree);
  441. }