Skip to contentSkip to conversation

hw/core/loader: Add Extended Tektronix HEX support

PATCH In review Nagendran K 2 participants 4 messages Nov 14 – Nov 20 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/core/loader: Add Extended Tektronix HEX support

---
 hw/core/loader.c         | 231 +++++++++++++++++++++++++++++++++++++++
 include/hw/core/loader.h |  15 +++
 2 files changed, 246 insertions(+)
hw/core/loader.c +231 -0
index 5cbfba0a86d24569923c863d6ecda125ce43bceb..33ccb51d56dda1035e815479e20c2a6c793843d9 100644
@@ -1940,3 +1940,234 @@ ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
     g_free(hex_blob);
     return total_size;
 }
+
+typedef enum Ext_Tek_HexRecord Ext_Tek_HexRecord;
+enum Ext_Tek_HexRecord {
+    TEK_DATA_RECORD = 0,
+    TEK_TERMINATION_RECORD
+};
+
+#define DATA_FIELD_MAX_LEN 0xff
+
+typedef struct {
+    uint8_t record_length;
+    uint8_t record_type;
+    uint8_t checksum;
+    uint32_t address;
+    uint8_t data[DATA_FIELD_MAX_LEN];
+} Ext_Tek_HexLine;
+
+/* return 0 or -1 if error */
+static bool parse_ext_tek_hex_record(Ext_Tek_HexLine *line,
+                                     uint8_t *our_checksum,
+                                     const uint8_t c,
+                                     uint32_t *index,
+                                     const bool in_process)
+{
+    /* +---------+---------------+-----------+---------------+-------------+
+     * | record  |    record     |           |               |             |
+     * | length  |    type       | checksum  |   address     |   data      |
+     * +---------+---------------+-----------+---------------+-------------+
+     *     ^             ^            ^              ^            ^
+     * |1 byte   |    1 bytes    |1 byte     | 1 to N byte   | 0-255 bytes |
+     */
+    uint8_t value = 0, address_len = 0;
+    uint32_t idx = *index;
+    /* ignore space */
+    if (g_ascii_isspace(c)) {
+        return true;
+    }
+    if (!g_ascii_isxdigit(c) || !in_process) {
+        return false;
+    }
+    /* still idx is computed nibble wise */
+    value = g_ascii_xdigit_value(c);
+    value = (idx & 0x1) ? (value & 0xf) : (value << 4);
+    if (idx < 2) {
+        line->record_length |= value;
+    } else if (idx == 2) {
+        line->record_type |= value;
+    } else if (2 < idx && idx <= 4) {
+        line->checksum |= value;
+    }
+    /*
+     * Extracting address length from record to fill that many no of address
+     * characters in the address field of the line structure.
+     */
+    else if (idx == 5) {
+        address_len |= value;
+    } else if (6 <= idx &&
+                   idx <= (6 + address_len)) {
+        line->address <<= address_len;
+        line->address += g_ascii_xdigit_value(c);
+    }
+    /*
+     * Checking whether index above address last char index
+     * and reaches till record length. Right shifting one divides the
+     * nibble wise index by 2 to get the byte wise index to
+     * fill the data field of the line structure
+     */
+    else if ((6 + address_len) < idx &&
+              idx <= line->record_length) {
+        line->data[(idx - (6 + address_len)) >> 1] |= value;
+    } else {
+        return false;
+    }
+    *our_checksum += value;
+    ++(*index);
+    return true;
+}
+
+typedef struct {
+    const char *filename;
+    Ext_Tek_HexLine line;
+    uint8_t *bin_buf;
+    hwaddr *start_addr;
+    int total_size;
+    uint32_t next_address_to_write;
+    uint32_t current_address;
+    uint32_t current_rom_index;
+    uint32_t rom_start_address;
+    AddressSpace *as;
+    bool complete;
+} Ext_TekHexParser;
+
+/* return size or -1 if error */
+static int handle_ext_tek_hex_record_type(Ext_TekHexParser *parser)
+{
+    Ext_Tek_HexLine *line = &(parser->line);
+    switch (line->record_type) {
+    case TEK_DATA_RECORD:
+        parser->current_address = line->address;
+        /* verify this is a contiguous block of memory */
+        if (parser->current_address != parser->next_address_to_write) {
+            if (parser->current_rom_index != 0) {
+                rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                      parser->current_rom_index,
+                                      parser->rom_start_address, parser->as);
+            }
+            parser->rom_start_address = parser->current_address;
+            parser->current_rom_index = 0;
+        }
+
+        /* copy from line buffer to output bin_buf */
+        /* sizeof(line->data) dynamically calculates the size of data field */
+        memcpy(parser->bin_buf + parser->current_rom_index, line->data,
+               sizeof(line->data));
+        parser->current_rom_index += sizeof(line->data);
+        parser->total_size += sizeof(line->data);
+        /* As address is absolute, we dont need to calculate next address */
+        break;
+
+    case TEK_TERMINATION_RECORD:
+        if (parser->current_rom_index != 0) {
+            rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                  parser->current_rom_index,
+                                  parser->rom_start_address, parser->as);
+        }
+        parser->complete = true;
+        return parser->total_size;
+
+    default:
+        return -1;
+    }
+
+    return parser->total_size;
+}
+
+/* return size or -1 if error */
+static int parse_ext_tek_hex_blob(const char *filename,
+                                  hwaddr *addr,
+                                  uint8_t *ext_tek_blob,
+                                  size_t ext_tek_blob_size,
+                                  AddressSpace *as)
+{
+    /* avoid re-enter and check whether record begin with '%' */
+        bool in_process = false;
+        uint8_t *end = ext_tek_blob + ext_tek_blob_size;
+        uint8_t our_checksum = 0;
+        uint32_t record_index = 0;
+
+        Ext_TekHexParser parser = {
+        .filename = filename,
+        .bin_buf = g_malloc(ext_tek_blob_size),
+        .start_addr = addr,
+        .as = as,
+        .complete = false
+        };
+
+        rom_transaction_begin();
+
+        for (; ext_tek_blob < end && !parser.complete; ++ext_tek_blob) {
+            switch (*ext_tek_blob) {
+            case '\r':
+            case '\n':
+                if (!in_process) {
+                    break;
+                }
+
+                in_process = false;
+                /*
+                 * record_length byte stores the number of hex characters
+                 * (nibbles) in the record and compare it directly to
+                 * the nibble index(record index).
+                 */
+                if ((parser.line.record_length != record_index) ||
+                    (our_checksum != 0)) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+
+                if (handle_ext_tek_hex_record_type(&parser) == -1) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+
+            /* start of a new record. */
+            case '%':
+                memset(&parser.line, 0, sizeof(Ext_Tek_HexLine));
+                in_process = true;
+                record_index = 0;
+                break;
+
+            /* decoding lines */
+            default:
+                if (!parse_ext_tek_hex_record(&parser.line,
+                                            &our_checksum,
+                                            *ext_tek_blob,
+                                            &record_index, in_process)) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+            }
+        }
+
+    out:
+        g_free(parser.bin_buf);
+        rom_transaction_end(parser.total_size != -1);
+        return parser.total_size;
+}
+
+
+/* return size or -1 if error */
+ssize_t load_targphys_ext_tek_hex_as(const char *filename,
+                                     hwaddr *entry,
+                                     AddressSpace *as)
+{
+    gsize ext_tek_blob_size;
+    gchar *ext_tek_blob;
+    int total_size = 0;
+
+    if (!g_file_get_contents(filename, &ext_tek_blob,
+                             &ext_tek_blob_size, NULL)) {
+        return -1;
+    }
+    total_size = parse_ext_tek_hex_blob(filename, entry,
+                                       (uint8_t *)ext_tek_blob,
+                                       ext_tek_blob_size, as);
+
+    g_free(ext_tek_blob);
+    return total_size;
+}
include/hw/core/loader.h +15 -0
index d9431e8a8d12e3b676160f70ed141f1064143ea3..c0d29b1d4456bb92984749e56f1109f8421cee51 100644
@@ -57,6 +57,21 @@ ssize_t load_image_targphys_as(const char *filename,
 ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
                              AddressSpace *as);
 
