idetool.c
来自「MPC5200 BSP 支持ATA,USB, I2C,扩展网口」· C语言 代码 · 共 280 行
C
280 行
/* ideTool.c V1.01 */
#include "VxWorks.h"
#include "iolib.h"
#include "dosFsLib.h"
#include "dcacheCbio.h"
#include "dpartCbio.h"
#include "cbiolib.h"
#include "usrFdiskPartLib.h"
#include "timers.h"
#include "dirent.h"
#include "logLib.h"
#include "taskLib.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 "stdlib.h"
#include "sys\stat.h" /* struct stat */
#include "errnoLib.h"
#include "ctype.h"
#include "pubDir.h"
#include "usb/usbPlatform.h"
#include "usb/tools/cmdParser.h"
#include "ataDrv.h"
#define IDE_NAME "ideTool"
#define IDE_DESCR "IDE Application Program"
#define IDE_VERSION "01f"
#define IDE_COPYRIGHT "Copyright (c) 2007, Zhao Housework.\n"
#define IDE_DEBUG 1
#undef IDE_DEBUG
/* Prompt definition */
#define PROMPT_IDE "ide>"
#define ATA_CACHE_SIZE 0x400000
BLK_DEV *pBlkDev = NULL;
CBIO_DEV_ID cbioVol = NULL;
CBIO_DEV_ID cbio=NULL;
ATA_RAW ataRaw;
LOCAL UINT16 cmdIdeInit(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
pBlkDev = ataDevCreate(0,0,0,0);
if (pBlkDev == NULL)
{
fprintf(fout,"create a device for a ATA/IDE disk failed!\n");
return RET_CONTINUE;
}
dcacheUpdTaskPriority = 30;
cbio=dcacheDevCreate(pBlkDev, 0, ATA_CACHE_SIZE, "/hda");
if (cbio == NULL)
{
fprintf(fout,"Create a disk cache failed!\n");
return RET_CONTINUE;
}
if(dcacheDevTune(cbio,2000, 256, 0, 30)==ERROR)
{
fprintf(fout,"dcache tune failed!\n");
}
dcacheShow(cbio,1);
/* dcacheDevDisable(cbio); */
fprintf(fout,"ide initialization success!\n");
return RET_CONTINUE;
}
LOCAL UINT16 cmdIdeDpart(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
if (usrFdiskPartCreate(cbio,2,50,0,0) ==ERROR)
{
fprintf(fout,"partition table failure!\n");
return RET_CONTINUE;
}
cbioVol=dpartDevCreate(cbio,2,usrFdiskPartRead);
if (cbioVol == NULL)
{
fprintf(fout,"Initialize a partitioned disk!\n");
return RET_CONTINUE;
}
if(dosFsDevCreate("/h1",dpartPartGet(cbioVol,0),8,0) == ERROR)
{
fprintf(fout,"create first file system device failed!\n");
return RET_CONTINUE;
}
if(dosFsDevCreate("/h2",dpartPartGet(cbioVol,1),8,0) == ERROR)
{
fprintf(fout,"create second file system device failed!\n");
return RET_CONTINUE;
}
fprintf(fout,"partition success, two partitions: /h1 and /h2!\n");
return RET_CONTINUE;
}
LOCAL UINT16 cmdIdeFormat(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
if(dosFsVolFormat("/h1",32,0) == ERROR)
{
fprintf(fout,"the first partition format failed!\n");
return RET_CONTINUE;
}
if(dosFsVolFormat("/h2",32,0) == ERROR)
{
fprintf(fout,"the second partition format failed!\n");
return RET_CONTINUE;
}
fprintf(fout,"disk format success!\n");
return RET_CONTINUE;
}
LOCAL UINT16 cmdReadFile(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
int fd;
int size = 1*1024*1024;
int i = 0;
int iRet = 0;
int num = 1;
struct timespec tt1,tt2;
char *rwbuf = NULL;
rwbuf = malloc(size*num);
if( rwbuf == NULL )
{
fprintf(fout, "ERROR failed to malloc buffer!\n" );
return RET_CONTINUE;
}
memset(rwbuf, '0', size);
fd = open("/h1/testfile.txt", O_CREAT | O_RDWR, 0);
if( fd == ERROR )
{
fprintf(fout, " failed to open file: /h1/testfile.txt!\n" );
return RET_CONTINUE;
}
clock_gettime(CLOCK_REALTIME , &tt1);
for(i=0; i<num; i++)
iRet = read(fd, rwbuf+size*i , size);
clock_gettime(CLOCK_REALTIME , &tt2);
fprintf(fout, "Read From file OK TotalSize=%dK Bytes (%ld %ld) (%ld %ld)\n", 1024*num, tt1.tv_sec , tt1.tv_nsec , tt2.tv_sec , tt2.tv_nsec);
close(fd);
free(rwbuf);
return RET_CONTINUE;
}
LOCAL UINT16 cmdWriteFile(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
int fd;
int size = 1 * 1024*1024;
int i = 0;
int iRet = 0;
int num = 1;
char *rwbuf = NULL;
struct timespec tt1,tt2;
rwbuf = malloc(size);
memset(rwbuf , 'a', size);
if( rwbuf == NULL )
{
fprintf(fout, "ERROR failed to malloc buffer!\n" );
return RET_CONTINUE;
}
fprintf(fout, "malloc buffer OK\n");
fd = open("/h1/testfile.txt" ,O_CREAT | O_RDWR, 0);
if( fd == ERROR )
{
fprintf(fout, " failed to open file: /h1/testfile.txt!\n" );
return RET_CONTINUE;
}
clock_gettime(CLOCK_REALTIME , &tt1);
for(i=0; i<num; i++)
{
iRet = write(fd, rwbuf, size);
}
clock_gettime(CLOCK_REALTIME , &tt2);
fprintf(fout,"Write to file OK TotalSize=%dK Bytes (%ld %ld) (%ld %ld)\n",1024*num, tt1.tv_sec , tt1.tv_nsec , tt2.tv_sec , tt2.tv_nsec);
close(fd);
free(rwbuf);
return RET_CONTINUE;
}
LOCAL UINT16 cmdrawRead(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
int vel = 1024*8*2;
int ssize = 512*vel;
int i;
int *pIntBuf = NULL;
int content =0;
int content1 = 0x05050505;
struct timespec t1,t2;
int nRet;
/* InitAta();*/
ataRaw.cylinder = 19;
ataRaw.head = 0;
ataRaw.sector = 40;
ataRaw.nSecs = vel;
ataRaw.direction = 0; /*0:Read;1:Write*/
ataRaw.pBuf = (char*)malloc(ssize);
if( ataRaw.pBuf == NULL ) return RET_CONTINUE;
memset(ataRaw.pBuf , content , ssize);
clock_gettime(CLOCK_REALTIME , &t1);
nRet = ataRawio(0 , 0 , &ataRaw);
clock_gettime(CLOCK_REALTIME , &t2);
fprintf(fout, "Read From file OK TotalSize=%dK (%ld %ld) (%ld %ld)\n", ssize/1024,t1.tv_sec , t1.tv_nsec , t2.tv_sec , t2.tv_nsec);
pIntBuf = (int*)ataRaw.pBuf;
for(i=0; i<ssize / 4; i++){
if(pIntBuf[i] != content1){
fprintf(fout, "Read Error: %x , expected %x\n",pIntBuf[i], content1);
return RET_CONTINUE;
}
}
free(ataRaw.pBuf);
return RET_CONTINUE;
}
LOCAL UINT16 cmdrawWrite(pVOID Param, char **ppCmd, FILE *fin, FILE *fout)
{
int vel = 1024*8*2;
int ssize = 512*vel;
int content =5;
int nRet;
struct timespec t1,t2;
/* InitAta();*/
ataRaw.cylinder = 19;
ataRaw.head = 0;
ataRaw.sector = 40;
ataRaw.nSecs = vel;
ataRaw.direction = 1; /*0:Read;1:Write*/
ataRaw.pBuf = (char*)malloc(ssize);
if( ataRaw.pBuf == NULL ) return RET_CONTINUE;
memset(ataRaw.pBuf , content , ssize);
clock_gettime(CLOCK_REALTIME , &t1);
nRet = ataRawio(0 , 0 , &ataRaw);
clock_gettime(CLOCK_REALTIME , &t2);
fprintf(fout, "Read From file OK TotalSize=%dK (%ld %ld) (%ld %ld)\n", ssize/1024,t1.tv_sec , t1.tv_nsec , t2.tv_sec , t2.tv_nsec);
free(ataRaw.pBuf);
return RET_CONTINUE;
}
LOCAL CMD_DESCR cmdTable [] =
{
{"ideInit", 4, "ideInit", "IDE operation initialization", cmdIdeInit},
{"ideDpart", 4, "ideDpart", "partition the Ide disk ", cmdIdeDpart},
{"ideFormat", 4, "ideFormat", "ideFormat", cmdIdeFormat},
{"ideReadFile", 4, "ideReadFile", "Read File From Ide dsc", cmdReadFile},
{"ideWriteFile", 4, "ideWriteFile", "write File to Ide dsc", cmdWriteFile},
{"rawRead", 4, "rawRead", "Read File from Ide dsc", cmdrawRead},
{"rawWrite", 4, "rawWrite", "Write File to Ide dsc", cmdrawWrite},
{"Help", 4, "Help/?", "Displays list of supported commands.", CmdParserHelpFunc},
{"?", 1, NULL, NULL, CmdParserHelpFunc},
{"Exit", 4, "Exit/Quit/Bye", "Exits program.", CmdParserExitFunc},
{"Quit", 1, NULL, NULL, CmdParserExitFunc},
{"Bye", 3, NULL, NULL, CmdParserExitFunc},
{NULL, 0, NULL, NULL, NULL}
};
UINT16 ideTool (void)
{
UINT16 s;
setvbuf (stdin, NULL, _IONBF, 0);
setvbuf (stdout, NULL, _IONBF, 0);
fprintf (stdout, IDE_NAME ": " IDE_DESCR ", version " IDE_VERSION "\n"
IDE_COPYRIGHT "\n\n");
while ((s = PromptAndExecCmd (NULL, PROMPT_IDE, stdin, stdout, cmdTable))
== RET_CONTINUE)
;
if (s == RET_OK)
fprintf (stdout, IDE_NAME " terminating normally.\n");
else if (s == RET_ERROR)
fprintf (stdout, IDE_NAME " terminating with error.\n");
else
fprintf (stdout, IDE_NAME " terminating with code %d.\n", s);
return s;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?