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

📄 support.c

📁 ST5518机顶盒系统文件系统源代码!绝对超值!
💻 C
字号:
/******************************************************************************    File Name   : support.c    Description : Functions generally used during development but not used in                  the final system.******************************************************************************//* Includes ---------------------------------------------------------------- */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <assert.h>#include "stddefs.h"#include "sttbx.h"#include "stavfs.h"#include "root.h"#include "avdevice.h"#include "diskpart.h"#include "dir.h"#include "hal.h"#include "cat.h"#include "file.h"/* Private Types ----------------------------------------------------------- *//* Private Constants ------------------------------------------------------- */static U64 const NullLBA = {0, 0};/* Private Variables ------------------------------------------------------- *//* Private Macros ---------------------------------------------------------- *//* Private Function Prototypes --------------------------------------------- *//* Functions --------------------------------------------------------------- */ST_ErrorCode_t STAVFS_GetNonPartitionedSpace(STAVFS_Handle_t Handle, U64 * SpaceInByte, U64 * Position, U32 * NumPartition){    return (ST_ERROR_FEATURE_NOT_SUPPORTED);}/******************************************************************************Function Name : CreateEmptyPartitionTable  Description : Set all partition_type flags in table to 'unused'   Parameters :******************************************************************************/ST_ErrorCode_t STAVFS_CreateEmptyPartitionTable (ST_DeviceName_t EVTName,                                                 ST_DeviceName_t ATAPIName, U16 UnitNumber, U16 Protocol){    ST_ErrorCode_t Error = ST_NO_ERROR;    stavfs_HAL_t Obj;    Obj.Initialised = FALSE;    Obj.Protocol = Protocol;    Obj.UnitNumber = UnitNumber;    strcpy (Obj.EVTName, EVTName);    strcpy (Obj.ATAPIName, ATAPIName);    if (ST_NO_ERROR != (Error = stavfs_HalInit (&Obj)))    {        STTBX_Print (("Error when opening a HAL device\n"));    }    else    {        char Buffer[DISK_SECTOR_SIZE];        U64 LBA64;        I64_SetValue (0, 0, LBA64);        if (ST_NO_ERROR != (Error = stavfs_HalRead (&Obj, &LBA64, 1, Buffer, FALSE)))        {            STTBX_Print (("Unable to read the partition table\n"));            Error = STAVFS_ERROR_UNREADABLE_DISK;        }        else        {            /* Clear the sector */                        memset(Buffer, 0, DISK_SECTOR_SIZE-2);                        /* Set the magic numbers */                        Buffer[DISK_SECTOR_SIZE-2] = 0X55;            Buffer[DISK_SECTOR_SIZE-1] = 0XAA;            /* Write the table back to the disk */                        if (ST_NO_ERROR != (Error = stavfs_HalWrite (&Obj, &LBA64, 1, Buffer, FALSE)))            {                STTBX_Print (("Unable to write to the partition table\n"));                Error = STAVFS_ERROR_UNWRITABLE_DISK;            }        }        if (ST_NO_ERROR != (Error = stavfs_HalTerm (&Obj)))        {            STTBX_Print (("Failed to closed HAL device\n"));        }    }    return (Error);}/******************************************************************************Function Name : FormatPartition  (originally SetDiskPartition)  Description : Create and format an STAVFS partition.                Partition must already be initialised.                Basically writes an entry in the partition table.   Parameters :******************************************************************************/ST_ErrorCode_t STAVFS_FormatPartition (ST_DeviceName_t Name, U64 SpaceInSectors, U64 Position, U16 SizeOfCluster){    ST_ErrorCode_t   Error  = ST_NO_ERROR;    ST_ErrorCode_t   TmpErr = ST_NO_ERROR;    stavfs_Device_t *Device_p;    if ((Name == NULL) || (NULL == (Device_p = stavfs_GetDevice(Name))))    {        STTBX_Print (("Device name not found\n"));        Error = ST_ERROR_UNKNOWN_DEVICE;    }    else if (Device_p->OpenHandles > 0)    {        STTBX_Print(("Device is open\n"));        Error = ST_ERROR_OPEN_HANDLE;    }    else    {        U64 MinPartitionSize;         /* Lock the driver */        semaphore_wait(&(Device_p->DeviceLock));         /* Initialise the default partition info */         Device_p->RootSectorLBA   = Position;        Device_p->ClusterSize     = SizeOfCluster;        Device_p->NumClusterBlock = 0;        Device_p->RootDirLBA      = NullLBA;        Device_p->DataStartLBA    = NullLBA;        Device_p->DataSize        = NullLBA;        Device_p->RWCache         = NULL;        Device_p->RootDir         = NULL;        Device_p->MCat            = NULL;         I64_SetValue(ROOT_SECTOR_AREA_SIZE +                     2*M_CAT_BLOCK_SIZE    +                     CLUSTER_BLOCK_SIZE(Device_p), 0, MinPartitionSize);         if (Device_p->ClusterSize == 0)        {            STTBX_Print(("Bad Cluster size \n"));            Error = ST_ERROR_BAD_PARAMETER;        }        else if (I64_IsLessThan(SpaceInSectors, MinPartitionSize))        {            STTBX_Print(("Partition to small\n"));            Error = ST_ERROR_BAD_PARAMETER;        }        else        {            /* Update the partition table */             Error = stavfs_SetThePartitionEntry(Device_p, &Position, &SpaceInSectors);             if (Error == ST_NO_ERROR)            {                U64 TmpSize;                 /* Setup the partitions root sector */                U32 AvailableSize   = SpaceInSectors.LSW - ROOT_SECTOR_AREA_SIZE;                U32 MasterCatSize   = M_CAT_BLOCK_SIZE;                U32 NumClusterBlock = 1;                 stavfs_RootSector_t RootSector;                /* Initialise up the root sector variables */                RootSector.Version       = ROOT_PARTITION_VERSION;                RootSector.ClusterSize   = SizeOfCluster;                RootSector.TrackSize     = 255;                RootSector.StateFlags    = 0x0000;                RootSector.Flags         = 0x0000;                RootSector.DataStart     = Position;                RootSector.DataSize      = SpaceInSectors;                RootSector.DirStart      = Position;                 /* Calculate the size of the required Master CAT */                /* This is a recusive computation */                 NumClusterBlock = (AvailableSize - MasterCatSize)/CLUSTER_BLOCK_SIZE(Device_p);                MasterCatSize   = (NumClusterBlock/M_CAT_ENTRIES_PER_BLOCK +2)*M_CAT_BLOCK_SIZE;                 /* Set the File data area */                 INT_I64_AddLit(RootSector.DataStart, MasterCatSize + ROOT_SECTOR_AREA_SIZE,                               RootSector.DataStart);                 INT_I64_SubLit(RootSector.DataSize,  MasterCatSize + ROOT_SECTOR_AREA_SIZE,                               RootSector.DataSize);                 Device_p->DataStartLBA = RootSector.DataStart;                Device_p->DataSize     = RootSector.DataSize;                 INT_I64_DivLit(RootSector.DataSize, CLUSTER_BLOCK_SIZE(Device_p), TmpSize);                Device_p->NumClusterBlock = TmpSize.LSW;                 /* Set the root directory position */                 /* The first Cluster in the middle Cluster Block */                stavfs_GetClusterLBA(Device_p, Device_p->NumClusterBlock/2, 0, &(RootSector.DirStart));                 Device_p->RootDirLBA = RootSector.DirStart;                 /* now write the root sector of the partition */                if (ST_NO_ERROR != stavfs_WriteRootSector (Device_p, &RootSector))                {                    STTBX_Print(("Bad disk write(1)\n"));                    Error = STAVFS_ERROR_UNWRITABLE_DISK;                }            }             /* Format the Master CAT */             if (Error == ST_NO_ERROR)            {                /* Format the Master CAT directory */                Error = stavfs_InitCat(Device_p);            }             /* Format the Cluster Blocks */             if (Error == ST_NO_ERROR)            {                /* Format the File Blocks */                int i;                U64 MaxBlocks;                U32 CRC;                U64 LBA;                 stavfs_LocalCat_t LocalCat;                 for (i = 0; (i < MAX_CLUSTERS_PER_BLOCK); i++)                {                    INT_I64_SetValue((U32)INVALID_LBA,(U32)INVALID_LBA, LocalCat.Cat[i].PrevLBA);                    INT_I64_SetValue((U32)INVALID_LBA,(U32)INVALID_LBA, LocalCat.Cat[i].NextLBA);                }                 LocalCat.FreeClusters = MAX_CLUSTERS_PER_BLOCK;                 /* Set the CRC */                CRC = 0XFFFFFFFF;                stavfs_DoBufferCRC((char*)LocalCat.Cat, sizeof(LocalCat)-sizeof(U64)-sizeof(U32), &CRC);                 /* Copy the CRC with byte swap for big-endian to little-endian */                LocalCat.CRC = (CRC >> 24) | (CRC << 24) | ((CRC >> 8) & 0X0000FF00) | ((CRC << 8) & 0X00FF0000);                 /* Write the CAT's to disk */                INT_I64_DivLit(Device_p->DataSize, CLUSTER_BLOCK_SIZE(Device_p), MaxBlocks);                 /* First Local CAT LBA */                stavfs_GetLocalCatLBA(Device_p, &(Device_p->DataStartLBA), &LBA);                 for (i = 0; (i < MaxBlocks.LSW) && (Error == ST_NO_ERROR); i++)                {                    /* Write the local CAT */                    Error = stavfs_HalWrite(Device_p->HALData, &LBA, LOCAL_CAT_SIZE, (char*)&LocalCat, FALSE);                     /* Next Local CAT LBA */                    INT_I64_AddLit(LBA, CLUSTER_BLOCK_SIZE(Device_p), LBA);                }            }             /* Format the root directory */             if (Error == ST_NO_ERROR)            {                U64 PrevLBA = NullLBA;                U64 FreeLBA = Device_p->RootDirLBA;                 /* Allocate the Cluster */                 stavfs_AllocFreeClusters(Device_p, 1, 1, &PrevLBA, &FreeLBA);                 /* Format the root directory */                 Error = stavfs_InitRootDir(Device_p);            }             /* Close down */             if (ST_NO_ERROR != (TmpErr = stavfs_CloseRootDir(Device_p)))            {                Error = TmpErr;            }             if (ST_NO_ERROR != (TmpErr = stavfs_CloseCat(Device_p)))            {                Error = TmpErr;            }        }         /* Release the driver */        semaphore_signal(&(Device_p->DeviceLock));    }    return (Error);}/******************************************************************************Function Name : DeletePartition  Description : Simply sets the partition type to 0x00   Parameters :******************************************************************************/ST_ErrorCode_t STAVFS_DeletePartition (ST_DeviceName_t Name){    stavfs_DiskPartitionTable_t  PartitionInfo;    ST_ErrorCode_t               Error    = ST_NO_ERROR;    stavfs_Device_t             *Device_p;    if (Name == NULL || (Device_p = stavfs_GetDevice(Name)) == NULL)    {        STTBX_Print (("Device name not found\n"));        Error = ST_ERROR_UNKNOWN_DEVICE;    }    else if (Device_p->OpenHandles > 0)    {        STTBX_Print (("Device is open\n"));        Error = ST_ERROR_OPEN_HANDLE;    }    else    {        /* Lock the driver */        semaphore_wait(&(Device_p->DeviceLock));                if (ST_NO_ERROR != stavfs_ReadPartitionTable(Device_p, &PartitionInfo))        {            STTBX_Print (("Unable to read the partition table\n"));            Error = STAVFS_ERROR_UNREADABLE_DISK;        }        else        {            /* Partition Type to 'unused' */            PartitionInfo.Type = 0X00;                    /* Write the table back to the disk */            if (ST_NO_ERROR != stavfs_WritePartitionTable(Device_p, &PartitionInfo))            {                STTBX_Print (("Unable to write the partition table\n"));                Error = STAVFS_ERROR_UNREADABLE_DISK;            }        }         /* Release the driver */        semaphore_signal(&(Device_p->DeviceLock));    }    return (Error);}/*******************************************************************Function Name : ResetPartition    (originally named FormatPartion)  Description : Resets the partition's file table.   Parameters : *******************************************************************/ST_ErrorCode_t STAVFS_ResetPartition (STAVFS_Handle_t Handle){    ST_ErrorCode_t   Error    = ST_NO_ERROR;    ST_ErrorCode_t   TmpErr   = ST_NO_ERROR;    stavfs_Device_t *Device_p = (stavfs_Device_t *) Handle;    /* Check that the driver is initialised */    if (ST_NO_ERROR != (Error = stavfs_ValidateDevice(Device_p)))    {        STTBX_Print (("Invalid Device\n"));        Error = ST_ERROR_BAD_PARAMETER;    }    else    {        /* Lock the driver */        semaphore_wait(&(Device_p->DeviceLock));                /* Check to see if any files are open */                if (0 != stavfs_CountOpenFilesOnDevice (Device_p))        {            Error = STAVFS_ERROR_FILE_IN_USE;        }                if (Error == ST_NO_ERROR)        {            stavfs_DirEntry_t *Entry_p = NULL;                        /* For each directory entry */                        TmpErr = stavfs_NextEntry(Device_p, NULL, &Entry_p);            while ((NULL != Entry_p) && (TmpErr == ST_NO_ERROR))            {                /* Delete the file */                                if (ST_NO_ERROR != (TmpErr = stavfs_DeleteFileEntry(Device_p, Entry_p)))                {                    Error = TmpErr;                }                                /* Get the next file */                                if (ST_NO_ERROR != (TmpErr = stavfs_NextEntry(Device_p, NULL, &Entry_p)))                {                    Error = TmpErr;                }            }        }                /* Update the directory */         if (ST_NO_ERROR != (TmpErr = stavfs_FlushDir(Device_p, NULL)))        {            Error = TmpErr;        }         /* Release the driver */        semaphore_signal(&(Device_p->DeviceLock));    }    return (Error);}

⌨️ 快捷键说明

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