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