+/**
+ * load_targphys_ext_tek_as:
+ * @filename: Path to the .tek file
+ * @entry: Store the entry point given by the .tek file
+ * @as: The AddressSpace to load the .tek file to. The value of
+ *      address_space_memory is used if nothing is supplied here.
+ *
+ * Load a fixed .tek file into memory.
+ *
+ * Returns the size of the loaded .tek file on success, -1 otherwise.
+ */
+ssize_t load_targphys_ext_tek_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.
Signature · 1 line
2.54.0.windows.1
#1 lore raw

On Sat, Nov 14, 2026 at 04:58:50PM +0530, Nagendran K wrote:

Please provide a commit message to explain the purpose of this addition.

Quoted 5 lines
---
 hw/core/loader.c         | 231 +++++++++++++++++++++++++++++++++++++++
 include/hw/core/loader.h |  15 +++
 2 files changed, 246 insertions(+)

....this is adding a new API to common code and then not calling it from anywhere in QEMU. This is pointless and so is not likely to be accepted in isolation without a proposed usage accompanying it.

Quoted 269 lines
hw/core/loader.c +231 -0
index 5cbfba0a86d24569923c863d6ecda125ce43bceb..33ccb51d56dda1035e815479e20c2a6c793843d9 100644
@@ -1940,3 +1940,234 @@ ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
     g_free(hex_blob);
     return total_size;
 }
