]> git.proxmox.com Git - mirror_qemu.git/blame - hw/nios2/boot.c
Do not include cpu.h if it's not really necessary
[mirror_qemu.git] / hw / nios2 / boot.c
CommitLineData
b7862564
MV
1/*
2 * Nios2 kernel loader
3 *
4 * Copyright (c) 2016 Marek Vasut <marek.vasut@gmail.com>
5 *
6 * Based on microblaze kernel loader
7 *
8 * Copyright (c) 2012 Peter Crosthwaite <peter.crosthwaite@petalogix.com>
9 * Copyright (c) 2012 PetaLogix
10 * Copyright (c) 2009 Edgar E. Iglesias.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
29 */
30
31#include "qemu/osdep.h"
a3c81ef9 32#include "qemu/units.h"
b7862564 33#include "qemu-common.h"
2c65db5e 34#include "qemu/datadir.h"
b7862564
MV
35#include "qemu/option.h"
36#include "qemu/config-file.h"
37#include "qemu/error-report.h"
b7862564 38#include "sysemu/device_tree.h"
71e8a915 39#include "sysemu/reset.h"
f2ce39b4 40#include "hw/boards.h"
b7862564
MV
41#include "hw/loader.h"
42#include "elf.h"
b7862564
MV
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
88 if (dtb_filename) {
89 fdt = load_device_tree(dtb_filename, &fdt_size);
90 }
91 if (!fdt) {
92 return 0;
93 }
94
95 if (kernel_cmdline) {
96 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
97 kernel_cmdline);
98 if (r < 0) {
99 fprintf(stderr, "couldn't set /chosen/bootargs\n");
100 }
101 }
102
103 if (bi.initrd_start) {
104 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
105 translate_kernel_address(NULL, bi.initrd_start));
106
107 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
108 translate_kernel_address(NULL, bi.initrd_end));
109 }
110
111 cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
757ca9b8 112 g_free(fdt);
b7862564
MV
113 return fdt_size;
114}
115
116void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
117 uint32_t ramsize,
118 const char *initrd_filename,
119 const char *dtb_filename,
120 void (*machine_cpu_reset)(Nios2CPU *))
121{
b7862564
MV
122 const char *kernel_filename;
123 const char *kernel_cmdline;
124 const char *dtb_arg;
125 char *filename = NULL;
126
f2ce39b4
PB
127 kernel_filename = current_machine->kernel_filename;
128 kernel_cmdline = current_machine->kernel_cmdline;
129 dtb_arg = current_machine->dtb;
b7862564
MV
130 /* default to pcbios dtb as passed by machine_init */
131 if (!dtb_arg) {
132 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
133 }
134
135 boot_info.machine_cpu_reset = machine_cpu_reset;
136 qemu_register_reset(main_cpu_reset, cpu);
137
138 if (kernel_filename) {
139 int kernel_size, fdt_size;
617160c9 140 uint64_t entry, high;
b7862564
MV
141 int big_endian = 0;
142
143#ifdef TARGET_WORDS_BIGENDIAN
144 big_endian = 1;
145#endif
146
147 /* Boots a kernel elf binary. */
4366e1db 148 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
617160c9 149 &entry, NULL, &high, NULL,
b7862564 150 big_endian, EM_ALTERA_NIOS2, 0, 0);
ed960ba9
SL
151 if ((uint32_t)entry == 0xc0000000) {
152 /*
153 * The Nios II processor reference guide documents that the
154 * kernel is placed at virtual memory address 0xc0000000,
155 * and we've got something that points there. Reload it
156 * and adjust the entry to get the address in physical RAM.
157 */
4366e1db
LM
158 kernel_size = load_elf(kernel_filename, NULL,
159 translate_kernel_address, NULL,
6cdda0ff 160 &entry, NULL, NULL, NULL,
b7862564 161 big_endian, EM_ALTERA_NIOS2, 0, 0);
ed960ba9
SL
162 boot_info.bootstrap_pc = ddr_base + 0xc0000000 +
163 (entry & 0x07ffffff);
164 } else {
165 /* Use the entry point in the ELF image. */
166 boot_info.bootstrap_pc = (uint32_t)entry;
b7862564
MV
167 }
168
b7862564
MV
169 /* If it wasn't an ELF image, try an u-boot image. */
170 if (kernel_size < 0) {
f831f955 171 hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
b7862564
MV
172
173 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
174 NULL, NULL);
175 boot_info.bootstrap_pc = uentry;
176 high = loadaddr + kernel_size;
177 }
178
179 /* Not an ELF image nor an u-boot image, try a RAW image. */
180 if (kernel_size < 0) {
181 kernel_size = load_image_targphys(kernel_filename, ddr_base,
541693a4 182 ramsize);
b7862564
MV
183 boot_info.bootstrap_pc = ddr_base;
184 high = ddr_base + kernel_size;
185 }
186
a3c81ef9 187 high = ROUND_UP(high, 1 * MiB);
b7862564
MV
188
189 /* If initrd is available, it goes after the kernel, aligned to 1M. */
190 if (initrd_filename) {
191 int initrd_size;
192 uint32_t initrd_offset;
193
194 boot_info.initrd_start = high;
195 initrd_offset = boot_info.initrd_start - ddr_base;
196
197 initrd_size = load_ramdisk(initrd_filename,
198 boot_info.initrd_start,
541693a4 199 ramsize - initrd_offset);
b7862564
MV
200 if (initrd_size < 0) {
201 initrd_size = load_image_targphys(initrd_filename,
202 boot_info.initrd_start,
541693a4 203 ramsize - initrd_offset);
b7862564
MV
204 }
205 if (initrd_size < 0) {
d0e31a10 206 error_report("could not load initrd '%s'",
b7862564
MV
207 initrd_filename);
208 exit(EXIT_FAILURE);
209 }
210 high += initrd_size;
211 }
212 high = ROUND_UP(high, 4);
213 boot_info.initrd_end = high;
214
215 /* Device tree must be placed right after initrd (if available) */
216 boot_info.fdt = high;
541693a4 217 fdt_size = nios2_load_dtb(boot_info, ramsize, kernel_cmdline,
b7862564
MV
218 /* Preference a -dtb argument */
219 dtb_arg ? dtb_arg : filename);
220 high += fdt_size;
221
222 /* Kernel command is at the end, 4k aligned. */
a3c81ef9 223 boot_info.cmdline = ROUND_UP(high, 4 * KiB);
b7862564
MV
224 if (kernel_cmdline && strlen(kernel_cmdline)) {
225 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
226 }
227 }
228 g_free(filename);
229}