xref: /openbmc/qemu/hw/microblaze/boot.c (revision 1d300b5f)
1 /*
2  * Microblaze kernel loader
3  *
4  * Copyright (c) 2012 Peter Crosthwaite <peter.crosthwaite@petalogix.com>
5  * Copyright (c) 2012 PetaLogix
6  * Copyright (c) 2009 Edgar E. Iglesias.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include "qemu/option.h"
28 #include "qemu/config-file.h"
29 #include "qemu-common.h"
30 #include "sysemu/device_tree.h"
31 #include "sysemu/sysemu.h"
32 #include "hw/loader.h"
33 #include "elf.h"
34 
35 #include "boot.h"
36 
37 static struct
38 {
39     void (*machine_cpu_reset)(MicroBlazeCPU *);
40     uint32_t bootstrap_pc;
41     uint32_t cmdline;
42     uint32_t fdt;
43 } boot_info;
44 
45 static void main_cpu_reset(void *opaque)
46 {
47     MicroBlazeCPU *cpu = opaque;
48     CPUMBState *env = &cpu->env;
49 
50     cpu_reset(CPU(cpu));
51     env->regs[5] = boot_info.cmdline;
52     env->regs[7] = boot_info.fdt;
53     env->sregs[SR_PC] = boot_info.bootstrap_pc;
54     if (boot_info.machine_cpu_reset) {
55         boot_info.machine_cpu_reset(cpu);
56     }
57 }
58 
59 static int microblaze_load_dtb(hwaddr addr,
60                                       uint32_t ramsize,
61                                       const char *kernel_cmdline,
62                                       const char *dtb_filename)
63 {
64     int fdt_size;
65     void *fdt = NULL;
66     int r;
67 
68     if (dtb_filename) {
69         fdt = load_device_tree(dtb_filename, &fdt_size);
70     }
71     if (!fdt) {
72         return 0;
73     }
74 
75     if (kernel_cmdline) {
76         r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
77                                                         kernel_cmdline);
78         if (r < 0) {
79             fprintf(stderr, "couldn't set /chosen/bootargs\n");
80         }
81     }
82 
83     cpu_physical_memory_write(addr, fdt, fdt_size);
84     return fdt_size;
85 }
86 
87 static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
88 {
89     return addr - 0x30000000LL;
90 }
91 
92 void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base,
93                             uint32_t ramsize, const char *dtb_filename,
94                             void (*machine_cpu_reset)(MicroBlazeCPU *))
95 {
96     QemuOpts *machine_opts;
97     const char *kernel_filename;
98     const char *kernel_cmdline;
99     const char *dtb_arg;
100 
101     machine_opts = qemu_get_machine_opts();
102     kernel_filename = qemu_opt_get(machine_opts, "kernel");
103     kernel_cmdline = qemu_opt_get(machine_opts, "append");
104     dtb_arg = qemu_opt_get(machine_opts, "dtb");
105     if (dtb_arg) { /* Preference a -dtb argument */
106         dtb_filename = dtb_arg;
107     } else { /* default to pcbios dtb as passed by machine_init */
108         dtb_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
109     }
110 
111     boot_info.machine_cpu_reset = machine_cpu_reset;
112     qemu_register_reset(main_cpu_reset, cpu);
113 
114     if (kernel_filename) {
115         int kernel_size;
116         uint64_t entry, low, high;
117         uint32_t base32;
118         int big_endian = 0;
119 
120 #ifdef TARGET_WORDS_BIGENDIAN
121         big_endian = 1;
122 #endif
123 
124         /* Boots a kernel elf binary.  */
125         kernel_size = load_elf(kernel_filename, NULL, NULL,
126                                &entry, &low, &high,
127                                big_endian, ELF_MACHINE, 0);
128         base32 = entry;
129         if (base32 == 0xc0000000) {
130             kernel_size = load_elf(kernel_filename, translate_kernel_address,
131                                    NULL, &entry, NULL, NULL,
132                                    big_endian, ELF_MACHINE, 0);
133         }
134         /* Always boot into physical ram.  */
135         boot_info.bootstrap_pc = ddr_base + (entry & 0x0fffffff);
136 
137         /* If it wasn't an ELF image, try an u-boot image.  */
138         if (kernel_size < 0) {
139             hwaddr uentry, loadaddr;
140 
141             kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0);
142             boot_info.bootstrap_pc = uentry;
143             high = (loadaddr + kernel_size + 3) & ~3;
144         }
145 
146         /* Not an ELF image nor an u-boot image, try a RAW image.  */
147         if (kernel_size < 0) {
148             kernel_size = load_image_targphys(kernel_filename, ddr_base,
149                                               ram_size);
150             boot_info.bootstrap_pc = ddr_base;
151             high = (ddr_base + kernel_size + 3) & ~3;
152         }
153 
154         boot_info.cmdline = high + 4096;
155         if (kernel_cmdline && strlen(kernel_cmdline)) {
156             pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
157         }
158         /* Provide a device-tree.  */
159         boot_info.fdt = boot_info.cmdline + 4096;
160         microblaze_load_dtb(boot_info.fdt, ram_size, kernel_cmdline,
161                                                      dtb_filename);
162     }
163 
164 }
165