From: Alistair Francis <alistair23@gmail.com>
To: "Philippe Mathieu-Daudé" <philmd@linaro.org>
Cc: Alistair Francis <alistair.francis@wdc.com>,
"Edgar E. Iglesias" <edgar.iglesias@amd.com>,
qemu-devel@nongnu.org, peter.maydell@linaro.org,
pbonzini@redhat.com, Thomas Huth <thuth@redhat.com>
Subject: Re: [PATCH v13 1/2] generic-loader: Add a generic loader
Date: Thu, 28 Nov 2024 10:41:20 +1000 [thread overview]
Message-ID: <CAKmqyKNJxL6fRWYA1fka3dERt+6FWM=YveoQVBpXe9GV16jmgQ@mail.gmail.com> (raw)
In-Reply-To: <c32ee498-4bc4-408a-bc28-ab21563bb2e3@linaro.org>
On Thu, Nov 28, 2024 at 2:09 AM Philippe Mathieu-Daudé
<philmd@linaro.org> wrote:
>
> Hi,
>
> [very old patch merged as commit e481a1f63c93]
>
> On 30/9/16 02:25, Alistair Francis wrote:
> > Add a generic loader to QEMU which can be used to load images or set
> > memory values.
> >
> > Internally inside QEMU this is a device. It is a strange device that
> > provides no hardware interface but allows QEMU to monkey patch memory
> > specified when it is created. To be able to do this it has a reset
> > callback that does the memory operations.
> >
> > This device allows the user to monkey patch memory. To be able to do
> > this it needs a backend to manage the datas, the same as other
> > memory-related devices. In this case as the backend is so trivial we
> > have merged it with the frontend instead of creating and maintaining a
> > seperate backend.
> >
> > Signed-off-by: Alistair Francis <alistair.francis@xilinx.com>
> > Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
> > Acked-by: Markus Armbruster <armbru@redhat.com>
> > ---
>
> > diff --git a/hw/core/generic-loader.c b/hw/core/generic-loader.c
> > new file mode 100644
> > index 0000000..79ab6df
> > --- /dev/null
> > +++ b/hw/core/generic-loader.c
> > @@ -0,0 +1,211 @@
> > +/*
> > + * Generic Loader
> > + *
> > + * Copyright (C) 2014 Li Guang
> > + * Copyright (C) 2016 Xilinx Inc.
> > + * Written by Li Guang <lig.fnst@cn.fujitsu.com>
> > + *
> > + * This program is free software; you can redistribute it and/or modify it
> > + * under the terms of the GNU General Public License as published by the
> > + * Free Software Foundation; either version 2 of the License, or
> > + * (at your option) any later version.
> > + *
> > + * This program is distributed in the hope that 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
> > + * for more details.
> > + *
> > + */
> > +
> > +/*
> > + * Internally inside QEMU this is a device. It is a strange device that
> > + * provides no hardware interface but allows QEMU to monkey patch memory
> > + * specified when it is created. To be able to do this it has a reset
> > + * callback that does the memory operations.
> > +
> > + * This device allows the user to monkey patch memory. To be able to do
> > + * this it needs a backend to manage the datas, the same as other
> > + * memory-related devices. In this case as the backend is so trivial we
> > + * have merged it with the frontend instead of creating and maintaining a
> > + * seperate backend.
> > + */
> > +
> > +#include "qemu/osdep.h"
> > +#include "qom/cpu.h"
> > +#include "hw/sysbus.h"
> > +#include "sysemu/dma.h"
> > +#include "hw/loader.h"
> > +#include "qapi/error.h"
> > +#include "hw/core/generic-loader.h"
> > +
> > +#define CPU_NONE 0xFFFFFFFF
> > +
> > +static void generic_loader_reset(void *opaque)
> > +{
> > + GenericLoaderState *s = GENERIC_LOADER(opaque);
> > +
> > + if (s->set_pc) {
> > + CPUClass *cc = CPU_GET_CLASS(s->cpu);
> > + cpu_reset(s->cpu);
> > + if (cc) {
> > + cc->set_pc(s->cpu, s->addr);
> > + }
> > + }
> > +
> > + if (s->data_len) {
> > + assert(s->data_len < sizeof(s->data));
> > + dma_memory_write(s->cpu->as, s->addr, &s->data, s->data_len);
> > + }
> > +}
> > +
> > +static void generic_loader_realize(DeviceState *dev, Error **errp)
> > +{
> > + GenericLoaderState *s = GENERIC_LOADER(dev);
> > + hwaddr entry;
> > + int big_endian;
> > + int size = 0;
> > +
> > + s->set_pc = false;
> > +
> > + /* Perform some error checking on the user's options */
> > + if (s->data || s->data_len || s->data_be) {
> > + /* User is loading memory values */
> > + if (s->file) {
> > + error_setg(errp, "Specifying a file is not supported when loading "
> > + "memory values");
> > + return;
> > + } else if (s->force_raw) {
> > + error_setg(errp, "Specifying force-raw is not supported when "
> > + "loading memory values");
> > + return;
> > + } else if (!s->data_len) {
> > + /* We cant' check for !data here as a value of 0 is still valid. */
> > + error_setg(errp, "Both data and data-len must be specified");
> > + return;
> > + } else if (s->data_len > 8) {
> > + error_setg(errp, "data-len cannot be greater then 8 bytes");
>
> If s->data_len < 8 (like 4 or 2) ...
>
> > + return;
> > + }
> > + } else if (s->file || s->force_raw) {
> > + /* User is loading an image */
> > + if (s->data || s->data_len || s->data_be) {
> > + error_setg(errp, "data can not be specified when loading an "
> > + "image");
> > + return;
> > + }
> > + s->set_pc = true;
> > + } else if (s->addr) {
> > + /* User is setting the PC */
> > + if (s->data || s->data_len || s->data_be) {
> > + error_setg(errp, "data can not be specified when setting a "
> > + "program counter");
> > + return;
> > + } else if (!s->cpu_num) {
> > + error_setg(errp, "cpu_num must be specified when setting a "
> > + "program counter");
> > + return;
> > + }
> > + s->set_pc = true;
> > + } else {
> > + /* Did the user specify anything? */
> > + error_setg(errp, "please include valid arguments");
> > + return;
> > + }
> > +
> > + qemu_register_reset(generic_loader_reset, dev);
> > +
> > + if (s->cpu_num != CPU_NONE) {
> > + s->cpu = qemu_get_cpu(s->cpu_num);
> > + if (!s->cpu) {
> > + error_setg(errp, "Specified boot CPU#%d is nonexistent",
> > + s->cpu_num);
> > + return;
> > + }
> > + } else {
> > + s->cpu = first_cpu;
> > + }
> > +
> > +#ifdef TARGET_WORDS_BIGENDIAN
> > + big_endian = 1;
> > +#else
> > + big_endian = 0;
> > +#endif
> > +
> > + if (s->file) {
> > + if (!s->force_raw) {
> > + size = load_elf_as(s->file, NULL, NULL, &entry, NULL, NULL,
> > + big_endian, 0, 0, 0, s->cpu->as);
> > +
> > + if (size < 0) {
> > + size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
> > + s->cpu->as);
> > + }
> > + }
> > +
> > + if (size < 0 || s->force_raw) {
> > + /* Default to the maximum size being the machine's ram size */
> > + size = load_image_targphys_as(s->file, s->addr, ram_size,
> > + s->cpu->as);
> > + } else {
> > + s->addr = entry;
> > + }
> > +
> > + if (size < 0) {
> > + error_setg(errp, "Cannot load specified image %s", s->file);
> > + return;
> > + }
> > + }
> > +
> > + /* Convert the data endiannes */
> > + if (s->data_be) {
> > + s->data = cpu_to_be64(s->data);
> > + } else {
> > + s->data = cpu_to_le64(s->data);
>
> ... and if we swap, we ignore the data-len and swap 64-bit
> regardless, returning invalid data.
>
> I.e. data=0x1122, data-len=2, once swapped we get data=0x0000.
>
> Is that expected?
I don't think that's expected
Alistair
next prev parent reply other threads:[~2024-11-28 0:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-30 0:25 [Qemu-devel] [PATCH v13 0/2] Add a generic loader Alistair Francis
2016-09-30 0:25 ` [Qemu-devel] [PATCH v13 1/2] generic-loader: " Alistair Francis
2024-11-27 16:08 ` Philippe Mathieu-Daudé
2024-11-28 0:41 ` Alistair Francis [this message]
2016-09-30 0:25 ` [Qemu-devel] [PATCH v13 2/2] docs: Add a generic loader explanation document Alistair Francis
2016-09-30 1:44 ` [Qemu-devel] [PATCH v13 0/2] Add a generic loader Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAKmqyKNJxL6fRWYA1fka3dERt+6FWM=YveoQVBpXe9GV16jmgQ@mail.gmail.com' \
--to=alistair23@gmail.com \
--cc=alistair.francis@wdc.com \
--cc=edgar.iglesias@amd.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).