qemu/hw/riscv/boot.c
<<
>>
Prefs
   1/*
   2 * QEMU RISC-V Boot Helper
   3 *
   4 * Copyright (c) 2017 SiFive, Inc.
   5 * Copyright (c) 2019 Alistair Francis <alistair.francis@wdc.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms and conditions of the GNU General Public License,
   9 * version 2 or later, as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 * You should have received a copy of the GNU General Public License along with
  17 * this program.  If not, see <http://www.gnu.org/licenses/>.
  18 */
  19
  20#include "qemu/osdep.h"
  21#include "qemu-common.h"
  22#include "qemu/units.h"
  23#include "qemu/error-report.h"
  24#include "exec/cpu-defs.h"
  25#include "hw/boards.h"
  26#include "hw/loader.h"
  27#include "hw/riscv/boot.h"
  28#include "elf.h"
  29#include "sysemu/qtest.h"
  30
  31#if defined(TARGET_RISCV32)
  32# define KERNEL_BOOT_ADDRESS 0x80400000
  33#else
  34# define KERNEL_BOOT_ADDRESS 0x80200000
  35#endif
  36
  37void riscv_find_and_load_firmware(MachineState *machine,
  38                                  const char *default_machine_firmware,
  39                                  hwaddr firmware_load_addr)
  40{
  41    char *firmware_filename = NULL;
  42
  43    if (!machine->firmware) {
  44        /*
  45         * The user didn't specify -bios.
  46         * At the moment we default to loading nothing when this hapens.
  47         * In the future this defaul will change to loading the prebuilt
  48         * OpenSBI firmware. Let's warn the user and then continue.
  49        */
  50        if (!qtest_enabled()) {
  51            warn_report("No -bios option specified. Not loading a firmware.");
  52            warn_report("This default will change in a future QEMU release. " \
  53                        "Please use the -bios option to avoid breakages when "\
  54                        "this happens.");
  55            warn_report("See QEMU's deprecation documentation for details.");
  56        }
  57        return;
  58    }
  59
  60    if (!strcmp(machine->firmware, "default")) {
  61        /*
  62         * The user has specified "-bios default". That means we are going to
  63         * load the OpenSBI binary included in the QEMU source.
  64         *
  65         * We can't load the binary by default as it will break existing users
  66         * as users are already loading their own firmware.
  67         *
  68         * Let's try to get everyone to specify the -bios option at all times,
  69         * so then in the future we can make "-bios default" the default option
  70         * if no -bios option is set without breaking anything.
  71         */
  72        firmware_filename = riscv_find_firmware(default_machine_firmware);
  73    } else if (strcmp(machine->firmware, "none")) {
  74        firmware_filename = riscv_find_firmware(machine->firmware);
  75    }
  76
  77    if (firmware_filename) {
  78        /* If not "none" load the firmware */
  79        riscv_load_firmware(firmware_filename, firmware_load_addr);
  80        g_free(firmware_filename);
  81    }
  82}
  83
  84char *riscv_find_firmware(const char *firmware_filename)
  85{
  86    char *filename;
  87
  88    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware_filename);
  89    if (filename == NULL) {
  90        error_report("Unable to load the RISC-V firmware \"%s\"",
  91                     firmware_filename);
  92        exit(1);
  93    }
  94
  95    return filename;
  96}
  97
  98target_ulong riscv_load_firmware(const char *firmware_filename,
  99                                 hwaddr firmware_load_addr)
 100{
 101    uint64_t firmware_entry, firmware_start, firmware_end;
 102
 103    if (load_elf(firmware_filename, NULL, NULL, NULL, &firmware_entry,
 104                 &firmware_start, &firmware_end, 0, EM_RISCV, 1, 0) > 0) {
 105        return firmware_entry;
 106    }
 107
 108    if (load_image_targphys_as(firmware_filename, firmware_load_addr,
 109                               ram_size, NULL) > 0) {
 110        return firmware_load_addr;
 111    }
 112
 113    error_report("could not load firmware '%s'", firmware_filename);
 114    exit(1);
 115}
 116
 117target_ulong riscv_load_kernel(const char *kernel_filename, symbol_fn_t sym_cb)
 118{
 119    uint64_t kernel_entry, kernel_high;
 120
 121    if (load_elf_ram_sym(kernel_filename, NULL, NULL, NULL,
 122                         &kernel_entry, NULL, &kernel_high, 0,
 123                         EM_RISCV, 1, 0, NULL, true, sym_cb) > 0) {
 124        return kernel_entry;
 125    }
 126
 127    if (load_uimage_as(kernel_filename, &kernel_entry, NULL, NULL,
 128                       NULL, NULL, NULL) > 0) {
 129        return kernel_entry;
 130    }
 131
 132    if (load_image_targphys_as(kernel_filename, KERNEL_BOOT_ADDRESS,
 133                               ram_size, NULL) > 0) {
 134        return KERNEL_BOOT_ADDRESS;
 135    }
 136
 137    error_report("could not load kernel '%s'", kernel_filename);
 138    exit(1);
 139}
 140
 141hwaddr riscv_load_initrd(const char *filename, uint64_t mem_size,
 142                         uint64_t kernel_entry, hwaddr *start)
 143{
 144    int size;
 145
 146    /*
 147     * We want to put the initrd far enough into RAM that when the
 148     * kernel is uncompressed it will not clobber the initrd. However
 149     * on boards without much RAM we must ensure that we still leave
 150     * enough room for a decent sized initrd, and on boards with large
 151     * amounts of RAM we must avoid the initrd being so far up in RAM
 152     * that it is outside lowmem and inaccessible to the kernel.
 153     * So for boards with less  than 256MB of RAM we put the initrd
 154     * halfway into RAM, and for boards with 256MB of RAM or more we put
 155     * the initrd at 128MB.
 156     */
 157    *start = kernel_entry + MIN(mem_size / 2, 128 * MiB);
 158
 159    size = load_ramdisk(filename, *start, mem_size - *start);
 160    if (size == -1) {
 161        size = load_image_targphys(filename, *start, mem_size - *start);
 162        if (size == -1) {
 163            error_report("could not load ramdisk '%s'", filename);
 164            exit(1);
 165        }
 166    }
 167
 168    return *start + size;
 169}
 170