]>
Commit | Line | Data |
---|---|---|
d94e7434 PC |
1 | /* |
2 | * Microblaze kernel loader | |
3 | * | |
4 | * Copyright (c) 2012 Peter Crosthwaite <[email protected]> | |
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.h" | |
29 | #include "qemu-common.h" | |
30 | #include "device_tree.h" | |
31 | #include "loader.h" | |
32 | #include "elf.h" | |
33 | ||
34 | #include "microblaze_boot.h" | |
35 | ||
36 | static struct | |
37 | { | |
ee118507 | 38 | void (*machine_cpu_reset)(CPUMBState *); |
d94e7434 PC |
39 | uint32_t bootstrap_pc; |
40 | uint32_t cmdline; | |
41 | uint32_t fdt; | |
42 | } boot_info; | |
43 | ||
44 | static void main_cpu_reset(void *opaque) | |
45 | { | |
ee118507 | 46 | CPUMBState *env = opaque; |
d94e7434 | 47 | |
1bba0dc9 | 48 | cpu_state_reset(env); |
d94e7434 PC |
49 | env->regs[5] = boot_info.cmdline; |
50 | env->regs[7] = boot_info.fdt; | |
51 | env->sregs[SR_PC] = boot_info.bootstrap_pc; | |
52 | if (boot_info.machine_cpu_reset) { | |
53 | boot_info.machine_cpu_reset(env); | |
54 | } | |
55 | } | |
56 | ||
57 | static int microblaze_load_dtb(target_phys_addr_t addr, | |
58 | uint32_t ramsize, | |
59 | const char *kernel_cmdline, | |
60 | const char *dtb_filename) | |
61 | { | |
d94e7434 PC |
62 | int fdt_size; |
63 | #ifdef CONFIG_FDT | |
da71ebd1 | 64 | void *fdt = NULL; |
d94e7434 PC |
65 | int r; |
66 | ||
da71ebd1 PC |
67 | if (dtb_filename) { |
68 | fdt = load_device_tree(dtb_filename, &fdt_size); | |
69 | } | |
d94e7434 | 70 | if (!fdt) { |
da71ebd1 | 71 | return 0; |
d94e7434 PC |
72 | } |
73 | ||
74 | if (kernel_cmdline) { | |
75 | r = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs", | |
76 | kernel_cmdline); | |
77 | if (r < 0) { | |
78 | fprintf(stderr, "couldn't set /chosen/bootargs\n"); | |
79 | } | |
80 | } | |
81 | ||
82 | cpu_physical_memory_write(addr, (void *)fdt, fdt_size); | |
83 | #else | |
84 | /* We lack libfdt so we cannot manipulate the fdt. Just pass on the blob | |
85 | to the kernel. */ | |
da71ebd1 PC |
86 | if (dtb_filename) { |
87 | fdt_size = load_image_targphys(dtb_filename, addr, 0x10000); | |
d94e7434 | 88 | } |
d94e7434 PC |
89 | if (kernel_cmdline) { |
90 | fprintf(stderr, | |
91 | "Warning: missing libfdt, cannot pass cmdline to kernel!\n"); | |
92 | } | |
93 | #endif | |
94 | return fdt_size; | |
95 | } | |
96 | ||
97 | static uint64_t translate_kernel_address(void *opaque, uint64_t addr) | |
98 | { | |
99 | return addr - 0x30000000LL; | |
100 | } | |
101 | ||
ee118507 | 102 | void microblaze_load_kernel(CPUMBState *env, target_phys_addr_t ddr_base, |
d94e7434 | 103 | uint32_t ramsize, const char *dtb_filename, |
ee118507 | 104 | void (*machine_cpu_reset)(CPUMBState *)) |
d94e7434 PC |
105 | { |
106 | ||
107 | QemuOpts *machine_opts; | |
108 | const char *kernel_filename = NULL; | |
109 | const char *kernel_cmdline = NULL; | |
110 | ||
111 | machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0); | |
112 | if (machine_opts) { | |
da71ebd1 | 113 | const char *dtb_arg; |
d94e7434 PC |
114 | kernel_filename = qemu_opt_get(machine_opts, "kernel"); |
115 | kernel_cmdline = qemu_opt_get(machine_opts, "append"); | |
da71ebd1 PC |
116 | dtb_arg = qemu_opt_get(machine_opts, "dtb"); |
117 | if (dtb_arg) { /* Preference a -dtb argument */ | |
118 | dtb_filename = dtb_arg; | |
119 | } else { /* default to pcbios dtb as passed by machine_init */ | |
120 | dtb_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename); | |
121 | } | |
d94e7434 PC |
122 | } |
123 | ||
124 | boot_info.machine_cpu_reset = machine_cpu_reset; | |
125 | qemu_register_reset(main_cpu_reset, env); | |
126 | ||
127 | if (kernel_filename) { | |
128 | int kernel_size; | |
129 | uint64_t entry, low, high; | |
130 | uint32_t base32; | |
131 | int big_endian = 0; | |
132 | ||
133 | #ifdef TARGET_WORDS_BIGENDIAN | |
134 | big_endian = 1; | |
135 | #endif | |
136 | ||
137 | /* Boots a kernel elf binary. */ | |
138 | kernel_size = load_elf(kernel_filename, NULL, NULL, | |
139 | &entry, &low, &high, | |
140 | big_endian, ELF_MACHINE, 0); | |
141 | base32 = entry; | |
142 | if (base32 == 0xc0000000) { | |
143 | kernel_size = load_elf(kernel_filename, translate_kernel_address, | |
144 | NULL, &entry, NULL, NULL, | |
145 | big_endian, ELF_MACHINE, 0); | |
146 | } | |
147 | /* Always boot into physical ram. */ | |
148 | boot_info.bootstrap_pc = ddr_base + (entry & 0x0fffffff); | |
149 | ||
150 | /* If it wasn't an ELF image, try an u-boot image. */ | |
151 | if (kernel_size < 0) { | |
152 | target_phys_addr_t uentry, loadaddr; | |
153 | ||
154 | kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0); | |
155 | boot_info.bootstrap_pc = uentry; | |
156 | high = (loadaddr + kernel_size + 3) & ~3; | |
157 | } | |
158 | ||
159 | /* Not an ELF image nor an u-boot image, try a RAW image. */ | |
160 | if (kernel_size < 0) { | |
161 | kernel_size = load_image_targphys(kernel_filename, ddr_base, | |
162 | ram_size); | |
163 | boot_info.bootstrap_pc = ddr_base; | |
164 | high = (ddr_base + kernel_size + 3) & ~3; | |
165 | } | |
166 | ||
167 | boot_info.cmdline = high + 4096; | |
168 | if (kernel_cmdline && strlen(kernel_cmdline)) { | |
169 | pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline); | |
170 | } | |
171 | /* Provide a device-tree. */ | |
172 | boot_info.fdt = boot_info.cmdline + 4096; | |
173 | microblaze_load_dtb(boot_info.fdt, ram_size, kernel_cmdline, | |
174 | dtb_filename); | |
175 | } | |
176 | ||
177 | } |