📄 mmc_init.c
字号:
#include <stdio.h>#include <rdcf2.h>#include <stdio.h>#include <types.h>#include <string.h>#include <mmc_hardware.h>#include <sysdefs.h>#ifdef HAS_MMC#define SIZEOF_DIR_ENTRY 32extern struct rdcf fcbs [MaxFileBuffers];/******************************************************************* * structure defs*******************************************************************/struct PARTITION_ENTRY { // single partition entry for MMC. uchar BootActive; uint FirstPartitionSector:24; uchar FileSystemDescriptor; uint LastPartitionSector:24; ulong FirstSectorPosition; ulong NumberSectorsInPartition;} __attribute__ ((packed));struct PARTITION_TABLE { uchar BootCode[446]; struct PARTITION_ENTRY PartitionEntry0; struct PARTITION_ENTRY PartitionEntry1; struct PARTITION_ENTRY PartitionEntry2; struct PARTITION_ENTRY PartitionEntry3; ushort Signature;} __attribute__ ((packed));struct BOOTSECTOR_ENTRY { // this description is for an MMC boot block not for MSDOS per se. uint JumpCommand:24; char OEM_NAME[8]; ushort BytesPerSector; uchar SectorsPerCluster; ushort ReservedSectors; uchar NumberOfFATs; ushort NumberRootDirEntries; ushort NumberOfSectorsOnMedia; uchar MediaDescriptor; ushort SectorsPerFAT; ushort SectorsPerTrack; ushort NumberOfHeads; ulong NumberOfHiddenSectors; ulong NumberOfTotalSectors; uchar DriveNumber; uchar __RESERVED__; uchar ExtendedBootSignature; ulong VolumeID; char VolumeLabel[11]; char FileSystemType[8]; uchar LoadProgramCode[448]; ushort Signature;} __attribute__ ((packed));struct DATABUFFER { uchar data [RDCF_SECTOR_SIZE];}; // re-use buffer area, don't waste RAM.union MMC_IO_BUFFER { struct BOOTSECTOR_ENTRY BootBlock; struct PARTITION_TABLE PartitionTable;};/******************************************************************* * structure vars*******************************************************************/union MMC_IO_BUFFER IoBuffer; // scratch area for transitory file i/o.DRIVE_DESCRIPTION DriveDesc;/******************************************************************* * routines specific to rdcf2 operation.*******************************************************************/static void init_rdcf2_struct(void){int i; // remember to do the reserved fcb as well. for (i=0; i<MaxFileBuffers+1; i++) { fcbs[i].ReadSector = mmcReadBlock; fcbs[i].WriteSector = mmcWriteBlock; fcbs[i].BufferInUse = False; }}/******************************************************************** * initMMCdrive * return True if error, False if everything went well.********************************************************************/static void CollectDataAboutDrive (void){ // How large are the FAT tables? DriveDesc.SectorsPerFAT = IoBuffer.BootBlock.SectorsPerFAT; // Important info to decode FAT entries into sectors. DriveDesc.SectorsPerCluster = IoBuffer.BootBlock.SectorsPerCluster; // First File Allocation Table. DriveDesc.FirstFatSector = DriveDesc.SectorZero + IoBuffer.BootBlock.ReservedSectors; // "backup" copy of the First FAT. usually to undelete files. if (IoBuffer.BootBlock.NumberOfFATs > 1) { DriveDesc.SecondFatSector = DriveDesc.FirstFatSector + DriveDesc.SectorsPerFAT; } else { // nope, only one FAT... DriveDesc.SecondFatSector = -1; } // Where does the actual drive data area start? if (DriveDesc.SecondFatSector == -1) { // only one FAT, so data follows first FAT. DriveDesc.RootDirSector = DriveDesc.FirstFatSector + IoBuffer.BootBlock.SectorsPerFAT; } else { // data follows both FAT tables. DriveDesc.RootDirSector = DriveDesc.FirstFatSector + (2 * IoBuffer.BootBlock.SectorsPerFAT); } // How many entries can be in the root directory? DriveDesc.NumberRootDirEntries = IoBuffer.BootBlock.NumberRootDirEntries; // where does cluster 2 begin? DriveDesc.DataStartSector = DriveDesc.RootDirSector + (DriveDesc.NumberRootDirEntries * SIZEOF_DIR_ENTRY) / RDCF_SECTOR_SIZE; // where does the partition end? DriveDesc.MaxDataSector = DriveDesc.SectorZero + ((IoBuffer.BootBlock.NumberOfSectorsOnMedia) ? IoBuffer.BootBlock.NumberOfSectorsOnMedia : IoBuffer.BootBlock.NumberOfTotalSectors);}bool initMMCdrive (void){ // access drive and collect structure info. // see if we have a card inserted. if (IO0PIN & MMC_DETECT_BIT) { // drive disappeared! DriveDesc.IsValid = False; return True; } if (DriveDesc.IsValid) { // we already know about this drive. return False; } if (mmcInit() == False) return True; // init fcbs. init_rdcf2_struct(); // get the partition table to find the boot block. if (mmcReadBlock(0, (uchar *) &IoBuffer)) return True; // validate. if (IoBuffer.PartitionTable.Signature != 0xaa55) return True; // get the boot block now. DriveDesc.SectorZero = IoBuffer.PartitionTable.PartitionEntry0.FirstSectorPosition; if (mmcReadBlock(DriveDesc.SectorZero, (uchar *) &IoBuffer)) return True; // validate. if (IoBuffer.BootBlock.Signature != 0xaa55) return True; // looks good, make a note of where stuff starts at. CollectDataAboutDrive(); // pass all tests before validating drive. DriveDesc.IsValid = True; return False;}#endif // HAS_MMC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -