Skip to content

Implement an ELF Loader#12

Merged
engdoreis merged 3 commits into
lowRISC:mainfrom
ziuziakowska:elf-loader
May 28, 2026
Merged

Implement an ELF Loader#12
engdoreis merged 3 commits into
lowRISC:mainfrom
ziuziakowska:elf-loader

Conversation

@ziuziakowska
Copy link
Copy Markdown
Contributor

@ziuziakowska ziuziakowska commented May 27, 2026

This PR implements a simple ELF loader using the ELFIO header-library, exposed through a new bootstrap-elf command.

This loader first erases the sectors corresponding to each segment, and then programs them. For binaries which are quite sparse (the main use-case for this is OpenSBI+payload firmwares) this saves a lot of time by not writing gaps between segments.

With this ELF loader we should also be able to remove the dependency on objcopy'd .bin files in Mocha (1), (2).

Comparison (put the board into Bootstrap mode using ftditool gpio-write --ftdi Digilent 0 0, then 2 0 and then 2 1:

$ time build/ftditool bootstrap ~/mocha/build/sw/opensbi_with_uboot/fw_payload.bin --addr 0x80000000 --ftdi Digilent
Opening 200300BD8376A, channel 0
Loading [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■] 100% 5.0 Mib in 17s, 2.4 Mib/s  

real    0m17.074s
user    0m0.371s
sys     0m4.224s
$ time build/ftditool bootstrap-elf ~/mocha/build/sw/opensbi_with_uboot/fw_payload.elf --ftdi Digilent
Opening 200300BD8376A, channel 0
Erasing [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■] 100% 692.0 kib in , 21.1 Mib/s 
Loading [■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■] 100% 671.3 kib in 2s, 2.6 Mib/s 

real    0m2.736s
user    0m0.052s
sys     0m0.608s

Signed-off-by: Alice Ziuziakowska <a.ziuziakowska@lowrisc.org>
Signed-off-by: Alice Ziuziakowska <a.ziuziakowska@lowrisc.org>
Copy link
Copy Markdown
Collaborator

@engdoreis engdoreis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the nice feature!
I quick review, I'll give a more in depth review later.

Comment thread app/commands.hh Outdated
Comment thread app/commands.hh Outdated
Comment thread app/commands.hh Outdated
@ziuziakowska ziuziakowska force-pushed the elf-loader branch 5 times, most recently from 3370378 to 0aceb00 Compare May 27, 2026 16:31
@ziuziakowska ziuziakowska requested a review from engdoreis May 27, 2026 16:37
Comment thread app/commands.hh Outdated
Comment thread app/commands.hh Outdated
Comment thread app/commands.hh Outdated
Comment thread app/commands.hh
if ((addr % flash::PageSize) != 0) {
auto to_next = flash::PageSize - (addr % flash::PageSize);
auto n = std::min(to_next, data.size());
std::vector<uint8_t> page(data.first(n).begin(), data.first(n).end());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this copy due to the type difference?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the page program commands expect a std::span<uint8_t>, I think because the underlying transfer operations use a RW span and use an enum for the transfer direction type.

Comment thread app/commands.hh Outdated
Signed-off-by: Alice Ziuziakowska <a.ziuziakowska@lowrisc.org>
@engdoreis engdoreis merged commit bc0ac1d into lowRISC:main May 28, 2026
1 check passed
@ziuziakowska ziuziakowska deleted the elf-loader branch May 28, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants