📄 flashfs.c
字号:
/* ############################################################################ (c) Copyright Virata Limited 1998## Virata Limited Confidential and Proprietary## The following software source code ("Software") is strictly confidential and# is proprietary to Virata Limited ("Virata"). It may only be read, used,# copied, adapted, modified or otherwise dealt with by you if you have# entered into a confidentiality agreement with Virata and then subject to the# terms of that confidentiality agreement and any other applicable agreement# between you and Virata. If you are in any doubt as to whether you are# entitled to access, read, use, copy, adapt, modify or otherwise deal with# the Software or whether you are entitled to disclose the Software to any# other person you should contact Virata. If you have not entered into a# confidentiality agreement with Virata granting access to this Software you# should forthwith return all media, copies and printed listings containing# the Software to Virata.## Virata reserves the right to take legal action against you should you breach# the above provisions.## If you are unsure, or to report violations, please contact# support@virata.com# ##########################################################################*//*********************************************************************** * * * ATMOS Flash Filing System support code * * * * FLASHFS.C * * * ***********************************************************************/#include "kernel.h"#include "stdio.h"#include "errno.h"#include "isfs.h"#include "flash.h"#include "config.h"#include "messages.h"#include "string.h"#include "stat.h"#include "ctype.h"#include "attribute.h"#include "limits.h"#include <assert.h>#include <stdlib.h>#include <dirent.h>#include <file.h>#include "whitelib.h"#include "magnolialib.h"#include "greenlib.h"#include "command.h"#include "flashfslib.h"/*====================================================================*//* Macro and type definitions *//*====================================================================*/#define NO_PARTITION (ULONG_MAX)#ifdef FLASHFS_REWRITE/*** The smallest permissible boot program.*/#define MIN_BOOT_PROGRAM_SIZE 8192#endif#define DYNAMIC_FILE_TYPICAL_MAX (8 * 1024)#define MAXIMUM_FLASHFS_FILES 40#define FLASHFS_OPENS 30#define DYNAMIC 0#define FIXED 1/* Macro for range checking next entry value in linked lists. */#define CHECK_ADDR(_x) ((U32)(_x) < (U32)flashfsSize ? (_x) : 0)/* Macros to place files on particular boundaries in FLASH memory. *//* These were _required_ for the new file placement algorithm when used *//* with the old programming code. *//* The programming code has since been changed to allow the writing of any *//* address in any order. *//* A page program can be avoided for the volroot page if, as a result of a *//* config save, the first dynamic file hasn't moved. For this reason the *//* files are not packed optimally (for space) to make it likely that the *//* file won't move ... */#ifndef GRANULARITY#define GRANULARITY 256 /* This value _must_ be more than 1 */#endif#if (GRANULARITY < 2)#error "GRANULARITY must be more than 1"#endif#define ALIGN_DOWN(_x) ( (U32)(_x) - ((U32)(_x) % GRANULARITY))#define ALIGN_UP(_x) (((U32)(_x) % GRANULARITY) ? \((U32)(_x) + (GRANULARITY - ((U32)(_x) % GRANULARITY))) : (_x))/*** Size of buffer to use when moving fixed files.*/#define SCRATCH_BUFFER_SIZE 4096typedef struct{ U32 inuse; /* Whether this slot is in use or not */ U32 fpos; /* Current file position (R/W) */ U32 fmax; /* Length of file */ U32 datptr; U32 limit; /* Max. permissible write length of file */ U32 partition; /* Partition in which the file resides */ char name [FLASHFS_NAME_LENGTH]; /* The name of the file */ FILE * fp; /* File handle of open file */ void * mmap; /* Memory mapped area */} OPENFILE;typedef struct{ ISFS_DIRENT *dirent; /* ptr to ISFS file dirent */ U32 nextFile; /* ptr value for FLASH entry */ U32 destAddr; /* address of data in FLASH */ U32 fileType; /* file type indicator. */ U32 nextEntry; /* index of next list entry */} LIST_ENT;typedef enum{ MoveIndeterminate, MoveUp, MoveDown} MoveDirection;/*** Enumeration of attributes supported by flashfs.*/typedef enum { FLASHFS_ATTR_PARTITION = 0, FLASHFS_ATTR_MAXPARTITION = 1} FlashFsAttributes;/*====================================================================*//* Forward definitions of procedures in this module *//*====================================================================*//* public functions */void flashfs_process(int argc, BYTE *argv[]);/* local functions */#ifdef USE_OLD_FS_CONSOLEstatic void flashfs_ls(char *p);static void flashfs_cat(char *p);#endif#ifdef FLASHFS_REWRITEstatic void flashfs_rewrite(char *p);#endifstatic int write_boot(ISFS_ROOT *root, U32 partition);static BOOL flashfs_fsck(U32 partition);static U32 check_for_fixed_files(U32 *origFix, U32 *highestFix, U32 startFix);static int program_file_list(LIST_ENT *fileList, U32 firstFile, U32 maxFiles);static int build_file_list(ISFS_ROOT *root, LIST_ENT *fileList, U32 maxFiles, U32 startFix, U32 endDyn, U32 highestFix, U32 *firstFix, U32 *firstDyn, U32 *fileCount);static int copy_existing_fixed_files (LIST_ENT *fileList, U32 firstFix, U32 startFix, U32 fileCount, U32 maxFiles, U32 size, U32 *newOrig);static int move_flash_file (U32 dest, U32 source, U32 length, U32 nextFile, BYTE *buffer);static void flashfs_InitialiseAttributes (OPENFILE *f, PTR *values);static OPENFILE * flashfs_OpenslotFind (void);static BOOL flashfs_Search (U32 list, OPENFILE *slot, U32 *prev);static BOOL flashfs_Find (OPENFILE *slot, U32 *prev, BOOL *isDynamic);static int flashfs_PartitionParse (ATTR_KEYLIST *kl, PTR *vl, char *k, char *v, int d);static BOOL flashfs_PartitionCheck (U32 partition, BOOL checkValidity);static BOOL flashfs_PartitionChange (U32 partition);static int flashfs_ParseOption (char **pBuffer, char *option, int optionLen);static int flashfs_ParseFilename (char *filename, char *namebuffer, U32 *partition);static BOOL flashfs_HasFilesOpen (U32 partition);static U32 flashfs_Last (U32 dataPtr, BOOL isFixed);static int flashfs_SysInfo (U32 partition, struct fsinfo *fsinfobuf);static int flashfs_CreateFile (OPENFILE *slot);static int flashfs_CloseFile (OPENFILE *slot);static int flashfs_RemoveFile (OPENFILE *slot);/*** Message handlers*/static void flashfs_msgClose (ATMOS_MESSAGE *m);static void flashfs_msgFlush (ATMOS_MESSAGE *m);static void flashfs_msgFormat (ATMOS_MESSAGE *m);static void flashfs_msgFseek (ATMOS_MESSAGE *m);static void flashfs_msgFsinfo (ATMOS_MESSAGE *m);static void flashfs_msgFstat (ATMOS_MESSAGE *m);static void flashfs_msgFtell (ATMOS_MESSAGE *m);static void flashfs_msgGetattr (ATMOS_MESSAGE *m);static void flashfs_msgOpen (ATMOS_MESSAGE *m);static void flashfs_msgRead (ATMOS_MESSAGE *m);static void flashfs_msgReaddir (ATMOS_MESSAGE *m);static void flashfs_msgRemove (ATMOS_MESSAGE *m);static void flashfs_msgRename (ATMOS_MESSAGE *m);static void flashfs_msgSetattr (ATMOS_MESSAGE *m);static void flashfs_msgStat (ATMOS_MESSAGE *m);static void flashfs_msgTell (ATMOS_MESSAGE *m);static void flashfs_msgUpdate (ATMOS_MESSAGE *m);static void flashfs_msgUpdatePartition (ATMOS_MESSAGE *m);static void flashfs_msgWrite (ATMOS_MESSAGE *m);static void flashfs_msgMmap(ATMOS_MESSAGE *m);static void flashfs_msgMunmap(ATMOS_MESSAGE *m);/*====================================================================*//* Global variables *//*====================================================================*/extern ISFS_ROOT *isfs1_volroot; /* Pointer to ISFS root structure *//* inter-process flag on isfs linked list status */BOOL flashfs_locking_isfs_list = FALSE;/*====================================================================*//* Local variables *//*====================================================================*/static BOOL flashGood;static FLASHFS_ROOT flashfsRoot;static OPENFILE openfile[FLASHFS_OPENS];static ATTR_KEYLIST flashfsKeys [] = { {"PARTITION", 0, ATTR_DYNAMIC, FLASHFS_ATTR_PARTITION, (PTR) flashfs_PartitionParse }, {"MAXPARTITION", ATTR_READONLY, ATTR_INTEGER, FLASHFS_ATTR_MAXPARTITION, NULL }, {"", 0, 0, 0, 0 }};#define MAX_FLASHFS_ATTRIBUTES (sizeof (flashfsKeys) / sizeof (flashfsKeys [0]))/*** The partition to which flashStartOffset, etc. refers.*/static U32 currentPartition = 0;/*** The partition which is used by default for all operations.*/static U32 currentDefaultPartition = 0;/*** The number of partitions contained in flash.*/static U32 availablePartitions = 1;/*====================================================================*//* Implementation *//*====================================================================*//***----------------------------------------------------------------------**** flashfs_PartitionCheck:**** Check whether the partition supplied is valid.**** Arguments:** partition Partition number.** checkValidity Check the partition's validity too.**** Returns:** FALSE Invalid partition.** TRUE Valid partition.****----------------------------------------------------------------------*/static BOOL flashfs_PartitionCheck (U32 partition, BOOL checkValidity){ BOOL rc; if (partition < availablePartitions) { if (!checkValidity || (ESUCCESS == flashfslib_PartitionValid (partition))) { rc = TRUE; } else { rc = FALSE; } } else { rc = FALSE; } return rc;}/***----------------------------------------------------------------------**** flashfs_PartitionChange:**** Set the current partition to that specified.**** Arguments:** partition Partition number.**** Returns:** FALSE Invalid partition.** TRUE Valid partition.****----------------------------------------------------------------------*/static BOOL flashfs_PartitionChange (U32 partition){ BOOL rc; assert (partition != NO_PARTITION); /* ** Don't change to the same partition. */ if (currentPartition != partition) { int ret; ret = flashfslib_PartitionActivate (partition); if (ESUCCESS == ret) { if (ESUCCESS == flashfslib_PartitionValid (partition)) { flash_read_fs_data (0, (BYTE *)&flashfsRoot, sizeof (FLASHFS_ROOT)); flashGood = TRUE; } else { /* ** Ensure that root structure is invalid. */ memset ((BYTE *) &flashfsRoot, 0, sizeof (FLASHFS_ROOT)); strcpy(flashfsRoot.volser, "ISFSV1"); flashGood = FALSE; } currentPartition = partition; rc = TRUE; } else { rc = FALSE; } } else { rc = TRUE; } return rc;}/***----------------------------------------------------------------------**** flashfs_InitialiseAttributes:**** Initialise the data pointers for attribute handling.**** Arguments:** f Pointer to file structure.** values Array for pointer values**** Returns:** <None>****----------------------------------------------------------------------*/static void flashfs_InitialiseAttributes (OPENFILE *f, PTR *values){ assert (NULL != f); assert (NULL != values); values [FLASHFS_ATTR_PARTITION] = (PTR) &f->partition; values [FLASHFS_ATTR_MAXPARTITION] = (PTR) &availablePartitions; return;}/***----------------------------------------------------------------------**** flashfs_PartitionParse:**** Parse a partition number.**** Arguments:** kl Attribute entry** vl Value for which to find string name** k Key name** v Value containing or to contain key** d Direction (set/get).**** Returns:** ENOENT Operation not supported** ESUCCESS Attribute set, or retrieved.****----------------------------------------------------------------------*/static int flashfs_PartitionParse (ATTR_KEYLIST *kl, PTR *vl, char *k, char *v, int d){ int ret = ESUCCESS; (void) k; /* parameter not used */ if (d) { /* ** Can't change partition once opened but still need to be able to read ** it. */ if (* (U32 *) vl [kl->index] == NO_PARTITION) { char * valueend; U32 partition = (U32) strtol( v, &valueend, 0 ); partition--; if (flashfs_PartitionCheck (partition, FALSE)) { * (U32 *) vl [kl->index] = partition; } else { ret = EINVAL; } } else { ret = ENOENT; } } else { sprintf( v, "%u", 1 + *(int *)vl[kl->index]); } return ret;}/***----------------------------------------------------------------------**** flashfs_ParseOption:**** Parse an option, possibly, from a string**** Arguments:** pBuffer** option
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -