00001
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include "type.h"
00009 #include "rdcf2.h"
00010 #include "ssp.h"
00011 #include "sdcard_driver.h"
00012 #include "drive.h"
00013
00014
00015 #define SIZEOF_DIR_ENTRY 32
00016
00017 extern struct rdcf fcbs [MaxFileBuffers];
00018
00019
00020
00021
00022
00023 struct PARTITION_ENTRY
00024 {
00025
00026 uint8 BootActive;
00027 uint FirstPartitionSector:24;
00028 uint8 FileSystemDescriptor;
00029 uint LastPartitionSector:24;
00030 uint32 FirstSectorPosition;
00031 uint32 NumberSectorsInPartition;
00032 } __attribute__ ((packed));
00033
00034
00035 struct PARTITION_TABLE
00036 {
00037 uint8 BootCode[446];
00038 struct PARTITION_ENTRY PartitionEntry0;
00039 struct PARTITION_ENTRY PartitionEntry1;
00040 struct PARTITION_ENTRY PartitionEntry2;
00041 struct PARTITION_ENTRY PartitionEntry3;
00042 uint16 Signature;
00043 } __attribute__ ((packed));
00044
00045
00046 struct BOOTSECTOR_ENTRY
00047 {
00048
00049 uint JumpCommand:24;
00050 int8 OEM_NAME[8];
00051 uint16 BytesPerSector;
00052 uint8 SectorsPerCluster;
00053 uint16 ReservedSectors;
00054 uint8 NumberOfFATs;
00055 uint16 NumberRootDirEntries;
00056 uint16 NumberOfSectorsOnMedia;
00057 uint8 MediaDescriptor;
00058 uint16 SectorsPerFAT;
00059 uint16 SectorsPerTrack;
00060 uint16 NumberOfHeads;
00061 uint32 NumberOfHiddenSectors;
00062 uint32 NumberOfTotalSectors;
00063 uint8 DriveNumber;
00064 uint8 __RESERVED__;
00065 uint8 ExtendedBootSignature;
00066 uint32 VolumeID;
00067 int8 VolumeLabel[11];
00068 int8 FileSystemType[8];
00069 uint8 LoadProgramCode[448];
00070 uint16 Signature;
00071 } __attribute__ ((packed));
00072
00073 struct DATABUFFER
00074 {
00075 uint8 data [RDCF_SECTOR_SIZE];
00076 };
00077
00078
00079 union MMC_IO_BUFFER
00080 {
00081 struct BOOTSECTOR_ENTRY BootBlock;
00082 struct PARTITION_TABLE PartitionTable;
00083 };
00084
00085
00086
00087
00088 union MMC_IO_BUFFER IoBuffer;
00089 DRIVE_DESCRIPTION DriveDesc;
00090
00091
00092
00093
00094
00095 static void drive_init_rdcf2_struct(void);
00096 static void drive_collect_data(void);
00097
00098 static void drive_init_rdcf2_struct(void)
00099 {
00100 int i;
00101
00102 for (i=0; i<MaxFileBuffers+1; i++)
00103 {
00104 fcbs[i].ReadSector = sdcard_read_block;
00105 fcbs[i].WriteSector = sdcard_write_block;
00106 fcbs[i].BufferInUse = FALSE;
00107 }
00108 }
00109
00110
00111 static void drive_collect_data(void)
00112 {
00113
00114 DriveDesc.SectorsPerFAT = IoBuffer.BootBlock.SectorsPerFAT;
00115
00116 DriveDesc.SectorsPerCluster = IoBuffer.BootBlock.SectorsPerCluster;
00117
00118 DriveDesc.FirstFatSector = DriveDesc.SectorZero + IoBuffer.BootBlock.ReservedSectors;
00119
00120 if (IoBuffer.BootBlock.NumberOfFATs>1)
00121 {
00122 DriveDesc.SecondFatSector = DriveDesc.FirstFatSector + DriveDesc.SectorsPerFAT;
00123 }
00124 else
00125 {
00126
00127 DriveDesc.SecondFatSector = -1;
00128 }
00129
00130 if (DriveDesc.SecondFatSector == -1)
00131 {
00132
00133 DriveDesc.RootDirSector = DriveDesc.FirstFatSector + IoBuffer.BootBlock.SectorsPerFAT;
00134 }
00135 else
00136 {
00137
00138 DriveDesc.RootDirSector = DriveDesc.FirstFatSector + (2 * IoBuffer.BootBlock.SectorsPerFAT);
00139 }
00140
00141 DriveDesc.NumberRootDirEntries = IoBuffer.BootBlock.NumberRootDirEntries;
00142
00143 DriveDesc.DataStartSector = DriveDesc.RootDirSector +
00144 (DriveDesc.NumberRootDirEntries * SIZEOF_DIR_ENTRY) / RDCF_SECTOR_SIZE;
00145
00146 DriveDesc.MaxDataSector = DriveDesc.SectorZero +
00147 ((IoBuffer.BootBlock.NumberOfSectorsOnMedia) ?
00148 IoBuffer.BootBlock.NumberOfSectorsOnMedia :
00149 IoBuffer.BootBlock.NumberOfTotalSectors);
00150 }
00151
00152
00153
00154
00155
00156
00157 BOOL drive_init(void)
00158 {
00159
00160 sdcard_hardware_init();
00161
00162 sdcard_power(SDCARD_POWER_ON);
00163
00164
00165 ssp_init();
00166
00167
00168
00169 if (sdcard_detect()==FALSE)
00170 {
00171
00172 DriveDesc.IsValid = FALSE;
00173 return TRUE;
00174 }
00175 if (DriveDesc.IsValid==TRUE)
00176 {
00177
00178 return FALSE;
00179 }
00180 if (sdcard_initialise()==FALSE) return TRUE;
00181
00182 drive_init_rdcf2_struct();
00183
00184 if (sdcard_read_block(0,(uint8*)&IoBuffer)) return TRUE;
00185
00186 if (IoBuffer.PartitionTable.Signature != 0xaa55) return TRUE;
00187
00188 DriveDesc.SectorZero = IoBuffer.PartitionTable.PartitionEntry0.FirstSectorPosition;
00189 if (sdcard_read_block(DriveDesc.SectorZero,(uint8*)&IoBuffer)) return TRUE;
00190
00191 if (IoBuffer.BootBlock.Signature != 0xaa55) return TRUE;
00192
00193 drive_collect_data();
00194
00195 DriveDesc.IsValid = TRUE;
00196 return FALSE;
00197 }
00198