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
28
29
30
31#include "qemu/osdep.h"
32#include "qemu/units.h"
33#include "qemu/datadir.h"
34#include "qemu/option.h"
35#include "qemu/config-file.h"
36#include "qemu/error-report.h"
37#include "qemu/guest-random.h"
38#include "sysemu/device_tree.h"
39#include "sysemu/reset.h"
40#include "hw/boards.h"
41#include "hw/loader.h"
42#include "elf.h"
43
44#include "boot.h"
45
46#define NIOS2_MAGIC 0x534f494e
47
48static struct nios2_boot_info {
49 void (*machine_cpu_reset)(Nios2CPU *);
50 uint32_t bootstrap_pc;
51 uint32_t cmdline;
52 uint32_t initrd_start;
53 uint32_t initrd_end;
54 uint32_t fdt;
55} boot_info;
56
57static void main_cpu_reset(void *opaque)
58{
59 Nios2CPU *cpu = opaque;
60 CPUState *cs = CPU(cpu);
61 CPUNios2State *env = &cpu->env;
62
63 cpu_reset(CPU(cpu));
64
65 env->regs[R_ARG0] = NIOS2_MAGIC;
66 env->regs[R_ARG1] = boot_info.initrd_start;
67 env->regs[R_ARG2] = boot_info.fdt;
68 env->regs[R_ARG3] = boot_info.cmdline;
69
70 cpu_set_pc(cs, boot_info.bootstrap_pc);
71 if (boot_info.machine_cpu_reset) {
72 boot_info.machine_cpu_reset(cpu);
73 }
74}
75
76static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
77{
78 return addr - 0xc0000000LL;
79}
80
81static int nios2_load_dtb(struct nios2_boot_info bi, const uint32_t ramsize,
82 const char *kernel_cmdline, const char *dtb_filename)
83{
84 int fdt_size;
85 void *fdt = NULL;
86 int r;
87 uint8_t rng_seed[32];
88
89 if (dtb_filename) {
90 fdt = load_device_tree(dtb_filename, &fdt_size);
91 }
92 if (!fdt) {
93 return 0;
94 }
95
96 qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
97 qemu_fdt_setprop(fdt, "/chosen", "rng-seed", rng_seed, sizeof(rng_seed));
98
99 if (kernel_cmdline) {
100 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
101 kernel_cmdline);
102 if (r < 0) {
103 fprintf(stderr, "couldn't set /chosen/bootargs\n");
104 }
105 }
106
107 if (bi.initrd_start) {
108 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
109 translate_kernel_address(NULL, bi.initrd_start));
110
111 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
112 translate_kernel_address(NULL, bi.initrd_end));
113 }
114
115 cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
116 g_free(fdt);
117 return fdt_size;
118}
119
120void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
121 uint32_t ramsize,
122 const char *initrd_filename,
123 const char *dtb_filename,
124 void (*machine_cpu_reset)(Nios2CPU *))
125{
126 const char *kernel_filename;
127 const char *kernel_cmdline;
128 const char *dtb_arg;
129 char *filename = NULL;
130
131 kernel_filename = current_machine->kernel_filename;
132 kernel_cmdline = current_machine->kernel_cmdline;
133 dtb_arg = current_machine->dtb;
134
135 if (!dtb_arg) {
136 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
137 }
138
139 boot_info.machine_cpu_reset = machine_cpu_reset;
140 qemu_register_reset(main_cpu_reset, cpu);
141
142 if (kernel_filename) {
143 int kernel_size, fdt_size;
144 uint64_t entry, high;
145 int big_endian = 0;
146
147#if TARGET_BIG_ENDIAN
148 big_endian = 1;
149#endif
150
151
152 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
153 &entry, NULL, &high, NULL,
154 big_endian, EM_ALTERA_NIOS2, 0, 0);
155 if ((uint32_t)entry == 0xc0000000) {
156
157
158
159
160
161
162 kernel_size = load_elf(kernel_filename, NULL,
163 translate_kernel_address, NULL,
164 &entry, NULL, NULL, NULL,
165 big_endian, EM_ALTERA_NIOS2, 0, 0);
166 boot_info.bootstrap_pc = ddr_base + 0xc0000000 +
167 (entry & 0x07ffffff);
168 } else {
169
170 boot_info.bootstrap_pc = (uint32_t)entry;
171 }
172
173
174 if (kernel_size < 0) {
175 hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
176
177 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
178 NULL, NULL);
179 boot_info.bootstrap_pc = uentry;
180 high = loadaddr + kernel_size;
181 }
182
183
184 if (kernel_size < 0) {
185 kernel_size = load_image_targphys(kernel_filename, ddr_base,
186 ramsize);
187 boot_info.bootstrap_pc = ddr_base;
188 high = ddr_base + kernel_size;
189 }
190
191 high = ROUND_UP(high, 1 * MiB);
192
193
194 if (initrd_filename) {
195 int initrd_size;
196 uint32_t initrd_offset;
197
198 boot_info.initrd_start = high;
199 initrd_offset = boot_info.initrd_start - ddr_base;
200
201 initrd_size = load_ramdisk(initrd_filename,
202 boot_info.initrd_start,
203 ramsize - initrd_offset);
204 if (initrd_size < 0) {
205 initrd_size = load_image_targphys(initrd_filename,
206 boot_info.initrd_start,
207 ramsize - initrd_offset);
208 }
209 if (initrd_size < 0) {
210 error_report("could not load initrd '%s'",
211 initrd_filename);
212 exit(EXIT_FAILURE);
213 }
214 high += initrd_size;
215 }
216 high = ROUND_UP(high, 4);
217 boot_info.initrd_end = high;
218
219
220 boot_info.fdt = high;
221 fdt_size = nios2_load_dtb(boot_info, ramsize, kernel_cmdline,
222
223 dtb_arg ? dtb_arg : filename);
224 high += fdt_size;
225
226
227 boot_info.cmdline = ROUND_UP(high, 4 * KiB);
228 if (kernel_cmdline && strlen(kernel_cmdline)) {
229 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
230 }
231 }
232 g_free(filename);
233}
234