pubdir.c

来自「MPC5200 BSP 支持ATA,USB, I2C,扩展网口」· C语言 代码 · 共 2,219 行 · 第 1/5 页

C
2,219
字号
#include <vxWorks.h>
#include <ioLib.h> /* open */
#include <iosLib.h>
#include <stdio.h> /* fopen, getchar */
#include <fioLib.h> /* printf */ /* fioLib - formatted I/O library */
#include <string.h>
#include <sysLib.h>
#include <usrLib.h>  /* cd */
#include <taskLib.h> /* taskDelay */
#include <sysLib.h> /* sysClkRateGet */
#include <ifLib.h>
#include <bootLib.h>
#include <intLib.h>
#include <sockLib.h>
#include <stdlib.h>
#include <dirent.h> /* DIR */
#include <sys\stat.h> /* struct stat */
#include <ctype.h> /* isprint */
#include "time.h" /* for struct tm */
#include "symLib.h"
#include "config.h"
#include "tffs/tffsDrv.h"
#include <dirent.h> /* statfs */
#include <stat.h> /* statfs */
#include <pingLib.h>
#include <taskLib.h>    /* TASK_DESC */
#include <routeLib.h>
#include <inetLib.h>
#include <ftpLib.h>
#include <loglib.h>
#include <errnoLib.h>
#include "pubDir.h"
#include "flashx9.h"
#include "i2c.h" 
#include "rtClk.h"

#define MAX_LINE        160 
#define MAX_PATH_NAME_LEN 256
#define VERSION_ROOT_DIR "/tffs0/version/"
#define VERFILE_NAME	"MPU5200/vx.bin"
#define SPRINTF_BUF_LEN     5120
#define READ_FILE_BUFFER_SIZE 1024
#define SWITCH_MATCH            0x00000100
#define FAIL Print("fail\n")
#define VERSION_DEST_ADDR RAM_LOW_ADRS /* RAM_LOW_ADRS #defined in config.h */
#define MAX_CMDTOKEN    10 /* 命令中的最大TOKEN数 */
#define MAX_PARAM_NUM (MAX_CMDTOKEN -1) /* 命令参数的最大个数 */
#define NO_BAT_CMD -1 /* 批处理缓冲区中 没有 待处理命令 */
#define GET_BAT_CMD 1 /* 批处理缓冲区中   有 待处理命令 */
#define BAT_BUF_SIZE 500
#define FTP_USER_LEN    16
#define FTP_PASSWD_LEN  16
#define PLINE   printf ("%d\n", __LINE__)
#define DPLINE  if (s_debug)    PLINE
#define SYNC_FS

#if (!defined(__VER) && defined(_MKBOOT))
int print (char *format, ...);
#endif
static void showDateTime ();
static void adjustTime();
static void intialTime();
static char * my_gets (char *v_buf, int v_len);
int write_with_dot (int v_fid, UCHAR *v_ptr, ULONG v_ulLen);
int downloadFile( int v_iWithParam, int v_flag_force, char  *v_pLocalFile, UCHAR *v_pHostFile,ULONG v_ulHostIp,UCHAR *v_pUserName, UCHAR *v_pPassWord);
static int copyFile(int originFileId, int newFileId);
int checkFileNameFormat (char *v_fileName);
int _sync_vol_vx (char *vol_name);
int _fGetFileSize (int v_fid);
int getActTxtPath(char *v_pActFilePath);
int resetActVer();
void _toBackSlash(char *v_str);
int isAbsOrRelPath(char *v_pPath);
int getActVerFileName(char *v_pFileName, int v_iBufLen);
static int my_remove(char *filename);
static int getFSSize();
static int getFSFreeSize();
int verifyMatch(char *str, char *pattern, int casesensitive);
static int neatenPattern(char *pattern);
static int doMatch(char *str, char *pattern, int casesensitive);
static void cmd_pause();
int Rel2Abs(char *v_cwd, char *v_dirname);
int getDirAndFileFromPath(char *v_path, char *v_dir, char *v_file);
static int isDir(char *name);
void _addSlash(char *v_str);
char *_getDefaultDeviceName();
int _rel2Abs(char *v_cwd, char *v_dirname);
void initPrintDot (int v_iPriority);
void exitPrintDot ();
void startPrintDot ();
void stopPrintDot ();
void setBurnBootFlag(void);
void clearBurnBootFlag(void);
int bootErase ();
int bootWrite (unsigned int offset, unsigned char *buffer, unsigned int dataLen);
DIR* my_opendir(char* v_dirName);
int my_closedir(DIR * v_pDir);
extern int devX9EraseChip(void);
extern int devX9FlashPrg(
    unsigned long offset,     /* the offset from flash base address */
    VBYTE *bufAddr,   /* the source content address */
    unsigned long dataLen     /* the length will be writen */
    );
extern void eraseTotalFlash(void);
IMPORT int	ifreset ();
IMPORT void muxDevStopAll();
extern void showDs1307RtcTime(void);
extern STATUS sysRtcInit (void);
extern STATUS setCurrentTime ( int year, int mon, int day, int weekday, int hour, int min, int sec);
extern int netLoad (char *hostName, char *fileName, char *usr, char *passwd, FUNCPTR *pEntry ); /* bootConfig.c */
extern void  go (FUNCPTR entry); /* bootConfig.c */
static void init();
static void getCwd(char *pcpwd);
static int getCommandFromBatBuf(char *pccmd);
static void execCmd( BYTE *strCmd );
static void cmd_bat(char *batfile);
static void cmd_m (FAST char *adrs,int v_type);
static void cmd_d (FAST char *adrs, int nwords);
static void cmd_d_pp();
static void cmd_m_pp();
static void printHelp( char *cmdstr );
int run();

char _ctoi(char v_HexDigit);
char _itoc(char v_DecDigit);
int _isValidIfMask (unsigned long ulIfMask);
int _isValidIfAddr (unsigned long ulIfAddr);
void setFtpPasswd(char *pcPasswd);
void setFtpUser(char *pcUserName);
void setFtpHostIp(ULONG ulNewHostIp);
void setOutBandMac(char *pcMac);
void setBoardWorkTypeByShell( BYTE vucType );
BYTE getBoardWorkType( void );
void getOutBandMac(char *pcMac);
int switchShell ();
int getCmdNumber();
int parseFileSize(char *v_ptr, int v_iLen);
int select_recv (int sock, char *pBuf, unsigned long BufSize, unsigned long options);
static int  ftpSocketRead(int sock, char *pBuf, unsigned long BufSize);
int _stricmp(char *v_str1, char *v_str2);
int my_open ( char * name, int  flags,int  mode);
int _getHostFileSize_pc (char *v_pHostDir, char *v_pHostFile, ULONG v_ulHostIp, UCHAR *v_pUserName, UCHAR *v_pPassWord );
CMD_FUNC_PTR getCmdFunc(char *v_pcCmdStr);
void _strLower( unsigned char *v_strCmd );
void * my_malloc (unsigned int v_nBytes);
void my_free (void *p);
int downloadData(int v_iWithParam, UCHAR *v_pBlock, int v_iBlockLen, int *v_piDataLen, UCHAR *v_pHostFile, ULONG v_ulHostIp, UCHAR *v_pUserName, UCHAR *v_pPassWord);
extern void InitBootCfgPara(char *pvCfgPara);
extern int WriteBootCfgPara(char *pvCfgPara);
ULONG getFtpHostIp();
void getFtpUser(char *pcUserName);
void getFtpPasswd(char *pcPasswd);
int getHostFileSize( char *vphostfile, ULONG vulhostip, UCHAR *vpusername, UCHAR *vpassword);
void _ulIp2strIp(char *v_ipstr, unsigned long v_ipaddr);
/* 各命令的执行函数 */
static void cmd_printAllHelp( void );
static void cmd_loadBin (char *v_filename);
#ifdef _MKBOOT
static void cmd_bootLoad();
#endif
static void cmd_run(void);
static void cmd_config(void);
static void cmd_burn(char *hostfile);
void cmd_cd(char *name);
static void cmd_dir(char *dirnameorpattern);
static void _cdToParent();
static void _mkDir();
int _mkdir (char *dirName);
static void cmd_mkDir(char *name);
static void cmd_rmDir(char *name);
void cmd_rmf(char *name);
static void cmd_ren(char *oldfile, char *newfile);
static void cmd_cp(char *source, char *dest); 
static void cmd_down(char *hostfile, char *localfile);
static void cmd_ping (char *v_host, int v_packetNum, int v_packetLen);
static void _ping ();
static void _loadBin();
static void cmd_p(void);
static void cmd_format( void );
static void cmd_lformat( void );
static void _burn();
static void cmd_pwd( void );
static void _cd();
static void _dir();
static void cmd_df();
static void _rmDir();
static void _rm();
static void _ren();
static void _cp();
static void _down();

