Statistics
| Branch: | Revision:

root / hw / arm_boot.c @ 28c5af54

History | View | Annotate | Download (7.3 kB)

1 5fafdf24 ths
/*
2 16406950 pbrook
 * ARM kernel loader.
3 16406950 pbrook
 *
4 9ee6e8bb pbrook
 * Copyright (c) 2006-2007 CodeSourcery.
5 16406950 pbrook
 * Written by Paul Brook
6 16406950 pbrook
 *
7 16406950 pbrook
 * This code is licenced under the GPL.
8 16406950 pbrook
 */
9 16406950 pbrook
10 16406950 pbrook
#include "vl.h"
11 16406950 pbrook
12 16406950 pbrook
#define KERNEL_ARGS_ADDR 0x100
13 16406950 pbrook
#define KERNEL_LOAD_ADDR 0x00010000
14 16406950 pbrook
#define INITRD_LOAD_ADDR 0x00800000
15 16406950 pbrook
16 16406950 pbrook
/* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
17 16406950 pbrook
static uint32_t bootloader[] = {
18 16406950 pbrook
  0xe3a00000, /* mov     r0, #0 */
19 16406950 pbrook
  0xe3a01000, /* mov     r1, #0x?? */
20 16406950 pbrook
  0xe3811c00, /* orr     r1, r1, #0x??00 */
21 16406950 pbrook
  0xe59f2000, /* ldr     r2, [pc, #0] */
22 16406950 pbrook
  0xe59ff000, /* ldr     pc, [pc, #0] */
23 16406950 pbrook
  0, /* Address of kernel args.  Set by integratorcp_init.  */
24 16406950 pbrook
  0  /* Kernel entry point.  Set by integratorcp_init.  */
25 16406950 pbrook
};
26 16406950 pbrook
27 9ee6e8bb pbrook
/* Entry point for secondary CPUs.  Enable interrupt controller and
28 9ee6e8bb pbrook
   Issue WFI until start address is written to system controller.  */
29 9ee6e8bb pbrook
static uint32_t smpboot[] = {
30 9ee6e8bb pbrook
  0xe3a00201, /* mov     r0, #0x10000000 */
31 9ee6e8bb pbrook
  0xe3800601, /* orr     r0, r0, #0x001000000 */
32 9ee6e8bb pbrook
  0xe3a01001, /* mov     r1, #1 */
33 9ee6e8bb pbrook
  0xe5801100, /* str     r1, [r0, #0x100] */
34 9ee6e8bb pbrook
  0xe3a00201, /* mov     r0, #0x10000000 */
35 9ee6e8bb pbrook
  0xe3800030, /* orr     r0, #0x30 */
36 9ee6e8bb pbrook
  0xe320f003, /* wfi */
37 9ee6e8bb pbrook
  0xe5901000, /* ldr     r1, [r0] */
38 9ee6e8bb pbrook
  0xe3110003, /* tst     r1, #3 */
39 9ee6e8bb pbrook
  0x1afffffb, /* bne     <wfi> */
40 9ee6e8bb pbrook
  0xe12fff11  /* bx      r1 */
41 9ee6e8bb pbrook
};
42 9ee6e8bb pbrook
43 f3d6b95e pbrook
static void main_cpu_reset(void *opaque)
44 f3d6b95e pbrook
{
45 f3d6b95e pbrook
    CPUState *env = opaque;
46 f3d6b95e pbrook
47 f3d6b95e pbrook
    cpu_reset(env);
48 f3d6b95e pbrook
    if (env->kernel_filename)
49 5fafdf24 ths
        arm_load_kernel(env, env->ram_size, env->kernel_filename,
50 5fafdf24 ths
                        env->kernel_cmdline, env->initrd_filename,
51 9d551997 balrog
                        env->board_id, env->loader_start);
52 9ee6e8bb pbrook
53 9ee6e8bb pbrook
    /* TODO:  Reset secondary CPUs.  */
54 f3d6b95e pbrook
}
55 f3d6b95e pbrook
56 16406950 pbrook
static void set_kernel_args(uint32_t ram_size, int initrd_size,
57 9d551997 balrog
                            const char *kernel_cmdline,
58 9d551997 balrog
                            target_phys_addr_t loader_start)