+
+typedef enum Ext_Tek_HexRecord Ext_Tek_HexRecord;
+enum Ext_Tek_HexRecord {
+    TEK_DATA_RECORD = 0,
+    TEK_TERMINATION_RECORD
+};
+
+#define DATA_FIELD_MAX_LEN 0xff
+
+typedef struct {
+    uint8_t record_length;
+    uint8_t record_type;
+    uint8_t checksum;
+    uint32_t address;
+    uint8_t data[DATA_FIELD_MAX_LEN];
+} Ext_Tek_HexLine;
+
+/* return 0 or -1 if error */
+static bool parse_ext_tek_hex_record(Ext_Tek_HexLine *line,
+                                     uint8_t *our_checksum,
+                                     const uint8_t c,
+                                     uint32_t *index,
+                                     const bool in_process)
+{
+    /* +---------+---------------+-----------+---------------+-------------+
+     * | record  |    record     |           |               |             |
+     * | length  |    type       | checksum  |   address     |   data      |
+     * +---------+---------------+-----------+---------------+-------------+
+     *     ^             ^            ^              ^            ^
+     * |1 byte   |    1 bytes    |1 byte     | 1 to N byte   | 0-255 bytes |
+     */
+    uint8_t value = 0, address_len = 0;
+    uint32_t idx = *index;
+    /* ignore space */
+    if (g_ascii_isspace(c)) {
+        return true;
+    }
+    if (!g_ascii_isxdigit(c) || !in_process) {
+        return false;
+    }
+    /* still idx is computed nibble wise */
+    value = g_ascii_xdigit_value(c);
+    value = (idx & 0x1) ? (value & 0xf) : (value << 4);
+    if (idx < 2) {
+        line->record_length |= value;
+    } else if (idx == 2) {
+        line->record_type |= value;
+    } else if (2 < idx && idx <= 4) {
+        line->checksum |= value;
+    }
+    /*
+     * Extracting address length from record to fill that many no of address
+     * characters in the address field of the line structure.
+     */
+    else if (idx == 5) {
+        address_len |= value;
+    } else if (6 <= idx &&
+                   idx <= (6 + address_len)) {
+        line->address <<= address_len;
+        line->address += g_ascii_xdigit_value(c);
+    }
+    /*
+     * Checking whether index above address last char index
+     * and reaches till record length. Right shifting one divides the
+     * nibble wise index by 2 to get the byte wise index to
+     * fill the data field of the line structure
+     */
+    else if ((6 + address_len) < idx &&
+              idx <= line->record_length) {
+        line->data[(idx - (6 + address_len)) >> 1] |= value;
+    } else {
+        return false;
+    }
+    *our_checksum += value;
+    ++(*index);
+    return true;
+}
+
+typedef struct {
+    const char *filename;
+    Ext_Tek_HexLine line;
+    uint8_t *bin_buf;
+    hwaddr *start_addr;
+    int total_size;
+    uint32_t next_address_to_write;
+    uint32_t current_address;
+    uint32_t current_rom_index;
+    uint32_t rom_start_address;
+    AddressSpace *as;
+    bool complete;
+} Ext_TekHexParser;
+
+/* return size or -1 if error */
+static int handle_ext_tek_hex_record_type(Ext_TekHexParser *parser)
+{
+    Ext_Tek_HexLine *line = &(parser->line);
+    switch (line->record_type) {
+    case TEK_DATA_RECORD:
+        parser->current_address = line->address;
+        /* verify this is a contiguous block of memory */
+        if (parser->current_address != parser->next_address_to_write) {
+            if (parser->current_rom_index != 0) {
+                rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                      parser->current_rom_index,
+                                      parser->rom_start_address, parser->as);
+            }
+            parser->rom_start_address = parser->current_address;
+            parser->current_rom_index = 0;
+        }
+
+        /* copy from line buffer to output bin_buf */
+        /* sizeof(line->data) dynamically calculates the size of data field */
+        memcpy(parser->bin_buf + parser->current_rom_index, line->data,
+               sizeof(line->data));
+        parser->current_rom_index += sizeof(line->data);
+        parser->total_size += sizeof(line->data);
+        /* As address is absolute, we dont need to calculate next address */
+        break;
+
+    case TEK_TERMINATION_RECORD:
+        if (parser->current_rom_index != 0) {
+            rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                  parser->current_rom_index,
+                                  parser->rom_start_address, parser->as);
+        }
+        parser->complete = true;
+        return parser->total_size;
+
+    default:
+        return -1;
+    }
+
+    return parser->total_size;
+}
+
+/* return size or -1 if error */
+static int parse_ext_tek_hex_blob(const char *filename,
+                                  hwaddr *addr,
+                                  uint8_t *ext_tek_blob,
+                                  size_t ext_tek_blob_size,
+                                  AddressSpace *as)
+{
+    /* avoid re-enter and check whether record begin with '%' */
+        bool in_process = false;
+        uint8_t *end = ext_tek_blob + ext_tek_blob_size;
+        uint8_t our_checksum = 0;
+        uint32_t record_index = 0;
+
+        Ext_TekHexParser parser = {
+        .filename = filename,
+        .bin_buf = g_malloc(ext_tek_blob_size),
+        .start_addr = addr,
+        .as = as,
+        .complete = false
+        };
+
+        rom_transaction_begin();
+
+        for (; ext_tek_blob < end && !parser.complete; ++ext_tek_blob) {
+            switch (*ext_tek_blob) {
+            case '\r':
+            case '\n':
+                if (!in_process) {
+                    break;
+                }
+
+                in_process = false;
+                /*
+                 * record_length byte stores the number of hex characters
+                 * (nibbles) in the record and compare it directly to
+                 * the nibble index(record index).
+                 */
+                if ((parser.line.record_length != record_index) ||
+                    (our_checksum != 0)) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+
+                if (handle_ext_tek_hex_record_type(&parser) == -1) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+
+            /* start of a new record. */
+            case '%':
+                memset(&parser.line, 0, sizeof(Ext_Tek_HexLine));
+                in_process = true;
+                record_index = 0;
+                break;
+
+            /* decoding lines */
+            default:
+                if (!parse_ext_tek_hex_record(&parser.line,
+                                            &our_checksum,
+                                            *ext_tek_blob,
+                                            &record_index, in_process)) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+            }
+        }
+
+    out:
+        g_free(parser.bin_buf);
+        rom_transaction_end(parser.total_size != -1);
+        return parser.total_size;
+}
+
+
+/* return size or -1 if error */
+ssize_t load_targphys_ext_tek_hex_as(const char *filename,
+                                     hwaddr *entry,
+                                     AddressSpace *as)
+{
+    gsize ext_tek_blob_size;
+    gchar *ext_tek_blob;
+    int total_size = 0;
+
+    if (!g_file_get_contents(filename, &ext_tek_blob,
+                             &ext_tek_blob_size, NULL)) {
+        return -1;
+    }
+    total_size = parse_ext_tek_hex_blob(filename, entry,
+                                       (uint8_t *)ext_tek_blob,
+                                       ext_tek_blob_size, as);
+
+    g_free(ext_tek_blob);
+    return total_size;
+}
include/hw/core/loader.h +15 -0
index d9431e8a8d12e3b676160f70ed141f1064143ea3..c0d29b1d4456bb92984749e56f1109f8421cee51 100644
@@ -57,6 +57,21 @@ ssize_t load_image_targphys_as(const char *filename,
 ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
                              AddressSpace *as);
 
