blob: 5f0ab2fbb9c205331d2f4a55be16b3c9c3e17ea5 [file] [log] [blame]
Marek Vasutb7862562017-01-18 23:01:45 +01001/*
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"
Philippe Mathieu-Daudéa3c81ef2018-06-25 09:42:17 -030032#include "qemu/units.h"
Marek Vasutb7862562017-01-18 23:01:45 +010033#include "qemu-common.h"
34#include "cpu.h"
35#include "qemu/option.h"
36#include "qemu/config-file.h"
37#include "qemu/error-report.h"
Marek Vasutb7862562017-01-18 23:01:45 +010038#include "sysemu/device_tree.h"
39#include "sysemu/sysemu.h"
40#include "hw/loader.h"
41#include "elf.h"
Marek Vasutb7862562017-01-18 23:01:45 +010042
43#include "boot.h"
44
45#define NIOS2_MAGIC 0x534f494e
46
47static struct nios2_boot_info {
48 void (*machine_cpu_reset)(Nios2CPU *);
49 uint32_t bootstrap_pc;
50 uint32_t cmdline;
51 uint32_t initrd_start;
52 uint32_t initrd_end;
53 uint32_t fdt;
54} boot_info;
55
56static void main_cpu_reset(void *opaque)
57{
58 Nios2CPU *cpu = opaque;
59 CPUState *cs = CPU(cpu);
60 CPUNios2State *env = &cpu->env;
61
62 cpu_reset(CPU(cpu));
63
64 env->regs[R_ARG0] = NIOS2_MAGIC;
65 env->regs[R_ARG1] = boot_info.initrd_start;
66 env->regs[R_ARG2] = boot_info.fdt;
67 env->regs[R_ARG3] = boot_info.cmdline;
68
69 cpu_set_pc(cs, boot_info.bootstrap_pc);
70 if (boot_info.machine_cpu_reset) {
71 boot_info.machine_cpu_reset(cpu);
72 }
73}
74
75static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
76{
77 return addr - 0xc0000000LL;
78}
79
80static int nios2_load_dtb(struct nios2_boot_info bi, const uint32_t ramsize,
81 const char *kernel_cmdline, const char *dtb_filename)
82{
83 int fdt_size;
84 void *fdt = NULL;
85 int r;
86
87 if (dtb_filename) {
88 fdt = load_device_tree(dtb_filename, &fdt_size);
89 }
90 if (!fdt) {
91 return 0;
92 }
93
94 if (kernel_cmdline) {
95 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
96 kernel_cmdline);
97 if (r < 0) {
98 fprintf(stderr, "couldn't set /chosen/bootargs\n");
99 }
100 }
101
102 if (bi.initrd_start) {
103 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
104 translate_kernel_address(NULL, bi.initrd_start));
105
106 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
107 translate_kernel_address(NULL, bi.initrd_end));
108 }
109
110 cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
111 return fdt_size;
112}
113
114void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
115 uint32_t ramsize,
116 const char *initrd_filename,
117 const char *dtb_filename,
118 void (*machine_cpu_reset)(Nios2CPU *))
119{
120 QemuOpts *machine_opts;
121 const char *kernel_filename;
122 const char *kernel_cmdline;
123 const char *dtb_arg;
124 char *filename = NULL;
125
126 machine_opts = qemu_get_machine_opts();
127 kernel_filename = qemu_opt_get(machine_opts, "kernel");
128 kernel_cmdline = qemu_opt_get(machine_opts, "append");
129 dtb_arg = qemu_opt_get(machine_opts, "dtb");
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;
140 uint64_t entry, low, high;
141 uint32_t base32;
142 int big_endian = 0;
143
144#ifdef TARGET_WORDS_BIGENDIAN
145 big_endian = 1;
146#endif
147
148 /* Boots a kernel elf binary. */
Liam Merwick4366e1d2019-01-15 12:18:03 +0000149 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
Marek Vasutb7862562017-01-18 23:01:45 +0100150 &entry, &low, &high,
151 big_endian, EM_ALTERA_NIOS2, 0, 0);
152 base32 = entry;
153 if (base32 == 0xc0000000) {
Liam Merwick4366e1d2019-01-15 12:18:03 +0000154 kernel_size = load_elf(kernel_filename, NULL,
155 translate_kernel_address, NULL,
156 &entry, NULL, NULL,
Marek Vasutb7862562017-01-18 23:01:45 +0100157 big_endian, EM_ALTERA_NIOS2, 0, 0);
158 }
159
160 /* Always boot into physical ram. */
161 boot_info.bootstrap_pc = ddr_base + 0xc0000000 + (entry & 0x07ffffff);
162
163 /* If it wasn't an ELF image, try an u-boot image. */
164 if (kernel_size < 0) {
Nick Hudsonf831f952019-01-07 08:31:50 +0000165 hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
Marek Vasutb7862562017-01-18 23:01:45 +0100166
167 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
168 NULL, NULL);
169 boot_info.bootstrap_pc = uentry;
170 high = loadaddr + kernel_size;
171 }
172
173 /* Not an ELF image nor an u-boot image, try a RAW image. */
174 if (kernel_size < 0) {
175 kernel_size = load_image_targphys(kernel_filename, ddr_base,
176 ram_size);
177 boot_info.bootstrap_pc = ddr_base;
178 high = ddr_base + kernel_size;
179 }
180
Philippe Mathieu-Daudéa3c81ef2018-06-25 09:42:17 -0300181 high = ROUND_UP(high, 1 * MiB);
Marek Vasutb7862562017-01-18 23:01:45 +0100182
183 /* If initrd is available, it goes after the kernel, aligned to 1M. */
184 if (initrd_filename) {
185 int initrd_size;
186 uint32_t initrd_offset;
187
188 boot_info.initrd_start = high;
189 initrd_offset = boot_info.initrd_start - ddr_base;
190
191 initrd_size = load_ramdisk(initrd_filename,
192 boot_info.initrd_start,
193 ram_size - initrd_offset);
194 if (initrd_size < 0) {
195 initrd_size = load_image_targphys(initrd_filename,
196 boot_info.initrd_start,
197 ram_size - initrd_offset);
198 }
199 if (initrd_size < 0) {
Ishani Chughd0e31a12017-04-13 21:44:39 +0530200 error_report("could not load initrd '%s'",
Marek Vasutb7862562017-01-18 23:01:45 +0100201 initrd_filename);
202 exit(EXIT_FAILURE);
203 }
204 high += initrd_size;
205 }
206 high = ROUND_UP(high, 4);
207 boot_info.initrd_end = high;
208
209 /* Device tree must be placed right after initrd (if available) */
210 boot_info.fdt = high;
211 fdt_size = nios2_load_dtb(boot_info, ram_size, kernel_cmdline,
212 /* Preference a -dtb argument */
213 dtb_arg ? dtb_arg : filename);
214 high += fdt_size;
215
216 /* Kernel command is at the end, 4k aligned. */
Philippe Mathieu-Daudéa3c81ef2018-06-25 09:42:17 -0300217 boot_info.cmdline = ROUND_UP(high, 4 * KiB);
Marek Vasutb7862562017-01-18 23:01:45 +0100218 if (kernel_cmdline && strlen(kernel_cmdline)) {
219 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
220 }
221 }
222 g_free(filename);
223}