fat.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. /****************************************************************************
  2. * This file is part of the AVRIDE device driver.
  3. *
  4. * Copyright (c) 2002-2003 by Michael Fischer. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the author nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  23. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. ****************************************************************************
  33. * History:
  34. *
  35. * 14.12.02 mifi First Version
  36. * 23.12.02 mifi Add FileOpen, FileClose, FileError, FileSize,
  37. * FileSeek and FileRead. But the FileSeek function.
  38. * does not work in the moment, later...
  39. * 28.12.02 mifi Now support FAT16 AND FAT32.
  40. * 01.01.03 mifi Support long directory entries, but without to
  41. * check the checksum of the short entry.
  42. * Change FAT32FileSize return value from int to long.
  43. * The max size of a long filename segment is
  44. * (FAT_LONG_NAME_LEN-1). But the complete filename can
  45. * be longer.
  46. *
  47. * segment1/segment2/segment3/index.html
  48. *
  49. * segmentX max length = (FAT_LONG_NAME_LEN-1)
  50. *
  51. * 04.01.03 mifi Take a look at the return values...
  52. * 18.01.03 mifi Change Licence from GPL to BSD.
  53. * 25.01.03 mifi Implement a new FindFile function.
  54. * I have some trouble with short file names under
  55. * Win98. Win98 store a short name like "enlogo.gif"
  56. * as a long name, nasty OS.
  57. * Remove FAT32_MAX_FILES and the array aFileHandle,
  58. * a file handle will now be allocated by NutHeapAlloc,
  59. * therefore we have no restrictions about the count of
  60. * the open file handle. (Only by available memory)
  61. * 27.01.03 mifi Rename all FAT32xxx function to FATxxx.
  62. *
  63. * 28.01.03 mifi Start porting to Nut/OS 3.X.X
  64. * 19.06.03 mifi Change the call of IDEInit, now we use the BaseAddress
  65. * of 0. Because the fat module does not need to know the
  66. * address. It will be handled in ide.c.
  67. * 29.06.03 mifi First ATAPI-Version
  68. * Now we can read files from a CD-ROM. But there exist
  69. * some open points:
  70. * - only first session from a multisession CD is supported
  71. * - only iso9660, no Joliet support now, later
  72. ****************************************************************************/
  73. #define __FAT_C__
  74. #include <string.h>
  75. #include <stddef.h>
  76. #include <ctype.h>
  77. #include <sys/heap.h>
  78. #include <sys/event.h>
  79. #include <sys/thread.h>
  80. #include <sys/device.h>
  81. #include <dev/ide.h>
  82. #include <fs/fat.h>
  83. #include <fs/typedefs.h>
  84. /*==========================================================*/
  85. /* DEFINE: All Structures and Common Constants */
  86. /*==========================================================*/
  87. //
  88. // FAT return codes
  89. //
  90. #define FAT_OK 0
  91. #define FAT_ERROR -1
  92. #define FAT_ERROR_EOF -2
  93. #define FAT_ERROR_IDE -3
  94. //
  95. // Define for correct return values Nut/OS
  96. //
  97. #define NUTDEV_OK 0
  98. #define NUTDEV_ERROR -1
  99. #define FAT_MAX_DRIVE 2
  100. //
  101. // Some defines for the FAT structures
  102. //
  103. #define ZIP_DRIVE_BR_SECTOR 32
  104. #define BPB_RsvdSecCnt 32
  105. #define BPB_NumFATs 2
  106. #define BPB_HiddSec 63
  107. #define FAT32_MEDIA 0xf8
  108. #define FAT32_OFFSET_FSINFO 1
  109. #define FAT32_OFFSET_BACKUP_BOOT 6
  110. #define FAT16_CLUSTER_EOF 0x0000FFFF
  111. #define FAT16_CLUSTER_ERROR 0x0000FFF7
  112. #define FAT16_CLUSTER_MASK 0x0000FFFF
  113. #define FAT32_CLUSTER_EOF 0x0FFFFFFF
  114. #define FAT32_CLUSTER_ERROR 0x0FFFFFF7
  115. #define FAT32_CLUSTER_MASK 0x0FFFFFFF
  116. #define FAT_SIGNATURE 0xAA55
  117. #define MBR_SIGNATURE FAT_SIGNATURE
  118. #define MBR_FAT32 0x0C
  119. #define FSINFO_FIRSTSIGNATURE 0x41615252
  120. #define FSINFO_FSINFOSIGNATURE 0x61417272
  121. #define FSINFO_SIGNATURE FAT_SIGNATURE
  122. #define DIRECTORY_ATTRIBUTE_READ_ONLY 0x01
  123. #define DIRECTORY_ATTRIBUTE_HIDDEN 0x02
  124. #define DIRECTORY_ATTRIBUTE_SYSTEM_FILE 0x04
  125. #define DIRECTORY_ATTRIBUTE_VOLUME_ID 0x08
  126. #define DIRECTORY_ATTRIBUTE_DIRECTORY 0x10
  127. #define DIRECTORY_ATTRIBUTE_ARCHIVE 0x20
  128. //
  129. // DIRECTORY_ATTRIBUTE_READ_ONLY |
  130. // DIRECTORY_ATTRIBUTE_HIDDEN |
  131. // DIRECTORY_ATTRIBUTE_SYSTEM_FILE |
  132. // DIRECTORY_ATTRIBUTE_VOLUME_ID
  133. //
  134. #define DIRECTORY_ATTRIBUTE_LONG_NAME 0x0F
  135. //
  136. // DIRECTORY_ATTRIBUTE_READ_ONLY |
  137. // DIRECTORY_ATTRIBUTE_HIDDEN |
  138. // DIRECTORY_ATTRIBUTE_SYSTEM_FILE |
  139. // DIRECTORY_ATTRIBUTE_VOLUME_ID |
  140. // DIRECTORY_ATTRIBUTE_DIRECTORY |
  141. // DIRECTORY_ATTRIBUTE_ARCHIVE
  142. //
  143. #define DIRECTORY_ATTRIBUTE_LONG_NAME_MASK 0x3F
  144. #define FAT_NAME_LEN 8
  145. #define FAT_EXT_LEN 3
  146. //
  147. // FAT_SHORT_NAME_LEN name len =
  148. // name + ext + 1 for the point
  149. //
  150. #define FAT_SHORT_NAME_LEN (FAT_NAME_LEN+FAT_EXT_LEN+1)
  151. #define FAT_LONG_NAME_LEN 64
  152. //
  153. // Some stuff for HD and CD, DRIVE_INFO Flags
  154. //
  155. //
  156. #define FLAG_FAT_IS_CDROM 0x0001
  157. #define FLAG_FAT_IS_ZIP 0x0002
  158. //
  159. // DiskSize to SectorPerCluster table
  160. //
  161. typedef struct {
  162. DWORD DiskSize;
  163. BYTE SecPerClusVal;
  164. } DSKSZTOSECPERCLUS;
  165. typedef struct _FAT32FileDataTime {
  166. unsigned Seconds:5;
  167. unsigned Minute:6;
  168. unsigned Hour:5;
  169. unsigned Day:5;
  170. unsigned Month:4;
  171. unsigned Year:7;
  172. } FAT32_FILEDATETIME, *PFAT32_FILEDATETIME;
  173. typedef struct _FAT32DirectoryEntry {
  174. BYTE Name[FAT_NAME_LEN];
  175. BYTE Extension[FAT_EXT_LEN];
  176. BYTE Attribute;
  177. BYTE Reserved[8];
  178. WORD HighCluster;
  179. FAT32_FILEDATETIME Date;
  180. WORD LowCluster;
  181. DWORD FileSize;
  182. } FAT32_DIRECTORY_ENTRY;
  183. typedef struct _FAT32DirectoryEntryLong {
  184. BYTE Order;
  185. WORD Name1[5];
  186. BYTE Attribute;
  187. BYTE Type;
  188. BYTE Chksum;
  189. WORD Name2[6];
  190. WORD LowCluster;
  191. WORD Name3[2];
  192. } FAT32_DIRECTORY_ENTRY_LONG;
  193. typedef struct _FAT32FileSystemInformation {
  194. DWORD FirstSignature;
  195. BYTE Reserved1[480];
  196. DWORD FSInfoSignature;
  197. DWORD NumberOfFreeClusters;
  198. DWORD MostRecentlyAllocatedCluster;
  199. BYTE Reserved2[12];
  200. BYTE Reserved3[2];
  201. WORD Signature;
  202. } FAT32_FSINFO;
  203. typedef struct _FAT32PartitionEntry {
  204. BYTE BootInd;
  205. BYTE FirstHead;
  206. BYTE FirstSector;
  207. BYTE FirstTrack;
  208. BYTE FileSystem;
  209. BYTE LastHead;
  210. BYTE LastSector;
  211. BYTE LastTrack;
  212. DWORD StartSectors;
  213. DWORD NumSectors;
  214. } FAT32_PARTITION_ENTRY;
  215. typedef struct _FAT32PartionTable {
  216. BYTE LoadInstruction[446];
  217. FAT32_PARTITION_ENTRY Partition[4];
  218. WORD Signature; /* AA55 */
  219. } FAT32_PARTITION_TABLE;
  220. typedef struct _bpbfat16 {
  221. BYTE DrvNum;
  222. BYTE Reserved1;
  223. BYTE BootSig;
  224. DWORD VollID;
  225. BYTE VolLab[11];
  226. BYTE FilSysType[8];
  227. BYTE Reserved2[28];
  228. } BPBFAT16;
  229. typedef struct _bpbfat32 {
  230. DWORD FATSz32; // xxx
  231. WORD ExtFlags; // 0
  232. WORD FSVer; // must 0
  233. DWORD RootClus; //
  234. WORD FSInfo; // typically 1
  235. WORD BkBootSec; // typically 6
  236. BYTE Reserved[12]; // set all to zero
  237. BYTE DrvNum; // must 0x80
  238. BYTE Reserved1; // set all to zero
  239. BYTE BootSig; // must 0x29
  240. DWORD VollID; // xxx
  241. BYTE VolLab[11]; // "abcdefghijk"
  242. BYTE FilSysType[8]; // "FAT32 "
  243. } BPBFAT32;
  244. typedef union _bpboffset36 {
  245. BPBFAT16 FAT16;
  246. BPBFAT32 FAT32;
  247. } BPBOFFSET36;
  248. typedef struct _FAT32BootRecord {
  249. BYTE JumpBoot[3]; // 0xeb, 0x58, 0x90
  250. BYTE OEMName[8]; // "MSWIN4.1"
  251. WORD BytsPerSec; // must 512
  252. BYTE SecPerClus; // 8 for 4K cluster
  253. WORD RsvdSecCnt; // typically 32 for FAT32
  254. BYTE NumFATs; // always 2
  255. WORD RootEntCnt; // must 0 for FAT32
  256. WORD TotSec16; // must 0 for FAT32
  257. BYTE Media; // must 0xf8
  258. WORD FATSz16; // 0 for FAT32
  259. WORD SecPerTrk; // 63 for FAT32
  260. WORD NumHeads; // 255 for FAT32
  261. DWORD HiddSec; // 63 for FAT32
  262. DWORD TotSec32; // xxx
  263. BPBOFFSET36 Off36;
  264. BYTE Reserved[420];
  265. WORD Signature; // must 0xAA55
  266. } FAT32_BOOT_RECORD, *PFAT32_BOOT_RECORD;
  267. typedef struct _fat_entry_table16 {
  268. WORD aEntry[256];
  269. } FAT_ENTRY_TABLE16;
  270. typedef struct _fat_entry_table32 {
  271. DWORD aEntry[128];
  272. } FAT_ENTRY_TABLE32;
  273. typedef union _fat_dir_table {
  274. FAT32_DIRECTORY_ENTRY aShort[16];
  275. FAT32_DIRECTORY_ENTRY_LONG aLong[16];
  276. } FAT_DIR_TABLE;
  277. typedef struct _drive_info {
  278. BYTE bIsFAT32;
  279. BYTE bDevice;
  280. BYTE bSectorsPerCluster;
  281. BYTE bFlags;
  282. WORD wSectorSize;
  283. DWORD dwRootDirSectors;
  284. DWORD dwFirstRootDirSector;
  285. DWORD dwRootCluster;
  286. DWORD dwFAT1StartSector;
  287. DWORD dwFAT2StartSector;
  288. DWORD dwCluster2StartSector;
  289. DWORD dwClusterSize;
  290. } DRIVE_INFO;
  291. typedef struct _fhandle {
  292. DWORD dwFileSize;
  293. DWORD dwStartCluster;
  294. DWORD dwReadCluster;
  295. DWORD dwFilePointer; /* total file pointer */
  296. DWORD dwClusterPointer; /* cluster read pointer */
  297. int nLastError;
  298. int nEOF;
  299. DRIVE_INFO *pDrive;
  300. } FHANDLE;
  301. #if (IDE_SUPPORT_ATAPI == 1)
  302. //
  303. // Some ATAPI stuff
  304. //
  305. typedef struct _atapi_pvd {
  306. BYTE descr_type;
  307. BYTE magic[5];
  308. BYTE descr_ver;
  309. BYTE unused;
  310. BYTE sysid[32];
  311. BYTE volid[32];
  312. BYTE zeros1[8];
  313. BYTE seknum[8];
  314. BYTE zeros2[32];
  315. BYTE volsetsize[4];
  316. BYTE volseqnum[4];
  317. BYTE seksize[4];
  318. BYTE pathtablen[8];
  319. DWORD firstsek_LEpathtab1_LE;
  320. DWORD firstsek_LEpathtab2_LE;
  321. BYTE firstsek_BEpathtab1_BE[4];
  322. BYTE firstsek_BEpathtab2_BE[4];
  323. BYTE rootdir[34];
  324. BYTE volsetid[128];
  325. BYTE pubid[128];
  326. BYTE dataprepid[128];
  327. BYTE appid[128];
  328. BYTE copyr[37];
  329. BYTE abstractfileid[37];
  330. BYTE bibliofileid[37];
  331. BYTE creationdate[17];
  332. BYTE modify[17];
  333. BYTE expire[17];
  334. BYTE effective[17];
  335. BYTE filestruc_ver;
  336. BYTE zero;
  337. BYTE app_use[512];
  338. BYTE res[653];
  339. } ATAPI_PVD;
  340. //
  341. // Descriptor values
  342. //
  343. #define ATAPI_BOOT_RECORD 0x00
  344. #define ATAPI_PVD_DESC 0x01
  345. #define ATAPI_SVD_DESC 0x02
  346. #define ATAPI_VDST_DESC 0xFF
  347. typedef struct _ATAPIDirectoryRecord {
  348. BYTE bRecordSize;
  349. BYTE bShouldBeZero;
  350. DWORD dwFirstSector;
  351. DWORD dwNotUse1;
  352. DWORD dwEntrySize;
  353. DWORD dwNotUse2;
  354. BYTE bDate[7];
  355. BYTE bFlags;
  356. BYTE bInterleaveInfo[2];
  357. BYTE bVolumeSequenceNumber[4];
  358. BYTE bIdentifierLength;
  359. BYTE bName[1];
  360. } ATAPI2_DIRECTORY_RECORD;
  361. #endif
  362. /*==========================================================*/
  363. /* DEFINE: Definition of all local Data */
  364. /*==========================================================*/
  365. static int gnIsInit = FALSE;
  366. static BYTE *pSectorBuffer = NULL;
  367. static char *pLongName1 = NULL;
  368. static char *pLongName2 = NULL;
  369. static DRIVE_INFO sDriveInfo[FAT_MAX_DRIVE];
  370. static HANDLE hFATSemaphore;
  371. /*==========================================================*/
  372. /* DEFINE: Definition of all local Procedures */
  373. /*==========================================================*/
  374. /************************************************************/
  375. /* FATLock */
  376. /************************************************************/
  377. void FATLock(void)
  378. {
  379. NutEventWait(&hFATSemaphore, 0);
  380. }
  381. /************************************************************/
  382. /* FATFree */
  383. /************************************************************/
  384. void FATFree(void)
  385. {
  386. NutEventPost(&hFATSemaphore);
  387. }
  388. /************************************************************/
  389. /* FATSemaInit */
  390. /************************************************************/
  391. void FATSemaInit(void)
  392. {
  393. NutEventPost(&hFATSemaphore);
  394. }
  395. /************************************************************/
  396. /* GetFirstSectorOfCluster */
  397. /************************************************************/
  398. static DWORD GetFirstSectorOfCluster(DRIVE_INFO * pDrive, DWORD dwCluster)
  399. {
  400. DWORD dwSector;
  401. if (pDrive->bFlags & FLAG_FAT_IS_CDROM) {
  402. dwSector = dwCluster;
  403. } else {
  404. dwSector = (dwCluster - 2) * pDrive->bSectorsPerCluster;
  405. dwSector += pDrive->dwCluster2StartSector;
  406. }
  407. return (dwSector);
  408. }
  409. /************************************************************/
  410. /* GetNextCluster */
  411. /************************************************************/
  412. static DWORD GetNextCluster(DRIVE_INFO * pDrive, DWORD dwCluster)
  413. {
  414. DWORD dwNextCluster;
  415. DWORD dwSector;
  416. DWORD dwIndex;
  417. FAT_ENTRY_TABLE16 *pFatTable16;
  418. FAT_ENTRY_TABLE32 *pFatTable32;
  419. if (pDrive->bFlags & FLAG_FAT_IS_CDROM) {
  420. dwNextCluster = dwCluster + 1;
  421. } else {
  422. if (pDrive->bIsFAT32 == TRUE) {
  423. //
  424. // (IDE_SECTOR_SIZE / sizeof(long)) == 128
  425. //
  426. dwSector = (dwCluster / 128) + pDrive->dwFAT1StartSector;
  427. dwIndex = dwCluster % 128;
  428. IDEReadSectors(pDrive->bDevice, pSectorBuffer, dwSector, 1);
  429. pFatTable32 = (FAT_ENTRY_TABLE32 *) pSectorBuffer;
  430. dwNextCluster = (pFatTable32->aEntry[dwIndex] & FAT32_CLUSTER_MASK);
  431. if ((dwNextCluster == FAT32_CLUSTER_EOF) || (dwNextCluster == FAT32_CLUSTER_ERROR)) {
  432. dwNextCluster = 0;
  433. }
  434. } else { /* FAT16 */
  435. //
  436. // (IDE_SECTOR_SIZE / sizeof(word)) == 256
  437. //
  438. dwSector = (dwCluster / 256) + pDrive->dwFAT1StartSector;
  439. dwIndex = dwCluster % 256;
  440. IDEReadSectors(pDrive->bDevice, pSectorBuffer, dwSector, 1);
  441. pFatTable16 = (FAT_ENTRY_TABLE16 *) pSectorBuffer;
  442. dwNextCluster = (pFatTable16->aEntry[dwIndex] & FAT16_CLUSTER_MASK);
  443. if ((dwNextCluster == FAT16_CLUSTER_EOF) || (dwNextCluster == FAT16_CLUSTER_ERROR)) {
  444. dwNextCluster = 0;
  445. }
  446. } /* endif pDrive->bIsFAT32 */
  447. }
  448. return (dwNextCluster);
  449. }
  450. /************************************************************/
  451. /* GetLongChar */
  452. /************************************************************/
  453. static char GetLongChar(WORD wValue)
  454. {
  455. BYTE Value;
  456. Value = (BYTE) (wValue & 0x00FF);
  457. if (Value == 0xFF) {
  458. Value = 0;
  459. }
  460. if (Value != 0) {
  461. Value = toupper(Value);
  462. }
  463. return ((char) Value);
  464. }
  465. #if (IDE_SUPPORT_ATAPI == 1)
  466. /************************************************************/
  467. /* CheckATAPIName */
  468. /* */
  469. /* Some values are not allowed in a "ATAPIname", */
  470. /* Therefore we will change the name here. */
  471. /************************************************************/
  472. static char *CheckATAPIName(char *pLongName)
  473. {
  474. char *pRead;
  475. pRead = pLongName;
  476. while (*pRead != 0) {
  477. switch (*pRead) {
  478. case '-':
  479. *pRead = '_';
  480. break;
  481. case ' ':
  482. *pRead = '_';
  483. break;
  484. }
  485. pRead++;
  486. }
  487. return (pLongName);
  488. }
  489. /************************************************************/
  490. /* RemoveChar */
  491. /* */
  492. /* Houston, we have a problem.... */
  493. /* Some chars are not allowed in a name, in the case of */
  494. /* ATAPI, the filename test.pic.gif is stored as */
  495. /* testpic.gif. For a directory, we must remove all '.' */
  496. /* Therefore we must correct the name :o( */
  497. /************************************************************/
  498. static char *RemoveChar(char *pLongName, BYTE bDir)
  499. {
  500. char *pRead;
  501. char *pWrite;
  502. BYTE bPointCount;
  503. pRead = pLongName;
  504. pWrite = pLongName;
  505. if (bDir == TRUE) {
  506. while (*pRead != 0) {
  507. if (*pRead != '.') {
  508. *pWrite = *pRead;
  509. pWrite++;
  510. }
  511. pRead++;
  512. }
  513. *pWrite = 0;
  514. } else {
  515. //
  516. // 1. search all points in the name
  517. //
  518. pRead = pLongName;
  519. bPointCount = 0;
  520. while (*pRead != 0) {
  521. if (*pRead == '.') {
  522. bPointCount++;
  523. }
  524. pRead++;
  525. }
  526. if (bPointCount >= 2) {
  527. //
  528. // Remove the points...
  529. //
  530. bPointCount--;
  531. pRead = pLongName;
  532. pWrite = pLongName;
  533. while (*pRead != 0) {
  534. if ((*pRead == '.') && (bPointCount)) {
  535. bPointCount--;
  536. } else {
  537. *pWrite = *pRead;
  538. pWrite++;
  539. }
  540. pRead++;
  541. }
  542. *pWrite = 0;
  543. }
  544. }
  545. return (pLongName);
  546. }
  547. /************************************************************/
  548. /* FindFileATAPI */
  549. /* */
  550. /* Find a file by a given name pLongName. */
  551. /* For ATAPI, we have NO cluster. A cluster is the same */
  552. /* as a sector. */
  553. /************************************************************/
  554. static DWORD FindFileATAPI(DRIVE_INFO * pDrive,
  555. FAT32_DIRECTORY_ENTRY * pSearchEntry,
  556. char *pLongName, DWORD dwDirCluster, DWORD * pFileSize, int nIsLongName)
  557. {
  558. DWORD dwSector;
  559. DWORD dwDirSector;
  560. DWORD dwMaxDirSector;
  561. WORD wByteCount;
  562. BYTE bLongNameLen;
  563. BYTE bSearchFlags;
  564. ATAPI2_DIRECTORY_RECORD *pDirEntry;
  565. dwSector = 0;
  566. dwDirSector = dwDirCluster;
  567. dwMaxDirSector = pDrive->dwFirstRootDirSector + pDrive->dwRootDirSectors - 1;
  568. bSearchFlags = 0;
  569. if (pSearchEntry->Attribute == DIRECTORY_ATTRIBUTE_DIRECTORY) {
  570. bSearchFlags = 0x02;
  571. pLongName = RemoveChar(pLongName, TRUE);
  572. } else {
  573. pLongName = RemoveChar(pLongName, FALSE);
  574. }
  575. //
  576. // ' ' and '-' are not allowed in the name, change it...
  577. //
  578. pLongName = CheckATAPIName(pLongName);
  579. bLongNameLen = strlen(pLongName);
  580. while (dwDirCluster <= dwMaxDirSector) {
  581. IDEReadSectors(pDrive->bDevice, pSectorBuffer, dwDirCluster, 1);
  582. wByteCount = 0;
  583. pDirEntry = (ATAPI2_DIRECTORY_RECORD *) & pSectorBuffer[0];
  584. while (wByteCount < pDrive->dwClusterSize) {
  585. if (pDirEntry->bRecordSize == 0) {
  586. break;
  587. } else {
  588. //
  589. // Check pDirEntry
  590. //
  591. if (((pDirEntry->bFlags & 0x03) == bSearchFlags) &&
  592. (pDirEntry->bIdentifierLength >= bLongNameLen) &&
  593. (strncmp((char*)&pDirEntry->bName[0], pLongName, bLongNameLen) == 0)) {
  594. //
  595. // We have found the file :-)
  596. //
  597. dwSector = pDirEntry->dwFirstSector;
  598. *pFileSize = pDirEntry->dwEntrySize;
  599. // Sorry for the return here :-(
  600. return (dwSector);
  601. }
  602. wByteCount += pDirEntry->bRecordSize;
  603. pDirEntry = (ATAPI2_DIRECTORY_RECORD *) & pSectorBuffer[wByteCount];
  604. }
  605. }
  606. dwDirCluster++;
  607. }
  608. return (dwSector);
  609. }
  610. #endif
  611. /************************************************************/
  612. /* FindFile */
  613. /* */
  614. /* Find a file by a given name pLongName. */
  615. /* */
  616. /* It is possible that a SHORT name like "enlogo.gif" */
  617. /* is stored as a LONG name. I have seen this */
  618. /* nasty behaviour by Win98. Therefore I will check */
  619. /* the long name too, even if nIsLongName is FALSE. */
  620. /************************************************************/
  621. static DWORD FindFile(DRIVE_INFO * pDrive,
  622. FAT32_DIRECTORY_ENTRY * pSearchEntry, char *pLongName, DWORD dwDirCluster, DWORD * pFileSize, int nIsLongName)
  623. {
  624. int i, x;
  625. BYTE bError;
  626. int nNameLen;
  627. int nMaxLen;
  628. BYTE bFound;
  629. BYTE bEndLoop;
  630. BYTE bOrder;
  631. BYTE bMaxOrder;
  632. int nDirMaxSector;
  633. DWORD dwSector;
  634. DWORD dwNewCluster;
  635. FAT32_DIRECTORY_ENTRY *pDirEntryShort;
  636. FAT32_DIRECTORY_ENTRY_LONG *pDirEntryLong;
  637. char *pDirName = 0;
  638. FAT_DIR_TABLE *pDirTable;
  639. bError = FALSE;
  640. *pFileSize = 0;
  641. dwNewCluster = 0;
  642. nNameLen = strlen(pLongName);
  643. bMaxOrder = (BYTE) ((nNameLen + 12) / 13);
  644. nMaxLen = (int) (bMaxOrder * 13);
  645. if (nMaxLen >= (FAT_LONG_NAME_LEN - 1)) {
  646. bError = TRUE;
  647. }
  648. bOrder = (BYTE) (0x40 | bMaxOrder);
  649. if (bOrder == 0xE5) {
  650. //
  651. // I do not know what should I do if the bOrder is 0xe5.
  652. // This is a sign for a "empty" entry.
  653. //
  654. bError = TRUE;
  655. }
  656. if (bError == FALSE) {
  657. bFound = FALSE;
  658. bEndLoop = FALSE;
  659. while ((bEndLoop == FALSE) && (dwDirCluster != 0)) {
  660. dwSector = GetFirstSectorOfCluster(pDrive, dwDirCluster);
  661. nDirMaxSector = (int) pDrive->bSectorsPerCluster;
  662. //
  663. // Test for special case dwDirCluster and FAT16.
  664. //
  665. if ((dwDirCluster == 1) && (pDrive->bIsFAT32 == FALSE)) {
  666. dwSector = pDrive->dwFirstRootDirSector;
  667. nDirMaxSector = (int) pDrive->dwRootDirSectors;
  668. }
  669. //
  670. // One cluster has SecPerCluster sectors.
  671. //
  672. for (i = 0; i < nDirMaxSector; i++) {
  673. IDEReadSectors(pDrive->bDevice, pSectorBuffer, dwSector + i, 1);
  674. pDirTable = (FAT_DIR_TABLE *) pSectorBuffer;
  675. //
  676. // And one sector has 16 entries.
  677. //
  678. // IDE_SECTOR_SIZE / sizeof(FAT32_DIRECTORY_ENTRY) = 16
  679. //
  680. for (x = 0; x < 16; x++) {
  681. if (bFound == TRUE) {
  682. pDirEntryShort = (FAT32_DIRECTORY_ENTRY *) & pDirTable->aShort[x];
  683. dwNewCluster = pDirEntryShort->HighCluster;
  684. dwNewCluster = (dwNewCluster << 16) | (DWORD) pDirEntryShort->LowCluster;
  685. *pFileSize = pDirEntryShort->FileSize;
  686. bEndLoop = TRUE;
  687. break;
  688. }
  689. //
  690. // Check for valid entry.
  691. //
  692. pDirEntryShort = (FAT32_DIRECTORY_ENTRY *) & pDirTable->aShort[x];
  693. pDirEntryLong = (FAT32_DIRECTORY_ENTRY_LONG *) & pDirTable->aLong[x];
  694. if (nIsLongName == FALSE) {
  695. //
  696. // Check if it could be a short name. If Win2000 tell us it
  697. // is a short name, it is true. But with Win98 we must
  698. // test both, short and long...
  699. //
  700. if ((pDirEntryShort->Name[0] != 0xE5) && (pDirEntryShort->Name[0] != 0x00)) {
  701. if (memcmp(pDirEntryShort, pSearchEntry, 11) == 0) {
  702. //
  703. // Check for the correct attribute, this is done with
  704. // the '&' and not with the memcmp.
  705. // With the '&' it is possible to find a hidden archive too :-)
  706. //
  707. if ((pDirEntryShort->Attribute & pSearchEntry->Attribute) == pSearchEntry->Attribute) {
  708. dwNewCluster = pDirEntryShort->HighCluster;
  709. dwNewCluster = (dwNewCluster << 16) | (DWORD) pDirEntryShort->LowCluster;
  710. *pFileSize = pDirEntryShort->FileSize;
  711. bEndLoop = TRUE;
  712. break;
  713. } /* endif Attribute */
  714. } /* endif test Name+Ext */
  715. } /* endif Name[0] != 0xe5, 0x00 */
  716. }
  717. /* endif nIsLongName == FALSE */
  718. if ((pDirEntryLong->Attribute == DIRECTORY_ATTRIBUTE_LONG_NAME) && (pDirEntryLong->Order == bOrder)) {
  719. //
  720. // Next bOrder is bOrder--
  721. //
  722. if (bOrder & 0x40) {
  723. bOrder &= ~0x40;
  724. //
  725. // Get the space for the name.
  726. //
  727. pDirName = pLongName2;
  728. //
  729. // Set the end of string.
  730. //
  731. pDirName[nMaxLen--] = 0;
  732. }
  733. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name3[1]);
  734. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name3[0]);
  735. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name2[5]);
  736. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name2[4]);
  737. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name2[3]);
  738. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name2[2]);
  739. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name2[1]);
  740. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name2[0]);
  741. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name1[4]);
  742. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name1[3]);
  743. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name1[2]);
  744. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name1[1]);
  745. pDirName[nMaxLen--] = GetLongChar(pDirEntryLong->Name1[0]);
  746. bOrder--;
  747. if (bOrder == 0) {
  748. //
  749. // Now compare the name.
  750. //
  751. bOrder = (BYTE) (0x40 | bMaxOrder);
  752. nMaxLen = (int) (bMaxOrder * 13);
  753. if (memcmp(pLongName, pDirName, strlen(pLongName)) == 0) {
  754. //
  755. // The next entry will be the correct entry.
  756. //
  757. bFound = TRUE;
  758. }
  759. }
  760. }
  761. /* pDirEntryLong->Order == bOrder */
  762. } /* endfor x<16 */
  763. if (bEndLoop == TRUE) {
  764. break;
  765. }
  766. }
  767. if (bEndLoop == FALSE) {
  768. //
  769. // No file found in this cluster, get the next one.
  770. //
  771. dwDirCluster = GetNextCluster(pDrive, dwDirCluster);
  772. }
  773. } /* endwhile */
  774. }
  775. /* endif bError == FALSE */
  776. return (dwNewCluster);
  777. }
  778. #if (IDE_SUPPORT_ATAPI == 1)
  779. /************************************************************/
  780. /* MountATAPI */
  781. /************************************************************/
  782. static int MountATAPI(int nDrive)
  783. {
  784. int nError = IDE_OK;
  785. DWORD dwSector;
  786. DWORD dwPVDSector;
  787. DRIVE_INFO *pDrive;
  788. ATAPI_PVD *pPVD;
  789. pDrive = &sDriveInfo[nDrive];
  790. dwSector = ATAPI_START_SEARCH_SECTOR;
  791. dwPVDSector = 0;
  792. while (dwSector <= ATAPI_MAX_SEARCH_SECTOR) {
  793. nError = IDEReadSectors(nDrive, pSectorBuffer, dwSector, 1);
  794. if (nError != IDE_OK) {
  795. break;
  796. } else {
  797. if ((pSectorBuffer[0] == ATAPI_PVD_DESC) &&
  798. (pSectorBuffer[1] == 'C') && (pSectorBuffer[2] == 'D') &&
  799. (pSectorBuffer[3] == '0') && (pSectorBuffer[4] == '0') && (pSectorBuffer[5] == '1')) {
  800. dwPVDSector = dwSector;
  801. }
  802. if ((pSectorBuffer[0] == ATAPI_VDST_DESC) &&
  803. (pSectorBuffer[1] == 'C') && (pSectorBuffer[2] == 'D') &&
  804. (pSectorBuffer[3] == '0') && (pSectorBuffer[4] == '0') && (pSectorBuffer[5] == '1')) {
  805. break;
  806. }
  807. }
  808. dwSector++;
  809. }
  810. if (dwPVDSector != 0) {
  811. IDEReadSectors(nDrive, pSectorBuffer, dwPVDSector, 1);
  812. pPVD = (ATAPI_PVD *) pSectorBuffer;
  813. pDrive->bSectorsPerCluster = 1;
  814. pDrive->dwFirstRootDirSector = *(DWORD *) & pPVD->rootdir[2];
  815. pDrive->dwFAT1StartSector = pPVD->firstsek_LEpathtab1_LE;
  816. pDrive->dwFAT2StartSector = pPVD->firstsek_LEpathtab2_LE;
  817. pDrive->dwRootDirSectors = pDrive->dwFAT1StartSector - pDrive->dwFirstRootDirSector;
  818. pDrive->dwRootCluster = pDrive->dwFirstRootDirSector;
  819. pDrive->dwClusterSize = (DWORD) pDrive->wSectorSize;
  820. //nError = IDEATAPISetCDSpeed(nDrive, 0xFFFF);
  821. }
  822. return (nError);
  823. }
  824. #endif
  825. /************************************************************/
  826. /* MountIDE */
  827. /************************************************************/
  828. static int MountIDE(int nDrive)
  829. {
  830. int nError;
  831. int i;
  832. DWORD dwSector;
  833. DWORD dwFATSz;
  834. DWORD dwRootDirSectors;
  835. FAT32_PARTITION_TABLE *pPartitionTable;
  836. FAT32_BOOT_RECORD *pBootRecord;
  837. DRIVE_INFO *pDrive;
  838. nError = IDE_OK;
  839. i = nDrive;
  840. pDrive = &sDriveInfo[nDrive];
  841. dwSector = 0;
  842. if (pDrive->bFlags & FLAG_FAT_IS_ZIP) {
  843. dwSector = ZIP_DRIVE_BR_SECTOR;
  844. } else {
  845. //
  846. // Try to find a PartitionTable.
  847. //
  848. nError = IDEReadSectors(nDrive, pSectorBuffer, 0, 1);
  849. if (nError == IDE_OK) {
  850. pPartitionTable = (FAT32_PARTITION_TABLE *) pSectorBuffer;
  851. if (pPartitionTable->Signature == FAT_SIGNATURE) {
  852. if (pPartitionTable->Partition[0].NumSectors) {
  853. //
  854. // We found a PartitionTable, read BootRecord.
  855. //
  856. dwSector = pPartitionTable->Partition[0].StartSectors;
  857. }
  858. }
  859. }
  860. }
  861. if (dwSector != 0) {
  862. IDEReadSectors(i, pSectorBuffer, dwSector, 1);
  863. pBootRecord = (FAT32_BOOT_RECORD *) pSectorBuffer;
  864. //
  865. // Test valid BootRecord.
  866. //
  867. if (pBootRecord->Signature == FAT_SIGNATURE) {
  868. pDrive->bSectorsPerCluster = pBootRecord->SecPerClus;
  869. if (pBootRecord->FATSz16 != 0) {
  870. dwFATSz = pBootRecord->FATSz16;
  871. pDrive->bIsFAT32 = FALSE;
  872. pDrive->dwRootCluster = 1; /* special value, see */
  873. /* FindFile */
  874. } else {
  875. dwFATSz = pBootRecord->Off36.FAT32.FATSz32;
  876. pDrive->bIsFAT32 = TRUE;
  877. pDrive->dwRootCluster = pBootRecord->Off36.FAT32.RootClus;
  878. }
  879. dwRootDirSectors = ((pBootRecord->RootEntCnt * 32) + (pBootRecord->BytsPerSec - 1)) / pBootRecord->BytsPerSec;
  880. sDriveInfo[i].dwFAT1StartSector = pBootRecord->HiddSec + pBootRecord->RsvdSecCnt;
  881. pDrive->dwFAT2StartSector = pDrive->dwFAT1StartSector + dwFATSz;
  882. sDriveInfo[i].dwCluster2StartSector =
  883. pBootRecord->HiddSec + pBootRecord->RsvdSecCnt + (pBootRecord->NumFATs * dwFATSz) + dwRootDirSectors;
  884. pDrive->dwClusterSize = pBootRecord->SecPerClus * pDrive->wSectorSize;
  885. pDrive->dwRootDirSectors = dwRootDirSectors;
  886. pDrive->dwFirstRootDirSector = pDrive->dwFAT2StartSector + dwFATSz;
  887. } /* endif pBootRecord->Signature */
  888. }
  889. /* endif dwSector != 0 */
  890. return (nError);
  891. }
  892. /*==========================================================*/
  893. /* DEFINE: All code exported by the NUTDEVICE */
  894. /*==========================================================*/
  895. /************************************************************/
  896. /* FATMountDrive */
  897. /************************************************************/
  898. static int FATMountDrive(int nDrive)
  899. {
  900. BYTE i;
  901. int nError;
  902. nError = IDE_OK;
  903. FATLock();
  904. nDrive = nDrive;
  905. if (pLongName1 == NULL) {
  906. pLongName1 = (char *) NutHeapAlloc(FAT_LONG_NAME_LEN);
  907. }
  908. if (pLongName2 == NULL) {
  909. pLongName2 = (char *) NutHeapAlloc(FAT_LONG_NAME_LEN);
  910. }
  911. if (pSectorBuffer == NULL) {
  912. pSectorBuffer = (BYTE *) NutHeapAlloc(MAX_SECTOR_SIZE);
  913. }
  914. if ((pSectorBuffer != NULL) && (pLongName1 != NULL) && (pLongName2 != NULL)) {
  915. for (i = 0; i < FAT_MAX_DRIVE; i++) {
  916. memset((BYTE *) & sDriveInfo[i], 0x00, sizeof(DRIVE_INFO));
  917. sDriveInfo[i].bDevice = i;
  918. sDriveInfo[i].wSectorSize = IDEGetSectorSize(i);
  919. if (IDEIsCDROMDevice(i) == TRUE) {
  920. sDriveInfo[i].bFlags |= FLAG_FAT_IS_CDROM;
  921. }
  922. if (IDEIsZIPDevice(i) == TRUE) {
  923. sDriveInfo[i].bFlags |= FLAG_FAT_IS_ZIP;
  924. }
  925. switch (sDriveInfo[i].wSectorSize) {
  926. case IDE_SECTOR_SIZE:{
  927. nError = MountIDE(i);
  928. break;
  929. }
  930. #if (IDE_SUPPORT_ATAPI == 1)
  931. case ATAPI_SECTOR_SIZE:{
  932. nError = MountATAPI(i);
  933. break;
  934. }
  935. #endif
  936. default:{
  937. nError = IDE_ERROR;
  938. break;
  939. }
  940. }
  941. } /* endfor i<FAT32_MAX_DRIVE */
  942. }
  943. /* endif pSectorBuffer != NULL */
  944. FATFree();
  945. return (nError);
  946. }
  947. /************************************************************/
  948. /* FATUnMountDrive */
  949. /************************************************************/
  950. static int FATUnMountDrive(int nDrive)
  951. {
  952. int nError;
  953. DRIVE_INFO *pDrive;
  954. FATLock();
  955. nError = FAT_OK;
  956. if ((nDrive >= IDE_DRIVE_C) && (nDrive <= IDE_DRIVE_D)) {
  957. pDrive = &sDriveInfo[nDrive];
  958. pDrive->bSectorsPerCluster = 0;
  959. } else {
  960. nError = FAT_ERROR;
  961. }
  962. FATFree();
  963. return (nError);
  964. }
  965. /************************************************************/
  966. /* CFMount */
  967. /************************************************************/
  968. static void CFMount(int nDrive)
  969. {
  970. BYTE *pSectorBuffer;
  971. pSectorBuffer = (BYTE *) NutHeapAlloc(MAX_SECTOR_SIZE);
  972. if (pSectorBuffer != NULL) {
  973. FATMountDrive(nDrive);
  974. NutHeapFree(pSectorBuffer);
  975. }
  976. }
  977. /************************************************************/
  978. /* CFUnMount */
  979. /************************************************************/
  980. static void CFUnMount(int nDrive)
  981. {
  982. FATUnMountDrive(nDrive);
  983. }
  984. /************************************************************/
  985. /* FATInit */
  986. /************************************************************/
  987. static int FATInit(NUTDEVICE * pDevice)
  988. {
  989. int nError;
  990. int nIDEMode;
  991. BYTE *pSectorBuffer;
  992. nError = NUTDEV_OK;
  993. nIDEMode = IDE_HARDDISK;
  994. pSectorBuffer = NULL;
  995. if (gnIsInit == FALSE) {
  996. gnIsInit = TRUE;
  997. //
  998. // Get the mode.
  999. //
  1000. switch (pDevice->dev_base) {
  1001. case FAT_MODE_IDE_HD:
  1002. nIDEMode = IDE_HARDDISK;
  1003. break;
  1004. case FAT_MODE_IDE_HD_7MHZ:
  1005. nIDEMode = IDE_HARDDISK_7MHZ;
  1006. break;
  1007. case FAT_MODE_IDE_CF:
  1008. nIDEMode = IDE_COMPACT_FLASH;
  1009. break;
  1010. case FAT_MODE_MEM_CF:
  1011. nIDEMode = MEM_8BIT_COMPACT_FLASH;
  1012. break;
  1013. default:
  1014. nError = NUTDEV_ERROR;
  1015. break;
  1016. }
  1017. if (nError == NUTDEV_OK) {
  1018. //
  1019. // Init my semaphore.
  1020. //
  1021. FATSemaInit();
  1022. pSectorBuffer = (BYTE *) NutHeapAlloc(MAX_SECTOR_SIZE);
  1023. if (pSectorBuffer != NULL) {
  1024. IDEInit(0, nIDEMode, CFMount, CFUnMount);
  1025. IDEMountDevice(IDE_DRIVE_C, pSectorBuffer);
  1026. FATMountDrive(IDE_DRIVE_C);
  1027. NutHeapFree(pSectorBuffer);
  1028. } /* endif pSectorBuffer != NULL */
  1029. } /* endif nError == NUTDEV_OK */
  1030. }
  1031. /* endif gnIsInit == FALSE */
  1032. return (nError);
  1033. }
  1034. /************************************************************/
  1035. /* FATFileOpen */
  1036. /* */
  1037. /* Opens an existing file for reading. */
  1038. /* */
  1039. /* Parameters: pName points to a string that specifies the */
  1040. /* name of the file to open. The name must */
  1041. /* exactly match the full pathname of the file.*/
  1042. /* */
  1043. /* Returns: A pointer to a FILE structure that can be */
  1044. /* used to read the file and retrieve */
  1045. /* information about the file. */
  1046. /* */
  1047. /* A return value of -1 indicates an error. */
  1048. /************************************************************/
  1049. static NUTFILE *FATFileOpen(NUTDEVICE * pDevice, const char *pName, int nMode, int nAccess)
  1050. {
  1051. int i, x;
  1052. int nError;
  1053. int nEndWhile;
  1054. DWORD dwFileSize;
  1055. DWORD dwCluster;
  1056. FHANDLE *hFile;
  1057. DRIVE_INFO *pDrive;
  1058. FAT32_DIRECTORY_ENTRY sDirEntry;
  1059. NUTFILE *hNUTFile;
  1060. int nLongName;
  1061. char *pLongName;
  1062. char *pShortName;
  1063. char *pExtension;
  1064. //
  1065. // If the user has forgotten to call NUTDeviceOpen,
  1066. // we must call FATInit.
  1067. //
  1068. if (gnIsInit == FALSE) {
  1069. FATInit(pDevice);
  1070. }
  1071. FATLock();
  1072. pDrive = NULL;
  1073. nError = TRUE;
  1074. nLongName = FALSE;
  1075. pLongName = (char *) pLongName1;
  1076. //
  1077. // hFile is our FAT-Handle.
  1078. //
  1079. hFile = NULL;
  1080. //
  1081. // hNUTFile is the.... correct, NUT handle.
  1082. //
  1083. hNUTFile = (NUTFILE *) NUTDEV_ERROR;
  1084. if (pDevice != NULL) {
  1085. switch (pDevice->dev_name[4]) {
  1086. case 'C':{
  1087. pDrive = &sDriveInfo[IDE_DRIVE_C];
  1088. break;
  1089. }
  1090. case 'D':{
  1091. pDrive = &sDriveInfo[IDE_DRIVE_D];
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. if ((pDrive != NULL) && (pDrive->bSectorsPerCluster != 0) && (pName[0] != 0)) {
  1097. //
  1098. // Create a new file handle.
  1099. //
  1100. hFile = (FHANDLE *) NutHeapAlloc(sizeof(FHANDLE));
  1101. if ((pDrive->dwFAT1StartSector) && (hFile != NULL) && (*pName != '.')) {
  1102. memset(hFile, 0x00, sizeof(FHANDLE));
  1103. //
  1104. // Start by the ROOT dir...
  1105. //
  1106. dwCluster = pDrive->dwRootCluster;
  1107. //
  1108. // If the first char a "/", jump over, e.g. "/index.html"
  1109. //
  1110. //
  1111. if (*pName == '/') {
  1112. pName++;
  1113. }
  1114. nEndWhile = FALSE;
  1115. while (nEndWhile == FALSE) { /* master loop */
  1116. nLongName = FALSE;
  1117. //
  1118. // Get Name
  1119. //
  1120. i = 0;
  1121. while ((*pName != '/') && (*pName != '\\') && (*pName != 0)) {
  1122. if (i >= (FAT_LONG_NAME_LEN - 1)) {
  1123. nEndWhile = TRUE;
  1124. break;
  1125. }
  1126. pLongName[i] = toupper(*pName);
  1127. i++;
  1128. pName++;
  1129. } /* endwhile Name */
  1130. if (nEndWhile == FALSE) {
  1131. pLongName[i] = 0;
  1132. //
  1133. // Check if it is a Long Directory Entry.
  1134. // Yes, I know that 'i' is the length of the string.
  1135. // But the code is easier to read with the next strlen.
  1136. //
  1137. if (strlen(pLongName) <= FAT_SHORT_NAME_LEN) {
  1138. //
  1139. // It could be a ShortName, but "abc.defg" is possible
  1140. // and this is a long name too. Therfore we need some tests.
  1141. //
  1142. pExtension = strchr(pLongName, '.');
  1143. if (pExtension == NULL) {
  1144. if (strlen(pLongName) > FAT_NAME_LEN) {
  1145. nLongName = TRUE;
  1146. } else {
  1147. nLongName = FALSE;
  1148. }
  1149. } else {
  1150. //
  1151. // Check the length of the extensions.
  1152. //
  1153. pExtension++; /* jump over the '.' */
  1154. if (strlen(pExtension) > 3) {
  1155. nLongName = TRUE;
  1156. }
  1157. }
  1158. } else { /* Len > FAT_SHORT_NAME_LEN */
  1159. //
  1160. // Now we have a LongName, sure.
  1161. // See the "nasty Win98" in FindFile :-)
  1162. //
  1163. nLongName = TRUE;
  1164. }
  1165. //
  1166. // Here we knows, if we have a LongName or ShortName.
  1167. //
  1168. if (nLongName == FALSE) {
  1169. //
  1170. // ShortName
  1171. //
  1172. pShortName = pLongName;
  1173. memset(&sDirEntry, 0x00, sizeof(FAT32_DIRECTORY_ENTRY));
  1174. memset(sDirEntry.Name, 0x20, FAT_NAME_LEN);
  1175. memset(sDirEntry.Extension, 0x20, FAT_EXT_LEN);
  1176. //
  1177. // Get the name
  1178. //
  1179. i = 0;
  1180. while ((pShortName[i] != '.') && (pShortName[i] != 0)) {
  1181. sDirEntry.Name[i] = pShortName[i];
  1182. i++;
  1183. }
  1184. //
  1185. // And the extension
  1186. //
  1187. if (pShortName[i] == '.') {
  1188. i++; /* jump over the '.' */
  1189. x = 0;
  1190. while (pShortName[i] != 0) {
  1191. sDirEntry.Extension[x] = pShortName[i];
  1192. i++;
  1193. x++;
  1194. }
  1195. }
  1196. }
  1197. //
  1198. // The file could be a long or short one.
  1199. // I have seen that Win98 store the short filename
  1200. // in a long one :-(
  1201. //
  1202. switch (*pName) {
  1203. //
  1204. // The file is an ARCHIVE
  1205. //
  1206. case 0:{
  1207. nEndWhile = TRUE;
  1208. sDirEntry.Attribute = DIRECTORY_ATTRIBUTE_ARCHIVE;
  1209. if (pDrive->bFlags & FLAG_FAT_IS_CDROM) {
  1210. dwCluster = FindFileATAPI(pDrive, &sDirEntry, pLongName, dwCluster, &dwFileSize, nLongName);
  1211. } else {
  1212. #if (IDE_SUPPORT_ATAPI == 1)
  1213. dwCluster = FindFile(pDrive, &sDirEntry, pLongName, dwCluster, &dwFileSize, nLongName);
  1214. #else
  1215. dwCluster = 0;
  1216. #endif
  1217. }
  1218. if (dwCluster != 0) {
  1219. hFile->dwFileSize = dwFileSize;
  1220. hFile->dwStartCluster = dwCluster;
  1221. hFile->dwReadCluster = dwCluster;
  1222. hFile->dwFilePointer = 0;
  1223. hFile->dwClusterPointer = 0;
  1224. hFile->pDrive = pDrive;
  1225. hFile->nLastError = FAT_OK;
  1226. hFile->nEOF = FALSE;
  1227. nError = FALSE;
  1228. }
  1229. break;
  1230. } /* endcase 0 */
  1231. //
  1232. // The file is a DIRECTORY
  1233. //
  1234. case '/':
  1235. case '\\':{
  1236. pName++; /* jump over the char */
  1237. sDirEntry.Attribute = DIRECTORY_ATTRIBUTE_DIRECTORY;
  1238. if (pDrive->bFlags & FLAG_FAT_IS_CDROM) {
  1239. dwCluster = FindFileATAPI(pDrive, &sDirEntry, pLongName, dwCluster, &dwFileSize, nLongName);
  1240. } else {
  1241. #if (IDE_SUPPORT_ATAPI == 1)
  1242. dwCluster = FindFile(pDrive, &sDirEntry, pLongName, dwCluster, &dwFileSize, nLongName);
  1243. #else
  1244. dwCluster = 0;
  1245. #endif
  1246. }
  1247. if (dwCluster != 0) {
  1248. //
  1249. // The new Cluster is the Cluster of the directory
  1250. //
  1251. } else {
  1252. nEndWhile = TRUE;
  1253. }
  1254. break;
  1255. } /* endcase / \ */
  1256. default:{
  1257. nEndWhile = TRUE;
  1258. break;
  1259. }
  1260. } /* end switch */
  1261. }
  1262. /* endif nEndWhile == FALSE */
  1263. } /* end while */
  1264. }
  1265. /* endif pDrive->dwFAT1StartSector */
  1266. if (nError == TRUE) {
  1267. //
  1268. // We found no file, therefore we can delete our FAT-Handle
  1269. //
  1270. if (hFile != NULL) {
  1271. NutHeapFree(hFile);
  1272. }
  1273. } else {
  1274. //
  1275. // We have found a FILE and can create a NUT-Handle.
  1276. //
  1277. hNUTFile = NutHeapAlloc(sizeof(NUTFILE));
  1278. if (hNUTFile != NULL) {
  1279. hNUTFile->nf_dev = pDevice;
  1280. hNUTFile->nf_fcb = hFile;
  1281. } else {
  1282. //
  1283. // Error, no mem for the NUT-Handle, therefore we
  1284. // can delete our FAT-Handle too.
  1285. //
  1286. NutHeapFree(hFile);
  1287. }
  1288. }
  1289. }
  1290. /* endif pName[0] != 0 */
  1291. FATFree();
  1292. return (hNUTFile);
  1293. }
  1294. /************************************************************/
  1295. /* FATFileClose */
  1296. /* */
  1297. /* Close a previously opened file. */
  1298. /* */
  1299. /* Parameters: hNUTFile Identifies the file to close. */
  1300. /* This pointer must have been created by */
  1301. /* calling FAT32FileOpen(). */
  1302. /* */
  1303. /* Returns: 0 if the function is successfully closed, */
  1304. /* -1 otherwise. */
  1305. /************************************************************/
  1306. static int FATFileClose(NUTFILE * hNUTFile)
  1307. {
  1308. int nError;
  1309. FHANDLE *hFile;
  1310. nError = NUTDEV_ERROR;
  1311. FATLock();
  1312. if (hNUTFile != NULL) {
  1313. hFile = (FHANDLE *) hNUTFile->nf_fcb;
  1314. if (hFile != NULL) {
  1315. //
  1316. // Clear our FAT-Handle
  1317. //
  1318. NutHeapFree(hFile);
  1319. }
  1320. //
  1321. // Clear the NUT-Handle
  1322. //
  1323. NutHeapFree(hNUTFile);
  1324. nError = NUTDEV_OK;
  1325. }
  1326. FATFree();
  1327. return (nError);
  1328. }
  1329. /************************************************************/
  1330. /* FATFileSize */
  1331. /* */
  1332. /* Retrieve the size of a file. */
  1333. /* */
  1334. /* Parameters: pFile Identifies the file to query. */
  1335. /* This pointer must have been created by */
  1336. /* calling FAT32FileOpen(). */
  1337. /* */
  1338. /* Returns: The number of bytes in this file or */
  1339. /* -1 if an error occured */
  1340. /************************************************************/
  1341. long FATFileSize(NUTFILE * hNUTFile)
  1342. {
  1343. long lSize;
  1344. FHANDLE *hFile;
  1345. FATLock();
  1346. lSize = NUTDEV_ERROR;
  1347. if (hNUTFile != NULL) {
  1348. hFile = (FHANDLE *) hNUTFile->nf_fcb;
  1349. if (hFile != NULL) {
  1350. lSize = hFile->dwFileSize;
  1351. }
  1352. }
  1353. FATFree();
  1354. return (lSize);
  1355. }
  1356. #if 0
  1357. /************************************************************/
  1358. /* FATFileSeek */
  1359. /* */
  1360. /* Move the file pointer to a new position. */
  1361. /* It points to the next byte to be read from a file. */
  1362. /* The file pointer is automatically incremented for */
  1363. /* each byte read. When the file is opened, it is at */
  1364. /* position 0, the beginning of the file. */
  1365. /* */
  1366. /* Parameters: pFile Identifies the file to seek. */
  1367. /* This pointer must have been created by */
  1368. /* calling FAT32FileOpen(). */
  1369. /* */
  1370. /* lPos Specifies the new absolute position */
  1371. /* of the file pointer. */
  1372. /* */
  1373. /* Returns: 0 if the function is successful, */
  1374. /* -1 otherwise. */
  1375. /************************************************************/
  1376. int FATFileSeek(FILE * pFile, long lPos)
  1377. {
  1378. int nError = NUTDEV_ERROR;
  1379. FHANDLE *hFile;
  1380. FATLock();
  1381. hFile = (FHANDLE *) pFile;
  1382. //
  1383. // We must do some work here...
  1384. //
  1385. nError = FAT32_ERROR;
  1386. FATFree();
  1387. return (nError);
  1388. }
  1389. #endif
  1390. /************************************************************/
  1391. /* FATFileRead */
  1392. /* */
  1393. /* Read data from a file. */
  1394. /* */
  1395. /* Parameters: pFile Identifies the file to read from. */
  1396. /* This pointer must have been created by */
  1397. /* calling FAT32FileOpen(). */
  1398. /* */
  1399. /* pData Points to the buffer that receives */
  1400. /* the data. */
  1401. /* */
  1402. /* nSize Specifies the number of bytes to */
  1403. /* read from the file. */
  1404. /* */
  1405. /* Returns: The number of bytes read from the file or */
  1406. /* -1 if an error occured. */
  1407. /************************************************************/
  1408. int FATFileRead(NUTFILE * hNUTFile, void *pData, int nSize)
  1409. {
  1410. int nError;
  1411. int nBytesRead;
  1412. int nBytesToRead;
  1413. FHANDLE *hFile;
  1414. DRIVE_INFO *pDrive;
  1415. BYTE *pByte;
  1416. DWORD dwReadSector;
  1417. DWORD dwSector;
  1418. int nSectorCount;
  1419. int nSectorOffset;
  1420. WORD wSectorSize;
  1421. nBytesRead = 0;
  1422. FATLock();
  1423. hFile = NULL;
  1424. nError = NUTDEV_ERROR;
  1425. if (hNUTFile != NULL) {
  1426. hFile = (FHANDLE *) hNUTFile->nf_fcb;
  1427. }
  1428. if ((hFile != NULL) && (nSize != 0)) {
  1429. if (hFile->dwFilePointer < hFile->dwFileSize) {
  1430. if ((hFile->dwFilePointer + nSize) > hFile->dwFileSize) {
  1431. nSize = (int) (hFile->dwFileSize - hFile->dwFilePointer);
  1432. }
  1433. pDrive = (DRIVE_INFO *) hFile->pDrive;
  1434. pByte = (BYTE *) pData;
  1435. nBytesRead = nSize;
  1436. wSectorSize = pDrive->wSectorSize;
  1437. while (nSize) {
  1438. dwSector = GetFirstSectorOfCluster(pDrive, hFile->dwReadCluster);
  1439. nSectorCount = hFile->dwClusterPointer / wSectorSize;
  1440. nSectorOffset = hFile->dwClusterPointer % wSectorSize;
  1441. //
  1442. // (Sector + SectorCount) is the sector to read
  1443. // SectorOffset is the start position in the sector itself
  1444. //
  1445. dwReadSector = dwSector + nSectorCount;
  1446. nError = IDEReadSectors(pDrive->bDevice, pSectorBuffer, dwReadSector, 1);
  1447. if (nError == IDE_OK) {
  1448. //
  1449. // Find the size we can read from ONE sector
  1450. //
  1451. if (nSize > (int) wSectorSize) {
  1452. nBytesToRead = wSectorSize;
  1453. } else {
  1454. nBytesToRead = nSize;
  1455. }
  1456. //
  1457. // Test inside a sector
  1458. //
  1459. if ((nSectorOffset + nBytesToRead) > (int) wSectorSize) {
  1460. nBytesToRead = wSectorSize - nSectorOffset;
  1461. }
  1462. memcpy(pByte, &pSectorBuffer[nSectorOffset], nBytesToRead);
  1463. pByte += nBytesToRead;
  1464. hFile->dwFilePointer += nBytesToRead;
  1465. hFile->dwClusterPointer += nBytesToRead;
  1466. //
  1467. // Check for EOF
  1468. if (hFile->dwFilePointer >= hFile->dwFileSize) {
  1469. hFile->nEOF = TRUE;
  1470. }
  1471. if (hFile->dwClusterPointer >= pDrive->dwClusterSize) {
  1472. //
  1473. // We must switch to the next cluster
  1474. //
  1475. hFile->dwReadCluster = GetNextCluster(pDrive, hFile->dwReadCluster);
  1476. hFile->dwClusterPointer = 0;
  1477. }
  1478. nSize -= nBytesToRead;
  1479. } else { /* IDEReadSectors Error */
  1480. nBytesRead = 0;
  1481. hFile->nLastError = FAT_ERROR_IDE;
  1482. break;
  1483. } /* endif nError == IDE_OK */
  1484. } /* endwhile */
  1485. } else { /* reached the EOF */
  1486. hFile->nLastError = FAT_ERROR_EOF;
  1487. } /* endif hFile->dwFilePointer < hFile->dwFileSize */
  1488. }
  1489. /* endif (hFile != NULL) && (nSize != 0) */
  1490. FATFree();
  1491. return (nBytesRead);
  1492. }
  1493. static int FATFileWrite(NUTFILE * hNUTFile, const void *pData, int nSize)
  1494. {
  1495. int nError;
  1496. nError = NUTDEV_ERROR;
  1497. return (nError);
  1498. }
  1499. #ifdef __HARVARD_ARCH__
  1500. static int FATFileWriteP(NUTFILE * hNUTFile, PGM_P pData, int nSize)
  1501. {
  1502. int nError;
  1503. nError = NUTDEV_ERROR;
  1504. return (nError);
  1505. }
  1506. #endif
  1507. //
  1508. // FATC Device information structure.
  1509. //
  1510. NUTDEVICE devFATC = {
  1511. 0, /* Pointer to next device. */
  1512. /* Unique device name. */
  1513. {'F', 'A', 'T', '_', 'C', 0, 0, 0, 0}
  1514. ,
  1515. IFTYP_STREAM, /* Type of device. */
  1516. 2, /* Base address. */
  1517. 0, /* First interrupt number. */
  1518. 0, /* Interface control block. */
  1519. 0, /* Driver control block. */
  1520. FATInit, /* Driver initialization routine. */
  1521. 0, /* Driver specific control function. */
  1522. FATFileRead, /* Driver specific read function. */
  1523. FATFileWrite, /* Driver specific write function. */
  1524. #ifdef __HARVARD_ARCH__
  1525. FATFileWriteP, /* Driver specific write_p function. */
  1526. #endif
  1527. FATFileOpen, /* Driver specific open function. */
  1528. FATFileClose, /* Driver specific close function. */
  1529. FATFileSize, /* Driver specific size function. */
  1530. NULL, /* Driver specific select function. */
  1531. };