Skip to contentSkip to conversation

Implement Hex file loader and add test case

GIT PULL Su Hang 3 participants 7 messages May 24, 2018 – May 25, 2018

lore
Share & tools
lore

Su Hang

author
#0 lore raw

The following changes since commit 9cac60db4513f968b56e12720c1ea433be318cc3:

Merge remote-tracking branch 'remotes/mwalle/tags/lm32-queue/20180521' into staging (2018-05-24 10:25:43 +0100)

are available in the git repository at:

git://github.com/DarcySail/qemu.git foo.for-upstream

for you to fetch changes up to a9120811a5b9109d79f704e0a79fb851860e9f04:

Add QTest testcase for the Intel Hexadecimal Object File Loader. (2018-05-24 18:54:02 +0800)

----------------------------------------------------------------
Su Hang (2):
Implement .hex file loader
Add QTest testcase for the Intel Hexadecimal Object File Loader.

 MAINTAINERS                          |   6 +
 configure                            |   4 +
 hw/arm/boot.c                        |   7 +-
 hw/core/loader.c                     | 246 +++++++++++++++++++++++++++++++++++
 include/hw/loader.h                  |  12 ++
 tests/Makefile.include               |   2 +
 tests/hex-loader-check-data/test.hex |  12 ++
 tests/hexloader-test.c               |  56 ++++++++
 8 files changed, 344 insertions(+), 1 deletion(-)
 create mode 100644 tests/hex-loader-check-data/test.hex
 create mode 100644 tests/hexloader-test.c
author
#1 lore raw

[Qemu-devel] [PULL 2/2] Add QTest testcase for the Intel Hexadecimal

'test.hex' file is a bare metal ARM software stored in Hexadecimal Object Format. When it's loaded by QEMU, it will print "Hello world!\n" on console.

pre_store array in 'hexloader-test.c' file, stores the binary format of 'test.hex' file, which is used to verify correctness.

---
 MAINTAINERS                          |  6 ++++
 configure                            |  4 +++
 tests/Makefile.include               |  2 ++
 tests/hex-loader-check-data/test.hex | 12 ++++++++
 tests/hexloader-test.c               | 56 ++++++++++++++++++++++++++++++++++++
 5 files changed, 80 insertions(+)
 create mode 100644 tests/hex-loader-check-data/test.hex
 create mode 100644 tests/hexloader-test.c
MAINTAINERS +6 -0
index e187b1f18f27..817ac7176b91 100644
@@ -1291,6 +1291,12 @@ F: hw/core/generic-loader.c
 F: include/hw/core/generic-loader.h
 F: docs/generic-loader.txt
 
+Intel Hexadecimal Object File Loader
+M: Su Hang <[email protected]>
+S: Maintained
+F: tests/hexloader-test.c
+F: tests/hex-loader-check-data/test.hex
+
 CHRP NVRAM
 M: Thomas Huth <[email protected]>
 S: Maintained
configure +4 -0
index 59f91ab3f996..15cb143b17ec 100755
@@ -7188,6 +7188,10 @@ for test_file in $(find $source_path/tests/acpi-test-data -type f)
 do
     FILES="$FILES tests/acpi-test-data$(echo $test_file | sed -e 's/.*acpi-test-data//')"
 done
+for test_file in $(find $source_path/tests/hex-loader-check-data -type f)
+do
+    FILES="$FILES tests/hex-loader-check-data$(echo $test_file | sed -e 's/.*hex-loader-check-data//')"
+done
 mkdir -p $DIRS
 for f in $FILES ; do
     if [ -e "$source_path/$f" ] && [ "$pwd_is_source_path" != "y" ]; then
tests/Makefile.include +2 -0
index 3b9a5e31a2c2..f4a3e71f34ee 100644
@@ -380,6 +380,7 @@ check-qtest-arm-y += tests/test-arm-mptimer$(EXESUF)
 gcov-files-arm-y += hw/timer/arm_mptimer.c
 check-qtest-arm-y += tests/boot-serial-test$(EXESUF)
 check-qtest-arm-y += tests/sdhci-test$(EXESUF)
+check-qtest-arm-y += tests/hexloader-test$(EXESUF)
 
 check-qtest-aarch64-y = tests/numa-test$(EXESUF)
 check-qtest-aarch64-y += tests/sdhci-test$(EXESUF)
