arm_boot.c 8.7 KB
Newer Older
1
/*
2 3
 * ARM kernel loader.
 *
P
pbrook 已提交
4
 * Copyright (c) 2006-2007 CodeSourcery.
5 6
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the GPL.
8 9
 */

P
pbrook 已提交
10 11 12
#include "hw.h"
#include "arm-misc.h"
#include "sysemu.h"
B
Blue Swirl 已提交
13 14
#include "loader.h"
#include "elf.h"
15 16 17

#define KERNEL_ARGS_ADDR 0x100
#define KERNEL_LOAD_ADDR 0x00010000
18
#define INITRD_LOAD_ADDR 0x00d00000
19 20 21 22 23 24 25 26 27 28 29 30

/* The worlds second smallest bootloader.  Set r0-r2, then jump to kernel.  */
static uint32_t bootloader[] = {
  0xe3a00000, /* mov     r0, #0 */
  0xe3a01000, /* mov     r1, #0x?? */
  0xe3811c00, /* orr     r1, r1, #0x??00 */
  0xe59f2000, /* ldr     r2, [pc, #0] */
  0xe59ff000, /* ldr     pc, [pc, #0] */
  0, /* Address of kernel args.  Set by integratorcp_init.  */
  0  /* Kernel entry point.  Set by integratorcp_init.  */
};

P
pbrook 已提交
31 32 33
/* Entry point for secondary CPUs.  Enable interrupt controller and
   Issue WFI until start address is written to system controller.  */
static uint32_t smpboot[] = {
34 35 36 37
  0xe59f201c, /* ldr r2, privbase */
  0xe59f001c, /* ldr r0, startaddr */
  0xe3a01001, /* mov r1, #1 */
  0xe5821100, /* str r1, [r2, #256] */
P
pbrook 已提交
38 39
  0xe320f003, /* wfi */
  0xe5901000, /* ldr     r1, [r0] */
P
Paul Brook 已提交
40 41
  0xe1110001, /* tst     r1, r1 */
  0x0afffffb, /* beq     <wfi> */
P
Paul Brook 已提交
42
  0xe12fff11, /* bx      r1 */
43 44
  0,          /* privbase: Private memory region base address.  */
  0           /* bootreg: Boot register address is held here */
P
pbrook 已提交
45 46
};

47 48 49 50 51
#define WRITE_WORD(p, value) do { \
    stl_phys_notdirty(p, value);  \
    p += 4;                       \
} while (0)

52
static void set_kernel_args(const struct arm_boot_info *info,
A
Anthony Liguori 已提交
53
                int initrd_size, target_phys_addr_t base)
54
{
A
Anthony Liguori 已提交
55
    target_phys_addr_t p;
56

57
    p = base + KERNEL_ARGS_ADDR;
58
    /* ATAG_CORE */
59 60 61 62 63
    WRITE_WORD(p, 5);
    WRITE_WORD(p, 0x54410001);
    WRITE_WORD(p, 1);
    WRITE_WORD(p, 0x1000);
    WRITE_WORD(p, 0);
64
    /* ATAG_MEM */
65
    /* TODO: handle multiple chips on one ATAG list */
66 67 68 69
    WRITE_WORD(p, 4);
    WRITE_WORD(p, 0x54410002);
    WRITE_WORD(p, info->ram_size);
    WRITE_WORD(p, info->loader_start);
70 71
    if (initrd_size) {
        /* ATAG_INITRD2 */
72 73 74 75
        WRITE_WORD(p, 4);
        WRITE_WORD(p, 0x54420005);
        WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
        WRITE_WORD(p, initrd_size);
76
    }
77
    if (info->kernel_cmdline && *info->kernel_cmdline) {
78 79 80
        /* ATAG_CMDLINE */
        int cmdline_size;

81
        cmdline_size = strlen(info->kernel_cmdline);
82 83
        cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
                                  cmdline_size + 1);
84
        cmdline_size = (cmdline_size >> 2) + 1;
85 86 87
        WRITE_WORD(p, cmdline_size + 2);
        WRITE_WORD(p, 0x54410009);
        p += cmdline_size * 4;
88
    }