59 16406950 pbrook
{
60 16406950 pbrook
    uint32_t *p;
61 16406950 pbrook
62 16406950 pbrook
    p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
63 16406950 pbrook
    /* ATAG_CORE */
64 16406950 pbrook
    stl_raw(p++, 5);
65 16406950 pbrook
    stl_raw(p++, 0x54410001);
66 16406950 pbrook
    stl_raw(p++, 1);
67 16406950 pbrook
    stl_raw(p++, 0x1000);
68 16406950 pbrook
    stl_raw(p++, 0);
69 16406950 pbrook
    /* ATAG_MEM */
70 16406950 pbrook
    stl_raw(p++, 4);
71 16406950 pbrook
    stl_raw(p++, 0x54410002);
72 16406950 pbrook
    stl_raw(p++, ram_size);
73 9d551997 balrog
    stl_raw(p++, loader_start);
74 16406950 pbrook
    if (initrd_size) {
75 16406950 pbrook
        /* ATAG_INITRD2 */
76 16406950 pbrook
        stl_raw(p++, 4);
77 16406950 pbrook
        stl_raw(p++, 0x54420005);
78 9d551997 balrog
        stl_raw(p++, loader_start + INITRD_LOAD_ADDR);
79 16406950 pbrook
        stl_raw(p++, initrd_size);
80 16406950 pbrook
    }
81 16406950 pbrook
    if (kernel_cmdline && *kernel_cmdline) {
82 16406950 pbrook
        /* ATAG_CMDLINE */
83 16406950 pbrook
        int cmdline_size;
84 16406950 pbrook
85 16406950 pbrook
        cmdline_size = strlen(kernel_cmdline);
86 16406950 pbrook
        memcpy (p + 2, kernel_cmdline, cmdline_size + 1);
87 16406950 pbrook
        cmdline_size = (cmdline_size >> 2) + 1;
88 16406950 pbrook
        stl_raw(p++, cmdline_size + 2);
89 16406950 pbrook
        stl_raw(p++, 0x54410009);
90 16406950 pbrook
        p += cmdline_size;
91 16406950 pbrook
    }
92 16406950 pbrook
    /* ATAG_END */
93 16406950 pbrook
    stl_raw(p++, 0);
94 16406950 pbrook
    stl_raw(p++, 0);
95 16406950 pbrook
}
96 16406950 pbrook
97 2b8f2d41 balrog
static void set_kernel_args_old(uint32_t ram_size, int initrd_size,
98 2b8f2d41 balrog
                                const char *kernel_cmdline,
99 2b8f2d41 balrog
                                target_phys_addr_t loader_start)
