blob: 41e99d1332233bec465b82599c85273fb2db97bf [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook16406952006-04-27 23:15:07 +00002 * ARM kernel loader.
3 *
pbrook9ee6e8b2007-11-11 00:04:49 +00004 * Copyright (c) 2006-2007 CodeSourcery.
pbrook16406952006-04-27 23:15:07 +00005 * Written by Paul Brook
6 *
7 * This code is licenced under the GPL.
8 */
9
pbrook87ecb682007-11-17 17:14:51 +000010#include "hw.h"
11#include "arm-misc.h"
12#include "sysemu.h"
Blue Swirlca20cf32009-09-20 14:58:02 +000013#include "loader.h"
14#include "elf.h"
pbrook16406952006-04-27 23:15:07 +000015
16#define KERNEL_ARGS_ADDR 0x100
17#define KERNEL_LOAD_ADDR 0x00010000
18#define INITRD_LOAD_ADDR 0x00800000
19
20/* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
21static uint32_t bootloader[] = {
22 0xe3a00000, /* mov r0, #0 */
23 0xe3a01000, /* mov r1, #0x?? */
24 0xe3811c00, /* orr r1, r1, #0x??00 */
25 0xe59f2000, /* ldr r2, [pc, #0] */
26 0xe59ff000, /* ldr pc, [pc, #0] */
27 0, /* Address of kernel args. Set by integratorcp_init. */
28 0 /* Kernel entry point. Set by integratorcp_init. */
29};
30
pbrook9ee6e8b2007-11-11 00:04:49 +000031/* Entry point for secondary CPUs. Enable interrupt controller and
32 Issue WFI until start address is written to system controller. */
33static uint32_t smpboot[] = {
Paul Brookf7c70322009-11-19 16:45:21 +000034 0xe59f0020, /* ldr r0, privbase */
pbrook9ee6e8b2007-11-11 00:04:49 +000035 0xe3a01001, /* mov r1, #1 */
36 0xe5801100, /* str r1, [r0, #0x100] */
37 0xe3a00201, /* mov r0, #0x10000000 */
38 0xe3800030, /* orr r0, #0x30 */
39 0xe320f003, /* wfi */
40 0xe5901000, /* ldr r1, [r0] */
Paul Brookbe0f2042009-11-11 19:59:29 +000041 0xe1110001, /* tst r1, r1 */
42 0x0afffffb, /* beq <wfi> */
Paul Brookf7c70322009-11-19 16:45:21 +000043 0xe12fff11, /* bx r1 */
44 0 /* privbase: Private memory region base address. */
pbrook9ee6e8b2007-11-11 00:04:49 +000045};
46
pbrook52b43732009-04-09 17:19:47 +000047#define WRITE_WORD(p, value) do { \
48 stl_phys_notdirty(p, value); \
49 p += 4; \
50} while (0)
pbrook16406952006-04-27 23:15:07 +000051
pbrook52b43732009-04-09 17:19:47 +000052static void set_kernel_args(struct arm_boot_info *info,
Anthony Liguoric227f092009-10-01 16:12:16 -050053 int initrd_size, target_phys_addr_t base)
pbrook52b43732009-04-09 17:19:47 +000054{
Anthony Liguoric227f092009-10-01 16:12:16 -050055 target_phys_addr_t p;
pbrook52b43732009-04-09 17:19:47 +000056
57 p = base + KERNEL_ARGS_ADDR;
pbrook16406952006-04-27 23:15:07 +000058 /* ATAG_CORE */
pbrook52b43732009-04-09 17:19:47 +000059 WRITE_WORD(p, 5);
60 WRITE_WORD(p, 0x54410001);
61 WRITE_WORD(p, 1);
62 WRITE_WORD(p, 0x1000);
63 WRITE_WORD(p, 0);
pbrook16406952006-04-27 23:15:07 +000064 /* ATAG_MEM */
balrogf93eb9f2008-04-14 20:27:51 +000065 /* TODO: handle multiple chips on one ATAG list */
pbrook52b43732009-04-09 17:19:47 +000066 WRITE_WORD(p, 4);
67 WRITE_WORD(p, 0x54410002);
68 WRITE_WORD(p, info->ram_size);
69 WRITE_WORD(p, info->loader_start);
pbrook16406952006-04-27 23:15:07 +000070 if (initrd_size) {
71 /* ATAG_INITRD2 */
pbrook52b43732009-04-09 17:19:47 +000072 WRITE_WORD(p, 4);
73 WRITE_WORD(p, 0x54420005);
74 WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
75 WRITE_WORD(p, initrd_size);
pbrook16406952006-04-27 23:15:07 +000076 }
balrogf93eb9f2008-04-14 20:27:51 +000077 if (info->kernel_cmdline && *info->kernel_cmdline) {
pbrook16406952006-04-27 23:15:07 +000078 /* ATAG_CMDLINE */
79 int cmdline_size;
80
balrogf93eb9f2008-04-14 20:27:51 +000081 cmdline_size = strlen(info->kernel_cmdline);
pbrook52b43732009-04-09 17:19:47 +000082 cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
83 cmdline_size + 1);
pbrook16406952006-04-27 23:15:07 +000084 cmdline_size = (cmdline_size >> 2) + 1;
pbrook52b43732009-04-09 17:19:47 +000085 WRITE_WORD(p, cmdline_size + 2);
86 WRITE_WORD(p, 0x54410009);
87 p += cmdline_size * 4;
pbrook16406952006-04-27 23:15:07 +000088 }
balrogf93eb9f2008-04-14 20:27:51 +000089 if (info->atag_board) {
90 /* ATAG_BOARD */
91 int atag_board_len;
pbrook52b43732009-04-09 17:19:47 +000092 uint8_t atag_board_buf[0x1000];
balrogf93eb9f2008-04-14 20:27:51 +000093
pbrook52b43732009-04-09 17:19:47 +000094 atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
95 WRITE_WORD(p, (atag_board_len + 8) >> 2);
96 WRITE_WORD(p, 0x414f4d50);
97 cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
balrogf93eb9f2008-04-14 20:27:51 +000098 p += atag_board_len;
99 }
pbrook16406952006-04-27 23:15:07 +0000100 /* ATAG_END */
pbrook52b43732009-04-09 17:19:47 +0000101 WRITE_WORD(p, 0);
102 WRITE_WORD(p, 0);
pbrook16406952006-04-27 23:15:07 +0000103}
104
balrogf93eb9f2008-04-14 20:27:51 +0000105static void set_kernel_args_old(struct arm_boot_info *info,
Anthony Liguoric227f092009-10-01 16:12:16 -0500106 int initrd_size, target_phys_addr_t base)
balrog2b8f2d42007-07-27 22:08:46 +0000107{
Anthony Liguoric227f092009-10-01 16:12:16 -0500108 target_phys_addr_t p;
pbrook52b43732009-04-09 17:19:47 +0000109 const char *s;
110
balrog2b8f2d42007-07-27 22:08:46 +0000111
112 /* see linux/include/asm-arm/setup.h */
pbrook52b43732009-04-09 17:19:47 +0000113 p = base + KERNEL_ARGS_ADDR;
balrog2b8f2d42007-07-27 22:08:46 +0000114 /* page_size */
pbrook52b43732009-04-09 17:19:47 +0000115 WRITE_WORD(p, 4096);
balrog2b8f2d42007-07-27 22:08:46 +0000116 /* nr_pages */
pbrook52b43732009-04-09 17:19:47 +0000117 WRITE_WORD(p, info->ram_size / 4096);
balrog2b8f2d42007-07-27 22:08:46 +0000118 /* ramdisk_size */
pbrook52b43732009-04-09 17:19:47 +0000119 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000120#define FLAG_READONLY 1
121#define FLAG_RDLOAD 4
122#define FLAG_RDPROMPT 8
123 /* flags */
pbrook52b43732009-04-09 17:19:47 +0000124 WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
balrog2b8f2d42007-07-27 22:08:46 +0000125 /* rootdev */
pbrook52b43732009-04-09 17:19:47 +0000126 WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
balrog2b8f2d42007-07-27 22:08:46 +0000127 /* video_num_cols */
pbrook52b43732009-04-09 17:19:47 +0000128 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000129 /* video_num_rows */
pbrook52b43732009-04-09 17:19:47 +0000130 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000131 /* video_x */
pbrook52b43732009-04-09 17:19:47 +0000132 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000133 /* video_y */
pbrook52b43732009-04-09 17:19:47 +0000134 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000135 /* memc_control_reg */
pbrook52b43732009-04-09 17:19:47 +0000136 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000137 /* unsigned char sounddefault */
138 /* unsigned char adfsdrives */
139 /* unsigned char bytes_per_char_h */
140 /* unsigned char bytes_per_char_v */
pbrook52b43732009-04-09 17:19:47 +0000141 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000142 /* pages_in_bank[4] */
pbrook52b43732009-04-09 17:19:47 +0000143 WRITE_WORD(p, 0);
144 WRITE_WORD(p, 0);
145 WRITE_WORD(p, 0);
146 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000147 /* pages_in_vram */
pbrook52b43732009-04-09 17:19:47 +0000148 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000149 /* initrd_start */
150 if (initrd_size)
pbrook52b43732009-04-09 17:19:47 +0000151 WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
balrog2b8f2d42007-07-27 22:08:46 +0000152 else
pbrook52b43732009-04-09 17:19:47 +0000153 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000154 /* initrd_size */
pbrook52b43732009-04-09 17:19:47 +0000155 WRITE_WORD(p, initrd_size);
balrog2b8f2d42007-07-27 22:08:46 +0000156 /* rd_start */
pbrook52b43732009-04-09 17:19:47 +0000157 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000158 /* system_rev */
pbrook52b43732009-04-09 17:19:47 +0000159 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000160 /* system_serial_low */
pbrook52b43732009-04-09 17:19:47 +0000161 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000162 /* system_serial_high */
pbrook52b43732009-04-09 17:19:47 +0000163 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000164 /* mem_fclk_21285 */
pbrook52b43732009-04-09 17:19:47 +0000165 WRITE_WORD(p, 0);
balrog2b8f2d42007-07-27 22:08:46 +0000166 /* zero unused fields */
pbrook52b43732009-04-09 17:19:47 +0000167 while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
168 WRITE_WORD(p, 0);
169 }
170 s = info->kernel_cmdline;
171 if (s) {
172 cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
173 } else {
174 WRITE_WORD(p, 0);
175 }
balrog2b8f2d42007-07-27 22:08:46 +0000176}
177
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100178static void do_cpu_reset(void *opaque)
Paul Brookf2d74972009-11-11 18:07:53 +0000179{
180 CPUState *env = opaque;
181 struct arm_boot_info *info = env->boot_info;
182
183 cpu_reset(env);
184 if (info) {
185 if (!info->is_linux) {
186 /* Jump to the entry point. */
187 env->regs[15] = info->entry & 0xfffffffe;
188 env->thumb = info->entry & 1;
189 } else {
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100190 if (env == first_cpu) {
191 env->regs[15] = info->loader_start;
192 if (old_param) {
193 set_kernel_args_old(info, info->initrd_size,
194 info->loader_start);
195 } else {
196 set_kernel_args(info, info->initrd_size,
Paul Brookf2d74972009-11-11 18:07:53 +0000197 info->loader_start);
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100198 }
Paul Brookf2d74972009-11-11 18:07:53 +0000199 } else {
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100200 env->regs[15] = info->smp_loader_start;
Paul Brookf2d74972009-11-11 18:07:53 +0000201 }
202 }
203 }
Paul Brookf2d74972009-11-11 18:07:53 +0000204}
205
balrogf93eb9f2008-04-14 20:27:51 +0000206void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
pbrook16406952006-04-27 23:15:07 +0000207{
208 int kernel_size;
209 int initrd_size;
210 int n;
pbrook1c7b3752007-03-06 23:52:01 +0000211 int is_linux = 0;
212 uint64_t elf_entry;
Anthony Liguoric227f092009-10-01 16:12:16 -0500213 target_phys_addr_t entry;
Blue Swirlca20cf32009-09-20 14:58:02 +0000214 int big_endian;
pbrook16406952006-04-27 23:15:07 +0000215
216 /* Load the kernel. */
balrogf93eb9f2008-04-14 20:27:51 +0000217 if (!info->kernel_filename) {
pbrook16406952006-04-27 23:15:07 +0000218 fprintf(stderr, "Kernel image must be specified\n");
219 exit(1);
220 }
pbrookdaf90622007-01-16 18:54:31 +0000221
Paul Brookf2d74972009-11-11 18:07:53 +0000222 if (info->nb_cpus == 0)
223 info->nb_cpus = 1;
balrogf93eb9f2008-04-14 20:27:51 +0000224
Blue Swirlca20cf32009-09-20 14:58:02 +0000225#ifdef TARGET_WORDS_BIGENDIAN
226 big_endian = 1;
227#else
228 big_endian = 0;
229#endif
230
pbrook1c7b3752007-03-06 23:52:01 +0000231 /* Assume that raw images are linux kernels, and ELF images are not. */
Aurelien Jarno409dbce2010-03-14 21:20:59 +0100232 kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
233 NULL, NULL, big_endian, ELF_MACHINE, 1);
pbrook1c7b3752007-03-06 23:52:01 +0000234 entry = elf_entry;
235 if (kernel_size < 0) {
aliguori5a9154e2008-11-20 22:14:40 +0000236 kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
237 &is_linux);
pbrook1c7b3752007-03-06 23:52:01 +0000238 }
239 if (kernel_size < 0) {
balrogf93eb9f2008-04-14 20:27:51 +0000240 entry = info->loader_start + KERNEL_LOAD_ADDR;
pbrook3b760e02009-04-09 17:30:32 +0000241 kernel_size = load_image_targphys(info->kernel_filename, entry,
242 ram_size - KERNEL_LOAD_ADDR);
pbrook1c7b3752007-03-06 23:52:01 +0000243 is_linux = 1;
244 }
245 if (kernel_size < 0) {
balrogf93eb9f2008-04-14 20:27:51 +0000246 fprintf(stderr, "qemu: could not load kernel '%s'\n",
247 info->kernel_filename);
pbrook1c7b3752007-03-06 23:52:01 +0000248 exit(1);
249 }
Paul Brookf2d74972009-11-11 18:07:53 +0000250 info->entry = entry;
251 if (is_linux) {
balrogf93eb9f2008-04-14 20:27:51 +0000252 if (info->initrd_filename) {
pbrook3b760e02009-04-09 17:30:32 +0000253 initrd_size = load_image_targphys(info->initrd_filename,
254 info->loader_start
255 + INITRD_LOAD_ADDR,
256 ram_size - INITRD_LOAD_ADDR);
pbrookdaf90622007-01-16 18:54:31 +0000257 if (initrd_size < 0) {
258 fprintf(stderr, "qemu: could not load initrd '%s'\n",
balrogf93eb9f2008-04-14 20:27:51 +0000259 info->initrd_filename);
pbrookdaf90622007-01-16 18:54:31 +0000260 exit(1);
261 }
262 } else {
263 initrd_size = 0;
264 }
balrogf93eb9f2008-04-14 20:27:51 +0000265 bootloader[1] |= info->board_id & 0xff;
266 bootloader[2] |= (info->board_id >> 8) & 0xff;
267 bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
pbrook1c7b3752007-03-06 23:52:01 +0000268 bootloader[6] = entry;
pbrook52b43732009-04-09 17:19:47 +0000269 for (n = 0; n < sizeof(bootloader) / 4; n++) {
Paul Brookf2d74972009-11-11 18:07:53 +0000270 bootloader[n] = tswap32(bootloader[n]);
pbrook52b43732009-04-09 17:19:47 +0000271 }
Paul Brookf2d74972009-11-11 18:07:53 +0000272 rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
273 info->loader_start);
pbrook52b43732009-04-09 17:19:47 +0000274 if (info->nb_cpus > 1) {
Paul Brookf7c70322009-11-19 16:45:21 +0000275 smpboot[10] = info->smp_priv_base;
pbrook52b43732009-04-09 17:19:47 +0000276 for (n = 0; n < sizeof(smpboot) / 4; n++) {
Paul Brookf2d74972009-11-11 18:07:53 +0000277 smpboot[n] = tswap32(smpboot[n]);
pbrook52b43732009-04-09 17:19:47 +0000278 }
Paul Brookf2d74972009-11-11 18:07:53 +0000279 rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
280 info->smp_loader_start);
pbrook52b43732009-04-09 17:19:47 +0000281 }
Paul Brookf2d74972009-11-11 18:07:53 +0000282 info->initrd_size = initrd_size;
pbrook16406952006-04-27 23:15:07 +0000283 }
Paul Brookf2d74972009-11-11 18:07:53 +0000284 info->is_linux = is_linux;
Adam Lackorzynski6ed221b2011-03-05 13:51:45 +0100285
286 for (; env; env = env->next_cpu) {
287 env->boot_info = info;
288 qemu_register_reset(do_cpu_reset, env);
289 }
pbrook16406952006-04-27 23:15:07 +0000290}