Skip to contentSkip to conversation

Implement Hex file loader and add test case

RFC PATCH Superseded Su Hang 2 participants 5 messages Apr 4, 2018

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Su Hang

author
#0 lore raw

v2 cover Implement Hex file loader and add test case

These series of patchs implement Intel Hexadecimal File loader and add QTest testcase to verify the correctness of Loader.

v1: Basic version.
v2: Replace do{}while(cond); block with for(;;) block to suppress code style complain.

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

 hw/arm/boot.c          |   9 +-
 hw/core/loader.c       | 280 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h    |  17 +++
 tests/Makefile.include |   2 +
 tests/hexloader-test.c |  56 ++++++++++
 tests/test.hex         |  11 ++
 6 files changed, 374 insertions(+), 1 deletion(-)
 create mode 100644 tests/hexloader-test.c
 create mode 100644 tests/test.hex

--
2.7.4

author
#1 lore raw

RFC PATCH v2 1/2 Implement .hex file loader

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 Arduino, ARM, STM32, etc.

---
 hw/arm/boot.c       |   9 +-
 hw/core/loader.c    | 280 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h |  17 ++++
 3 files changed, 305 insertions(+), 1 deletion(-)
hw/arm/boot.c +8 -1
index 9319b12fcd2a..07ce54e5936d 100644
@@ -1060,8 +1060,15 @@ static void arm_load_kernel_notify(Notifier *notifier, void *data)
         kernel_size = load_aarch64_image(info->kernel_filename,
                                          info->loader_start, &entry, as);
         is_linux = 1;
+    } else if (kernel_size < 0 && strstr(info->kernel_filename, ".hex")) {
+        /* 32-bit ARM .hex file */
+        entry = info->loader_start + KERNEL_LOAD_ADDR;
+        kernel_size = load_targphys_hex_as(info->kernel_filename, entry,
+                                           info->ram_size - KERNEL_LOAD_ADDR,
+                                           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 +280 -0

283 lines not on this page · raw message

include/hw/loader.h +17 -0
index 5ed3fd8ae67a..176b11221a27 100644
@@ -28,6 +28,20 @@ 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_image_targphys_hex_as:
+ * @filename: Path to the .hex file
+ * @addr: Address to load the .hex file to
+ * @max_sz: The maximum size of the .hex file to load
+ * @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 addr, uint64_t max_sz, AddressSpace *as);
+
 /** load_image_targphys:
  * Same as load_image_targphys_as(), but doesn't allow the caller to specify
  * an AddressSpace.
@@ -210,6 +224,9 @@ void pstrcpy_targphys(const char *name,
 extern bool option_rom_has_mr;
 extern bool rom_file_has_mr;
 
+int rom_add_hex_file(const char *file, const char *fw_dir,
+                 hwaddr addr, int32_t bootindex,
+                 bool option_rom, MemoryRegion *mr, AddressSpace *as);
 int rom_add_file(const char *file, const char *fw_dir,
                  hwaddr addr, int32_t bootindex,
                  bool option_rom, MemoryRegion *mr, AddressSpace *as);

--
2.7.4

author
#2 lore raw

RFC PATCH v2 2/2 Add QTest testcase for the Intel Hexadecimal Object File Loader.

In reply to 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.

---
 tests/Makefile.include |  2 ++
 tests/hexloader-test.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test.hex         | 11 ++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 tests/hexloader-test.c
 create mode 100644 tests/test.hex
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/hexloader-test.c new file +56 -0

56 lines not on this page · raw message

tests/test.hex new file +11 -0
new file mode 100644
index 000000000000..59b96e3e6fa7
@@ -0,0 +1,11 @@
+:1000000004D09FE5160000EBFEFFFFEA9810010008
+:1000100004B02DE500B08DE20CD04DE208000BE5F8
+:10002000060000EA08301BE50020D3E52C309FE5F0
+:10003000002083E508301BE5013083E208300BE542
+:1000400008301BE50030D3E5000053E3F4FFFF1A4E
+:100050000000A0E100D08BE204B09DE41EFF2FE180
+:1000600000101F1000482DE904B08DE208009FE544
+:10007000E6FFFFEB0000A0E10088BDE8840001007E
+:1000800000101F1048656C6C6F20776F726C6421D4
+:020090000A0064
+:00000001FF

--
2.7.4

#3 lore raw
In reply to Su Hang
Quoted 6 lines · Su Hang

These series of patchs implement Intel Hexadecimal File loader and add QTest testcase to verify the correctness of Loader.

v1: Basic version.
v2: Replace do{}while(cond); block with for(;;) block to suppress code style complain.

Better than rewriting code to work around a false positive is to fix the false positive in the first place. And I see you have a patch proposed for that, but it was still marked RFC; unfortunately, during freeze, patches that are marked RFC tend to be deferred in favor of patches marked 'for-2.12' (RFC means the patch may be incomplete, and there's no rush to get an incomplete patch in before the release, after all). I'll reply on that patch, now that it's got my attention; but for this particular series, I'm more comfortable leaving the review to the maintainer, modulo one comment:

Quoted 5 lines

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

Quoted 9 lines
 hw/arm/boot.c          |   9 +-
 hw/core/loader.c       | 280 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h    |  17 +++
 tests/Makefile.include |   2 +
 tests/hexloader-test.c |  56 ++++++++++
 tests/test.hex         |  11 ++
 6 files changed, 374 insertions(+), 1 deletion(-)
 create mode 100644 tests/hexloader-test.c
 create mode 100644 tests/test.hex

This creates new files that are not covered by MAINTAINERS. Please be sure to patch MAINTAINERS as well in your v3.

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

Thanks for your reply! Owning to cultural difference, I used to regard “RFC” as a way of show modest... I will apply your suggestion in v3.

Best,
Su Hang

Quoted 41 lines · Eric Blake
Quoted 6 lines · Su Hang

These series of patchs implement Intel Hexadecimal File loader and add QTest testcase to verify the correctness of Loader.

v1: Basic version.
v2: Replace do{}while(cond); block with for(;;) block to suppress code style complain.

Better than rewriting code to work around a false positive is to fix the false positive in the first place. And I see you have a patch proposed for that, but it was still marked RFC; unfortunately, during freeze, patches that are marked RFC tend to be deferred in favor of patches marked 'for-2.12' (RFC means the patch may be incomplete, and there's no rush to get an incomplete patch in before the release, after all). I'll reply on that patch, now that it's got my attention; but for this particular series, I'm more comfortable leaving the review to the maintainer, modulo one comment:

Quoted 5 lines

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

Quoted 9 lines
 hw/arm/boot.c          |   9 +-
 hw/core/loader.c       | 280 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h    |  17 +++
 tests/Makefile.include |   2 +
 tests/hexloader-test.c |  56 ++++++++++
 tests/test.hex         |  11 ++
 6 files changed, 374 insertions(+), 1 deletion(-)
 create mode 100644 tests/hexloader-test.c
 create mode 100644 tests/test.hex

This creates new files that are not covered by MAINTAINERS. Please be sure to patch MAINTAINERS as well in your v3.

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