+/**
+ * load_targphys_ext_tek_as:
+ * @filename: Path to the .tek file
+ * @entry: Store the entry point given by the .tek file
+ * @as: The AddressSpace to load the .tek file to. The value of
+ *      address_space_memory is used if nothing is supplied here.
+ *
+ * Load a fixed .tek file into memory.
+ *
+ * Returns the size of the loaded .tek file on success, -1 otherwise.
+ */
+ssize_t load_targphys_ext_tek_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.
Signature · 1 line
2.54.0.windows.1

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|

A message that is not in the archive (sent off-list or not yet received): CAEAkEp4N1uFczHexCAbABzYf5O-Th-o5dE-KasU1RXM_k7WKKQ@mail.gmail.com

#3 lore raw

Re-adding qemu-devel - please don't drop the mailing list when replying to review comments.

Quoted 7 lines · Nagendran Jairam
Quoted 5 lines · On Fri, Nov 20, 2026, 14:38 Daniel P. Berrangé <[email protected]> wrote:

On Sat, Nov 14, 2026 at 04:58:50PM +0530, Nagendran K wrote:

Please provide a commit message to explain the purpose of this addition.

Quoted 7 lines

I am sorry, I forgot to mention that this function is being called
in generic-loader.c
The purpose is to load .tek files (binaries commonly used in Texas
instruments microcontrollers) directly without worrying about raw binary or hex conversion
Hex and tek files have major differences and it reduces manual
effort for a user to convert .tek to .hex

It is clearly not being called in generic-loader.c since this patch makes no changes to that file.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
---
 hw/core/loader.c         | 231 +++++++++++++++++++++++++++++++++++++++
 include/hw/core/loader.h |  15 +++
 2 files changed, 246 insertions(+)
Quoted 306 lines
Quoted 4 lines

....this is adding a new API to common code and then not calling it from anywhere in QEMU. This is pointless and so is not likely to be accepted in isolation without a proposed usage accompanying it.

This is function has a call in generic-loader.c to load .tek files

       Example Usecase: -device loader, file=my_test.tek, addr=0x00000000
Quoted 298 lines
Quoted 5 lines
hw/core/loader.c +0 -0

index

5cbfba0a86d24569923c863d6ecda125ce43bceb..33ccb51d56dda1035e815479e20c2a6c793843d9 100644

