ff.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. /*----------------------------------------------------------------------------/
  2. / FatFs - FAT file system module R0.06 (C)ChaN, 2008
  3. /-----------------------------------------------------------------------------/
  4. / The FatFs module is an experimenal project to implement FAT file system to
  5. / cheap microcontrollers. This is a free software and is opened for education,
  6. / research and development under license policy of following trems.
  7. /
  8. / Copyright (C) 2008, ChaN, all right reserved.
  9. /
  10. / * The FatFs module is a free software and there is no warranty.
  11. / * You can use, modify and/or redistribute it for personal, non-profit or
  12. / commercial use without restriction under your responsibility.
  13. / * Redistributions of source code must retain the above copyright notice.
  14. /
  15. /-----------------------------------------------------------------------------/
  16. / Feb 26,'06 R0.00 Prototype.
  17. /
  18. / Apr 29,'06 R0.01 First stable version.
  19. /
  20. / Jun 01,'06 R0.02 Added FAT12 support.
  21. / Removed unbuffered mode.
  22. / Fixed a problem on small (<32M) patition.
  23. / Jun 10,'06 R0.02a Added a configuration option (_FS_MINIMUM).
  24. /
  25. / Sep 22,'06 R0.03 Added f_rename().
  26. / Changed option _FS_MINIMUM to _FS_MINIMIZE.
  27. / Dec 11,'06 R0.03a Improved cluster scan algolithm to write files fast.
  28. / Fixed f_mkdir() creates incorrect directory on FAT32.
  29. /
  30. / Feb 04,'07 R0.04 Supported multiple drive system.
  31. / Changed some interfaces for multiple drive system.
  32. / Changed f_mountdrv() to f_mount().
  33. / Added f_mkfs().
  34. / Apr 01,'07 R0.04a Supported multiple partitions on a plysical drive.
  35. / Added a capability of extending file size to f_lseek().
  36. / Added minimization level 3.
  37. / Fixed an endian sensitive code in f_mkfs().
  38. / May 05,'07 R0.04b Added a configuration option _USE_NTFLAG.
  39. / Added FSInfo support.
  40. / Fixed DBCS name can result FR_INVALID_NAME.
  41. / Fixed short seek (<= csize) collapses the file object.
  42. /
  43. / Aug 25,'07 R0.05 Changed arguments of f_read(), f_write() and f_mkfs().
  44. / Fixed f_mkfs() on FAT32 creates incorrect FSInfo.
  45. / Fixed f_mkdir() on FAT32 creates incorrect directory.
  46. / Feb 03,'08 R0.05a Added f_truncate() and f_utime().
  47. / Fixed off by one error at FAT sub-type determination.
  48. / Fixed btr in f_read() can be mistruncated.
  49. / Fixed cached sector is not flushed when create and close
  50. / without write.
  51. /
  52. / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
  53. / Improved performance of f_lseek() on moving to the same
  54. / or following cluster.
  55. /---------------------------------------------------------------------------*/
  56. #include <string.h>
  57. #include "ff.h" /* FatFs declarations */
  58. #include "diskio.h" /* Include file for user provided disk functions */
  59. /*--------------------------------------------------------------------------
  60. Module Private Functions
  61. ---------------------------------------------------------------------------*/
  62. DWORD get_fattime(void)
  63. {
  64. return 0x12345678;
  65. }
  66. static
  67. FATFS *FatFs[_DRIVES]; /* Pointer to the file system objects (logical drives) */
  68. static
  69. WORD fsid; /* File system mount ID */
  70. FATFS g_fs;
  71. /*-----------------------------------------------------------------------*/
  72. /* Change window offset */
  73. /*-----------------------------------------------------------------------*/
  74. static
  75. BOOL move_window ( /* TRUE: successful, FALSE: failed */
  76. FATFS *fs, /* File system object */
  77. DWORD sector /* Sector number to make apperance in the fs->win[] */
  78. ) /* Move to zero only writes back dirty window */
  79. {
  80. DWORD wsect;
  81. wsect = fs->winsect;
  82. if (wsect != sector) { /* Changed current window */
  83. #if !_FS_READONLY
  84. BYTE n;
  85. if (fs->winflag) { /* Write back dirty window if needed */
  86. if (disk_write(fs->drive, fs->win, wsect, 1) != RES_OK)
  87. return FALSE;
  88. fs->winflag = 0;
  89. if (wsect < (fs->fatbase + fs->sects_fat)) { /* In FAT area */
  90. for (n = fs->n_fats; n >= 2; n--) { /* Refrect the change to FAT copy */
  91. wsect += fs->sects_fat;
  92. disk_write(fs->drive, fs->win, wsect, 1);
  93. }
  94. }
  95. }
  96. #endif
  97. if (sector) {
  98. if (disk_read(fs->drive, fs->win, sector, 1) != RES_OK)
  99. return FALSE;
  100. fs->winsect = sector;
  101. }
  102. }
  103. return TRUE;
  104. }
  105. /*-----------------------------------------------------------------------*/
  106. /* Clean-up cached data */
  107. /*-----------------------------------------------------------------------*/
  108. #if !_FS_READONLY
  109. static
  110. FRESULT sync ( /* FR_OK: successful, FR_RW_ERROR: failed */
  111. FATFS *fs /* File system object */
  112. )
  113. {
  114. fs->winflag = 1;
  115. if (!move_window(fs, 0)) return FR_RW_ERROR;
  116. #if _USE_FSINFO
  117. /* Update FSInfo sector if needed */
  118. if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
  119. fs->winsect = 0;
  120. memset(fs->win, 0, 512);
  121. ST_WORD(&fs->win[BS_55AA], 0xAA55);
  122. ST_DWORD(&fs->win[FSI_LeadSig], 0x41615252);
  123. ST_DWORD(&fs->win[FSI_StrucSig], 0x61417272);
  124. ST_DWORD(&fs->win[FSI_Free_Count], fs->free_clust);
  125. ST_DWORD(&fs->win[FSI_Nxt_Free], fs->last_clust);
  126. disk_write(fs->drive, fs->win, fs->fsi_sector, 1);
  127. fs->fsi_flag = 0;
  128. }
  129. #endif
  130. /* Make sure that no pending write process in the physical drive */
  131. if (disk_ioctl(fs->drive, CTRL_SYNC, NULL) != RES_OK)
  132. return FR_RW_ERROR;
  133. return FR_OK;
  134. }
  135. #endif
  136. /*-----------------------------------------------------------------------*/
  137. /* Get a cluster status */
  138. /*-----------------------------------------------------------------------*/
  139. static
  140. DWORD get_cluster ( /* 0,>=2: successful, 1: failed */
  141. FATFS *fs, /* File system object */
  142. DWORD clust /* Cluster# to get the link information */
  143. )
  144. {
  145. WORD wc, bc;
  146. DWORD fatsect;
  147. if (clust >= 2 && clust < fs->max_clust) { /* Is it a valid cluster#? */
  148. fatsect = fs->fatbase;
  149. switch (fs->fs_type) {
  150. case FS_FAT12 :
  151. bc = (WORD)clust * 3 / 2;
  152. if (!move_window(fs, fatsect + (bc / SS(fs)))) break;
  153. wc = fs->win[bc & (SS(fs) - 1)]; bc++;
  154. if (!move_window(fs, fatsect + (bc / SS(fs)))) break;
  155. wc |= (WORD)fs->win[bc & (SS(fs) - 1)] << 8;
  156. return (clust & 1) ? (wc >> 4) : (wc & 0xFFF);
  157. case FS_FAT16 :
  158. if (!move_window(fs, fatsect + (clust / (SS(fs) / 2)))) break;
  159. return LD_WORD(&fs->win[((WORD)clust * 2) & (SS(fs) - 1)]);
  160. case FS_FAT32 :
  161. if (!move_window(fs, fatsect + (clust / (SS(fs) / 4)))) break;
  162. return LD_DWORD(&fs->win[((WORD)clust * 4) & (SS(fs) - 1)]) & 0x0FFFFFFF;
  163. }
  164. }
  165. return 1; /* Out of cluster range, or an error occured */
  166. }
  167. /*-----------------------------------------------------------------------*/
  168. /* Change a cluster status */
  169. /*-----------------------------------------------------------------------*/
  170. #if !_FS_READONLY
  171. static
  172. BOOL put_cluster ( /* TRUE: successful, FALSE: failed */
  173. FATFS *fs, /* File system object */
  174. DWORD clust, /* Cluster# to change (must be 2 to fs->max_clust-1) */
  175. DWORD val /* New value to mark the cluster */
  176. )
  177. {
  178. WORD bc;
  179. BYTE *p;
  180. DWORD fatsect;
  181. fatsect = fs->fatbase;
  182. switch (fs->fs_type) {
  183. case FS_FAT12 :
  184. bc = (WORD)clust * 3 / 2;
  185. if (!move_window(fs, fatsect + (bc / SS(fs)))) return FALSE;
  186. p = &fs->win[bc & (SS(fs) - 1)];
  187. *p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
  188. bc++;
  189. fs->winflag = 1;
  190. if (!move_window(fs, fatsect + (bc / SS(fs)))) return FALSE;
  191. p = &fs->win[bc & (SS(fs) - 1)];
  192. *p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
  193. break;
  194. case FS_FAT16 :
  195. if (!move_window(fs, fatsect + (clust / (SS(fs) / 2)))) return FALSE;
  196. ST_WORD(&fs->win[((WORD)clust * 2) & (SS(fs) - 1)], (WORD)val);
  197. break;
  198. case FS_FAT32 :
  199. if (!move_window(fs, fatsect + (clust / (SS(fs) / 4)))) return FALSE;
  200. ST_DWORD(&fs->win[((WORD)clust * 4) & (SS(fs) - 1)], val);
  201. break;
  202. default :
  203. return FALSE;
  204. }
  205. fs->winflag = 1;
  206. return TRUE;
  207. }
  208. #endif /* !_FS_READONLY */
  209. /*-----------------------------------------------------------------------*/
  210. /* Remove a cluster chain */
  211. /*-----------------------------------------------------------------------*/
  212. #if !_FS_READONLY
  213. static
  214. BOOL remove_chain ( /* TRUE: successful, FALSE: failed */
  215. FATFS *fs, /* File system object */
  216. DWORD clust /* Cluster# to remove chain from */
  217. )
  218. {
  219. DWORD nxt;
  220. while (clust >= 2 && clust < fs->max_clust) {
  221. nxt = get_cluster(fs, clust);
  222. if (nxt == 1) return FALSE;
  223. if (!put_cluster(fs, clust, 0)) return FALSE;
  224. if (fs->free_clust != 0xFFFFFFFF) {
  225. fs->free_clust++;
  226. #if _USE_FSINFO
  227. fs->fsi_flag = 1;
  228. #endif
  229. }
  230. clust = nxt;
  231. }
  232. return TRUE;
  233. }
  234. #endif
  235. /*-----------------------------------------------------------------------*/
  236. /* Stretch or create a cluster chain */
  237. /*-----------------------------------------------------------------------*/
  238. #if !_FS_READONLY
  239. static
  240. DWORD create_chain ( /* 0: No free cluster, 1: Error, >=2: New cluster number */
  241. FATFS *fs, /* File system object */
  242. DWORD clust /* Cluster# to stretch, 0 means create new */
  243. )
  244. {
  245. DWORD cstat, ncl, scl, mcl = fs->max_clust;
  246. if (clust == 0) { /* Create new chain */
  247. scl = fs->last_clust; /* Get suggested start point */
  248. if (scl == 0 || scl >= mcl) scl = 1;
  249. }
  250. else { /* Stretch existing chain */
  251. cstat = get_cluster(fs, clust); /* Check the cluster status */
  252. if (cstat < 2) return 1; /* It is an invalid cluster */
  253. if (cstat < mcl) return cstat; /* It is already followed by next cluster */
  254. scl = clust;
  255. }
  256. ncl = scl; /* Start cluster */
  257. for (;;) {
  258. ncl++; /* Next cluster */
  259. if (ncl >= mcl) { /* Wrap around */
  260. ncl = 2;
  261. if (ncl > scl) return 0; /* No free custer */
  262. }
  263. cstat = get_cluster(fs, ncl); /* Get the cluster status */
  264. if (cstat == 0) break; /* Found a free cluster */
  265. if (cstat == 1) return 1; /* Any error occured */
  266. if (ncl == scl) return 0; /* No free custer */
  267. }
  268. if (!put_cluster(fs, ncl, 0x0FFFFFFF)) return 1; /* Mark the new cluster "in use" */
  269. if (clust != 0 && !put_cluster(fs, clust, ncl)) return 1; /* Link it to previous one if needed */
  270. fs->last_clust = ncl; /* Update fsinfo */
  271. if (fs->free_clust != 0xFFFFFFFF) {
  272. fs->free_clust--;
  273. #if _USE_FSINFO
  274. fs->fsi_flag = 1;
  275. #endif
  276. }
  277. return ncl; /* Return new cluster number */
  278. }
  279. #endif /* !_FS_READONLY */
  280. /*-----------------------------------------------------------------------*/
  281. /* Get sector# from cluster# */
  282. /*-----------------------------------------------------------------------*/
  283. static
  284. DWORD clust2sect ( /* !=0: sector number, 0: failed - invalid cluster# */
  285. FATFS *fs, /* File system object */
  286. DWORD clust /* Cluster# to be converted */
  287. )
  288. {
  289. clust -= 2;
  290. if (clust >= (fs->max_clust - 2)) return 0; /* Invalid cluster# */
  291. return clust * fs->csize + fs->database;
  292. }
  293. /*-----------------------------------------------------------------------*/
  294. /* Move directory pointer to next */
  295. /*-----------------------------------------------------------------------*/
  296. static
  297. BOOL next_dir_entry ( /* TRUE: successful, FALSE: could not move next */
  298. DIR *dj /* Pointer to directory object */
  299. )
  300. {
  301. DWORD clust;
  302. WORD idx;
  303. idx = dj->index + 1;
  304. if ((idx & ((SS(dj->fs) - 1) / 32)) == 0) { /* Table sector changed? */
  305. dj->sect++; /* Next sector */
  306. if (dj->clust == 0) { /* In static table */
  307. if (idx >= dj->fs->n_rootdir) return FALSE; /* Reached to end of table */
  308. } else { /* In dynamic table */
  309. if (((idx / (SS(dj->fs) / 32)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
  310. clust = get_cluster(dj->fs, dj->clust); /* Get next cluster */
  311. if (clust < 2 || clust >= dj->fs->max_clust) /* Reached to end of table */
  312. return FALSE;
  313. dj->clust = clust; /* Initialize for new cluster */
  314. dj->sect = clust2sect(dj->fs, clust);
  315. }
  316. }
  317. }
  318. dj->index = idx; /* Lower several bits of dj->index indicates offset in dj->sect */
  319. return TRUE;
  320. }
  321. /*-----------------------------------------------------------------------*/
  322. /* Get file status from directory entry */
  323. /*-----------------------------------------------------------------------*/
  324. #if _FS_MINIMIZE <= 1
  325. static
  326. void get_fileinfo ( /* No return code */
  327. FILINFO *finfo, /* Ptr to store the file information */
  328. const BYTE *dir /* Ptr to the directory entry */
  329. )
  330. {
  331. BYTE n, c, a;
  332. char *p;
  333. p = &finfo->fname[0];
  334. a = _USE_NTFLAG ? dir[DIR_NTres] : 0; /* NT flag */
  335. for (n = 0; n < 8; n++) { /* Convert file name (body) */
  336. c = dir[n];
  337. if (c == ' ') break;
  338. if (c == 0x05) c = 0xE5;
  339. if (a & 0x08 && c >= 'A' && c <= 'Z') c += 0x20;
  340. *p++ = c;
  341. }
  342. if (dir[8] != ' ') { /* Convert file name (extension) */
  343. *p++ = '.';
  344. for (n = 8; n < 11; n++) {
  345. c = dir[n];
  346. if (c == ' ') break;
  347. if (a & 0x10 && c >= 'A' && c <= 'Z') c += 0x20;
  348. *p++ = c;
  349. }
  350. }
  351. *p = '\0';
  352. finfo->fattrib = dir[DIR_Attr]; /* Attribute */
  353. finfo->fsize = LD_DWORD(&dir[DIR_FileSize]); /* Size */
  354. finfo->fdate = LD_WORD(&dir[DIR_WrtDate]); /* Date */
  355. finfo->ftime = LD_WORD(&dir[DIR_WrtTime]); /* Time */
  356. }
  357. #endif /* _FS_MINIMIZE <= 1 */
  358. /*-----------------------------------------------------------------------*/
  359. /* Pick a paragraph and create the name in format of directory entry */
  360. /*-----------------------------------------------------------------------*/
  361. static
  362. char make_dirfile ( /* 1: error - detected an invalid format, '\0'or'/': next character */
  363. const char **path, /* Pointer to the file path pointer */
  364. char *dirname /* Pointer to directory name buffer {Name(8), Ext(3), NT flag(1)} */
  365. )
  366. {
  367. BYTE n, t, c, a, b;
  368. memset(dirname, ' ', 8+3); /* Fill buffer with spaces */
  369. a = 0; b = 0x18; /* NT flag */
  370. n = 0; t = 8;
  371. for (;;) {
  372. c = *(*path)++;
  373. if (c == '\0' || c == '/') { /* Reached to end of str or directory separator */
  374. if (n == 0) break;
  375. dirname[11] = _USE_NTFLAG ? (a & b) : 0;
  376. return c;
  377. }
  378. if (c <= ' ' || c == 0x7F) break; /* Reject invisible chars */
  379. if (c == '.') {
  380. if (!(a & 1) && n >= 1 && n <= 8) { /* Enter extension part */
  381. n = 8; t = 11; continue;
  382. }
  383. break;
  384. }
  385. if (_USE_SJIS &&
  386. ((c >= 0x81 && c <= 0x9F) || /* Accept S-JIS code */
  387. (c >= 0xE0 && c <= 0xFC))) {
  388. if (n == 0 && c == 0xE5) /* Change heading \xE5 to \x05 */
  389. c = 0x05;
  390. a ^= 0x01; goto md_l2;
  391. }
  392. if (c == '"') break; /* Reject " */
  393. if (c <= ')') goto md_l1; /* Accept ! # $ % & ' ( ) */
  394. if (c <= ',') break; /* Reject * + , */
  395. if (c <= '9') goto md_l1; /* Accept - 0-9 */
  396. if (c <= '?') break; /* Reject : ; < = > ? */
  397. if (!(a & 1)) { /* These checks are not applied to S-JIS 2nd byte */
  398. if (c == '|') break; /* Reject | */
  399. if (c >= '[' && c <= ']') break;/* Reject [ \ ] */
  400. if (_USE_NTFLAG && c >= 'A' && c <= 'Z')
  401. (t == 8) ? (b &= 0xF7) : (b &= 0xEF);
  402. if (c >= 'a' && c <= 'z') { /* Convert to upper case */
  403. c -= 0x20;
  404. if (_USE_NTFLAG) (t == 8) ? (a |= 0x08) : (a |= 0x10);
  405. }
  406. }
  407. md_l1:
  408. a &= 0xFE;
  409. md_l2:
  410. if (n >= t) break;
  411. dirname[n++] = c;
  412. }
  413. return 1;
  414. }
  415. /*-----------------------------------------------------------------------*/
  416. /* Trace a file path */
  417. /*-----------------------------------------------------------------------*/
  418. static
  419. FRESULT trace_path ( /* FR_OK(0): successful, !=0: error code */
  420. DIR *dj, /* Pointer to directory object to return last directory */
  421. char *fn, /* Pointer to last segment name to return {file(8),ext(3),attr(1)} */
  422. const char *path, /* Full-path string to trace a file or directory */
  423. BYTE **dir /* Pointer to pointer to found entry to retutn */
  424. )
  425. {
  426. DWORD clust;
  427. char ds;
  428. BYTE *dptr = NULL;
  429. FATFS *fs = dj->fs;
  430. /* Initialize directory object */
  431. clust = fs->dirbase;
  432. if (fs->fs_type == FS_FAT32) {
  433. dj->clust = dj->sclust = clust;
  434. dj->sect = clust2sect(fs, clust);
  435. } else {
  436. dj->clust = dj->sclust = 0;
  437. dj->sect = clust;
  438. }
  439. dj->index = 0;
  440. if (*path == '\0') { /* Null path means the root directory */
  441. *dir = NULL; return FR_OK;
  442. }
  443. for (;;) {
  444. ds = make_dirfile(&path, fn); /* Get a paragraph into fn[] */
  445. if (ds == 1) return FR_INVALID_NAME;
  446. for (;;) {
  447. if (!move_window(fs, dj->sect)) return FR_RW_ERROR;
  448. dptr = &fs->win[(dj->index & ((SS(fs) - 1) / 32)) * 32]; /* Pointer to the directory entry */
  449. if (dptr[DIR_Name] == 0) /* Has it reached to end of dir? */
  450. return !ds ? FR_NO_FILE : FR_NO_PATH;
  451. if (dptr[DIR_Name] != 0xE5 /* Matched? */
  452. && !(dptr[DIR_Attr] & AM_VOL)
  453. && !memcmp(&dptr[DIR_Name], fn, 8+3) ) break;
  454. if (!next_dir_entry(dj)) /* Next directory pointer */
  455. return !ds ? FR_NO_FILE : FR_NO_PATH;
  456. }
  457. if (!ds) { *dir = dptr; return FR_OK; } /* Matched with end of path */
  458. if (!(dptr[DIR_Attr] & AM_DIR)) return FR_NO_PATH; /* Cannot trace because it is a file */
  459. clust = ((DWORD)LD_WORD(&dptr[DIR_FstClusHI]) << 16) | LD_WORD(&dptr[DIR_FstClusLO]); /* Get cluster# of the directory */
  460. dj->clust = dj->sclust = clust; /* Restart scanning at the new directory */
  461. dj->sect = clust2sect(fs, clust);
  462. dj->index = 2;
  463. }
  464. }
  465. /*-----------------------------------------------------------------------*/
  466. /* Reserve a directory entry */
  467. /*-----------------------------------------------------------------------*/
  468. #if !_FS_READONLY
  469. static
  470. FRESULT reserve_direntry ( /* FR_OK: successful, FR_DENIED: no free entry, FR_RW_ERROR: a disk error occured */
  471. DIR *dj, /* Target directory to create new entry */
  472. BYTE **dir /* Pointer to pointer to created entry to retutn */
  473. )
  474. {
  475. DWORD clust, sector;
  476. BYTE c, n, *dptr;
  477. FATFS *fs = dj->fs;
  478. /* Re-initialize directory object */
  479. clust = dj->sclust;
  480. if (clust != 0) { /* Dyanmic directory table */
  481. dj->clust = clust;
  482. dj->sect = clust2sect(fs, clust);
  483. } else { /* Static directory table */
  484. dj->sect = fs->dirbase;
  485. }
  486. dj->index = 0;
  487. do {
  488. if (!move_window(fs, dj->sect)) return FR_RW_ERROR;
  489. dptr = &fs->win[(dj->index & ((SS(dj->fs) - 1) / 32)) * 32]; /* Pointer to the directory entry */
  490. c = dptr[DIR_Name];
  491. if (c == 0 || c == 0xE5) { /* Found an empty entry */
  492. *dir = dptr; return FR_OK;
  493. }
  494. } while (next_dir_entry(dj)); /* Next directory pointer */
  495. /* Reached to end of the directory table */
  496. /* Abort when it is a static table or could not stretch dynamic table */
  497. if (clust == 0 || !(clust = create_chain(fs, dj->clust))) return FR_DENIED;
  498. if (clust == 1 || !move_window(fs, 0)) return FR_RW_ERROR;
  499. /* Cleanup the expanded table */
  500. fs->winsect = sector = clust2sect(fs, clust);
  501. memset(fs->win, 0, SS(fs));
  502. for (n = fs->csize; n; n--) {
  503. if (disk_write(fs->drive, fs->win, sector, 1) != RES_OK)
  504. return FR_RW_ERROR;
  505. sector++;
  506. }
  507. fs->winflag = 1;
  508. *dir = fs->win;
  509. return FR_OK;
  510. }
  511. #endif /* !_FS_READONLY */
  512. /*-----------------------------------------------------------------------*/
  513. /* Load boot record and check if it is an FAT boot record */
  514. /*-----------------------------------------------------------------------*/
  515. static
  516. BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record or error */
  517. FATFS *fs, /* File system object */
  518. DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
  519. )
  520. {
  521. if (disk_read(fs->drive, fs->win, sect, 1) != RES_OK) /* Load boot record */
  522. return 2;
  523. if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */
  524. return 2;
  525. if (!memcmp(&fs->win[BS_FilSysType], "FAT", 3)) /* Check FAT signature */
  526. return 0;
  527. if (!memcmp(&fs->win[BS_FilSysType32], "FAT32", 5) && !(fs->win[BPB_ExtFlags] & 0x80))
  528. return 0;
  529. return 1;
  530. }
  531. /*-----------------------------------------------------------------------*/
  532. /* Make sure that the file system is valid */
  533. /*-----------------------------------------------------------------------*/
  534. static
  535. FRESULT auto_mount ( /* FR_OK(0): successful, !=0: any error occured */
  536. const char **path, /* Pointer to pointer to the path name (drive number) */
  537. FATFS **rfs, /* Pointer to pointer to the found file system object */
  538. BYTE chk_wp /* !=0: Check media write protection for write access */
  539. )
  540. {
  541. BYTE drv, fmt, *tbl;
  542. DSTATUS stat;
  543. DWORD bootsect, fatsize, totalsect, maxclust;
  544. const char *p = *path;
  545. FATFS *fs;
  546. /* Get drive number from the path name */
  547. while (*p == ' ') p++; /* Strip leading spaces */
  548. drv = p[0] - '0'; /* Is there a drive number? */
  549. if (drv <= 9 && p[1] == ':')
  550. p += 2; /* Found a drive number, get and strip it */
  551. else
  552. drv = 0; /* No drive number is given, use drive number 0 as default */
  553. if (*p == '/') p++; /* Strip heading slash */
  554. *path = p; /* Return pointer to the path name */
  555. /* Check if the drive number is valid or not */
  556. if (drv >= _DRIVES) return FR_INVALID_DRIVE; /* Is the drive number valid? */
  557. *rfs = fs = FatFs[drv]; /* Returen pointer to the corresponding file system object */
  558. if (!fs) return FR_NOT_ENABLED; /* Is the file system object registered? */
  559. if (fs->fs_type) { /* If the logical drive has been mounted */
  560. stat = disk_status(fs->drive);
  561. if (!(stat & STA_NOINIT)) { /* and physical drive is kept initialized (has not been changed), */
  562. #if !_FS_READONLY
  563. if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
  564. return FR_WRITE_PROTECTED;
  565. #endif
  566. return FR_OK; /* The file system object is valid */
  567. }
  568. }
  569. /* The logical drive must be re-mounted. Following code attempts to mount the logical drive */
  570. memset(fs, 0, sizeof(FATFS)); /* Clean-up the file system object */
  571. fs->drive = LD2PD(drv); /* Bind the logical drive and a physical drive */
  572. stat = disk_initialize(fs->drive); /* Initialize low level disk I/O layer */
  573. if (stat & STA_NOINIT) /* Check if the drive is ready */
  574. return FR_NOT_READY;
  575. #if S_MAX_SIZ > 512 /* Get disk sector size if needed */
  576. if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > S_MAX_SIZ)
  577. return FR_NO_FILESYSTEM;
  578. #endif
  579. #if !_FS_READONLY
  580. if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
  581. return FR_WRITE_PROTECTED;
  582. #endif
  583. /* Search FAT partition on the drive */
  584. fmt = check_fs(fs, bootsect = 0); /* Check sector 0 as an SFD format */
  585. #if 0
  586. if (fmt == 1) { /* Not an FAT boot record, it may be patitioned */
  587. /* Check a partition listed in top of the partition table */
  588. tbl = &fs->win[MBR_Table + LD2PT(drv) * 16]; /* Partition table */
  589. if (tbl[4]) { /* Is the partition existing? */
  590. bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
  591. fmt = check_fs(fs, bootsect); /* Check the partition */
  592. }
  593. }
  594. #else
  595. // if (fmt == 1)
  596. {
  597. int i;
  598. /* Check a first fat partition listed in partition table */
  599. for(i = 0; i < 4; i++) {
  600. tbl = &fs->win[MBR_Table + i * 16]; /* Partition table */
  601. if (tbl[4] == 0x01 || tbl[4] == 0x04 || tbl[4] == 0x06 || tbl[4] == 0x0B ||
  602. tbl[4] == 0x0C || tbl[4] == 0x0E) { /* Is the FAT partition? */
  603. bootsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
  604. fmt = check_fs(fs, bootsect); /* Check the partition */
  605. break;
  606. }
  607. }
  608. }
  609. #endif
  610. if (fmt || LD_WORD(&fs->win[BPB_BytsPerSec]) != SS(fs)) /* No valid FAT patition is found */
  611. return FR_NO_FILESYSTEM;
  612. /* Initialize the file system object */
  613. fatsize = LD_WORD(&fs->win[BPB_FATSz16]); /* Number of sectors per FAT */
  614. if (!fatsize) fatsize = LD_DWORD(&fs->win[BPB_FATSz32]);
  615. fs->sects_fat = fatsize;
  616. fs->n_fats = fs->win[BPB_NumFATs]; /* Number of FAT copies */
  617. fatsize *= fs->n_fats; /* (Number of sectors in FAT area) */
  618. fs->fatbase = bootsect + LD_WORD(&fs->win[BPB_RsvdSecCnt]); /* FAT start sector (lba) */
  619. fs->csize = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
  620. fs->n_rootdir = LD_WORD(&fs->win[BPB_RootEntCnt]); /* Nmuber of root directory entries */
  621. totalsect = LD_WORD(&fs->win[BPB_TotSec16]); /* Number of sectors on the file system */
  622. if (!totalsect) totalsect = LD_DWORD(&fs->win[BPB_TotSec32]);
  623. fs->max_clust = maxclust = (totalsect /* max_clust = Last cluster# + 1 */
  624. - LD_WORD(&fs->win[BPB_RsvdSecCnt]) - fatsize - fs->n_rootdir / (SS(fs)/32)
  625. ) / fs->csize + 2;
  626. fmt = FS_FAT12; /* Determine the FAT sub type */
  627. if (maxclust >= 0xFF7) fmt = FS_FAT16;
  628. if (maxclust >= 0xFFF7) fmt = FS_FAT32;
  629. if (fmt == FS_FAT32)
  630. fs->dirbase = LD_DWORD(&fs->win[BPB_RootClus]); /* Root directory start cluster */
  631. else
  632. fs->dirbase = fs->fatbase + fatsize; /* Root directory start sector (lba) */
  633. fs->database = fs->fatbase + fatsize + fs->n_rootdir / (SS(fs)/32); /* Data start sector (lba) */
  634. #if !_FS_READONLY
  635. /* Initialize allocation information */
  636. fs->free_clust = 0xFFFFFFFF;
  637. #if _USE_FSINFO
  638. /* Get fsinfo if needed */
  639. if (fmt == FS_FAT32) {
  640. fs->fsi_sector = bootsect + LD_WORD(&fs->win[BPB_FSInfo]);
  641. if (disk_read(fs->drive, fs->win, fs->fsi_sector, 1) == RES_OK &&
  642. LD_WORD(&fs->win[BS_55AA]) == 0xAA55 &&
  643. LD_DWORD(&fs->win[FSI_LeadSig]) == 0x41615252 &&
  644. LD_DWORD(&fs->win[FSI_StrucSig]) == 0x61417272) {
  645. fs->last_clust = LD_DWORD(&fs->win[FSI_Nxt_Free]);
  646. fs->free_clust = LD_DWORD(&fs->win[FSI_Free_Count]);
  647. }
  648. }
  649. #endif
  650. #endif
  651. fs->fs_type = fmt; /* FAT syb-type */
  652. fs->id = ++fsid; /* File system mount ID */
  653. return FR_OK;
  654. }
  655. /*-----------------------------------------------------------------------*/
  656. /* Check if the file/dir object is valid or not */
  657. /*-----------------------------------------------------------------------*/
  658. static
  659. FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
  660. const FATFS *fs, /* Pointer to the file system object */
  661. WORD id /* Member id of the target object to be checked */
  662. )
  663. {
  664. if (!fs || !fs->fs_type || fs->id != id)
  665. return FR_INVALID_OBJECT;
  666. if (disk_status(fs->drive) & STA_NOINIT)
  667. return FR_NOT_READY;
  668. return FR_OK;
  669. }
  670. /*--------------------------------------------------------------------------
  671. Public Functions
  672. --------------------------------------------------------------------------*/
  673. /*-----------------------------------------------------------------------*/
  674. /* Mount/Unmount a Locical Drive */
  675. /*-----------------------------------------------------------------------*/
  676. FRESULT f_mount (
  677. BYTE drv, /* Logical drive number to be mounted/unmounted */
  678. FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
  679. )
  680. {
  681. if (drv >= _DRIVES) return FR_INVALID_DRIVE;
  682. if (FatFs[drv]) FatFs[drv]->fs_type = 0; /* Clear old object */
  683. FatFs[drv] = fs; /* Register and clear new object */
  684. if (fs) fs->fs_type = 0;
  685. return FR_OK;
  686. }
  687. /*-----------------------------------------------------------------------*/
  688. /* Open or Create a File */
  689. /*-----------------------------------------------------------------------*/
  690. FRESULT f_open (
  691. FIL *fp, /* Pointer to the blank file object */
  692. const char *path, /* Pointer to the file name */
  693. BYTE mode /* Access mode and file open mode flags */
  694. )
  695. {
  696. FRESULT res;
  697. DIR dj;
  698. BYTE *dir;
  699. char fn[8+3+1];
  700. fp->fs = NULL; /* Clear file object */
  701. #if !_FS_READONLY
  702. mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW);
  703. res = auto_mount(&path, &dj.fs, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)));
  704. #else
  705. mode &= FA_READ;
  706. res = auto_mount(&path, &dj.fs, 0);
  707. #endif
  708. if (res != FR_OK) return res;
  709. res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
  710. #if !_FS_READONLY
  711. /* Create or Open a file */
  712. if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {
  713. DWORD ps, rs;
  714. if (res != FR_OK) { /* No file, create new */
  715. if (res != FR_NO_FILE) return res;
  716. res = reserve_direntry(&dj, &dir);
  717. if (res != FR_OK) return res;
  718. memset(dir, 0, 32); /* Initialize the new entry with open name */
  719. memcpy(&dir[DIR_Name], fn, 8+3);
  720. dir[DIR_NTres] = fn[11];
  721. mode |= FA_CREATE_ALWAYS;
  722. }
  723. else { /* Any object is already existing */
  724. if (mode & FA_CREATE_NEW) /* Cannot create new */
  725. return FR_EXIST;
  726. if (!dir || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite it (R/O or DIR) */
  727. return FR_DENIED;
  728. if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero if needed */
  729. rs = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]); /* Get start cluster */
  730. ST_WORD(&dir[DIR_FstClusHI], 0); /* cluster = 0 */
  731. ST_WORD(&dir[DIR_FstClusLO], 0);
  732. ST_DWORD(&dir[DIR_FileSize], 0); /* size = 0 */
  733. dj.fs->winflag = 1;
  734. ps = dj.fs->winsect; /* Remove the cluster chain */
  735. if (!remove_chain(dj.fs, rs) || !move_window(dj.fs, ps))
  736. return FR_RW_ERROR;
  737. dj.fs->last_clust = rs - 1; /* Reuse the cluster hole */
  738. }
  739. }
  740. if (mode & FA_CREATE_ALWAYS) {
  741. dir[DIR_Attr] = 0; /* Reset attribute */
  742. ps = get_fattime();
  743. ST_DWORD(&dir[DIR_CrtTime], ps); /* Created time */
  744. dj.fs->winflag = 1;
  745. mode |= FA__WRITTEN; /* Set file changed flag */
  746. }
  747. }
  748. /* Open an existing file */
  749. else {
  750. #endif /* !_FS_READONLY */
  751. if (res != FR_OK) return res; /* Trace failed */
  752. if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
  753. return FR_NO_FILE;
  754. #if !_FS_READONLY
  755. if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
  756. return FR_DENIED;
  757. }
  758. fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
  759. fp->dir_ptr = dir;
  760. #endif
  761. fp->flag = mode; /* File access mode */
  762. fp->org_clust = /* File start cluster */
  763. ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
  764. fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */
  765. fp->fptr = 0; fp->csect = 255; /* File pointer */
  766. fp->curr_sect = 0;
  767. fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
  768. return FR_OK;
  769. }
  770. /*-----------------------------------------------------------------------*/
  771. /* Read File */
  772. /*-----------------------------------------------------------------------*/
  773. FRESULT f_read (
  774. FIL *fp, /* Pointer to the file object */
  775. void *buff, /* Pointer to data buffer */
  776. UINT btr, /* Number of bytes to read */
  777. UINT *br /* Pointer to number of bytes read */
  778. )
  779. {
  780. FRESULT res;
  781. DWORD clust, sect, remain;
  782. UINT rcnt, cc;
  783. BYTE *rbuff = buff;
  784. *br = 0;
  785. res = validate(fp->fs, fp->id); /* Check validity of the object */
  786. if (res != FR_OK) return res;
  787. if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
  788. if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
  789. remain = fp->fsize - fp->fptr;
  790. if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
  791. for ( ; btr; /* Repeat until all data transferred */
  792. rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
  793. if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
  794. if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
  795. clust = (fp->fptr == 0) ? /* On the top of the file? */
  796. fp->org_clust : get_cluster(fp->fs, fp->curr_clust);
  797. if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error;
  798. fp->curr_clust = clust; /* Update current cluster */
  799. fp->csect = 0; /* Reset sector address in the cluster */
  800. }
  801. sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */
  802. cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
  803. if (cc) { /* Read maximum contiguous sectors directly */
  804. if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
  805. cc = fp->fs->csize - fp->csect;
  806. if (disk_read(fp->fs->drive, rbuff, sect, (BYTE)cc) != RES_OK)
  807. goto fr_error;
  808. fp->csect += (BYTE)cc; /* Next sector address in the cluster */
  809. rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
  810. continue;
  811. }
  812. if (sect != fp->curr_sect) { /* Is window offset changed? */
  813. #if !_FS_READONLY
  814. if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */
  815. if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
  816. goto fr_error;
  817. fp->flag &= (BYTE)~FA__DIRTY;
  818. }
  819. #endif
  820. if (disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK) /* Fill file I/O buffer with file data */
  821. goto fr_error;
  822. fp->curr_sect = sect;
  823. }
  824. fp->csect++; /* Next sector address in the cluster */
  825. }
  826. rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector from file I/O buffer */
  827. if (rcnt > btr) rcnt = btr;
  828. memcpy(rbuff, &fp->buffer[fp->fptr % SS(fp->fs)], rcnt);
  829. }
  830. return FR_OK;
  831. fr_error: /* Abort this file due to an unrecoverable error */
  832. fp->flag |= FA__ERROR;
  833. return FR_RW_ERROR;
  834. }
  835. #if !_FS_READONLY
  836. /*-----------------------------------------------------------------------*/
  837. /* Write File */
  838. /*-----------------------------------------------------------------------*/
  839. FRESULT f_write (
  840. FIL *fp, /* Pointer to the file object */
  841. const void *buff, /* Pointer to the data to be written */
  842. UINT btw, /* Number of bytes to write */
  843. UINT *bw /* Pointer to number of bytes written */
  844. )
  845. {
  846. FRESULT res;
  847. DWORD clust, sect;
  848. UINT wcnt, cc;
  849. const BYTE *wbuff = buff;
  850. *bw = 0;
  851. res = validate(fp->fs, fp->id); /* Check validity of the object */
  852. if (res != FR_OK) return res;
  853. if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
  854. if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
  855. if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */
  856. for ( ; btw; /* Repeat until all data transferred */
  857. wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
  858. if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
  859. if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
  860. if (fp->fptr == 0) { /* On the top of the file? */
  861. clust = fp->org_clust; /* Follow from the origin */
  862. if (clust == 0) /* When there is no cluster chain, */
  863. fp->org_clust = clust = create_chain(fp->fs, 0); /* Create a new cluster chain */
  864. } else { /* Middle or end of the file */
  865. clust = create_chain(fp->fs, fp->curr_clust); /* Trace or streach cluster chain */
  866. }
  867. if (clust == 0) break; /* Could not allocate a new cluster (disk full) */
  868. if (clust == 1 || clust >= fp->fs->max_clust) goto fw_error;
  869. fp->curr_clust = clust; /* Update current cluster */
  870. fp->csect = 0; /* Reset sector address in the cluster */
  871. }
  872. sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */
  873. cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
  874. if (cc) { /* Write maximum contiguous sectors directly */
  875. if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
  876. cc = fp->fs->csize - fp->csect;
  877. if (disk_write(fp->fs->drive, wbuff, sect, (BYTE)cc) != RES_OK)
  878. goto fw_error;
  879. fp->csect += (BYTE)cc; /* Next sector address in the cluster */
  880. wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
  881. continue;
  882. }
  883. if (sect != fp->curr_sect) { /* Is window offset changed? */
  884. if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */
  885. if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
  886. goto fw_error;
  887. fp->flag &= (BYTE)~FA__DIRTY;
  888. }
  889. if (fp->fptr < fp->fsize && /* Fill file I/O buffer with file data */
  890. disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK)
  891. goto fw_error;
  892. fp->curr_sect = sect;
  893. }
  894. fp->csect++; /* Next sector address in the cluster */
  895. }
  896. wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Put partial sector into file I/O buffer */
  897. if (wcnt > btw) wcnt = btw;
  898. memcpy(&fp->buffer[fp->fptr % SS(fp->fs)], wbuff, wcnt);
  899. fp->flag |= FA__DIRTY;
  900. }
  901. if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
  902. fp->flag |= FA__WRITTEN; /* Set file changed flag */
  903. return FR_OK;
  904. fw_error: /* Abort this file due to an unrecoverable error */
  905. fp->flag |= FA__ERROR;
  906. return FR_RW_ERROR;
  907. }
  908. /*-----------------------------------------------------------------------*/
  909. /* Synchronize the file object */
  910. /*-----------------------------------------------------------------------*/
  911. FRESULT f_sync (
  912. FIL *fp /* Pointer to the file object */
  913. )
  914. {
  915. FRESULT res;
  916. DWORD tim;
  917. BYTE *dir;
  918. res = validate(fp->fs, fp->id); /* Check validity of the object */
  919. if (res == FR_OK) {
  920. if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
  921. /* Write back data buffer if needed */
  922. if (fp->flag & FA__DIRTY) {
  923. if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
  924. return FR_RW_ERROR;
  925. fp->flag &= (BYTE)~FA__DIRTY;
  926. }
  927. /* Update the directory entry */
  928. if (!move_window(fp->fs, fp->dir_sect))
  929. return FR_RW_ERROR;
  930. dir = fp->dir_ptr;
  931. dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
  932. ST_DWORD(&dir[DIR_FileSize], fp->fsize); /* Update file size */
  933. ST_WORD(&dir[DIR_FstClusLO], fp->org_clust); /* Update start cluster */
  934. ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16);
  935. tim = get_fattime(); /* Updated time */
  936. ST_DWORD(&dir[DIR_WrtTime], tim);
  937. fp->flag &= (BYTE)~FA__WRITTEN;
  938. res = sync(fp->fs);
  939. }
  940. }
  941. return res;
  942. }
  943. #endif /* !_FS_READONLY */
  944. /*-----------------------------------------------------------------------*/
  945. /* Close File */
  946. /*-----------------------------------------------------------------------*/
  947. FRESULT f_close (
  948. FIL *fp /* Pointer to the file object to be closed */
  949. )
  950. {
  951. FRESULT res;
  952. #if !_FS_READONLY
  953. res = f_sync(fp);
  954. #else
  955. res = validate(fp->fs, fp->id);
  956. #endif
  957. if (res == FR_OK) fp->fs = NULL;
  958. return res;
  959. }
  960. #if _FS_MINIMIZE <= 2
  961. /*-----------------------------------------------------------------------*/
  962. /* Seek File R/W Pointer */
  963. /*-----------------------------------------------------------------------*/
  964. FRESULT f_lseek (
  965. FIL *fp, /* Pointer to the file object */
  966. DWORD ofs /* File pointer from top of file */
  967. )
  968. {
  969. FRESULT res;
  970. DWORD clust, csize, nsect, ifptr;
  971. res = validate(fp->fs, fp->id); /* Check validity of the object */
  972. if (res != FR_OK) return res;
  973. if (fp->flag & FA__ERROR) return FR_RW_ERROR;
  974. if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
  975. #if !_FS_READONLY
  976. && !(fp->flag & FA_WRITE)
  977. #endif
  978. ) ofs = fp->fsize;
  979. ifptr = fp->fptr;
  980. fp->fptr = 0; fp->csect = 255;
  981. nsect = 0;
  982. if (ofs > 0) {
  983. csize = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
  984. if (ifptr > 0 &&
  985. (ofs - 1) / csize >= (ifptr - 1) / csize) {/* When seek to same or following cluster, */
  986. fp->fptr = (ifptr - 1) & ~(csize - 1); /* start from the current cluster */
  987. ofs -= fp->fptr;
  988. clust = fp->curr_clust;
  989. } else { /* When seek to back cluster, */
  990. clust = fp->org_clust; /* start from the first cluster */
  991. #if !_FS_READONLY
  992. if (clust == 0) { /* If no cluster chain, create a new chain */
  993. clust = create_chain(fp->fs, 0);
  994. if (clust == 1) goto fk_error;
  995. fp->org_clust = clust;
  996. }
  997. #endif
  998. fp->curr_clust = clust;
  999. }
  1000. if (clust != 0) {
  1001. while (ofs > csize) { /* Cluster following loop */
  1002. #if !_FS_READONLY
  1003. if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
  1004. clust = create_chain(fp->fs, clust); /* Force streached if in write mode */
  1005. if (clust == 0) { /* When disk gets full, clip file size */
  1006. ofs = csize; break;
  1007. }
  1008. } else
  1009. #endif
  1010. clust = get_cluster(fp->fs, clust); /* Follow cluster chain if not in write mode */
  1011. if (clust < 2 || clust >= fp->fs->max_clust) goto fk_error;
  1012. fp->curr_clust = clust;
  1013. fp->fptr += csize;
  1014. ofs -= csize;
  1015. }
  1016. fp->fptr += ofs;
  1017. fp->csect = (BYTE)(ofs / SS(fp->fs)); /* Sector offset in the cluster */
  1018. if (ofs & (SS(fp->fs) - 1)) {
  1019. nsect = clust2sect(fp->fs, clust) + fp->csect; /* Current sector */
  1020. fp->csect++;
  1021. }
  1022. }
  1023. }
  1024. if (nsect && nsect != fp->curr_sect) {
  1025. #if !_FS_READONLY
  1026. if (fp->flag & FA__DIRTY) { /* Write-back dirty buffer if needed */
  1027. if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
  1028. goto fk_error;
  1029. fp->flag &= (BYTE)~FA__DIRTY;
  1030. }
  1031. #endif
  1032. if (disk_read(fp->fs->drive, fp->buffer, nsect, 1) != RES_OK)
  1033. goto fk_error;
  1034. fp->curr_sect = nsect;
  1035. }
  1036. #if !_FS_READONLY
  1037. if (fp->fptr > fp->fsize) { /* Set changed flag if the file was extended */
  1038. fp->fsize = fp->fptr;
  1039. fp->flag |= FA__WRITTEN;
  1040. }
  1041. #endif
  1042. return FR_OK;
  1043. fk_error: /* Abort this file due to an unrecoverable error */
  1044. fp->flag |= FA__ERROR;
  1045. return FR_RW_ERROR;
  1046. }
  1047. #if _FS_MINIMIZE <= 1
  1048. /*-----------------------------------------------------------------------*/
  1049. /* Create a directroy object */
  1050. /*-----------------------------------------------------------------------*/
  1051. FRESULT f_opendir (
  1052. DIR *dj, /* Pointer to directory object to create */
  1053. const char *path /* Pointer to the directory path */
  1054. )
  1055. {
  1056. FRESULT res;
  1057. BYTE *dir;
  1058. char fn[8+3+1];
  1059. res = auto_mount(&path, &dj->fs, 0);
  1060. if (res == FR_OK) {
  1061. res = trace_path(dj, fn, path, &dir); /* Trace the directory path */
  1062. if (res == FR_OK) { /* Trace completed */
  1063. if (dir) { /* It is not the root dir */
  1064. if (dir[DIR_Attr] & AM_DIR) { /* The entry is a directory */
  1065. dj->clust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
  1066. dj->sect = clust2sect(dj->fs, dj->clust);
  1067. dj->index = 2;
  1068. } else { /* The entry is not a directory */
  1069. res = FR_NO_FILE;
  1070. }
  1071. }
  1072. dj->id = dj->fs->id;
  1073. }
  1074. }
  1075. return res;
  1076. }
  1077. /*-----------------------------------------------------------------------*/
  1078. /* Read Directory Entry in Sequense */
  1079. /*-----------------------------------------------------------------------*/
  1080. FRESULT f_readdir (
  1081. DIR *dj, /* Pointer to the directory object */
  1082. FILINFO *finfo /* Pointer to file information to return */
  1083. )
  1084. {
  1085. BYTE *dir, c, res;
  1086. res = validate(dj->fs, dj->id); /* Check validity of the object */
  1087. if (res != FR_OK) return (FRESULT)res;
  1088. finfo->fname[0] = 0;
  1089. while (dj->sect) {
  1090. if (!move_window(dj->fs, dj->sect))
  1091. return FR_RW_ERROR;
  1092. dir = &dj->fs->win[(dj->index & ((SS(dj->fs) - 1) >> 5)) * 32]; /* pointer to the directory entry */
  1093. c = dir[DIR_Name];
  1094. if (c == 0) break; /* Has it reached to end of dir? */
  1095. if (c != 0xE5 && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
  1096. get_fileinfo(finfo, dir);
  1097. if (!next_dir_entry(dj)) dj->sect = 0; /* Next entry */
  1098. if (finfo->fname[0]) break; /* Found valid entry */
  1099. }
  1100. return FR_OK;
  1101. }
  1102. #if _FS_MINIMIZE == 0
  1103. /*-----------------------------------------------------------------------*/
  1104. /* Get File Status */
  1105. /*-----------------------------------------------------------------------*/
  1106. FRESULT f_stat (
  1107. const char *path, /* Pointer to the file path */
  1108. FILINFO *finfo /* Pointer to file information to return */
  1109. )
  1110. {
  1111. FRESULT res;
  1112. DIR dj;
  1113. BYTE *dir;
  1114. char fn[8+3+1];
  1115. res = auto_mount(&path, &dj.fs, 0);
  1116. if (res == FR_OK) {
  1117. res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
  1118. if (res == FR_OK) { /* Trace completed */
  1119. if (dir) /* Found an object */
  1120. get_fileinfo(finfo, dir);
  1121. else /* It is root dir */
  1122. res = FR_INVALID_NAME;
  1123. }
  1124. }
  1125. return res;
  1126. }
  1127. #if !_FS_READONLY
  1128. /*-----------------------------------------------------------------------*/
  1129. /* Truncate File */
  1130. /*-----------------------------------------------------------------------*/
  1131. FRESULT f_truncate (
  1132. FIL *fp /* Pointer to the file object */
  1133. )
  1134. {
  1135. FRESULT res;
  1136. DWORD ncl;
  1137. res = validate(fp->fs, fp->id); /* Check validity of the object */
  1138. if (res != FR_OK) return res;
  1139. if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
  1140. if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
  1141. if (fp->fsize > fp->fptr) {
  1142. fp->fsize = fp->fptr; /* Set file size to current R/W point */
  1143. fp->flag |= FA__WRITTEN;
  1144. if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
  1145. if (!remove_chain(fp->fs, fp->org_clust)) goto ft_error;
  1146. fp->org_clust = 0;
  1147. } else { /* When truncate a part of the file, remove remaining clusters */
  1148. ncl = get_cluster(fp->fs, fp->curr_clust);
  1149. if (ncl < 2) goto ft_error;
  1150. if (ncl < fp->fs->max_clust) {
  1151. if (!put_cluster(fp->fs, fp->curr_clust, 0x0FFFFFFF)) goto ft_error;
  1152. if (!remove_chain(fp->fs, ncl)) goto ft_error;
  1153. }
  1154. }
  1155. }
  1156. return FR_OK;
  1157. ft_error: /* Abort this file due to an unrecoverable error */
  1158. fp->flag |= FA__ERROR;
  1159. return FR_RW_ERROR;
  1160. }
  1161. /*-----------------------------------------------------------------------*/
  1162. /* Get Number of Free Clusters */
  1163. /*-----------------------------------------------------------------------*/
  1164. FRESULT f_getfree (
  1165. const char *drv, /* Pointer to the logical drive number (root dir) */
  1166. DWORD *nclust, /* Pointer to the variable to return number of free clusters */
  1167. FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
  1168. )
  1169. {
  1170. FRESULT res;
  1171. DWORD n, clust, sect;
  1172. BYTE fat, f, *p;
  1173. /* Get drive number */
  1174. res = auto_mount(&drv, fatfs, 0);
  1175. if (res != FR_OK) return res;
  1176. /* If number of free cluster is valid, return it without cluster scan. */
  1177. if ((*fatfs)->free_clust <= (*fatfs)->max_clust - 2) {
  1178. *nclust = (*fatfs)->free_clust;
  1179. return FR_OK;
  1180. }
  1181. /* Get number of free clusters */
  1182. fat = (*fatfs)->fs_type;
  1183. n = 0;
  1184. if (fat == FS_FAT12) {
  1185. clust = 2;
  1186. do {
  1187. if ((WORD)get_cluster(*fatfs, clust) == 0) n++;
  1188. } while (++clust < (*fatfs)->max_clust);
  1189. } else {
  1190. clust = (*fatfs)->max_clust;
  1191. sect = (*fatfs)->fatbase;
  1192. f = 0; p = 0;
  1193. do {
  1194. if (!f) {
  1195. if (!move_window(*fatfs, sect++)) return FR_RW_ERROR;
  1196. p = (*fatfs)->win;
  1197. }
  1198. if (fat == FS_FAT16) {
  1199. if (LD_WORD(p) == 0) n++;
  1200. p += 2; f += 1;
  1201. } else {
  1202. if (LD_DWORD(p) == 0) n++;
  1203. p += 4; f += 2;
  1204. }
  1205. } while (--clust);
  1206. }
  1207. (*fatfs)->free_clust = n;
  1208. #if _USE_FSINFO
  1209. if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;
  1210. #endif
  1211. *nclust = n;
  1212. return FR_OK;
  1213. }
  1214. /*-----------------------------------------------------------------------*/
  1215. /* Delete a File or Directory */
  1216. /*-----------------------------------------------------------------------*/
  1217. FRESULT f_unlink (
  1218. const char *path /* Pointer to the file or directory path */
  1219. )
  1220. {
  1221. FRESULT res;
  1222. DIR dj;
  1223. BYTE *dir, *sdir;
  1224. DWORD dclust, dsect;
  1225. char fn[8+3+1];
  1226. res = auto_mount(&path, &dj.fs, 1);
  1227. if (res != FR_OK) return res;
  1228. res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
  1229. if (res != FR_OK) return res; /* Trace failed */
  1230. if (!dir) return FR_INVALID_NAME; /* It is the root directory */
  1231. if (dir[DIR_Attr] & AM_RDO) return FR_DENIED; /* It is a R/O object */
  1232. dsect = dj.fs->winsect;
  1233. dclust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
  1234. if (dir[DIR_Attr] & AM_DIR) { /* It is a sub-directory */
  1235. dj.clust = dclust; /* Check if the sub-dir is empty or not */
  1236. dj.sect = clust2sect(dj.fs, dclust);
  1237. dj.index = 2;
  1238. do {
  1239. if (!move_window(dj.fs, dj.sect)) return FR_RW_ERROR;
  1240. sdir = &dj.fs->win[(dj.index & ((SS(dj.fs) - 1) >> 5)) * 32];
  1241. if (sdir[DIR_Name] == 0) break;
  1242. if (sdir[DIR_Name] != 0xE5 && !(sdir[DIR_Attr] & AM_VOL))
  1243. return FR_DENIED; /* The directory is not empty */
  1244. } while (next_dir_entry(&dj));
  1245. }
  1246. if (!move_window(dj.fs, dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */
  1247. dir[DIR_Name] = 0xE5;
  1248. dj.fs->winflag = 1;
  1249. if (!remove_chain(dj.fs, dclust)) return FR_RW_ERROR; /* Remove the cluster chain */
  1250. return sync(dj.fs);
  1251. }
  1252. /*-----------------------------------------------------------------------*/
  1253. /* Create a Directory */
  1254. /*-----------------------------------------------------------------------*/
  1255. FRESULT f_mkdir (
  1256. const char *path /* Pointer to the directory path */
  1257. )
  1258. {
  1259. FRESULT res;
  1260. DIR dj;
  1261. BYTE *dir, *fw, n;
  1262. char fn[8+3+1];
  1263. DWORD sect, dsect, dclust, pclust, tim;
  1264. res = auto_mount(&path, &dj.fs, 1);
  1265. if (res != FR_OK) return res;
  1266. res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
  1267. if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */
  1268. if (res != FR_NO_FILE) return res;
  1269. res = reserve_direntry(&dj, &dir); /* Reserve a directory entry */
  1270. if (res != FR_OK) return res;
  1271. sect = dj.fs->winsect;
  1272. dclust = create_chain(dj.fs, 0); /* Allocate a cluster for new directory table */
  1273. if (dclust == 1) return FR_RW_ERROR;
  1274. dsect = clust2sect(dj.fs, dclust);
  1275. if (!dsect) return FR_DENIED;
  1276. if (!move_window(dj.fs, dsect)) return FR_RW_ERROR;
  1277. fw = dj.fs->win;
  1278. memset(fw, 0, SS(dj.fs)); /* Clear the new directory table */
  1279. for (n = 1; n < dj.fs->csize; n++) {
  1280. if (disk_write(dj.fs->drive, fw, ++dsect, 1) != RES_OK)
  1281. return FR_RW_ERROR;
  1282. }
  1283. memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */
  1284. fw[DIR_Name] = '.';
  1285. fw[DIR_Attr] = AM_DIR;
  1286. tim = get_fattime();
  1287. ST_DWORD(&fw[DIR_WrtTime], tim);
  1288. memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */
  1289. ST_WORD(&fw[ DIR_FstClusLO], dclust);
  1290. ST_WORD(&fw[ DIR_FstClusHI], dclust >> 16);
  1291. pclust = dj.sclust;
  1292. if (dj.fs->fs_type == FS_FAT32 && pclust == dj.fs->dirbase) pclust = 0;
  1293. ST_WORD(&fw[32+DIR_FstClusLO], pclust);
  1294. ST_WORD(&fw[32+DIR_FstClusHI], pclust >> 16);
  1295. dj.fs->winflag = 1;
  1296. if (!move_window(dj.fs, sect)) return FR_RW_ERROR;
  1297. memset(&dir[0], 0, 32); /* Initialize the new entry */
  1298. memcpy(&dir[DIR_Name], fn, 8+3); /* Name */
  1299. dir[DIR_NTres] = fn[11];
  1300. dir[DIR_Attr] = AM_DIR; /* Attribute */
  1301. ST_DWORD(&dir[DIR_WrtTime], tim); /* Crated time */
  1302. ST_WORD(&dir[DIR_FstClusLO], dclust); /* Table start cluster */
  1303. ST_WORD(&dir[DIR_FstClusHI], dclust >> 16);
  1304. return sync(dj.fs);
  1305. }
  1306. /*-----------------------------------------------------------------------*/
  1307. /* Change File Attribute */
  1308. /*-----------------------------------------------------------------------*/
  1309. FRESULT f_chmod (
  1310. const char *path, /* Pointer to the file path */
  1311. BYTE value, /* Attribute bits */
  1312. BYTE mask /* Attribute mask to change */
  1313. )
  1314. {
  1315. FRESULT res;
  1316. DIR dj;
  1317. BYTE *dir;
  1318. char fn[8+3+1];
  1319. res = auto_mount(&path, &dj.fs, 1);
  1320. if (res == FR_OK) {
  1321. res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
  1322. if (res == FR_OK) { /* Trace completed */
  1323. if (!dir) {
  1324. res = FR_INVALID_NAME; /* Root directory */
  1325. } else {
  1326. mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
  1327. dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
  1328. res = sync(dj.fs);
  1329. }
  1330. }
  1331. }
  1332. return res;
  1333. }
  1334. /*-----------------------------------------------------------------------*/
  1335. /* Change Timestamp */
  1336. /*-----------------------------------------------------------------------*/
  1337. FRESULT f_utime (
  1338. const char *path, /* Pointer to the file/directory name */
  1339. const FILINFO *finfo /* Pointer to the timestamp to be set */
  1340. )
  1341. {
  1342. FRESULT res;
  1343. DIR dj;
  1344. BYTE *dir;
  1345. char fn[8+3+1];
  1346. res = auto_mount(&path, &dj.fs, 1);
  1347. if (res == FR_OK) {
  1348. res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
  1349. if (res == FR_OK) { /* Trace completed */
  1350. if (!dir) {
  1351. res = FR_INVALID_NAME; /* Root directory */
  1352. } else {
  1353. ST_WORD(&dir[DIR_WrtTime], finfo->ftime);
  1354. ST_WORD(&dir[DIR_WrtDate], finfo->fdate);
  1355. res = sync(dj.fs);
  1356. }
  1357. }
  1358. }
  1359. return res;
  1360. }
  1361. /*-----------------------------------------------------------------------*/
  1362. /* Rename File/Directory */
  1363. /*-----------------------------------------------------------------------*/
  1364. FRESULT f_rename (
  1365. const char *path_old, /* Pointer to the old name */
  1366. const char *path_new /* Pointer to the new name */
  1367. )
  1368. {
  1369. FRESULT res;
  1370. DIR dj;
  1371. DWORD sect_old;
  1372. BYTE *dir_old, *dir_new, direntry[32-11];
  1373. char fn[8+3+1];
  1374. res = auto_mount(&path_old, &dj.fs, 1);
  1375. if (res != FR_OK) return res;
  1376. res = trace_path(&dj, fn, path_old, &dir_old); /* Check old object */
  1377. if (res != FR_OK) return res; /* The old object is not found */
  1378. if (!dir_old) return FR_NO_FILE;
  1379. sect_old = dj.fs->winsect; /* Save the object information */
  1380. memcpy(direntry, &dir_old[DIR_Attr], 32-11);
  1381. res = trace_path(&dj, fn, path_new, &dir_new); /* Check new object */
  1382. if (res == FR_OK) return FR_EXIST; /* The new object name is already existing */
  1383. if (res != FR_NO_FILE) return res; /* Is there no old name? */
  1384. res = reserve_direntry(&dj, &dir_new); /* Reserve a directory entry */
  1385. if (res != FR_OK) return res;
  1386. memcpy(&dir_new[DIR_Attr], direntry, 32-11); /* Create new entry */
  1387. memcpy(&dir_new[DIR_Name], fn, 8+3);
  1388. dir_new[DIR_NTres] = fn[11];
  1389. dj.fs->winflag = 1;
  1390. if (!move_window(dj.fs, sect_old)) return FR_RW_ERROR; /* Delete old entry */
  1391. dir_old[DIR_Name] = 0xE5;
  1392. return sync(dj.fs);
  1393. }
  1394. #endif /* !_FS_READONLY */
  1395. #endif /* _FS_MINIMIZE == 0 */
  1396. #endif /* _FS_MINIMIZE <= 1 */
  1397. #endif /* _FS_MINIMIZE <= 2 */
  1398. #if _USE_MKFS && !_FS_READONLY
  1399. /*-----------------------------------------------------------------------*/
  1400. /* Create File System on the Drive */
  1401. /*-----------------------------------------------------------------------*/
  1402. #define N_ROOTDIR 512 /* Multiple of 32 and <= 2048 */
  1403. #define N_FATS 1 /* 1 or 2 */
  1404. #define MAX_SECTOR 64000000UL /* Maximum partition size */
  1405. #define MIN_SECTOR 2000UL /* Minimum partition size */
  1406. FRESULT f_mkfs (
  1407. BYTE drv, /* Logical drive number */
  1408. BYTE partition, /* Partitioning rule 0:FDISK, 1:SFD */
  1409. WORD allocsize /* Allocation unit size [bytes] */
  1410. )
  1411. {
  1412. BYTE fmt, m, *tbl;
  1413. DWORD b_part, b_fat, b_dir, b_data; /* Area offset (LBA) */
  1414. DWORD n_part, n_rsv, n_fat, n_dir; /* Area size */
  1415. DWORD n_clust, n;
  1416. FATFS *fs;
  1417. DSTATUS stat;
  1418. /* Check validity of the parameters */
  1419. if (drv >= _DRIVES) return FR_INVALID_DRIVE;
  1420. if (partition >= 2) return FR_MKFS_ABORTED;
  1421. for (n = 512; n <= 32768U && n != allocsize; n <<= 1);
  1422. if (n != allocsize) return FR_MKFS_ABORTED;
  1423. /* Check mounted drive and clear work area */
  1424. fs = FatFs[drv];
  1425. if (!fs) return FR_NOT_ENABLED;
  1426. fs->fs_type = 0;
  1427. drv = LD2PD(drv);
  1428. /* Get disk statics */
  1429. stat = disk_initialize(drv);
  1430. if (stat & STA_NOINIT) return FR_NOT_READY;
  1431. if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
  1432. if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_part) != RES_OK || n_part < MIN_SECTOR)
  1433. return FR_MKFS_ABORTED;
  1434. if (n_part > MAX_SECTOR) n_part = MAX_SECTOR;
  1435. b_part = (!partition) ? 63 : 0; /* Boot sector */
  1436. n_part -= b_part;
  1437. #if S_MAX_SIZ > 512 /* Check disk sector size */
  1438. if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
  1439. || SS(fs) > S_MAX_SIZ
  1440. || SS(fs) > allocsize)
  1441. return FR_MKFS_ABORTED;
  1442. #endif
  1443. allocsize /= SS(fs); /* Number of sectors per cluster */
  1444. /* Pre-compute number of clusters and FAT type */
  1445. n_clust = n_part / allocsize;
  1446. fmt = FS_FAT12;
  1447. if (n_clust >= 0xFF5) fmt = FS_FAT16;
  1448. if (n_clust >= 0xFFF5) fmt = FS_FAT32;
  1449. /* Determine offset and size of FAT structure */
  1450. switch (fmt) {
  1451. case FS_FAT12:
  1452. n_fat = ((n_clust * 3 + 1) / 2 + 3 + SS(fs) - 1) / SS(fs);
  1453. n_rsv = 1 + partition;
  1454. n_dir = N_ROOTDIR * 32 / SS(fs);
  1455. break;
  1456. case FS_FAT16:
  1457. n_fat = ((n_clust * 2) + 4 + SS(fs) - 1) / SS(fs);
  1458. n_rsv = 1 + partition;
  1459. n_dir = N_ROOTDIR * 32 / SS(fs);
  1460. break;
  1461. default:
  1462. n_fat = ((n_clust * 4) + 8 + SS(fs) - 1) / SS(fs);
  1463. n_rsv = 33 - partition;
  1464. n_dir = 0;
  1465. }
  1466. b_fat = b_part + n_rsv; /* FATs start sector */
  1467. b_dir = b_fat + n_fat * N_FATS; /* Directory start sector */
  1468. b_data = b_dir + n_dir; /* Data start sector */
  1469. /* Align data start sector to erase block boundary (for flash memory media) */
  1470. if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK) return FR_MKFS_ABORTED;
  1471. n = (b_data + n - 1) & ~(n - 1);
  1472. n_fat += (n - b_data) / N_FATS;
  1473. /* b_dir and b_data are no longer used below */
  1474. /* Determine number of cluster and final check of validity of the FAT type */
  1475. n_clust = (n_part - n_rsv - n_fat * N_FATS - n_dir) / allocsize;
  1476. if ( (fmt == FS_FAT16 && n_clust < 0xFF5)
  1477. || (fmt == FS_FAT32 && n_clust < 0xFFF5))
  1478. return FR_MKFS_ABORTED;
  1479. /* Create partition table if needed */
  1480. if (!partition) {
  1481. DWORD n_disk = b_part + n_part;
  1482. tbl = &fs->win[MBR_Table];
  1483. ST_DWORD(&tbl[0], 0x00010180); /* Partition start in CHS */
  1484. if (n_disk < 63UL * 255 * 1024) { /* Partition end in CHS */
  1485. n_disk = n_disk / 63 / 255;
  1486. tbl[7] = (BYTE)n_disk;
  1487. tbl[6] = (BYTE)((n_disk >> 2) | 63);
  1488. } else {
  1489. ST_WORD(&tbl[6], 0xFFFF);
  1490. }
  1491. tbl[5] = 254;
  1492. if (fmt != FS_FAT32) /* System ID */
  1493. tbl[4] = (n_part < 0x10000) ? 0x04 : 0x06;
  1494. else
  1495. tbl[4] = 0x0c;
  1496. ST_DWORD(&tbl[8], 63); /* Partition start in LBA */
  1497. ST_DWORD(&tbl[12], n_part); /* Partition size in LBA */
  1498. ST_WORD(&tbl[64], 0xAA55); /* Signature */
  1499. if (disk_write(drv, fs->win, 0, 1) != RES_OK)
  1500. return FR_RW_ERROR;
  1501. }
  1502. /* Create boot record */
  1503. tbl = fs->win; /* Clear buffer */
  1504. memset(tbl, 0, SS(fs));
  1505. ST_DWORD(&tbl[BS_jmpBoot], 0x90FEEB); /* Boot code (jmp $, nop) */
  1506. ST_WORD(&tbl[BPB_BytsPerSec], SS(fs)); /* Sector size */
  1507. tbl[BPB_SecPerClus] = (BYTE)allocsize; /* Sectors per cluster */
  1508. ST_WORD(&tbl[BPB_RsvdSecCnt], n_rsv); /* Reserved sectors */
  1509. tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
  1510. ST_WORD(&tbl[BPB_RootEntCnt], SS(fs) / 32 * n_dir); /* Number of rootdir entries */
  1511. if (n_part < 0x10000) { /* Number of total sectors */
  1512. ST_WORD(&tbl[BPB_TotSec16], n_part);
  1513. } else {
  1514. ST_DWORD(&tbl[BPB_TotSec32], n_part);
  1515. }
  1516. tbl[BPB_Media] = 0xF8; /* Media descripter */
  1517. ST_WORD(&tbl[BPB_SecPerTrk], 63); /* Number of sectors per track */
  1518. ST_WORD(&tbl[BPB_NumHeads], 255); /* Number of heads */
  1519. ST_DWORD(&tbl[BPB_HiddSec], b_part); /* Hidden sectors */
  1520. n = get_fattime(); /* Use current time as a VSN */
  1521. if (fmt != FS_FAT32) {
  1522. ST_DWORD(&tbl[BS_VolID], n); /* Volume serial number */
  1523. ST_WORD(&tbl[BPB_FATSz16], n_fat); /* Number of secters per FAT */
  1524. tbl[BS_DrvNum] = 0x80; /* Drive number */
  1525. tbl[BS_BootSig] = 0x29; /* Extended boot signature */
  1526. memcpy(&tbl[BS_VolLab], "NO NAME FAT ", 19); /* Volume lavel, FAT signature */
  1527. } else {
  1528. ST_DWORD(&tbl[BS_VolID32], n); /* Volume serial number */
  1529. ST_DWORD(&tbl[BPB_FATSz32], n_fat); /* Number of secters per FAT */
  1530. ST_DWORD(&tbl[BPB_RootClus], 2); /* Root directory cluster (2) */
  1531. ST_WORD(&tbl[BPB_FSInfo], 1); /* FSInfo record (bs+1) */
  1532. ST_WORD(&tbl[BPB_BkBootSec], 6); /* Backup boot record (bs+6) */
  1533. tbl[BS_DrvNum32] = 0x80; /* Drive number */
  1534. tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
  1535. memcpy(&tbl[BS_VolLab32], "NO NAME FAT32 ", 19); /* Volume lavel, FAT signature */
  1536. }
  1537. ST_WORD(&tbl[BS_55AA], 0xAA55); /* Signature */
  1538. if (disk_write(drv, tbl, b_part+0, 1) != RES_OK)
  1539. return FR_RW_ERROR;
  1540. if (fmt == FS_FAT32)
  1541. disk_write(drv, tbl, b_part+6, 1);
  1542. /* Initialize FAT area */
  1543. for (m = 0; m < N_FATS; m++) {
  1544. memset(tbl, 0, SS(fs)); /* 1st sector of the FAT */
  1545. if (fmt != FS_FAT32) {
  1546. n = (fmt == FS_FAT12) ? 0x00FFFFF8 : 0xFFFFFFF8;
  1547. ST_DWORD(&tbl[0], n); /* Reserve cluster #0-1 (FAT12/16) */
  1548. } else {
  1549. ST_DWORD(&tbl[0], 0xFFFFFFF8); /* Reserve cluster #0-1 (FAT32) */
  1550. ST_DWORD(&tbl[4], 0xFFFFFFFF);
  1551. ST_DWORD(&tbl[8], 0x0FFFFFFF); /* Reserve cluster #2 for root dir */
  1552. }
  1553. if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
  1554. return FR_RW_ERROR;
  1555. memset(tbl, 0, SS(fs)); /* Following FAT entries are filled by zero */
  1556. for (n = 1; n < n_fat; n++) {
  1557. if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
  1558. return FR_RW_ERROR;
  1559. }
  1560. }
  1561. /* Initialize Root directory */
  1562. m = (BYTE)((fmt == FS_FAT32) ? allocsize : n_dir);
  1563. do {
  1564. if (disk_write(drv, tbl, b_fat++, 1) != RES_OK)
  1565. return FR_RW_ERROR;
  1566. } while (--m);
  1567. /* Create FSInfo record if needed */
  1568. if (fmt == FS_FAT32) {
  1569. ST_WORD(&tbl[BS_55AA], 0xAA55);
  1570. ST_DWORD(&tbl[FSI_LeadSig], 0x41615252);
  1571. ST_DWORD(&tbl[FSI_StrucSig], 0x61417272);
  1572. ST_DWORD(&tbl[FSI_Free_Count], n_clust - 1);
  1573. ST_DWORD(&tbl[FSI_Nxt_Free], 0xFFFFFFFF);
  1574. disk_write(drv, tbl, b_part+1, 1);
  1575. disk_write(drv, tbl, b_part+7, 1);
  1576. }
  1577. return (disk_ioctl(drv, CTRL_SYNC, NULL) == RES_OK) ? FR_OK : FR_RW_ERROR;
  1578. }
  1579. #endif /* _USE_MKFS && !_FS_READONLY */
  1580. #if _USE_STRFUNC >= 1
  1581. /*-----------------------------------------------------------------------*/
  1582. /* Get a string from the file */
  1583. /*-----------------------------------------------------------------------*/
  1584. char* fgets (
  1585. char* buff, /* Pointer to the string buffer to read */
  1586. int len, /* Size of string buffer */
  1587. FIL* fil /* Pointer to the file object */
  1588. )
  1589. {
  1590. int i = 0;
  1591. char *p = buff;
  1592. UINT rc;
  1593. while (i < len - 1) { /* Read bytes until buffer gets filled */
  1594. f_read(fil, p, 1, &rc);
  1595. if (rc != 1) break; /* Break when no data to read */
  1596. #if _USE_STRFUNC >= 2
  1597. if (*p == '\r') continue; /* Strip '\r' */
  1598. #endif
  1599. i++;
  1600. if (*p++ == '\n') break; /* Break when reached end of line */
  1601. }
  1602. *p = 0;
  1603. return i ? buff : 0; /* When no data read (eof or error), return with error. */
  1604. }
  1605. #if !_FS_READONLY
  1606. #include <stdarg.h>
  1607. /*-----------------------------------------------------------------------*/
  1608. /* Put a character to the file */
  1609. /*-----------------------------------------------------------------------*/
  1610. int fputc (
  1611. int chr, /* A character to be output */
  1612. FIL* fil /* Ponter to the file object */
  1613. )
  1614. {
  1615. UINT bw;
  1616. char c;
  1617. #if _USE_STRFUNC >= 2
  1618. if (chr == '\n') fputc ('\r', fil); /* LF -> CRLF conversion */
  1619. #endif
  1620. if (!fil) { /* Special value may be used to switch the destination to any other device */
  1621. /* put_console(chr); */
  1622. return chr;
  1623. }
  1624. c = (char)chr;
  1625. f_write(fil, &c, 1, &bw); /* Write a byte to the file */
  1626. return bw ? chr : EOF; /* Return the resulut */
  1627. }
  1628. /*-----------------------------------------------------------------------*/
  1629. /* Put a string to the file */
  1630. /*-----------------------------------------------------------------------*/
  1631. int fputs (
  1632. const char* str, /* Pointer to the string to be output */
  1633. FIL* fil /* Pointer to the file object */
  1634. )
  1635. {
  1636. int n;
  1637. for (n = 0; *str; str++, n++) {
  1638. if (fputc(*str, fil) == EOF) return EOF;
  1639. }
  1640. return n;
  1641. }
  1642. /*-----------------------------------------------------------------------*/
  1643. /* Put a formatted string to the file */
  1644. /*-----------------------------------------------------------------------*/
  1645. int fprintf (
  1646. FIL* fil, /* Pointer to the file object */
  1647. const char* str, /* Pointer to the format string */
  1648. ... /* Optional arguments... */
  1649. )
  1650. {
  1651. va_list arp;
  1652. UCHAR c, f, r;
  1653. ULONG val;
  1654. char s[16];
  1655. int i, w, res, cc;
  1656. va_start(arp, str);
  1657. for (cc = res = 0; cc != EOF; res += cc) {
  1658. c = *str++;
  1659. if (c == 0) break; /* End of string */
  1660. if (c != '%') { /* Non escape cahracter */
  1661. cc = fputc(c, fil);
  1662. if (cc != EOF) cc = 1;
  1663. continue;
  1664. }
  1665. w = f = 0;
  1666. c = *str++;
  1667. if (c == '0') { /* Flag: '0' padding */
  1668. f = 1; c = *str++;
  1669. }
  1670. while (c >= '0' && c <= '9') { /* Precision */
  1671. w = w * 10 + (c - '0');
  1672. c = *str++;
  1673. }
  1674. if (c == 'l') { /* Prefix: Size is long int */
  1675. f |= 2; c = *str++;
  1676. }
  1677. if (c == 's') { /* Type is string */
  1678. cc = fputs(va_arg(arp, char*), fil);
  1679. continue;
  1680. }
  1681. if (c == 'c') { /* Type is character */
  1682. cc = fputc(va_arg(arp, char), fil);
  1683. if (cc != EOF) cc = 1;
  1684. continue;
  1685. }
  1686. r = 0;
  1687. if (c == 'd') r = 10; /* Type is signed decimal */
  1688. if (c == 'u') r = 10; /* Type is unsigned decimal */
  1689. if (c == 'X') r = 16; /* Type is unsigned hexdecimal */
  1690. if (r == 0) break; /* Unknown type */
  1691. if (f & 2) { /* Get the value */
  1692. val = (ULONG)va_arg(arp, long);
  1693. } else {
  1694. val = (c == 'd') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int);
  1695. }
  1696. /* Put numeral string */
  1697. if (c == 'd') {
  1698. if (val >= 0x80000000) {
  1699. val = 0 - val;
  1700. f |= 4;
  1701. }
  1702. }
  1703. i = sizeof(s) - 1; s[i] = 0;
  1704. do {
  1705. c = (UCHAR)(val % r + '0');
  1706. if (c > '9') c += 7;
  1707. s[--i] = c;
  1708. val /= r;
  1709. } while (i && val);
  1710. if (i && (f & 4)) s[--i] = '-';
  1711. w = sizeof(s) - 1 - w;
  1712. while (i && i > w) s[--i] = (f & 1) ? '0' : ' ';
  1713. cc = fputs(&s[i], fil);
  1714. }
  1715. va_end(arp);
  1716. return (cc == EOF) ? cc : res;
  1717. }
  1718. #endif /* !_FS_READONLY */
  1719. #endif /* _USE_STRFUNC >= 1*/