100 2b8f2d41 balrog
{
101 2b8f2d41 balrog
    uint32_t *p;
102 2b8f2d41 balrog
    unsigned char *s;
103 2b8f2d41 balrog
104 2b8f2d41 balrog
    /* see linux/include/asm-arm/setup.h */
105 2b8f2d41 balrog
    p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
106 2b8f2d41 balrog
    /* page_size */
107 2b8f2d41 balrog
    stl_raw(p++, 4096);
108 2b8f2d41 balrog
    /* nr_pages */
109 2b8f2d41 balrog
    stl_raw(p++, ram_size / 4096);
110 2b8f2d41 balrog
    /* ramdisk_size */
111 2b8f2d41 balrog
    stl_raw(p++, 0);
112 2b8f2d41 balrog
#define FLAG_READONLY        1
113 2b8f2d41 balrog
#define FLAG_RDLOAD        4
114 2b8f2d41 balrog
#define FLAG_RDPROMPT        8
115 2b8f2d41 balrog
    /* flags */
116 2b8f2d41 balrog
    stl_raw(p++, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
117 2b8f2d41 balrog
    /* rootdev */
118 2b8f2d41 balrog
    stl_raw(p++, (31 << 8) | 0);        /* /dev/mtdblock0 */
119 2b8f2d41 balrog
    /* video_num_cols */
120 2b8f2d41 balrog
    stl_raw(p++, 0);
121 2b8f2d41 balrog
    /* video_num_rows */
122 2b8f2d41 balrog
    stl_raw(p++, 0);
123 2b8f2d41 balrog
    /* video_x */
124 2b8f2d41 balrog
    stl_raw(p++, 0);
125 2b8f2d41 balrog
    /* video_y */
126 2b8f2d41 balrog
    stl_raw(p++, 0);
127 2b8f2d41 balrog
    /* memc_control_reg */
128 2b8f2d41 balrog
    stl_raw(p++, 0);
129 2b8f2d41 balrog
    /* unsigned char sounddefault */
130 2b8f2d41 balrog
    /* unsigned char adfsdrives */
131 2b8f2d41 balrog
    /* unsigned char bytes_per_char_h */
132 2b8f2d41 balrog
    /* unsigned char bytes_per_char_v */
133 2b8f2d41 balrog
    stl_raw(p++, 0);
134 2b8f2d41 balrog
    /* pages_in_bank[4] */
135 2b8f2d41 balrog
    stl_raw(p++, 0);
136 2b8f2d41 balrog
    stl_raw(p++, 0);
137 2b8f2d41 balrog
    stl_raw(p++, 0);
138 2b8f2d41 balrog
    stl_raw(p++, 0);
139 2b8f2d41 balrog
    /* pages_in_vram */
140 2b8f2d41 balrog
    stl_raw(p++, 0);
141 2b8f2d41 balrog
    /* initrd_start */
142 2b8f2d41 balrog
    if (initrd_size)
143 2b8f2d41 balrog
        stl_raw(p++, loader_start + INITRD_LOAD_ADDR);
144 2b8f2d41 balrog
    else
145 2b8f2d41 balrog
        stl_raw(p++, 0);
146 2b8f2d41 balrog
    /* initrd_size */
147 2b8f2d41 balrog
    stl_raw(p++, initrd_size);
148 2b8f2d41 balrog
    /* rd_start */
149 2b8f2d41 balrog
    stl_raw(p++, 0);
150 2b8f2d41 balrog
    /* system_rev */
151 2b8f2d41 balrog
    stl_raw(p++, 0);
152 2b8f2d41 balrog
    /* system_serial_low */
153 2b8f2d41 balrog
    stl_raw(p++, 0);
154 2b8f2d41 balrog
    /* system_serial_high */
155 2b8f2d41 balrog
    stl_raw(p++, 0);
156 2b8f2d41 balrog
    /* mem_fclk_21285 */
157 2b8f2d41 balrog
    stl_raw(p++, 0);
158 2b8f2d41 balrog
    /* zero unused fields */
159 2b8f2d41 balrog
    memset(p, 0, 256 + 1024 -
160 2b8f2d41 balrog
           (p - ((uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR))));
161 2b8f2d41 balrog
    s = phys_ram_base + KERNEL_ARGS_ADDR + 256 + 1024;
162 2b8f2d41 balrog
    if (kernel_cmdline)
163 2b8f2d41 balrog
        strcpy (s, kernel_cmdline);
164 2b8f2d41 balrog
    else
165 2b8f2d41 balrog
        stb_raw(s, 0);
166 2b8f2d41 balrog
}
167 2b8f2d41 balrog
168 daf90626 pbrook
void arm_load_kernel(CPUState *env, int ram_size, const char *kernel_filename,
169 16406950 pbrook
                     const char *kernel_cmdline, const char *initrd_filename,
170 9d551997 balrog
                     int board_id, target_phys_addr_t loader_start)
171 16406950 pbrook
{
172 16406950 pbrook
    int kernel_size;
173 16406950 pbrook
    int initrd_size;
174 16406950 pbrook
    int n;
175 1c7b3754 pbrook
    int is_linux = 0;
176 1c7b3754 pbrook
    uint64_t elf_entry;
177 1c7b3754 pbrook
    target_ulong entry;
178 16406950 pbrook
179 16406950 pbrook
    /* Load the kernel.  */
180 16406950 pbrook
    if (!kernel_filename) {
181 16406950 pbrook
        fprintf(stderr, "Kernel image must be specified\n");
182 16406950 pbrook
        exit(1);
183 16406950 pbrook
    }
184 daf90626 pbrook
185 f3d6b95e pbrook
    if (!env->kernel_filename) {
186 f3d6b95e pbrook
        env->ram_size = ram_size;
187 f3d6b95e pbrook
        env->kernel_filename = kernel_filename;
188 f3d6b95e pbrook
        env->kernel_cmdline = kernel_cmdline;
189 f3d6b95e pbrook
        env->initrd_filename = initrd_filename;
190 f3d6b95e pbrook
        env->board_id = board_id;
191 9d551997 balrog
        env->loader_start = loader_start;
192 f3d6b95e pbrook
        qemu_register_reset(main_cpu_reset, env);
193 f3d6b95e pbrook
    }
194 1c7b3754 pbrook
    /* Assume that raw images are linux kernels, and ELF images are not.  */
195 74287114 ths
    kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL);
196 1c7b3754 pbrook
    entry = elf_entry;
197 1c7b3754 pbrook
    if (kernel_size < 0) {
198 1c7b3754 pbrook
        kernel_size = load_uboot(kernel_filename, &entry, &is_linux);
199 1c7b3754 pbrook
    }
200 1c7b3754 pbrook
    if (kernel_size < 0) {
201 1c7b3754 pbrook
        kernel_size = load_image(kernel_filename,
202 1c7b3754 pbrook
                                 phys_ram_base + KERNEL_LOAD_ADDR);
203 9d551997 balrog
        entry = loader_start + KERNEL_LOAD_ADDR;
204 1c7b3754 pbrook
        is_linux = 1;
205 1c7b3754 pbrook
    }
206 1c7b3754 pbrook
    if (kernel_size < 0) {
207 1c7b3754 pbrook
        fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
208 1c7b3754 pbrook
        exit(1);
209 1c7b3754 pbrook
    }
210 1c7b3754 pbrook
    if (!is_linux) {
211 1c7b3754 pbrook
        /* Jump to the entry point.  */
212 daf90626 pbrook
        env->regs[15] = entry & 0xfffffffe;
213 daf90626 pbrook
        env->thumb = entry & 1;
214 daf90626 pbrook
    } else {
215 daf90626 pbrook
        if (initrd_filename) {
216 daf90626 pbrook
            initrd_size = load_image(initrd_filename,
217 daf90626 pbrook
                                     phys_ram_base + INITRD_LOAD_ADDR);
218 daf90626 pbrook
            if (initrd_size < 0) {
219 daf90626 pbrook
                fprintf(stderr, "qemu: could not load initrd '%s'\n",
220 daf90626 pbrook
                        initrd_filename);
221 daf90626 pbrook
                exit(1);
222 daf90626 pbrook
            }
223 daf90626 pbrook
        } else {
224 daf90626 pbrook
            initrd_size = 0;
225 daf90626 pbrook
        }
226 daf90626 pbrook
        bootloader[1] |= board_id & 0xff;
227 daf90626 pbrook
        bootloader[2] |= (board_id >> 8) & 0xff;
228 9d551997 balrog
        bootloader[5] = loader_start + KERNEL_ARGS_ADDR;
229 1c7b3754 pbrook
        bootloader[6] = entry;
230 daf90626 pbrook
        for (n = 0; n < sizeof(bootloader) / 4; n++)
231 daf90626 pbrook
            stl_raw(phys_ram_base + (n * 4), bootloader[n]);
232 9ee6e8bb pbrook
        for (n = 0; n < sizeof(smpboot) / 4; n++)
233 9ee6e8bb pbrook
            stl_raw(phys_ram_base + ram_size + (n * 4), smpboot[n]);
234 2b8f2d41 balrog
        if (old_param)
235 2b8f2d41 balrog
            set_kernel_args_old(ram_size, initrd_size,
236 2b8f2d41 balrog
                                kernel_cmdline, loader_start);
237 2b8f2d41 balrog
        else
238 2b8f2d41 balrog
            set_kernel_args(ram_size, initrd_size,
239 2b8f2d41 balrog
                            kernel_cmdline, loader_start);
240 16406950 pbrook
    }
241 16406950 pbrook
}