SYMTAB_ID   statSymTbl;
static char s_acBuffer[READ_FILE_BUFFER_SIZE];
int g_opendirTimes = 0;
int g_closedirTimes = 0;
static int isBurning =0;
static int      s_printDot = 0;         /* 打点的开关 */
static int      print_dot_state = 0;    /* 状态机的状态,初始值must be 0 */
static int      exit_print_dot = 0;     /* 是否退出打点任务,初始值must be 0 */
static  char    acBlock[4000]; /* array of char,保存一个文件的信息 */
static int  s_debug = 0;
unsigned long g_switch = 0;
int g_mallocTimes = 0;
int g_freeTimes = 0;
int g_openTimes = 0;
int g_closeTimes = 0;
static char s_s [SPRINTF_BUF_LEN];
static int     globalInFd;
static int     globalOutFd;
static int     globalErrFd;
int     g_null_fd;
static int     bshellOutFd;
static int     bshellErrFd;
int     g_global_std_out;
static char s_acBatBuf[BAT_BUF_SIZE] = {0}; /* bat file buffer */
char *tokens[MAX_PARAM_NUM +1];
CFG_PARA s_structCfgPara = { {0x00,0xD0,0xD0,0x92,0x80,0x01},
                       {0x00,0xD0,0xD0,0x92,0x80,0x02},
                       0x0A3E19B4,  /* 10.62.25.180 */
                       0xFFFF0000,
                       0x0A0619B4,  /* 10.6.25.180*/
                       0xFFFF0000,
                       /* 0x0A3E1BF2,   10.62.27.242*/
                       0x0A3D6491, /* 10.61.100.145 */
                       {'t','a','r','g','e','t','\0'},
                       {'t','a','r','g','e','t','\0'},
                       0x00000000, /* gateway */
                       0,  /* 1483B mode */
                       1,  /* ETH mode */
                       93, /* IMA mode */
                       0,  /* unknown board work type */
                       0x0A0619B4,  /* 10.6.25.180*/
											 8                       
                     };
/* function table */
CMD_DESC g_CmdDescList[] = { /* the command string here must be lower case */
    /* 一般命令 */
    { "help",           (CMD_FUNC_PTR)cmd_printAllHelp,
                "?/help                         Display ADSL bootrom command list\n"
    },
    { "?",              (CMD_FUNC_PTR)cmd_printAllHelp,
                ""
    },
    { "!",              (CMD_FUNC_PTR)_loadBin,
                "! [filename]                   Load and run binary file\n"
    },
    #ifdef _MKBOOT
    { "@",              (CMD_FUNC_PTR)cmd_bootLoad,
                "@                              Load\n" /* 将vxworks核载进去跑 */
    },
    #endif
    { "run",            (CMD_FUNC_PTR)cmd_run,
                "run                            Run the local version\n"
    },
    { "c",              (CMD_FUNC_PTR)cmd_config,
                "c                              Config IP and MAC address and so on\n"
    },
    { "p",              (CMD_FUNC_PTR)cmd_p,
                "p                              Print configuration\n"
    },
    { "format",         (CMD_FUNC_PTR)cmd_format,
                "format                         Format the total flash, then reset\n"
    },
    { "lformat",        (CMD_FUNC_PTR)cmd_lformat,
                "lformat                        Low-Format the total flash, then reset\n"
    },
    { "burn",           (CMD_FUNC_PTR)_burn,
                "burn <hf>                      Download a file and burn it into bootrom\n"
    }, /* 软件烧写boot, or named WR? */
    { "pwd",            (CMD_FUNC_PTR)cmd_pwd,
                "pwd/cd                         Display current directory\n"
    },
    { "cd",             (CMD_FUNC_PTR)_cd,
                "cd <dirname>                   Change current directory\n"
    },
		{ "cd..",           (CMD_FUNC_PTR)_cdToParent,
                ""
    },
    { "dir",            (CMD_FUNC_PTR)_dir,
                "dir/ls                         Display a list of files and subdirs in a dir\n"
    },
    { "ls",             (CMD_FUNC_PTR)_dir,
                ""
    },
    { "df",             (CMD_FUNC_PTR)cmd_df,
                "df                             Display file system space info\n"
    },
    { "mkdir",          (CMD_FUNC_PTR)_mkDir,
                "mkdir <dirname>                Make dir\n"
    },
    { "rmdir",          (CMD_FUNC_PTR)_rmDir,
                "rmdir <dirname>                Remove dir\n"
    },
    { "rm",             (CMD_FUNC_PTR)_rm,
                "rm <filename>                  Remove file\n"
    },
    { "ren",            (CMD_FUNC_PTR)_ren,
                "ren <oldfile> <newfile>        Rename file\n"
    },
    { "cp",             (CMD_FUNC_PTR)_cp,
                "cp/copy <source> <dest>        Copy file(s)\n"
    },
    { "copy",           (CMD_FUNC_PTR)_cp,
                ""
    },
    { "d",            (CMD_FUNC_PTR)cmd_d_pp,
                "d addr [num]                   Display the content of memory\n"
    },
    { "m",            (CMD_FUNC_PTR)cmd_m_pp,
                "m addr                         Set the value to memory\n"
    },
     /* 版本相关,上传下载,活动版本 */
    { "down",           (CMD_FUNC_PTR)_down,
                "down/download <hf> <lf>        Download hostfile, save it as localfile locally\n"
    },
    { "ping",           (CMD_FUNC_PTR)_ping,
                "ping <host> <pakNum> <pakLen>\n"
    },
    { "intialTime",           (CMD_FUNC_PTR)intialTime,
                "intialTime                     intialize realtime clock time\n"
    },
    { "adjustTime",           (CMD_FUNC_PTR)adjustTime,
                "adjustTime                     Specify new date-time\n"
    },
    { "date",           (CMD_FUNC_PTR)showDateTime,
                "date/time                      show current date-time\n"
    },
    { "time",           (CMD_FUNC_PTR)showDateTime,
                "date/time                      show current date-time\n"
    },
    { "",               (CMD_FUNC_PTR)NULL, NULL } };

