Skip to contentSkip to conversation

hw/core: properly terminate loading .hex on EOF record

PATCH In review Alex Bennée 4 participants 4 messages Apr 1, 2020 – Apr 2, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/core: properly terminate loading .hex on EOF record

The https://makecode.microbit.org/#editor generates slightly weird .hex files which work fine on a real microbit but causes QEMU to choke. The reason is extraneous data after the EOF record which causes the loader to attempt to write a bigger file than it should to the "rom". According to the HEX file spec an EOF really should be the last thing we process so lets do that.

---
 hw/core/loader.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader.c +4 -1
index eeef6da9a1b..8bbb1797a4c 100644
@@ -1447,6 +1447,7 @@ typedef struct {
     uint32_t current_rom_index;
     uint32_t rom_start_address;
     AddressSpace *as;
+    bool complete;
 } HexParser;
 
 /* return size or -1 if error */
@@ -1484,6 +1485,7 @@ static int handle_record_type(HexParser *parser)
                                   parser->current_rom_index,
                                   parser->rom_start_address, parser->as);
         }
+        parser->complete = true;
         return parser->total_size;
     case EXT_SEG_ADDR_RECORD:
     case EXT_LINEAR_ADDR_RECORD:
@@ -1548,11 +1550,12 @@ static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
         .bin_buf = g_malloc(hex_blob_size),
         .start_addr = addr,
         .as = as,
+        .complete = false
     };
 
     rom_transaction_begin();
 
-    for (; hex_blob < end; ++hex_blob) {
+    for (; hex_blob < end && !parser.complete; ++hex_blob) {
         switch (*hex_blob) {
         case '\r':
         case '\n':
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Alex Bennée

The https://makecode.microbit.org/#editor generates slightly weird .hex files which work fine on a real microbit but causes QEMU to choke. The reason is extraneous data after the EOF record which causes the loader to attempt to write a bigger file than it should to the "rom". According to the HEX file spec an EOF really should be the last thing we process so lets do that.

Quoted 5 lines
---
 hw/core/loader.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Alex Bennée
Quoted 7 lines · Alex Bennée

The https://makecode.microbit.org/#editor generates slightly weird .hex files which work fine on a real microbit but causes QEMU to choke. The reason is extraneous data after the EOF record which causes the loader to attempt to write a bigger file than it should to the "rom". According to the HEX file spec an EOF really should be the last thing we process so lets do that.

Quoted 5 lines
---
 hw/core/loader.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

r~

Reviewed-by on v1 1/1
#3 lore raw
In reply to Alex Bennée
Quoted 38 lines · Alex Bennée

The https://makecode.microbit.org/#editor generates slightly weird .hex files which work fine on a real microbit but causes QEMU to choke. The reason is extraneous data after the EOF record which causes the loader to attempt to write a bigger file than it should to the "rom". According to the HEX file spec an EOF really should be the last thing we process so lets do that.

---
  hw/core/loader.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/core/loader.c +2 -0
index eeef6da9a1b..8bbb1797a4c 100644
@@ -1447,6 +1447,7 @@ typedef struct {
      uint32_t current_rom_index;
      uint32_t rom_start_address;
      AddressSpace *as;
+    bool complete;
  } HexParser;
 
  /* return size or -1 if error */
@@ -1484,6 +1485,7 @@ static int handle_record_type(HexParser *parser)
                                    parser->current_rom_index,
                                    parser->rom_start_address, parser->as);
          }
+        parser->complete = true;
          return parser->total_size;
      case EXT_SEG_ADDR_RECORD:
      case EXT_LINEAR_ADDR_RECORD:
@@ -1548,11 +1550,12 @@ static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
          .bin_buf = g_malloc(hex_blob_size),
          .start_addr = addr,
          .as = as,

hw/core/loader.c static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob, · line 1561

Quoted 10 lines
hw/core/loader.c +2 -1
+        .complete = false
      };
 
      rom_transaction_begin();
 
-    for (; hex_blob < end; ++hex_blob) {
+    for (; hex_blob < end && !parser.complete; ++hex_blob) {
          switch (*hex_blob) {
          case '\r':
          case '\n':