⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs.c

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 C
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************//// FS.C - Generic file system translation layer.//// Copyright (c) 1999,2000,2001 Cirrus Logic, Inc.////****************************************************************************#include "globals.h"//****************************************************************************//// The number of file system drivers which are supported by the file system// layer.////****************************************************************************#define NUMFS                           2//****************************************************************************//// An array of entry points for the file system drivers supported by the file// system layer.////****************************************************************************static unsigned long (* const pfnIoctl[NUMFS])(unsigned char *pucScratch,                                               unsigned char *pucWriteBuffer,                                               unsigned long ulIoctl,                                               unsigned long ulInstance,                                               unsigned long ulParam1,                                               unsigned long ulParam2) ={    //    // The on-board NAND FLASH file system.    //    NANDIoctl,    //    // The removeable SmartMedia card.    //    SMIoctl,    //    // The two MMC cards.    //    //MMCIoctl1,    //MMCIoctl2,};//****************************************************************************//// The following structure contains the persistent state of the generic// file system layer.////****************************************************************************static struct{    //    // The number of files that are currently opened.    //    unsigned short usNumFiles;    //    // The number of directories that are currently opened.    //    unsigned short usNumDirs;    //    // A scratch buffer to be used during read or write operations by the file    // system drivers.  This is shared between all file system drivers and no    // driver should assume that the data they store in this buffer will be    // maintained between calls to the file system driver.    //    unsigned char ucScratch[512];    //    // A pointer to the additional scratch buffer to be used during write    // operation by the file system drivers.  The size of this buffer is 512    // bytes.  This is also shared between all file system drivers with the    // same assumptions about data persistence.    //    unsigned char *pucWriteScratch;} sFS;//****************************************************************************//// FSInit initializes the generic file system layer.////****************************************************************************voidFSInit(void){    unsigned long ulDrive;    //    // Initialize each "drive" in the system.    //    for(ulDrive = 0; ulDrive < NUMFS; ulDrive++)    {        (pfnIoctl[ulDrive])(sFS.ucScratch, sFS.pucWriteScratch, IOCTL_FS_INIT,                            0, 0, 0);    }    //    // Initially, there is not a file or directory opened.    //    sFS.usNumFiles = 0;    sFS.usNumDirs = 0;}//****************************************************************************//// FSGetScratch returns the address of the scratch buffer used by the file// system for read and write operations.  The contents of this buffer are not// guaranteed to remain constant across calls to the file system, but can be// used as the read buffer for a call to FSRead().////****************************************************************************unsigned char *FSGetScratch(void){    //    // Return the address of the scratch buffer.    //    return(sFS.ucScratch);}//****************************************************************************//// FSSetWriteScratch sets the address of the scratch buffer to be used during// write operations.////****************************************************************************voidFSSetWriteScratch(unsigned char *pucWriteScratch){    //    // Remember the address of the write scratch buffer.    //    sFS.pucWriteScratch = pucWriteScratch;}//****************************************************************************//// FSNumDrives returns the number of drives supported by the file system// layer.////****************************************************************************unsigned longFSNumDrives(void){    //    // Return the number of drives we support.    //    return(NUMFS);}//****************************************************************************//// FSGetMediaID returns the media unique ID for the given drive, if it exists.////****************************************************************************unsigned longFSGetMediaID(unsigned long ulDrive, unsigned char *pucMediaIDBuffer,             unsigned long *pulLength){    unsigned long ulRet;    //    // If the drive number is invalid, then return a failure.    //    if(ulDrive >= NUMFS)    {        return(0);    }    //    // Pass the get media ID request to the entry point for the specified    // drive.    //    ulRet = (pfnIoctl[ulDrive])(sFS.ucScratch, sFS.pucWriteScratch,                                IOCTL_FS_GETMEDIAID, 0,                                (unsigned long)pucMediaIDBuffer,                                (unsigned long)pulLength);    //    // If the request failed, then we need to return the device's unique ID.    //    if(ulRet == 0)    {#ifdef SUPPORT_RIGHTS_PD        static const unsigned char ucID[16] = { 0x12, 0x34, 0x56, 0x70 };        //        // This is the device ID to which the sample content in the Rights/PD        // distribution was bound.  First, make sure the ID buffer is large        // enough.        //        if(*pulLength < (16 * 8))        {            return(0);        }        //        // Return our device ID.        //        memcpy(pucMediaIDBuffer, ucID, 16);        *pulLength = 16 * 8;#else#ifdef SUPPORT_WMA        //        // This is the device ID to which the sample content in the WMA        // distribution was bound.  First, make sure the ID buffer is large        // enough.        //        if(*pulLength < (20 * 8))        {            return(0);        }        //        // Return the Microsoft generated ID.        //        memcpy(pucMediaIDBuffer, "        M 3T10050095", 20);        *pulLength = 20 * 8;#else        //        // We have no way of specifying a unique device ID at this time.        //        return(0);#endif#endif    }    //    // Success.    //    return(1);}//****************************************************************************//// FSGetMediaName returns the name of the specified drive.////****************************************************************************unsigned longFSGetMediaName(unsigned long ulDrive, unsigned short *pusName,               unsigned long *pulLength){    //    // If the drive number is invalid, then return a failure.    //    if(ulDrive >= NUMFS)    {        return(0);    }    //    // Pass the get media ID request to the entry point for the specified    // drive.    //    return((pfnIoctl[ulDrive])(sFS.ucScratch, sFS.pucWriteScratch,                               IOCTL_FS_GETMEDIANAME, 0,                               (unsigned long)pusName,                               (unsigned long)pulLength));}//****************************************************************************//// FSOpen opens or creates the specified file on the specified drive.////****************************************************************************unsigned longFSOpen(tFile *pFile, unsigned long ulDrive, const unsigned short *pusFileName,       unsigned long ulFlags){    unsigned long ulRet;    //    // If the drive number is invalid, then return a failure.    //    if(ulDrive >= NUMFS)    {        return(0);    }    //    // Pass the open request to the entry point for the specified drive.    //    ulRet = (pfnIoctl[ulDrive])(sFS.ucScratch, sFS.pucWriteScratch,                                IOCTL_FS_OPEN,                                (unsigned long)&pFile->uInternal,                                (unsigned long)pusFileName, ulFlags);    //    // If the open succeeded, then increment the count of opened files.    //    if(ulRet)    {        //        // Remember the drive on which this file resides.        //        pFile->ucDrive = ulDrive;        //        // Remember the flags used to open this file.        //        pFile->ucFlags = ulFlags;#ifdef SUPPORT_RIGHTS_PD        //        // The size of the header defaults to zero.        //        pFile->ulHeaderSize = 0;        //        // See if we should decrypt this file (if it is encrypted).        //        if(ulFlags == (FS_OPEN_READ | FS_OPEN_DECRYPT))        {            //            // Have the Rights/PD library open this file.            //            ulRet = RPDLib_Open(pFile);            if(ulRet != 0)            {                //                // This is a DigiFile which is bound to a different device,                // so we can not open this file.  Close the Rights/PD library.                //                RPDLib_Close(pFile);                //                // Close this file.                //                (pfnIoctl[pFile->ucDrive])(sFS.ucScratch, sFS.pucWriteScratch,                                           IOCTL_FS_CLOSE,                                           (unsigned long)&pFile->uInternal,                                           ulFlags, 0);                //                // Return a failure.                //                return(0);            }            //            // See if this is a DigiFile.            //            if(pFile->ulHeaderSize == 0)            {                //                // This is not a DigiFile, so close the Rights/PD library.                //                RPDLib_Close(pFile);            }            else            {                //                // Seek to the beginning of the file data.                //                FSSeek(pFile, 0);            }            //            // The file has been successfully opened.            //            ulRet = 1;        }#endif        //        // Increment the count of opened files.        //        sFS.usNumFiles++;    }    //    // Return the result to the caller.    //    return(ulRet);}//****************************************************************************//// FSCreate creates the specified file with the given size on the specified// drive.////****************************************************************************unsigned longFSCreate(tFile *pFile, unsigned long ulDrive,         const unsigned short *pusFileName, unsigned long ulFileLength){    unsigned long ulRet;    //    // If the drive number is invalid, then return a failure.    //    if(ulDrive >= NUMFS)    {        return(0);    }    //    // Pass the create request to the entry point for the specified drive.    //    ulRet = (pfnIoctl[ulDrive])(sFS.ucScratch, sFS.pucWriteScratch,                                IOCTL_FS_CREATE,                                (unsigned long)&pFile->uInternal,                                (unsigned long)pusFileName, ulFileLength);    //    // If the create succeeded, then increment the count of opened files.    //    if(ulRet)    {#ifdef SUPPORT_RIGHTS_PD        //        // The size of the header is always zero.        //        pFile->ulHeaderSize = 0;#endif        //        // Remember the drive on which this file resides.        //        pFile->ucDrive = ulDrive;        //        // Remember the flags used to open this file.        //        pFile->ucFlags = FS_OPEN_READ | FS_OPEN_WRITE | FS_OPEN_CREATE;        //        // Increment the count of opened files.        //        sFS.usNumFiles++;    }    //    // Return the result to the caller.    //    return(ulRet);}//****************************************************************************//// FSRead reads data from the opened file.////****************************************************************************unsigned longFSRead(tFile *pFile, void *pvBuffer, unsigned long ulNumBytes){    unsigned long ulCount;#ifdef SUPPORT_RIGHTS_PD    unsigned long ulPos = 0;#endif    //    // If the file was not opened for reading, then return an error.    //    if(!(pFile->ucFlags & FS_OPEN_READ))    {        return(0);    }#ifdef SUPPORT_RIGHTS_PD    //    // Get the current file pointer if this is a DigiFile.    //    if(pFile->ulHeaderSize != 0)    {        ulPos = FSTell(pFile);    }#endif    //    // Pass the read request to the entry point for the specified drive.    //    ulCount = (pfnIoctl[pFile->ucDrive])(sFS.ucScratch, sFS.pucWriteScratch,                                         IOCTL_FS_READ,                                         (unsigned long)&pFile->uInternal,                                         (unsigned long)pvBuffer, ulNumBytes);#ifdef SUPPORT_RIGHTS_PD    //    // If this is a DigiFile, then decrypt the bytes that were just read.    //    if((pFile->ulHeaderSize != 0) && (ulCount != 0))    {        ulCount = RPDLib_Decrypt(pFile, (unsigned char *)pvBuffer, ulCount,                                 ulPos);    }#endif    //    // Return the number of bytes read.    //    return(ulCount);}//****************************************************************************//// FSReadBS reads data from the opened file, performing a byte-swap on each// 32-bit word in the read data.////****************************************************************************unsigned longFSReadBS(tFile *pFile, void *pvBuffer, unsigned long ulNumBytes){#ifdef SUPPORT_RIGHTS_PD    unsigned long *pulPtr;    unsigned long ulCount, ulIdx, ulData;#endif    //

⌨️ 快捷键说明

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