1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "qemu-common.h"
29#include "cpu.h"
30#include "qemu/option.h"
31#include "qemu/config-file.h"
32#include "qemu/error-report.h"
33#include "qemu-common.h"
34#include "sysemu/device_tree.h"
35#include "sysemu/sysemu.h"
36#include "hw/loader.h"
37#include "elf.h"
38#include "qemu/cutils.h"
39
40#include "boot.h"
41
42static struct
43{
44 void (*machine_cpu_reset)(MicroBlazeCPU *);
45 uint32_t bootstrap_pc;
46 uint32_t cmdline;
47 uint32_t initrd_start;
48 uint32_t initrd_end;
49 uint32_t fdt;
50} boot_info;
51
52static void main_cpu_reset(void *opaque)
53{
54 MicroBlazeCPU *cpu = opaque;
55 CPUState *cs = CPU(cpu);
56 CPUMBState *env = &cpu->env;
57
58 cpu_reset(cs);
59 env->regs[5] = boot_info.cmdline;
60 env->regs[6] = boot_info.initrd_start;
61 env->regs[7] = boot_info.fdt;
62 cpu_set_pc(cs, boot_info.bootstrap_pc);
63 if (boot_info.machine_cpu_reset) {
64 boot_info.machine_cpu_reset(cpu);
65 }
66}
67
68static int microblaze_load_dtb(hwaddr addr,
69 uint32_t ramsize,
70 uint32_t initrd_start,
71 uint32_t initrd_end,
72 const char *kernel_cmdline,
73 const char *dtb_filename)
74{
75 int fdt_size;
76 void *fdt = NULL;
77 int r;
78
79 if (dtb_filename) {
80 fdt = load_device_tree(dtb_filename, &fdt_size);
81 }
82 if (!fdt) {
83 return 0;
84 }
85
86 if (kernel_cmdline) {
87 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
88 kernel_cmdline);
89 if (r < 0) {
90 fprintf(stderr, "couldn't set /chosen/bootargs\n");
91 }
92 }
93
94 if (initrd_start) {
95 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
96 initrd_start);
97
98 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
99 initrd_end);
100 }
101
102 cpu_physical_memory_write(addr, fdt, fdt_size);
103 return fdt_size;
104}
105
106static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
107{
108 return addr - 0x30000000LL;
109}
110
111void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base,
112 uint32_t ramsize,
113 const char *initrd_filename,
114 const char *dtb_filename,
115 void (*machine_cpu_reset)(MicroBlazeCPU *))
116{
117 QemuOpts *machine_opts;
118 const char *kernel_filename;
119 const char *kernel_cmdline;
120 const char *dtb_arg;
121 char *filename = NULL;
122
123 machine_opts = qemu_get_machine_opts();
124 kernel_filename = qemu_opt_get(machine_opts, "kernel");
125 kernel_cmdline = qemu_opt_get(machine_opts, "append");
126 dtb_arg = qemu_opt_get(machine_opts, "dtb");
127
128 if (!dtb_arg) {
129 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
130 }
131
132 boot_info.machine_cpu_reset = machine_cpu_reset;
133 qemu_register_reset(main_cpu_reset, cpu);
134
135 if (kernel_filename) {
136 int kernel_size;
137 uint64_t entry, low, high;
138 uint32_t base32;
139 int big_endian = 0;
140
141#ifdef TARGET_WORDS_BIGENDIAN
142 big_endian = 1;
143#endif
144
145
146 kernel_size = load_elf(kernel_filename, NULL, NULL,
147 &entry, &low, &high,
148 big_endian, EM_MICROBLAZE, 0, 0);
149 base32 = entry;
150 if (base32 == 0xc0000000) {
151 kernel_size = load_elf(kernel_filename, translate_kernel_address,
152 NULL, &entry, NULL, NULL,
153 big_endian, EM_MICROBLAZE, 0, 0);
154 }
155
156 boot_info.bootstrap_pc = (uint32_t)entry;
157
158
159 if (kernel_size < 0) {
160 hwaddr uentry, loadaddr;
161
162 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
163 NULL, NULL);
164 boot_info.bootstrap_pc = uentry;
165 high = (loadaddr + kernel_size + 3) & ~3;
166 }
167
168
169 if (kernel_size < 0) {
170 kernel_size = load_image_targphys(kernel_filename, ddr_base,
171 ram_size);
172 boot_info.bootstrap_pc = ddr_base;
173 high = (ddr_base + kernel_size + 3) & ~3;
174 }
175
176 if (initrd_filename) {
177 int initrd_size;
178 uint32_t initrd_offset;
179
180 high = ROUND_UP(high + kernel_size, 4);
181 boot_info.initrd_start = high;
182 initrd_offset = boot_info.initrd_start - ddr_base;
183
184 initrd_size = load_ramdisk(initrd_filename,
185 boot_info.initrd_start,
186 ram_size - initrd_offset);
187 if (initrd_size < 0) {
188 initrd_size = load_image_targphys(initrd_filename,
189 boot_info.initrd_start,
190 ram_size - initrd_offset);
191 }
192 if (initrd_size < 0) {
193 error_report("qemu: could not load initrd '%s'",
194 initrd_filename);
195 exit(EXIT_FAILURE);
196 }
197 boot_info.initrd_end = boot_info.initrd_start + initrd_size;
198 high = ROUND_UP(high + initrd_size, 4);
199 }
200
201 boot_info.cmdline = high + 4096;
202 if (kernel_cmdline && strlen(kernel_cmdline)) {
203 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
204 }
205
206 boot_info.fdt = boot_info.cmdline + 4096;
207 microblaze_load_dtb(boot_info.fdt, ram_size,
208 boot_info.initrd_start,
209 boot_info.initrd_end,
210 kernel_cmdline,
211
212 dtb_arg ? dtb_arg : filename);
213 }
214 g_free(filename);
215}
216