Skip to contentSkip to conversation

Add a generic loader

PATCH Superseded Alistair Francis 2 participants 6 messages May 12, 2016

v5 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add a generic loader

This work is based on the original work by Li Guang with extra features added by Peter C and myself.

The idea of this loader is to allow the user to load multiple images or values into QEMU at startup.

Memory values can be loaded like this: -device loader,addr=0xfd1a0104,data=0x8000000e,data-len=4

Images can be loaded like this: -device loader,file=./images/u-boot.elf,cpu=0

This can be useful and we use it a lot in Xilinx to load multiple images into a machine at creation (ATF, Kernel and DTB for example).

It can also be used to set registers.

This patch series also make the load_elf() function more generic by not requiring an architecture.

V5:
- Rebase
V4:
- Re-write documentation
- Allow the loader to work with every architecture
- Move the file to hw/core
- Increase the maximum number of CPUs
- Make the CPU operations conditional
- Convert the cpu option to cpu-num
- Require the user to specify endianess
V2:
- Add an entry to the maintainers file
- Add some documentation
- Perform bounds checking on the data_len
- Register and unregister the reset in the realise/unrealise
Changes since RFC:
- Add support for BE

Alistair Francis (3):
loader: Allow ELF loader to auto-detect the ELF arch
generic-loader: Add a generic loader
docs: Add a generic loader explanation document

 MAINTAINERS                      |   6 ++
 docs/generic-loader.txt          |  56 +++++++++++++++
 hw/core/Makefile.objs            |   2 +
 hw/core/generic-loader.c         | 145 +++++++++++++++++++++++++++++++++++++++
 hw/core/loader.c                 |  10 +++
 include/hw/core/generic-loader.h |  45 ++++++++++++
 6 files changed, 264 insertions(+)
 create mode 100644 docs/generic-loader.txt
 create mode 100644 hw/core/generic-loader.c
 create mode 100644 include/hw/core/generic-loader.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH v5 1/3 loader: Allow ELF loader to auto-detect the ELF arch

If the caller didn't specify an architecture for the ELF machine the load_elf() function will auto detect it based on the ELF file.

---

 hw/core/loader.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/core/loader.c +10 -0
