/* _tfsclean(): * This is an alternative to the complicated defragmentation above. * It simply scans through the file list and copies all valid files * to RAM; then flash is erased and the RAM is copied back to flash. * <<< WARNING >>> * THIS FUNCTION SHOULD NOT BE INTERRUPTED AND IT WILL BLOW AWAY * ANY APPLICATION CURRENTLY IN CLIENT RAM SPACE. */ int _tfsclean(TDEV *tdp, int notused, int verbose) { ulong appramstart; TFILE *tfp, *lasttfp; uchar *tbuf, *cp1, *cp2; int dtot, nfadd, len, err, chkstat; if (TfsCleanEnable < 0) return(TFSERR_CLEANOFF); appramstart = getAppRamStart(); /* Determine how many "dead" files exist. */ dtot = 0; tfp = (TFILE *)tdp->start; while(validtfshdr(tfp)) { if (!TFS_FILEEXISTS(tfp)) dtot++; tfp = nextfp(tfp,tdp); } if (dtot == 0) return(TFS_OKAY); printf("TFS device '%s' non-powersafe defragmentation\n",tdp->prefix); tbuf = (uchar *)appramstart; lasttfp = tfp = (TFILE *)(tdp->start); nfadd = tdp->start; while(validtfshdr(tfp)) { if (TFS_FILEEXISTS(tfp)) { len = TFS_SIZE(tfp) + sizeof(struct tfshdr); if (len % TFS_FSIZEMOD) len += TFS_FSIZEMOD - (len % TFS_FSIZEMOD); nfadd += len; if (s_memcpy((char *)tbuf,(char *)tfp,len,0,0) != 0) return(TFSERR_MEMFAIL); ((struct tfshdr *)tbuf)->next = (struct tfshdr *)nfadd; tbuf += len; } lasttfp = tfp; tfp = nextfp(tfp,tdp); } /* We've now copied all of the active files from flash to ram. * Now we want to see how much of the flash space needs to be * erased. We only need to erase the sectors that have changed... */ cp1 = (uchar *)tdp->start; cp2 = (uchar *)appramstart; while(cp2 < tbuf) { if (*cp1 != *cp2) break; cp1++; cp2++; } #if INCLUDE_FLASH if ((cp2 != tbuf) || (!TFS_FILEEXISTS(lasttfp))) { int first, last; if (addrtosector(cp1,&first,0,0) == -1) return(TFSERR_FLASHFAILURE); if (addrtosector((uchar *)tdp->end,&last,0,0) == -1) return(TFSERR_FLASHFAILURE); printf("Erasing sectors %d-%d...\n",first,last); while(first<last) { if (flasherase(first++) == 0) return(TFSERR_FLASHFAILURE); } } #endif /* Copy data placed in RAM back to flash: */ printf("Restoring flash...\n"); if (TFS_DEVTYPE_ISRAM(tdp)) { memcpy((char *)(tdp->start),(char *)appramstart, (tbuf-(uchar*)appramstart)); } else { #if INCLUDE_FLASH err = AppFlashWrite((uchar *)(tdp->start),(uchar *)appramstart, (tbuf-(uchar*)appramstart)); if (err < 0) #endif return(TFSERR_FLASHFAILURE); } /* All defragmentation is done, so verify sanity of files... */ chkstat = tfscheck(tdp,verbose); return(chkstat); }
int Prof(int argc,char *argv[]) { char *arg1, *arg2, *arg3, *arg4; ulong address; int i, opt, minhit, ret, more; ret = CMD_SUCCESS; minhit = 1; more = 0; address = 0; while((opt=getopt(argc,argv,"a:h:m:s:")) != -1) { switch(opt) { case 'a': address = strtoul(optarg,0,0); break; case 'h': minhit = atoi(optarg); break; case 'm': more = atoi(optarg); break; case 's': strncpy(prof_SymFile,optarg,TFSNAMESIZE); prof_SymFile[TFSNAMESIZE] = 0; break; default: return(CMD_PARAM_ERROR); } } arg1 = argv[optind]; arg2 = argv[optind+1]; arg3 = argv[optind+2]; arg4 = argv[optind+3]; if(argc == optind+1) { if(!strcmp(arg1,"on")) { prof_Enabled = 1; } else if(!strcmp(arg1,"off")) { prof_Enabled = 0; } else if(!strcmp(arg1,"show")) { prof_ShowStats(minhit, more); } else if(!strcmp(arg1,"init")) { prof_BadSymCnt = 0; prof_CallCnt = 0; prof_TidTally = 0; prof_Enabled = 0; prof_FuncTot = 0; prof_TidTot = 0; prof_PcTot = 0; prof_FuncTbl = (struct pdata *)0; prof_TidTbl = (struct pdata *)0; prof_PcTbl = (uchar *)0; prof_PcWidth = 0; prof_PcDelta = 0; prof_SymFile[0] = 0; } else if(!strcmp(arg1,"restart")) { prof_BadSymCnt = 0; prof_CallCnt = 0; prof_TidTally = 0; if(prof_FuncTbl) { for(i=0; i<prof_FuncTot; i++) { prof_FuncTbl[i].pcount = 0; } } if(prof_TidTbl) { for(i=0; i<prof_TidTot; i++) { prof_TidTbl[i].pcount = 0; } prof_TidTbl[i].data = 0; } if(prof_PcTbl) { memset((char *)prof_PcTbl,0,prof_PcTot*prof_PcWidth); } } else if(!strcmp(arg1,"funccfg")) { if(prof_FuncTbl) { printf("Already configured, run init to re-configure\n"); return(CMD_FAILURE); } else if(prof_PcTbl) { printf("Can't use PC and FUNC profiling simultaneously\n"); return(CMD_FAILURE); } else if(address) { prof_FuncTbl = (struct pdata *)address; } else if(prof_TidTbl) { prof_FuncTbl = &prof_TidTbl[prof_TidTot]; } else { prof_FuncTbl = (struct pdata *)getAppRamStart(); } prof_FuncConfig(); } else { ret = CMD_PARAM_ERROR; } } else if(argc == optind+2) { if(!strcmp(arg1,"tidcfg")) { if(prof_TidTbl) { printf("Already configured, run init to re-configure\n"); return(CMD_FAILURE); } else if(address) { prof_TidTbl = (struct pdata *)address; } else if(prof_FuncTbl) { prof_TidTbl = &prof_FuncTbl[prof_FuncTot]; } else if(prof_PcTbl) { prof_TidTbl = (struct pdata *)&prof_PcTbl[prof_PcTot]; } else { prof_TidTbl = (struct pdata *)getAppRamStart(); } prof_TidTot = strtoul(arg2,0,0); for(i=0; i<prof_TidTot; i++) { prof_TidTbl[i].data = 0; prof_TidTbl[i].pcount = 0; } } else { ret = CMD_PARAM_ERROR; } } else if(argc == optind+4) { if(!strcmp(arg1,"call")) { struct monprof mp; mp.type = 0; if(strchr(arg2,'f')) { mp.type |= MONPROF_FUNCLOG; } if(strchr(arg2,'p')) { mp.type |= MONPROF_PCLOG; } if(strchr(arg2,'t')) { mp.type |= MONPROF_TIDLOG; } mp.pc = strtoul(arg3,0,0); mp.tid = strtoul(arg4,0,0); profiler(&mp); } else if(!strcmp(arg1,"pccfg")) { int size; if(prof_PcTbl) { printf("Already configured, run init to re-configure\n"); return(CMD_FAILURE); } else if(prof_FuncTbl) { printf("Can't use PC and FUNC profiling simultaneously\n"); return(CMD_FAILURE); } else if(address) { prof_PcTbl = (uchar *)address; } else if(prof_TidTbl) { prof_PcTbl = (uchar *)&prof_TidTbl[prof_TidTot]; } else { prof_PcTbl = (uchar *)getAppRamStart(); } prof_PcWidth = strtol(arg2,0,0); /* instruction width */ prof_PcTxtBase = strtol(arg3,0,0); /* address of .text */ size = strtol(arg4,0,0); /* size of .text */ prof_PcTxtEnd = prof_PcTxtBase + size; prof_PcTot = size / prof_PcWidth; prof_PcDelta = (ulong)prof_PcTxtBase - (ulong)prof_PcTbl; memset((char *)prof_PcTbl,0,prof_PcTot*prof_PcWidth); } else { ret = CMD_PARAM_ERROR; } } else { ret = CMD_PARAM_ERROR; } return(ret); }
/* _tfsclean(): * This is an alternative to the complicated defragmentation above. * It simply scans through the file list and copies all valid files * to RAM; then flash is erased and the RAM is copied back to flash. * <<< WARNING >>> * THIS FUNCTION SHOULD NOT BE INTERRUPTED AND IT WILL BLOW AWAY * ANY APPLICATION CURRENTLY IN CLIENT RAM SPACE. */ int _tfsclean(TDEV *tdp, int notused, int verbose) { TFILE *tfp; uchar *tbuf; ulong appramstart; int dtot, nfadd, len, err, chkstat; if (TfsCleanEnable < 0) return(TFSERR_CLEANOFF); appramstart = getAppRamStart(); /* Determine how many "dead" files exist. */ dtot = 0; tfp = (TFILE *)tdp->start; while(validtfshdr(tfp)) { if (!TFS_FILEEXISTS(tfp)) dtot++; tfp = nextfp(tfp,tdp); } if (dtot == 0) return(TFS_OKAY); printf("Reconstructing device %s with %d dead file%s removed...\n", tdp->prefix, dtot,dtot>1 ? "s":""); tbuf = (char *)appramstart; tfp = (TFILE *)(tdp->start); nfadd = tdp->start; while(validtfshdr(tfp)) { if (TFS_FILEEXISTS(tfp)) { len = TFS_SIZE(tfp) + sizeof(struct tfshdr); if (len % TFS_FSIZEMOD) len += TFS_FSIZEMOD - (len % TFS_FSIZEMOD); nfadd += len; if (s_memcpy(tbuf,(uchar *)tfp,len,0,0) != 0) return(TFSERR_MEMFAIL); ((struct tfshdr *)tbuf)->next = (struct tfshdr *)nfadd; tbuf += len; } tfp = nextfp(tfp,tdp); } /* Erase the flash device: */ err = _tfsinit(tdp); if (err != TFS_OKAY) return(err); /* Copy data placed in RAM back to flash: */ err = AppFlashWrite((ulong *)(tdp->start),(ulong *)appramstart, (tbuf-(uchar*)appramstart)); if (err < 0) return(TFSERR_FLASHFAILURE); /* All defragmentation is done, so verify sanity of files... */ chkstat = tfscheck(tdp,verbose); return(chkstat); }
/* FatfsCmd(): * This command provides a front end (command line interface) to the * dosfs.c code. The intent is that this be a template for other * "fs-ish" commands within uMon (refer to discussion at the top of this * file). */ int FatfsCmd(int argc, char *argv[]) { uint32_t i, j; int opt, verbose; char *cmd, *env, *arg1, *arg2, *arg3; /* Establish default read/write functions to access underlying * storage media. These defaults can be overridden by the * command line options -r & -w below. */ if ((env = getenv("FATFS_RD")) != 0) fatfsRead = (int(*)(int,char*,int,int))strtol(env,0,0); if ((env = getenv("FATFS_WR")) != 0) fatfsWrite = (int(*)(int,char*,int,int))strtol(env,0,0); verbose = 0; fatfs_tmp_space = (char *)0xffffffff; while ((opt=getopt(argc,argv,"r:s:vw:")) != -1) { switch(opt) { case 'r': fatfsRead = (int(*)(int,char*,int,int))strtoul(optarg,0,0); break; case 's': fatfs_tmp_space = (char *)strtoul(optarg,0,0); break; case 'w': fatfsWrite = (int(*)(int,char*,int,int))strtoul(optarg,0,0); break; case 'v': verbose++; break; default: return(CMD_PARAM_ERROR); } } if ((fatfsRead == 0) || (fatfsWrite == 0)) { printf("FATFS access pointers not initialized\n"); return(CMD_FAILURE); } if (argc < optind + 1) return(CMD_PARAM_ERROR); if (fatfs_tmp_space == (char *)0xffffffff) fatfs_tmp_space = (char *)getAppRamStart(); cmd = argv[optind]; arg1 = argv[optind+1]; arg2 = argv[optind+2]; arg3 = argv[optind+3]; if (strcmp(cmd,"init") == 0) { if (fatfsInit(1,verbose) < 0) return(CMD_FAILURE); } else if (strcmp(cmd,"cat") == 0) { if (argc != optind + 2) return(CMD_PARAM_ERROR); if (fatfsInit(0,0) < 0) return(CMD_FAILURE); for(i=optind+1;i<argc;i++) { if (fatfsCat(argv[i]) < 0) return(CMD_FAILURE); } } else if (strcmp(cmd,"get") == 0) { if (argc != optind + 3) return(CMD_PARAM_ERROR); if (fatfsInit(0,0) < 0) return(CMD_FAILURE); if (fatfsGet(arg1,arg2) < 0) return(CMD_FAILURE); } else if (strcmp(cmd,"put") == 0) { if (argc != optind + 3) return(CMD_PARAM_ERROR); if (fatfsInit(0,0) < 0) return(CMD_FAILURE); if (fatfsPut(arg1,arg2) < 0) return(CMD_FAILURE); } else if (strcmp(cmd,"qry") == 0) { if (argc != optind + 2) return(CMD_PARAM_ERROR); if (fatfsInit(0,0) < 0) return(CMD_FAILURE); if (fatfsQry(arg1,verbose) < 0) return(CMD_FAILURE); } else if (strcmp(cmd,"ls") == 0) { if (fatfsInit(0,0) < 0) return(CMD_FAILURE); if (fatfsLs(arg1) < 0) return(CMD_FAILURE); } else if (strcmp(cmd,"rm") == 0) { if (fatfsInit(0,0) < 0) return(CMD_FAILURE); for(j=optind+1;j<argc;j++) { if (fatfsRm(argv[j]) < 0) return(CMD_FAILURE); } } else { printf("fatfs op <%s> not found\n",cmd); return(CMD_FAILURE); } return(CMD_SUCCESS); }