diff options
Diffstat (limited to 'main/common/tfsclean2.c')
-rw-r--r-- | main/common/tfsclean2.c | 185 |
1 files changed, 97 insertions, 88 deletions
diff --git a/main/common/tfsclean2.c b/main/common/tfsclean2.c index e304f88..8cc6f31 100644 --- a/main/common/tfsclean2.c +++ b/main/common/tfsclean2.c @@ -1,7 +1,7 @@ /************************************************************************** * * Copyright (c) 2013 Alcatel-Lucent - * + * * Alcatel Lucent licenses this file to You under the Apache License, * Version 2.0 (the "License"); you may not use this file except in * compliance with the License. A copy of the License is contained the @@ -26,7 +26,7 @@ * the concatenated data back to the flash. Simple but dangerous. * * If automatic defragmentation (through tfsadd()) is to be used in this - * mode, then the application must reside in ram space that is above + * mode, then the application must reside in ram space that is above * APPRAMSTART + SIZEOF_TFSFLASH. This version of defragmentation assumes * that the ram space needed for defrag will start at APPRAMBASE. * @@ -47,28 +47,28 @@ int tfsfixup(int verbose, int dontquery) { - return(TFSERR_NOTAVAILABLE); + return(TFSERR_NOTAVAILABLE); } #if DEFRAG_TEST_ENABLED int dumpDhdr(DEFRAGHDR *dhp) { - return(TFSERR_NOTAVAILABLE); + return(TFSERR_NOTAVAILABLE); } int dumpDhdrTbl(DEFRAGHDR *dhp, int ftot) { - return(TFSERR_NOTAVAILABLE); + return(TFSERR_NOTAVAILABLE); } #endif /* _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. + * 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. @@ -76,105 +76,114 @@ dumpDhdrTbl(DEFRAGHDR *dhp, int ftot) int _tfsclean(TDEV *tdp, int notused, int verbose) { - TFILE *tfp; - ulong appramstart; - uchar *tbuf, *cp1, *cp2; - int dtot, nfadd, len, chkstat; + TFILE *tfp; + ulong appramstart; + uchar *tbuf, *cp1, *cp2; + int dtot, nfadd, len, chkstat; #if INCLUDE_FLASH - TFILE *lasttfp; + TFILE *lasttfp; #endif - if (TfsCleanEnable < 0) - return(TFSERR_CLEANOFF); + if(TfsCleanEnable < 0) { + return(TFSERR_CLEANOFF); + } - appramstart = getAppRamStart(); + 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); - } + /* 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); + if(dtot == 0) { + return(TFS_OKAY); + } - printf("TFS device '%s' non-powersafe defragmentation\n",tdp->prefix); + printf("TFS device '%s' non-powersafe defragmentation\n",tdp->prefix); - tbuf = (uchar *)appramstart; - tfp = (TFILE *)(tdp->start); + tbuf = (uchar *)appramstart; + tfp = (TFILE *)(tdp->start); #if INCLUDE_FLASH - lasttfp = tfp; + lasttfp = tfp; #endif - 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; - } + 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; + } #if INCLUDE_FLASH - lasttfp = tfp; + lasttfp = tfp; #endif - 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++; - } + 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); - } - } + 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 { + /* 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 - int err; + int err; - err = AppFlashWrite((uchar *)(tdp->start),(uchar *)appramstart, - (tbuf-(uchar*)appramstart)); - if (err < 0) + err = AppFlashWrite((uchar *)(tdp->start),(uchar *)appramstart, + (tbuf-(uchar *)appramstart)); + if(err < 0) #endif - return(TFSERR_FLASHFAILURE); - } + return(TFSERR_FLASHFAILURE); + } - /* All defragmentation is done, so verify sanity of files... */ - chkstat = tfscheck(tdp,verbose); + /* All defragmentation is done, so verify sanity of files... */ + chkstat = tfscheck(tdp,verbose); - return(chkstat); + return(chkstat); } #endif |