Skip to contentSkip to conversation

Implement Hex file loader and add test case

PATCH Superseded Su Hang 2 participants 4 messages Apr 20, 2018 – Apr 23, 2018

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Su Hang

author
#0 lore raw

v5 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.
v3: Add two new files information in MAINTAINERS.
v4: Correct the 'test.hex' path in hexloader-test.c.
v5: Split huge parse_hex_blob() function into four small function;

    Add check for memory bounds;
    Check validation for Record type;
    Replace function ctoh() with glib function g_ascii_xdigit_value();
    Remove check for '.hex' suffix;
    Remove unnecessary type cast;
    Remove duplicate zero-initialization;
    Correct typos;

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

 MAINTAINERS                          |   6 +
 hw/arm/boot.c                        |  18 ++-
 hw/core/loader.c                     | 253 +++++++++++++++++++++++++++++++++++
 include/hw/loader.h                  |  14 ++
 tests/Makefile.include               |   2 +
 tests/hex-loader-check-data/test.hex |  11 ++
 tests/hexloader-test.c               |  56 ++++++++
 7 files changed, 355 insertions(+), 5 deletions(-)
 create mode 100644 tests/hex-loader-check-data/test.hex
 create mode 100644 tests/hexloader-test.c
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v5 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       |  18 ++--
 hw/core/loader.c    | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/loader.h |  14 +++
 3 files changed, 280 insertions(+), 5 deletions(-)
hw/arm/boot.c +13 -5
index 26184bcd7c26..898a7af114ea 100644
@@ -1070,12 +1070,20 @@ 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) {
-        /* 32-bit ARM */
+    }
+    if (kernel_size < 0) {
+        /* 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);
+    }
+    if (kernel_size < 0) {
+        /* 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,
-                                             as);
+        kernel_size =
+            load_image_targphys_as(info->kernel_filename, entry,
+                                   info->ram_size - KERNEL_LOAD_ADDR, as);
         is_linux = 1;
     }
     if (kernel_size < 0) {
hw/core/loader.c +253 -0

256 lines not on this page · raw message

include/hw/loader.h +14 -0
index 5ed3fd8ae67a..2b0cdfe56e70 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.
Signature · 1 line
2.7.4
#2 lore raw

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

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 1074

Quoted 12 lines · Su Hang
hw/arm/boot.c +2 -2
index 26184bcd7c26..898a7af114ea 100644
@@ -1070,12 +1070,20 @@ 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) {
-        /* 32-bit ARM */
+    }
+    if (kernel_size < 0) {

Please try the Intel HEX loader right after load_uimage_as(). In theory it should be usable on aarch64 too, so the Intel HEX loader needs to be called earlier.

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 1076

Quoted 2 lines
hw/arm/boot.c +2 -0
+        /* 32-bit ARM .hex file */
+        entry = info->loader_start + KERNEL_LOAD_ADDR;

KERNEL_LOAD_ADDR is for Linux, can you explain why you used it for this non-Linux image format?

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 1079

Quoted 3 lines
hw/arm/boot.c +3 -0
+        kernel_size =
+            load_targphys_hex_as(info->kernel_filename, entry,
+                                 info->ram_size - KERNEL_LOAD_ADDR, as);

The Intel HEX file may provide the entrypoint address, so the argument should be &entry.

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data) · line 1087

Quoted 8 lines
hw/arm/boot.c +8 -0
+/* return size or -1 if error */
+static size_t handle_record_type(const char *filename, HexLine *line,
+                                 uint8_t *bin_buf, const hwaddr addr,
+                                 size_t *total_size, uint8_t *ext_linear_record,
+                                 uint32_t *next_address_to_write,
+                                 uint32_t *current_address,
+                                 uint32_t *current_rom_index,
+                                 uint32_t *rom_start_address, AddressSpace *as)

There are a lot of arguments. A struct makes the code easier to read and prevents bugs if arguments are accidentally swapped (e.g. next_address_to_write and current_address have the same type so the compiler cannot warn if they are swapped):

  typedef struct {
      HexLine line;
      uint8_t *bin_buf;
      ...
  } HexParser;

static size_t handle_record_type(HexParser *parser);

Quoted 4 lines
hw/arm/boot.c +4 -0
+{
+    switch (line->record_type) {
+    case DATA_RECORD:
+        *current_address =

hw/arm/boot.c static void arm_load_kernel_notify(Notifier *notifier, void *data)

Quoted 10 lines
hw/arm/boot.c +10 -0
+            (*next_address_to_write & 0xffff0000) | line->address;
+        /* verify this is a contiguous block of memory */
+        if (*current_address != *next_address_to_write ||
+            *ext_linear_record == 1) {
+            if (*ext_linear_record != 1) {
+                return -1;
+            }
+            *ext_linear_record = 0;
+            *rom_start_address = *current_address;
+        }

What is the purpose of ext_linear_record?

Given this input file:

:10000000C0070000D1060000D1000000B1060000CA
<--- address 0010 is skipped
:100020000000000000000000000000005107000078
<--- address 0030 is skipped
:10004000EF000000F9000000030100000D010000B6
:00000001FF

ext_linear_record is 0 when the final data record is handled, so handle_record_type() returns -1. I don't understand why the parser should reject this input file.

I think a rom blob should be added if data records are discontiguous.

Quoted 17 lines
hw/arm/boot.c +17 -0
+
+        /* copy from line buffer to output bin_buf */
+        memcpy(bin_buf + *current_rom_index, line->data, line->byte_count);
+        *current_rom_index += line->byte_count;
+        *total_size += line->byte_count;
+        /* Save next address to write */
+        *next_address_to_write = *current_address + line->byte_count;
+        break;
+
+    case EOF_RECORD:
+        if (*current_rom_index != 0) {
+            rom_add_blob(filename, bin_buf, *current_rom_index,
+                         *current_rom_index, addr + *rom_start_address, NULL,
+                         NULL, NULL, as, true);
+        }
+        return *total_size;
+    case EXT_SEG_ADDR_RECORD:

hw/arm/boot.c

Quoted 10 lines
hw/arm/boot.c +10 -0
+    case EXT_LINEAR_ADDR_RECORD:
+        if (line->byte_count != 2) {
+            return -1;
+        }
+
+        if (*current_rom_index != 0) {
+            rom_add_blob(filename, bin_buf, *current_rom_index,
+                         *current_rom_index, addr + *rom_start_address, NULL,
+                         NULL, NULL, as, true);
+            memset(bin_buf, 0, *current_rom_index);

This is unnecessary since bin_buf is overwritten from index 0 (*current_rom_index = 0 below) before it is read again.

Quoted 1 line

+ memset(line, 0, sizeof(HexLine));

This is unnecessary since in_process = 0 and the next ':' input character will clear line.

hw/arm/boot.c

Quoted 9 lines
hw/arm/boot.c +9 -0
+        }
+
+        *current_rom_index = 0;
+        *ext_linear_record = 1;
+
+        /* save next address to write,
+         * in case of non-contiguous block of memory */
+        *next_address_to_write =
+            line->record_type == EXT_SEG_ADDR_RECORD

This is broken because of the memset(line, 0, sizeof(HexLine)) above.

hw/arm/boot.c

Quoted 11 lines
hw/arm/boot.c +11 -0
+                ? ((line->data[0] << 12) | (line->data[1] << 4))
+                : ((line->data[0] << 24) | (line->data[1] << 16));
+        break;
+
+    case START_SEG_ADDR_RECORD:
+        /* nothing to do */
+        break;
+
+    case START_LINEAR_ADDR_RECORD:
+        /* nothing to do */
+        break;

These record types specify the entry point (the starting address). Please store the entry point into arm_boot_info->entry so that do_cpu_reset() starts the CPU at the right address.

hw/arm/boot.c

Quoted 2 lines
hw/arm/boot.c +2 -0
+/* return size or -1 if error */
+static size_t __parse_hex_blob(const char *filename, const hwaddr addr,

QEMU does not use double underscore function names because they are reserved by the C standard (see 2.4 in ./HACKING and 7.1.3 in the C standard). Please rename this function.

hw/arm/boot.c

Quoted 4 lines
hw/arm/boot.c +4 -0
+                               uint8_t *hex_blob, off_t hex_blob_size,
+                               uint8_t *bin_buf, AddressSpace *as)
+{
+    uint8_t ext_linear_record = 1; /* record non-constitutes block */

s/constitutes/contiguous/ ?

hw/arm/boot.c

Quoted 2 lines
hw/arm/boot.c +2 -0
+    uint8_t in_process = 0;        /* avoid re-enter and
+                                    * check whether record begin with ':' */

As mentioned in previous review comments, please use bool for boolean flags and not integer types.

hw/arm/boot.c

Quoted 8 lines
hw/arm/boot.c +8 -0
+/* return size or -1 if error */
+int load_targphys_hex_as(const char *filename, hwaddr addr, uint64_t max_sz,
+                         AddressSpace *as)
+{
+    int size;
+
+    size = get_image_size(filename);
+    if (size < 0 || size > max_sz) {

Why check the size of the file? I'm not sure why the size of the file matters here.

Quoted 10 lines
hw/arm/boot.c +5 -0
+        return -1;
+    }
+
+    return parse_hex_blob(filename, addr, as);
+}
include/hw/loader.h +0 -0
index 5ed3fd8ae67a..2b0cdfe56e70 100644

include/hw/loader.h ssize_t load_image_size(const char *filename, void *addr, size_t size); · line 33

Quoted 6 lines
include/hw/loader.h +3 -0
@@ -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

Do you have a use in mind for this argument? It seems like most callers would pass 0 and treat the Intel HEX file addresses as absolute addresses.

If there is no real need for this then please drop it.

author
#3 lore raw

PATCH v5 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.

---
 MAINTAINERS                          |  6 ++++
 tests/Makefile.include               |  2 ++
 tests/hex-loader-check-data/test.hex | 11 +++++++
 tests/hexloader-test.c               | 56 ++++++++++++++++++++++++++++++++++++
 4 files changed, 75 insertions(+)
 create mode 100644 tests/hex-loader-check-data/test.hex
 create mode 100644 tests/hexloader-test.c
MAINTAINERS +6 -0
index 24b70169bc37..3d37d04c3345 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
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 +11 -0
new file mode 100644
index 000000000000..59b96e3e6fa7
@@ -0,0 +1,11 @@
+: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

Signature · 1 line
2.7.4