From nobody Sun Feb 8 02:50:45 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1598532003; cv=none; d=zohomail.com; s=zohoarc; b=Yss2R/sXXbz7k9vxR8OeBnXGIug2mr8kz2hEXC26MenybMFO9qi13hkqIh93x05D1KnjXFUcd+HNkWRuSpAxddbHfM3hfu2dSv3gY785INZCMzgJpKSti9LYnkwxAE5w8P/9v06rEePqBeglAxFRytOKUdz495L86P9JDvPDpog= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1598532003; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HDsHFtC7DfdxNrX6DNTLYtSHI5I+A9ONNjRkCoz7VEI=; b=P4Ss5g7MxLeDuIx/aOF9XgZ4lSnFnaEahyR/dlRLPkoczcc3M+lPimIbjLZd6y8ZC25bkaxGHX9VuS8vPk6jW5LpUq/WSNfto5ZTvE15MvYEZ3p9O7YO/tytoBXPDiaL9+FGAHOQH5HuT1bhAE1oHcCJOs5XSGzPlBv0qsPxyjU= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15985320030721009.9528799557985; Thu, 27 Aug 2020 05:40:03 -0700 (PDT) Received: from localhost ([::1]:38742 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBHCD-0001AB-NJ for importer@patchew.org; Thu, 27 Aug 2020 08:40:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58900) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kBHBQ-0007xC-6F for qemu-devel@nongnu.org; Thu, 27 Aug 2020 08:39:12 -0400 Received: from mail02.asahi-net.or.jp ([202.224.55.14]:56302) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kBHBN-0005u8-TJ for qemu-devel@nongnu.org; Thu, 27 Aug 2020 08:39:11 -0400 Received: from sakura.ysato.name (ik1-413-38519.vs.sakura.ne.jp [153.127.30.23]) (Authenticated sender: PQ4Y-STU) by mail02.asahi-net.or.jp (Postfix) with ESMTPA id CC51026329; Thu, 27 Aug 2020 21:39:05 +0900 (JST) Received: from yo-satoh-debian.localdomain (ZM005235.ppp.dion.ne.jp [222.8.5.235]) by sakura.ysato.name (Postfix) with ESMTPSA id 7C96C1C07B2; Thu, 27 Aug 2020 21:39:05 +0900 (JST) From: Yoshinori Sato To: qemu-devel@nongnu.org Subject: [PATCH 03/20] hw/rx: Firmware and kernel loader. Date: Thu, 27 Aug 2020 21:38:42 +0900 Message-Id: <20200827123859.81793-4-ysato@users.sourceforge.jp> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200827123859.81793-1-ysato@users.sourceforge.jp> References: <20200827123859.81793-1-ysato@users.sourceforge.jp> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: softfail client-ip=202.224.55.14; envelope-from=ysato@users.sourceforge.jp; helo=mail02.asahi-net.or.jp X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/27 08:39:06 X-ACL-Warn: Detected OS = ??? X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yoshinori Sato Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Suppoerted format. ELF, HEX, SREC and Raw firmware. fit and Raw kernel image. Signed-off-by: Yoshinori Sato --- include/hw/rx/loader.h | 35 ++++++++ hw/rx/loader.c | 182 +++++++++++++++++++++++++++++++++++++++++ hw/rx/Kconfig | 1 + hw/rx/meson.build | 1 + 4 files changed, 219 insertions(+) create mode 100644 include/hw/rx/loader.h create mode 100644 hw/rx/loader.c diff --git a/include/hw/rx/loader.h b/include/hw/rx/loader.h new file mode 100644 index 0000000000..71f3bd2bb3 --- /dev/null +++ b/include/hw/rx/loader.h @@ -0,0 +1,35 @@ +/* + * RX QEMU frimware loader + * + * Copyright (c) 2020 Yoshinori Sato + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#include "qapi/error.h" +#include "qemu/error-report.h" + +typedef struct { + hwaddr ram_start; + size_t ram_size; + hwaddr entry; + hwaddr kernel_entry; + hwaddr dtb_address; + const char *filename; + const char *dtbname; + const char *cmdline; +} rx_kernel_info_t; + +bool load_bios(const char *filename, int rom_size, Error **errp); + +bool load_kernel(rx_kernel_info_t *info); diff --git a/hw/rx/loader.c b/hw/rx/loader.c new file mode 100644 index 0000000000..c262f3ef86 --- /dev/null +++ b/hw/rx/loader.c @@ -0,0 +1,182 @@ +/* + * RX QEMU frimware loader + * + * Copyright (c) 2020 Yoshinori Sato + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "elf.h" +#include "hw/loader.h" +#include "hw/loader-fit.h" +#include "hw/rx/loader.h" +#include "sysemu/device_tree.h" +#include "exec/cpu-defs.h" +#include + +#define RX_RESET_VEC 0xfffffffc +#define ADDRESS_TOP ((1LL << TARGET_PHYS_ADDR_SPACE_BITS) - 1) + +bool load_bios(const char *filename, int rom_size, Error **errp) +{ + int size; + uint64_t entry64 =3D UINT64_MAX; + uint32_t entry; + + size =3D load_elf(filename, NULL, NULL, NULL, &entry64, + NULL, NULL, NULL, 0, EM_RX, 0, 0); + if (size > 0) { + goto load_ok; + } + size =3D load_targphys_hex_as(filename, &entry64, NULL); + if (size > 0) { + goto load_ok; + } + size =3D load_targphys_srec_as(filename, &entry64, NULL); + if (size > 0) { + goto load_ok; + } + size =3D get_image_size(filename); + if (size < 0) { + error_setg(errp, "\"%s\" is open failed.", filename); + return false; + } + if (size > rom_size) { + error_setg(errp, "\"%s\" is too large for ROM area.", filename); + return false; + } + + /* + * The RX CPU reset vector is at the top of the ROM, + * so the raw binary is loaded there. + */ + rom_add_file_fixed(filename, -size, 0); + load_ok: + if (rom_ptr(RX_RESET_VEC, 4) =3D=3D NULL) { + if (entry64 <=3D ADDRESS_TOP) { + entry =3D cpu_to_le32(entry64); + rom_add_blob_fixed("entry", &entry, 4, RX_RESET_VEC); + } else { + error_setg(errp, "Reset vector is not set"); + return false; + } + } + return true; +} + +static hwaddr rx_addr_to_phys(void *opaque, uint64_t addr) +{ + /* No address translation */ + return addr; +} + +static bool setup_commandline(void *dtb, rx_kernel_info_t *info) +{ + if (info->cmdline && + qemu_fdt_setprop_string(dtb, "/chosen", "bootargs", + info->cmdline) < 0) { + return false; + } + return true; +} + + +static const void *rx_fdt_filter(void *opaque, const void *fdt_orig, + const void *match_data, hwaddr *load_addr) +{ + rx_kernel_info_t *info =3D opaque; + void *fdt; + size_t fdt_sz; + int err; + + fdt_sz =3D fdt_totalsize(fdt_orig) + 0x1000; + fdt =3D g_malloc0(fdt_sz); + + err =3D fdt_open_into(fdt_orig, fdt, fdt_sz); + if (err) { + error_report("couldn't open dtb"); + return NULL; + } + + if (!setup_commandline(fdt, info)) { + error_report("couldn't set /chosen/bootargs"); + return NULL; + } + fdt_sz =3D fdt_totalsize(fdt); + fdt =3D g_realloc(fdt, fdt_totalsize(fdt)); + info->dtb_address =3D info->ram_start + info->ram_size - fdt_sz; + *load_addr =3D info->dtb_address; + + return fdt; +} + +static const void *rx_kernel_filter(void *opaque, const void *kernel, + hwaddr *load_addr, hwaddr *entry_a= ddr) +{ + rx_kernel_info_t *info =3D opaque; + + info->kernel_entry =3D *entry_addr; + + return kernel; +} + +static const struct fit_loader rx_fit_loader =3D { + .addr_to_phys =3D rx_addr_to_phys, + .fdt_filter =3D rx_fdt_filter, + .kernel_filter =3D rx_kernel_filter, +}; + +bool load_kernel(rx_kernel_info_t *info) +{ + ram_addr_t kernel_offset; + size_t kernel_size; + + if (load_fit(&rx_fit_loader, info->filename, info) =3D=3D 0) { + return true; + } + + /* + * The kernel image is loaded into + * the latter half of the SDRAM space. + */ + kernel_offset =3D info->ram_size / 2; + + info->entry =3D info->ram_start + kernel_offset; + kernel_size =3D load_image_targphys(info->filename, + info->entry, info->ram_size / 2); + if (kernel_size =3D=3D -1) { + return false; + } + if (info->dtbname) { + ram_addr_t dtb_offset; + int dtb_size; + void *dtb; + + dtb =3D load_device_tree(info->dtbname, &dtb_size); + if (dtb =3D=3D NULL) { + error_report("Couldn't open dtb file %s", info->dtbname); + return false; + } + if (!setup_commandline(dtb, info)) { + error_report("Couldn't set /chosen/bootargs"); + return false; + } + /* DTB is located at the end of SDRAM space. */ + dtb_size =3D fdt_totalsize(dtb); + dtb_offset =3D info->ram_size - dtb_size; + info->dtb_address =3D info->ram_start + dtb_offset; + rom_add_blob_fixed("dtb", dtb, dtb_size, info->dtb_address); + } + return true; +} diff --git a/hw/rx/Kconfig b/hw/rx/Kconfig index 2b297c5a6a..a63e4a5520 100644 --- a/hw/rx/Kconfig +++ b/hw/rx/Kconfig @@ -8,3 +8,4 @@ config RX62N_MCU config RX_GDBSIM bool select RX62N_MCU + select FITLOADER diff --git a/hw/rx/meson.build b/hw/rx/meson.build index d223512a78..e73850f303 100644 --- a/hw/rx/meson.build +++ b/hw/rx/meson.build @@ -1,4 +1,5 @@ rx_ss =3D ss.source_set() +rx_ss.add(files('loader.c')) rx_ss.add(when: 'CONFIG_RX_GDBSIM', if_true: files('rx-gdbsim.c')) rx_ss.add(when: 'CONFIG_RX62N_MCU', if_true: files('rx62n.c')) =20 --=20 2.20.1