@@ -724,6 +724,23 @@ impl CrosEc {
724724 }
725725 }
726726
727+ // Determine recommended flash parameters
728+ let info = EcRequestFlashInfo { } . send_command ( self ) ?;
729+
730+ // Check that our hardcoded offsets are valid for the available flash
731+ if FLASH_RO_SIZE + FLASH_RW_SIZE > info. flash_size {
732+ return Err ( EcError :: DeviceError ( format ! (
733+ "RO+RW larger than flash 0x{:X}" ,
734+ { info. flash_size }
735+ ) ) ) ;
736+ }
737+ if FLASH_RW_BASE + FLASH_RW_SIZE > info. flash_size {
738+ return Err ( EcError :: DeviceError ( format ! (
739+ "RW overruns end of flash 0x{:X}" ,
740+ { info. flash_size }
741+ ) ) ) ;
742+ }
743+
727744 println ! ( "Unlocking flash" ) ;
728745 self . flash_notify ( MecFlashNotify :: AccessSpi ) ?;
729746 self . flash_notify ( MecFlashNotify :: FirmwareStart ) ?;
@@ -736,11 +753,22 @@ impl CrosEc {
736753 if ft == EcFlashType :: Full || ft == EcFlashType :: Rw {
737754 let rw_data = & data[ FLASH_RW_BASE as usize ..( FLASH_RW_BASE + FLASH_RW_SIZE ) as usize ] ;
738755
739- println ! ( "Erasing RW region{}" , if dry_run { " (DRY RUN)" } else { "" } ) ;
740- self . erase_ec_flash ( FLASH_BASE + FLASH_RW_BASE , FLASH_RW_SIZE , dry_run) ?;
756+ println ! (
757+ "Erasing RW region{}" ,
758+ if dry_run { " (DRY RUN)" } else { "" }
759+ ) ;
760+ self . erase_ec_flash (
761+ FLASH_BASE + FLASH_RW_BASE ,
762+ FLASH_RW_SIZE ,
763+ dry_run,
764+ info. erase_block_size ,
765+ ) ?;
741766 println ! ( " Done" ) ;
742767
743- println ! ( "Writing RW region{}" , if dry_run { " (DRY RUN)" } else { "" } ) ;
768+ println ! (
769+ "Writing RW region{}" ,
770+ if dry_run { " (DRY RUN)" } else { "" }
771+ ) ;
744772 self . write_ec_flash ( FLASH_BASE + FLASH_RW_BASE , rw_data, dry_run) ?;
745773 println ! ( " Done" ) ;
746774
@@ -758,7 +786,12 @@ impl CrosEc {
758786 let ro_data = & data[ FLASH_RO_BASE as usize ..( FLASH_RO_BASE + FLASH_RO_SIZE ) as usize ] ;
759787
760788 println ! ( "Erasing RO region" ) ;
761- self . erase_ec_flash ( FLASH_BASE + FLASH_RO_BASE , FLASH_RO_SIZE , dry_run) ?;
789+ self . erase_ec_flash (
790+ FLASH_BASE + FLASH_RO_BASE ,
791+ FLASH_RO_SIZE ,
792+ dry_run,
793+ info. erase_block_size ,
794+ ) ?;
762795 println ! ( " Done" ) ;
763796
764797 println ! ( "Writing RO region" ) ;
@@ -840,10 +873,15 @@ impl CrosEc {
840873 . send_command_extra ( self , data)
841874 }
842875
843- fn erase_ec_flash ( & self , offset : u32 , size : u32 , dry_run : bool ) -> EcResult < ( ) > {
876+ fn erase_ec_flash (
877+ & self ,
878+ offset : u32 ,
879+ size : u32 ,
880+ dry_run : bool ,
881+ chunk_size : u32 ,
882+ ) -> EcResult < ( ) > {
844883 // Erasing a big section takes too long sometimes and the linux kernel driver times out, so
845- // split it up into chunks. One chunk is 1/8 of EC ROM size.
846- let chunk_size = 0x10000 ;
884+ // split it up into chunks.
847885 let mut cur_offset = offset;
848886
849887 while cur_offset < offset + size {
0 commit comments