📄 fs_init.c
字号:
//#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <kernel\ros33\ros33.h>
#include <filesys\fs.h>
#include <filesys\fstouser.h>
#include "file.h"
#include "filebuf.h"
#include <sys\flash.h>
#include <sys\gpc.h>
extern int init_area_array();
extern unsigned long CheckDirInfo( void );
#define ERASEAREA(area) \
do{\
(*FlashUnlockBlockP)(area);\
(*FlashEraseBlockP)((unsigned int)(area));\
(*FlashLockBlockP)( area );\
}while(0)
//#define INIT(name1, name2, length) drv_flash_write( (unsigned char *)(name1), (unsigned int )(name2), length )
#define INIT(name1, name2, length)\
do{\
(*FlashUnlockBlockP)((unsigned int )(name2));\
(*FlashWriteBlockP)( (unsigned int )(name2), (unsigned char *)(name1), length );\
(*FlashLockBlockP)( (unsigned int )(name2) );\
}while(0)
//#define FLASH_AREA_SIZE (64*1024)
//#define ICCARD_AREA_SIZE (4*1024)
DEV_DSP_S device_dispcription[] = {
/* devid, rbufsize, rbuf, start, read, write*/
{FLASH_DEVID, 0, NULL, 0, drv_flash_read, drv_flash_write},
// {ICCARD_DEVID, 256, (unsigned char *)iccard_rbuf, 0, 0, 0},
{0, 0, NULL, 0, NULL, NULL}
};
#define FLASH_START_ADDRESS 0xB00000
#define AREA_START(i) ((unsigned int)(FLASH_START_ADDRESS+(i)*FLASH_AREA_SIZE))
#define AREA_USER_BLK(i,j) (AREA_START(i)+(j)*BLOCKSIZE)
#define AREA_NUM 7
const AREA_DSP_S area_dispcription[AREA_NUM+1] = {
/* devid, start, size */
{FLASH_DEVID, AREA_START(0), FLASH_AREA_SIZE},
{FLASH_DEVID, AREA_START(1), FLASH_AREA_SIZE},
{FLASH_DEVID, AREA_START(2), FLASH_AREA_SIZE},
{FLASH_DEVID, AREA_START(3), FLASH_AREA_SIZE},
{FLASH_DEVID, AREA_START(4), FLASH_AREA_SIZE},
{FLASH_DEVID, AREA_START(5), FLASH_AREA_SIZE},
{FLASH_DEVID, AREA_START(6), FLASH_AREA_SIZE},
// {FLASH_DEVID, AREA_START(7), FLASH_AREA_SIZE},
// {FLASH_DEVID, AREA_START(8), FLASH_AREA_SIZE},
// {FLASH_DEVID, AREA_START(9), FLASH_AREA_SIZE},
//{ICCARD_DEVID, AREA_START(10), ICCARD_AREA_SIZE},
//{ICCARD_DEVID, AREA_START(11), ICCARD_AREA_SIZE},
{0, 0, 0}
};
#define PARTITION_NUM 6
const PARTITION Partition[PARTITION_NUM] = {
/* start, num */
{0, 1},
{1, 2},
{3, 1},
{4, 1},
{5, 1},
{6, 1}
};
DIR_CACHE_S cache_data;
/*
* we assume the first block in area_1 stores the root_dir.
* we assume the first block in the first area of every partition
* stores the blockbitmap to flag the status(used or not) of all
* the blocks in this partition.
* we assume the first block in the first area of every partition
* stores the dirctory of this partition.
*/
DIR_S RootDir[PARTITION_NUM] = {
{
{ 0, FILE_MAGIC, AREA_USER_BLK(0,1), NULL, 0, 0, "/", 5 },
{
{ AREA_USER_BLK(1,1), FS_DIR, "db1/" },
{ AREA_USER_BLK(3,1), FS_DIR, "db2/" },
{ AREA_USER_BLK(4,1), FS_DIR, "db3/" },
{ AREA_USER_BLK(5,1), FS_DIR, "db4/" },
{ AREA_USER_BLK(6,1), FS_DIR, "db5/" },
}
},
{{ 0, FILE_MAGIC, AREA_USER_BLK(1,1), NULL, 0, 1, "db1/", 0 },{}},
{{ 0, FILE_MAGIC, AREA_USER_BLK(3,1), NULL, 0, 2, "db2/", 0 },{}},
{{ 0, FILE_MAGIC, AREA_USER_BLK(4,1), NULL, 0, 3, "db3/", 0 },{}},
{{ 0, FILE_MAGIC, AREA_USER_BLK(5,1), NULL, 0, 4, "db4/", 0 },{}},
{{ 0, FILE_MAGIC, AREA_USER_BLK(6,1), NULL, 0, 5, "db5/", 0 },{}},
};
//FILE_S GLOABLE_FILE;
int ERROR_FLAG = 0;
int error_flag = 0;
void dir_init()
{
char blkbitmap[BLOCKSIZE];
int i;
/*
* initial flash rom
*/
for ( i=0; i<AREA_NUM; i++)
ERASEAREA( AREA_START(i) );
/*
* initial DCB_S structs to flash rom
*/
for( i = 0; i < PARTITION_NUM; i++ )
INIT( &RootDir[i], AREA_USER_BLK(Partition[i].start,Partition[i].num), sizeof(DIR_S) );
// INIT( &db1_dir, AREA_USER_BLK(1,1), sizeof(DIR_S) );
// INIT( &db2_dir, AREA_USER_BLK(5,1), sizeof(DIR_S) );
// INIT( &db3_dir, AREA_USER_BLK(7,1), sizeof(DIR_S) );
// INIT( &db4_dir, AREA_USER_BLK(8,1), sizeof(DIR_S) );
// INIT( &db5_dir, AREA_USER_BLK(9,1), sizeof(DIR_S) );
memset( blkbitmap, 0, BLOCKSIZE - sizeof(FCB_S) );
/*
* Initial FCB_S structs to flash rom in a block,
* and clear the following field in the same block.
* Because when we create a file, 暂时!!!02-8-21 21:56
*/
/*
* initial blockbitmaps in flash rom.
* here we assume blockbitmaps store in the first block of
* every area.
*/
memset( blkbitmap, 0xFF, BLOCKSIZE );
blkbitmap[0] = 0x3f;
INIT( blkbitmap, AREA_START(0), BLOCKSIZE );
INIT( blkbitmap, AREA_START(1), BLOCKSIZE );
INIT( blkbitmap, AREA_START(3), BLOCKSIZE );
INIT( blkbitmap, AREA_START(4), BLOCKSIZE );
INIT( blkbitmap, AREA_START(5), BLOCKSIZE );
INIT( blkbitmap, AREA_START(6), BLOCKSIZE );
memset( blkbitmap, 0xFF, BLOCKSIZE );
blkbitmap[0] = 0x1f;
INIT( blkbitmap, AREA_START(2), BLOCKSIZE );
// INIT( blkbitmap, AREA_START(3), BLOCKSIZE );
// INIT( blkbitmap, AREA_START(4), BLOCKSIZE );
// INIT( blkbitmap, AREA_START(6), BLOCKSIZE );
}
int init_cache_data()
{
int num;
// DIR_CACHE_NODE_S *dir_node;
/*
* initial the dirctory root to the first dir_cache_node
*/
cache_data.dir_cache_data[0].flag = 1;
INIT_LIST_HEAD( &cache_data.dir_cache_data[0].child );
INIT_LIST_HEAD( &cache_data.dir_cache_data[0].next );
memcpy( cache_data.dir_cache_data[0].dir, &RootDir[0], sizeof(DIR_S) );
/*
* initial the dir cache struct
*/
cache_data.freenum = DIR_CACHE_NODE_MAX_NUM - 1;
cache_data.freehead = &(cache_data.dir_cache_data[1]);
INIT_LIST_HEAD( &cache_data.treehead );
list_add( &((cache_data.dir_cache_data[0]).next), &cache_data.treehead );
// list_add( &cache_data.dir_cache_data[0].next, &cache_data.dir_cache_data[0].child );
// dir_node = list_entry( &cache_data[0].treehead.next, DIR_CACHE_NODE_S, next );
for( num = 1; num < DIR_CACHE_NODE_MAX_NUM; num++ )
{
memset( &(cache_data.dir_cache_data[num]), 0, DIR_CACHE_NODE_MAX_NUM );
}
return FS_OK;
}
setup1( ){
dir_init();
init_cache_data();
init_area_array();
init_FILE_buf();
}
setup2( ){
init_cache_data();
init_area_array();
init_FILE_buf();
}
unsigned long InitFileSystem( void )
{
unsigned long error;
error = CheckDirInfo();
setup2();
return error;
}
unsigned long CheckDirInfo( void )
{
FCB_S fcb;
int i, j;
unsigned long address;
unsigned short info_y = 26;
unsigned long error = 0;
unsigned long hGC;
hGC = SysGetGC();
for( i = 0; i < PARTITION_NUM; i++ )
{
address = AREA_START(Partition[i].start) + BLOCKSIZE;
memcpy( &fcb, address, sizeof( FCB_S ) );
if( fcb.magic != FILE_MAGIC || fcb.blkid != address || fcb.partition != i || strcmp( fcb.file_name, RootDir[i].curdir.file_name ) )
{
char blkbitmap[BLOCKSIZE];
char info[50];
// sprintf( info, "File %s Corrupt!", RootDir[i].curdir.file_name );
// SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
// info_y += 16;
// initialize direction
for ( j = 0; j < Partition[i].num; j++ )
ERASEAREA( AREA_START(Partition[i].start+j) );
// sprintf( info, "..." );
// SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
INIT( &RootDir[i], AREA_USER_BLK(Partition[i].start, 1 ), sizeof(DIR_S) );
// sprintf( info, "... ..." );
// SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
memset( blkbitmap, 0xFF, BLOCKSIZE );
blkbitmap[0] = 0x3f;
INIT( blkbitmap, AREA_START(Partition[i].start), BLOCKSIZE );
// sprintf( info, "... ... ..." );
// SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
// info_y += 16;
blkbitmap[0] = 0x1f;
for( j = 1; j < Partition[i].num; j++ )
INIT( blkbitmap, AREA_START(Partition[i].start+j), BLOCKSIZE );
// sprintf( info, "Retrieve Succeed!" );
// SysTextOut( hGC, GPC_BLACK, 10, info_y, info, GPC_REPLACE_STYLE );
// info_y += 16;
error |= ( 1 << i );
}
}
return error;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -