arm_boot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * ARM kernel loader.
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. #include "config.h"
  10. #include "hw.h"
  11. #include "arm-misc.h"
  12. #include "sysemu.h"
  13. #include "boards.h"
  14. #include "loader.h"
  15. #include "elf.h"
  16. #include "device_tree.h"
  17. #define KERNEL_ARGS_ADDR 0x100
  18. #define KERNEL_LOAD_ADDR 0x00010000
  19. #define INITRD_LOAD_ADDR 0x00d00000
  20. /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
  21. static uint32_t bootloader[] = {
  22. 0xe3a00000, /* mov r0, #0 */
  23. 0xe59f1004, /* ldr r1, [pc, #4] */
  24. 0xe59f2004, /* ldr r2, [pc, #4] */
  25. 0xe59ff004, /* ldr pc, [pc, #4] */
  26. 0, /* Board ID */
  27. 0, /* Address of kernel args. Set by integratorcp_init. */
  28. 0 /* Kernel entry point. Set by integratorcp_init. */
  29. };
  30. /* Handling for secondary CPU boot in a multicore system.
  31. * Unlike the uniprocessor/primary CPU boot, this is platform
  32. * dependent. The default code here is based on the secondary
  33. * CPU boot protocol used on realview/vexpress boards, with
  34. * some parameterisation to increase its flexibility.
  35. * QEMU platform models for which this code is not appropriate
  36. * should override write_secondary_boot and secondary_cpu_reset_hook
  37. * instead.
  38. *
  39. * This code enables the interrupt controllers for the secondary
  40. * CPUs and then puts all the secondary CPUs into a loop waiting
  41. * for an interprocessor interrupt and polling a configurable
  42. * location for the kernel secondary CPU entry point.
  43. */
  44. static uint32_t smpboot[] = {
  45. 0xe59f201c, /* ldr r2, gic_cpu_if */
  46. 0xe59f001c, /* ldr r0, startaddr */
  47. 0xe3a01001, /* mov r1, #1 */
  48. 0xe5821000, /* str r1, [r2] */
  49. 0xe320f003, /* wfi */
  50. 0xe5901000, /* ldr r1, [r0] */
  51. 0xe1110001, /* tst r1, r1 */
  52. 0x0afffffb, /* beq <wfi> */
  53. 0xe12fff11, /* bx r1 */
  54. 0, /* gic_cpu_if: base address of GIC CPU interface */
  55. 0 /* bootreg: Boot register address is held here */
  56. };
  57. static void default_write_secondary(ARMCPU *cpu,
  58. const struct arm_boot_info *info)
  59. {
  60. int n;
  61. smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
  62. smpboot[ARRAY_SIZE(smpboot) - 2] = info->gic_cpu_if_addr;
  63. for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
  64. smpboot[n] = tswap32(smpboot[n]);
  65. }
  66. rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
  67. info->smp_loader_start);
  68. }
  69. static void default_reset_secondary(ARMCPU *cpu,
  70. const struct arm_boot_info *info)
  71. {
  72. CPUARMState *env = &cpu->env;
  73. stl_phys_notdirty(info->smp_bootreg_addr, 0);
  74. env->regs[15] = info->smp_loader_start;
  75. }
  76. #define WRITE_WORD(p, value) do { \
  77. stl_phys_notdirty(p, value); \
  78. p += 4; \
  79. } while (0)
  80. static void set_kernel_args(const struct arm_boot_info *info)
  81. {
  82. int initrd_size = info->initrd_size;
  83. target_phys_addr_t base = info->loader_start;
  84. target_phys_addr_t p;
  85. p = base + KERNEL_ARGS_ADDR;
  86. /* ATAG_CORE */
  87. WRITE_WORD(p, 5);
  88. WRITE_WORD(p, 0x54410001);
  89. WRITE_WORD(p, 1);
  90. WRITE_WORD(p, 0x1000);
  91. WRITE_WORD(p, 0);
  92. /* ATAG_MEM */
  93. /* TODO: handle multiple chips on one ATAG list */
  94. WRITE_WORD(p, 4);
  95. WRITE_WORD(p, 0x54410002);
  96. WRITE_WORD(p, info->ram_size);
  97. WRITE_WORD(p, info->loader_start);
  98. if (initrd_size) {
  99. /* ATAG_INITRD2 */
  100. WRITE_WORD(p, 4);
  101. WRITE_WORD(p, 0x54420005);
  102. WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
  103. WRITE_WORD(p, initrd_size);
  104. }
  105. if (info->kernel_cmdline && *info->kernel_cmdline) {
  106. /* ATAG_CMDLINE */
  107. int cmdline_size;
  108. cmdline_size = strlen(info->kernel_cmdline);
  109. cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
  110. cmdline_size + 1);
  111. cmdline_size = (cmdline_size >> 2) + 1;
  112. WRITE_WORD(p, cmdline_size + 2);
  113. WRITE_WORD(p, 0x54410009);
  114. p += cmdline_size * 4;
  115. }
  116. if (info->atag_board) {
  117. /* ATAG_BOARD */
  118. int atag_board_len;
  119. uint8_t atag_board_buf[0x1000];
  120. atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
  121. WRITE_WORD(p, (atag_board_len + 8) >> 2);
  122. WRITE_WORD(p, 0x414f4d50);
  123. cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
  124. p += atag_board_len;
  125. }
  126. /* ATAG_END */
  127. WRITE_WORD(p, 0);
  128. WRITE_WORD(p, 0);
  129. }
  130. static void set_kernel_args_old(const struct arm_boot_info *info)
  131. {
  132. target_phys_addr_t p;
  133. const char *s;
  134. int initrd_size = info->initrd_size;
  135. target_phys_addr_t base = info->loader_start;
  136. /* see linux/include/asm-arm/setup.h */
  137. p = base + KERNEL_ARGS_ADDR;
  138. /* page_size */
  139. WRITE_WORD(p, 4096);
  140. /* nr_pages */
  141. WRITE_WORD(p, info->ram_size / 4096);
  142. /* ramdisk_size */
  143. WRITE_WORD(p, 0);
  144. #define FLAG_READONLY 1
  145. #define FLAG_RDLOAD 4
  146. #define FLAG_RDPROMPT 8
  147. /* flags */
  148. WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
  149. /* rootdev */
  150. WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
  151. /* video_num_cols */
  152. WRITE_WORD(p, 0);
  153. /* video_num_rows */
  154. WRITE_WORD(p, 0);
  155. /* video_x */
  156. WRITE_WORD(p, 0);
  157. /* video_y */
  158. WRITE_WORD(p, 0);
  159. /* memc_control_reg */
  160. WRITE_WORD(p, 0);
  161. /* unsigned char sounddefault */
  162. /* unsigned char adfsdrives */
  163. /* unsigned char bytes_per_char_h */
  164. /* unsigned char bytes_per_char_v */
  165. WRITE_WORD(p, 0);
  166. /* pages_in_bank[4] */
  167. WRITE_WORD(p, 0);
  168. WRITE_WORD(p, 0);
  169. WRITE_WORD(p, 0);
  170. WRITE_WORD(p, 0);
  171. /* pages_in_vram */
  172. WRITE_WORD(p, 0);
  173. /* initrd_start */
  174. if (initrd_size)
  175. WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
  176. else
  177. WRITE_WORD(p, 0);
  178. /* initrd_size */
  179. WRITE_WORD(p, initrd_size);
  180. /* rd_start */
  181. WRITE_WORD(p, 0);
  182. /* system_rev */
  183. WRITE_WORD(p, 0);
  184. /* system_serial_low */
  185. WRITE_WORD(p, 0);
  186. /* system_serial_high */
  187. WRITE_WORD(p, 0);
  188. /* mem_fclk_21285 */
  189. WRITE_WORD(p, 0);
  190. /* zero unused fields */
  191. while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
  192. WRITE_WORD(p, 0);
  193. }
  194. s = info->kernel_cmdline;
  195. if (s) {
  196. cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
  197. } else {
  198. WRITE_WORD(p, 0);
  199. }
  200. }
  201. static int load_dtb(target_phys_addr_t addr, const struct arm_boot_info *binfo)
  202. {
  203. #ifdef CONFIG_FDT
  204. uint32_t *mem_reg_property;
  205. uint32_t mem_reg_propsize;
  206. void *fdt = NULL;
  207. char *filename;
  208. int size, rc;
  209. uint32_t acells, scells, hival;
  210. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
  211. if (!filename) {
  212. fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
  213. return -1;
  214. }
  215. fdt = load_device_tree(filename, &size);
  216. if (!fdt) {
  217. fprintf(stderr, "Couldn't open dtb file %s\n", filename);
  218. g_free(filename);
  219. return -1;
  220. }
  221. g_free(filename);
  222. acells = qemu_devtree_getprop_cell(fdt, "/", "#address-cells");
  223. scells = qemu_devtree_getprop_cell(fdt, "/", "#size-cells");
  224. if (acells == 0 || scells == 0) {
  225. fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
  226. return -1;
  227. }
  228. mem_reg_propsize = acells + scells;
  229. mem_reg_property = g_new0(uint32_t, mem_reg_propsize);
  230. mem_reg_property[acells - 1] = cpu_to_be32(binfo->loader_start);
  231. hival = cpu_to_be32(binfo->loader_start >> 32);
  232. if (acells > 1) {
  233. mem_reg_property[acells - 2] = hival;
  234. } else if (hival != 0) {
  235. fprintf(stderr, "qemu: dtb file not compatible with "
  236. "RAM start address > 4GB\n");
  237. exit(1);
  238. }
  239. mem_reg_property[acells + scells - 1] = cpu_to_be32(binfo->ram_size);
  240. hival = cpu_to_be32(binfo->ram_size >> 32);
  241. if (scells > 1) {
  242. mem_reg_property[acells + scells - 2] = hival;
  243. } else if (hival != 0) {
  244. fprintf(stderr, "qemu: dtb file not compatible with "
  245. "RAM size > 4GB\n");
  246. exit(1);
  247. }
  248. rc = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
  249. mem_reg_propsize * sizeof(uint32_t));
  250. if (rc < 0) {
  251. fprintf(stderr, "couldn't set /memory/reg\n");
  252. }
  253. if (binfo->kernel_cmdline && *binfo->kernel_cmdline) {
  254. rc = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
  255. binfo->kernel_cmdline);
  256. if (rc < 0) {
  257. fprintf(stderr, "couldn't set /chosen/bootargs\n");
  258. }
  259. }
  260. if (binfo->initrd_size) {
  261. rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  262. binfo->loader_start + INITRD_LOAD_ADDR);
  263. if (rc < 0) {
  264. fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
  265. }
  266. rc = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
  267. binfo->loader_start + INITRD_LOAD_ADDR +
  268. binfo->initrd_size);
  269. if (rc < 0) {
  270. fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
  271. }
  272. }
  273. cpu_physical_memory_write(addr, fdt, size);
  274. return 0;
  275. #else
  276. fprintf(stderr, "Device tree requested, "
  277. "but qemu was compiled without fdt support\n");
  278. return -1;
  279. #endif
  280. }
  281. static void do_cpu_reset(void *opaque)
  282. {
  283. ARMCPU *cpu = opaque;
  284. CPUARMState *env = &cpu->env;
  285. const struct arm_boot_info *info = env->boot_info;
  286. cpu_reset(CPU(cpu));
  287. if (info) {
  288. if (!info->is_linux) {
  289. /* Jump to the entry point. */
  290. env->regs[15] = info->entry & 0xfffffffe;
  291. env->thumb = info->entry & 1;
  292. } else {
  293. if (env == first_cpu) {
  294. env->regs[15] = info->loader_start;
  295. if (!info->dtb_filename) {
  296. if (old_param) {
  297. set_kernel_args_old(info);
  298. } else {
  299. set_kernel_args(info);
  300. }
  301. }
  302. } else {
  303. info->secondary_cpu_reset_hook(cpu, info);
  304. }
  305. }
  306. }
  307. }
  308. void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
  309. {
  310. CPUARMState *env = &cpu->env;
  311. int kernel_size;
  312. int initrd_size;
  313. int n;
  314. int is_linux = 0;
  315. uint64_t elf_entry;
  316. target_phys_addr_t entry;
  317. int big_endian;
  318. QemuOpts *machine_opts;
  319. /* Load the kernel. */
  320. if (!info->kernel_filename) {
  321. fprintf(stderr, "Kernel image must be specified\n");
  322. exit(1);
  323. }
  324. machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  325. if (machine_opts) {
  326. info->dtb_filename = qemu_opt_get(machine_opts, "dtb");
  327. } else {
  328. info->dtb_filename = NULL;
  329. }
  330. if (!info->secondary_cpu_reset_hook) {
  331. info->secondary_cpu_reset_hook = default_reset_secondary;
  332. }
  333. if (!info->write_secondary_boot) {
  334. info->write_secondary_boot = default_write_secondary;
  335. }
  336. if (info->nb_cpus == 0)
  337. info->nb_cpus = 1;
  338. #ifdef TARGET_WORDS_BIGENDIAN
  339. big_endian = 1;
  340. #else
  341. big_endian = 0;
  342. #endif
  343. /* Assume that raw images are linux kernels, and ELF images are not. */
  344. kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
  345. NULL, NULL, big_endian, ELF_MACHINE, 1);
  346. entry = elf_entry;
  347. if (kernel_size < 0) {
  348. kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
  349. &is_linux);
  350. }
  351. if (kernel_size < 0) {
  352. entry = info->loader_start + KERNEL_LOAD_ADDR;
  353. kernel_size = load_image_targphys(info->kernel_filename, entry,
  354. info->ram_size - KERNEL_LOAD_ADDR);
  355. is_linux = 1;
  356. }
  357. if (kernel_size < 0) {
  358. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  359. info->kernel_filename);
  360. exit(1);
  361. }
  362. info->entry = entry;
  363. if (is_linux) {
  364. if (info->initrd_filename) {
  365. initrd_size = load_image_targphys(info->initrd_filename,
  366. info->loader_start
  367. + INITRD_LOAD_ADDR,
  368. info->ram_size
  369. - INITRD_LOAD_ADDR);
  370. if (initrd_size < 0) {
  371. fprintf(stderr, "qemu: could not load initrd '%s'\n",
  372. info->initrd_filename);
  373. exit(1);
  374. }
  375. } else {
  376. initrd_size = 0;
  377. }
  378. info->initrd_size = initrd_size;
  379. bootloader[4] = info->board_id;
  380. /* for device tree boot, we pass the DTB directly in r2. Otherwise
  381. * we point to the kernel args.
  382. */
  383. if (info->dtb_filename) {
  384. /* Place the DTB after the initrd in memory */
  385. target_phys_addr_t dtb_start = TARGET_PAGE_ALIGN(info->loader_start
  386. + INITRD_LOAD_ADDR
  387. + initrd_size);
  388. if (load_dtb(dtb_start, info)) {
  389. exit(1);
  390. }
  391. bootloader[5] = dtb_start;
  392. } else {
  393. bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
  394. if (info->ram_size >= (1ULL << 32)) {
  395. fprintf(stderr, "qemu: RAM size must be less than 4GB to boot"
  396. " Linux kernel using ATAGS (try passing a device tree"
  397. " using -dtb)\n");
  398. exit(1);
  399. }
  400. }
  401. bootloader[6] = entry;
  402. for (n = 0; n < sizeof(bootloader) / 4; n++) {
  403. bootloader[n] = tswap32(bootloader[n]);
  404. }
  405. rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
  406. info->loader_start);
  407. if (info->nb_cpus > 1) {
  408. info->write_secondary_boot(cpu, info);
  409. }
  410. }
  411. info->is_linux = is_linux;
  412. for (; env; env = env->next_cpu) {
  413. cpu = arm_env_get_cpu(env);
  414. env->boot_info = info;
  415. qemu_register_reset(do_cpu_reset, cpu);
  416. }
  417. }