Simple u-boot image loading support.
[qemu] / hw / arm_boot.c
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
27 static void set_kernel_args(uint32_t ram_size, int initrd_size,
28                             const char *kernel_cmdline)
29 {
30     uint32_t *p;
31
32     p = (uint32_t *)(phys_ram_base + KERNEL_ARGS_ADDR);
33     /* ATAG_CORE */
34     stl_raw(p++, 5);
35     stl_raw(p++, 0x54410001);
36     stl_raw(p++, 1);
37     stl_raw(p++, 0x1000);
38     stl_raw(p++, 0);
39     /* ATAG_MEM */
40     stl_raw(p++, 4);
41     stl_raw(p++, 0x54410002);
42     stl_raw(p++, ram_size);
43     stl_raw(p++, 0);
44     if (initrd_size) {
45         /* ATAG_INITRD2 */
46         stl_raw(p++, 4);
47         stl_raw(p++, 0x54420005);
48         stl_raw(p++, INITRD_LOAD_ADDR);
49         stl_raw(p++, initrd_size);
50     }
51     if (kernel_cmdline && *kernel_cmdline) {
52         /* ATAG_CMDLINE */
53         int cmdline_size;
54
55         cmdline_size = strlen(kernel_cmdline);
56         memcpy (p + 2, kernel_cmdline, cmdline_size + 1);
57         cmdline_size = (cmdline_size >> 2) + 1;
58         stl_raw(p++, cmdline_size + 2);
59         stl_raw(p++, 0x54410009);
60         p += cmdline_size;
61     }
62     /* ATAG_END */
63     stl_raw(p++, 0);
64     stl_raw(p++, 0);
65 }
66
67 void arm_load_kernel(CPUState *env, int ram_size, const char *kernel_filename,
68                      const char *kernel_cmdline, const char *initrd_filename,
69                      int board_id)
70 {
71     int kernel_size;
72     int initrd_size;
73     int n;
74     int is_linux = 0;
75     uint64_t elf_entry;
76     target_ulong entry;
77
78     /* Load the kernel.  */
79     if (!kernel_filename) {
80         fprintf(stderr, "Kernel image must be specified\n");
81         exit(1);
82     }
83
84     /* Assume that raw images are linux kernels, and ELF images are not.  */
85     kernel_size = load_elf(kernel_filename, 0, &elf_entry);
86     entry = elf_entry;
87     if (kernel_size < 0) {
88         kernel_size = load_uboot(kernel_filename, &entry, &is_linux);
89     }
90     if (kernel_size < 0) {
91         kernel_size = load_image(kernel_filename,
92                                  phys_ram_base + KERNEL_LOAD_ADDR);
93         entry = KERNEL_LOAD_ADDR;
94         is_linux = 1;
95     }
96     if (kernel_size < 0) {
97         fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
98         exit(1);
99     }
100     if (!is_linux) {
101         /* Jump to the entry point.  */
102         env->regs[15] = entry & 0xfffffffe;
103         env->thumb = entry & 1;
104     } else {
105         if (initrd_filename) {
106             initrd_size = load_image(initrd_filename,
107                                      phys_ram_base + INITRD_LOAD_ADDR);
108             if (initrd_size < 0) {
109                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
110                         initrd_filename);
111                 exit(1);
112             }
113         } else {
114             initrd_size = 0;
115         }
116         bootloader[1] |= board_id & 0xff;
117         bootloader[2] |= (board_id >> 8) & 0xff;
118         bootloader[5] = KERNEL_ARGS_ADDR;
119         bootloader[6] = entry;
120         for (n = 0; n < sizeof(bootloader) / 4; n++)
121             stl_raw(phys_ram_base + (n * 4), bootloader[n]);
122         set_kernel_args(ram_size, initrd_size, kernel_cmdline);
123     }
124 }
125