index c049957..6b8be6b 100644
@@ -419,6 +419,7 @@ int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
 {
     int fd, data_order, target_data_order, must_swab, ret = ELF_LOAD_FAILED;
     uint8_t e_ident[EI_NIDENT];
+    uint16_t e_machine;
 
     fd = open(filename, O_RDONLY | O_BINARY);
     if (fd < 0) {
@@ -451,6 +452,15 @@ int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
         goto fail;
     }
 
+    if (elf_machine < 1) {
+        /* The caller didn't specify and ARCH, we can figure it out */
+        lseek(fd, 0x12, SEEK_SET);
+        if (read(fd, &e_machine, sizeof(e_machine)) != sizeof(e_machine)) {
+            goto fail;
+        }
+        elf_machine = e_machine;
+    }
+
     lseek(fd, 0, SEEK_SET);
     if (e_ident[EI_CLASS] == ELFCLASS64) {
         ret = load_elf64(filename, fd, translate_fn, translate_opaque, must_swab,
Signature · 1 line
2.7.4
#2 lore raw

PATCH v5 2/3 generic-loader: Add a generic loader

In reply to Alistair Francis

Add a generic loader to QEMU which can be used to load images or set memory values.

---
V5:
- Rebase
V4:
- Allow the loader to work with every architecture
- Move the file to hw/core
- Increase the maximum number of CPUs
- Make the CPU operations conditional
- Convert the cpu option to cpu-num
- Require the user to specify endianess
V3:
- Pass the ram_size to load_image_targphys()
V2:
- Add maintainers entry
- Perform bounds checking
- Register and unregister the reset in the realise/unrealise
Changes since RFC:
- Add BE support

 MAINTAINERS                      |   6 ++
 hw/core/Makefile.objs            |   2 +
 hw/core/generic-loader.c         | 145 +++++++++++++++++++++++++++++++++++++++
 include/hw/core/generic-loader.h |  45 ++++++++++++
 4 files changed, 198 insertions(+)
 create mode 100644 hw/core/generic-loader.c
 create mode 100644 include/hw/core/generic-loader.h
MAINTAINERS +6 -0
index 81e7fac..b4a77d8 100644
@@ -972,6 +972,12 @@ F: hw/acpi/nvdimm.c
 F: hw/mem/nvdimm.c
 F: include/hw/mem/nvdimm.h
 
+Generic Loader
+M: Alistair Francis <[email protected]>
+S: Maintained
+F: hw/core/generic-loader.c
+F: include/hw/core/generic-loader.h
+
 Subsystems
 ----------
 Audio
hw/core/Makefile.objs +2 -0
index abb3560..b5d5197 100644
@@ -15,3 +15,5 @@ common-obj-$(CONFIG_SOFTMMU) += null-machine.o
 common-obj-$(CONFIG_SOFTMMU) += loader.o
 common-obj-$(CONFIG_SOFTMMU) += qdev-properties-system.o
 common-obj-$(CONFIG_PLATFORM_BUS) += platform-bus.o
+
+obj-$(CONFIG_SOFTMMU) += generic-loader.o
hw/core/generic-loader.c new file +145 -0

145 lines not on this page · raw message

include/hw/core/generic-loader.h new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

PATCH v5 3/3 docs: Add a generic loader explanation document

In reply to Alistair Francis

---
V4:
- Re-write to be more comprehensive

 docs/generic-loader.txt | 56 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 docs/generic-loader.txt
docs/generic-loader.txt new file +56 -0
new file mode 100644
index 0000000..b748f2a
@@ -0,0 +1,56 @@
+Copyright (c) 2016 Xilinx Inc.
+
+This work is licensed under the terms of the GNU GPL, version 2 or later.  See
+the COPYING file in the top-level directory.
+
+
+The 'loader' device allows the user to load multiple images or values into
+QEMU at startup.
+
+Loading Memory Values
+---------------------------
+The loader device allows memory values to be set from the command line. This
+can be done by following the syntax below:
+
+    -device loader,addr=<addr>,data=<data>,data-len=<len>
+    -device loader,addr=<addr>,cpu-num=<cpu-num>
+
+NOTE: The loader device supports other options (see the next section) but they
+      do not apply to setting memory values and will be ignored.
+      It is also possible to mix the commands above, e.g. include the cpu-num
+      argument with the data argument.
+
+    <addr>      - The address to store the data or the value to set the CPUs PC
+    <data>      - The value to be written to the addr. The maximum size of the
+                  data is 8 bytes.
+    <data-len>  - The length of the data in bytes. This argument must be included
+                  if the data argument is.
+    <data-be>   - Set to true if the data to be stored on the guest should be
+                  written as big endian data. The default is to write little
+                  endian data.
+    <cpu-num>   - This will cause the CPU to be reset and the PC to be set to
+                  the value of addr.
+
+An example of loading value 0x8000000e to address 0xfd1a0104 is:
+    -device loader,addr=0xfd1a0104,data=0x8000000e,data-len=4
+
+Loading Files
+---------------------------
+The loader device also allows files to be loaded into memory. This can be done
+similarly to setting memory values. The syntax is shown below:
+
+    -device loader,file=<file>,addr=<addr>,cpu-num=<cpu-num>,force-raw=<raw>
+
+    <file>      - A file to be loaded into memory
+    <addr>      - The addr in memory that the file should be loaded. This is
+                  ignored if you are using an ELF (unless force-raw is true).
+                  This is requried if you aren't loading an ELF.
+    <cpu-num>   - This specifices the CPU that should be used. This is an
+                  optional argument and will cause the CPU's PC to be set to
+                  where the image is stored. This option should only be used
+                  for the boot image.
+    <force-raw> - Forces the file to be treated as a raw image. This can be
+                  used to specificy the load address of ELF files.
+
+An example of loading an ELF file which CPU0 will boot is shown below:
+    -device loader,file=./images/boot.elf,cpu-num=0
Signature · 1 line
2.7.4
#4 lore raw

Re: [Qemu-devel] [PATCH v5 3/3] docs: Add a generic loader explanation document

Quoted 11 lines · Alistair Francis

---
V4:
- Re-write to be more comprehensive

 docs/generic-loader.txt | 56 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 docs/generic-loader.txt

+Loading Memory Values
+---------------------------

Worth matching ---- line length to the line above?

docs/generic-loader.txt line 19

Quoted 3 lines
docs/generic-loader.txt new file +3 -0
+
+NOTE: The loader device supports other options (see the next section) but they
+      do not apply to setting memory values and will be ignored.

Ignoring invalid option combinations is not as friendly as outright rejecting them.

docs/generic-loader.txt line 38

Quoted 3 lines
docs/generic-loader.txt new file +3 -0
+
+Loading Files
+---------------------------

And again

docs/generic-loader.txt line 47

Quoted 9 lines
docs/generic-loader.txt new file +9 -0
+The loader device also allows files to be loaded into memory. This can be done
+similarly to setting memory values. The syntax is shown below:
+
+    -device loader,file=<file>,addr=<addr>,cpu-num=<cpu-num>,force-raw=<raw>
+
+    <file>      - A file to be loaded into memory
+    <addr>      - The addr in memory that the file should be loaded. This is
+                  ignored if you are using an ELF (unless force-raw is true).
+                  This is requried if you aren't loading an ELF.

s/requried/required/

Quoted 1 line

+ <cpu-num> - This specifices the CPU that should be used. This is an

s/specifices/specifies/

docs/generic-loader.txt line 53

Quoted 5 lines
docs/generic-loader.txt new file +5 -0
+                  optional argument and will cause the CPU's PC to be set to
+                  where the image is stored. This option should only be used
+                  for the boot image.
+    <force-raw> - Forces the file to be treated as a raw image. This can be
+                  used to specificy the load address of ELF files.

s/specificy/specify/

Quoted 4 lines
docs/generic-loader.txt new file +3 -0
+
+An example of loading an ELF file which CPU0 will boot is shown below:
+    -device loader,file=./images/boot.elf,cpu-num=0
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH v5 3/3] docs: Add a generic loader explanation document

Quoted 6 lines · Eric Blake
Quoted 5 lines · Alistair Francis

---
V4:
- Re-write to be more comprehensive

Quoted 8 lines
Quoted 6 lines
 docs/generic-loader.txt | 56 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 docs/generic-loader.txt

+Loading Memory Values
+---------------------------

Worth matching ---- line length to the line above?

Good point, fixed.

Quoted 7 lines

docs/generic-loader.txt line 19

Quoted 4 lines
docs/generic-loader.txt new file +3 -0
+
+NOTE: The loader device supports other options (see the next section) but they
+      do not apply to setting memory values and will be ignored.

Ignoring invalid option combinations is not as friendly as outright rejecting them.

Ok, I have added stricter checking to cover most invalid use cases.

Quoted 7 lines

docs/generic-loader.txt line 38

Quoted 5 lines
docs/generic-loader.txt new file +3 -0
+
+Loading Files
+---------------------------

And again

Fixed.

Quoted 12 lines

docs/generic-loader.txt line 47

Quoted 10 lines
docs/generic-loader.txt new file +9 -0
+The loader device also allows files to be loaded into memory. This can be done
+similarly to setting memory values. The syntax is shown below:
+
+    -device loader,file=<file>,addr=<addr>,cpu-num=<cpu-num>,force-raw=<raw>
+
+    <file>      - A file to be loaded into memory
+    <addr>      - The addr in memory that the file should be loaded. This is
+                  ignored if you are using an ELF (unless force-raw is true).
+                  This is requried if you aren't loading an ELF.

s/requried/required/

Fixed

Quoted 4 lines
Quoted 2 lines

+ <cpu-num> - This specifices the CPU that should be used. This is an

s/specifices/specifies/

Fixed

Quoted 8 lines

docs/generic-loader.txt line 53

Quoted 6 lines
docs/generic-loader.txt new file +5 -0
+                  optional argument and will cause the CPU's PC to be set to
+                  where the image is stored. This option should only be used
+                  for the boot image.
+    <force-raw> - Forces the file to be treated as a raw image. This can be
+                  used to specificy the load address of ELF files.

s/specificy/specify/

Fixed.

Thanks for reading the patch.

Alistair

Quoted 10 lines

docs/generic-loader.txt line 56

Quoted 5 lines
docs/generic-loader.txt new file +3 -0
+
+An example of loading an ELF file which CPU0 will boot is shown below:
+    -device loader,file=./images/boot.elf,cpu-num=0

--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org