hw/core/loader.c ssize_t load_targphys_hex_as(const char

Quoted 3 lines
hw/core/loader.c +0 -0
@@ -1940,3 +1940,234 @@ ssize_t load_targphys_hex_as(const char

*filename, hwaddr *entry,

Quoted 18 lines
hw/core/loader.c +15 -0
     g_free(hex_blob);
     return total_size;
 }
+
+typedef enum Ext_Tek_HexRecord Ext_Tek_HexRecord;
+enum Ext_Tek_HexRecord {
+    TEK_DATA_RECORD = 0,
+    TEK_TERMINATION_RECORD
+};
+
+#define DATA_FIELD_MAX_LEN 0xff
+
+typedef struct {
+    uint8_t record_length;
+    uint8_t record_type;
+    uint8_t checksum;
+    uint32_t address;
+    uint8_t data[DATA_FIELD_MAX_LEN];

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry, · line 1993

Quoted 10 lines
hw/core/loader.c +10 -0
+} Ext_Tek_HexLine;
+
+/* return 0 or -1 if error */
+static bool parse_ext_tek_hex_record(Ext_Tek_HexLine *line,
+                                     uint8_t *our_checksum,
+                                     const uint8_t c,
+                                     uint32_t *index,
+                                     const bool in_process)
+{
+    /*
+---------+---------------+-----------+---------------+-------------+
> +     * | record  |    record     |           |               |
   |
> +     * | length  |    type       | checksum  |   address     |   data
    |
> +     *
+---------+---------------+-----------+---------------+-------------+
> +     *     ^             ^            ^              ^            ^
> +     * |1 byte   |    1 bytes    |1 byte     | 1 to N byte   | 0-255
bytes |
> +     */
> +    uint8_t value = 0, address_len = 0;
> +    uint32_t idx = *index;
> +    /* ignore space */
> +    if (g_ascii_isspace(c)) {
> +        return true;
> +    }
> +    if (!g_ascii_isxdigit(c) || !in_process) {
> +        return false;
> +    }
> +    /* still idx is computed nibble wise */
> +    value = g_ascii_xdigit_value(c);
> +    value = (idx & 0x1) ? (value & 0xf) : (value << 4);
> +    if (idx < 2) {
> +        line->record_length |= value;
> +    } else if (idx == 2) {
> +        line->record_type |= value;
> +    } else if (2 < idx && idx <= 4) {
> +        line->checksum |= value;
> +    }
> +    /*
> +     * Extracting address length from record to fill that many no of

address

Quoted 43 lines
hw/core/loader.c +43 -0
+     * characters in the address field of the line structure.
+     */
+    else if (idx == 5) {
+        address_len |= value;
+    } else if (6 <= idx &&
+                   idx <= (6 + address_len)) {
+        line->address <<= address_len;
+        line->address += g_ascii_xdigit_value(c);
+    }
+    /*
+     * Checking whether index above address last char index
+     * and reaches till record length. Right shifting one divides the
+     * nibble wise index by 2 to get the byte wise index to
+     * fill the data field of the line structure
+     */
+    else if ((6 + address_len) < idx &&
+              idx <= line->record_length) {
+        line->data[(idx - (6 + address_len)) >> 1] |= value;
+    } else {
+        return false;
+    }
+    *our_checksum += value;
+    ++(*index);
+    return true;
+}
+
+typedef struct {
+    const char *filename;
+    Ext_Tek_HexLine line;
+    uint8_t *bin_buf;
+    hwaddr *start_addr;
+    int total_size;
+    uint32_t next_address_to_write;
+    uint32_t current_address;
+    uint32_t current_rom_index;
+    uint32_t rom_start_address;
+    AddressSpace *as;
+    bool complete;
+} Ext_TekHexParser;
+
+/* return size or -1 if error */
+static int handle_ext_tek_hex_record_type(Ext_TekHexParser *parser)
+{

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 10 lines
hw/core/loader.c +10 -0
+    Ext_Tek_HexLine *line = &(parser->line);
+    switch (line->record_type) {
+    case TEK_DATA_RECORD:
+        parser->current_address = line->address;
+        /* verify this is a contiguous block of memory */
+        if (parser->current_address != parser->next_address_to_write) {
+            if (parser->current_rom_index != 0) {
+                rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                      parser->current_rom_index,
+                                      parser->rom_start_address,

parser->as);

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 7 lines
hw/core/loader.c +7 -0
+            }
+            parser->rom_start_address = parser->current_address;
+            parser->current_rom_index = 0;
+        }
+
+        /* copy from line buffer to output bin_buf */
+        /* sizeof(line->data) dynamically calculates the size of data

field */

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 5 lines
hw/core/loader.c +5 -0
+        memcpy(parser->bin_buf + parser->current_rom_index, line->data,
+               sizeof(line->data));
+        parser->current_rom_index += sizeof(line->data);
+        parser->total_size += sizeof(line->data);
+        /* As address is absolute, we dont need to calculate next

address */

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 7 lines
hw/core/loader.c +7 -0
+        break;
+
+    case TEK_TERMINATION_RECORD:
+        if (parser->current_rom_index != 0) {
+            rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                  parser->current_rom_index,
+                                  parser->rom_start_address,

parser->as);

Quoted 35 lines
hw/core/loader.c +35 -0
+        }
+        parser->complete = true;
+        return parser->total_size;
+
+    default:
+        return -1;
+    }
+
+    return parser->total_size;
+}
+
+/* return size or -1 if error */
+static int parse_ext_tek_hex_blob(const char *filename,
+                                  hwaddr *addr,
+                                  uint8_t *ext_tek_blob,
+                                  size_t ext_tek_blob_size,
+                                  AddressSpace *as)
+{
+    /* avoid re-enter and check whether record begin with '%' */
+        bool in_process = false;
+        uint8_t *end = ext_tek_blob + ext_tek_blob_size;
+        uint8_t our_checksum = 0;
+        uint32_t record_index = 0;
+
+        Ext_TekHexParser parser = {
+        .filename = filename,
+        .bin_buf = g_malloc(ext_tek_blob_size),
+        .start_addr = addr,
+        .as = as,
+        .complete = false
+        };
+
+        rom_transaction_begin();
+
+        for (; ext_tek_blob < end && !parser.complete; ++ext_tek_blob) {

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 10 lines
hw/core/loader.c +10 -0
+            switch (*ext_tek_blob) {
+            case '\r':
+            case '\n':
+                if (!in_process) {
+                    break;
+                }
+
+                in_process = false;
+                /*
+                 * record_length byte stores the number of hex

characters

Quoted 18 lines
hw/core/loader.c +18 -0
+                 * (nibbles) in the record and compare it directly to
+                 * the nibble index(record index).
+                 */
+                if ((parser.line.record_length != record_index) ||
+                    (our_checksum != 0)) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+
+                if (handle_ext_tek_hex_record_type(&parser) == -1) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+
+            /* start of a new record. */
+            case '%':
+                memset(&parser.line, 0, sizeof(Ext_Tek_HexLine));

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 10 lines
hw/core/loader.c +10 -0
+                in_process = true;
+                record_index = 0;
+                break;
+
+            /* decoding lines */
+            default:
+                if (!parse_ext_tek_hex_record(&parser.line,
+                                            &our_checksum,
+                                            *ext_tek_blob,
+                                            &record_index, in_process))

{

Quoted 35 lines
hw/core/loader.c +34 -0
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+            }
+        }
+
+    out:
+        g_free(parser.bin_buf);
+        rom_transaction_end(parser.total_size != -1);
+        return parser.total_size;
+}
+
+
+/* return size or -1 if error */
+ssize_t load_targphys_ext_tek_hex_as(const char *filename,
+                                     hwaddr *entry,
+                                     AddressSpace *as)
+{
+    gsize ext_tek_blob_size;
+    gchar *ext_tek_blob;
+    int total_size = 0;
+
+    if (!g_file_get_contents(filename, &ext_tek_blob,
+                             &ext_tek_blob_size, NULL)) {
+        return -1;
+    }
+    total_size = parse_ext_tek_hex_blob(filename, entry,
+                                       (uint8_t *)ext_tek_blob,
+                                       ext_tek_blob_size, as);
+
+    g_free(ext_tek_blob);
+    return total_size;
+}
include/hw/core/loader.h +0 -0
Quoted 1 line

index

d9431e8a8d12e3b676160f70ed141f1064143ea3..c0d29b1d4456bb92984749e56f1109f8421cee51 100644

Quoted 13 lines
include/hw/core/loader.h +7 -0
@@ -57,6 +57,21 @@ ssize_t load_image_targphys_as(const char *filename,
 ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
                              AddressSpace *as);
 
+/**
+ * load_targphys_ext_tek_as:
+ * @filename: Path to the .tek file
+ * @entry: Store the entry point given by the .tek file
+ * @as: The AddressSpace to load the .tek file to. The value of
+ *      address_space_memory is used if nothing is supplied here.
+ *

include/hw/core/loader.h ssize_t load_image_targphys_as(const char *filename, · line 76

Quoted 10 lines
include/hw/core/loader.h +8 -0
+ * Load a fixed .tek file into memory.
+ *
+ * Returns the size of the loaded .tek file on success, -1 otherwise.
+ */
+ssize_t load_targphys_ext_tek_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

Quoted 4 lines
include/hw/core/loader.h +0 -1
  * an AddressSpace.
--

2.54.0.windows.1

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#4 lore raw
Quoted 20 lines · On Fri, Nov 20, 2026, 15:01 Daniel P. Berrangé <[email protected]> wrote:

Re-adding qemu-devel - please don't drop the mailing list when replying to review comments.

Quoted 1 line · Nagendran Jairam

On Fri, Nov 20, 2026, 14:38 Daniel P. Berrangé <[email protected]>

Quoted 6 lines · wrote:
Quoted 6 lines · Nagendran K

Please provide a commit message to explain the purpose of this addition.

Quoted 7 lines

I am sorry, I forgot to mention that this function is being called
in generic-loader.c
The purpose is to load .tek files (binaries commonly used in Texas
instruments microcontrollers) directly without worrying about raw binary or hex conversion
Hex and tek files have major differences and it reduces manual
effort for a user to convert .tek to .hex

Quoted 6 lines

It is clearly not being called in generic-loader.c since this patch makes no changes to that file.

Alistair mailed me with the same issue too...Aactually my whole
implementation is not in master..Its updated in my local not in upstream

I will update my patch in upstream master..Thanks
Nagendran

Quoted 339 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
---
 hw/core/loader.c         | 231

+++++++++++++++++++++++++++++++++++++++

Quoted 17 lines
Quoted 7 lines
Quoted 2 lines
 include/hw/core/loader.h |  15 +++
 2 files changed, 246 insertions(+)

....this is adding a new API to common code and then not calling it from anywhere in QEMU. This is pointless and so is not likely to be accepted in isolation without a proposed usage accompanying it.

This is function has a call in generic-loader.c to load .tek files

       Example Usecase: -device loader, file=my_test.tek, addr=0x00000000
Quoted 6 lines
Quoted 5 lines
hw/core/loader.c +0 -0

index

5cbfba0a86d24569923c863d6ecda125ce43bceb..33ccb51d56dda1035e815479e20c2a6c793843d9

Quoted 34 lines
Quoted 34 lines

100644

hw/core/loader.c ssize_t load_targphys_hex_as(const char

Quoted 3 lines
hw/core/loader.c +0 -0
@@ -1940,3 +1940,234 @@ ssize_t load_targphys_hex_as(const char

*filename, hwaddr *entry,

Quoted 18 lines
hw/core/loader.c +15 -0
     g_free(hex_blob);
     return total_size;
 }
+
+typedef enum Ext_Tek_HexRecord Ext_Tek_HexRecord;
+enum Ext_Tek_HexRecord {
+    TEK_DATA_RECORD = 0,
+    TEK_TERMINATION_RECORD
+};
+
+#define DATA_FIELD_MAX_LEN 0xff
+
+typedef struct {
+    uint8_t record_length;
+    uint8_t record_type;
+    uint8_t checksum;
+    uint32_t address;
+    uint8_t data[DATA_FIELD_MAX_LEN];

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry, · line 1993

Quoted 10 lines
hw/core/loader.c +10 -0
+} Ext_Tek_HexLine;
+
+/* return 0 or -1 if error */
+static bool parse_ext_tek_hex_record(Ext_Tek_HexLine *line,
+                                     uint8_t *our_checksum,
+                                     const uint8_t c,
+                                     uint32_t *index,
+                                     const bool in_process)
+{
+    /*

+---------+---------------+-----------+---------------+-------------+

Quoted 3 lines
Quoted 3 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char · line 1968

Quoted 1 line
hw/core/loader.c +1 -0
+     * | record  |    record     |           |               |

|

Quoted 1 line
hw/core/loader.c +1 -0
+     * | length  |    type       | checksum  |   address     |

data

Quoted 29 lines
Quoted 29 lines
    |

hw/core/loader.c ssize_t load_targphys_hex_as(const char · line 1970

Quoted 1 line
hw/core/loader.c +1 -0
+     *
+---------+---------------+-----------+---------------+-------------+
> +     *     ^             ^            ^              ^            ^
> +     * |1 byte   |    1 bytes    |1 byte     | 1 to N byte   | 0-255
bytes |
> +     */
> +    uint8_t value = 0, address_len = 0;
> +    uint32_t idx = *index;
> +    /* ignore space */
> +    if (g_ascii_isspace(c)) {
> +        return true;
> +    }
> +    if (!g_ascii_isxdigit(c) || !in_process) {
> +        return false;
> +    }
> +    /* still idx is computed nibble wise */
> +    value = g_ascii_xdigit_value(c);
> +    value = (idx & 0x1) ? (value & 0xf) : (value << 4);
> +    if (idx < 2) {
> +        line->record_length |= value;
> +    } else if (idx == 2) {
> +        line->record_type |= value;
> +    } else if (2 < idx && idx <= 4) {
> +        line->checksum |= value;
> +    }
> +    /*
> +     * Extracting address length from record to fill that many no of

address

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
hw/core/loader.c +4 -0
+     * characters in the address field of the line structure.
+     */
+    else if (idx == 5) {
+        address_len |= value;
Quoted 8 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 8 lines
hw/core/loader.c +8 -0
+    } else if (6 <= idx &&
+                   idx <= (6 + address_len)) {
+        line->address <<= address_len;
+        line->address += g_ascii_xdigit_value(c);
+    }
+    /*
+     * Checking whether index above address last char index
+     * and reaches till record length. Right shifting one divides

the

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
hw/core/loader.c +29 -0
+     * nibble wise index by 2 to get the byte wise index to
+     * fill the data field of the line structure
+     */
+    else if ((6 + address_len) < idx &&
+              idx <= line->record_length) {
+        line->data[(idx - (6 + address_len)) >> 1] |= value;
+    } else {
+        return false;
+    }
+    *our_checksum += value;
+    ++(*index);
+    return true;
+}
+
+typedef struct {
+    const char *filename;
+    Ext_Tek_HexLine line;
+    uint8_t *bin_buf;
+    hwaddr *start_addr;
+    int total_size;
+    uint32_t next_address_to_write;
+    uint32_t current_address;
+    uint32_t current_rom_index;
+    uint32_t rom_start_address;
+    AddressSpace *as;
+    bool complete;
+} Ext_TekHexParser;
+
+/* return size or -1 if error */
Quoted 8 lines
Quoted 8 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 8 lines
hw/core/loader.c +8 -0
+static int handle_ext_tek_hex_record_type(Ext_TekHexParser *parser)
+{
+    Ext_Tek_HexLine *line = &(parser->line);
+    switch (line->record_type) {
+    case TEK_DATA_RECORD:
+        parser->current_address = line->address;
+        /* verify this is a contiguous block of memory */
+        if (parser->current_address !=

parser->next_address_to_write) {

Quoted 2 lines
Quoted 2 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char · line 2021

Quoted 2 lines
hw/core/loader.c +2 -0
+            if (parser->current_rom_index != 0) {
+                rom_add_blob_fixed_as(parser->filename,

parser->bin_buf,

Quoted 10 lines
Quoted 10 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char · line 2023

Quoted 2 lines
hw/core/loader.c +2 -0
+                                      parser->current_rom_index,
+                                      parser->rom_start_address,

parser->as);

Quoted 7 lines
hw/core/loader.c +7 -0
+            }
+            parser->rom_start_address = parser->current_address;
+            parser->current_rom_index = 0;
+        }
+
+        /* copy from line buffer to output bin_buf */
+        /* sizeof(line->data) dynamically calculates the size of

data

Quoted 2 lines
Quoted 2 lines

field */

Quoted 1 line
hw/core/loader.c +1 -0
+        memcpy(parser->bin_buf + parser->current_rom_index,

line->data,

Quoted 40 lines
Quoted 40 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 4 lines
hw/core/loader.c +4 -0
+               sizeof(line->data));
+        parser->current_rom_index += sizeof(line->data);
+        parser->total_size += sizeof(line->data);
+        /* As address is absolute, we dont need to calculate next

address */

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 7 lines
hw/core/loader.c +7 -0
+        break;
+
+    case TEK_TERMINATION_RECORD:
+        if (parser->current_rom_index != 0) {
+            rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
+                                  parser->current_rom_index,
+                                  parser->rom_start_address,

parser->as);

Quoted 27 lines
hw/core/loader.c +27 -0
+        }
+        parser->complete = true;
+        return parser->total_size;
+
+    default:
+        return -1;
+    }
+
+    return parser->total_size;
+}
+
+/* return size or -1 if error */
+static int parse_ext_tek_hex_blob(const char *filename,
+                                  hwaddr *addr,
+                                  uint8_t *ext_tek_blob,
+                                  size_t ext_tek_blob_size,
+                                  AddressSpace *as)
+{
+    /* avoid re-enter and check whether record begin with '%' */
+        bool in_process = false;
+        uint8_t *end = ext_tek_blob + ext_tek_blob_size;
+        uint8_t our_checksum = 0;
+        uint32_t record_index = 0;
+
+        Ext_TekHexParser parser = {
+        .filename = filename,
+        .bin_buf = g_malloc(ext_tek_blob_size),
Quoted 8 lines
Quoted 8 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 8 lines
hw/core/loader.c +8 -0
+        .start_addr = addr,
+        .as = as,
+        .complete = false
+        };
+
+        rom_transaction_begin();
+
+        for (; ext_tek_blob < end && !parser.complete;

++ext_tek_blob) {

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
hw/core/loader.c +10 -0
+            switch (*ext_tek_blob) {
+            case '\r':
+            case '\n':
+                if (!in_process) {
+                    break;
+                }
+
+                in_process = false;
+                /*
+                 * record_length byte stores the number of hex
Quoted 2 lines
Quoted 2 lines

characters

Quoted 1 line
hw/core/loader.c +1 -0
+                 * (nibbles) in the record and compare it directly

to

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
hw/core/loader.c +19 -0
+                 * the nibble index(record index).
+                 */
+                if ((parser.line.record_length != record_index) ||
+                    (our_checksum != 0)) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+
+                if (handle_ext_tek_hex_record_type(&parser) == -1) {
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+
+            /* start of a new record. */
+            case '%':
+                memset(&parser.line, 0, sizeof(Ext_Tek_HexLine));
+                in_process = true;
+                record_index = 0;
Quoted 8 lines
Quoted 8 lines

hw/core/loader.c ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,

Quoted 8 lines
hw/core/loader.c +8 -0
+                break;
+
+            /* decoding lines */
+            default:
+                if (!parse_ext_tek_hex_record(&parser.line,
+                                            &our_checksum,
+                                            *ext_tek_blob,
+                                            &record_index,

in_process))

Quoted 30 lines
Quoted 30 lines

{

Quoted 29 lines
hw/core/loader.c +29 -0
+                    parser.total_size = -1;
+                    goto out;
+                }
+                break;
+            }
+        }
+
+    out:
+        g_free(parser.bin_buf);
+        rom_transaction_end(parser.total_size != -1);
+        return parser.total_size;
+}
+
+
+/* return size or -1 if error */
+ssize_t load_targphys_ext_tek_hex_as(const char *filename,
+                                     hwaddr *entry,
+                                     AddressSpace *as)
+{
+    gsize ext_tek_blob_size;
+    gchar *ext_tek_blob;
+    int total_size = 0;
+
+    if (!g_file_get_contents(filename, &ext_tek_blob,
+                             &ext_tek_blob_size, NULL)) {
+        return -1;
+    }
+    total_size = parse_ext_tek_hex_blob(filename, entry,
+                                       (uint8_t *)ext_tek_blob,
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
hw/core/loader.c +5 -0
+                                       ext_tek_blob_size, as);
+
+    g_free(ext_tek_blob);
+    return total_size;
+}
include/hw/core/loader.h +0 -0

index

d9431e8a8d12e3b676160f70ed141f1064143ea3..c0d29b1d4456bb92984749e56f1109f8421cee51

Quoted 4 lines
Quoted 4 lines

100644

include/hw/core/loader.h ssize_t load_image_targphys_as(const char

Quoted 3 lines
include/hw/core/loader.h +0 -0
@@ -57,6 +57,21 @@ ssize_t load_image_targphys_as(const char

*filename,

Quoted 13 lines
Quoted 13 lines

include/hw/core/loader.h ssize_t load_image_targphys_as(const char *filename,

Quoted 13 lines
include/hw/core/loader.h +10 -0
 ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
                              AddressSpace *as);
 
+/**
+ * load_targphys_ext_tek_as:
+ * @filename: Path to the .tek file
+ * @entry: Store the entry point given by the .tek file
+ * @as: The AddressSpace to load the .tek file to. The value of
+ *      address_space_memory is used if nothing is supplied here.
+ *
+ * Load a fixed .tek file into memory.
+ *
+ * Returns the size of the loaded .tek file on success, -1

otherwise.

Quoted 13 lines
Quoted 13 lines

include/hw/core/loader.h ssize_t load_image_targphys_as(const char *filename,

Quoted 7 lines
include/hw/core/loader.h +5 -0
+ */
+ssize_t load_targphys_ext_tek_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

Quoted 4 lines
include/hw/core/loader.h +0 -1
  * an AddressSpace.
--

2.54.0.windows.1

Quoted 4 lines
Quoted 4 lines

With regards,
Daniel
--
|: https://berrange.com ~~ https://hachyderm.io/@berrange

:|

Quoted 1 line
Quoted 1 line

|: https://libvirt.org ~~ https://entangle-photo.org

:|

Quoted 1 line
Quoted 1 line

|: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com

:|

Quoted 2 lines
Quoted 2 lines

With regards,
Daniel
--

|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|