89 90 91
    if (info->atag_board) {
        /* ATAG_BOARD */
        int atag_board_len;
92
        uint8_t atag_board_buf[0x1000];
93

94 95 96 97
        atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
        WRITE_WORD(p, (atag_board_len + 8) >> 2);
        WRITE_WORD(p, 0x414f4d50);
        cpu_physical_memory_write(p, atag_board_buf, atag_board_len);
98 99
        p += atag_board_len;
    }
100
    /* ATAG_END */
101 102
    WRITE_WORD(p, 0);
    WRITE_WORD(p, 0);
103 104
}

105
static void set_kernel_args_old(const struct arm_boot_info *info,
A
Anthony Liguori 已提交
106
                int initrd_size, target_phys_addr_t base)
107
{
A
Anthony Liguori 已提交
108
    target_phys_addr_t p;
109 110
    const char *s;

111 112

    /* see linux/include/asm-arm/setup.h */
113
    p = base + KERNEL_ARGS_ADDR;
114
    /* page_size */
115
    WRITE_WORD(p, 4096);
116
    /* nr_pages */
117
    WRITE_WORD(p, info->ram_size / 4096);
118
    /* ramdisk_size */
119
    WRITE_WORD(p, 0);
120 121 122 123
#define FLAG_READONLY	1
#define FLAG_RDLOAD	4
#define FLAG_RDPROMPT	8
    /* flags */
124
    WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
125
    /* rootdev */
126
    WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
127
    /* video_num_cols */
128
    WRITE_WORD(p, 0);
129
    /* video_num_rows */
130
    WRITE_WORD(p, 0);
131
    /* video_x */
132
    WRITE_WORD(p, 0);
133
    /* video_y */
134
    WRITE_WORD(p, 0);
135
    /* memc_control_reg */
136
    WRITE_WORD(p, 0);
137 138 139 140
    /* unsigned char sounddefault */
    /* unsigned char adfsdrives */
    /* unsigned char bytes_per_char_h */
    /* unsigned char bytes_per_char_v */
141
    WRITE_WORD(p, 0);
142
    /* pages_in_bank[4] */
143 144 145 146
    WRITE_WORD(p, 0);
    WRITE_WORD(p, 0);
    WRITE_WORD(p, 0);
    WRITE_WORD(p, 0);
147
    /* pages_in_vram */
148
    WRITE_WORD(p, 0);
149 150
    /* initrd_start */
    if (initrd_size)
151
        WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
152
    else
153
        WRITE_WORD(p, 0);
154
    /* initrd_size */
155
    WRITE_WORD(p, initrd_size);
156
    /* rd_start */
157
    WRITE_WORD(p, 0);
158
    /* system_rev */
159
    WRITE_WORD(p, 0);
160
    /* system_serial_low */
161
    WRITE_WORD(p, 0);
162
    /* system_serial_high */
163
    WRITE_WORD(p, 0);
164
    /* mem_fclk_21285 */
165
    WRITE_WORD(p, 0);
166
    /* zero unused fields */
167 168 169 170 171 172 173 174 175
    while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
        WRITE_WORD(p, 0);
    }
    s = info->kernel_cmdline;
    if (s) {
        cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
    } else {
        WRITE_WORD(p, 0);
    }
176 177
}

178
static void do_cpu_reset(void *opaque)
P
Paul Brook 已提交
179 180
{
    CPUState *env = opaque;
181
    const struct arm_boot_info *info = env->boot_info;
P
Paul Brook 已提交
182 183 184 185 186 187 188 189

    cpu_reset(env);
    if (info) {
        if (!info->is_linux) {
            /* Jump to the entry point.  */
            env->regs[15] = info->entry & 0xfffffffe;
            env->thumb = info->entry & 1;
        } else {
190 191 192 193 194 195 196
            if (env == first_cpu) {
                env->regs[15] = info->loader_start;
                if (old_param) {
                    set_kernel_args_old(info, info->initrd_size,
                                        info->loader_start);
                } else {
                    set_kernel_args(info, info->initrd_size,
P
Paul Brook 已提交
197
                                    info->loader_start);
198
                }
P
Paul Brook 已提交
199
            } else {
200
                stl_phys_notdirty(info->smp_bootreg_addr, 0);
201
                env->regs[15] = info->smp_loader_start;
P
Paul Brook 已提交
202 203 204 205 206
            }
        }
    }
}

