📄 tmfat32ram.c
字号:
/* * Copyright (C) 2003 Koninklijke Philips Electronics N.V., * All Rights Reserved. * * This source code and any compilation or derivative thereof is the * proprietary information of Koninklijke Philips Electronics N.V. * and is confidential in nature. * Under no circumstances is this software to be exposed to or placed * under an Open Source License of any type without the expressed * written permission of Koninklijke Philips Electronics N.V. * *----------------------------------------------------------*//*! * \file tmFat32Ram.c * * This file implements the tmFat32 device driver for a * RAM disk. * *//*----------------------------------------------------------- * * %version: ds08#9 % * instance: DS_4 * %date_created: Sun Nov 12 15:07:20 2006 % * */ //-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Standard include files://-----------------------------------------------------------------------------//#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#if USE_GENERIC_C#include <tmFat32Generic.h>#else#include <sys/syslimits.h>#include <tmStdLib.h>#include <tmNxTypes.h>#include <tmDbg.h>#endif//-----------------------------------------------------------------------------// Project include files://-----------------------------------------------------------------------------//#include <tmFat32.h>#include <tmFat32Debug.h>#include <tmFat32Device.h>#include <tmFat32Ram.h>//-----------------------------------------------------------------------------// Types and defines://-----------------------------------------------------------------------------#define NUM_RAM_DISK 2 // To demonstrate that we can support more than // one device of a given tmFat32_DeviceType_t#define KEEP_RAM_DISK True // If True don't de-allocate storage and contents // of a RAMDISK after it has been terminated // or uninstalled.//-----------------------------------------------------------------------------// Global data://-----------------------------------------------------------------------------//static tmFat32_Ram_t ram_device[NUM_RAM_DISK];// Set default RAMDISK sizes in bytes. Make it at least 4.1 MB so we can format it// for FAT16. Size should be in multiples of TMFAT_RAM_SECTORSIZE.static UInt32 ram_size[NUM_RAM_DISK] = { (5 * 1024 * 1024), (8 * 1024 * 1024) };//-----------------------------------------------------------------------------// Internal Prototypes://-----------------------------------------------------------------------------//static tmErrorCode_t tmFat32_Ram_Init (ptmFat32_Dev_t p);static tmErrorCode_t tmFat32_Ram_Term (ptmFat32_Dev_t p);static tmErrorCode_t tmFat32_Ram_Read_Sectors ( ptmFat32_Dev_t p, // I: Pointer to tmFat32_Dev_t structure UInt64 sector, // I: Starting sector number UInt32 count, // I: Number of sectors UInt8 * buffer // 0: Pointer to buffer to receive data );static tmErrorCode_t tmFat32_Ram_Write_Sectors ( ptmFat32_Dev_t p, // I: Pointer to tmFat32_Dev_t structure UInt64 sector, // I: Starting sector number UInt32 count, // I: Number of sectors UInt8 * buffer // I: Pointer to buffer containing data );//-----------------------------------------------------------------------------// Internal Functions//-----------------------------------------------------------------------------////-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_Init//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ram_Init ( ptmFat32_Dev_t d // IO: Pointer to tmFat32_Dev_t structure ){ ptmFat32_Ram_t r; UInt32 sectors; TMFAT_DEBUG1("tmFat32_Ram_Init(d=0x%X)\n", d); TMFAT_ASSERT (d != Null); r = &ram_device[d->unit]; if (r->keepAlive && r->installed && r->disk) return TM_OK; if (d->unit >= NUM_RAM_DISK) return TM_FAT32_ERR_BAD_PARAMETER; sectors = ram_size[d->unit] / TMFAT_RAM_SECTORSIZE; // Allocate storage for RAM disk printf("Simulating RAMDISK for %lu sectors (%lu bytes).\n", sectors, sectors * TMFAT_RAM_SECTORSIZE); r->disk = calloc(sectors * TMFAT_RAM_SECTORSIZE, 1); if (r->disk == Null) { TMFAT_DEBUG1("ERROR: Failed to allocate %d bytes\n", sectors * TMFAT_RAM_SECTORSIZE); return TM_FAT32_ERR_OUT_OF_MEMORY; } d->lbaMax = sectors - 1; return TM_OK;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_Read_Sectors//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ram_Read_Sectors ( ptmFat32_Dev_t d, // I: Pointer to tmFat32_Dev_t structure UInt64 sector, // I: Starting sector number UInt32 count, // I: Number of sectors UInt8 * buffer // 0: Pointer to buffer to receive data ){ UInt32 factor; UInt8 *disk; TMFAT_ASSERT (d != Null); disk = ram_device[d->unit].disk; // DOS BPB_BytsPerSec may be 512, 1024, 2048, or 4096 factor = d->bytesPerSector / TMFAT_RAM_SECTORSIZE; count *= factor; if (sector + count > d->lbaMax + 1) { TMFAT_DEBUG3("tmFat32_Ram_Read_Sectors: sector %llu + count %u > %llu\n", sector, count, d->lbaMax + 1); return TM_FAT32_ERR_READ; } if (d->eventNotify && (d->eventMask & TM_FAT32_EVENT_READ)) d->eventNotify(d->vol, TM_FAT32_EVENT_READ); memcpy(buffer, &disk[sector * TMFAT_RAM_SECTORSIZE], count * TMFAT_RAM_SECTORSIZE); return TM_OK;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_Write_Sectors//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ram_Write_Sectors ( ptmFat32_Dev_t d, // I: Pointer to tmFat32_Dev_t structure UInt64 sector, // I: Starting sector number UInt32 count, // I: Number of sectors UInt8 * buffer // I: Pointer to buffer containing data ){ UInt32 factor; UInt8 *disk; TMFAT_ASSERT (d != Null); disk = ram_device[d->unit].disk; // DOS BPB_BytsPerSec may be 512, 1024, 2048, or 4096 factor = d->bytesPerSector / TMFAT_RAM_SECTORSIZE; count *= factor; if (sector + count > d->lbaMax + 1) { TMFAT_DEBUG3("tmFat32_Ram_Write_Sectors: sector %llu + count %u > %llu\n", sector, count, d->lbaMax + 1); return TM_FAT32_ERR_WRITE; } if (d->eventNotify && (d->eventMask & TM_FAT32_EVENT_WRITE)) d->eventNotify(d->vol, TM_FAT32_EVENT_WRITE); memcpy(&disk[sector * TMFAT_RAM_SECTORSIZE], buffer, count * TMFAT_RAM_SECTORSIZE); return TM_OK;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_Term//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ram_Term ( ptmFat32_Dev_t d // IO: Pointer to tmFat32_Dev_t structure ){ ptmFat32_Ram_t r; TMFAT_ASSERT (d != Null); r = &ram_device[d->unit]; if (!r->keepAlive) { // Don't retain the disk contents free(r->disk); memset(r, 0, sizeof(tmFat32_Ram_t)); } return TM_OK;}//-----------------------------------------------------------------------------// External Functions://-----------------------------------------------------------------------------////-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_SetSize//// DESCRIPTION: Set tmFat32 RAM disk size//// RETURN: Return TM_OK on success.//// NOTES: The size will ceiling'ed to a multiple of TMFAT_RAM_SECTORSIZE//-----------------------------------------------------------------------------//tmErrorCode_ttmFat32_Ram_SetSize ( UInt32 unit, // I: Unit number UInt32 size // I: Size in bytes ){ if (unit >= NUM_RAM_DISK) return TM_FAT32_ERR_BAD_PARAMETER; // Can't change size while we're using it if (ram_device[unit].disk) return TM_FAT32_ERR_BAD_PARAMETER; size = (size + TMFAT_RAM_SECTORSIZE - 1) / TMFAT_RAM_SECTORSIZE; ram_size[unit] = size * TMFAT_RAM_SECTORSIZE; return TM_OK;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_Install//// DESCRIPTION: Install RAM disk tmFat32 device driver//// RETURN: Pointer ptmFat32_DeviceDriver_t. Return Null on failure.//// NOTES://-----------------------------------------------------------------------------//ptmFat32_DeviceDriver_ttmFat32_Ram_Install ( UInt32 unit // I: Unit number, we support NUM_RAM_DISK units ){ ptmFat32_DeviceDriver_t p = Null; ptmFat32_Ram_t r; if (unit >= NUM_RAM_DISK) goto bail; r = &ram_device[unit]; if (!r->installed) { // Not installed. We don't allocate any memory yet. p = &r->driver; r->common.numUnits = NUM_RAM_DISK; r->common.unit = unit; r->common.bytesPerSector = TMFAT_RAM_SECTORSIZE; r->common.type = tmFat32_RamDevice; r->common.removable = False; p->Info = (ptmFat32_Dev_t) &r->common; p->Init = tmFat32_Ram_Init; p->Term = tmFat32_Ram_Term; p->Read = tmFat32_Ram_Read_Sectors; p->Write = tmFat32_Ram_Write_Sectors; r->keepAlive = KEEP_RAM_DISK; r->installed = True; } else { // Already installed if (r->keepAlive) p = &r->driver; }bail: TMFAT_DEBUG1("tmFat32_Ram_Install() returning 0x%X\n", p); return p;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_Uninstall//// DESCRIPTION: Uninstall RAM disk tmFat32 device driver//// RETURN: void//// NOTES://-----------------------------------------------------------------------------//voidtmFat32_Ram_Uninstall ( ptmFat32_DeviceDriver_t p // IO: Pointer to tmFat32_DeviceDriver_t structure ){ UInt32 unit; TMFAT_ASSERT (p != Null); unit = p->Info->unit; if (unit >= NUM_RAM_DISK) return; // If device is installed and don't keep its contents clear // everything. Otherwise, leave it alone. if (!ram_device[unit].keepAlive && ram_device[unit].installed) { memset(&ram_device[unit], 0, sizeof(tmFat32_Ram_t)); p->Info = Null; p->Init = Null; p->Term = Null; p->Read = Null; p->Write = Null; }}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ram_GetInfo//// DESCRIPTION: Get information about the RAM disk tmFat32 device driver//// RETURN: void//// NOTES: Return numUnits (number of units supported). No other// fields are set.//-----------------------------------------------------------------------------//voidtmFat32_Ram_GetInfo ( ptmFat32_Dev_t p // IO: Pointer to tmFat32_Dev_t structure ){ TMFAT_ASSERT (p != Null); p->numUnits = NUM_RAM_DISK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -