qemu/hw/microblaze/boot.c
<<
>>
Prefs
   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/osdep.h"
  28#include "qemu-common.h"
  29#include "cpu.h"
  30#include "qemu/option.h"
  31#include "qemu/config-file.h"
  32#include "qemu/error-report.h"
  33#include "qemu-common.h"
  34#include "sysemu/device_tree.h"
  35#include "sysemu/sysemu.h"
  36#include "hw/loader.h"
  37#include "elf.h"
  38#include "qemu/cutils.h"
  39
  40#include "boot.h"
  41
  42static struct
  43{
  44    void (*machine_cpu_reset)(MicroBlazeCPU *);
  45    uint32_t bootstrap_pc;
  46    uint32_t cmdline;
  47    uint32_t initrd_start;
  48    uint32_t initrd_end;
  49    uint32_t fdt;
  50} boot_info;
  51
  52static void main_cpu_reset(void *opaque)
  53{
  54    MicroBlazeCPU *cpu = opaque;
  55    CPUState *cs = CPU(cpu);
  56    CPUMBState *env = &cpu->env;
  57
  58    cpu_reset(cs);
  59    env->regs[5] = boot_info.cmdline;
  60    env->regs[6] = boot_info.initrd_start;
  61    env->regs[7] = boot_info.fdt;
  62    cpu_set_pc(cs, boot_info.bootstrap_pc);
  63    if (boot_info.machine_cpu_reset) {
  64        boot_info.machine_cpu_reset(cpu);
  65    }
  66}
  67
  68static int microblaze_load_dtb(hwaddr addr,
  69                               uint32_t ramsize,
  70                               uint32_t initrd_start,
  71                               uint32_t initrd_end,
  72                               const char *kernel_cmdline,
  73                               const char *dtb_filename,
  74                               void *fdt,
  75                               int fdt_size)
  76{
  77    int r;
  78
  79    if (!fdt) {
  80        /* No fdt information was passed in, load it from the DTB */
  81        if (dtb_filename) {
  82            fdt = load_device_tree(dtb_filename, &fdt_size);
  83        }
  84        if (!fdt) {
  85            return 0;
  86        }
  87    }
  88
  89    if (kernel_cmdline) {
  90        r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
  91                                    kernel_cmdline);
  92        if (r < 0) {
  93            fprintf(stderr, "couldn't set /chosen/bootargs\n");
  94        }
  95    }
  96
  97    if (initrd_start) {
  98        qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
  99                              initrd_start);
 100
 101        qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
 102                              initrd_end);
 103    }
 104
 105    cpu_physical_memory_write(addr, fdt, fdt_size);
 106    return fdt_size;
 107}
 108
 109static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
 110{
 111    return addr - 0x30000000LL;
 112}
 113
 114void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base,
 115                            uint32_t ramsize,
 116                            const char *initrd_filename,
 117                            const char *dtb_filename,
 118                            void (*machine_cpu_reset)(MicroBlazeCPU *),
 119                            void *fdt, int fdt_size)
 120{
 121    QemuOpts *machine_opts;
 122    const char *kernel_filename;
 123    const char *kernel_cmdline;
 124    const char *dtb_arg;
 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    if (!fdt) {
 131        if (dtb_arg) { /* Preference a -dtb argument */
 132            dtb_filename = dtb_arg;
 133        } else { /* default to pcbios dtb as passed by machine_init */
 134            dtb_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
 135        }
 136    }
 137
 138    boot_info.machine_cpu_reset = machine_cpu_reset;
 139    qemu_register_reset(main_cpu_reset, cpu);
 140
 141    if (kernel_filename) {
 142        int kernel_size;
 143        uint64_t entry, low, high;
 144        uint32_t base32;
 145        int big_endian = 0;
 146
 147#ifdef TARGET_WORDS_BIGENDIAN
 148        big_endian = 1;
 149#endif
 150
 151        /* Boots a kernel elf binary.  */
 152        kernel_size = load_elf(kernel_filename, NULL, NULL,
 153                               &entry, &low, &high,
 154                               big_endian, EM_MICROBLAZE, 0, 0);
 155        base32 = entry;
 156        if (base32 == 0xc0000000) {
 157            kernel_size = load_elf(kernel_filename, translate_kernel_address,
 158                                   NULL, &entry, NULL, NULL,
 159                                   big_endian, EM_MICROBLAZE, 0, 0);
 160        }
 161        /* Always boot into physical ram.  */
 162        boot_info.bootstrap_pc = (uint32_t)entry;
 163
 164        /* If it wasn't an ELF image, try an u-boot image.  */
 165        if (kernel_size < 0) {
 166            hwaddr uentry, loadaddr;
 167
 168            kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
 169                                      NULL, NULL);
 170            boot_info.bootstrap_pc = uentry;
 171            high = (loadaddr + kernel_size + 3) & ~3;
 172        }
 173
 174        /* Not an ELF image nor an u-boot image, try a RAW image.  */
 175        if (kernel_size < 0) {
 176            kernel_size = load_image_targphys(kernel_filename, ddr_base,
 177                                              ram_size);
 178            boot_info.bootstrap_pc = ddr_base;
 179            high = (ddr_base + kernel_size + 3) & ~3;
 180        }
 181
 182        if (initrd_filename) {
 183            int initrd_size;
 184            uint32_t initrd_offset;
 185
 186            high = ROUND_UP(high + kernel_size, 4);
 187            boot_info.initrd_start = high;
 188            initrd_offset = boot_info.initrd_start - ddr_base;
 189
 190            initrd_size = load_ramdisk(initrd_filename,
 191                                       boot_info.initrd_start,
 192                                       ram_size - initrd_offset);
 193            if (initrd_size < 0) {
 194                initrd_size = load_image_targphys(initrd_filename,
 195                                                  boot_info.initrd_start,
 196                                                  ram_size - initrd_offset);
 197            }
 198            if (initrd_size < 0) {
 199                error_report("qemu: could not load initrd '%s'",
 200                             initrd_filename);
 201                exit(EXIT_FAILURE);
 202            }
 203            boot_info.initrd_end = boot_info.initrd_start + initrd_size;
 204            high = ROUND_UP(high + initrd_size, 4);
 205        }
 206
 207        boot_info.cmdline = high + 4096;
 208        if (kernel_cmdline && strlen(kernel_cmdline)) {
 209            pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
 210        }
 211        /* Provide a device-tree.  */
 212        boot_info.fdt = boot_info.cmdline + 4096;
 213        microblaze_load_dtb(boot_info.fdt, ram_size,
 214                            boot_info.initrd_start,
 215                            boot_info.initrd_end,
 216                            kernel_cmdline,
 217                            dtb_filename,
 218                            fdt,
 219                            fdt_size);
 220    }
 221
 222}
 223