/********************************************************************************************/
/* check dirs */
int checkAllDirs ()
{
    char *pCardName = NULL;
    int fid = INT_INIT;

    /* */
    chdir (ROOT_DIR);
    if (NO == _isExist("/tffs0/version"))
        _mkdir ("/tffs0/version");

    if (NO == _isExist("/tffs0/data"))
        _mkdir ("/tffs0/data");

    chdir ("/tffs0/version");
	  pCardName="MPU5200";
	  _checkDir (pCardName);
    chdir (OWN_VER_DIR);
    /* creat act.txt */
    if (NO == _isExist("act.txt"))
    {
        printf("\nfile act.txt does not exist, create it ... ");
        fid = my_creat ("act.txt", O_RDWR, FLAG_CREATE_UNTOUCH);
        if (ERROR == fid)
        {
            printf ("failed!\n");
            chdir (ROOT_DIR);
            return ERROR;
        }
        else
        {
            printf ("done!\n");
            my_close (fid);
            chdir (ROOT_DIR);
            return OK;
        }
    }

    chdir (OWN_VER_DIR);
    return OK;
}
/**********************************************************/
/*
 * 文件或目录是否已经存在
 * YES or NO
 */
int _isExist(char *v_pcName)
{
    struct stat statistic; /* 只是用来判断文件是否存在,并没有引用成员变量 */

    if (OK == stat(v_pcName, &statistic))
        return YES;
    else
        return NO;
}
/*
 * 描述:
 *   该函数对于creat的封装在于,可以有一个flag指示,如何处理和要新的文件同名的文件已经存在的情况
 *   创建一个文件,如果该文件已经存在,根据flag_force作相应的处理。
 *   由于creat也打开了新建的文件,所以g_openTimes++;
 * 参数说明:
 *   v_flag: O_RDONLY, O_WRONLY, or O_RDWR
 */
FID my_creat(char *v_pFileName, int v_flag, int v_flag_force)
{
    char ans[2];
    FID fId;

    /* filename所指定的文件是否已经存在? */
    if (YES == _isExist (v_pFileName))
    {
        if (FLAG_CREATE_UNTOUCH == v_flag_force)
        {
            printf("fail: the local file with the same name already exists.\n");
            return ERROR;
        }
        else if (FLAG_CREATE_FORCE == v_flag_force)
        {
            if (ERROR == remove(v_pFileName))
            {
                printf("fail: delete file %s.\n", v_pFileName);
                return ERROR;
            }
        }
        else
        {
            ans[0] = 'N';
            ans[1] = '\0';
            Prompt("the local file with the same name already exists, delete it?", PROMPT_CHAR, ans);
            if ('Y' == ans[0] || 'y' == ans[0])
            {
                if (ERROR == remove(v_pFileName))
                {
                    printf("fail: delete file %s.\n", v_pFileName);
                    return ERROR;
                }
            }
            else
            {
                return ERROR;
            }
        }
    }

    /* creat local file */
    if (ERROR == (fId = creat(v_pFileName, v_flag)))
    {
        printf("fail: create file %s.\n", v_pFileName);
        return ERROR;
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?