00001
00002
00003
00004
00005
00006
00007
00008
00015
00016
00017 #include "xio.h"
00018 #include "microsd.h"
00019 #include "fat16.h"
00020 #include "string.h"
00021
00022
00023 Xuint8 SectorBuff[512];
00024 Xuint8 DirBuff[512*32];
00025
00026
00038 int Fat16_InitializeLoadBoot(Fat16 *fp, Microsd *SDPtr)
00039 {
00040 int i;
00041 Xuint32 BootSectorNum;
00042
00043
00044 if( !(SDPtr->Status & SD_STATUS_READY_MASK) ) {
00045 xil_printf("SD card is not initialized.\r\n"); return FALSE; }
00046
00047 fp->SDPtr = SDPtr;
00048
00049 Microsd_ReadSector(SDPtr, 0, SectorBuff);
00050
00051
00052 if( (SectorBuff[510] != 0x55) || (SectorBuff[511] != 0xAA) )
00053 {
00054 xil_printf("Problems with MBR.\r\n");
00055 return FALSE;
00056 }
00057
00058
00059 for(i = 0; i < 4; i++)
00060 BootSectorNum = (BootSectorNum << 8) | SectorBuff[457-i];
00061
00062
00063 Microsd_ReadSector(SDPtr, BootSectorNum, SectorBuff);
00064
00065
00066 if((SectorBuff[38] != 0x29) ||
00067 (SectorBuff[54] != (char)'F') ||
00068 (SectorBuff[55] != (char)'A') ||
00069 (SectorBuff[56] != (char)'T') ||
00070 (SectorBuff[57] != (char)'1') ||
00071 (SectorBuff[58] != (char)'6') ||
00072 (SectorBuff[510] != 0x55) ||
00073 (SectorBuff[511] != 0xAA) )
00074 {
00075 xil_printf("Problems with Boot Sector.\r\n");
00076 return FALSE;
00077 }
00078
00079
00080 if((SectorBuff[11] != 0x00) ||
00081 (SectorBuff[12] != 0x02) )
00082 {
00083 print("Sector size is not 512 bytes per sector.\r\n");
00084 return FALSE;
00085 }
00086
00087
00088 fp->RootMaxDir = (SectorBuff[18] << 8) + SectorBuff[17];
00089 fp->SectPerCluster = SectorBuff[13];
00090 fp->SectPerFAT = (SectorBuff[23] << 8) + SectorBuff[22];
00091
00092
00093 fp->FAT1SectorNum = BootSectorNum + (SectorBuff[15] << 8)
00094 + SectorBuff[14];
00095
00096
00097 fp->RootDirSectorNum = fp->FAT1SectorNum
00098 + SectorBuff[16] * fp->SectPerFAT;
00099
00100
00101 fp->Cluster0SectorNum = fp->RootDirSectorNum
00102 + ((Xuint32)fp->RootMaxDir * 32)/512 - 2 * fp->SectPerCluster;
00103
00104 fp->SDPtr->Status |= SD_STATUS_FAT_READY_MASK;
00105 return TRUE;
00106 }
00107
00108
00119 int Fat16_SDLoadRootDir(Fat16 *fp)
00120 {
00121 Xuint16 idx;
00122
00123 if( !(fp->SDPtr->Status & SD_STATUS_FAT_READY_MASK) ) {
00124 xil_printf("Boot Sector was not loaded.\r\n"); return FALSE; }
00125
00126 for (idx = 0; idx < (fp->RootMaxDir*32+511)/512; idx++)
00127 Microsd_ReadSector(fp->SDPtr, fp->RootDirSectorNum+idx, &DirBuff[idx*SECTOR_SIZE]);
00128
00129 return TRUE;
00130 }
00131
00132
00141 void Fat16_SDPrintDir(Fat16 *fp)
00142 {
00143 Xuint32 FileSize;
00144 Xuint32 idx;
00145 char FileName[16], *name, *ext;
00146
00147 xil_printf("Files:\r\n");
00148
00149 for(idx = 0; (idx < fp->RootMaxDir) && DirBuff[idx+FAT_FILENAME]; idx += 32)
00150 {
00151 if (DirBuff[idx+FAT_FILENAME] != FAT_DIR_UNUSED)
00152 {
00153
00154 memcpy(FileName, &DirBuff[idx+FAT_FILENAME], 8);
00155 FileName[8] = '.';
00156 memcpy(&FileName[9], &DirBuff[idx+FAT_EXTENSION], 3);
00157 FileName[12] = '\0';
00158
00159 name = strtok(FileName, " .");
00160 ext = strtok(NULL, " .");
00161 xil_printf("%s%c%s\r\n", name, (ext?'.':' '), ext);
00162 }
00163 }
00164
00165 }
00166
00167
00179 int Fat16_SDReadFile(Fat16 *fp, char *FileName)
00180 {
00181 int i, j, idx;
00182 char FileNameFormatted[16], CurrFileName[16];
00183 Xuint16 StartingClusterNum;
00184 Xuint32 FileLen, StartingSectorNum;
00185
00186 if( !(fp->SDPtr->Status & SD_STATUS_FAT_READY_MASK) ) {
00187 xil_printf("Boot Sector was not loaded.\r\n"); return FALSE; }
00188
00189
00190 for(i = 0; i < 8 && FileName[i] != '.'; i++)
00191 if(FileName[i] >= 'a' && FileName[i] <= 'z')
00192 FileNameFormatted[i] = FileName[i] + ('A'-'a');
00193 else
00194 FileNameFormatted[i] = FileName[i];
00195
00196 for(j = i; j < 8; j++)
00197 FileNameFormatted[j] = ' ';
00198
00199 FileNameFormatted[8] = '.';
00200
00201 for(j = 9, i++; j < 12; j++, i++)
00202 if(FileName[i] >= 'a' && FileName[i] <= 'z')
00203 FileNameFormatted[j] = FileName[i] + ('A'-'a');
00204 else
00205 FileNameFormatted[j] = FileName[i];
00206
00207
00208
00209 for(idx = 0; (idx < fp->RootMaxDir) && DirBuff[idx+FAT_FILENAME]; idx += 32)
00210 {
00211 if (DirBuff[idx+FAT_FILENAME] != FAT_DIR_UNUSED)
00212 {
00213
00214 memcpy(CurrFileName, &DirBuff[idx+FAT_FILENAME], 8);
00215 CurrFileName[8] = '.';
00216 memcpy(&CurrFileName[9], &DirBuff[idx+FAT_EXTENSION], 3);
00217 CurrFileName[12] = '\0';
00218
00219 if(!strcmp(CurrFileName, FileNameFormatted))
00220 break;
00221 }
00222 }
00223
00224 if(!( (idx < fp->RootMaxDir) && DirBuff[idx+FAT_FILENAME]) )
00225 {
00226 xil_printf("No such file!\r\n");
00227 return FALSE;
00228 }
00229
00230 StartingClusterNum = (DirBuff[idx+27] << 8) + DirBuff[idx+26];
00231 StartingSectorNum = fp->Cluster0SectorNum
00232 + StartingClusterNum * fp->SectPerCluster;
00233
00234 for(i = 0, FileLen = 0; i < 4; i++)
00235 FileLen = (FileLen << 8) | DirBuff[idx+31-i];
00236
00237
00238 Microsd_ReadBlock(fp->SDPtr, StartingSectorNum * SECTOR_SIZE, SectorBuff,
00239 FileLen);
00240
00241
00242 for(i = 0; i < FileLen; i++)
00243 {
00244 if(i%100 == 0) xil_printf("\r\n");
00245 xil_printf("%c", (char) SectorBuff[i]);
00246 }
00247
00248 xil_printf("\r\n");
00249
00250 return TRUE;
00251 }
00252
00253
00266 int Fat16_SDWriteFile(Fat16 *fp, char *FileName)
00267 {
00268 int i, ChangeDirFlag = FALSE;
00269 Xuint16 FreeClusterNum = 0;
00270 char test_string[29] = "Data Sent to microSD card!\r\n";
00271
00272 if( !(fp->SDPtr->Status & SD_STATUS_FAT_READY_MASK) ) {
00273 xil_printf("Boot Sector was not loaded.\r\n"); return 1; }
00274
00275 if(Microsd_ReadSector(fp->SDPtr, fp->FAT1SectorNum, SectorBuff) != 0xFF) {
00276 xil_printf("Falied reading FAT table.\r\n"); return 2; }
00277
00278
00279 for(i = 2; (FreeClusterNum == 0) && (i < 256*fp->SectPerFAT); i++)
00280 if( (SectorBuff[2*i+1] == 0) && (SectorBuff[2*i] == 0) )
00281 FreeClusterNum = i;
00282
00283 if(FreeClusterNum == 0) {
00284 xil_printf("Volume is full.\r\n"); return 3; }
00285
00286 xil_printf("Writing to free cluster: 0x%X.\r\n", FreeClusterNum);
00287
00288
00289 SectorBuff[2*FreeClusterNum+1] = 0xFF;
00290 SectorBuff[2*FreeClusterNum] = 0xFF;
00291
00292
00293 if( Microsd_WriteBlock(fp->SDPtr, (fp->FAT1SectorNum)*SECTOR_SIZE,
00294 SectorBuff, SECTOR_SIZE) != 0x00 ) {
00295 xil_printf("SDWriteFile(): Write Failed.\r\n"); return 4; }
00296
00297 Fat16_SDLoadRootDir(fp);
00298 for(i = 0; (i < 32*fp->RootMaxDir) && (DirBuff[i] != FAT_DIR_UNUSED)
00299 && (DirBuff[i] != FAT_DIR_LAST); i += 32);
00300 if(i >= 32*fp->RootMaxDir) {
00301 xil_printf("Root directory if full.\r\n"); return 5; }
00302
00303 xil_printf("i = %X.\r\n", i);
00304
00305 if((SectorBuff[i] == FAT_DIR_LAST) && (i+32 < fp->RootMaxDir)) {
00306 SectorBuff[i+32] = FAT_DIR_LAST;
00307 ChangeDirFlag = TRUE;
00308 }
00309
00310 memcpy(&DirBuff[i], "TEST TXT", 11);
00311 DirBuff[i+0x0b]= 0x00;
00312 DirBuff[i+0x0c]= 0x00;
00313 DirBuff[i+0x0d]= 0x00;
00314 DirBuff[i+0x0e]= 0x00;
00315 DirBuff[i+0x0f]= 0x00;
00316 DirBuff[i+0x10]= 0x00;
00317 DirBuff[i+0x11]= 0x00;
00318 DirBuff[i+0x12]= 0x00;
00319 DirBuff[i+0x13]= 0x00;
00320 DirBuff[i+0x14]= 0x00;
00321 DirBuff[i+0x15]= 0x00;
00322 DirBuff[i+0x16]= 0x00;
00323 DirBuff[i+0x17]= 0x00;
00324 DirBuff[i+0x18]= 0x00;
00325 DirBuff[i+0x19]= 0x00;
00326 DirBuff[i+0x1a]= FreeClusterNum;
00327 DirBuff[i+0x1b]= FreeClusterNum >> 8;
00328 DirBuff[i+0x1c]= strlen(test_string);
00329 DirBuff[i+0x1d]= 0x00;
00330 DirBuff[i+0x1e]= 0x00;
00331 DirBuff[i+0x1f]= 0x00;
00332
00333 if( Microsd_WriteBlock(fp->SDPtr, fp->RootDirSectorNum*SECTOR_SIZE,
00334 DirBuff, SECTOR_SIZE) != 0x00) {
00335 xil_printf("SDWriteFile(): Write to dir Failed.\r\n"); return 6; }
00336
00337
00338
00339
00340
00341
00342
00343 strncpy(SectorBuff, test_string, sizeof(SectorBuff));
00344
00345 if( Microsd_WriteBlock(fp->SDPtr,
00346 (fp->Cluster0SectorNum + FreeClusterNum*fp->SectPerCluster)*SECTOR_SIZE,
00347 SectorBuff, SECTOR_SIZE) != 0x00) {
00348 xil_printf("SDWriteFile(): Writing file content failed.\r\n"); return 7; }
00349
00350 xil_printf("A new file has been written.\r\n");
00351 return 0;
00352 }