@@ -4603,7 +4603,9 @@ bool load_guts(picoboot::connection con, iostream_memory_access &file_access) {
46034603 // new scope for progress bar
46044604 {
46054605 progress_bar bar (" Loading into " + memory_names[type] + " : " );
4606- uint32_t batch_size = FLASH_SECTOR_ERASE_SIZE;
4606+ // Use batches of size/100 rounded up to FLASH_SECTOR_ERASE_SIZE
4607+ uint32_t batch_size = std::max (FLASH_SECTOR_ERASE_SIZE,
4608+ (mem_range.len ()/100 + FLASH_SECTOR_ERASE_SIZE - 1 ) & ~(FLASH_SECTOR_ERASE_SIZE - 1 ));
46074609 bool ok = true ;
46084610 vector<uint8_t > file_buf;
46094611 vector<uint8_t > device_buf;
@@ -4612,24 +4614,24 @@ bool load_guts(picoboot::connection con, iostream_memory_access &file_access) {
46124614 if (type == flash) {
46134615 // we have to erase an entire page, so then fill with zeros
46144616 range aligned_range (base & ~(FLASH_SECTOR_ERASE_SIZE - 1 ),
4615- (base & ~( FLASH_SECTOR_ERASE_SIZE - 1 )) + FLASH_SECTOR_ERASE_SIZE);
4617+ (base + this_batch + FLASH_SECTOR_ERASE_SIZE - 1 ) & ~( FLASH_SECTOR_ERASE_SIZE - 1 ) );
46164618 range read_range (base, base + this_batch);
46174619 read_range.intersect (aligned_range);
46184620 file_access.read_into_vector (read_range.from , read_range.to - read_range.from , file_buf, true ); // zero fill to cope with holes
4619- // zero padding up to FLASH_SECTOR_ERASE_SIZE
4621+ // zero padding up to batch_size
46204622 file_buf.insert (file_buf.begin (), read_range.from - aligned_range.from , 0 );
46214623 file_buf.insert (file_buf.end (), aligned_range.to - read_range.to , 0 );
4622- assert (file_buf.size () == FLASH_SECTOR_ERASE_SIZE );
4624+ assert (file_buf.size () == aligned_range. len () );
46234625
46244626 bool skip = false ;
46254627 if (settings.load .update ) {
46264628 vector<uint8_t > read_device_buf;
4627- raw_access.read_into_vector (aligned_range.from , batch_size , read_device_buf);
4629+ raw_access.read_into_vector (aligned_range.from , file_buf. size () , read_device_buf);
46284630 skip = file_buf == read_device_buf;
46294631 }
46304632 if (!skip) {
46314633 con.exit_xip ();
4632- con.flash_erase (aligned_range.from , FLASH_SECTOR_ERASE_SIZE );
4634+ con.flash_erase (aligned_range.from , file_buf. size () );
46334635 raw_access.write_vector (aligned_range.from , file_buf);
46344636 }
46354637 base = read_range.to ; // about to add batch_size
0 commit comments