@@ -755,6 +756,7 @@ tests/qmp-test$(EXESUF): tests/qmp-test.o
 tests/device-introspect-test$(EXESUF): tests/device-introspect-test.o
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
+tests/hexloader-test$(EXESUF): tests/hexloader-test.o
 tests/endianness-test$(EXESUF): tests/endianness-test.o
 tests/spapr-phb-test$(EXESUF): tests/spapr-phb-test.o $(libqos-obj-y)
 tests/prom-env-test$(EXESUF): tests/prom-env-test.o $(libqos-obj-y)
tests/hex-loader-check-data/test.hex new file +12 -0
new file mode 100644
index 000000000000..7e99b452f5cc
@@ -0,0 +1,12 @@
+:020000040001F9
+:1000000004D09FE5160000EBFEFFFFEA9810010008
+:1000100004B02DE500B08DE20CD04DE208000BE5F8
+:10002000060000EA08301BE50020D3E52C309FE5F0
+:10003000002083E508301BE5013083E208300BE542
+:1000400008301BE50030D3E5000053E3F4FFFF1A4E
+:100050000000A0E100D08BE204B09DE41EFF2FE180
+:1000600000101F1000482DE904B08DE208009FE544
+:10007000E6FFFFEB0000A0E10088BDE8840001007E
+:1000800000101F1048656C6C6F20776F726C6421D4
+:020090000A0064
+:00000001FF
tests/hexloader-test.c new file +56 -0

56 lines not on this page · raw message

--
2.7.4

#2 lore raw

Re: [Qemu-devel] [PULL 2/2] Add QTest testcase for the Intel Hexadecimal

Quoted 11 lines · Su Hang

'test.hex' file is a bare metal ARM software stored in Hexadecimal Object Format. When it's loaded by QEMU, it will print "Hello world!\n" on console.

pre_store array in 'hexloader-test.c' file, stores the binary format of 'test.hex' file, which is used to verify correctness.

Quoted 6 lines
---
  MAINTAINERS                          |  6 ++++
  configure                            |  4 +++
  tests/Makefile.include               |  2 ++
  tests/hex-loader-check-data/test.hex | 12 ++++++++
  tests/hexloader-test.c               | 56 ++++++++++++++++++++++++++++++++++++

The previous patch also touched:

 hw/arm/boot.c       |   7 +-
 hw/core/loader.c    | 246 
++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h |  12 +++

MAINTAINERS F: hw/core/generic-loader.c · line 1299

Quoted 11 lines

+++ b/MAINTAINERS

MAINTAINERS +6 -0
@@ -1291,6 +1291,12 @@ F: hw/core/generic-loader.c
  F: include/hw/core/generic-loader.h
  F: docs/generic-loader.txt
 
+Intel Hexadecimal Object File Loader
+M: Su Hang <[email protected]>
+S: Maintained
+F: tests/hexloader-test.c
+F: tests/hex-loader-check-data/test.hex
+

It looks odd having a maintainer that claims only test files; do you want to also list some of the other files touched by this patch so that you get notification if one of the implementation files has subsequent patches (rather than just the test files)?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#3 lore raw

Re: [Qemu-devel] [PULL 2/2] Add QTest testcase for the Intel Hexadecimal

Sure, I will list other involved files. Thanks for you suggestion.

SU Hang

Quoted 47 lines · Eric Blake
Quoted 11 lines · Su Hang

'test.hex' file is a bare metal ARM software stored in Hexadecimal Object Format. When it's loaded by QEMU, it will print "Hello world!\n" on console.

pre_store array in 'hexloader-test.c' file, stores the binary format of 'test.hex' file, which is used to verify correctness.

Quoted 6 lines
---
  MAINTAINERS                          |  6 ++++
  configure                            |  4 +++
  tests/Makefile.include               |  2 ++
  tests/hex-loader-check-data/test.hex | 12 ++++++++
  tests/hexloader-test.c               | 56 ++++++++++++++++++++++++++++++++++++

The previous patch also touched:

  hw/arm/boot.c       |   7 +-
  hw/core/loader.c    | 246 
++++++++++++++++++++++++++++++++++++++++++++++++++++
  include/hw/loader.h |  12 +++

MAINTAINERS F: hw/core/generic-loader.c · line 1299

Quoted 11 lines

+++ b/MAINTAINERS

MAINTAINERS +6 -0
@@ -1291,6 +1291,12 @@ F: hw/core/generic-loader.c
  F: include/hw/core/generic-loader.h
  F: docs/generic-loader.txt
 
+Intel Hexadecimal Object File Loader
+M: Su Hang <[email protected]>
+S: Maintained
+F: tests/hexloader-test.c
+F: tests/hex-loader-check-data/test.hex
+

It looks odd having a maintainer that claims only test files; do you want to also list some of the other files touched by this patch so that you get notification if one of the implementation files has subsequent patches (rather than just the test files)?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#4 lore raw

[Qemu-devel] [PULL 1/2] Implement .hex file loader

In reply to Su Hang

This patch adds Intel Hexadecimal Object File format support to the loader. The file format specification is available here:
http://www.piclist.com/techref/fileext/hex/intel.htm

The file format is mainly intended for embedded systems and microcontrollers, such as Micro:bit Arduino, ARM, STM32, etc.

---
 hw/arm/boot.c       |   7 +-
 hw/core/loader.c    | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h |  12 +++
 3 files changed, 264 insertions(+), 1 deletion(-)
hw/arm/boot.c +6 -1
index 9496f331a8fa..17fe1a8c287b 100644
@@ -1038,12 +1038,17 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         kernel_size = load_uimage_as(info->kernel_filename, &entry, NULL,
                                      &is_linux, NULL, NULL, as);
     }
+    if (kernel_size < 0) {
+        /* 32-bit ARM Intel HEX file */
+        entry = 0;
+        kernel_size = load_targphys_hex_as(info->kernel_filename, &entry, as);
+    }
     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
         kernel_size = load_aarch64_image(info->kernel_filename,
                                          info->loader_start, &entry, as);
         is_linux = 1;
     } else if (kernel_size < 0) {
-        /* 32-bit ARM */
+        /* 32-bit ARM binary file */
         entry = info->loader_start + KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys_as(info->kernel_filename, entry,
                                              info->ram_size - KERNEL_LOAD_ADDR,
hw/core/loader.c +246 -0

249 lines not on this page · raw message

include/hw/loader.h +12 -0
index 5ed3fd8ae67a..bd9d7b380076 100644
@@ -28,6 +28,18 @@ ssize_t load_image_size(const char *filename, void *addr, size_t size);
 int load_image_targphys_as(const char *filename,
                            hwaddr addr, uint64_t max_sz, AddressSpace *as);
 
+/**load_targphys_hex_as:
+ * @filename: Path to the .hex file
+ * @entry: Store the entry point given by the .hex file
+ * @as: The AddressSpace to load the .hex file to. The value of
+ *      address_space_memory is used if nothing is supplied here.
+ *
+ * Load a fixed .hex file into memory.
+ *
+ * Returns the size of the loaded .hex file on success, -1 otherwise.
+ */
+int load_targphys_hex_as(const char *filename, hwaddr *entry, AddressSpace *as);
+
 /** load_image_targphys:
  * Same as load_image_targphys_as(), but doesn't allow the caller to specify
  * an AddressSpace.

--
2.7.4

#5 lore raw

Re: [Qemu-devel] [PULL 1/2] Implement .hex file loader

Quoted 6 lines · Su Hang

This patch adds Intel Hexadecimal Object File format support to the loader. The file format specification is available here:
http://www.piclist.com/techref/fileext/hex/intel.htm

The file format is mainly intended for embedded systems and microcontrollers, such as Micro:bit Arduino, ARM, STM32, etc.

Could we have some more rationale here, please? For instance, we could mention who ships binaries in this format, what other boot loaders handle this, and so on. The idea is to explain why it's worth our having this code, rather than just having users convert their hex files to a format we already handle (ie why there is a significant body of users with hex format images they want to load).

Quoted 13 lines
hw/arm/boot.c +5 -0
index 9496f331a8fa..17fe1a8c287b 100644
@@ -1038,12 +1038,17 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         kernel_size = load_uimage_as(info->kernel_filename, &entry, NULL,
                                      &is_linux, NULL, NULL, as);
     }
+    if (kernel_size < 0) {
+        /* 32-bit ARM Intel HEX file */
+        entry = 0;
+        kernel_size = load_targphys_hex_as(info->kernel_filename, &entry, as);
+    }

hw/arm/boot.c void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) · line 1054

Quoted 10 lines
hw/arm/boot.c +1 -1
     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
         kernel_size = load_aarch64_image(info->kernel_filename,
                                          info->loader_start, &entry, as);
         is_linux = 1;
     } else if (kernel_size < 0) {
-        /* 32-bit ARM */
+        /* 32-bit ARM binary file */
         entry = info->loader_start + KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys_as(info->kernel_filename, entry,
                                              info->ram_size - KERNEL_LOAD_ADDR,

I don't think it makes sense to add support for this format here. Who is using hex files to provide A-class Linux kernels?

(Note that hw/arm/boot.c does *not* handle -kernel for M-class cores.)

There might be an argument for wiring up hex file support in the "generic loader" hw/misc/generic-loader.c (documentation in docs/generic-loader.txt).

It looks like your implementation calls rom_add_blob_fixed_as() as it goes along to add chunks of data to guest memory, but it doesn't do anything to undo that if it detects an error in the input halfway through and returns a failure code. That matters if you want to put it in a chain of "try this format? if that didn't work, try this other format; last resort, load as binary" calls like you have here.

It's probably worth splitting the "add load_targphys_hex_as()" patch from the one that wires it up for a specific loader.

thanks
-- PMM

author
#6 lore raw

Re: [Qemu-devel] [PULL 1/2] Implement .hex file loader

You are right. I'm shocked by my mistakes...

Quoted 5 lines

The hex format is used mainly by Cortex-M boards. This code doesn't support them, because armv7m uses its own load function. Could you add support for armv7m?

Best regards, Julia Suvorova.

Julia indeed have mentioned me about this. But at that time, I was thinking about "get this patch merged first, then add support for armv7m". I am wrong.

SU Hang

Quoted 69 lines

-----Original Messages-----
From: "Peter Maydell" <[email protected]>
Sent Time: 2018-05-24 22:40:04 (Thursday)
To: "Su Hang" <[email protected]>
Cc: "Stefan Hajnoczi" <[email protected]>, [email protected], "Joel Stanley" <[email protected]>, "QEMU Developers" <[email protected]>
Subject: Re: [Qemu-devel] [PULL 1/2] Implement .hex file loader

Quoted 6 lines · Su Hang

This patch adds Intel Hexadecimal Object File format support to the loader. The file format specification is available here:
http://www.piclist.com/techref/fileext/hex/intel.htm

The file format is mainly intended for embedded systems and microcontrollers, such as Micro:bit Arduino, ARM, STM32, etc.

Could we have some more rationale here, please? For instance, we could mention who ships binaries in this format, what other boot loaders handle this, and so on. The idea is to explain why it's worth our having this code, rather than just having users convert their hex files to a format we already handle (ie why there is a significant body of users with hex format images they want to load).

Quoted 13 lines
hw/arm/boot.c +5 -0
index 9496f331a8fa..17fe1a8c287b 100644
@@ -1038,12 +1038,17 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info)
         kernel_size = load_uimage_as(info->kernel_filename, &entry, NULL,
                                      &is_linux, NULL, NULL, as);
     }
+    if (kernel_size < 0) {
+        /* 32-bit ARM Intel HEX file */
+        entry = 0;
+        kernel_size = load_targphys_hex_as(info->kernel_filename, &entry, as);
+    }

hw/arm/boot.c void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) · line 1054

Quoted 10 lines
hw/arm/boot.c +1 -1
     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
         kernel_size = load_aarch64_image(info->kernel_filename,
                                          info->loader_start, &entry, as);
         is_linux = 1;
     } else if (kernel_size < 0) {
-        /* 32-bit ARM */
+        /* 32-bit ARM binary file */
         entry = info->loader_start + KERNEL_LOAD_ADDR;
         kernel_size = load_image_targphys_as(info->kernel_filename, entry,
                                              info->ram_size - KERNEL_LOAD_ADDR,

I don't think it makes sense to add support for this format here. Who is using hex files to provide A-class Linux kernels?

(Note that hw/arm/boot.c does *not* handle -kernel for M-class cores.)

There might be an argument for wiring up hex file support in the "generic loader" hw/misc/generic-loader.c (documentation in docs/generic-loader.txt).

It looks like your implementation calls rom_add_blob_fixed_as() as it goes along to add chunks of data to guest memory, but it doesn't do anything to undo that if it detects an error in the input halfway through and returns a failure code. That matters if you want to put it in a chain of "try this format? if that didn't work, try this other format; last resort, load as binary" calls like you have here.

It's probably worth splitting the "add load_targphys_hex_as()" patch from the one that wires it up for a specific loader.

thanks
-- PMM