@@ -70,7 +70,7 @@ static size_t flash_addr = M_OS_APP_TABLE_BASE;
70
70
static list_t * flash_list = NULL ;
71
71
static list_t * lst = NULL ;
72
72
typedef struct to_flash_rec {
73
- size_t paddr ;
73
+ size_t offset ;
74
74
size_t size ;
75
75
} to_flash_rec_t ;
76
76
@@ -81,14 +81,18 @@ void __not_in_flash_func(flash_block)(uint8_t* buffer, size_t flash_target_offse
81
81
node_t * n = flash_list -> first ;
82
82
while (n ) {
83
83
to_flash_rec_t * rec = (to_flash_rec_t * )n -> data ;
84
- if (flash_target_offset >= rec -> paddr && flash_target_offset < rec -> paddr + FLASH_SECTOR_SIZE ) {
85
- goutf ("WARN: Impossible to use already allocated flash block: [%p]-[%p]\n" , flash_target_offset , flash_target_offset + FLASH_SECTOR_SIZE );
84
+ if (flash_target_offset >= rec -> offset && flash_target_offset < rec -> offset + FLASH_SECTOR_SIZE ) {
85
+ goutf (
86
+ "WARN: Attempt to use already allocated flash block: [%p]-[%p] (rejected)\n" ,
87
+ flash_target_offset ,
88
+ flash_target_offset + FLASH_SECTOR_SIZE
89
+ );
86
90
return ;
87
91
}
88
92
n = n -> next ;
89
93
}
90
94
to_flash_rec_t * rec = (to_flash_rec_t * )pvPortMalloc (sizeof (to_flash_rec_t ));
91
- rec -> paddr = flash_target_offset ;
95
+ rec -> offset = flash_target_offset ;
92
96
rec -> size = FLASH_SECTOR_SIZE ;
93
97
list_push_back (flash_list , rec );
94
98
uint8_t * e = (uint8_t * )(XIP_BASE + flash_target_offset );
@@ -170,21 +174,22 @@ bool __not_in_flash_func(load_firmware_sram)(char* pathname) {
170
174
}
171
175
172
176
bool load_firmware (char * pathname ) {
177
+ if (flash_list ) delete_list (flash_list );
173
178
FILINFO * pfileinfo = pvPortMalloc (sizeof (FILINFO ));
174
179
f_stat (pathname , pfileinfo );
175
180
if ((flash_size - (100 << 10 )) < (pfileinfo -> fsize >> 1 )) { // TODO: free, ...
176
- fgoutf ( get_stdout (), "ERROR: Firmware too large (%dK)! Canceled!\n" , pfileinfo -> fsize >> 11 );
181
+ goutf ( "ERROR: Firmware too large (%dK)! Canceled!\n" , pfileinfo -> fsize >> 11 );
177
182
vPortFree (pfileinfo );
178
183
return false;
179
184
}
180
185
vPortFree (pfileinfo );
181
- fgoutf ( get_stdout (), "Loading firmware: '%s'\n" , pathname );
186
+ goutf ( "Loading firmware: '%s'\n" , pathname );
182
187
return load_firmware_sram (pathname );
183
188
}
184
189
185
190
void vAppTask (void * pv ) {
186
191
int res = ((boota_ptr_t )M_OS_APP_TABLE_BASE [0 ])(pv ); // TODO: 0 - 2nd page, what exactly page used by app?
187
- goutf ("RET_CODE: %d\n" , res );
192
+ // goutf("RET_CODE: %d\n", res);
188
193
vTaskDelete ( NULL );
189
194
// TODO: ?? return res;
190
195
}
@@ -378,6 +383,40 @@ void cleanup_bootb_ctx(cmd_ctx_t* ctx) {
378
383
bootb_ctx_t * bootb_ctx = ctx -> pboot_ctx ;
379
384
if (!bootb_ctx ) return ;
380
385
if (bootb_ctx -> sections ) {
386
+ if (flash_list ) {
387
+ uint32_t min_addr = 0xFFFFFFFF ;
388
+ uint32_t max_addr = 0 ;
389
+ node_t * n = bootb_ctx -> sections -> first ;
390
+ while (n ) {
391
+ sect_entry_t * se = (sect_entry_t * )n -> data ;
392
+ uint32_t prg_addr = se -> prg_addr ;
393
+ if (prg_addr <= 0x20000000 ) {
394
+ if ( min_addr > prg_addr ) min_addr = prg_addr ;
395
+ if ( max_addr < prg_addr ) max_addr = prg_addr ;
396
+ }
397
+ n = n -> next ;
398
+ }
399
+ flash_addr = M_OS_APP_TABLE_BASE ;
400
+ // goutf("flash_addr [%p]\n", flash_addr);
401
+ node_t * fn = flash_list -> first ;
402
+ while (fn ) {
403
+ uint32_t nn = fn -> next ;
404
+ to_flash_rec_t * fse = (to_flash_rec_t * )fn -> data ;
405
+ uint32_t faddr = fse -> offset + XIP_BASE ;
406
+ if (faddr >= min_addr && faddr <= max_addr ) {
407
+ // goutf("Free [%p]\n", faddr);
408
+ list_erase_node (flash_list , fn );
409
+ }
410
+ else {
411
+ faddr += fse -> size ;
412
+ if (flash_addr < faddr ) {
413
+ flash_addr = faddr ;
414
+ // goutf("flash_addr [%p]\n", flash_addr);
415
+ }
416
+ }
417
+ fn = nn ;
418
+ }
419
+ }
381
420
delete_list (bootb_ctx -> sections );
382
421
bootb_ctx -> sections = 0 ;
383
422
}
@@ -520,13 +559,13 @@ static uint8_t* load_sec2mem(load_sec_ctx * c, uint16_t sec_num, bool try_to_use
520
559
size_t sz = new_flash_addr - prev_flash_addr ;
521
560
if (prg_addr && sz ) {
522
561
to_flash_rec_t * o = (to_flash_rec_t * )pvPortMalloc (sizeof (to_flash_rec_t ));
523
- o -> paddr = prg_addr ; // TODO: out of empty flash?
562
+ o -> offset = prg_addr - XIP_BASE ; // TODO: out of empty flash?
524
563
o -> size = sz ;
525
564
char * tmp = get_ctx_var (get_cmd_startup_ctx (), TEMP );
526
565
if (!tmp ) tmp = "" ;
527
566
size_t cdl = strlen (tmp );
528
567
char * flash_me_file = concat (tmp , _flash_me );
529
- FIL * f = (FIL * )malloc (sizeof (FIL ));
568
+ FIL * f = (FIL * )pvPortMalloc (sizeof (FIL ));
530
569
if ( FR_OK != f_open (f , flash_me_file , FA_WRITE | FA_CREATE_ALWAYS ) ) {
531
570
goutf ("Unable to open file '%s'\n" , flash_me_file );
532
571
goto e5 ;
@@ -546,7 +585,7 @@ static uint8_t* load_sec2mem(load_sec_ctx * c, uint16_t sec_num, bool try_to_use
546
585
f_close (f );
547
586
e5 :
548
587
vPortFree (flash_me_file );
549
- free (f );
588
+ vPortFree (f );
550
589
list_push_back (lst , o );
551
590
vPortFree (del_addr );
552
591
del_addr = 0 ;
@@ -609,10 +648,10 @@ bool load_app(cmd_ctx_t* ctx) {
609
648
size_t free_sz = xPortGetFreeHeapSize ();
610
649
if ((free_sz >> 1 ) < f -> obj .objsize ) {
611
650
try_to_use_flash = true;
612
- gouta ("Try to use flash (by size)\n" );
651
+ gouta ("Attempt to use flash (by size)\n" );
613
652
}
614
653
} else {
615
- gouta ("Try to use flash (Alt+Enter)\n" );
654
+ gouta ("Attempt to use flash (Alt+Enter)\n" );
616
655
}
617
656
elf32_header * pehdr = (elf32_header * )pvPortMalloc (sizeof (elf32_header ));
618
657
UINT rb ;
@@ -706,20 +745,19 @@ bool load_app(cmd_ctx_t* ctx) {
706
745
uint32_t max_addr = 0 ;
707
746
while (n ) {
708
747
to_flash_rec_t * tf = (to_flash_rec_t * )n -> data ;
709
- if ( min_addr > tf -> paddr ) min_addr = tf -> paddr ;
710
- if ( max_addr < tf -> paddr + tf -> size ) max_addr = tf -> paddr + tf -> size ;
748
+ if ( min_addr > tf -> offset + XIP_BASE ) min_addr = tf -> offset + XIP_BASE ;
749
+ if ( max_addr < tf -> offset + XIP_BASE + tf -> size ) max_addr = tf -> offset + XIP_BASE + tf -> size ;
711
750
n = n -> next ;
712
751
}
713
752
delete_list (lst );
714
753
lst = 0 ;
715
754
716
- goutf ("Going to flash: [%p]-[%p] %dK (%d pages)\n" , min_addr , max_addr , (max_addr - min_addr ) >> 10 , (max_addr - min_addr ) >> 12 );
717
- // bootb_ctx->
755
+ goutf ("Going to flash: [%p]-[%p] %dK (%d pages)\n" , min_addr , max_addr , 1 + ((max_addr - min_addr ) >> 10 ), 1 + ((max_addr - min_addr ) >> 12 ));
718
756
if (xPortGetFreeHeapSize () < (FLASH_SECTOR_SIZE + 511 )) {
719
757
goutf ("WARN: free_sz: %d; required: %d\n" , xPortGetFreeHeapSize (), (FLASH_SECTOR_SIZE + 511 ));
720
758
goto e8 ;
721
759
}
722
- char * alloc = (char * )pvPortCalloc (1 , FLASH_SECTOR_SIZE + 511 ); // TODO: aliment by ?
760
+ char * alloc = (char * )pvPortCalloc (1 , FLASH_SECTOR_SIZE + 511 );
723
761
char * buffer = (char * )((uint32_t )(alloc + 511 ) & 0xFFFFFE00 ); // align 512
724
762
char * tmp = get_ctx_var (get_cmd_startup_ctx (), TEMP );
725
763
if (!tmp ) tmp = "" ;
@@ -773,7 +811,7 @@ bool load_app(cmd_ctx_t* ctx) {
773
811
goutf ("[%p][%p][%p][%p]\n" , bootb_ctx -> bootb [0 ], bootb_ctx -> bootb [1 ], bootb_ctx -> bootb [2 ], bootb_ctx -> bootb [3 ]);
774
812
#endif
775
813
if (bootb_ctx -> bootb [2 ] == 0 ) {
776
- goutf ("'main' global function is not found in the '%s' elf-file\n" , fn );
814
+ goutf ("'main' global function is not found in (or failed to load from) the '%s' elf-file\n" , fn );
777
815
ctx -> ret_code = -1 ;
778
816
return false;
779
817
}
@@ -944,7 +982,7 @@ void vCmdTask(void *pv) {
944
982
vTaskSetThreadLocalStoragePointer (th , 0 , ctx );
945
983
//run_app(ctx->orig_cmd);
946
984
int res = ((boota_ptr_t )M_OS_APP_TABLE_BASE [0 ])(ctx -> orig_cmd ); // TODO: 0 - 2nd page, what exactly page used by app?
947
- goutf ("RET_CODE: %d\n" , res );
985
+ // goutf("RET_CODE: %d\n", res);
948
986
ctx -> stage = EXECUTED ;
949
987
cleanup_ctx (ctx );
950
988
} else {
@@ -1017,6 +1055,6 @@ int kill(uint32_t task_number) {
1017
1055
return res ;
1018
1056
}
1019
1057
1020
- void reboot_me (void ) {
1058
+ void __not_in_flash_func ( reboot_me ) (void ) {
1021
1059
reboot_is_requested = true;
1022
1060
}
0 commit comments