Skip to content

Commit

Permalink
loader: add intel hex format
Browse files Browse the repository at this point in the history
  • Loading branch information
RubBra authored Sep 9, 2024
1 parent e42a6dd commit b27f4b2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/vcml/debugging/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum image_type {
IMAGE_ELF,
IMAGE_BIN,
IMAGE_SREC,
IMAGE_IHEX,
IMAGE_UIMAGE,
};

Expand All @@ -46,6 +47,7 @@ class loader
bool cmd_load_bin(const vector<string>& args, ostream& os);
bool cmd_load_elf(const vector<string>& args, ostream& os);
bool cmd_load_srec(const vector<string>& args, ostream& os);
bool cmd_load_ihex(const vector<string>& args, ostream& os);
bool cmd_load_uimage(const vector<string>& args, ostream& os);

static unordered_map<string, loader*> s_loaders;
Expand All @@ -54,6 +56,7 @@ class loader
virtual void load_bin(const string& filename, u64 offset);
virtual void load_elf(const string& filename, u64 offset);
virtual void load_srec(const string& filename, u64 offset);
virtual void load_ihex(const string& filename, u64 offset);
virtual void load_uimage(const string& filename, u64 offset);

typedef mwr::elf::segment elf_segment;
Expand Down
36 changes: 36 additions & 0 deletions src/vcml/debugging/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const char* image_type_to_str(image_type type) {
return "bin";
case IMAGE_SREC:
return "srec";
case IMAGE_IHEX:
return "ihex";
case IMAGE_UIMAGE:
return "uimage";
default:
Expand All @@ -35,6 +37,8 @@ image_type detect_image_type(const string& filename) {
return IMAGE_ELF;
if (ends_with(filename, ".srec"))
return IMAGE_SREC;
if (ends_with(filename, ".hex") || ends_with(filename, ".ihex"))
return IMAGE_IHEX;
if (ends_with(filename, ".bin"))
return IMAGE_BIN;

Expand Down Expand Up @@ -129,6 +133,17 @@ bool loader::cmd_load_srec(const vector<string>& args, ostream& os) {
return true;
}

bool loader::cmd_load_ihex(const vector<string>& args, ostream& os) {
const string& image = args[0];
u64 offset = 0ull;

if (args.size() > 1)
offset = strtoull(args[1].c_str(), NULL, 0);

load_image(image, offset, IMAGE_IHEX);
return true;
}

bool loader::cmd_load_uimage(const vector<string>& args, ostream& os) {
const string& image = args[0];
u64 offset = 0ull;
Expand Down Expand Up @@ -203,6 +218,20 @@ void loader::load_srec(const string& filename, u64 offset) {
}
}

void loader::load_ihex(const string& filename, u64 offset) {
mwr::ihex reader(filename);

m_log.debug("loading ihex file '%s' (%zu records) to offset 0x%llx",
filename.c_str(), reader.records().size(), offset);

for (auto rec : reader.records()) {
if (u8* image = allocate_image(rec.data.size(), rec.addr + offset))
memcpy(image, rec.data.data(), rec.data.size());
else
copy_image(rec.data.data(), rec.data.size(), rec.addr + offset);
}
}

void loader::load_uimage(const string& filename, u64 offset) {
mwr::uimage reader(filename);

Expand Down Expand Up @@ -253,6 +282,10 @@ loader::loader(module& mod, bool reg_cmds): m_owner(mod), m_log(mod.log) {
m_owner.register_command("load_srec", 1, this, &loader::cmd_load_srec,
"load_srec <image> [offset] to load the SREC "
"file <image> to memory with an optional offset");
m_owner.register_command("load_ihex", 1, this, &loader::cmd_load_ihex,
"load_ihex <image> [offset] to load the "
"Intel HEX file <image> to memory with an "
"optional offset");
m_owner.register_command("load_uimage", 1, this, &loader::cmd_load_uimage,
"load_uimage <image> [offset] to load the uImage "
"file <image> to memory with an optional offset");
Expand Down Expand Up @@ -284,6 +317,9 @@ void loader::load_image(const image_info& image) {
case IMAGE_SREC:
load_srec(image.filename, image.offset);
break;
case IMAGE_IHEX:
load_ihex(image.filename, image.offset);
break;
case IMAGE_UIMAGE:
load_uimage(image.filename, image.offset);
break;
Expand Down

0 comments on commit b27f4b2

Please sign in to comment.