@@ -319,11 +319,14 @@ std::unique_ptr<block> get_last_block(std::vector<uint8_t> &bin, uint32_t storag
319
319
uint32_t next_block_addr = first_block->physical_addr + first_block->next_block_rel ;
320
320
std::unique_ptr<block> new_first_block;
321
321
uint32_t read_size = PICOBIN_MAX_BLOCK_SIZE;
322
+ uint32_t current_bin_start = storage_addr;
322
323
while (true ) {
323
- auto offset = next_block_addr - storage_addr;
324
- if (offset + read_size > bin.size () && more_cb != nullptr ) {
325
- more_cb (bin, offset + read_size);
324
+ if (next_block_addr + read_size > current_bin_start + bin.size () && more_cb != nullptr ) {
325
+ DEBUG_LOG (" Reading into bin %08x+%x\n " , next_block_addr, read_size);
326
+ more_cb (bin, next_block_addr, read_size);
327
+ current_bin_start = next_block_addr;
326
328
}
329
+ auto offset = next_block_addr - current_bin_start;
327
330
std::vector<uint32_t > words = lsb_bytes_to_words (bin.begin () + offset, bin.end ());
328
331
if (words.front () != PICOBIN_BLOCK_MARKER_START) {
329
332
fail (ERROR_UNKNOWN, " Block loop is not valid - no block found at %08x\n " , (int )(next_block_addr));
@@ -362,12 +365,15 @@ std::vector<std::unique_ptr<block>> get_all_blocks(std::vector<uint8_t> &bin, ui
362
365
uint32_t next_block_addr = first_block->physical_addr + first_block->next_block_rel ;
363
366
std::vector<std::unique_ptr<block>> all_blocks;
364
367
uint32_t read_size = PICOBIN_MAX_BLOCK_SIZE;
368
+ uint32_t current_bin_start = storage_addr;
365
369
while (true ) {
366
370
std::unique_ptr<block> new_first_block;
367
- auto offset = next_block_addr - storage_addr;
368
- if (offset + read_size > bin.size () && more_cb != nullptr ) {
369
- more_cb (bin, offset + read_size);
371
+ if (next_block_addr + read_size > current_bin_start + bin.size () && more_cb != nullptr ) {
372
+ DEBUG_LOG (" Reading into bin %08x+%x\n " , next_block_addr, read_size);
373
+ more_cb (bin, next_block_addr, read_size);
374
+ current_bin_start = next_block_addr;
370
375
}
376
+ auto offset = next_block_addr - current_bin_start;
371
377
std::vector<uint32_t > words = lsb_bytes_to_words (bin.begin () + offset, bin.end ());
372
378
words.erase (words.begin ());
373
379
DEBUG_LOG (" Checking block at %x\n " , next_block_addr);
@@ -698,7 +704,7 @@ std::vector<uint8_t> get_lm_hash_data(elf_file *elf, block *new_block, bool clea
698
704
}
699
705
700
706
701
- std::vector<uint8_t > get_lm_hash_data (std::vector<uint8_t > bin, uint32_t storage_addr, uint32_t runtime_addr, block *new_block, bool clear_sram = false ) {
707
+ std::vector<uint8_t > get_lm_hash_data (std::vector<uint8_t > bin, uint32_t storage_addr, uint32_t runtime_addr, block *new_block, get_more_bin_cb more_cb, bool clear_sram = false ) {
702
708
std::vector<uint8_t > to_hash;
703
709
std::shared_ptr<load_map_item> load_map = new_block->get_item <load_map_item>();
704
710
if (load_map == nullptr ) {
@@ -729,6 +735,7 @@ std::vector<uint8_t> get_lm_hash_data(std::vector<uint8_t> bin, uint32_t storage
729
735
} else {
730
736
DEBUG_LOG (" Already has load map, so hashing that\n " );
731
737
// todo hash existing load map
738
+ uint32_t current_bin_start = storage_addr;
732
739
for (const auto &entry : load_map->entries ) {
733
740
if (entry.storage_address == 0 ) {
734
741
std::copy (
@@ -737,7 +744,15 @@ std::vector<uint8_t> get_lm_hash_data(std::vector<uint8_t> bin, uint32_t storage
737
744
std::back_inserter (to_hash));
738
745
DEBUG_LOG (" CLEAR %08x + %08x\n " , (int )entry.runtime_address , (int )entry.size );
739
746
} else {
740
- uint32_t rel_addr = entry.storage_address - storage_addr;
747
+ if (entry.storage_address + entry.size > current_bin_start + bin.size ()) {
748
+ if (more_cb == nullptr ) {
749
+ fail (ERROR_NOT_POSSIBLE, " BIN does not contain data for load_map entry %08x->%08x" , entry.storage_address , entry.storage_address + entry.size );
750
+ }
751
+ DEBUG_LOG (" Reading into bin %08x+%x\n " , entry.storage_address , entry.size );
752
+ more_cb (bin, entry.storage_address , entry.size );
753
+ current_bin_start = entry.storage_address ;
754
+ }
755
+ uint32_t rel_addr = entry.storage_address - current_bin_start;
741
756
std::copy (
742
757
bin.begin () + rel_addr,
743
758
bin.begin () + rel_addr + entry.size ,
@@ -787,10 +802,10 @@ int hash_andor_sign(elf_file *elf, block *new_block, const public_t public_key,
787
802
788
803
789
804
std::vector<uint8_t > hash_andor_sign (std::vector<uint8_t > bin, uint32_t storage_addr, uint32_t runtime_addr, block *new_block, const public_t public_key, const private_t private_key, bool hash_value, bool sign, bool clear_sram) {
790
- std::vector<uint8_t > to_hash = get_lm_hash_data (bin, storage_addr, runtime_addr, new_block, clear_sram);
805
+ std::vector<uint8_t > to_hash = get_lm_hash_data (bin, storage_addr, runtime_addr, new_block, nullptr , clear_sram);
806
+
807
+ hash_andor_sign_block (new_block, public_key, private_key, hash_value, sign, to_hash);
791
808
792
- hash_andor_sign_block (new_block, public_key, private_key, hash_value, sign, bin);
793
-
794
809
auto tmp = new_block->to_words ();
795
810
std::vector<uint8_t > data = words_to_lsb_bytes (tmp.begin (), tmp.end ());
796
811
@@ -800,15 +815,15 @@ std::vector<uint8_t> hash_andor_sign(std::vector<uint8_t> bin, uint32_t storage_
800
815
}
801
816
802
817
803
- void verify_block (std::vector<uint8_t > bin, uint32_t storage_addr, uint32_t runtime_addr, block *block, verified_t &hash_verified, verified_t &sig_verified) {
818
+ void verify_block (std::vector<uint8_t > bin, uint32_t storage_addr, uint32_t runtime_addr, block *block, verified_t &hash_verified, verified_t &sig_verified, get_more_bin_cb more_cb ) {
804
819
std::shared_ptr<load_map_item> load_map = block->get_item <load_map_item>();
805
820
std::shared_ptr<hash_def_item> hash_def = block->get_item <hash_def_item>();
806
821
hash_verified = none;
807
822
sig_verified = none;
808
823
if (load_map == nullptr || hash_def == nullptr ) {
809
824
return ;
810
825
}
811
- std::vector<uint8_t > to_hash = get_lm_hash_data (bin, storage_addr, runtime_addr, block, false );
826
+ std::vector<uint8_t > to_hash = get_lm_hash_data (bin, storage_addr, runtime_addr, block, more_cb, false );
812
827
813
828
// auto it = std::find(block->items.begin(), block->items.end(), hash_def);
814
829
// assert (it != block->items.end());
0 commit comments