207
void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
208 209 210 211
{
    int kernel_size;
    int initrd_size;
    int n;
P
pbrook 已提交
212 213
    int is_linux = 0;
    uint64_t elf_entry;
A
Anthony Liguori 已提交
214
    target_phys_addr_t entry;
B
Blue Swirl 已提交
215
    int big_endian;
216 217

    /* Load the kernel.  */
218
    if (!info->kernel_filename) {
219 220 221
        fprintf(stderr, "Kernel image must be specified\n");
        exit(1);
    }
P
pbrook 已提交
222

P
Paul Brook 已提交
223 224
    if (info->nb_cpus == 0)
        info->nb_cpus = 1;
225

B
Blue Swirl 已提交
226 227 228 229 230 231
#ifdef TARGET_WORDS_BIGENDIAN
    big_endian = 1;
#else
    big_endian = 0;
#endif

P
pbrook 已提交
232
    /* Assume that raw images are linux kernels, and ELF images are not.  */
233 234
    kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
                           NULL, NULL, big_endian, ELF_MACHINE, 1);
P
pbrook 已提交
235 236
    entry = elf_entry;
    if (kernel_size < 0) {
237 238
        kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
                                  &is_linux);
P
pbrook 已提交
239 240
    }
    if (kernel_size < 0) {
241
        entry = info->loader_start + KERNEL_LOAD_ADDR;
P
pbrook 已提交
242 243
        kernel_size = load_image_targphys(info->kernel_filename, entry,
                                          ram_size - KERNEL_LOAD_ADDR);
P
pbrook 已提交
244 245 246
        is_linux = 1;
    }
    if (kernel_size < 0) {
247 248
        fprintf(stderr, "qemu: could not load kernel '%s'\n",
                info->kernel_filename);
P
pbrook 已提交
249 250
        exit(1);
    }
P
Paul Brook 已提交
251 252
    info->entry = entry;
    if (is_linux) {
253
        if (info->initrd_filename) {
P
pbrook 已提交
254 255 256 257
            initrd_size = load_image_targphys(info->initrd_filename,
                                              info->loader_start
                                              + INITRD_LOAD_ADDR,
                                              ram_size - INITRD_LOAD_ADDR);
P
pbrook 已提交
258 259
            if (initrd_size < 0) {
                fprintf(stderr, "qemu: could not load initrd '%s'\n",
260
                        info->initrd_filename);
P
pbrook 已提交
261 262 263 264 265
                exit(1);
            }
        } else {
            initrd_size = 0;
        }
266 267 268
        bootloader[1] |= info->board_id & 0xff;
        bootloader[2] |= (info->board_id >> 8) & 0xff;
        bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
P
pbrook 已提交
269
        bootloader[6] = entry;
270
        for (n = 0; n < sizeof(bootloader) / 4; n++) {
P
Paul Brook 已提交
271
            bootloader[n] = tswap32(bootloader[n]);
272
        }
P
Paul Brook 已提交
273 274
        rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
                           info->loader_start);
275
        if (info->nb_cpus > 1) {
276 277 278
            smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
            smpboot[ARRAY_SIZE(smpboot) - 2] = info->smp_priv_base;
            for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
P
Paul Brook 已提交
279
                smpboot[n] = tswap32(smpboot[n]);
280
            }
P
Paul Brook 已提交
281 282
            rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
                               info->smp_loader_start);
283
        }
P
Paul Brook 已提交
284
        info->initrd_size = initrd_size;
285
    }
P
Paul Brook 已提交
286
    info->is_linux = is_linux;
287 288 289 290 291

    for (; env; env = env->next_cpu) {
        env->boot_info = info;
        qemu_register_reset(do_cpu_reset, env);
    }
292
}