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

📄 stavfs.c

📁 ST5518机顶盒系统文件系统源代码!绝对超值!
💻 C
📖 第 1 页 / 共 2 页
字号:
Function Name : Open  Description : Open the partiton for use - creates a handle for it.                Can only open an AVFS partition (checked by reading partition table)		Reads the root sector and sets partition varables (data start, clusters size etc)		Read the partitions file table.        Parameters :******************************************************************************/ST_ErrorCode_t STAVFS_Open (ST_DeviceName_t Name, STAVFS_OpenParams_t * OpenParams, STAVFS_Handle_t * Handle){    ST_ErrorCode_t Error = ST_NO_ERROR;    stavfs_DiskPartitionTable_t PartitionInfo;    int i;    *Handle = NULL;    if ((Name == NULL) || (Name[0] == 0)) /* zero length */     {        STTBX_Print (("Bad device name\n"));        Error = ST_ERROR_BAD_PARAMETER;    }    else if ((OpenParams == NULL) || (Handle == NULL))    {        STTBX_Print (("NULL OpenParams and/or Handle destination\n"));        Error = ST_ERROR_BAD_PARAMETER;    }    else if (OpenParams->Flags != 0)    {        /* Check the structure flags */        STTBX_Print (("Bad open parameters\n"));        Error = ST_ERROR_FEATURE_NOT_SUPPORTED;    }    else if (!InitialisedLock)    {        Error = ST_ERROR_UNKNOWN_DEVICE;    }    else    {        stavfs_Device_t *Device_p = NULL;                semaphore_wait (&(GlobalDeviceLock));        /* Locate the specified device */        for (i = 0; (i < TABLE_LEN (LocalDevice)) && (Device_p == NULL); i++)        {            if ((LocalDevice[i].DeviceActive) &&                (0 == strncmp (LocalDevice[i].DeviceName, Name, sizeof (LocalDevice[i].DeviceName))))            {                Device_p = LocalDevice + i;            }        }        if (Device_p == NULL)        {            STTBX_Print (("Device name not found\n"));            Error = ST_ERROR_UNKNOWN_DEVICE;        }        else if (Device_p->OpenHandles > 0)        {            STTBX_Print (("Device open [%d]\n", Device_p->OpenHandles));            Error = ST_ERROR_OPEN_HANDLE;        }        else        {            /* re-read the partiton table because a FormatPartition function               may have been called since Intitialising */                    semaphore_wait (&(Device_p->DeviceLock));                        if (ST_NO_ERROR != (Error = stavfs_ReadPartitionTable (Device_p, &PartitionInfo)))            {                STTBX_Print (("Cannot read the partition table when opening\n"));            }            /* Make sure partition is AVFS */            else if (PartitionInfo.Type != AVFS_PARTITION)            {                STTBX_Print (("Cannot open a non AVFS partition\n"));                Error = STAVFS_ERROR_NOT_AVFS_PARTITION;            }            else            {                stavfs_RootSector_t RootSector;                I64_SetValue (PartitionInfo.StartLBA, 0, Device_p->RootSectorLBA);                              /* Read the partitions root sector to set up device */                if (ST_NO_ERROR != (Error = stavfs_ReadRootSector (Device_p, &RootSector)))                {                    STTBX_Print (("Error reading the root sector\n"));                }                else if (RootSector.Version != ROOT_PARTITION_VERSION)                {                    STTBX_Print (("Cannot open a partition with this version file system"));                    Error = STAVFS_ERROR_BAD_AVFS_VERSION;                }                /* Check for a posibly corrupt file system */                else if (RootSector.StateFlags & (ROOT_PARTITION_FLAG_INUSE |                                                  ROOT_PARTITION_FLAG_BAD))                {                    STTBX_Print (("The partition was not correctly shut down\n"));                    Error = STAVFS_ERROR_REQUIRES_FSCK;                }                else                {                    U64 TmpSize;                                RootSector.StateFlags |= ROOT_PARTITION_FLAG_INUSE;                    Device_p->RootDirLBA      = RootSector.DirStart;                    Device_p->ClusterSize     = RootSector.ClusterSize;                    Device_p->DataSize        = RootSector.DataSize;                    Device_p->DataStartLBA    = RootSector.DataStart;                                        INT_I64_DivLit(RootSector.DataSize, CLUSTER_BLOCK_SIZE(Device_p), TmpSize);                    Device_p->NumClusterBlock = TmpSize.LSW;                    /* read in the file table */                    if (ST_NO_ERROR != (Error = stavfs_OpenRootDir(Device_p)))                    {                        STTBX_Print (("Failed to open the root directory.\n"));                    }                    else if (ST_NO_ERROR != (Error = stavfs_OpenCat(Device_p)))                    {                        STTBX_Print (("Error opening the CAT Tables\n"));                    }                    else if (ST_NO_ERROR != (Error = stavfs_WriteRootSector (Device_p, &RootSector)))                    {                        STTBX_Print (("Error writing the root sector\n"));                    }                    if (Error == ST_NO_ERROR)                    {                        /* Everything went OK */                        *Handle = (STAVFS_Handle_t) (Device_p);                        Device_p->OpenHandles++;                    }                    else                    {                        /* We have a problem - close down again */                                                stavfs_CloseRootDir(Device_p);                        stavfs_CloseCat    (Device_p);                                                RootSector.StateFlags &= ~ROOT_PARTITION_FLAG_INUSE;                        stavfs_WriteRootSector (Device_p, &RootSector);                    }                }            }                    semaphore_signal (&(Device_p->DeviceLock));        }        semaphore_signal (&(GlobalDeviceLock));    }    return (Error);}/******************************************************************************Function Name : STAVFS_Term  Description : Terminates the driver   Parameters :******************************************************************************/ST_ErrorCode_t STAVFS_Term (ST_DeviceName_t Name, STAVFS_TermParams_t * TermParams){    ST_ErrorCode_t Error = ST_NO_ERROR;    stavfs_HAL_t *ThisHal = NULL;    int i;    if (Name == NULL || Name[0] == 0 /* zero length */ )    {        STTBX_Print (("Bad device name\n"));        Error = ST_ERROR_BAD_PARAMETER;    }    else if (TermParams == NULL)    {        STTBX_Print (("NULL TermParams\n"));        Error = ST_ERROR_BAD_PARAMETER;    }    else if (TermParams->Flags != 0)    {        /* Check the structure flags */        STTBX_Print (("Bad term parameters\n"));        Error = ST_ERROR_FEATURE_NOT_SUPPORTED;    }    /* Check that we are initialised */    else if (!InitialisedLock)    {        Error = ST_ERROR_UNKNOWN_DEVICE;    }    else    {        stavfs_Device_t *Device_p = NULL;        semaphore_wait (&(GlobalDeviceLock));        /* Locate the specified device */        for (i = 0; (i < TABLE_LEN (LocalDevice)) && (Device_p == NULL); i++)        {            if ((LocalDevice[i].DeviceActive) &&                (0 == strncmp (LocalDevice[i].DeviceName, Name, sizeof (LocalDevice[i].DeviceName))))            {                /* Found the device */                Device_p = LocalDevice + i;            }        }        if (Device_p == NULL)        {            STTBX_Print (("Device not found\n"));            Error = ST_ERROR_UNKNOWN_DEVICE;        }        else        {            /* Check to see if the file system is open (don't acquire the lock              yet because STAVFS_Close needs to do the same) */            if (Device_p->OpenHandles)            {                /* Close the partition before terminating */                if (ST_NO_ERROR != (Error = STAVFS_Close (Device_p)))                {                    STTBX_Print (("Unable to close the partition before terminating\n"));                    /* but still keep going ... */                }            }                        /* Now the file system is closed we can shut down */                        semaphore_wait (&(Device_p->DeviceLock));            Device_p->DeviceActive = FALSE;            if (Device_p->RWCache != NULL)            {                /* Clear the R/W Cache */                Error = stavfs_TermRWCache (Device_p);            }            ThisHal = Device_p->HALData;            Device_p->HALData = NULL;            /* Check if this leaves the HAL unused */            for (i = 0; (i < TABLE_LEN (LocalDevice)) && (LocalDevice[i].HALData != ThisHal); i++)            {                /* All done in the FOR */            }            if (i == TABLE_LEN (LocalDevice))            {                /* The HAL is not being used */                /* Close down the HAL Layer */                if (ST_NO_ERROR != stavfs_HalTerm (ThisHal))                {                    STTBX_Print (("Failed to close HAL device\n"));                    Error = STAVFS_ERROR_UNWRITABLE_DISK;                }            }            semaphore_signal (&(Device_p->DeviceLock));        }        semaphore_signal (&(GlobalDeviceLock));    }    return (Error);}/******************************************************************************Function Name : InitialisedGlobalData  Description : Increment the count of initialised devices and set up if this is                the first device. (Also applies the access lock.)   Parameters :******************************************************************************/static void InitialiseGlobalData (){    int i;    task_lock ();    if (InitialisedLock)    {        task_unlock ();        semaphore_wait (&(GlobalDeviceLock));    }    else    {        /* Initialise the re-entrant lock semaphore */        InitialisedLock = TRUE;        semaphore_init_fifo (&(GlobalDeviceLock), 0);        task_unlock ();        /* Initialise the HAL structures */        for (i = 0; (i < TABLE_LEN (HALData)); i++)        {            HALData[i].Initialised = FALSE;        }        /* Initialise the Device structures */        for (i = 0; (i < TABLE_LEN (LocalDevice)); i++)        {            /* Setup the Device semaphore */                        semaphore_init_fifo (&(LocalDevice[i].DeviceLock), 1);                        LocalDevice[i].DeviceActive = FALSE;            LocalDevice[i].HALData = NULL;        }        /* Initialise the file handle list */        stavfs_InitFileTable ();    }}/******************************************************************************Function Name : stavfs_GetDevice  Description : Find the named device.        Parameters :******************************************************************************/stavfs_Device_t *stavfs_GetDevice(ST_DeviceName_t Name){    int i;    stavfs_Device_t *Device_p = NULL;    assert (NULL != Name);        if (InitialisedLock)    {        semaphore_wait (&(GlobalDeviceLock));        /* Locate the specified device */        for (i = 0; (i < TABLE_LEN (LocalDevice)) && (Device_p == NULL); i++)        {            if ((LocalDevice[i].DeviceActive) &&                (0 == strncmp (LocalDevice[i].DeviceName, Name, sizeof (LocalDevice[i].DeviceName))))            {                Device_p = LocalDevice + i;            }        }                semaphore_signal (&(GlobalDeviceLock));    }        return (Device_p);}/******************************************************************************Function Name : stavfs_ValidateDevice  Description : Check that the handle is valid.        Parameters :******************************************************************************/ST_ErrorCode_t stavfs_ValidateDevice(stavfs_Device_t *Device_p){    ST_ErrorCode_t Error = ST_NO_ERROR;        if (!InitialisedLock         ||        (Device_p < LocalDevice) || (LocalDevice+TABLE_LEN(LocalDevice) <= Device_p) ||        !Device_p->DeviceActive  || (Device_p->OpenHandles == 0))    {        Error = ST_ERROR_INVALID_HANDLE;    }        return (Error);}/************************************************************************* * * This code is used for crash testing only * * DO NOT COMPILE WITH "STAVFS_CRASH_TEST" DEFINED FOR A LIVE SYSTEM * *************************************************************************/#ifdef STAVFS_CRASH_TEST/******************************************************************************Function Name : stavfs_CrashTheDriver  Description : Crash the driver cleanly.        Parameters :******************************************************************************/void stavfs_CrashTheDriver(){    stavfs_Device_t *Device_p;        stavfs_HalTerm(HALData);    memset(HALData, sizeof(HALData), 0);        for (Device_p = LocalDevice; (Device_p < LocalDevice+TABLE_LEN(LocalDevice)); Device_p++)    {        /* Free allocated memory, then scrub the device structure */         if (Device_p->DeviceActive)        {            memory_deallocate(Device_p->MemoryPartition, Device_p->RWCache);            memory_deallocate(Device_p->MemoryPartition, Device_p->RootDir);            memory_deallocate(Device_p->MemoryPartition, Device_p->MCat);        }                /* deleting the semaphore is a convenient way to get the count          re-initialised correctly; note we must clear InitialisedLock below */                  semaphore_delete(&(Device_p->DeviceLock));                memset(Device_p, sizeof(*Device_p), 0);    }            semaphore_delete(&(GlobalDeviceLock));    InitialisedLock = FALSE;        /* With no other threads working, this will lead to a clean      re-initialisation very similar to the power-on state */}#endif

⌨️ 快捷键说明

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