📄 tmfat32ide.c
字号:
// Forgetaboutit errorCode = TM_FAT32_ERR_INIT_FAILED; } return errorCode;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ide_Read_Sectors//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ide_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 ){ tmErrorCode_t errorCode = TM_OK; tmdlIdeLbaAddress_t lbaAddress; UInt32 nrSectors; UInt16 sector_count; UInt32 dataSize; UInt32 factor; Int32 timeout = 60000; tmFat32_Ide_t *p; TMFAT_ASSERT (d != Null); p = &ide_device[d->unit];// TMFAT_DEBUG2("tmFat32_Ide_Read_Sectors(sector=%llu,count=%u)\n", sector, count);// TMFAT_DEBUG2("tmFat32_Ide_Read_Sectors: lbaMax=%llu,bytesPerSector=%u\n",// d->lbaMax, d->bytesPerSector); // DOS BPB_BytsPerSec may be 512, 1024, 2048, or 4096 factor = d->bytesPerSector / TMBSL_IDE_SECTORSIZE; count *= factor; // Sector count must be in range for the unit if (sector + count > d->lbaMax + 1) return TM_FAT32_ERR_READ; /* initialize pointers */ sector_count = count; lbaAddress = sector * factor; // Read up to SCRATCH_SECTORS at a time while(sector_count) { if(sector_count > SCRATCH_SECTORS) { nrSectors = SCRATCH_SECTORS; sector_count -= SCRATCH_SECTORS; } else { nrSectors = sector_count; sector_count = 0; } // Create the 2 necessary SG lists errorCode = CreateSGLists(p, &p->SGListInput, &p->SGListOutput, nrSectors, pScratchPad); CHECK(CreateSGLists, errorCode); if (p->syncIO == False) { errorCode = tmosalSemAcquire(p->semHandle, &timeout); CHECK(tmosalSemAcquire, errorCode); if (d->eventNotify && (d->eventMask & TM_FAT32_EVENT_READ)) d->eventNotify(d->vol, TM_FAT32_EVENT_READ); errorCode = tmdlIdeRead(p->ideInstance, &lbaAddress, nrSectors, tmdlIdeAsynchronous, p->SGListInput, p->SGListOutput); CHECK(tmdlIdeRead, errorCode); errorCode = tmosalSemAcquire(p->semHandle, &timeout); if (errorCode == TM_OK) { errorCode = tmosalSemRelease(p->semHandle); CHECK(tmosalSemRelease, errorCode); } } else { if (d->eventNotify && (d->eventMask & TM_FAT32_EVENT_READ)) d->eventNotify(d->vol, TM_FAT32_EVENT_READ); errorCode = tmdlIdeRead(p->ideInstance, &lbaAddress, nrSectors, tmdlIdeSynchronous, p->SGListInput, p->SGListOutput); CHECK(tmdlIdeRead, errorCode); } tmutilSGListGetData(p->SGListOutput, 0/*offsetInSGList*/, nrSectors * TMBSL_IDE_SECTORSIZE, &p->pMemorySGListUnit, &dataSize); memcpy(buffer, p->pMemorySGListUnit, dataSize); buffer += dataSize; lbaAddress += nrSectors; // Destroy the 2 necessary SG lists errorCode = DestroySGLists(p, &p->SGListInput, &p->SGListOutput); CHECK(DestroySGLists, errorCode); } return errorCode;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ide_Write_Sectors//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ide_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 ){ tmErrorCode_t errorCode = TM_OK; tmdlIdeLbaAddress_t lbaAddress; UInt32 nrSectors; UInt16 sector_count; UInt32 factor; Int32 timeout = 60000; tmFat32_Ide_t *p; TMFAT_ASSERT (d != Null); TMFAT_ASSERT (buffer != Null); p = &ide_device[d->unit]; // DOS BPB_BytsPerSec may be 512, 1024, 2048, or 4096// TMFAT_DEBUG2("tmFat32_Ide_Write_Sectors(sector=%llu,count=%u)\n", sector, count); factor = d->bytesPerSector / TMBSL_IDE_SECTORSIZE; count *= factor; // Sector count must be in range for the unit if (sector + count > d->lbaMax + 1) return TM_FAT32_ERR_WRITE; sector_count = count; lbaAddress = sector * factor; // Write up to SCRATCH_SECTORS at a time while(sector_count) { if(sector_count > SCRATCH_SECTORS) { nrSectors = SCRATCH_SECTORS; sector_count -= SCRATCH_SECTORS; } else { nrSectors = sector_count; sector_count = 0; } memcpy(pScratchPad, buffer, nrSectors * TMBSL_IDE_SECTORSIZE); // Create the 2 necessary SG lists// TMFAT_DEBUG2("write buffer=0x%X, nrSectors=%d\n", buffer, nrSectors); errorCode = CreateSGLists(p, &p->SGListInput, &p->SGListOutput, nrSectors, pScratchPad); CHECK(CreateSGLists, errorCode); if (p->syncIO == False) { errorCode = tmosalSemAcquire(p->semHandle, &timeout); CHECK(tmosalSemAcquire, errorCode); if (d->eventNotify && (d->eventMask & TM_FAT32_EVENT_WRITE)) d->eventNotify(d->vol, TM_FAT32_EVENT_WRITE); errorCode = tmdlIdeWrite(p->ideInstance, &lbaAddress, nrSectors, tmdlIdeAsynchronous, p->SGListInput, p->SGListOutput); CHECK(tmdlIdeWrite, errorCode); errorCode = tmosalSemAcquire(p->semHandle, &timeout); if (errorCode == TM_OK) { errorCode = tmosalSemRelease(p->semHandle); CHECK(tmosalSemRelease, errorCode); } } else { if (d->eventNotify && (d->eventMask & TM_FAT32_EVENT_WRITE)) d->eventNotify(d->vol, TM_FAT32_EVENT_WRITE); errorCode = tmdlIdeWrite(p->ideInstance, &lbaAddress, nrSectors, tmdlIdeSynchronous, p->SGListInput, p->SGListOutput); CHECK(tmdlIdeWrite, errorCode); } lbaAddress += nrSectors; buffer += nrSectors * TMBSL_IDE_SECTORSIZE; // Destroy the 2 necessary SG lists errorCode = DestroySGLists(p, &p->SGListInput, &p->SGListOutput); CHECK(DestroySGLists, errorCode); } /* return error code */ return errorCode;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ide_Term//// DESCRIPTION://// RETURN: tmErrorCode_t: Status of operation (TM_OK = PASS)//// NOTES://-----------------------------------------------------------------------------//static tmErrorCode_ttmFat32_Ide_Term ( ptmFat32_Dev_t d // IO: Pointer to tmFat32_Dev_t structure ){ tmErrorCode_t status = TM_OK; tmFat32_Ide_t *p; p = &ide_device[d->unit]; p->init_count--; if (p->init_count == 0) { status = tmdlIdeClose(p->ideInstance); CHECK(tmdlIdeClose, status); } return status;}//-----------------------------------------------------------------------------// External Functions://-----------------------------------------------------------------------------////-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ide_Install//// DESCRIPTION: Install IDE tmFat32 device driver//// RETURN: Pointer ptmFat32_DeviceDriver_t. Return Null on failure.//// NOTES: The device bytesPerSector must be set so the Init function// can read from the device.//-----------------------------------------------------------------------------//ptmFat32_DeviceDriver_ttmFat32_Ide_Install ( UInt32 unit // I: Unit number, we support IDE_MAX_UNITS units ){ tmErrorCode_t errorCode; ptmFat32_DeviceDriver_t p = Null; ptmFat32_Ide_t d; if (unit >= IDE_MAX_UNITS) goto bail; d = &ide_device[unit]; if (!d->installed) { // Not installed. p = &d->driver; // Create semaphore in case asynchronous I/O is requested later errorCode = tmosalSemCreate(1, &d->semHandle, tmosalSemCreateFlagNone); if (errorCode != TM_OK) { p = Null; goto bail; } d->common.numUnits = IDE_MAX_UNITS; d->common.unit = unit; d->common.bytesPerSector = TMBSL_IDE_SECTORSIZE; d->common.type = tmFat32_IdeDevice; d->common.removable = False; tmutilSGListMemSize(1, &d->inputSGListSize); d->nrUnitsOutputSGList = (SCRATCH_SECTORS / 16) + 2; tmutilSGListMemSize(d->nrUnitsOutputSGList, &d->outputSListGSize); p->Info = (ptmFat32_Dev_t) &d->common; p->Init = tmFat32_Ide_Init; p->Term = tmFat32_Ide_Term; p->Read = tmFat32_Ide_Read_Sectors; p->Write = tmFat32_Ide_Write_Sectors; d->installed = True; } else { // Already installed p = &d->driver; }bail: TMFAT_DEBUG1("tmFat32_Ide_Install() returning 0x%X\n", p); return p;}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ide_Uninstall//// DESCRIPTION: Uninstall IDE tmFat32 device driver//// RETURN: void//// NOTES://-----------------------------------------------------------------------------//voidtmFat32_Ide_Uninstall ( ptmFat32_DeviceDriver_t p // IO: Pointer to tmFat32_DeviceDriver_t structure ){ UInt32 unit; TMFAT_ASSERT (p != Null); unit = p->Info->unit; if (unit >= IDE_MAX_UNITS) return; if (ide_device[unit].installed && ide_device[unit].init_count == 0) { tmErrorCode_t errorCode; errorCode = tmosalSemDestroy(ide_device[unit].semHandle); if (errorCode != TM_OK) { TMFAT_DEBUG1("tmosalSemDestroy() failed, returned 0x%X\n", errorCode); } p->Info = Null; p->Init = Null; p->Term = Null; p->Read = Null; p->Write = Null; memset(&ide_device[unit], 0, sizeof(tmFat32_Ide_t)); }}//-----------------------------------------------------------------------------// FUNCTION: tmFat32_Ide_GetInfo//// DESCRIPTION: Get information about the IDE tmFat32 device driver//// RETURN: void//// NOTES: Return numUnits (number of units supported). No other// fields are set.//-----------------------------------------------------------------------------//voidtmFat32_Ide_GetInfo ( ptmFat32_Dev_t p // IO: Pointer to tmFat32_Dev_t structure ){ TMFAT_ASSERT (p != Null); p->numUnits = IDE_MAX_UNITS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -