Skip to content

Commit 9e33bad

Browse files
committed
[ALT]+[Enter] - try to use flasg instead of RAM to launch application (from mc)
1 parent 7c2d2ec commit 9e33bad

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ whetstone [n] - MIPS (whetstone) performance test (see: https://github.com/DnCra
132132
[CTRL]+[TAB]+[+] - increase CPU freq.</br>
133133
[CTRL]+[TAB]+[-] - decrease CPU freq.</br>
134134
[ALT]+[0-9]+[0-9]+[0-9] - manual enter some character by its decimal code (CP-866 codepage)<br/>
135-
135+
[ALT]+[Enter] - try to use flasg instead of RAM to launch application (from `mc`)<br/>
136+
<br/>
136137
# M-OS system variables
137138
BASE - base directory with commands implementations<br/>
138139
SWAP - swap settings<br/>

src/app.c

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static size_t flash_addr = M_OS_APP_TABLE_BASE;
7070
static list_t* flash_list = NULL;
7171
static list_t* lst = NULL;
7272
typedef struct to_flash_rec {
73-
size_t paddr;
73+
size_t offset;
7474
size_t size;
7575
} to_flash_rec_t;
7676

@@ -81,14 +81,18 @@ void __not_in_flash_func(flash_block)(uint8_t* buffer, size_t flash_target_offse
8181
node_t* n = flash_list->first;
8282
while (n) {
8383
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+
);
8690
return;
8791
}
8892
n = n->next;
8993
}
9094
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;
9296
rec->size = FLASH_SECTOR_SIZE;
9397
list_push_back(flash_list, rec);
9498
uint8_t *e = (uint8_t*)(XIP_BASE + flash_target_offset);
@@ -170,21 +174,22 @@ bool __not_in_flash_func(load_firmware_sram)(char* pathname) {
170174
}
171175

172176
bool load_firmware(char* pathname) {
177+
if (flash_list) delete_list(flash_list);
173178
FILINFO* pfileinfo = pvPortMalloc(sizeof(FILINFO));
174179
f_stat(pathname, pfileinfo);
175180
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);
177182
vPortFree(pfileinfo);
178183
return false;
179184
}
180185
vPortFree(pfileinfo);
181-
fgoutf(get_stdout(), "Loading firmware: '%s'\n", pathname);
186+
goutf("Loading firmware: '%s'\n", pathname);
182187
return load_firmware_sram(pathname);
183188
}
184189

185190
void vAppTask(void *pv) {
186191
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);
188193
vTaskDelete( NULL );
189194
// TODO: ?? return res;
190195
}
@@ -378,6 +383,40 @@ void cleanup_bootb_ctx(cmd_ctx_t* ctx) {
378383
bootb_ctx_t* bootb_ctx = ctx->pboot_ctx;
379384
if (!bootb_ctx) return;
380385
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+
}
381420
delete_list(bootb_ctx->sections);
382421
bootb_ctx->sections = 0;
383422
}
@@ -520,13 +559,13 @@ static uint8_t* load_sec2mem(load_sec_ctx * c, uint16_t sec_num, bool try_to_use
520559
size_t sz = new_flash_addr - prev_flash_addr;
521560
if (prg_addr && sz) {
522561
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?
524563
o->size = sz;
525564
char* tmp = get_ctx_var(get_cmd_startup_ctx(), TEMP);
526565
if(!tmp) tmp = "";
527566
size_t cdl = strlen(tmp);
528567
char * flash_me_file = concat(tmp, _flash_me);
529-
FIL* f = (FIL*)malloc(sizeof(FIL));
568+
FIL* f = (FIL*)pvPortMalloc(sizeof(FIL));
530569
if ( FR_OK != f_open(f, flash_me_file, FA_WRITE | FA_CREATE_ALWAYS) ) {
531570
goutf("Unable to open file '%s'\n", flash_me_file);
532571
goto e5;
@@ -546,7 +585,7 @@ static uint8_t* load_sec2mem(load_sec_ctx * c, uint16_t sec_num, bool try_to_use
546585
f_close(f);
547586
e5:
548587
vPortFree(flash_me_file);
549-
free(f);
588+
vPortFree(f);
550589
list_push_back(lst, o);
551590
vPortFree(del_addr);
552591
del_addr = 0;
@@ -609,10 +648,10 @@ bool load_app(cmd_ctx_t* ctx) {
609648
size_t free_sz = xPortGetFreeHeapSize();
610649
if ((free_sz >> 1) < f->obj.objsize) {
611650
try_to_use_flash = true;
612-
gouta("Try to use flash (by size)\n");
651+
gouta("Attempt to use flash (by size)\n");
613652
}
614653
} else {
615-
gouta("Try to use flash (Alt+Enter)\n");
654+
gouta("Attempt to use flash (Alt+Enter)\n");
616655
}
617656
elf32_header* pehdr = (elf32_header*)pvPortMalloc(sizeof(elf32_header));
618657
UINT rb;
@@ -706,20 +745,19 @@ bool load_app(cmd_ctx_t* ctx) {
706745
uint32_t max_addr = 0;
707746
while(n) {
708747
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;
711750
n = n->next;
712751
}
713752
delete_list(lst);
714753
lst = 0;
715754

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));
718756
if (xPortGetFreeHeapSize() < (FLASH_SECTOR_SIZE + 511)) {
719757
goutf("WARN: free_sz: %d; required: %d\n", xPortGetFreeHeapSize(), (FLASH_SECTOR_SIZE + 511));
720758
goto e8;
721759
}
722-
char* alloc = (char*)pvPortCalloc(1, FLASH_SECTOR_SIZE + 511); // TODO: aliment by ?
760+
char* alloc = (char*)pvPortCalloc(1, FLASH_SECTOR_SIZE + 511);
723761
char* buffer = (char*)((uint32_t)(alloc + 511) & 0xFFFFFE00); // align 512
724762
char* tmp = get_ctx_var(get_cmd_startup_ctx(), TEMP);
725763
if(!tmp) tmp = "";
@@ -773,7 +811,7 @@ bool load_app(cmd_ctx_t* ctx) {
773811
goutf("[%p][%p][%p][%p]\n", bootb_ctx->bootb[0], bootb_ctx->bootb[1], bootb_ctx->bootb[2], bootb_ctx->bootb[3]);
774812
#endif
775813
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);
777815
ctx->ret_code = -1;
778816
return false;
779817
}
@@ -944,7 +982,7 @@ void vCmdTask(void *pv) {
944982
vTaskSetThreadLocalStoragePointer(th, 0, ctx);
945983
//run_app(ctx->orig_cmd);
946984
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);
948986
ctx->stage = EXECUTED;
949987
cleanup_ctx(ctx);
950988
} else {
@@ -1017,6 +1055,6 @@ int kill(uint32_t task_number) {
10171055
return res;
10181056
}
10191057

1020-
void reboot_me(void) {
1058+
void __not_in_flash_func(reboot_me)(void) {
10211059
reboot_is_requested = true;
10221060
}

0 commit comments

Comments
 (0)