void EFile::EeFsCreate(uint8_t *eeprom, int size, BoardEnum board) { this->eeprom = eeprom; this->eeprom_size = size; this->board = board; if (IS_SKY9X(board)) { memset(eeprom, 0xFF, size); } else if (IS_TARANIS(board)) { eeFsArm = (EeFsArm *)eeprom; eeFsVersion = 5; eeFsSize = 8+4*62; eeFsBlockSize = 64; eeFsFirstBlock = 1; eeFsBlocksOffset = eeFsSize - eeFsBlockSize; eeFsBlocksMax = 1 + (EESIZE_TARANIS-eeFsSize)/eeFsBlockSize; eeFsLinkSize = sizeof(int16_t); memset(eeprom, 0, size); eeFsArm->version = eeFsVersion; eeFsArm->mySize = eeFsSize; eeFsArm->freeList = 0; eeFsArm->bs = 64; for (unsigned int i=eeFsFirstBlock; i<eeFsBlocksMax-1; i++) EeFsSetLink(i, i+1); EeFsSetLink(eeFsBlocksMax-1, 0); eeFsArm->freeList = eeFsFirstBlock; // EeFsFlush(); } else { eeFs = (EeFs *)eeprom; eeFsVersion = (board==BOARD_GRUVIN9X || board==BOARD_M128) ? 5 : 4; eeFsBlockSize = 16; eeFsLinkSize = 1; if (eeFsVersion == 5) { eeFsSize = 4+3*36; eeFsFirstBlock = 1; eeFsBlocksOffset = 112 - 16; eeFsBlocksMax = 1 + (4096-112)/16; } else { eeFsSize = 4+3*20; eeFsFirstBlock = 4; eeFsBlocksOffset = 0; eeFsBlocksMax = 2048/16; } memset(eeprom, 0, size); eeFs->version = eeFsVersion; eeFs->mySize = eeFsSize; eeFs->freeList = 0; eeFs->bs = eeFsBlockSize; for (unsigned int i=eeFsFirstBlock; i<eeFsBlocksMax-1; i++) EeFsSetLink(i, i+1); EeFsSetLink(eeFsBlocksMax-1, 0); eeFs->freeList = eeFsFirstBlock; // EeFsFlush(); } }
/* * free one or more blocks */ void EFile::EeFsFree(unsigned int blk) { unsigned int i = blk; while (EeFsGetLink(i)) i = EeFsGetLink(i); if (IS_ARM(board)) { EeFsSetLink(i, eeFsArm->freeList); eeFsArm->freeList = blk; //chain in front } else { EeFsSetLink(i, eeFs->freeList); eeFs->freeList = blk; //chain in front } }
int8_t EFile::EeFsck() { uint8_t *bufp; static uint8_t buffer[BLOCKS]; bufp = buffer; memset(bufp,0,BLOCKS); uint8_t blk ; int8_t ret=0; for(uint8_t i = 0; i <= MAXFILES; i++){ uint8_t *startP = i==MAXFILES ? &(eeFs->freeList) : &(eeFs->files[i].startBlk); uint8_t lastBlk = 0; blk = *startP; //if(i == MAXFILES) blk = eeFs->freeList; // else blk = eeFs->files[i].startBlk; while(blk){ // if(blk < FIRSTBLK ) goto err_1; //bad blk index // if(blk >= BLOCKS ) goto err_2; //bad blk index // if(bufp[blk]) goto err_3; //blk double usage if( ( blk < FIRSTBLK ) //goto err_1; //bad blk index || (blk >= BLOCKS ) //goto err_2; //bad blk index || (bufp[blk] ))//goto err_3; //blk double usage { if(lastBlk){ EeFsSetLink(lastBlk,0); }else{ *startP = 0; //interrupt chain at startpos EeFsFlush(); } blk=0; //abort }else{ bufp[blk] = i+1; lastBlk = blk; blk = EeFsGetLink(blk); } } } for(blk = FIRSTBLK; blk < BLOCKS; blk++){ if(bufp[blk]==0) { //goto err_4; //unused block EeFsSetLink(blk,eeFs->freeList); eeFs->freeList = blk; //chain in front EeFsFlushFreelist(); } } // if(0){ //err_4: ret--; //err_3: ret--; // err_2: ret--; // err_1: ret--; // } return ret; }
void EFile::EeFsFree(uint8_t blk){///free one or more blocks uint8_t i = blk; while( EeFsGetLink(i)) i = EeFsGetLink(i); EeFsSetLink(i,eeFs->freeList); eeFs->freeList = blk; //chain in front EeFsFlushFreelist(); }
uint8_t EFile::write(uint8_t*buf,uint8_t i_len){ uint8_t len=i_len; if(!m_currBlk && m_pos==0) { eeFs->files[m_fileId].startBlk = m_currBlk = EeFsAlloc(); } while(len) { if(!m_currBlk) { m_err = ERR_FULL; break; } if(m_ofs>=(BS-1)){ m_ofs=0; if( ! EeFsGetLink(m_currBlk) ){ EeFsSetLink(m_currBlk, EeFsAlloc()); } m_currBlk = EeFsGetLink(m_currBlk); } if(!m_currBlk) { m_err = ERR_FULL; break; } uint8_t l = BS-1-m_ofs; if(l>len) l=len; EeFsSetDat(m_currBlk, m_ofs, buf, l); buf +=l; m_ofs +=l; len -=l; } m_pos += i_len - len; return i_len - len; }
void EFile::EeFsFormat() { // if(sizeof(eeFs) != RESV){ // extern void eeprom_RESV_mismatch(); // eeprom_RESV_mismatch(); // } memset(eeFs,0, sizeof(EeFs)); eeFs->version = EEFS_VERS; eeFs->mySize = sizeof(EeFs); eeFs->freeList = 0; eeFs->bs = BS; for(uint8_t i = FIRSTBLK; i < BLOCKS; i++) EeFsSetLink(i,i+1); EeFsSetLink(BLOCKS-1, 0); eeFs->freeList = FIRSTBLK; EeFsFlush(); }
void EFile::closeTrunc() { uint8_t fri=0; eeFs->files[m_fileId].size = m_pos; if(m_currBlk && ( fri = EeFsGetLink(m_currBlk))) EeFsSetLink(m_currBlk, 0); EeFsFlush(); //chained out if(fri) EeFsFree( fri ); //chain in }
uint8_t EFile::EeFsAlloc(){ ///alloc one block from freelist uint8_t ret=eeFs->freeList; if(ret){ eeFs->freeList = EeFsGetLink(ret); EeFsFlushFreelist(); EeFsSetLink(ret,0); } return ret; }
void EeFsFormat() { ENABLE_SYNC_WRITE(true); memclear(&eeFs, sizeof(eeFs)); eeFs.version = EEFS_VERS; eeFs.mySize = sizeof(eeFs); eeFs.freeList = 0; eeFs.bs = BS; for (blkid_t i=FIRSTBLK; i<BLOCKS-1; i++) { EeFsSetLink(i, i+1); } EeFsSetLink(BLOCKS-1, 0); eeFs.freeList = FIRSTBLK; #if defined(PCBTARANIS) freeBlocks = BLOCKS; #endif EeFsFlush(); ENABLE_SYNC_WRITE(false); }
void EeFsFormat() { ENABLE_SYNC_WRITE(true); memclear(&eeFs, sizeof(eeFs)); eeFs.version = EEFS_VERS; eeFs.mySize = sizeof(eeFs); eeFs.freeList = 0; eeFs.bs = BS; for (blkidL_t i=FIRSTBLK; i<BLOCKS-1 ; i++) { EeFsSetLink(i, i+1) ; wdt_reset() ; } EeFsSetLink(BLOCKS-1, 0); eeFs.freeList = FIRSTBLK; freeBlocks = BLOCKS ; EeFsFlush(); ENABLE_SYNC_WRITE(false); }
/* * alloc one block from freelist */ unsigned int EFile::EeFsAlloc() { unsigned int ret = (IS_ARM(board) ? eeFsArm->freeList : eeFs->freeList); if (ret) { if (IS_ARM(board)) eeFsArm->freeList = EeFsGetLink(ret); else eeFs->freeList = EeFsGetLink(ret); EeFsSetLink(ret, 0); } return ret; }
/// free one or more blocks static void EeFsFree(blkid_t blk) { blkid_t i = blk; blkid_t tmp; freeBlocks++; while ((tmp=EeFsGetLink(i))) { i = tmp; freeBlocks++; } EeFsSetLink(i, eeFs.freeList); eeFs.freeList = blk; //chain in front EeFsFlushFreelist(); }
/// free one or more blocks static void EeFsFree(blkid_t blk) { blkid_t i = blk; blkid_t tmp; #if defined(PCBTARANIS) freeBlocks++; #endif while ((tmp=EeFsGetLink(i))) { i = tmp; #if defined(PCBTARANIS) freeBlocks++; #endif } EeFsSetLink(i, eeFs.freeList); eeFs.freeList = blk; //chain in front EeFsFlushFreelist(); }
int8_t EeFsck() { ENABLE_SYNC_WRITE(true); uint8_t *bufp = (uint8_t *)&g_model; memclear(bufp, BLOCKS); blkid_t blk ; #if defined(PCBTARANIS) blkid_t blocksCount; #endif for (uint8_t i=0; i<=MAXFILES; i++) { #if defined(PCBTARANIS) blocksCount = 0; #endif blkid_t *startP = (i==MAXFILES ? &eeFs.freeList : &eeFs.files[i].startBlk); blkid_t lastBlk = 0; blk = *startP; while (blk) { if (blk < FIRSTBLK || // bad blk index blk >= BLOCKS || // bad blk indexchan bufp[blk]) // blk double usage { if (lastBlk) { EeFsSetLink(lastBlk, 0); } else { *startP = 0; // interrupt chain at startpos EeFsFlush(); } blk = 0; // abort } else { #if defined(PCBTARANIS) blocksCount++; #endif bufp[blk] = i+1; lastBlk = blk; blk = EeFsGetLink(blk); } } } #if defined(PCBTARANIS) freeBlocks = blocksCount; #endif for (blk=FIRSTBLK; blk<BLOCKS; blk++) { if (!bufp[blk]) { // unused block #if defined(PCBTARANIS) freeBlocks++; #endif EeFsSetLink(blk, eeFs.freeList); eeFs.freeList = blk; // chain in front EeFsFlushFreelist(); } } ENABLE_SYNC_WRITE(false); return 0; }