]>
Commit | Line | Data |
---|---|---|
16406950 PB |
1 | /* |
2 | * ARM kernel loader. | |
3 | * | |
4 | * Copyright (c) 2006 CodeSourcery. | |
5 | * Written by Paul Brook | |
6 | * | |
7 | * This code is licenced under the GPL. | |
8 | */ | |
9 | ||
10 | #include "vl.h" | |
11 | ||
12 | #define KERNEL_ARGS_ADDR 0x100 | |
13 | #define KERNEL_LOAD_ADDR 0x00010000 | |
14 | #define INITRD_LOAD_ADDR 0x00800000 | |
15 | ||
16 | /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */ | |
17 | static uint32_t bootloader[] = { | |
18 | 0xe3a00000, /* mov r0, #0 */ | |
19 | 0xe3a01000, /* mov r1, #0x?? */ | |
20 | 0xe3811c00, /* orr r1, r1, #0x??00 */ | |
21 | 0xe59f2000, /* ldr r2, [pc, #0] */ | |
22 | 0xe59ff000, /* ldr pc, [pc, #0] */ | |
23 | 0, /* Address of kernel args. Set by integratorcp_init. */ | |
24 | 0 /* Kernel entry point. Set by integratorcp_init. */ | |
25 | }; | |
26 | ||
f3d6b95e PB |
27 | static void main_cpu_reset(void *opaque) |
28 | { | |
29 | CPUState *env = opaque; | |
30 | ||
31 | cpu_reset(env); | |
32 | if (env->kernel_filename) | |
33 | arm_load_kernel(env, env->ram_size, env->kernel_filename, | |
34 | env->kernel_cmdline, env->initrd_filename, | |
35 | env->board_id); | |
36 | } | |
37 | ||
16406950 PB |
38 | static void set_kernel_args(uint32_t ram_size, int initrd_size, |
39 | const char *kernel_cmdline) | |
40 | { | |
41 | uint32_t *p; | |
42 | ||
43 | p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR); | |
44 | /* ATAG_CORE */ | |
45 | stl_raw(p++, 5); | |
46 | stl_raw(p++, 0x54410001); | |
47 | stl_raw(p++, 1); | |
48 | stl_raw(p++, 0x1000); | |
49 | stl_raw(p++, 0); | |
50 | /* ATAG_MEM */ | |
51 | stl_raw(p++, 4); | |
52 | stl_raw(p++, 0x54410002); | |
53 | stl_raw(p++, ram_size); | |
54 | stl_raw(p++, 0); | |
55 | if (initrd_size) { | |
56 | /* ATAG_INITRD2 */ | |
57 | stl_raw(p++, 4); | |
58 | stl_raw(p++, 0x54420005); | |
59 | stl_raw(p++, INITRD_LOAD_ADDR); | |
60 | stl_raw(p++, initrd_size); | |
61 | } | |
62 | if (kernel_cmdline && *kernel_cmdline) { | |
63 | /* ATAG_CMDLINE */ | |
64 | int cmdline_size; | |
65 | ||
66 | cmdline_size = strlen(kernel_cmdline); | |
67 | memcpy (p + 2, kernel_cmdline, cmdline_size + 1); | |
68 | cmdline_size = (cmdline_size >> 2) + 1; | |
69 | stl_raw(p++, cmdline_size + 2); | |
70 | stl_raw(p++, 0x54410009); | |
71 | p += cmdline_size; | |
72 | } | |
73 | /* ATAG_END */ | |
74 | stl_raw(p++, 0); | |
75 | stl_raw(p++, 0); | |
76 | } | |
77 | ||
daf90626 | 78 | void arm_load_kernel(CPUState *env, int ram_size, const char *kernel_filename, |
16406950 PB |
79 | const char *kernel_cmdline, const char *initrd_filename, |
80 | int board_id) | |
81 | { | |
82 | int kernel_size; | |
83 | int initrd_size; | |
84 | int n; | |
1c7b3754 PB |
85 | int is_linux = 0; |
86 | uint64_t elf_entry; | |
87 | target_ulong entry; | |
16406950 PB |
88 | |
89 | /* Load the kernel. */ | |
90 | if (!kernel_filename) { | |
91 | fprintf(stderr, "Kernel image must be specified\n"); | |
92 | exit(1); | |
93 | } | |
daf90626 | 94 | |
f3d6b95e PB |
95 | if (!env->kernel_filename) { |
96 | env->ram_size = ram_size; | |
97 | env->kernel_filename = kernel_filename; | |
98 | env->kernel_cmdline = kernel_cmdline; | |
99 | env->initrd_filename = initrd_filename; | |
100 | env->board_id = board_id; | |
101 | qemu_register_reset(main_cpu_reset, env); | |
102 | } | |
1c7b3754 | 103 | /* Assume that raw images are linux kernels, and ELF images are not. */ |
74287114 | 104 | kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL); |
1c7b3754 PB |
105 | entry = elf_entry; |
106 | if (kernel_size < 0) { | |
107 | kernel_size = load_uboot(kernel_filename, &entry, &is_linux); | |
108 | } | |
109 | if (kernel_size < 0) { | |
110 | kernel_size = load_image(kernel_filename, | |
111 | phys_ram_base + KERNEL_LOAD_ADDR); | |
112 | entry = KERNEL_LOAD_ADDR; | |
113 | is_linux = 1; | |
114 | } | |
115 | if (kernel_size < 0) { | |
116 | fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename); | |
117 | exit(1); | |
118 | } | |
119 | if (!is_linux) { | |
120 | /* Jump to the entry point. */ | |
daf90626 PB |
121 | env->regs[15] = entry & 0xfffffffe; |
122 | env->thumb = entry & 1; | |
123 | } else { | |
daf90626 PB |
124 | if (initrd_filename) { |
125 | initrd_size = load_image(initrd_filename, | |
126 | phys_ram_base + INITRD_LOAD_ADDR); | |
127 | if (initrd_size < 0) { | |
128 | fprintf(stderr, "qemu: could not load initrd '%s'\n", | |
129 | initrd_filename); | |
130 | exit(1); | |
131 | } | |
132 | } else { | |
133 | initrd_size = 0; | |
134 | } | |
135 | bootloader[1] |= board_id & 0xff; | |
136 | bootloader[2] |= (board_id >> 8) & 0xff; | |
137 | bootloader[5] = KERNEL_ARGS_ADDR; | |
1c7b3754 | 138 | bootloader[6] = entry; |
daf90626 PB |
139 | for (n = 0; n < sizeof(bootloader) / 4; n++) |
140 | stl_raw(phys_ram_base + (n * 4), bootloader[n]); | |
141 | set_kernel_args(ram_size, initrd_size, kernel_cmdline); | |
16406950 | 142 | } |
16406950 PB |
143 | } |
144 |