From nobody Thu Nov 13 12:16:16 2025 Delivered-To: importer@patchew.org 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; Authentication-Results: mx.zohomail.com; dkim=fail; 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=1579827364; cv=none; d=zohomail.com; s=zohoarc; b=N3L6vKl/blw5mKaJ0zVjnpaKjMEfPFb+tQxxtIAfCk5UvVh+yuuEnoPtw2fQ4cMt4bwfUh7JrZMRIHwFOran6Q6u9oJS/ID49ACd06Idz8SJTYsGoQvrKhKNFujRKwn6PCUYpt4khEvBGd85mCvUqquDLV4KTiJSYqOH7zrBHsM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579827364; h=Content-Type: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=Wn/b5e0hYxjWbkJVXyQPyX2UxO9y0IeO0Qmgm+3sqXU=; b=g8XK+7hp1X/FcnJizumbS7JZ/amaXjpo4STv7CWk74LKWel92QS8ZljdSzq8JmHYSWzHsCAGgwFVFDKmTF10cfvXgXBAqy7Zh7yLi4ZZ7SlfkWe3SJaVE6aB88OV4HROB8S7rIiH4ImExl5L0xK2HvQmIGoCYwMRCtGX8UYRa4c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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 1579827364761257.6529491264881; Thu, 23 Jan 2020 16:56:04 -0800 (PST) Received: from localhost ([::1]:36046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iunGV-0002N2-Lk for importer@patchew.org; Thu, 23 Jan 2020 19:56:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:44560) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iunCy-0002dl-84 for qemu-devel@nongnu.org; Thu, 23 Jan 2020 19:52:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iunCw-0005bS-8P for qemu-devel@nongnu.org; Thu, 23 Jan 2020 19:52:24 -0500 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]:36158) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iunCw-0005ZT-1b; Thu, 23 Jan 2020 19:52:22 -0500 Received: by mail-wm1-x342.google.com with SMTP id p17so146748wma.1; Thu, 23 Jan 2020 16:52:19 -0800 (PST) Received: from x1w.redhat.com (113.red-83-57-172.dynamicip.rima-tde.net. [83.57.172.113]) by smtp.gmail.com with ESMTPSA id s139sm4598271wme.35.2020.01.23.16.52.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 16:52:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Wn/b5e0hYxjWbkJVXyQPyX2UxO9y0IeO0Qmgm+3sqXU=; b=DHK64CCEwniEBPz36VcJhwTa1sb6g9uNqsyRPJI1yA2fGEfny1zU3i9i0DrTrEGn9d TSLUh7AzaWJ1984CZsHnOgE5RYyTeStk5zHg14w1NW4+qZWdiVqAqWUZwwFj8Gv70wQb PmzUze64oBRWLxdYMaLRTUvQ5Ti70gGD9xGyxgFIc34kUmL3vLmwIB0OQJ2w8uSmFpGE yIZoehrAIYopuQOGF3G5/cDL/kSEC0trDjIvK7kVAXjfUwAjuL9Z1o6I18aWyy2dc+kX qJsvk5OWbDEq1GHiUQIEvGlBp7lcAlu3aUbh9CmO5dbIZlz8vrJrvizkQTOvMuFj+HSu DG0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Wn/b5e0hYxjWbkJVXyQPyX2UxO9y0IeO0Qmgm+3sqXU=; b=t1ULE9Sdqp7p1T+upAepkglsXD2QoPeF3azdBSD8KpPzog4BhOeNxEnCrCMDC5/KsD RsGuxN4StcBUR6eKeZRxne8+scgZ2RvyB82MV2RSHIzSMFusBs6+VSBboOxuEO45bCBe /KRLPLrnN50K4RJ8cBhXu7LrnCnU4/K0s2bY437Sp475ysE8r4wAWwJNcHgCwexxzAMd WgMzFhFy6Xzw6L9A15i89bCMoIYIwc4G20+Z84SahJ1VGwGY7K15Z2RkF8wmAu/mMWZQ fdDyOrHSym0VxG1rt09/rXHVQ45c00hkI1AX0S71gEKkwpvg5VB7bRcueSX3sExarudZ fPWg== X-Gm-Message-State: APjAAAXp2fq+26iMIE2b+kgH0FqHD3uvv7evN3pDOmhh7Evoa3TTdk34 5LzD8+wjKH7kB8leqsnaoIcxbLao X-Google-Smtp-Source: APXvYqx4MdJ5ovjlpVfAtGzGfuaL+KumnswpemnAk2jS7XrGPHVQpAPD6HXtTo228S56ZPU7Al5Q4A== X-Received: by 2002:a05:600c:1009:: with SMTP id c9mr554909wmc.162.1579827138366; Thu, 23 Jan 2020 16:52:18 -0800 (PST) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org, mrolnik@gmail.com, richard.henderson@linaro.org, me@xcancerberox.com.ar Subject: [PATCH rc2 19/25] hw/avr: Add helper to load raw/ELF firmware binaries Date: Fri, 24 Jan 2020 01:51:25 +0100 Message-Id: <20200124005131.16276-20-f4bug@amsat.org> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200124005131.16276-1-f4bug@amsat.org> References: <20200124005131.16276-1-f4bug@amsat.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::342 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: Laurent Vivier , Fam Zheng , S.E.Harris@kent.ac.uk, qemu-riscv@nongnu.org, Eduardo Habkost , Sagar Karandikar , dovgaluk@ispras.ru, Bastian Koppelmann , thuth@redhat.com, Markus Armbruster , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Alistair Francis , imammedo@redhat.com, Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Palmer Dabbelt , aleksandar.m.mail@gmail.com, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Add avr_load_firmware() function to load firmware in ELF or raw binary format. Suggested-by: Aleksandar Markovic Signed-off-by: Philippe Mathieu-Daud=C3=A9 --- hw/avr/boot.h | 33 ++++++++++++++++++++ include/elf.h | 2 ++ hw/avr/boot.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ hw/avr/Makefile.objs | 1 + 4 files changed, 110 insertions(+) create mode 100644 hw/avr/boot.h create mode 100644 hw/avr/boot.c create mode 100644 hw/avr/Makefile.objs diff --git a/hw/avr/boot.h b/hw/avr/boot.h new file mode 100644 index 0000000000..62bc10c922 --- /dev/null +++ b/hw/avr/boot.h @@ -0,0 +1,33 @@ +/* + * AVR loader helpers + * + * Copyright (c) 2019 Philippe Mathieu-Daud=C3=A9 + * + * This work is licensed under the terms of the GNU GPLv2 or later. + * See the COPYING file in the top-level directory. + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef HW_AVR_BOOT_H +#define HW_AVR_BOOT_H + +#include "hw/boards.h" +#include "cpu.h" + +/** + * avr_load_firmware: load an image into a memory region + * + * @cpu: Handle a AVR CPU object + * @ms: A MachineState + * @mr: Memory Region to load into + * @firmware: Path to the firmware file (raw binary or ELF format) + * + * Load a firmware supplied by the machine or by the user with the + * '-bios' command line option, and put it in target memory. + * + * Returns: true on success, false on error. + */ +bool avr_load_firmware(AVRCPU *cpu, MachineState *ms, + MemoryRegion *mr, const char *firmware); + +#endif diff --git a/include/elf.h b/include/elf.h index 3501e0c8d0..53cdfa23b7 100644 --- a/include/elf.h +++ b/include/elf.h @@ -202,6 +202,8 @@ typedef struct mips_elf_abiflags_v0 { #define EM_MOXIE 223 /* Moxie processor family */ #define EM_MOXIE_OLD 0xFEED =20 +#define EM_AVR 83 /* AVR 8-bit microcontroller */ + /* This is the info that is needed to parse the dynamic section of the fil= e */ #define DT_NULL 0 #define DT_NEEDED 1 diff --git a/hw/avr/boot.c b/hw/avr/boot.c new file mode 100644 index 0000000000..2d9bd0fe59 --- /dev/null +++ b/hw/avr/boot.c @@ -0,0 +1,74 @@ +/* + * AVR loader helpers + * + * Copyright (c) 2019 Philippe Mathieu-Daud=C3=A9 + * + * This work is licensed under the terms of the GNU GPLv2 or later. + * See the COPYING file in the top-level directory. + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "hw/loader.h" +#include "elf.h" +#include "boot.h" +#include "qemu/error-report.h" + +bool avr_load_firmware(AVRCPU *cpu, MachineState *ms, + MemoryRegion *mr, const char *firmware) +{ + const char *filename; + int bytes_loaded; + uint64_t entry; + int e_flags; + + filename =3D qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware); + if (filename =3D=3D NULL) { + error_report("Unable to find %s", firmware); + return false; + } + + bytes_loaded =3D load_elf_ram_sym(filename, + NULL, NULL, NULL, + &entry, NULL, NULL, + &e_flags, 0, EM_AVR, 0, 0, + NULL, true, NULL); + if (bytes_loaded >=3D 0) { + /* If ELF file is provided, determine CPU type reading ELF e_flags= . */ + const char *elf_cpu =3D avr_flags_to_cpu_type(e_flags, NULL); + const char *mcu_cpu_type =3D object_get_typename(OBJECT(cpu)); + int cpu_len =3D strlen(mcu_cpu_type) - strlen(AVR_CPU_TYPE_SUFFIX); + + if (entry) { + error_report("BIOS entry_point must be 0x0000 " + "(ELF image '%s' has entry_point 0x%04" PRIx64 ")= ", + firmware, entry); + return false; + } + if (!elf_cpu) { + warn_report("Could not determine CPU type for ELF image '%s', " + "assuming '%.*s' CPU", + firmware, cpu_len, mcu_cpu_type); + return true; + } + if (strcmp(elf_cpu, mcu_cpu_type)) { + error_report("Current machine: %s with '%.*s' CPU", + MACHINE_GET_CLASS(ms)->desc, cpu_len, mcu_cpu_typ= e); + error_report("ELF image '%s' is for '%.*s' CPU", + firmware, + (int)(strlen(elf_cpu) - strlen(AVR_CPU_TYPE_SUFFI= X)), + elf_cpu); + return false; + } + } else { + bytes_loaded =3D load_image_targphys(filename, OFFSET_CODE, + memory_region_size(mr)); + } + if (bytes_loaded < 0) { + error_report("Unable to load firmware image %s as ELF or raw binar= y", + firmware); + return false; + } + return true; +} diff --git a/hw/avr/Makefile.objs b/hw/avr/Makefile.objs new file mode 100644 index 0000000000..123f174f0e --- /dev/null +++ b/hw/avr/Makefile.objs @@ -0,0 +1 @@ +obj-y +=3D boot.o --=20 2.21.1