2
0

arm_boot.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "hw.h"
  10. #include "arm-misc.h"
  11. #include "sysemu.h"
  12. #include "loader.h"
  13. #include "elf.h"
  14. #define KERNEL_ARGS_ADDR 0x100
  15. #define KERNEL_LOAD_ADDR 0x00010000
  16. #define INITRD_LOAD_ADDR 0x00d00000
  17. /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
  18. static uint32_t bootloader[] = {
  19. 0xe3a00000, /* mov r0, #0 */
  20. 0xe3a01000, /* mov r1, #0x?? */
  21. 0xe3811c00, /* orr r1, r1, #0x??00 */
  22. 0xe59f2000, /* ldr r2, [pc, #0] */
  23. 0xe59ff000, /* ldr pc, [pc, #0] */
  24. 0, /* Address of kernel args. Set by integratorcp_init. */
  25. 0 /* Kernel entry point. Set by integratorcp_init. */
  26. };
  27. /* Entry point for secondary CPUs. Enable interrupt controller and
  28. Issue WFI until start address is written to system controller. */
  29. static uint32_t smpboot[] = {
  30. 0xe59f0020, /* ldr r0, privbase */
  31. 0xe3a01001, /* mov r1, #1 */
  32. 0xe5801100, /* str r1, [r0, #0x100] */
  33. 0xe3a00201, /* mov r0, #0x10000000 */
  34. 0xe3800030, /* orr r0, #0x30 */
  35. 0xe320f003, /* wfi */
  36. 0xe5901000, /* ldr r1, [r0] */
  37. 0xe1110001, /* tst r1, r1 */
  38. 0x0afffffb, /* beq <wfi> */
  39. 0xe12fff11, /* bx r1 */
  40. 0 /* privbase: Private memory region base address. */
  41. };
  42. #define WRITE_WORD(p, value) do { \
  43. stl_phys_notdirty(p, value); \
  44. p += 4; \
  45. } while (0)
  46. static void set_kernel_args(const struct arm_boot_info *info,
  47. int initrd_size, target_phys_addr_t base)
  48. {
  49. target_phys_addr_t p;
  50. p = base + KERNEL_ARGS_ADDR;
  51. /* ATAG_CORE */
  52. WRITE_WORD(p, 5);
  53. WRITE_WORD(p, 0x54410001);
  54. WRITE_WORD(p, 1);
  55. WRITE_WORD(p, 0x1000);
  56. WRITE_WORD(p, 0);
  57. /* ATAG_MEM */
  58. /* TODO: handle multiple chips on one ATAG list */
  59. WRITE_WORD(p, 4);
  60. WRITE_WORD(p, 0x54410002);
  61. WRITE_WORD(p, info->ram_size);
  62. WRITE_WORD(p, info->loader_start);
  63. if (initrd_size) {
  64. /* ATAG_INITRD2 */
  65. WRITE_WORD(p, 4);
  66. WRITE_WORD(p, 0x54420005);
  67. WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
  68. WRITE_WORD(p, initrd_size);
  69. }
  70. if (info->kernel_cmdline && *info->kernel_cmdline) {
  71. /* ATAG_CMDLINE */
  72. int cmdline_size;
  73. cmdline_size = strlen(info->kernel_cmdline);
  74. cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
  75. cmdline_size + 1);
  76. cmdline_size = (cmdline_size >> 2) + 1;
  77. WRITE_WORD(p, cmdline_size + 2);
  78. WRITE_WORD(p, 0x54410009);
  79. p += cmdline_size * 4;
  80. }
  81. if (info->atag_board) {
  82. /* ATAG_BOARD */
  83. int atag_board_len;
  84. uint8_t atag_board_buf[0x1000];
  85. atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
  86. WRITE_WORD(p, (atag_board_len + 8) >> 2);
  87. WRITE_WORD(p, 0x414f4d50);
  88. cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
  89. p += atag_board_len;
  90. }
  91. /* ATAG_END */
  92. WRITE_WORD(p, 0);
  93. WRITE_WORD(p, 0);
  94. }
  95. static void set_kernel_args_old(const struct arm_boot_info *info,
  96. int initrd_size, target_phys_addr_t base)
  97. {
  98. target_phys_addr_t p;
  99. const char *s;
  100. /* see linux/include/asm-arm/setup.h */
  101. p = base + KERNEL_ARGS_ADDR;
  102. /* page_size */
  103. WRITE_WORD(p, 4096);
  104. /* nr_pages */
  105. WRITE_WORD(p, info->ram_size / 4096);
  106. /* ramdisk_size */
  107. WRITE_WORD(p, 0);
  108. #define FLAG_READONLY 1
  109. #define FLAG_RDLOAD 4
  110. #define FLAG_RDPROMPT 8
  111. /* flags */
  112. WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
  113. /* rootdev */
  114. WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
  115. /* video_num_cols */
  116. WRITE_WORD(p, 0);
  117. /* video_num_rows */
  118. WRITE_WORD(p, 0);
  119. /* video_x */
  120. WRITE_WORD(p, 0);
  121. /* video_y */
  122. WRITE_WORD(p, 0);
  123. /* memc_control_reg */
  124. WRITE_WORD(p, 0);
  125. /* unsigned char sounddefault */
  126. /* unsigned char adfsdrives */
  127. /* unsigned char bytes_per_char_h */
  128. /* unsigned char bytes_per_char_v */
  129. WRITE_WORD(p, 0);
  130. /* pages_in_bank[4] */
  131. WRITE_WORD(p, 0);
  132. WRITE_WORD(p, 0);
  133. WRITE_WORD(p, 0);
  134. WRITE_WORD(p, 0);
  135. /* pages_in_vram */
  136. WRITE_WORD(p, 0);
  137. /* initrd_start */
  138. if (initrd_size)
  139. WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
  140. else
  141. WRITE_WORD(p, 0);
  142. /* initrd_size */
  143. WRITE_WORD(p, initrd_size);
  144. /* rd_start */
  145. WRITE_WORD(p, 0);
  146. /* system_rev */
  147. WRITE_WORD(p, 0);
  148. /* system_serial_low */
  149. WRITE_WORD(p, 0);
  150. /* system_serial_high */
  151. WRITE_WORD(p, 0);
  152. /* mem_fclk_21285 */
  153. WRITE_WORD(p, 0);
  154. /* zero unused fields */
  155. while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
  156. WRITE_WORD(p, 0);
  157. }
  158. s = info->kernel_cmdline;
  159. if (s) {
  160. cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
  161. } else {
  162. WRITE_WORD(p, 0);
  163. }
  164. }
  165. static void do_cpu_reset(void *opaque)
  166. {
  167. CPUState *env = opaque;
  168. const struct arm_boot_info *info = env->boot_info;
  169. cpu_reset(env);
  170. if (info) {
  171. if (!info->is_linux) {
  172. /* Jump to the entry point. */
  173. env->regs[15] = info->entry & 0xfffffffe;
  174. env->thumb = info->entry & 1;
  175. } else {
  176. if (env == first_cpu) {
  177. env->regs[15] = info->loader_start;
  178. if (old_param) {
  179. set_kernel_args_old(info, info->initrd_size,
  180. info->loader_start);
  181. } else {
  182. set_kernel_args(info, info->initrd_size,
  183. info->loader_start);
  184. }
  185. } else {
  186. env->regs[15] = info->smp_loader_start;
  187. }
  188. }
  189. }
  190. }
  191. void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
  192. {
  193. int kernel_size;
  194. int initrd_size;
  195. int n;
  196. int is_linux = 0;
  197. uint64_t elf_entry;
  198. target_phys_addr_t entry;
  199. int big_endian;
  200. /* Load the kernel. */
  201. if (!info->kernel_filename) {
  202. fprintf(stderr, "Kernel image must be specified\n");
  203. exit(1);
  204. }
  205. if (info->nb_cpus == 0)
  206. info->nb_cpus = 1;
  207. #ifdef TARGET_WORDS_BIGENDIAN
  208. big_endian = 1;
  209. #else
  210. big_endian = 0;
  211. #endif
  212. /* Assume that raw images are linux kernels, and ELF images are not. */
  213. kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
  214. NULL, NULL, big_endian, ELF_MACHINE, 1);
  215. entry = elf_entry;
  216. if (kernel_size < 0) {
  217. kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
  218. &is_linux);
  219. }
  220. if (kernel_size < 0) {
  221. entry = info->loader_start + KERNEL_LOAD_ADDR;
  222. kernel_size = load_image_targphys(info->kernel_filename, entry,
  223. ram_size - KERNEL_LOAD_ADDR);
  224. is_linux = 1;
  225. }
  226. if (kernel_size < 0) {
  227. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  228. info->kernel_filename);
  229. exit(1);
  230. }
  231. info->entry = entry;
  232. if (is_linux) {
  233. if (info->initrd_filename) {
  234. initrd_size = load_image_targphys(info->initrd_filename,
  235. info->loader_start
  236. + INITRD_LOAD_ADDR,
  237. ram_size - INITRD_LOAD_ADDR);
  238. if (initrd_size < 0) {
  239. fprintf(stderr, "qemu: could not load initrd '%s'\n",
  240. info->initrd_filename);
  241. exit(1);
  242. }
  243. } else {
  244. initrd_size = 0;
  245. }
  246. bootloader[1] |= info->board_id & 0xff;
  247. bootloader[2] |= (info->board_id >> 8) & 0xff;
  248. bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
  249. bootloader[6] = entry;
  250. for (n = 0; n < sizeof(bootloader) / 4; n++) {
  251. bootloader[n] = tswap32(bootloader[n]);
  252. }
  253. rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
  254. info->loader_start);
  255. if (info->nb_cpus > 1) {
  256. smpboot[10] = info->smp_priv_base;
  257. for (n = 0; n < sizeof(smpboot) / 4; n++) {
  258. smpboot[n] = tswap32(smpboot[n]);
  259. }
  260. rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
  261. info->smp_loader_start);
  262. }
  263. info->initrd_size = initrd_size;
  264. }
  265. info->is_linux = is_linux;
  266. for (; env; env = env->next_cpu) {
  267. env->boot_info = info;
  268. qemu_register_reset(do_cpu_reset, env);
  269. }
  270. }