//Implementation of _fat32GetFileAttributes. static DWORD _fat32GetFileAttributes(__COMMON_OBJECT* lpDev, LPCTSTR pszFileName) //Get file's attribute. { __FAT32_FS* pFat32Fs = NULL; CHAR FileName[MAX_FILE_NAME_LEN]; __FAT32_SHORTENTRY ShortEntry; DWORD dwFileAttributes = 0; if((NULL == pszFileName) || (NULL == lpDev)) { goto __TERMINAL; } pFat32Fs = (__FAT32_FS*)(((__DEVICE_OBJECT*)lpDev)->lpDevExtension); //strcpy(FileName,(LPSTR)lpDrcb->lpInputBuffer); StrCpy((CHAR*)pszFileName,FileName); ToCapital(FileName); if(!GetDirEntry(pFat32Fs,&FileName[0],&ShortEntry,NULL,NULL)) { _hx_printf("In _fat32GetFileAttributes: Can not get directory entry.\n%s\n",FileName); goto __TERMINAL; } dwFileAttributes = ShortEntry.FileAttributes; __TERMINAL: return dwFileAttributes; }
static DWORD rd(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF CHAR FullName[MAX_FILE_NAME_LEN]; BOOL bResult = FALSE; if(pCmdObj->byParameterNum < 2) { PrintLine(" Please specify the directory name to be deleted."); goto __TERMINAL; } strcpy(FullName,FsGlobalData.CurrentDir); strcat(FullName,pCmdObj->Parameter[1]); ToCapital(FullName); bResult = IOManager.RemoveDirectory((__COMMON_OBJECT*)&IOManager, FullName); if(!bResult) { PrintLine(" Can not delete the specified directory."); goto __TERMINAL; } __TERMINAL: return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }
static DWORD md(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF CHAR DirPath[MAX_FILE_NAME_LEN]; if(pCmdObj->byParameterNum < 2) { PrintLine(" Please specify the directory name to be created."); return SHELL_CMD_PARSER_SUCCESS;; } strcpy(DirPath,FsGlobalData.CurrentDir); strcat(DirPath,pCmdObj->Parameter[1]); ToCapital(DirPath); if(IOManager.CreateDirectory((__COMMON_OBJECT*)&IOManager, DirPath, NULL)) { PrintLine(" Create directory successfully."); } else { PrintLine(" Create directory failed."); } return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }
static DWORD dir(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF FS_FIND_DATA ffd = {0}; __COMMON_OBJECT* pFindHandle = NULL; BOOL bFindNext = FALSE; strcpy(Buffer,FsGlobalData.CurrentDir); /*if(pCmdObj->byParameterNum >= 2) //Target directory specified. { strcat(Buffer,pCmdObj->Parameter[1]); }*/ ToCapital(Buffer); //Convert to capital. if(pCmdObj->byParameterNum >= 2 && strcmp(pCmdObj->Parameter[1],"-l") == 0) { ffd.bGetLongName = TRUE; } pFindHandle = IOManager.FindFirstFile((__COMMON_OBJECT*)&IOManager, Buffer, &ffd); if(NULL == pFindHandle) //Can not open the target directory. { PrintLine("Can not open the specified or current directory to display."); goto __TERMINAL; } //Dump out the directory's content. if(ffd.bGetLongName) { PrintLine("File_Name "); } else { PrintLine(" File_Name FileSize F_D"); } PrintLine("--------------------------------------------"); do{ PrintDir(&ffd); bFindNext = IOManager.FindNextFile((__COMMON_OBJECT*)&IOManager, Buffer, pFindHandle, &ffd); }while(bFindNext); PrintLine("--------------------------------------------"); //Close the find handle. IOManager.FindClose((__COMMON_OBJECT*)&IOManager, Buffer, pFindHandle); __TERMINAL: return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }
static DWORD vl(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF HANDLE hFile = NULL; CHAR Buffer[128]; DWORD dwWritten = 0; CHAR FullName[MAX_FILE_NAME_LEN]; INT dwCounter = 6; if(pCmdObj->byParameterNum < 2) { PrintLine(" Please specify the file name to be displayed."); goto __TERMINAL; } strcpy(FullName,FsGlobalData.CurrentDir); strcat(FullName,pCmdObj->Parameter[1]); ToCapital(FullName); //Try to open the target file. hFile = IOManager.CreateFile((__COMMON_OBJECT*)&IOManager, FullName, FILE_ACCESS_WRITE | FILE_OPEN_ALWAYS, 0, NULL); if(NULL == hFile) { PrintLine(" Please specify a valid and present file name."); goto __TERMINAL; } while(dwCounter!= 0) { _hx_sprintf(Buffer,"Wish it can work,this line number is %d.\r\n",dwCounter); if(!IOManager.WriteFile((__COMMON_OBJECT*)&IOManager, hFile, strlen(Buffer), Buffer, &dwWritten)) { _hx_sprintf(Buffer," Can not write to file,The line number is %d.",dwCounter); PrintLine(Buffer); goto __TERMINAL; } dwCounter --; } __TERMINAL: if(hFile) { IOManager.CloseFile((__COMMON_OBJECT*)&IOManager, hFile); } return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }
static DWORD cd(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF CHAR NewPath[MAX_FILE_NAME_LEN]; CHAR* pCurrPos = NULL; DWORD dwFileAttr = 0; if(1 == pCmdObj->byParameterNum) //Print out the current directory. { _hx_sprintf(Buffer," Current directory is: %s",FsGlobalData.CurrentDir); PrintLine(Buffer); goto __TERMINAL; } if(0 == strcmp(".",pCmdObj->Parameter[1])) //Current directory. { goto __TERMINAL; } if(0 == strcmp("..",pCmdObj->Parameter[1])) //Parent directory. { PrintLine(" Can not supported yet.:-)"); goto __TERMINAL; } if(0 == strcmp("\\",pCmdObj->Parameter[1])) //Root directory is specified. { FsGlobalData.CurrentDir[0] = FsGlobalData.CurrentFs; FsGlobalData.CurrentDir[1] = ':'; FsGlobalData.CurrentDir[2] = '\\'; FsGlobalData.CurrentDir[3] = 0; goto __TERMINAL; } //Want to enter a new directory. strcpy(NewPath,FsGlobalData.CurrentDir); strcat(NewPath,pCmdObj->Parameter[1]); //Form the new path. strcat(NewPath,"\\"); //Specify a slash. ToCapital(NewPath); //Convert to capital. if((dwFileAttr = IOManager.GetFileAttributes((__COMMON_OBJECT*)&IOManager, NewPath)) == 0) //Use GetFileAttributes to verify the validity of new directory. { PrintLine(" Bad target directory name."); goto __TERMINAL; } if((dwFileAttr & FILE_ATTR_DIRECTORY) == 0) //Is not a directory,is a regular file. { PrintLine(" Please specify a DIRECTORY name,not a file name."); goto __TERMINAL; } //Validation is ok,set the new path as current directory. strcpy(FsGlobalData.CurrentDir,NewPath); __TERMINAL: return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }
//Implementation of CreateDirectory. static BOOL _CreateDirectory(__COMMON_OBJECT* lpThis, LPCTSTR lpszFileName, LPVOID lpReserved) { __DEVICE_OBJECT* pFsObject = NULL; __DRCB* pDrcb = NULL; __IO_MANAGER* pIoManager = (__IO_MANAGER*)lpThis; BYTE FsIdentifier = 0; DWORD dwFlags; CHAR FileName[512]; DWORD dwResult; int i; if((NULL == lpszFileName) || (NULL == pIoManager)) { return FALSE; } StrCpy((CHAR*)lpszFileName,FileName); ToCapital(FileName); //Convert to capital. //Get file system identifier. FsIdentifier = FileName[0]; //Get the file system driver object. __ENTER_CRITICAL_SECTION(NULL,dwFlags); for(i = 0;i < FILE_SYSTEM_NUM;i ++) { if(pIoManager->FsArray[i].FileSystemIdentifier == FsIdentifier) { pFsObject = (__DEVICE_OBJECT*)pIoManager->FsArray[i].pFileSystemObject; break; } } __LEAVE_CRITICAL_SECTION(NULL,dwFlags); if(NULL == pFsObject) //Can not allocate the specified file system object. { return FALSE; } //Validate file system object's validity. if(DEVICE_OBJECT_SIGNATURE != pFsObject->dwSignature) { return FALSE; } //Create DRCB object and issue Create Directory command. pDrcb = (__DRCB*)ObjectManager.CreateObject(&ObjectManager, NULL, OBJECT_TYPE_DRCB); if(NULL == pDrcb) //Failed to create DRCB object. goto __TERMINAL; if(!pDrcb->Initialize((__COMMON_OBJECT*)pDrcb)) //Failed to initialize. goto __TERMINAL; pDrcb->dwRequestMode = DRCB_REQUEST_MODE_IOCTRL; pDrcb->dwCtrlCommand = IOCONTROL_FS_CREATEDIR; pDrcb->dwStatus = DRCB_STATUS_INITIALIZED; pDrcb->dwInputLen = sizeof(LPCTSTR); pDrcb->lpInputBuffer = (LPVOID)&FileName[0]; dwResult = pFsObject->lpDriverObject->DeviceCtrl((__COMMON_OBJECT*)pFsObject->lpDriverObject, (__COMMON_OBJECT*)pFsObject, pDrcb); __TERMINAL: if(pDrcb) { ObjectManager.DestroyObject(&ObjectManager, (__COMMON_OBJECT*)pDrcb); } return dwResult ? TRUE : FALSE; }
//CreateFile,open a device or file given it's name. static __COMMON_OBJECT* _CreateFile(__COMMON_OBJECT* lpThis, //IOManager object. LPSTR lpszFileName, DWORD dwAccessMode, DWORD dwShareMode, LPVOID lpReserved) { CHAR FileName[512]; __COMMON_OBJECT* pFileHandle = NULL; if(NULL == lpszFileName) { return NULL; } if(StrLen(lpszFileName) > 511) //File name too long. { return NULL; } if((lpszFileName[0] == 0) || (lpszFileName[1] == 0) || (lpszFileName[2] == 0)) //Target file name should has //at lease 3 characters. { return NULL; } //strcpy(FileName,lpszFileName); StrCpy(lpszFileName,FileName); ToCapital(FileName); //Convert to capital. if(IS_LETTER(FileName[0])) //Maybe a file object. { if(FileName[1] != ':') //Invalid file system name. { return NULL; } if(FileName[2] != '\\') //Third character should specify root directory. { return NULL; } //A valid file name specified,so try to open it. pFileHandle = __OpenFile(lpThis,FileName,dwAccessMode,dwShareMode); if(NULL == pFileHandle) { if(FILE_OPEN_ALWAYS & dwAccessMode) //Try to create one. { if(CreateNewFile(lpThis,FileName)) //Can create it. { pFileHandle = __OpenFile(lpThis,FileName,dwAccessMode,dwShareMode); //Try to open again. } } } return pFileHandle; } //The target name is not a file name,check if a device name. if((FileName[0] == '\\') && //For device name,the first 2 character should be '\'. (FileName[1] == '\\') && (FileName[2] == '.' )) //The third character should be a dot. { if(FileName[3] != '\\') //The 4th character also must be '\'. { return NULL; } //The name is a device name,try to open ti. return __OpenDevice(lpThis,FileName,dwAccessMode,dwShareMode); } return NULL; }
//Implementation of CreateFile for FAT file system. static __COMMON_OBJECT* FatDeviceOpen(__COMMON_OBJECT* lpDrv, __COMMON_OBJECT* lpDev, __DRCB* lpDrcb) //Open file. { __FAT32_FILE* pFat32File = NULL; __FAT32_FS* pFat32Fs = NULL; __COMMON_OBJECT* pFileDevice = NULL; CHAR FileName[MAX_FILE_NAME_LEN]; __FAT32_SHORTENTRY ShortEntry; DWORD dwDirClus = 0; DWORD dwDirOffset = 0; DWORD dwFlags; static CHAR NameIndex = 0; BOOL bResult = FALSE; CHAR FileDevName[16]; if((NULL == lpDrv) || (NULL == lpDev)) { goto __TERMINAL; } pFat32Fs = (__FAT32_FS*)(((__DEVICE_OBJECT*)lpDev)->lpDevExtension); //strcpy(FileName,(LPSTR)lpDrcb->lpInputBuffer); StrCpy((LPSTR)lpDrcb->lpInputBuffer,FileName); ToCapital(FileName); if(!GetDirEntry(pFat32Fs,&FileName[0],&ShortEntry,&dwDirClus,&dwDirOffset)) { goto __TERMINAL; } if(FILE_ATTR_DIRECTORY & ShortEntry.FileAttributes) //Is a directory. { goto __TERMINAL; } //Create a file object. pFat32File = (__FAT32_FILE*)CREATE_OBJECT(__FAT32_FILE); if(NULL == pFat32File) { goto __TERMINAL; } pFat32File->Attributes = ShortEntry.FileAttributes; pFat32File->dwClusOffset = 0; pFat32File->bInRoot = FALSE; //Caution,not check currently. pFat32File->dwCurrClusNum = ((DWORD)ShortEntry.wFirstClusHi << 16) + (DWORD)ShortEntry.wFirstClusLow; pFat32File->dwCurrPos = 0; pFat32File->dwFileSize = ShortEntry.dwFileSize; pFat32File->dwOpenMode = lpDrcb->dwInputLen; //dwInputLen is used to contain open mode. pFat32File->dwShareMode = lpDrcb->dwOutputLen; //dwOutputLen is used to contain share mode. pFat32File->dwStartClusNum = pFat32File->dwCurrClusNum; pFat32File->pFileSystem = pFat32Fs; pFat32File->pPartition = pFat32Fs->pPartition; //CAUTION!!! pFat32File->dwParentClus = dwDirClus; //Save parent directory's information. pFat32File->dwParentOffset = dwDirOffset; pFat32File->pNext = NULL; pFat32File->pPrev = NULL; //Now insert the file object to file system object's file list. __ENTER_CRITICAL_SECTION(NULL,dwFlags); if(pFat32Fs->pFileList == NULL) //Not any file object in list yet. { pFat32Fs->pFileList = pFat32File; pFat32File->pNext = NULL; pFat32File->pPrev = NULL; } else { pFat32File->pNext = pFat32Fs->pFileList; pFat32Fs->pFileList->pPrev = pFat32File; pFat32File->pPrev = NULL; pFat32Fs->pFileList = pFat32File; } __LEAVE_CRITICAL_SECTION(NULL,dwFlags); //Now create file device object. //strcpy(FileDevName,FAT32_FILE_NAME_BASE); StrCpy(FAT32_FILE_NAME_BASE,FileDevName); FileDevName[13] += NameIndex; NameIndex ++; pFileDevice = (__COMMON_OBJECT*)IOManager.CreateDevice((__COMMON_OBJECT*)&IOManager, FileDevName, DEVICE_TYPE_FILE, 1, //For file,block size is 1. DEVICE_BLOCK_SIZE_ANY, DEVICE_BLOCK_SIZE_ANY, pFat32File, (__DRIVER_OBJECT*)lpDrv); if(NULL == pFileDevice) { goto __TERMINAL; } bResult = TRUE; __TERMINAL: if(!bResult) //The transaction has failed. { if(pFat32File) { RELEASE_OBJECT(pFat32File); } if(pFileDevice) { IOManager.DestroyDevice((__COMMON_OBJECT*)&IOManager, (__DEVICE_OBJECT*)pFileDevice); } return NULL; } return pFileDevice; }
//A helper routine used to load a specified module described by module description. static BOOL LoadModule(__EXTERNAL_MOD_DESC* pModDesc) { CHAR FullPathName[64]; CHAR Msg[128]; HANDLE hFile = NULL; BYTE* pStartAddr = (BYTE*)pModDesc->StartAddress; BOOL bResult = FALSE; DWORD dwReadSize = 0; BOOL bInitResult = FALSE; __MODULE_INIT initRoutine = (__MODULE_INIT)pModDesc->StartAddress; //Initialize routine. strcpy(FullPathName,OS_ROOT_DIR); //Append the root directory before module's file name. strcat(FullPathName,pModDesc->ModFileName); ToCapital(FullPathName); //Now try to open the module. hFile = CreateFile(FullPathName, FILE_ACCESS_READ, 0, NULL); if(NULL == hFile) //Can not open the file. { sprintf(Msg,"Can not open the module named %s.",FullPathName); PrintLine(Msg); goto __TERMINAL; } //Now try to read the module file into memory,each for 8K byte. do{ if(!ReadFile(hFile, 8192, pStartAddr, &dwReadSize)) //Failed to read. { sprintf(Msg,"Can not read from module file %s.",FullPathName); PrintLine(Msg); goto __TERMINAL; } pStartAddr += 8192; //Move to next 8k block. }while(dwReadSize == 8192); //Try to initialize the module. if(pModDesc->dwModAttribute & MOD_ATTR_BIN) //BIN file. { bInitResult = initRoutine(); if(bInitResult) { sprintf(Msg,"Initialize module %s at 0x%X successful.",FullPathName,pModDesc->StartAddress); PrintLine(Msg); } else { sprintf(Msg,"Initialize module %s at 0x%X failed.",FullPathName,pModDesc->StartAddress); PrintLine(Msg); } } if(pModDesc->dwModAttribute & MOD_ATTR_EXE) //Will be implemented later. { } if(pModDesc->dwModAttribute & MOD_ATTR_EXE) //Will be implemented later. { } sprintf(Msg,"Load module %s at 0x%X successful.",FullPathName,pModDesc->StartAddress); PrintLine(Msg); bResult = TRUE; __TERMINAL: if(NULL != hFile) //Should close it. { CloseFile(hFile); } return bResult; }
static DWORD copy(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF HANDLE hSourceFile = NULL; HANDLE hDestinationFile = NULL; char* pBuffer = NULL; DWORD dwReadSize = 0; DWORD dwWriteSize = 0; DWORD dwTotalRead = 0; char srcFullName[MAX_FILE_NAME_LEN]; char dstFullName[MAX_FILE_NAME_LEN]; DWORD dwFileSize = 0; DWORD dwBatSize = 0; if (pCmdObj->byParameterNum < 3) { _hx_printf("Please specify the source and destination file name.\r\n"); goto __TERMINAL; } /* Construct source file's full name. */ if (IsRelative(pCmdObj->Parameter[1])) { /* Append current directory into the relative file name. */ strcpy(srcFullName, FsGlobalData.CurrentDir); strcat(srcFullName, pCmdObj->Parameter[1]); } else { strcpy(srcFullName, pCmdObj->Parameter[1]); } ToCapital(srcFullName); /* Construct destination file's full name. */ if (IsRelative(pCmdObj->Parameter[2])) { /* Append current directory into the relative file name. */ strcpy(dstFullName, FsGlobalData.CurrentDir); strcat(dstFullName, pCmdObj->Parameter[2]); } else { strcpy(dstFullName, pCmdObj->Parameter[2]); } ToCapital(dstFullName); /* Can not copy one file to itself. */ if (0 == strcmp(srcFullName, dstFullName)) { _hx_printf("Can not copy a file to it's self.\r\n"); goto __TERMINAL; } /* Try to open the source file. */ hSourceFile = IOManager.CreateFile((__COMMON_OBJECT*)&IOManager, srcFullName, FILE_ACCESS_READ, 0, NULL); if (NULL == hSourceFile) { _hx_printf("Can not open the source file[%s].\r\n", srcFullName); goto __TERMINAL; } /* Try to open or create the destination file name. */ hDestinationFile = IOManager.CreateFile((__COMMON_OBJECT*)&IOManager, dstFullName, FILE_OPEN_ALWAYS, 0, NULL); if (NULL == hDestinationFile) { _hx_printf("Can not open the target file[%s].\r\n", dstFullName); goto __TERMINAL; } /* Get the source file's size. */ dwFileSize = GetFileSize(hSourceFile, NULL); dwBatSize = dwFileSize / 20; /* Allocate a buffer to hold the file's data. */ #define __TMP_FILE_BUFFSZ (64 * 1024) pBuffer = (char*)_hx_malloc(__TMP_FILE_BUFFSZ); if (NULL == pBuffer) { _hx_printf("Failed to allocate data buffer.\r\n"); goto __TERMINAL; } /* Copy data now. */ do { /* Read the source file. */ if (!IOManager.ReadFile((__COMMON_OBJECT*)&IOManager, hSourceFile, __TMP_FILE_BUFFSZ, pBuffer, &dwReadSize)) { _hx_printf("Can not read the source file.\r\n"); goto __TERMINAL; } /* Write the data block into destination file. */ if (!IOManager.WriteFile((__COMMON_OBJECT*)&IOManager, hDestinationFile, dwReadSize, pBuffer, &dwWriteSize)) { _hx_printf("Failed to write data into target file.\r\n"); goto __TERMINAL; } dwTotalRead += dwReadSize; /* Show out copying progress. */ if (dwBatSize < dwReadSize) { _hx_printf("."); dwBatSize = dwFileSize / 20; } else { dwBatSize -= dwReadSize; } } while (dwReadSize == __TMP_FILE_BUFFSZ); #undef __TMP_FILE_BUFFSZ _hx_printf("\r\n"); _hx_printf("[copy]: %d byte(s) copied.\r\n", dwTotalRead); __TERMINAL: if (NULL != hSourceFile) { IOManager.CloseFile((__COMMON_OBJECT*)&IOManager, hSourceFile); } if (NULL != hDestinationFile) { IOManager.CloseFile((__COMMON_OBJECT*)&IOManager, hDestinationFile); } if (NULL != pBuffer) { _hx_free(pBuffer); } return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }
static DWORD type(__CMD_PARA_OBJ* pCmdObj) { #ifdef __CFG_SYS_DDF HANDLE hFile = NULL; CHAR Buffer[128]; DWORD dwReadSize = 0; DWORD dwTotalRead = 0; DWORD i; CHAR FullName[MAX_FILE_NAME_LEN]; WORD ch = 0x0700; if(pCmdObj->byParameterNum < 2) { PrintLine(" Please specify the file name to be displayed."); goto __TERMINAL; } strcpy(FullName,FsGlobalData.CurrentDir); strcat(FullName,pCmdObj->Parameter[1]); ToCapital(FullName); //Try to open the target file. hFile = IOManager.CreateFile((__COMMON_OBJECT*)&IOManager, FullName, FILE_ACCESS_READ, 0, NULL); if(NULL == hFile) { PrintLine(" Please specify a valid and present file name."); goto __TERMINAL; } //Try to read the target file and display it. GotoHome(); ChangeLine(); do{ if(!IOManager.ReadFile((__COMMON_OBJECT*)&IOManager, hFile, 128, Buffer, &dwReadSize)) { PrintLine(" Can not read the target file."); goto __TERMINAL; } for(i = 0;i < dwReadSize;i ++) { if('\r' == Buffer[i]) { GotoHome(); continue; } if('\n' == Buffer[i]) { ChangeLine(); continue; } ch += Buffer[i]; PrintCh(ch); ch = 0x0700; } dwTotalRead += dwReadSize; }while(dwReadSize == 128); GotoHome(); ChangeLine(); _hx_sprintf(Buffer,"%d byte(s) read.",dwTotalRead); PrintLine(Buffer); __TERMINAL: if(NULL != hFile) { IOManager.CloseFile((__COMMON_OBJECT*)&IOManager, hFile); } return SHELL_CMD_PARSER_SUCCESS;; #else return FS_CMD_FAILED; #endif }