ff.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026
  1. /*----------------------------------------------------------------------------/
  2. / FatFs - FAT file system module R0.08b (C)ChaN, 2011
  3. /-----------------------------------------------------------------------------/
  4. / FatFs module is a generic FAT file system module for small embedded systems.
  5. / This is a free software that opened for education, research and commercial
  6. / developments under license policy of following terms.
  7. /
  8. / Copyright (C) 2011, ChaN, all right reserved.
  9. /
  10. / * The FatFs module is a free software and there is NO WARRANTY.
  11. / * No restriction on use. You can use, modify and redistribute it for
  12. / personal, non-profit or commercial products 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) partition.
  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 algorithm 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 physical 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 without write.
  50. /
  51. / Apr 01,'08 R0.06 Added fputc(), fputs(), fprintf() and fgets().
  52. / Improved performance of f_lseek() on moving to the same or following cluster.
  53. /
  54. / Apr 01,'09 R0.07 Merged Tiny-FatFs as a configuration option. (_FS_TINY)
  55. / Added long file name feature.
  56. / Added multiple code page feature.
  57. / Added re-entrancy for multitask operation.
  58. / Added auto cluster size selection to f_mkfs().
  59. / Added rewind option to f_readdir().
  60. / Changed result code of critical errors.
  61. / Renamed string functions to avoid name collision.
  62. / Apr 14,'09 R0.07a Separated out OS dependent code on reentrant cfg.
  63. / Added multiple sector size feature.
  64. / Jun 21,'09 R0.07c Fixed f_unlink() can return FR_OK on error.
  65. / Fixed wrong cache control in f_lseek().
  66. / Added relative path feature.
  67. / Added f_chdir() and f_chdrive().
  68. / Added proper case conversion to extended char.
  69. / Nov 03,'09 R0.07e Separated out configuration options from ff.h to ffconf.h.
  70. / Fixed f_unlink() fails to remove a sub-dir on _FS_RPATH.
  71. / Fixed name matching error on the 13 char boundary.
  72. / Added a configuration option, _LFN_UNICODE.
  73. / Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
  74. /
  75. / May 15,'10 R0.08 Added a memory configuration option. (_USE_LFN = 3)
  76. / Added file lock feature. (_FS_SHARE)
  77. / Added fast seek feature. (_USE_FASTSEEK)
  78. / Changed some types on the API, XCHAR->TCHAR.
  79. / Changed fname member in the FILINFO structure on Unicode cfg.
  80. / String functions support UTF-8 encoding files on Unicode cfg.
  81. / Aug 16,'10 R0.08a Added f_getcwd(). (_FS_RPATH = 2)
  82. / Added sector erase feature. (_USE_ERASE)
  83. / Moved file lock semaphore table from fs object to the bss.
  84. / Fixed a wrong directory entry is created on non-LFN cfg when the given name contains ';'.
  85. / Fixed f_mkfs() creates wrong FAT32 volume.
  86. / Jan 15,'11 R0.08b Fast seek feature is also applied to f_read() and f_write().
  87. / f_lseek() reports required table size on creating CLMP.
  88. / Extended format syntax of f_printf function.
  89. / Ignores duplicated directory separators in given path names.
  90. /---------------------------------------------------------------------------*/
  91. #include "ff.h" /* FatFs configurations and declarations */
  92. #include "diskio.h" /* Declarations of low level disk I/O functions */
  93. /*--------------------------------------------------------------------------
  94. Module Private Definitions
  95. ---------------------------------------------------------------------------*/
  96. #if _FATFS != 8237
  97. #error Wrong include file (ff.h).
  98. #endif
  99. /* Definitions on sector size */
  100. #if _MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096
  101. #error Wrong sector size.
  102. #endif
  103. #if _MAX_SS != 512
  104. #define SS(fs) ((fs)->ssize) /* Multiple sector size */
  105. #else
  106. #define SS(fs) 512U /* Fixed sector size */
  107. #endif
  108. /* Reentrancy related */
  109. #if _FS_REENTRANT
  110. #if _USE_LFN == 1
  111. #error Static LFN work area must not be used in re-entrant configuration.
  112. #endif
  113. #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
  114. #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
  115. #else
  116. #define ENTER_FF(fs)
  117. #define LEAVE_FF(fs, res) return res
  118. #endif
  119. #define ABORT(fs, res) { fp->flag |= FA__ERROR; LEAVE_FF(fs, res); }
  120. /* File shareing feature */
  121. #if _FS_SHARE
  122. #if _FS_READONLY
  123. #error _FS_SHARE must be 0 on read-only cfg.
  124. #endif
  125. typedef struct {
  126. FATFS *fs; /* File ID 1, volume (NULL:blank entry) */
  127. DWORD clu; /* File ID 2, directory */
  128. WORD idx; /* File ID 3, directory index */
  129. WORD ctr; /* File open counter, 0:none, 0x01..0xFF:read open count, 0x100:write mode */
  130. } FILESEM;
  131. #endif
  132. /* Misc definitions */
  133. #define LD_CLUST(dir) (((DWORD)LD_WORD(dir+DIR_FstClusHI)<<16) | LD_WORD(dir+DIR_FstClusLO))
  134. #define ST_CLUST(dir,cl) {ST_WORD(dir+DIR_FstClusLO, cl); ST_WORD(dir+DIR_FstClusHI, (DWORD)cl>>16);}
  135. /* DBCS code ranges and SBCS extend char conversion table */
  136. #if _CODE_PAGE == 932 /* Japanese Shift-JIS */
  137. #define _DF1S 0x81 /* DBC 1st byte range 1 start */
  138. #define _DF1E 0x9F /* DBC 1st byte range 1 end */
  139. #define _DF2S 0xE0 /* DBC 1st byte range 2 start */
  140. #define _DF2E 0xFC /* DBC 1st byte range 2 end */
  141. #define _DS1S 0x40 /* DBC 2nd byte range 1 start */
  142. #define _DS1E 0x7E /* DBC 2nd byte range 1 end */
  143. #define _DS2S 0x80 /* DBC 2nd byte range 2 start */
  144. #define _DS2E 0xFC /* DBC 2nd byte range 2 end */
  145. #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
  146. #define _DF1S 0x81
  147. #define _DF1E 0xFE
  148. #define _DS1S 0x40
  149. #define _DS1E 0x7E
  150. #define _DS2S 0x80
  151. #define _DS2E 0xFE
  152. #elif _CODE_PAGE == 949 /* Korean */
  153. #define _DF1S 0x81
  154. #define _DF1E 0xFE
  155. #define _DS1S 0x41
  156. #define _DS1E 0x5A
  157. #define _DS2S 0x61
  158. #define _DS2E 0x7A
  159. #define _DS3S 0x81
  160. #define _DS3E 0xFE
  161. #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */
  162. #define _DF1S 0x81
  163. #define _DF1E 0xFE
  164. #define _DS1S 0x40
  165. #define _DS1E 0x7E
  166. #define _DS2S 0xA1
  167. #define _DS2E 0xFE
  168. #elif _CODE_PAGE == 437 /* U.S. (OEM) */
  169. #define _DF1S 0
  170. #define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  171. 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  172. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  173. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  174. #elif _CODE_PAGE == 720 /* Arabic (OEM) */
  175. #define _DF1S 0
  176. #define _EXCVT {0x80,0x81,0x45,0x41,0x84,0x41,0x86,0x43,0x45,0x45,0x45,0x49,0x49,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x49,0x49,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  177. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  178. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  179. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  180. #elif _CODE_PAGE == 737 /* Greek (OEM) */
  181. #define _DF1S 0
  182. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x92,0x92,0x93,0x94,0x95,0x96,0x97,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, \
  183. 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0xAA,0x92,0x93,0x94,0x95,0x96,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  184. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  185. 0x97,0xEA,0xEB,0xEC,0xE4,0xED,0xEE,0xE7,0xE8,0xF1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  186. #elif _CODE_PAGE == 775 /* Baltic (OEM) */
  187. #define _DF1S 0
  188. #define _EXCVT {0x80,0x9A,0x91,0xA0,0x8E,0x95,0x8F,0x80,0xAD,0xED,0x8A,0x8A,0xA1,0x8D,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0x95,0x96,0x97,0x97,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
  189. 0xA0,0xA1,0xE0,0xA3,0xA3,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  190. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xB5,0xB6,0xB7,0xB8,0xBD,0xBE,0xC6,0xC7,0xA5,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  191. 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE3,0xE8,0xE8,0xEA,0xEA,0xEE,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  192. #elif _CODE_PAGE == 850 /* Multilingual Latin 1 (OEM) */
  193. #define _DF1S 0
  194. #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
  195. 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  196. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  197. 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  198. #elif _CODE_PAGE == 852 /* Latin 2 (OEM) */
  199. #define _DF1S 0
  200. #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xDE,0x8F,0x80,0x9D,0xD3,0x8A,0x8A,0xD7,0x8D,0x8E,0x8F,0x90,0x91,0x91,0xE2,0x99,0x95,0x95,0x97,0x97,0x99,0x9A,0x9B,0x9B,0x9D,0x9E,0x9F, \
  201. 0xB5,0xD6,0xE0,0xE9,0xA4,0xA4,0xA6,0xA6,0xA8,0xA8,0xAA,0x8D,0xAC,0xB8,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBD,0xBF, \
  202. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC6,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD2,0xD5,0xD6,0xD7,0xB7,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  203. 0xE0,0xE1,0xE2,0xE3,0xE3,0xD5,0xE6,0xE6,0xE8,0xE9,0xE8,0xEB,0xED,0xED,0xDD,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xEB,0xFC,0xFC,0xFE,0xFF}
  204. #elif _CODE_PAGE == 855 /* Cyrillic (OEM) */
  205. #define _DF1S 0
  206. #define _EXCVT {0x81,0x81,0x83,0x83,0x85,0x85,0x87,0x87,0x89,0x89,0x8B,0x8B,0x8D,0x8D,0x8F,0x8F,0x91,0x91,0x93,0x93,0x95,0x95,0x97,0x97,0x99,0x99,0x9B,0x9B,0x9D,0x9D,0x9F,0x9F, \
  207. 0xA1,0xA1,0xA3,0xA3,0xA5,0xA5,0xA7,0xA7,0xA9,0xA9,0xAB,0xAB,0xAD,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB6,0xB6,0xB8,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBE,0xBF, \
  208. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD3,0xD3,0xD5,0xD5,0xD7,0xD7,0xDD,0xD9,0xDA,0xDB,0xDC,0xDD,0xE0,0xDF, \
  209. 0xE0,0xE2,0xE2,0xE4,0xE4,0xE6,0xE6,0xE8,0xE8,0xEA,0xEA,0xEC,0xEC,0xEE,0xEE,0xEF,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF8,0xFA,0xFA,0xFC,0xFC,0xFD,0xFE,0xFF}
  210. #elif _CODE_PAGE == 857 /* Turkish (OEM) */
  211. #define _DF1S 0
  212. #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0x98,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x98,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9E, \
  213. 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA6,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  214. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  215. 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xDE,0x59,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  216. #elif _CODE_PAGE == 858 /* Multilingual Latin 1 + Euro (OEM) */
  217. #define _DF1S 0
  218. #define _EXCVT {0x80,0x9A,0x90,0xB6,0x8E,0xB7,0x8F,0x80,0xD2,0xD3,0xD4,0xD8,0xD7,0xDE,0x8E,0x8F,0x90,0x92,0x92,0xE2,0x99,0xE3,0xEA,0xEB,0x59,0x99,0x9A,0x9D,0x9C,0x9D,0x9E,0x9F, \
  219. 0xB5,0xD6,0xE0,0xE9,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  220. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC7,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD1,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  221. 0xE0,0xE1,0xE2,0xE3,0xE5,0xE5,0xE6,0xE7,0xE7,0xE9,0xEA,0xEB,0xED,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  222. #elif _CODE_PAGE == 862 /* Hebrew (OEM) */
  223. #define _DF1S 0
  224. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  225. 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  226. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  227. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  228. #elif _CODE_PAGE == 866 /* Russian (OEM) */
  229. #define _DF1S 0
  230. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  231. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  232. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  233. 0x90,0x91,0x92,0x93,0x9d,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,0xF0,0xF0,0xF2,0xF2,0xF4,0xF4,0xF6,0xF6,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  234. #elif _CODE_PAGE == 874 /* Thai (OEM, Windows) */
  235. #define _DF1S 0
  236. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  237. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  238. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  239. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  240. #elif _CODE_PAGE == 1250 /* Central Europe (Windows) */
  241. #define _DF1S 0
  242. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \
  243. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xA3,0xB4,0xB5,0xB6,0xB7,0xB8,0xA5,0xAA,0xBB,0xBC,0xBD,0xBC,0xAF, \
  244. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  245. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
  246. #elif _CODE_PAGE == 1251 /* Cyrillic (Windows) */
  247. #define _DF1S 0
  248. #define _EXCVT {0x80,0x81,0x82,0x82,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x80,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x8D,0x8E,0x8F, \
  249. 0xA0,0xA2,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB2,0xA5,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xA3,0xBD,0xBD,0xAF, \
  250. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  251. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF}
  252. #elif _CODE_PAGE == 1252 /* Latin 1 (Windows) */
  253. #define _DF1S 0
  254. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0xAd,0x9B,0x8C,0x9D,0xAE,0x9F, \
  255. 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  256. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  257. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}
  258. #elif _CODE_PAGE == 1253 /* Greek (Windows) */
  259. #define _DF1S 0
  260. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  261. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  262. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xA2,0xB8,0xB9,0xBA, \
  263. 0xE0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xFB,0xBC,0xFD,0xBF,0xFF}
  264. #elif _CODE_PAGE == 1254 /* Turkish (Windows) */
  265. #define _DF1S 0
  266. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x8A,0x9B,0x8C,0x9D,0x9E,0x9F, \
  267. 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  268. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  269. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0x9F}
  270. #elif _CODE_PAGE == 1255 /* Hebrew (Windows) */
  271. #define _DF1S 0
  272. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  273. 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  274. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  275. 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
  276. #elif _CODE_PAGE == 1256 /* Arabic (Windows) */
  277. #define _DF1S 0
  278. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x8C,0x9D,0x9E,0x9F, \
  279. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  280. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  281. 0x41,0xE1,0x41,0xE3,0xE4,0xE5,0xE6,0x43,0x45,0x45,0x45,0x45,0xEC,0xED,0x49,0x49,0xF0,0xF1,0xF2,0xF3,0x4F,0xF5,0xF6,0xF7,0xF8,0x55,0xFA,0x55,0x55,0xFD,0xFE,0xFF}
  282. #elif _CODE_PAGE == 1257 /* Baltic (Windows) */
  283. #define _DF1S 0
  284. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \
  285. 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xBC,0xBD,0xBE,0xAF, \
  286. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  287. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF}
  288. #elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */
  289. #define _DF1S 0
  290. #define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \
  291. 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \
  292. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \
  293. 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F}
  294. #elif _CODE_PAGE == 1 /* ASCII (for only non-LFN cfg) */
  295. #if _USE_LFN
  296. #error Cannot use LFN feature without valid code page.
  297. #endif
  298. #define _DF1S 0
  299. #else
  300. #error Unknown code page
  301. #endif
  302. /* Character code support macros */
  303. #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
  304. #define IsLower(c) (((c)>='a')&&((c)<='z'))
  305. #define IsDigit(c) (((c)>='0')&&((c)<='9'))
  306. #if _DF1S /* Code page is DBCS */
  307. #ifdef _DF2S /* Two 1st byte areas */
  308. #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
  309. #else /* One 1st byte area */
  310. #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
  311. #endif
  312. #ifdef _DS3S /* Three 2nd byte areas */
  313. #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
  314. #else /* Two 2nd byte areas */
  315. #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
  316. #endif
  317. #else /* Code page is SBCS */
  318. #define IsDBCS1(c) 0
  319. #define IsDBCS2(c) 0
  320. #endif /* _DF1S */
  321. /* Name status flags */
  322. #define NS 11 /* Index of name status byte in fn[] */
  323. #define NS_LOSS 0x01 /* Out of 8.3 format */
  324. #define NS_LFN 0x02 /* Force to create LFN entry */
  325. #define NS_LAST 0x04 /* Last segment */
  326. #define NS_BODY 0x08 /* Lower case flag (body) */
  327. #define NS_EXT 0x10 /* Lower case flag (ext) */
  328. #define NS_DOT 0x20 /* Dot entry */
  329. /* FAT sub-type boundaries */
  330. /* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */
  331. #define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */
  332. #define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */
  333. /* FatFs refers the members in the FAT structures as byte array instead of
  334. / structure member because the structure is not binary compatible between
  335. / different platforms */
  336. #define BS_jmpBoot 0 /* Jump instruction (3) */
  337. #define BS_OEMName 3 /* OEM name (8) */
  338. #define BPB_BytsPerSec 11 /* Sector size [byte] (2) */
  339. #define BPB_SecPerClus 13 /* Cluster size [sector] (1) */
  340. #define BPB_RsvdSecCnt 14 /* Size of reserved area [sector] (2) */
  341. #define BPB_NumFATs 16 /* Number of FAT copies (1) */
  342. #define BPB_RootEntCnt 17 /* Number of root dir entries for FAT12/16 (2) */
  343. #define BPB_TotSec16 19 /* Volume size [sector] (2) */
  344. #define BPB_Media 21 /* Media descriptor (1) */
  345. #define BPB_FATSz16 22 /* FAT size [sector] (2) */
  346. #define BPB_SecPerTrk 24 /* Track size [sector] (2) */
  347. #define BPB_NumHeads 26 /* Number of heads (2) */
  348. #define BPB_HiddSec 28 /* Number of special hidden sectors (4) */
  349. #define BPB_TotSec32 32 /* Volume size [sector] (4) */
  350. #define BS_DrvNum 36 /* Physical drive number (2) */
  351. #define BS_BootSig 38 /* Extended boot signature (1) */
  352. #define BS_VolID 39 /* Volume serial number (4) */
  353. #define BS_VolLab 43 /* Volume label (8) */
  354. #define BS_FilSysType 54 /* File system type (1) */
  355. #define BPB_FATSz32 36 /* FAT size [sector] (4) */
  356. #define BPB_ExtFlags 40 /* Extended flags (2) */
  357. #define BPB_FSVer 42 /* File system version (2) */
  358. #define BPB_RootClus 44 /* Root dir first cluster (4) */
  359. #define BPB_FSInfo 48 /* Offset of FSInfo sector (2) */
  360. #define BPB_BkBootSec 50 /* Offset of backup boot sectot (2) */
  361. #define BS_DrvNum32 64 /* Physical drive number (2) */
  362. #define BS_BootSig32 66 /* Extended boot signature (1) */
  363. #define BS_VolID32 67 /* Volume serial number (4) */
  364. #define BS_VolLab32 71 /* Volume label (8) */
  365. #define BS_FilSysType32 82 /* File system type (1) */
  366. #define FSI_LeadSig 0 /* FSI: Leading signature (4) */
  367. #define FSI_StrucSig 484 /* FSI: Structure signature (4) */
  368. #define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */
  369. #define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */
  370. #define MBR_Table 446 /* MBR: Partition table offset (2) */
  371. #define SZ_PTE 16 /* MBR: Size of a partition table entry */
  372. #define BS_55AA 510 /* Boot sector signature (2) */
  373. #define DIR_Name 0 /* Short file name (11) */
  374. #define DIR_Attr 11 /* Attribute (1) */
  375. #define DIR_NTres 12 /* NT flag (1) */
  376. #define DIR_CrtTime 14 /* Created time (2) */
  377. #define DIR_CrtDate 16 /* Created date (2) */
  378. #define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */
  379. #define DIR_WrtTime 22 /* Modified time (2) */
  380. #define DIR_WrtDate 24 /* Modified date (2) */
  381. #define DIR_FstClusLO 26 /* Lower 16-bit of first cluster (2) */
  382. #define DIR_FileSize 28 /* File size (4) */
  383. #define LDIR_Ord 0 /* LFN entry order and LLE flag (1) */
  384. #define LDIR_Attr 11 /* LFN attribute (1) */
  385. #define LDIR_Type 12 /* LFN type (1) */
  386. #define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
  387. #define LDIR_FstClusLO 26 /* Filled by zero (0) */
  388. #define SZ_DIR 32 /* Size of a directory entry */
  389. #define LLE 0x40 /* Last long entry flag in LDIR_Ord */
  390. #define DDE 0xE5 /* Deleted directory enrty mark in DIR_Name[0] */
  391. #define NDDE 0x05 /* Replacement of a character collides with DDE */
  392. /*------------------------------------------------------------*/
  393. /* Work area */
  394. #if _VOLUMES
  395. static
  396. FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */
  397. #else
  398. #error Number of drives must not be 0.
  399. #endif
  400. static
  401. WORD Fsid; /* File system mount ID */
  402. #if _FS_RPATH
  403. static
  404. BYTE CurrVol; /* Current drive */
  405. #endif
  406. #if _FS_SHARE
  407. static
  408. FILESEM Files[_FS_SHARE]; /* File lock semaphores */
  409. #endif
  410. #if _USE_LFN == 0 /* No LFN */
  411. #define DEF_NAMEBUF BYTE sfn[12]
  412. #define INIT_BUF(dobj) (dobj).fn = sfn
  413. #define FREE_BUF()
  414. #elif _USE_LFN == 1 /* LFN with static LFN working buffer */
  415. static WCHAR LfnBuf[_MAX_LFN+1];
  416. #define DEF_NAMEBUF BYTE sfn[12]
  417. #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
  418. #define FREE_BUF()
  419. #elif _USE_LFN == 2 /* LFN with dynamic LFN working buffer on the stack */
  420. #define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
  421. #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
  422. #define FREE_BUF()
  423. #elif _USE_LFN == 3 /* LFN with dynamic LFN working buffer on the heap */
  424. #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
  425. #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
  426. if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
  427. (dobj).lfn = lfn; (dobj).fn = sfn; }
  428. #define FREE_BUF() ff_memfree(lfn)
  429. #else
  430. #error Wrong LFN configuration.
  431. #endif
  432. /*--------------------------------------------------------------------------
  433. Module Private Functions
  434. ---------------------------------------------------------------------------*/
  435. /*-----------------------------------------------------------------------*/
  436. /* String functions */
  437. /*-----------------------------------------------------------------------*/
  438. /* Copy memory to memory */
  439. static
  440. void mem_cpy (void* dst, const void* src, UINT cnt) {
  441. BYTE *d = (BYTE*)dst;
  442. const BYTE *s = (const BYTE*)src;
  443. #if _WORD_ACCESS == 1
  444. if (((unsigned)d & 0x03)!=0 || ((unsigned)s & 0x03)!=0)
  445. {
  446. while (cnt--)
  447. *d++ = *s++;
  448. return;
  449. }
  450. while (cnt >= sizeof(int)) {
  451. *(int*)d = *(int*)s;
  452. d += sizeof(int); s += sizeof(int);
  453. cnt -= sizeof(int);
  454. }
  455. #endif
  456. while (cnt--)
  457. *d++ = *s++;
  458. }
  459. /* Fill memory */
  460. static
  461. void mem_set (void* dst, int val, UINT cnt) {
  462. BYTE *d = (BYTE*)dst;
  463. while (cnt--)
  464. *d++ = (BYTE)val;
  465. }
  466. /* Compare memory to memory */
  467. static
  468. int mem_cmp (const void* dst, const void* src, UINT cnt) {
  469. const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
  470. int r = 0;
  471. while (cnt-- && (r = *d++ - *s++) == 0) ;
  472. return r;
  473. }
  474. /* Check if chr is contained in the string */
  475. static
  476. int chk_chr (const char* str, int chr) {
  477. while (*str && *str != chr) str++;
  478. return *str;
  479. }
  480. /*-----------------------------------------------------------------------*/
  481. /* Request/Release grant to access the volume */
  482. /*-----------------------------------------------------------------------*/
  483. #if _FS_REENTRANT
  484. static
  485. int lock_fs (
  486. FATFS *fs /* File system object */
  487. )
  488. {
  489. return ff_req_grant(fs->sobj);
  490. }
  491. static
  492. void unlock_fs (
  493. FATFS *fs, /* File system object */
  494. FRESULT res /* Result code to be returned */
  495. )
  496. {
  497. if (res != FR_NOT_ENABLED &&
  498. res != FR_INVALID_DRIVE &&
  499. res != FR_INVALID_OBJECT &&
  500. res != FR_TIMEOUT) {
  501. ff_rel_grant(fs->sobj);
  502. }
  503. }
  504. #endif
  505. /*-----------------------------------------------------------------------*/
  506. /* File shareing control functions */
  507. /*-----------------------------------------------------------------------*/
  508. #if _FS_SHARE
  509. static
  510. FRESULT chk_lock ( /* Check if the file can be accessed */
  511. DIR* dj, /* Directory object pointing the file to be checked */
  512. int acc /* Desired access (0:Read, 1:Write, 2:Delete/Rename) */
  513. )
  514. {
  515. UINT i, be;
  516. /* Search file semaphore table */
  517. for (i = be = 0; i < _FS_SHARE; i++) {
  518. if (Files[i].fs) { /* Existing entry */
  519. if (Files[i].fs == dj->fs && /* Check if the file matched with an open file */
  520. Files[i].clu == dj->sclust &&
  521. Files[i].idx == dj->index) break;
  522. } else { /* Blank entry */
  523. be++;
  524. }
  525. }
  526. if (i == _FS_SHARE) /* The file is not opened */
  527. return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES; /* Is there a blank entry for new file? */
  528. /* The file has been opened. Reject any open against writing file and all write mode open */
  529. return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
  530. }
  531. static
  532. int enq_lock ( /* Check if an entry is available for a new file */
  533. FATFS* fs /* File system object */
  534. )
  535. {
  536. UINT i;
  537. for (i = 0; i < _FS_SHARE && Files[i].fs; i++) ;
  538. return (i == _FS_SHARE) ? 0 : 1;
  539. }
  540. static
  541. UINT inc_lock ( /* Increment file open counter and returns its index (0:int error) */
  542. DIR* dj, /* Directory object pointing the file to register or increment */
  543. int acc /* Desired access mode (0:Read, !0:Write) */
  544. )
  545. {
  546. UINT i;
  547. for (i = 0; i < _FS_SHARE; i++) { /* Find the file */
  548. if (Files[i].fs == dj->fs &&
  549. Files[i].clu == dj->sclust &&
  550. Files[i].idx == dj->index) break;
  551. }
  552. if (i == _FS_SHARE) { /* Not opened. Register it as new. */
  553. for (i = 0; i < _FS_SHARE && Files[i].fs; i++) ;
  554. if (i == _FS_SHARE) return 0; /* No space to register (int err) */
  555. Files[i].fs = dj->fs;
  556. Files[i].clu = dj->sclust;
  557. Files[i].idx = dj->index;
  558. Files[i].ctr = 0;
  559. }
  560. if (acc && Files[i].ctr) return 0; /* Access violation (int err) */
  561. Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1; /* Set semaphore value */
  562. return i + 1;
  563. }
  564. static
  565. FRESULT dec_lock ( /* Decrement file open counter */
  566. UINT i /* Semaphore index */
  567. )
  568. {
  569. WORD n;
  570. FRESULT res;
  571. if (--i < _FS_SHARE) {
  572. n = Files[i].ctr;
  573. if (n == 0x100) n = 0;
  574. if (n) n--;
  575. Files[i].ctr = n;
  576. if (!n) Files[i].fs = 0;
  577. res = FR_OK;
  578. } else {
  579. res = FR_INT_ERR;
  580. }
  581. return res;
  582. }
  583. static
  584. void clear_lock ( /* Clear lock entries of the volume */
  585. FATFS *fs
  586. )
  587. {
  588. UINT i;
  589. for (i = 0; i < _FS_SHARE; i++) {
  590. if (Files[i].fs == fs) Files[i].fs = 0;
  591. }
  592. }
  593. #endif
  594. /*-----------------------------------------------------------------------*/
  595. /* Change window offset */
  596. /*-----------------------------------------------------------------------*/
  597. static
  598. FRESULT move_window (
  599. FATFS *fs, /* File system object */
  600. DWORD sector /* Sector number to make appearance in the fs->win[] */
  601. ) /* Move to zero only writes back dirty window */
  602. {
  603. DWORD wsect;
  604. wsect = fs->winsect;
  605. if (wsect != sector) { /* Changed current window */
  606. #if !_FS_READONLY
  607. if (fs->wflag) { /* Write back dirty window if needed */
  608. if (disk_write(fs->drv, fs->win, wsect, 1) != RES_OK)
  609. return FR_DISK_ERR;
  610. fs->wflag = 0;
  611. if (wsect < (fs->fatbase + fs->fsize)) { /* In FAT area */
  612. BYTE nf;
  613. for (nf = fs->n_fats; nf > 1; nf--) { /* Reflect the change to all FAT copies */
  614. wsect += fs->fsize;
  615. disk_write(fs->drv, fs->win, wsect, 1);
  616. }
  617. }
  618. }
  619. #endif
  620. if (sector) {
  621. if (disk_read(fs->drv, fs->win, sector, 1) != RES_OK)
  622. return FR_DISK_ERR;
  623. fs->winsect = sector;
  624. }
  625. }
  626. return FR_OK;
  627. }
  628. /*-----------------------------------------------------------------------*/
  629. /* Clean-up cached data */
  630. /*-----------------------------------------------------------------------*/
  631. #if !_FS_READONLY
  632. static
  633. FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */
  634. FATFS *fs /* File system object */
  635. )
  636. {
  637. FRESULT res;
  638. res = move_window(fs, 0);
  639. if (res == FR_OK) {
  640. /* Update FSInfo sector if needed */
  641. if (fs->fs_type == FS_FAT32 && fs->fsi_flag) {
  642. fs->winsect = 0;
  643. /* Create FSInfo structure */
  644. mem_set(fs->win, 0, 512);
  645. ST_WORD(fs->win+BS_55AA, 0xAA55);
  646. ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
  647. ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
  648. ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);
  649. ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);
  650. /* Write it into the FSInfo sector */
  651. disk_write(fs->drv, fs->win, fs->fsi_sector, 1);
  652. fs->fsi_flag = 0;
  653. }
  654. /* Make sure that no pending write process in the physical drive */
  655. if (disk_ioctl(fs->drv, CTRL_SYNC, (void*)0) != RES_OK)
  656. res = FR_DISK_ERR;
  657. }
  658. return res;
  659. }
  660. #endif
  661. /*-----------------------------------------------------------------------*/
  662. /* Get sector# from cluster# */
  663. /*-----------------------------------------------------------------------*/
  664. DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */
  665. FATFS *fs, /* File system object */
  666. DWORD clst /* Cluster# to be converted */
  667. )
  668. {
  669. clst -= 2;
  670. if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */
  671. return clst * fs->csize + fs->database;
  672. }
  673. /*-----------------------------------------------------------------------*/
  674. /* FAT access - Read value of a FAT entry */
  675. /*-----------------------------------------------------------------------*/
  676. DWORD get_fat ( /* 0xFFFFFFFF:Disk error, 1:Internal error, Else:Cluster status */
  677. FATFS *fs, /* File system object */
  678. DWORD clst /* Cluster# to get the link information */
  679. )
  680. {
  681. UINT wc, bc;
  682. BYTE *p;
  683. if (clst < 2 || clst >= fs->n_fatent) /* Chack range */
  684. return 1;
  685. switch (fs->fs_type) {
  686. case FS_FAT12 :
  687. bc = (UINT)clst; bc += bc / 2;
  688. if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
  689. wc = fs->win[bc % SS(fs)]; bc++;
  690. if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
  691. wc |= fs->win[bc % SS(fs)] << 8;
  692. return (clst & 1) ? (wc >> 4) : (wc & 0xFFF);
  693. case FS_FAT16 :
  694. if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break;
  695. p = &fs->win[clst * 2 % SS(fs)];
  696. return LD_WORD(p);
  697. case FS_FAT32 :
  698. if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break;
  699. p = &fs->win[clst * 4 % SS(fs)];
  700. return LD_DWORD(p) & 0x0FFFFFFF;
  701. }
  702. return 0xFFFFFFFF; /* An error occurred at the disk I/O layer */
  703. }
  704. /*-----------------------------------------------------------------------*/
  705. /* FAT access - Change value of a FAT entry */
  706. /*-----------------------------------------------------------------------*/
  707. #if !_FS_READONLY
  708. FRESULT put_fat (
  709. FATFS *fs, /* File system object */
  710. DWORD clst, /* Cluster# to be changed in range of 2 to fs->n_fatent - 1 */
  711. DWORD val /* New value to mark the cluster */
  712. )
  713. {
  714. UINT bc;
  715. BYTE *p;
  716. FRESULT res;
  717. if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
  718. res = FR_INT_ERR;
  719. } else {
  720. switch (fs->fs_type) {
  721. case FS_FAT12 :
  722. bc = clst; bc += bc / 2;
  723. res = move_window(fs, fs->fatbase + (bc / SS(fs)));
  724. if (res != FR_OK) break;
  725. p = &fs->win[bc % SS(fs)];
  726. *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
  727. bc++;
  728. fs->wflag = 1;
  729. res = move_window(fs, fs->fatbase + (bc / SS(fs)));
  730. if (res != FR_OK) break;
  731. p = &fs->win[bc % SS(fs)];
  732. *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
  733. break;
  734. case FS_FAT16 :
  735. res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
  736. if (res != FR_OK) break;
  737. p = &fs->win[clst * 2 % SS(fs)];
  738. ST_WORD(p, (WORD)val);
  739. break;
  740. case FS_FAT32 :
  741. res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
  742. if (res != FR_OK) break;
  743. p = &fs->win[clst * 4 % SS(fs)];
  744. val |= LD_DWORD(p) & 0xF0000000;
  745. ST_DWORD(p, val);
  746. break;
  747. default :
  748. res = FR_INT_ERR;
  749. }
  750. fs->wflag = 1;
  751. }
  752. return res;
  753. }
  754. #endif /* !_FS_READONLY */
  755. /*-----------------------------------------------------------------------*/
  756. /* FAT handling - Remove a cluster chain */
  757. /*-----------------------------------------------------------------------*/
  758. #if !_FS_READONLY
  759. static
  760. FRESULT remove_chain (
  761. FATFS *fs, /* File system object */
  762. DWORD clst /* Cluster# to remove a chain from */
  763. )
  764. {
  765. FRESULT res;
  766. DWORD nxt;
  767. #if _USE_ERASE
  768. DWORD scl = clst, ecl = clst, resion[2];
  769. #endif
  770. if (clst < 2 || clst >= fs->n_fatent) { /* Check range */
  771. res = FR_INT_ERR;
  772. } else {
  773. res = FR_OK;
  774. while (clst < fs->n_fatent) { /* Not a last link? */
  775. nxt = get_fat(fs, clst); /* Get cluster status */
  776. if (nxt == 0) break; /* Empty cluster? */
  777. if (nxt == 1) { res = FR_INT_ERR; break; } /* Internal error? */
  778. if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; } /* Disk error? */
  779. res = put_fat(fs, clst, 0); /* Mark the cluster "empty" */
  780. if (res != FR_OK) break;
  781. if (fs->free_clust != 0xFFFFFFFF) { /* Update FSInfo */
  782. fs->free_clust++;
  783. fs->fsi_flag = 1;
  784. }
  785. #if _USE_ERASE
  786. if (ecl + 1 == nxt) { /* Next cluster is contiguous */
  787. ecl = nxt;
  788. } else { /* End of contiguous clusters */
  789. resion[0] = clust2sect(fs, scl); /* Start sector */
  790. resion[1] = clust2sect(fs, ecl) + fs->csize - 1; /* End sector */
  791. disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, resion); /* Erase the block */
  792. scl = ecl = nxt;
  793. }
  794. #endif
  795. clst = nxt; /* Next cluster */
  796. }
  797. }
  798. return res;
  799. }
  800. #endif
  801. /*-----------------------------------------------------------------------*/
  802. /* FAT handling - Stretch or Create a cluster chain */
  803. /*-----------------------------------------------------------------------*/
  804. #if !_FS_READONLY
  805. static
  806. DWORD create_chain ( /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
  807. FATFS *fs, /* File system object */
  808. DWORD clst /* Cluster# to stretch. 0 means create a new chain. */
  809. )
  810. {
  811. DWORD cs, ncl, scl;
  812. FRESULT res;
  813. if (clst == 0) { /* Create a new chain */
  814. scl = fs->last_clust; /* Get suggested start point */
  815. if (!scl || scl >= fs->n_fatent) scl = 1;
  816. }
  817. else { /* Stretch the current chain */
  818. cs = get_fat(fs, clst); /* Check the cluster status */
  819. if (cs < 2) return 1; /* It is an invalid cluster */
  820. if (cs < fs->n_fatent) return cs; /* It is already followed by next cluster */
  821. scl = clst;
  822. }
  823. ncl = scl; /* Start cluster */
  824. for (;;) {
  825. ncl++; /* Next cluster */
  826. if (ncl >= fs->n_fatent) { /* Wrap around */
  827. ncl = 2;
  828. if (ncl > scl) return 0; /* No free cluster */
  829. }
  830. cs = get_fat(fs, ncl); /* Get the cluster status */
  831. if (cs == 0) break; /* Found a free cluster */
  832. if (cs == 0xFFFFFFFF || cs == 1)/* An error occurred */
  833. return cs;
  834. if (ncl == scl) return 0; /* No free cluster */
  835. }
  836. res = put_fat(fs, ncl, 0x0FFFFFFF); /* Mark the new cluster "last link" */
  837. if (res == FR_OK && clst != 0) {
  838. res = put_fat(fs, clst, ncl); /* Link it to the previous one if needed */
  839. }
  840. if (res == FR_OK) {
  841. fs->last_clust = ncl; /* Update FSINFO */
  842. if (fs->free_clust != 0xFFFFFFFF) {
  843. fs->free_clust--;
  844. fs->fsi_flag = 1;
  845. }
  846. } else {
  847. ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
  848. }
  849. return ncl; /* Return new cluster number or error code */
  850. }
  851. #endif /* !_FS_READONLY */
  852. /*-----------------------------------------------------------------------*/
  853. /* FAT handling - Convert offset into cluster with link map table */
  854. /*-----------------------------------------------------------------------*/
  855. #if _USE_FASTSEEK
  856. static
  857. DWORD clmt_clust ( /* <2:Error, >=2:Cluster number */
  858. FIL* fp, /* Pointer to the file object */
  859. DWORD ofs /* File offset to be converted to cluster# */
  860. )
  861. {
  862. DWORD cl, ncl, *tbl;
  863. tbl = fp->cltbl + 1; /* Top of CLMT */
  864. cl = ofs / SS(fp->fs) / fp->fs->csize; /* Cluster order from top of the file */
  865. for (;;) {
  866. ncl = *tbl++; /* Number of cluters in the fragment */
  867. if (!ncl) return 0; /* End of table? (error) */
  868. if (cl < ncl) break; /* In this fragment? */
  869. cl -= ncl; tbl++; /* Next fragment */
  870. }
  871. return cl + *tbl; /* Return the cluster number */
  872. }
  873. #endif /* _USE_FASTSEEK */
  874. /*-----------------------------------------------------------------------*/
  875. /* Directory handling - Set directory index */
  876. /*-----------------------------------------------------------------------*/
  877. static
  878. FRESULT dir_sdi (
  879. DIR *dj, /* Pointer to directory object */
  880. WORD idx /* Directory index number */
  881. )
  882. {
  883. DWORD clst;
  884. WORD ic;
  885. dj->index = idx;
  886. clst = dj->sclust;
  887. if (clst == 1 || clst >= dj->fs->n_fatent) /* Check start cluster range */
  888. return FR_INT_ERR;
  889. if (!clst && dj->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
  890. clst = dj->fs->dirbase;
  891. if (clst == 0) { /* Static table (root-dir in FAT12/16) */
  892. dj->clust = clst;
  893. if (idx >= dj->fs->n_rootdir) /* Index is out of range */
  894. return FR_INT_ERR;
  895. dj->sect = dj->fs->dirbase + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */
  896. }
  897. else { /* Dynamic table (sub-dirs or root-dir in FAT32) */
  898. ic = SS(dj->fs) / SZ_DIR * dj->fs->csize; /* Entries per cluster */
  899. while (idx >= ic) { /* Follow cluster chain */
  900. clst = get_fat(dj->fs, clst); /* Get next cluster */
  901. if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
  902. if (clst < 2 || clst >= dj->fs->n_fatent) /* Reached to end of table or int error */
  903. return FR_INT_ERR;
  904. idx -= ic;
  905. }
  906. dj->clust = clst;
  907. dj->sect = clust2sect(dj->fs, clst) + idx / (SS(dj->fs) / SZ_DIR); /* Sector# */
  908. }
  909. dj->dir = dj->fs->win + (idx % (SS(dj->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */
  910. return FR_OK; /* Seek succeeded */
  911. }
  912. /*-----------------------------------------------------------------------*/
  913. /* Directory handling - Move directory index next */
  914. /*-----------------------------------------------------------------------*/
  915. static
  916. FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */
  917. DIR *dj, /* Pointer to directory object */
  918. int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
  919. )
  920. {
  921. DWORD clst;
  922. WORD i;
  923. i = dj->index + 1;
  924. if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
  925. return FR_NO_FILE;
  926. if (!(i % (SS(dj->fs) / SZ_DIR))) { /* Sector changed? */
  927. dj->sect++; /* Next sector */
  928. if (dj->clust == 0) { /* Static table */
  929. if (i >= dj->fs->n_rootdir) /* Report EOT when end of table */
  930. return FR_NO_FILE;
  931. }
  932. else { /* Dynamic table */
  933. if (((i / (SS(dj->fs) / SZ_DIR)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
  934. clst = get_fat(dj->fs, dj->clust); /* Get next cluster */
  935. if (clst <= 1) return FR_INT_ERR;
  936. if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
  937. if (clst >= dj->fs->n_fatent) { /* When it reached end of dynamic table */
  938. #if !_FS_READONLY
  939. BYTE c;
  940. if (!stretch) return FR_NO_FILE; /* When do not stretch, report EOT */
  941. clst = create_chain(dj->fs, dj->clust); /* Stretch cluster chain */
  942. if (clst == 0) return FR_DENIED; /* No free cluster */
  943. if (clst == 1) return FR_INT_ERR;
  944. if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
  945. /* Clean-up stretched table */
  946. if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */
  947. mem_set(dj->fs->win, 0, SS(dj->fs)); /* Clear window buffer */
  948. dj->fs->winsect = clust2sect(dj->fs, clst); /* Cluster start sector */
  949. for (c = 0; c < dj->fs->csize; c++) { /* Fill the new cluster with 0 */
  950. dj->fs->wflag = 1;
  951. if (move_window(dj->fs, 0)) return FR_DISK_ERR;
  952. dj->fs->winsect++;
  953. }
  954. dj->fs->winsect -= c; /* Rewind window address */
  955. #else
  956. return FR_NO_FILE; /* Report EOT */
  957. #endif
  958. }
  959. dj->clust = clst; /* Initialize data for new cluster */
  960. dj->sect = clust2sect(dj->fs, clst);
  961. }
  962. }
  963. }
  964. dj->index = i;
  965. dj->dir = dj->fs->win + (i % (SS(dj->fs) / SZ_DIR)) * SZ_DIR;
  966. return FR_OK;
  967. }
  968. /*-----------------------------------------------------------------------*/
  969. /* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
  970. /*-----------------------------------------------------------------------*/
  971. #if _USE_LFN
  972. static
  973. const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN chars in the directory entry */
  974. static
  975. int cmp_lfn ( /* 1:Matched, 0:Not matched */
  976. WCHAR *lfnbuf, /* Pointer to the LFN to be compared */
  977. BYTE *dir /* Pointer to the directory entry containing a part of LFN */
  978. )
  979. {
  980. UINT i, s;
  981. WCHAR wc, uc;
  982. i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13; /* Get offset in the LFN buffer */
  983. s = 0; wc = 1;
  984. do {
  985. uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
  986. if (wc) { /* Last char has not been processed */
  987. wc = ff_wtoupper(uc); /* Convert it to upper case */
  988. if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
  989. return 0; /* Not matched */
  990. } else {
  991. if (uc != 0xFFFF) return 0; /* Check filler */
  992. }
  993. } while (++s < 13); /* Repeat until all chars in the entry are checked */
  994. if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i]) /* Last segment matched but different length */
  995. return 0;
  996. return 1; /* The part of LFN matched */
  997. }
  998. static
  999. int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
  1000. WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */
  1001. BYTE *dir /* Pointer to the directory entry */
  1002. )
  1003. {
  1004. UINT i, s;
  1005. WCHAR wc, uc;
  1006. i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
  1007. s = 0; wc = 1;
  1008. do {
  1009. uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
  1010. if (wc) { /* Last char has not been processed */
  1011. if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
  1012. lfnbuf[i++] = wc = uc; /* Store it */
  1013. } else {
  1014. if (uc != 0xFFFF) return 0; /* Check filler */
  1015. }
  1016. } while (++s < 13); /* Read all character in the entry */
  1017. if (dir[LDIR_Ord] & LLE) { /* Put terminator if it is the last LFN part */
  1018. if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
  1019. lfnbuf[i] = 0;
  1020. }
  1021. return 1;
  1022. }
  1023. #if !_FS_READONLY
  1024. static
  1025. void fit_lfn (
  1026. const WCHAR *lfnbuf, /* Pointer to the LFN buffer */
  1027. BYTE *dir, /* Pointer to the directory entry */
  1028. BYTE ord, /* LFN order (1-20) */
  1029. BYTE sum /* SFN sum */
  1030. )
  1031. {
  1032. UINT i, s;
  1033. WCHAR wc;
  1034. dir[LDIR_Chksum] = sum; /* Set check sum */
  1035. dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
  1036. dir[LDIR_Type] = 0;
  1037. ST_WORD(dir+LDIR_FstClusLO, 0);
  1038. i = (ord - 1) * 13; /* Get offset in the LFN buffer */
  1039. s = wc = 0;
  1040. do {
  1041. if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective char */
  1042. ST_WORD(dir+LfnOfs[s], wc); /* Put it */
  1043. if (!wc) wc = 0xFFFF; /* Padding chars following last char */
  1044. } while (++s < 13);
  1045. if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE; /* Bottom LFN part is the start of LFN sequence */
  1046. dir[LDIR_Ord] = ord; /* Set the LFN order */
  1047. }
  1048. #endif
  1049. #endif
  1050. /*-----------------------------------------------------------------------*/
  1051. /* Create numbered name */
  1052. /*-----------------------------------------------------------------------*/
  1053. #if _USE_LFN
  1054. void gen_numname (
  1055. BYTE *dst, /* Pointer to generated SFN */
  1056. const BYTE *src, /* Pointer to source SFN to be modified */
  1057. const WCHAR *lfn, /* Pointer to LFN */
  1058. WORD seq /* Sequence number */
  1059. )
  1060. {
  1061. BYTE ns[8], c;
  1062. UINT i, j;
  1063. mem_cpy(dst, src, 11);
  1064. if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */
  1065. do seq = (seq >> 1) + (seq << 15) + (WORD)*lfn++; while (*lfn);
  1066. }
  1067. /* itoa (hexdecimal) */
  1068. i = 7;
  1069. do {
  1070. c = (seq % 16) + '0';
  1071. if (c > '9') c += 7;
  1072. ns[i--] = c;
  1073. seq /= 16;
  1074. } while (seq);
  1075. ns[i] = '~';
  1076. /* Append the number */
  1077. for (j = 0; j < i && dst[j] != ' '; j++) {
  1078. if (IsDBCS1(dst[j])) {
  1079. if (j == i - 1) break;
  1080. j++;
  1081. }
  1082. }
  1083. do {
  1084. dst[j++] = (i < 8) ? ns[i++] : ' ';
  1085. } while (j < 8);
  1086. }
  1087. #endif
  1088. /*-----------------------------------------------------------------------*/
  1089. /* Calculate sum of an SFN */
  1090. /*-----------------------------------------------------------------------*/
  1091. #if _USE_LFN
  1092. static
  1093. BYTE sum_sfn (
  1094. const BYTE *dir /* Ptr to directory entry */
  1095. )
  1096. {
  1097. BYTE sum = 0;
  1098. UINT n = 11;
  1099. do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
  1100. return sum;
  1101. }
  1102. #endif
  1103. /*-----------------------------------------------------------------------*/
  1104. /* Directory handling - Find an object in the directory */
  1105. /*-----------------------------------------------------------------------*/
  1106. static
  1107. FRESULT dir_find (
  1108. DIR *dj /* Pointer to the directory object linked to the file name */
  1109. )
  1110. {
  1111. FRESULT res;
  1112. BYTE c, *dir;
  1113. #if _USE_LFN
  1114. BYTE a, ord, sum;
  1115. #endif
  1116. res = dir_sdi(dj, 0); /* Rewind directory object */
  1117. if (res != FR_OK) return res;
  1118. #if _USE_LFN
  1119. ord = sum = 0xFF;
  1120. #endif
  1121. do {
  1122. res = move_window(dj->fs, dj->sect);
  1123. if (res != FR_OK) break;
  1124. dir = dj->dir; /* Ptr to the directory entry of current index */
  1125. c = dir[DIR_Name];
  1126. if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
  1127. #if _USE_LFN /* LFN configuration */
  1128. a = dir[DIR_Attr] & AM_MASK;
  1129. if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
  1130. ord = 0xFF;
  1131. } else {
  1132. if (a == AM_LFN) { /* An LFN entry is found */
  1133. if (dj->lfn) {
  1134. if (c & LLE) { /* Is it start of LFN sequence? */
  1135. sum = dir[LDIR_Chksum];
  1136. c &= ~LLE; ord = c; /* LFN start order */
  1137. dj->lfn_idx = dj->index;
  1138. }
  1139. /* Check validity of the LFN entry and compare it with given name */
  1140. ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
  1141. }
  1142. } else { /* An SFN entry is found */
  1143. if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
  1144. ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
  1145. if (!(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */
  1146. }
  1147. }
  1148. #else /* Non LFN configuration */
  1149. if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
  1150. break;
  1151. #endif
  1152. res = dir_next(dj, 0); /* Next entry */
  1153. } while (res == FR_OK);
  1154. return res;
  1155. }
  1156. /*-----------------------------------------------------------------------*/
  1157. /* Read an object from the directory */
  1158. /*-----------------------------------------------------------------------*/
  1159. #if _FS_MINIMIZE <= 1
  1160. static
  1161. FRESULT dir_read (
  1162. DIR *dj /* Pointer to the directory object that pointing the entry to be read */
  1163. )
  1164. {
  1165. FRESULT res;
  1166. BYTE c, *dir;
  1167. #if _USE_LFN
  1168. BYTE a, ord = 0xFF, sum = 0xFF;
  1169. #endif
  1170. res = FR_NO_FILE;
  1171. while (dj->sect) {
  1172. res = move_window(dj->fs, dj->sect);
  1173. if (res != FR_OK) break;
  1174. dir = dj->dir; /* Ptr to the directory entry of current index */
  1175. c = dir[DIR_Name];
  1176. if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
  1177. #if _USE_LFN /* LFN configuration */
  1178. a = dir[DIR_Attr] & AM_MASK;
  1179. if (c == DDE || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
  1180. ord = 0xFF;
  1181. } else {
  1182. if (a == AM_LFN) { /* An LFN entry is found */
  1183. if (c & LLE) { /* Is it start of LFN sequence? */
  1184. sum = dir[LDIR_Chksum];
  1185. c &= ~LLE; ord = c;
  1186. dj->lfn_idx = dj->index;
  1187. }
  1188. /* Check LFN validity and capture it */
  1189. ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
  1190. } else { /* An SFN entry is found */
  1191. if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
  1192. dj->lfn_idx = 0xFFFF; /* It has no LFN. */
  1193. break;
  1194. }
  1195. }
  1196. #else /* Non LFN configuration */
  1197. if (c != DDE && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
  1198. break;
  1199. #endif
  1200. res = dir_next(dj, 0); /* Next entry */
  1201. if (res != FR_OK) break;
  1202. }
  1203. if (res != FR_OK) dj->sect = 0;
  1204. return res;
  1205. }
  1206. #endif
  1207. /*-----------------------------------------------------------------------*/
  1208. /* Register an object to the directory */
  1209. /*-----------------------------------------------------------------------*/
  1210. #if !_FS_READONLY
  1211. static
  1212. FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */
  1213. DIR *dj /* Target directory with object name to be created */
  1214. )
  1215. {
  1216. FRESULT res;
  1217. BYTE c, *dir;
  1218. #if _USE_LFN /* LFN configuration */
  1219. WORD n, ne, is;
  1220. BYTE sn[12], *fn, sum;
  1221. WCHAR *lfn;
  1222. fn = dj->fn; lfn = dj->lfn;
  1223. mem_cpy(sn, fn, 12);
  1224. if (_FS_RPATH && (sn[NS] & NS_DOT)) /* Cannot create dot entry */
  1225. return FR_INVALID_NAME;
  1226. if (sn[NS] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
  1227. fn[NS] = 0; dj->lfn = 0; /* Find only SFN */
  1228. for (n = 1; n < 100; n++) {
  1229. gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
  1230. res = dir_find(dj); /* Check if the name collides with existing SFN */
  1231. if (res != FR_OK) break;
  1232. }
  1233. if (n == 100) return FR_DENIED; /* Abort if too many collisions */
  1234. if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
  1235. fn[NS] = sn[NS]; dj->lfn = lfn;
  1236. }
  1237. if (sn[NS] & NS_LFN) { /* When LFN is to be created, reserve an SFN + LFN entries. */
  1238. for (ne = 0; lfn[ne]; ne++) ;
  1239. ne = (ne + 25) / 13;
  1240. } else { /* Otherwise reserve only an SFN entry. */
  1241. ne = 1;
  1242. }
  1243. /* Reserve contiguous entries */
  1244. res = dir_sdi(dj, 0);
  1245. if (res != FR_OK) return res;
  1246. n = is = 0;
  1247. do {
  1248. res = move_window(dj->fs, dj->sect);
  1249. if (res != FR_OK) break;
  1250. c = *dj->dir; /* Check the entry status */
  1251. if (c == DDE || c == 0) { /* Is it a blank entry? */
  1252. if (n == 0) is = dj->index; /* First index of the contiguous entry */
  1253. if (++n == ne) break; /* A contiguous entry that required count is found */
  1254. } else {
  1255. n = 0; /* Not a blank entry. Restart to search */
  1256. }
  1257. res = dir_next(dj, 1); /* Next entry with table stretch */
  1258. } while (res == FR_OK);
  1259. if (res == FR_OK && ne > 1) { /* Initialize LFN entry if needed */
  1260. res = dir_sdi(dj, is);
  1261. if (res == FR_OK) {
  1262. sum = sum_sfn(dj->fn); /* Sum of the SFN tied to the LFN */
  1263. ne--;
  1264. do { /* Store LFN entries in bottom first */
  1265. res = move_window(dj->fs, dj->sect);
  1266. if (res != FR_OK) break;
  1267. fit_lfn(dj->lfn, dj->dir, (BYTE)ne, sum);
  1268. dj->fs->wflag = 1;
  1269. res = dir_next(dj, 0); /* Next entry */
  1270. } while (res == FR_OK && --ne);
  1271. }
  1272. }
  1273. #else /* Non LFN configuration */
  1274. res = dir_sdi(dj, 0);
  1275. if (res == FR_OK) {
  1276. do { /* Find a blank entry for the SFN */
  1277. res = move_window(dj->fs, dj->sect);
  1278. if (res != FR_OK) break;
  1279. c = *dj->dir;
  1280. if (c == DDE || c == 0) break; /* Is it a blank entry? */
  1281. res = dir_next(dj, 1); /* Next entry with table stretch */
  1282. } while (res == FR_OK);
  1283. }
  1284. #endif
  1285. if (res == FR_OK) { /* Initialize the SFN entry */
  1286. res = move_window(dj->fs, dj->sect);
  1287. if (res == FR_OK) {
  1288. dir = dj->dir;
  1289. mem_set(dir, 0, SZ_DIR); /* Clean the entry */
  1290. mem_cpy(dir, dj->fn, 11); /* Put SFN */
  1291. #if _USE_LFN
  1292. dir[DIR_NTres] = *(dj->fn+NS) & (NS_BODY | NS_EXT); /* Put NT flag */
  1293. #endif
  1294. dj->fs->wflag = 1;
  1295. }
  1296. }
  1297. return res;
  1298. }
  1299. #endif /* !_FS_READONLY */
  1300. /*-----------------------------------------------------------------------*/
  1301. /* Remove an object from the directory */
  1302. /*-----------------------------------------------------------------------*/
  1303. #if !_FS_READONLY && !_FS_MINIMIZE
  1304. static
  1305. FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */
  1306. DIR *dj /* Directory object pointing the entry to be removed */
  1307. )
  1308. {
  1309. FRESULT res;
  1310. #if _USE_LFN /* LFN configuration */
  1311. WORD i;
  1312. i = dj->index; /* SFN index */
  1313. res = dir_sdi(dj, (WORD)((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx)); /* Goto the SFN or top of the LFN entries */
  1314. if (res == FR_OK) {
  1315. do {
  1316. res = move_window(dj->fs, dj->sect);
  1317. if (res != FR_OK) break;
  1318. *dj->dir = DDE; /* Mark the entry "deleted" */
  1319. dj->fs->wflag = 1;
  1320. if (dj->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */
  1321. res = dir_next(dj, 0); /* Next entry */
  1322. } while (res == FR_OK);
  1323. if (res == FR_NO_FILE) res = FR_INT_ERR;
  1324. }
  1325. #else /* Non LFN configuration */
  1326. res = dir_sdi(dj, dj->index);
  1327. if (res == FR_OK) {
  1328. res = move_window(dj->fs, dj->sect);
  1329. if (res == FR_OK) {
  1330. *dj->dir = DDE; /* Mark the entry "deleted" */
  1331. dj->fs->wflag = 1;
  1332. }
  1333. }
  1334. #endif
  1335. return res;
  1336. }
  1337. #endif /* !_FS_READONLY */
  1338. /*-----------------------------------------------------------------------*/
  1339. /* Pick a segment and create the object name in directory form */
  1340. /*-----------------------------------------------------------------------*/
  1341. static
  1342. FRESULT create_name (
  1343. DIR *dj, /* Pointer to the directory object */
  1344. const TCHAR **path /* Pointer to pointer to the segment in the path string */
  1345. )
  1346. {
  1347. #ifdef _EXCVT
  1348. static const BYTE excvt[] = _EXCVT; /* Upper conversion table for extended chars */
  1349. #endif
  1350. #if _USE_LFN /* LFN configuration */
  1351. BYTE b, cf;
  1352. WCHAR w, *lfn;
  1353. UINT i, ni, si, di;
  1354. const TCHAR *p;
  1355. /* Create LFN in Unicode */
  1356. for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
  1357. lfn = dj->lfn;
  1358. si = di = 0;
  1359. for (;;) {
  1360. w = p[si++]; /* Get a character */
  1361. if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
  1362. if (di >= _MAX_LFN) /* Reject too long name */
  1363. return FR_INVALID_NAME;
  1364. #if !_LFN_UNICODE
  1365. w &= 0xFF;
  1366. if (IsDBCS1(w)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
  1367. b = (BYTE)p[si++]; /* Get 2nd byte */
  1368. if (!IsDBCS2(b))
  1369. return FR_INVALID_NAME; /* Reject invalid sequence */
  1370. w = (w << 8) + b; /* Create a DBC */
  1371. }
  1372. w = ff_convert(w, 1); /* Convert ANSI/OEM to Unicode */
  1373. if (!w) return FR_INVALID_NAME; /* Reject invalid code */
  1374. #endif
  1375. if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal chars for LFN */
  1376. return FR_INVALID_NAME;
  1377. lfn[di++] = w; /* Store the Unicode char */
  1378. }
  1379. *path = &p[si]; /* Return pointer to the next segment */
  1380. cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
  1381. #if _FS_RPATH
  1382. if ((di == 1 && lfn[di-1] == '.') || /* Is this a dot entry? */
  1383. (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {
  1384. lfn[di] = 0;
  1385. for (i = 0; i < 11; i++)
  1386. dj->fn[i] = (i < di) ? '.' : ' ';
  1387. dj->fn[i] = cf | NS_DOT; /* This is a dot entry */
  1388. return FR_OK;
  1389. }
  1390. #endif
  1391. while (di) { /* Strip trailing spaces and dots */
  1392. w = lfn[di-1];
  1393. if (w != ' ' && w != '.') break;
  1394. di--;
  1395. }
  1396. if (!di) return FR_INVALID_NAME; /* Reject nul string */
  1397. lfn[di] = 0; /* LFN is created */
  1398. /* Create SFN in directory form */
  1399. mem_set(dj->fn, ' ', 11);
  1400. for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
  1401. if (si) cf |= NS_LOSS | NS_LFN;
  1402. while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
  1403. b = i = 0; ni = 8;
  1404. for (;;) {
  1405. w = lfn[si++]; /* Get an LFN char */
  1406. if (!w) break; /* Break on end of the LFN */
  1407. if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
  1408. cf |= NS_LOSS | NS_LFN; continue;
  1409. }
  1410. if (i >= ni || si == di) { /* Extension or end of SFN */
  1411. if (ni == 11) { /* Long extension */
  1412. cf |= NS_LOSS | NS_LFN; break;
  1413. }
  1414. if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
  1415. if (si > di) break; /* No extension */
  1416. si = di; i = 8; ni = 11; /* Enter extension section */
  1417. b <<= 2; continue;
  1418. }
  1419. if (w >= 0x80) { /* Non ASCII char */
  1420. #ifdef _EXCVT
  1421. w = ff_convert(w, 0); /* Unicode -> OEM code */
  1422. if (w) w = excvt[w - 0x80]; /* Convert extended char to upper (SBCS) */
  1423. #else
  1424. w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
  1425. #endif
  1426. cf |= NS_LFN; /* Force create LFN entry */
  1427. }
  1428. if (_DF1S && w >= 0x100) { /* Double byte char (always false on SBCS cfg) */
  1429. if (i >= ni - 1) {
  1430. cf |= NS_LOSS | NS_LFN; i = ni; continue;
  1431. }
  1432. dj->fn[i++] = (BYTE)(w >> 8);
  1433. } else { /* Single byte char */
  1434. if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal chars for SFN */
  1435. w = '_'; cf |= NS_LOSS | NS_LFN;/* Lossy conversion */
  1436. } else {
  1437. if (IsUpper(w)) { /* ASCII large capital */
  1438. b |= 2;
  1439. } else {
  1440. if (IsLower(w)) { /* ASCII small capital */
  1441. b |= 1; w -= 0x20;
  1442. }
  1443. }
  1444. }
  1445. }
  1446. dj->fn[i++] = (BYTE)w;
  1447. }
  1448. if (dj->fn[0] == DDE) dj->fn[0] = NDDE; /* If the first char collides with deleted mark, replace it with 0x05 */
  1449. if (ni == 8) b <<= 2;
  1450. if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
  1451. cf |= NS_LFN;
  1452. if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended char, NT flags are created */
  1453. if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
  1454. if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
  1455. }
  1456. dj->fn[NS] = cf; /* SFN is created */
  1457. return FR_OK;
  1458. #else /* Non-LFN configuration */
  1459. BYTE b, c, d, *sfn;
  1460. UINT ni, si, i;
  1461. const char *p;
  1462. /* Create file name in directory form */
  1463. for (p = *path; *p == '/' || *p == '\\'; p++) ; /* Strip duplicated separator */
  1464. sfn = dj->fn;
  1465. mem_set(sfn, ' ', 11);
  1466. si = i = b = 0; ni = 8;
  1467. #if _FS_RPATH
  1468. if (p[si] == '.') { /* Is this a dot entry? */
  1469. for (;;) {
  1470. c = (BYTE)p[si++];
  1471. if (c != '.' || si >= 3) break;
  1472. sfn[i++] = c;
  1473. }
  1474. if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
  1475. *path = &p[si]; /* Return pointer to the next segment */
  1476. sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
  1477. return FR_OK;
  1478. }
  1479. #endif
  1480. for (;;) {
  1481. c = (BYTE)p[si++];
  1482. if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */
  1483. if (c == '.' || i >= ni) {
  1484. if (ni != 8 || c != '.') return FR_INVALID_NAME;
  1485. i = 8; ni = 11;
  1486. b <<= 2; continue;
  1487. }
  1488. if (c >= 0x80) { /* Extended char? */
  1489. b |= 3; /* Eliminate NT flag */
  1490. #ifdef _EXCVT
  1491. c = excvt[c-0x80]; /* Upper conversion (SBCS) */
  1492. #else
  1493. #if !_DF1S /* ASCII only cfg */
  1494. return FR_INVALID_NAME;
  1495. #endif
  1496. #endif
  1497. }
  1498. if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
  1499. d = (BYTE)p[si++]; /* Get 2nd byte */
  1500. if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */
  1501. return FR_INVALID_NAME;
  1502. sfn[i++] = c;
  1503. sfn[i++] = d;
  1504. } else { /* Single byte code */
  1505. if (chk_chr("\"*+,:;<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */
  1506. return FR_INVALID_NAME;
  1507. if (IsUpper(c)) { /* ASCII large capital? */
  1508. b |= 2;
  1509. } else {
  1510. if (IsLower(c)) { /* ASCII small capital? */
  1511. b |= 1; c -= 0x20;
  1512. }
  1513. }
  1514. sfn[i++] = c;
  1515. }
  1516. }
  1517. *path = &p[si]; /* Return pointer to the next segment */
  1518. c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
  1519. if (!i) return FR_INVALID_NAME; /* Reject nul string */
  1520. if (sfn[0] == DDE) sfn[0] = NDDE; /* When first char collides with DDE, replace it with 0x05 */
  1521. if (ni == 8) b <<= 2;
  1522. if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
  1523. if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
  1524. sfn[NS] = c; /* Store NT flag, File name is created */
  1525. return FR_OK;
  1526. #endif
  1527. }
  1528. /*-----------------------------------------------------------------------*/
  1529. /* Get file information from directory entry */
  1530. /*-----------------------------------------------------------------------*/
  1531. #if _FS_MINIMIZE <= 1
  1532. static
  1533. void get_fileinfo ( /* No return code */
  1534. DIR *dj, /* Pointer to the directory object */
  1535. FILINFO *fno /* Pointer to the file information to be filled */
  1536. )
  1537. {
  1538. UINT i;
  1539. BYTE nt, *dir;
  1540. TCHAR *p, c;
  1541. p = fno->fname;
  1542. if (dj->sect) {
  1543. dir = dj->dir;
  1544. nt = dir[DIR_NTres]; /* NT flag */
  1545. for (i = 0; i < 8; i++) { /* Copy name body */
  1546. c = dir[i];
  1547. if (c == ' ') break;
  1548. if (c == NDDE) c = (TCHAR)DDE;
  1549. if (_USE_LFN && (nt & NS_BODY) && IsUpper(c)) c += 0x20;
  1550. #if _LFN_UNICODE
  1551. if (IsDBCS1(c) && i < 7 && IsDBCS2(dir[i+1]))
  1552. c = (c << 8) | dir[++i];
  1553. c = ff_convert(c, 1);
  1554. if (!c) c = '?';
  1555. #endif
  1556. *p++ = c;
  1557. }
  1558. if (dir[8] != ' ') { /* Copy name extension */
  1559. *p++ = '.';
  1560. for (i = 8; i < 11; i++) {
  1561. c = dir[i];
  1562. if (c == ' ') break;
  1563. if (_USE_LFN && (nt & NS_EXT) && IsUpper(c)) c += 0x20;
  1564. #if _LFN_UNICODE
  1565. if (IsDBCS1(c) && i < 10 && IsDBCS2(dir[i+1]))
  1566. c = (c << 8) | dir[++i];
  1567. c = ff_convert(c, 1);
  1568. if (!c) c = '?';
  1569. #endif
  1570. *p++ = c;
  1571. }
  1572. }
  1573. fno->fattrib = dir[DIR_Attr]; /* Attribute */
  1574. fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
  1575. fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
  1576. fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
  1577. }
  1578. *p = 0; /* Terminate SFN str by a \0 */
  1579. #if _USE_LFN
  1580. if (fno->lfname && fno->lfsize) {
  1581. TCHAR *tp = fno->lfname;
  1582. WCHAR w, *lfn;
  1583. i = 0;
  1584. if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */
  1585. lfn = dj->lfn;
  1586. while ((w = *lfn++) != 0) { /* Get an LFN char */
  1587. #if !_LFN_UNICODE
  1588. w = ff_convert(w, 0); /* Unicode -> OEM conversion */
  1589. if (!w) { i = 0; break; } /* Could not convert, no LFN */
  1590. if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC (always false on SBCS cfg) */
  1591. tp[i++] = (TCHAR)(w >> 8);
  1592. #endif
  1593. if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */
  1594. tp[i++] = (TCHAR)w;
  1595. }
  1596. }
  1597. tp[i] = 0; /* Terminate the LFN str by a \0 */
  1598. }
  1599. #endif
  1600. }
  1601. #endif /* _FS_MINIMIZE <= 1 */
  1602. /*-----------------------------------------------------------------------*/
  1603. /* Follow a file path */
  1604. /*-----------------------------------------------------------------------*/
  1605. static
  1606. FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
  1607. DIR *dj, /* Directory object to return last directory and found object */
  1608. const TCHAR *path /* Full-path string to find a file or directory */
  1609. )
  1610. {
  1611. FRESULT res;
  1612. BYTE *dir, ns;
  1613. #if _FS_RPATH
  1614. if (*path == '/' || *path == '\\') { /* There is a heading separator */
  1615. path++; dj->sclust = 0; /* Strip it and start from the root dir */
  1616. } else { /* No heading separator */
  1617. dj->sclust = dj->fs->cdir; /* Start from the current dir */
  1618. }
  1619. #else
  1620. if (*path == '/' || *path == '\\') /* Strip heading separator if exist */
  1621. path++;
  1622. dj->sclust = 0; /* Start from the root dir */
  1623. #endif
  1624. if ((UINT)*path < ' ') { /* Nul path means the start directory itself */
  1625. res = dir_sdi(dj, 0);
  1626. dj->dir = 0;
  1627. } else { /* Follow path */
  1628. for (;;) {
  1629. res = create_name(dj, &path); /* Get a segment */
  1630. if (res != FR_OK) break;
  1631. res = dir_find(dj); /* Find it */
  1632. ns = *(dj->fn+NS);
  1633. if (res != FR_OK) { /* Failed to find the object */
  1634. if (res != FR_NO_FILE) break; /* Abort if any hard error occured */
  1635. /* Object not found */
  1636. if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exit */
  1637. dj->sclust = 0; dj->dir = 0; /* It is the root dir */
  1638. res = FR_OK;
  1639. if (!(ns & NS_LAST)) continue;
  1640. } else { /* Could not find the object */
  1641. if (!(ns & NS_LAST)) res = FR_NO_PATH;
  1642. }
  1643. break;
  1644. }
  1645. if (ns & NS_LAST) break; /* Last segment match. Function completed. */
  1646. dir = dj->dir; /* There is next segment. Follow the sub directory */
  1647. if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */
  1648. res = FR_NO_PATH; break;
  1649. }
  1650. dj->sclust = LD_CLUST(dir);
  1651. }
  1652. }
  1653. return res;
  1654. }
  1655. /*-----------------------------------------------------------------------*/
  1656. /* Load boot record and check if it is an FAT boot record */
  1657. /*-----------------------------------------------------------------------*/
  1658. static
  1659. BYTE check_fs ( /* 0:The FAT BR, 1:Valid BR but not an FAT, 2:Not a BR, 3:Disk error */
  1660. FATFS *fs, /* File system object */
  1661. DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
  1662. )
  1663. {
  1664. if (disk_read(fs->drv, fs->win, sect, 1) != RES_OK) /* Load boot record */
  1665. return 3;
  1666. if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */
  1667. return 2;
  1668. if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
  1669. return 0;
  1670. if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)
  1671. return 0;
  1672. return 1;
  1673. }
  1674. /*-----------------------------------------------------------------------*/
  1675. /* Check if the file system object is valid or not */
  1676. /*-----------------------------------------------------------------------*/
  1677. FRESULT chk_mounted ( /* FR_OK(0): successful, !=0: any error occurred */
  1678. const TCHAR **path, /* Pointer to pointer to the path name (drive number) */
  1679. FATFS **rfs, /* Pointer to pointer to the found file system object */
  1680. BYTE chk_wp /* !=0: Check media write protection for write access */
  1681. )
  1682. {
  1683. BYTE fmt, b, *tbl;
  1684. UINT vol;
  1685. DSTATUS stat;
  1686. DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
  1687. WORD nrsv;
  1688. const TCHAR *p = *path;
  1689. FATFS *fs;
  1690. /* Get logical drive number from the path name */
  1691. vol = p[0] - '0'; /* Is there a drive number? */
  1692. if (vol <= 9 && p[1] == ':') { /* Found a drive number, get and strip it */
  1693. p += 2; *path = p; /* Return pointer to the path name */
  1694. } else { /* No drive number is given */
  1695. #if _FS_RPATH
  1696. vol = CurrVol; /* Use current drive */
  1697. #else
  1698. vol = 0; /* Use drive 0 */
  1699. #endif
  1700. }
  1701. /* Check if the logical drive is valid or not */
  1702. if (vol >= _VOLUMES) /* Is the drive number valid? */
  1703. return FR_INVALID_DRIVE;
  1704. *rfs = fs = FatFs[vol]; /* Return pointer to the corresponding file system object */
  1705. if (!fs) return FR_NOT_ENABLED; /* Is the file system object available? */
  1706. ENTER_FF(fs); /* Lock file system */
  1707. if (fs->fs_type) { /* If the logical drive has been mounted */
  1708. stat = disk_status(fs->drv);
  1709. if (!(stat & STA_NOINIT)) { /* and the physical drive is kept initialized (has not been changed), */
  1710. #if !_FS_READONLY
  1711. if (chk_wp && (stat & STA_PROTECT)) /* Check write protection if needed */
  1712. return FR_WRITE_PROTECTED;
  1713. #endif
  1714. return FR_OK; /* The file system object is valid */
  1715. }
  1716. }
  1717. /* The logical drive must be mounted. */
  1718. /* Following code attempts to mount a volume. (analyze BPB and initialize the fs object) */
  1719. fs->fs_type = 0; /* Clear the file system object */
  1720. fs->drv = (BYTE)LD2PD(vol); /* Bind the logical drive and a physical drive */
  1721. stat = disk_initialize(fs->drv); /* Initialize low level disk I/O layer */
  1722. if (stat & STA_NOINIT) /* Check if the initialization succeeded */
  1723. return FR_NOT_READY; /* Failed to initialize due to no media or hard error */
  1724. #if _MAX_SS != 512 /* Get disk sector size (variable sector size cfg only) */
  1725. if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &fs->ssize) != RES_OK)
  1726. return FR_DISK_ERR;
  1727. #endif
  1728. #if !_FS_READONLY
  1729. if (chk_wp && (stat & STA_PROTECT)) /* Check disk write protection if needed */
  1730. return FR_WRITE_PROTECTED;
  1731. #endif
  1732. /* Search FAT partition on the drive. Supports only generic partitionings, FDISK and SFD. */
  1733. fmt = check_fs(fs, bsect = 0); /* Check sector 0 if it is a VBR */
  1734. if (fmt == 1) { /* Not an FAT-VBR, the disk may be partitioned */
  1735. /* Check the partition listed in top of the partition table */
  1736. tbl = &fs->win[MBR_Table + LD2PT(vol) * SZ_PTE];/* Partition table */
  1737. if (tbl[4]) { /* Is the partition existing? */
  1738. bsect = LD_DWORD(&tbl[8]); /* Partition offset in LBA */
  1739. fmt = check_fs(fs, bsect); /* Check the partition */
  1740. }
  1741. }
  1742. if (fmt == 3) return FR_DISK_ERR;
  1743. if (fmt) return FR_NO_FILESYSTEM; /* No FAT volume is found */
  1744. /* Following code initializes the file system object */
  1745. if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs)) /* (BPB_BytsPerSec must be equal to the physical sector size) */
  1746. return FR_NO_FILESYSTEM;
  1747. fasize = LD_WORD(fs->win+BPB_FATSz16); /* Number of sectors per FAT */
  1748. if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
  1749. fs->fsize = fasize;
  1750. fs->n_fats = b = fs->win[BPB_NumFATs]; /* Number of FAT copies */
  1751. if (b != 1 && b != 2) return FR_NO_FILESYSTEM; /* (Must be 1 or 2) */
  1752. fasize *= b; /* Number of sectors for FAT area */
  1753. fs->csize = b = fs->win[BPB_SecPerClus]; /* Number of sectors per cluster */
  1754. if (!b || (b & (b - 1))) return FR_NO_FILESYSTEM; /* (Must be power of 2) */
  1755. fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt); /* Number of root directory entries */
  1756. if (fs->n_rootdir % (SS(fs) / SZ_DIR)) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be sector aligned) */
  1757. tsect = LD_WORD(fs->win+BPB_TotSec16); /* Number of sectors on the volume */
  1758. if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
  1759. nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt); /* Number of reserved sectors */
  1760. if (!nrsv) return FR_NO_FILESYSTEM; /* (BPB_RsvdSecCnt must not be 0) */
  1761. /* Determine the FAT sub type */
  1762. sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR); /* RSV+FAT+DIR */
  1763. if (tsect < sysect) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
  1764. nclst = (tsect - sysect) / fs->csize; /* Number of clusters */
  1765. if (!nclst) return FR_NO_FILESYSTEM; /* (Invalid volume size) */
  1766. fmt = FS_FAT12;
  1767. if (nclst >= MIN_FAT16) fmt = FS_FAT16;
  1768. if (nclst >= MIN_FAT32) fmt = FS_FAT32;
  1769. /* Boundaries and Limits */
  1770. fs->n_fatent = nclst + 2; /* Number of FAT entries */
  1771. fs->database = bsect + sysect; /* Data start sector */
  1772. fs->fatbase = bsect + nrsv; /* FAT start sector */
  1773. if (fmt == FS_FAT32) {
  1774. if (fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must be 0) */
  1775. fs->dirbase = LD_DWORD(fs->win+BPB_RootClus); /* Root directory start cluster */
  1776. szbfat = fs->n_fatent * 4; /* (Required FAT size) */
  1777. } else {
  1778. if (!fs->n_rootdir) return FR_NO_FILESYSTEM; /* (BPB_RootEntCnt must not be 0) */
  1779. fs->dirbase = fs->fatbase + fasize; /* Root directory start sector */
  1780. szbfat = (fmt == FS_FAT16) ? /* (Required FAT size) */
  1781. fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
  1782. }
  1783. if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs)) /* (BPB_FATSz must not be less than required) */
  1784. return FR_NO_FILESYSTEM;
  1785. #if !_FS_READONLY
  1786. /* Initialize cluster allocation information */
  1787. fs->free_clust = 0xFFFFFFFF;
  1788. fs->last_clust = 0;
  1789. /* Get fsinfo if available */
  1790. if (fmt == FS_FAT32) {
  1791. fs->fsi_flag = 0;
  1792. fs->fsi_sector = bsect + LD_WORD(fs->win+BPB_FSInfo);
  1793. if (disk_read(fs->drv, fs->win, fs->fsi_sector, 1) == RES_OK &&
  1794. LD_WORD(fs->win+BS_55AA) == 0xAA55 &&
  1795. LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252 &&
  1796. LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272) {
  1797. fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
  1798. fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
  1799. }
  1800. }
  1801. #endif
  1802. fs->fs_type = fmt; /* FAT sub-type */
  1803. fs->id = ++Fsid; /* File system mount ID */
  1804. fs->winsect = 0; /* Invalidate sector cache */
  1805. fs->wflag = 0;
  1806. #if _FS_RPATH
  1807. fs->cdir = 0; /* Current directory (root dir) */
  1808. #endif
  1809. #if _FS_SHARE /* Clear file lock semaphores */
  1810. clear_lock(fs);
  1811. #endif
  1812. return FR_OK;
  1813. }
  1814. /*-----------------------------------------------------------------------*/
  1815. /* Check if the file/dir object is valid or not */
  1816. /*-----------------------------------------------------------------------*/
  1817. static
  1818. FRESULT validate ( /* FR_OK(0): The object is valid, !=0: Invalid */
  1819. FATFS *fs, /* Pointer to the file system object */
  1820. WORD id /* Member id of the target object to be checked */
  1821. )
  1822. {
  1823. if (!fs || !fs->fs_type || fs->id != id)
  1824. return FR_INVALID_OBJECT;
  1825. ENTER_FF(fs); /* Lock file system */
  1826. if (disk_status(fs->drv) & STA_NOINIT)
  1827. return FR_NOT_READY;
  1828. return FR_OK;
  1829. }
  1830. /*--------------------------------------------------------------------------
  1831. Public Functions
  1832. --------------------------------------------------------------------------*/
  1833. /*-----------------------------------------------------------------------*/
  1834. /* Mount/Unmount a Logical Drive */
  1835. /*-----------------------------------------------------------------------*/
  1836. FRESULT f_mount (
  1837. BYTE vol, /* Logical drive number to be mounted/unmounted */
  1838. FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
  1839. )
  1840. {
  1841. FATFS *rfs;
  1842. if (vol >= _VOLUMES) /* Check if the drive number is valid */
  1843. return FR_INVALID_DRIVE;
  1844. rfs = FatFs[vol]; /* Get current fs object */
  1845. if (rfs) {
  1846. #if _FS_SHARE
  1847. clear_lock(rfs);
  1848. #endif
  1849. #if _FS_REENTRANT /* Discard sync object of the current volume */
  1850. if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
  1851. #endif
  1852. rfs->fs_type = 0; /* Clear old fs object */
  1853. }
  1854. if (fs) {
  1855. fs->fs_type = 0; /* Clear new fs object */
  1856. #if _FS_REENTRANT /* Create sync object for the new volume */
  1857. if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
  1858. #endif
  1859. }
  1860. FatFs[vol] = fs; /* Register new fs object */
  1861. return FR_OK;
  1862. }
  1863. /*-----------------------------------------------------------------------*/
  1864. /* Open or Create a File */
  1865. /*-----------------------------------------------------------------------*/
  1866. FRESULT f_open (
  1867. FIL *fp, /* Pointer to the blank file object */
  1868. const TCHAR *path, /* Pointer to the file name */
  1869. BYTE mode /* Access mode and file open mode flags */
  1870. )
  1871. {
  1872. FRESULT res;
  1873. DIR dj;
  1874. BYTE *dir;
  1875. DEF_NAMEBUF;
  1876. fp->fs = 0; /* Clear file object */
  1877. #if !_FS_READONLY
  1878. mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
  1879. res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ));
  1880. #else
  1881. mode &= FA_READ;
  1882. res = chk_mounted(&path, &dj.fs, 0);
  1883. #endif
  1884. INIT_BUF(dj);
  1885. if (res == FR_OK)
  1886. res = follow_path(&dj, path); /* Follow the file path */
  1887. dir = dj.dir;
  1888. #if !_FS_READONLY /* R/W configuration */
  1889. if (res == FR_OK) {
  1890. if (!dir) /* Current dir itself */
  1891. res = FR_INVALID_NAME;
  1892. #if _FS_SHARE
  1893. else
  1894. res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
  1895. #endif
  1896. }
  1897. /* Create or Open a file */
  1898. if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
  1899. DWORD dw, cl;
  1900. if (res != FR_OK) { /* No file, create new */
  1901. if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
  1902. #if _FS_SHARE
  1903. res = enq_lock(dj.fs) ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
  1904. #else
  1905. res = dir_register(&dj);
  1906. #endif
  1907. mode |= FA_CREATE_ALWAYS; /* File is created */
  1908. dir = dj.dir; /* New entry */
  1909. }
  1910. else { /* Any object is already existing */
  1911. if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) { /* Cannot overwrite it (R/O or DIR) */
  1912. res = FR_DENIED;
  1913. } else {
  1914. if (mode & FA_CREATE_NEW) /* Cannot create as new file */
  1915. res = FR_EXIST;
  1916. }
  1917. }
  1918. if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) { /* Truncate it if overwrite mode */
  1919. dw = get_fattime(); /* Created time */
  1920. ST_DWORD(dir+DIR_CrtTime, dw);
  1921. dir[DIR_Attr] = 0; /* Reset attribute */
  1922. ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */
  1923. cl = LD_CLUST(dir); /* Get start cluster */
  1924. ST_CLUST(dir, 0); /* cluster = 0 */
  1925. dj.fs->wflag = 1;
  1926. if (cl) { /* Remove the cluster chain if exist */
  1927. dw = dj.fs->winsect;
  1928. res = remove_chain(dj.fs, cl);
  1929. if (res == FR_OK) {
  1930. dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
  1931. res = move_window(dj.fs, dw);
  1932. }
  1933. }
  1934. }
  1935. }
  1936. else { /* Open an existing file */
  1937. if (res == FR_OK) { /* Follow succeeded */
  1938. if (dir[DIR_Attr] & AM_DIR) { /* It is a directory */
  1939. res = FR_NO_FILE;
  1940. } else {
  1941. if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
  1942. res = FR_DENIED;
  1943. }
  1944. }
  1945. }
  1946. if (res == FR_OK) {
  1947. if (mode & FA_CREATE_ALWAYS) /* Set file change flag if created or overwritten */
  1948. mode |= FA__WRITTEN;
  1949. fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
  1950. fp->dir_ptr = dir;
  1951. #if _FS_SHARE
  1952. fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
  1953. if (!fp->lockid) res = FR_INT_ERR;
  1954. #endif
  1955. }
  1956. #else /* R/O configuration */
  1957. if (res == FR_OK) { /* Follow succeeded */
  1958. if (!dir) { /* Current dir itself */
  1959. res = FR_INVALID_NAME;
  1960. } else {
  1961. if (dir[DIR_Attr] & AM_DIR) /* It is a directory */
  1962. res = FR_NO_FILE;
  1963. }
  1964. }
  1965. #endif
  1966. FREE_BUF();
  1967. if (res == FR_OK) {
  1968. fp->flag = mode; /* File access mode */
  1969. fp->sclust = LD_CLUST(dir); /* File start cluster */
  1970. fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
  1971. fp->fptr = 0; /* File pointer */
  1972. fp->dsect = 0;
  1973. #if _USE_FASTSEEK
  1974. fp->cltbl = 0; /* Normal seek mode */
  1975. #endif
  1976. fp->fs = dj.fs; fp->id = dj.fs->id; /* Validate file object */
  1977. }
  1978. LEAVE_FF(dj.fs, res);
  1979. }
  1980. /*-----------------------------------------------------------------------*/
  1981. /* Read File */
  1982. /*-----------------------------------------------------------------------*/
  1983. FRESULT f_read (
  1984. FIL *fp, /* Pointer to the file object */
  1985. void *buff, /* Pointer to data buffer */
  1986. UINT btr, /* Number of bytes to read */
  1987. UINT *br /* Pointer to number of bytes read */
  1988. )
  1989. {
  1990. FRESULT res;
  1991. DWORD clst, sect, remain;
  1992. UINT rcnt, cc;
  1993. BYTE csect, *rbuff = buff;
  1994. *br = 0; /* Initialize byte counter */
  1995. res = validate(fp->fs, fp->id); /* Check validity */
  1996. if (res != FR_OK) LEAVE_FF(fp->fs, res);
  1997. if (fp->flag & FA__ERROR) /* Aborted file? */
  1998. LEAVE_FF(fp->fs, FR_INT_ERR);
  1999. if (!(fp->flag & FA_READ)) /* Check access mode */
  2000. LEAVE_FF(fp->fs, FR_DENIED);
  2001. remain = fp->fsize - fp->fptr;
  2002. if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
  2003. for ( ; btr; /* Repeat until all data read */
  2004. rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
  2005. if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
  2006. csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
  2007. if (!csect) { /* On the cluster boundary? */
  2008. if (fp->fptr == 0) { /* On the top of the file? */
  2009. clst = fp->sclust; /* Follow from the origin */
  2010. } else { /* Middle or end of the file */
  2011. #if _USE_FASTSEEK
  2012. if (fp->cltbl)
  2013. clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
  2014. else
  2015. #endif
  2016. clst = get_fat(fp->fs, fp->clust); /* Follow cluster chain on the FAT */
  2017. }
  2018. if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
  2019. if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  2020. fp->clust = clst; /* Update current cluster */
  2021. }
  2022. sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
  2023. if (!sect) ABORT(fp->fs, FR_INT_ERR);
  2024. sect += csect;
  2025. cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
  2026. if (cc) { /* Read maximum contiguous sectors directly */
  2027. if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
  2028. cc = fp->fs->csize - csect;
  2029. if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)
  2030. ABORT(fp->fs, FR_DISK_ERR);
  2031. #if !_FS_READONLY && _FS_MINIMIZE <= 2 /* Replace one of the read sectors with cached data if it contains a dirty sector */
  2032. #if _FS_TINY
  2033. if (fp->fs->wflag && fp->fs->winsect - sect < cc)
  2034. mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
  2035. #else
  2036. if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
  2037. mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
  2038. #endif
  2039. #endif
  2040. rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
  2041. continue;
  2042. }
  2043. #if !_FS_TINY
  2044. if (fp->dsect != sect) { /* Load data sector if not in cache */
  2045. #if !_FS_READONLY
  2046. if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
  2047. if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
  2048. ABORT(fp->fs, FR_DISK_ERR);
  2049. fp->flag &= ~FA__DIRTY;
  2050. }
  2051. #endif
  2052. if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK) /* Fill sector cache */
  2053. ABORT(fp->fs, FR_DISK_ERR);
  2054. }
  2055. #endif
  2056. fp->dsect = sect;
  2057. }
  2058. rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
  2059. if (rcnt > btr) rcnt = btr;
  2060. #if _FS_TINY
  2061. if (move_window(fp->fs, fp->dsect)) /* Move sector window */
  2062. ABORT(fp->fs, FR_DISK_ERR);
  2063. mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
  2064. #else
  2065. mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
  2066. #endif
  2067. }
  2068. LEAVE_FF(fp->fs, FR_OK);
  2069. }
  2070. #if !_FS_READONLY
  2071. /*-----------------------------------------------------------------------*/
  2072. /* Write File */
  2073. /*-----------------------------------------------------------------------*/
  2074. FRESULT f_write (
  2075. FIL *fp, /* Pointer to the file object */
  2076. const void *buff, /* Pointer to the data to be written */
  2077. UINT btw, /* Number of bytes to write */
  2078. UINT *bw /* Pointer to number of bytes written */
  2079. )
  2080. {
  2081. FRESULT res;
  2082. DWORD clst, sect;
  2083. UINT wcnt, cc;
  2084. const BYTE *wbuff = buff;
  2085. BYTE csect;
  2086. *bw = 0; /* Initialize byte counter */
  2087. res = validate(fp->fs, fp->id); /* Check validity */
  2088. if (res != FR_OK) LEAVE_FF(fp->fs, res);
  2089. if (fp->flag & FA__ERROR) /* Aborted file? */
  2090. LEAVE_FF(fp->fs, FR_INT_ERR);
  2091. if (!(fp->flag & FA_WRITE)) /* Check access mode */
  2092. LEAVE_FF(fp->fs, FR_DENIED);
  2093. if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0; /* File size cannot reach 4GB */
  2094. for ( ; btw; /* Repeat until all data written */
  2095. wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
  2096. if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
  2097. csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
  2098. if (!csect) { /* On the cluster boundary? */
  2099. if (fp->fptr == 0) { /* On the top of the file? */
  2100. clst = fp->sclust; /* Follow from the origin */
  2101. if (clst == 0) /* When no cluster is allocated, */
  2102. fp->sclust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
  2103. } else { /* Middle or end of the file */
  2104. #if _USE_FASTSEEK
  2105. if (fp->cltbl)
  2106. clst = clmt_clust(fp, fp->fptr); /* Get cluster# from the CLMT */
  2107. else
  2108. #endif
  2109. clst = create_chain(fp->fs, fp->clust); /* Follow or stretch cluster chain on the FAT */
  2110. }
  2111. if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
  2112. if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
  2113. if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  2114. fp->clust = clst; /* Update current cluster */
  2115. }
  2116. #if _FS_TINY
  2117. if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write-back sector cache */
  2118. ABORT(fp->fs, FR_DISK_ERR);
  2119. #else
  2120. if (fp->flag & FA__DIRTY) { /* Write-back sector cache */
  2121. if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
  2122. ABORT(fp->fs, FR_DISK_ERR);
  2123. fp->flag &= ~FA__DIRTY;
  2124. }
  2125. #endif
  2126. sect = clust2sect(fp->fs, fp->clust); /* Get current sector */
  2127. if (!sect) ABORT(fp->fs, FR_INT_ERR);
  2128. sect += csect;
  2129. cc = btw / SS(fp->fs); /* When remaining bytes >= sector size, */
  2130. if (cc) { /* Write maximum contiguous sectors directly */
  2131. if (csect + cc > fp->fs->csize) /* Clip at cluster boundary */
  2132. cc = fp->fs->csize - csect;
  2133. if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK)
  2134. ABORT(fp->fs, FR_DISK_ERR);
  2135. #if _FS_TINY
  2136. if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
  2137. mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
  2138. fp->fs->wflag = 0;
  2139. }
  2140. #else
  2141. if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
  2142. mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
  2143. fp->flag &= ~FA__DIRTY;
  2144. }
  2145. #endif
  2146. wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
  2147. continue;
  2148. }
  2149. #if _FS_TINY
  2150. if (fp->fptr >= fp->fsize) { /* Avoid silly cache filling at growing edge */
  2151. if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
  2152. fp->fs->winsect = sect;
  2153. }
  2154. #else
  2155. if (fp->dsect != sect) { /* Fill sector cache with file data */
  2156. if (fp->fptr < fp->fsize &&
  2157. disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
  2158. ABORT(fp->fs, FR_DISK_ERR);
  2159. }
  2160. #endif
  2161. fp->dsect = sect;
  2162. }
  2163. wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
  2164. if (wcnt > btw) wcnt = btw;
  2165. #if _FS_TINY
  2166. if (move_window(fp->fs, fp->dsect)) /* Move sector window */
  2167. ABORT(fp->fs, FR_DISK_ERR);
  2168. mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
  2169. fp->fs->wflag = 1;
  2170. #else
  2171. mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
  2172. fp->flag |= FA__DIRTY;
  2173. #endif
  2174. }
  2175. if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
  2176. fp->flag |= FA__WRITTEN; /* Set file change flag */
  2177. LEAVE_FF(fp->fs, FR_OK);
  2178. }
  2179. /*-----------------------------------------------------------------------*/
  2180. /* Synchronize the File Object */
  2181. /*-----------------------------------------------------------------------*/
  2182. FRESULT f_sync (
  2183. FIL *fp /* Pointer to the file object */
  2184. )
  2185. {
  2186. FRESULT res;
  2187. DWORD tim;
  2188. BYTE *dir;
  2189. res = validate(fp->fs, fp->id); /* Check validity of the object */
  2190. if (res == FR_OK) {
  2191. if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
  2192. #if !_FS_TINY /* Write-back dirty buffer */
  2193. if (fp->flag & FA__DIRTY) {
  2194. if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
  2195. LEAVE_FF(fp->fs, FR_DISK_ERR);
  2196. fp->flag &= ~FA__DIRTY;
  2197. }
  2198. #endif
  2199. /* Update the directory entry */
  2200. res = move_window(fp->fs, fp->dir_sect);
  2201. if (res == FR_OK) {
  2202. dir = fp->dir_ptr;
  2203. dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
  2204. ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */
  2205. ST_CLUST(dir, fp->sclust); /* Update start cluster */
  2206. tim = get_fattime(); /* Update updated time */
  2207. ST_DWORD(dir+DIR_WrtTime, tim);
  2208. fp->flag &= ~FA__WRITTEN;
  2209. fp->fs->wflag = 1;
  2210. res = sync(fp->fs);
  2211. }
  2212. }
  2213. }
  2214. LEAVE_FF(fp->fs, res);
  2215. }
  2216. #endif /* !_FS_READONLY */
  2217. /*-----------------------------------------------------------------------*/
  2218. /* Close File */
  2219. /*-----------------------------------------------------------------------*/
  2220. FRESULT f_close (
  2221. FIL *fp /* Pointer to the file object to be closed */
  2222. )
  2223. {
  2224. FRESULT res;
  2225. #if _FS_READONLY
  2226. FATFS *fs = fp->fs;
  2227. res = validate(fs, fp->id);
  2228. if (res == FR_OK) fp->fs = 0; /* Discard file object */
  2229. LEAVE_FF(fs, res);
  2230. #else
  2231. res = f_sync(fp); /* Flush cached data */
  2232. #if _FS_SHARE
  2233. if (res == FR_OK) { /* Decrement open counter */
  2234. #if _FS_REENTRANT
  2235. res = validate(fp->fs, fp->id);
  2236. if (res == FR_OK) {
  2237. res = dec_lock(fp->lockid);
  2238. unlock_fs(fp->fs, FR_OK);
  2239. }
  2240. #else
  2241. res = dec_lock(fp->lockid);
  2242. #endif
  2243. }
  2244. #endif
  2245. if (res == FR_OK) fp->fs = 0; /* Discard file object */
  2246. return res;
  2247. #endif
  2248. }
  2249. /*-----------------------------------------------------------------------*/
  2250. /* Current Drive/Directory Handlings */
  2251. /*-----------------------------------------------------------------------*/
  2252. #if _FS_RPATH >= 1
  2253. FRESULT f_chdrive (
  2254. BYTE drv /* Drive number */
  2255. )
  2256. {
  2257. if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
  2258. CurrVol = drv;
  2259. return FR_OK;
  2260. }
  2261. FRESULT f_chdir (
  2262. const TCHAR *path /* Pointer to the directory path */
  2263. )
  2264. {
  2265. FRESULT res;
  2266. DIR dj;
  2267. DEF_NAMEBUF;
  2268. res = chk_mounted(&path, &dj.fs, 0);
  2269. if (res == FR_OK) {
  2270. INIT_BUF(dj);
  2271. res = follow_path(&dj, path); /* Follow the path */
  2272. FREE_BUF();
  2273. if (res == FR_OK) { /* Follow completed */
  2274. if (!dj.dir) {
  2275. dj.fs->cdir = dj.sclust; /* Start directory itself */
  2276. } else {
  2277. if (dj.dir[DIR_Attr] & AM_DIR) /* Reached to the directory */
  2278. dj.fs->cdir = LD_CLUST(dj.dir);
  2279. else
  2280. res = FR_NO_PATH; /* Reached but a file */
  2281. }
  2282. }
  2283. if (res == FR_NO_FILE) res = FR_NO_PATH;
  2284. }
  2285. LEAVE_FF(dj.fs, res);
  2286. }
  2287. #if _FS_RPATH >= 2
  2288. FRESULT f_getcwd (
  2289. TCHAR *path, /* Pointer to the directory path */
  2290. UINT sz_path /* Size of path */
  2291. )
  2292. {
  2293. FRESULT res;
  2294. DIR dj;
  2295. UINT i, n;
  2296. DWORD ccl;
  2297. TCHAR *tp;
  2298. FILINFO fno;
  2299. DEF_NAMEBUF;
  2300. *path = 0;
  2301. res = chk_mounted((const TCHAR**)&path, &dj.fs, 0); /* Get current volume */
  2302. if (res == FR_OK) {
  2303. INIT_BUF(dj);
  2304. i = sz_path; /* Bottom of buffer (dir stack base) */
  2305. dj.sclust = dj.fs->cdir; /* Start to follow upper dir from current dir */
  2306. while ((ccl = dj.sclust) != 0) { /* Repeat while current dir is a sub-dir */
  2307. res = dir_sdi(&dj, 1); /* Get parent dir */
  2308. if (res != FR_OK) break;
  2309. res = dir_read(&dj);
  2310. if (res != FR_OK) break;
  2311. dj.sclust = LD_CLUST(dj.dir); /* Goto parent dir */
  2312. res = dir_sdi(&dj, 0);
  2313. if (res != FR_OK) break;
  2314. do { /* Find the entry links to the child dir */
  2315. res = dir_read(&dj);
  2316. if (res != FR_OK) break;
  2317. if (ccl == LD_CLUST(dj.dir)) break; /* Found the entry */
  2318. res = dir_next(&dj, 0);
  2319. } while (res == FR_OK);
  2320. if (res == FR_NO_FILE) res = FR_INT_ERR;/* It cannot be 'not found'. */
  2321. if (res != FR_OK) break;
  2322. #if _USE_LFN
  2323. fno.lfname = path;
  2324. fno.lfsize = i;
  2325. #endif
  2326. get_fileinfo(&dj, &fno); /* Get the dir name and push it to the buffer */
  2327. tp = fno.fname;
  2328. if (_USE_LFN && *path) tp = path;
  2329. for (n = 0; tp[n]; n++) ;
  2330. if (i < n + 3) {
  2331. res = FR_NOT_ENOUGH_CORE; break;
  2332. }
  2333. while (n) path[--i] = tp[--n];
  2334. path[--i] = '/';
  2335. }
  2336. tp = path;
  2337. if (res == FR_OK) {
  2338. *tp++ = '0' + CurrVol; /* Put drive number */
  2339. *tp++ = ':';
  2340. if (i == sz_path) { /* Root-dir */
  2341. *tp++ = '/';
  2342. } else { /* Sub-dir */
  2343. do /* Add stacked path str */
  2344. *tp++ = path[i++];
  2345. while (i < sz_path);
  2346. }
  2347. }
  2348. *tp = 0;
  2349. FREE_BUF();
  2350. }
  2351. LEAVE_FF(dj.fs, res);
  2352. }
  2353. #endif /* _FS_RPATH >= 2 */
  2354. #endif /* _FS_RPATH >= 1 */
  2355. #if _FS_MINIMIZE <= 2
  2356. /*-----------------------------------------------------------------------*/
  2357. /* Seek File R/W Pointer */
  2358. /*-----------------------------------------------------------------------*/
  2359. FRESULT f_lseek (
  2360. FIL *fp, /* Pointer to the file object */
  2361. DWORD ofs /* File pointer from top of file */
  2362. )
  2363. {
  2364. FRESULT res;
  2365. res = validate(fp->fs, fp->id); /* Check validity of the object */
  2366. if (res != FR_OK) LEAVE_FF(fp->fs, res);
  2367. if (fp->flag & FA__ERROR) /* Check abort flag */
  2368. LEAVE_FF(fp->fs, FR_INT_ERR);
  2369. #if _USE_FASTSEEK
  2370. if (fp->cltbl) { /* Fast seek */
  2371. DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
  2372. if (ofs == CREATE_LINKMAP) { /* Create CLMT */
  2373. tbl = fp->cltbl;
  2374. tlen = *tbl++; ulen = 2; /* Given table size and required table size */
  2375. cl = fp->sclust; /* Top of the chain */
  2376. if (cl) {
  2377. do {
  2378. /* Get a fragment */
  2379. tcl = cl; ncl = 0; ulen += 2; /* Top, length and used items */
  2380. do {
  2381. pcl = cl; ncl++;
  2382. cl = get_fat(fp->fs, cl);
  2383. if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
  2384. if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  2385. } while (cl == pcl + 1);
  2386. if (ulen <= tlen) { /* Store the length and top of the fragment */
  2387. *tbl++ = ncl; *tbl++ = tcl;
  2388. }
  2389. } while (cl < fp->fs->n_fatent); /* Repeat until end of chain */
  2390. }
  2391. *fp->cltbl = ulen; /* Number of items used */
  2392. if (ulen <= tlen)
  2393. *tbl = 0; /* Terminate table */
  2394. else
  2395. res = FR_NOT_ENOUGH_CORE; /* Given table size is smaller than required */
  2396. } else { /* Fast seek */
  2397. if (ofs > fp->fsize) /* Clip offset at the file size */
  2398. ofs = fp->fsize;
  2399. fp->fptr = ofs; /* Set file pointer */
  2400. if (ofs) {
  2401. fp->clust = clmt_clust(fp, ofs - 1);
  2402. dsc = clust2sect(fp->fs, fp->clust);
  2403. if (!dsc) ABORT(fp->fs, FR_INT_ERR);
  2404. dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
  2405. if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) { /* Refill sector cache if needed */
  2406. #if !_FS_TINY
  2407. #if !_FS_READONLY
  2408. if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
  2409. if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
  2410. ABORT(fp->fs, FR_DISK_ERR);
  2411. fp->flag &= ~FA__DIRTY;
  2412. }
  2413. #endif
  2414. if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK) /* Load current sector */
  2415. ABORT(fp->fs, FR_DISK_ERR);
  2416. #endif
  2417. fp->dsect = dsc;
  2418. }
  2419. }
  2420. }
  2421. } else
  2422. #endif
  2423. /* Normal Seek */
  2424. {
  2425. DWORD clst, bcs, nsect, ifptr;
  2426. if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
  2427. #if !_FS_READONLY
  2428. && !(fp->flag & FA_WRITE)
  2429. #endif
  2430. ) ofs = fp->fsize;
  2431. ifptr = fp->fptr;
  2432. fp->fptr = nsect = 0;
  2433. if (ofs) {
  2434. bcs = (DWORD)fp->fs->csize * SS(fp->fs); /* Cluster size (byte) */
  2435. if (ifptr > 0 &&
  2436. (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */
  2437. fp->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */
  2438. ofs -= fp->fptr;
  2439. clst = fp->clust;
  2440. } else { /* When seek to back cluster, */
  2441. clst = fp->sclust; /* start from the first cluster */
  2442. #if !_FS_READONLY
  2443. if (clst == 0) { /* If no cluster chain, create a new chain */
  2444. clst = create_chain(fp->fs, 0);
  2445. if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
  2446. if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  2447. fp->sclust = clst;
  2448. }
  2449. #endif
  2450. fp->clust = clst;
  2451. }
  2452. if (clst != 0) {
  2453. while (ofs > bcs) { /* Cluster following loop */
  2454. #if !_FS_READONLY
  2455. if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
  2456. clst = create_chain(fp->fs, clst); /* Force stretch if in write mode */
  2457. if (clst == 0) { /* When disk gets full, clip file size */
  2458. ofs = bcs; break;
  2459. }
  2460. } else
  2461. #endif
  2462. clst = get_fat(fp->fs, clst); /* Follow cluster chain if not in write mode */
  2463. if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  2464. if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
  2465. fp->clust = clst;
  2466. fp->fptr += bcs;
  2467. ofs -= bcs;
  2468. }
  2469. fp->fptr += ofs;
  2470. if (ofs % SS(fp->fs)) {
  2471. nsect = clust2sect(fp->fs, clst); /* Current sector */
  2472. if (!nsect) ABORT(fp->fs, FR_INT_ERR);
  2473. nsect += ofs / SS(fp->fs);
  2474. }
  2475. }
  2476. }
  2477. if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) { /* Fill sector cache if needed */
  2478. #if !_FS_TINY
  2479. #if !_FS_READONLY
  2480. if (fp->flag & FA__DIRTY) { /* Write-back dirty sector cache */
  2481. if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
  2482. ABORT(fp->fs, FR_DISK_ERR);
  2483. fp->flag &= ~FA__DIRTY;
  2484. }
  2485. #endif
  2486. if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK) /* Fill sector cache */
  2487. ABORT(fp->fs, FR_DISK_ERR);
  2488. #endif
  2489. fp->dsect = nsect;
  2490. }
  2491. #if !_FS_READONLY
  2492. if (fp->fptr > fp->fsize) { /* Set file change flag if the file size is extended */
  2493. fp->fsize = fp->fptr;
  2494. fp->flag |= FA__WRITTEN;
  2495. }
  2496. #endif
  2497. }
  2498. LEAVE_FF(fp->fs, res);
  2499. }
  2500. #if _FS_MINIMIZE <= 1
  2501. /*-----------------------------------------------------------------------*/
  2502. /* Create a Directroy Object */
  2503. /*-----------------------------------------------------------------------*/
  2504. FRESULT f_opendir (
  2505. DIR *dj, /* Pointer to directory object to create */
  2506. const TCHAR *path /* Pointer to the directory path */
  2507. )
  2508. {
  2509. FRESULT res;
  2510. DEF_NAMEBUF;
  2511. res = chk_mounted(&path, &dj->fs, 0);
  2512. if (res == FR_OK) {
  2513. INIT_BUF(*dj);
  2514. res = follow_path(dj, path); /* Follow the path to the directory */
  2515. FREE_BUF();
  2516. if (res == FR_OK) { /* Follow completed */
  2517. if (dj->dir) { /* It is not the root dir */
  2518. if (dj->dir[DIR_Attr] & AM_DIR) { /* The object is a directory */
  2519. dj->sclust = LD_CLUST(dj->dir);
  2520. } else { /* The object is not a directory */
  2521. res = FR_NO_PATH;
  2522. }
  2523. }
  2524. if (res == FR_OK) {
  2525. dj->id = dj->fs->id;
  2526. res = dir_sdi(dj, 0); /* Rewind dir */
  2527. }
  2528. }
  2529. if (res == FR_NO_FILE) res = FR_NO_PATH;
  2530. }
  2531. LEAVE_FF(dj->fs, res);
  2532. }
  2533. /*-----------------------------------------------------------------------*/
  2534. /* Read Directory Entry in Sequense */
  2535. /*-----------------------------------------------------------------------*/
  2536. FRESULT f_readdir (
  2537. DIR *dj, /* Pointer to the open directory object */
  2538. FILINFO *fno /* Pointer to file information to return */
  2539. )
  2540. {
  2541. FRESULT res;
  2542. DEF_NAMEBUF;
  2543. res = validate(dj->fs, dj->id); /* Check validity of the object */
  2544. if (res == FR_OK) {
  2545. if (!fno) {
  2546. res = dir_sdi(dj, 0); /* Rewind the directory object */
  2547. } else {
  2548. INIT_BUF(*dj);
  2549. res = dir_read(dj); /* Read an directory item */
  2550. if (res == FR_NO_FILE) { /* Reached end of dir */
  2551. dj->sect = 0;
  2552. res = FR_OK;
  2553. }
  2554. if (res == FR_OK) { /* A valid entry is found */
  2555. get_fileinfo(dj, fno); /* Get the object information */
  2556. res = dir_next(dj, 0); /* Increment index for next */
  2557. if (res == FR_NO_FILE) {
  2558. dj->sect = 0;
  2559. res = FR_OK;
  2560. }
  2561. }
  2562. FREE_BUF();
  2563. }
  2564. }
  2565. LEAVE_FF(dj->fs, res);
  2566. }
  2567. FRESULT f_seekdir(
  2568. DIR *dj, /* Pointer to the open directory object */
  2569. int offset /* the seek offset */
  2570. )
  2571. {
  2572. int i = 0;
  2573. if (dir_sdi(dj, 0) != FR_OK || offset < 0)
  2574. return FR_INT_ERR;
  2575. while(i < offset)
  2576. {
  2577. if(dir_read(dj) != FR_OK || dir_next(dj, 0) != FR_OK)
  2578. return FR_INT_ERR;
  2579. i++;
  2580. }
  2581. return FR_OK;
  2582. }
  2583. #if _FS_MINIMIZE == 0
  2584. /*-----------------------------------------------------------------------*/
  2585. /* Get File Status */
  2586. /*-----------------------------------------------------------------------*/
  2587. FRESULT f_stat (
  2588. const TCHAR *path, /* Pointer to the file path */
  2589. FILINFO *fno /* Pointer to file information to return */
  2590. )
  2591. {
  2592. FRESULT res;
  2593. DIR dj;
  2594. DEF_NAMEBUF;
  2595. res = chk_mounted(&path, &dj.fs, 0);
  2596. if (res == FR_OK) {
  2597. INIT_BUF(dj);
  2598. res = follow_path(&dj, path); /* Follow the file path */
  2599. if (res == FR_OK) { /* Follow completed */
  2600. if (dj.dir) /* Found an object */
  2601. get_fileinfo(&dj, fno);
  2602. else /* It is root dir */
  2603. res = FR_INVALID_NAME;
  2604. }
  2605. FREE_BUF();
  2606. }
  2607. LEAVE_FF(dj.fs, res);
  2608. }
  2609. #if !_FS_READONLY
  2610. /*-----------------------------------------------------------------------*/
  2611. /* Get Number of Free Clusters */
  2612. /*-----------------------------------------------------------------------*/
  2613. FRESULT f_getfree (
  2614. const TCHAR *path, /* Pointer to the logical drive number (root dir) */
  2615. DWORD *nclst, /* Pointer to the variable to return number of free clusters */
  2616. FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
  2617. )
  2618. {
  2619. FRESULT res;
  2620. DWORD n, clst, sect, stat;
  2621. UINT i;
  2622. BYTE fat, *p;
  2623. /* Get drive number */
  2624. res = chk_mounted(&path, fatfs, 0);
  2625. if (res == FR_OK) {
  2626. /* If free_clust is valid, return it without full cluster scan */
  2627. if ((*fatfs)->free_clust <= (*fatfs)->n_fatent - 2) {
  2628. *nclst = (*fatfs)->free_clust;
  2629. } else {
  2630. /* Get number of free clusters */
  2631. fat = (*fatfs)->fs_type;
  2632. n = 0;
  2633. if (fat == FS_FAT12) {
  2634. clst = 2;
  2635. do {
  2636. stat = get_fat(*fatfs, clst);
  2637. if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
  2638. if (stat == 1) { res = FR_INT_ERR; break; }
  2639. if (stat == 0) n++;
  2640. } while (++clst < (*fatfs)->n_fatent);
  2641. } else {
  2642. clst = (*fatfs)->n_fatent;
  2643. sect = (*fatfs)->fatbase;
  2644. i = 0; p = 0;
  2645. do {
  2646. if (!i) {
  2647. res = move_window(*fatfs, sect++);
  2648. if (res != FR_OK) break;
  2649. p = (*fatfs)->win;
  2650. i = SS(*fatfs);
  2651. }
  2652. if (fat == FS_FAT16) {
  2653. if (LD_WORD(p) == 0) n++;
  2654. p += 2; i -= 2;
  2655. } else {
  2656. if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
  2657. p += 4; i -= 4;
  2658. }
  2659. } while (--clst);
  2660. }
  2661. (*fatfs)->free_clust = n;
  2662. if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;
  2663. *nclst = n;
  2664. }
  2665. }
  2666. LEAVE_FF(*fatfs, res);
  2667. }
  2668. /*-----------------------------------------------------------------------*/
  2669. /* Truncate File */
  2670. /*-----------------------------------------------------------------------*/
  2671. FRESULT f_truncate (
  2672. FIL *fp /* Pointer to the file object */
  2673. )
  2674. {
  2675. FRESULT res;
  2676. DWORD ncl;
  2677. res = validate(fp->fs, fp->id); /* Check validity of the object */
  2678. if (res == FR_OK) {
  2679. if (fp->flag & FA__ERROR) { /* Check abort flag */
  2680. res = FR_INT_ERR;
  2681. } else {
  2682. if (!(fp->flag & FA_WRITE)) /* Check access mode */
  2683. res = FR_DENIED;
  2684. }
  2685. }
  2686. if (res == FR_OK) {
  2687. if (fp->fsize > fp->fptr) {
  2688. fp->fsize = fp->fptr; /* Set file size to current R/W point */
  2689. fp->flag |= FA__WRITTEN;
  2690. if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
  2691. res = remove_chain(fp->fs, fp->sclust);
  2692. fp->sclust = 0;
  2693. } else { /* When truncate a part of the file, remove remaining clusters */
  2694. ncl = get_fat(fp->fs, fp->clust);
  2695. res = FR_OK;
  2696. if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
  2697. if (ncl == 1) res = FR_INT_ERR;
  2698. if (res == FR_OK && ncl < fp->fs->n_fatent) {
  2699. res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
  2700. if (res == FR_OK) res = remove_chain(fp->fs, ncl);
  2701. }
  2702. }
  2703. }
  2704. if (res != FR_OK) fp->flag |= FA__ERROR;
  2705. }
  2706. LEAVE_FF(fp->fs, res);
  2707. }
  2708. /*-----------------------------------------------------------------------*/
  2709. /* Delete a File or Directory */
  2710. /*-----------------------------------------------------------------------*/
  2711. FRESULT f_unlink (
  2712. const TCHAR *path /* Pointer to the file or directory path */
  2713. )
  2714. {
  2715. FRESULT res;
  2716. DIR dj, sdj;
  2717. BYTE *dir;
  2718. DWORD dclst;
  2719. DEF_NAMEBUF;
  2720. res = chk_mounted(&path, &dj.fs, 1);
  2721. if (res == FR_OK) {
  2722. INIT_BUF(dj);
  2723. res = follow_path(&dj, path); /* Follow the file path */
  2724. if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
  2725. res = FR_INVALID_NAME; /* Cannot remove dot entry */
  2726. #if _FS_SHARE
  2727. if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open file */
  2728. #endif
  2729. if (res == FR_OK) { /* The object is accessible */
  2730. dir = dj.dir;
  2731. if (!dir) {
  2732. res = FR_INVALID_NAME; /* Cannot remove the start directory */
  2733. } else {
  2734. if (dir[DIR_Attr] & AM_RDO)
  2735. res = FR_DENIED; /* Cannot remove R/O object */
  2736. }
  2737. dclst = LD_CLUST(dir);
  2738. if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) { /* Is it a sub-dir? */
  2739. if (dclst < 2) {
  2740. res = FR_INT_ERR;
  2741. } else {
  2742. mem_cpy(&sdj, &dj, sizeof(DIR)); /* Check if the sub-dir is empty or not */
  2743. sdj.sclust = dclst;
  2744. res = dir_sdi(&sdj, 2); /* Exclude dot entries */
  2745. if (res == FR_OK) {
  2746. res = dir_read(&sdj);
  2747. if (res == FR_OK /* Not empty dir */
  2748. #if _FS_RPATH
  2749. || dclst == sdj.fs->cdir /* Current dir */
  2750. #endif
  2751. ) res = FR_DENIED;
  2752. if (res == FR_NO_FILE) res = FR_OK; /* Empty */
  2753. }
  2754. }
  2755. }
  2756. if (res == FR_OK) {
  2757. res = dir_remove(&dj); /* Remove the directory entry */
  2758. if (res == FR_OK) {
  2759. if (dclst) /* Remove the cluster chain if exist */
  2760. res = remove_chain(dj.fs, dclst);
  2761. if (res == FR_OK) res = sync(dj.fs);
  2762. }
  2763. }
  2764. }
  2765. FREE_BUF();
  2766. }
  2767. LEAVE_FF(dj.fs, res);
  2768. }
  2769. /*-----------------------------------------------------------------------*/
  2770. /* Create a Directory */
  2771. /*-----------------------------------------------------------------------*/
  2772. FRESULT f_mkdir (
  2773. const TCHAR *path /* Pointer to the directory path */
  2774. )
  2775. {
  2776. FRESULT res;
  2777. DIR dj;
  2778. BYTE *dir, n;
  2779. DWORD dsc, dcl, pcl, tim = get_fattime();
  2780. DEF_NAMEBUF;
  2781. res = chk_mounted(&path, &dj.fs, 1);
  2782. if (res == FR_OK) {
  2783. INIT_BUF(dj);
  2784. res = follow_path(&dj, path); /* Follow the file path */
  2785. if (res == FR_OK) res = FR_EXIST; /* Any object with same name is already existing */
  2786. if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
  2787. res = FR_INVALID_NAME;
  2788. if (res == FR_NO_FILE) { /* Can create a new directory */
  2789. dcl = create_chain(dj.fs, 0); /* Allocate a cluster for the new directory table */
  2790. res = FR_OK;
  2791. if (dcl == 0) res = FR_DENIED; /* No space to allocate a new cluster */
  2792. if (dcl == 1) res = FR_INT_ERR;
  2793. if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
  2794. if (res == FR_OK) /* Flush FAT */
  2795. res = move_window(dj.fs, 0);
  2796. if (res == FR_OK) { /* Initialize the new directory table */
  2797. dsc = clust2sect(dj.fs, dcl);
  2798. dir = dj.fs->win;
  2799. mem_set(dir, 0, SS(dj.fs));
  2800. mem_set(dir+DIR_Name, ' ', 8+3); /* Create "." entry */
  2801. dir[DIR_Name] = '.';
  2802. dir[DIR_Attr] = AM_DIR;
  2803. ST_DWORD(dir+DIR_WrtTime, tim);
  2804. ST_CLUST(dir, dcl);
  2805. mem_cpy(dir+SZ_DIR, dir, SZ_DIR); /* Create ".." entry */
  2806. dir[33] = '.'; pcl = dj.sclust;
  2807. if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
  2808. pcl = 0;
  2809. ST_CLUST(dir+SZ_DIR, pcl);
  2810. for (n = dj.fs->csize; n; n--) { /* Write dot entries and clear following sectors */
  2811. dj.fs->winsect = dsc++;
  2812. dj.fs->wflag = 1;
  2813. res = move_window(dj.fs, 0);
  2814. if (res != FR_OK) break;
  2815. mem_set(dir, 0, SS(dj.fs));
  2816. }
  2817. }
  2818. if (res == FR_OK) res = dir_register(&dj); /* Register the object to the directoy */
  2819. if (res != FR_OK) {
  2820. remove_chain(dj.fs, dcl); /* Could not register, remove cluster chain */
  2821. } else {
  2822. dir = dj.dir;
  2823. dir[DIR_Attr] = AM_DIR; /* Attribute */
  2824. ST_DWORD(dir+DIR_WrtTime, tim); /* Created time */
  2825. ST_CLUST(dir, dcl); /* Table start cluster */
  2826. dj.fs->wflag = 1;
  2827. res = sync(dj.fs);
  2828. }
  2829. }
  2830. FREE_BUF();
  2831. }
  2832. LEAVE_FF(dj.fs, res);
  2833. }
  2834. /*-----------------------------------------------------------------------*/
  2835. /* Change Attribute */
  2836. /*-----------------------------------------------------------------------*/
  2837. FRESULT f_chmod (
  2838. const TCHAR *path, /* Pointer to the file path */
  2839. BYTE value, /* Attribute bits */
  2840. BYTE mask /* Attribute mask to change */
  2841. )
  2842. {
  2843. FRESULT res;
  2844. DIR dj;
  2845. BYTE *dir;
  2846. DEF_NAMEBUF;
  2847. res = chk_mounted(&path, &dj.fs, 1);
  2848. if (res == FR_OK) {
  2849. INIT_BUF(dj);
  2850. res = follow_path(&dj, path); /* Follow the file path */
  2851. FREE_BUF();
  2852. if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
  2853. res = FR_INVALID_NAME;
  2854. if (res == FR_OK) {
  2855. dir = dj.dir;
  2856. if (!dir) { /* Is it a root directory? */
  2857. res = FR_INVALID_NAME;
  2858. } else { /* File or sub directory */
  2859. mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
  2860. dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
  2861. dj.fs->wflag = 1;
  2862. res = sync(dj.fs);
  2863. }
  2864. }
  2865. }
  2866. LEAVE_FF(dj.fs, res);
  2867. }
  2868. /*-----------------------------------------------------------------------*/
  2869. /* Change Timestamp */
  2870. /*-----------------------------------------------------------------------*/
  2871. FRESULT f_utime (
  2872. const TCHAR *path, /* Pointer to the file/directory name */
  2873. const FILINFO *fno /* Pointer to the time stamp to be set */
  2874. )
  2875. {
  2876. FRESULT res;
  2877. DIR dj;
  2878. BYTE *dir;
  2879. DEF_NAMEBUF;
  2880. res = chk_mounted(&path, &dj.fs, 1);
  2881. if (res == FR_OK) {
  2882. INIT_BUF(dj);
  2883. res = follow_path(&dj, path); /* Follow the file path */
  2884. FREE_BUF();
  2885. if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
  2886. res = FR_INVALID_NAME;
  2887. if (res == FR_OK) {
  2888. dir = dj.dir;
  2889. if (!dir) { /* Root directory */
  2890. res = FR_INVALID_NAME;
  2891. } else { /* File or sub-directory */
  2892. ST_WORD(dir+DIR_WrtTime, fno->ftime);
  2893. ST_WORD(dir+DIR_WrtDate, fno->fdate);
  2894. dj.fs->wflag = 1;
  2895. res = sync(dj.fs);
  2896. }
  2897. }
  2898. }
  2899. LEAVE_FF(dj.fs, res);
  2900. }
  2901. /*-----------------------------------------------------------------------*/
  2902. /* Rename File/Directory */
  2903. /*-----------------------------------------------------------------------*/
  2904. FRESULT f_rename (
  2905. const TCHAR *path_old, /* Pointer to the old name */
  2906. const TCHAR *path_new /* Pointer to the new name */
  2907. )
  2908. {
  2909. FRESULT res;
  2910. DIR djo, djn;
  2911. BYTE buf[21], *dir;
  2912. DWORD dw;
  2913. DEF_NAMEBUF;
  2914. res = chk_mounted(&path_old, &djo.fs, 1);
  2915. if (res == FR_OK) {
  2916. djn.fs = djo.fs;
  2917. INIT_BUF(djo);
  2918. res = follow_path(&djo, path_old); /* Check old object */
  2919. if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))
  2920. res = FR_INVALID_NAME;
  2921. #if _FS_SHARE
  2922. if (res == FR_OK) res = chk_lock(&djo, 2);
  2923. #endif
  2924. if (res == FR_OK) { /* Old object is found */
  2925. if (!djo.dir) { /* Is root dir? */
  2926. res = FR_NO_FILE;
  2927. } else {
  2928. mem_cpy(buf, djo.dir+DIR_Attr, 21); /* Save the object information except for name */
  2929. mem_cpy(&djn, &djo, sizeof(DIR)); /* Check new object */
  2930. res = follow_path(&djn, path_new);
  2931. if (res == FR_OK) res = FR_EXIST; /* The new object name is already existing */
  2932. if (res == FR_NO_FILE) { /* Is it a valid path and no name collision? */
  2933. /* Start critical section that any interruption or error can cause cross-link */
  2934. res = dir_register(&djn); /* Register the new entry */
  2935. if (res == FR_OK) {
  2936. dir = djn.dir; /* Copy object information except for name */
  2937. mem_cpy(dir+13, buf+2, 19);
  2938. dir[DIR_Attr] = buf[0] | AM_ARC;
  2939. djo.fs->wflag = 1;
  2940. if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) { /* Update .. entry in the directory if needed */
  2941. dw = clust2sect(djn.fs, LD_CLUST(dir));
  2942. if (!dw) {
  2943. res = FR_INT_ERR;
  2944. } else {
  2945. res = move_window(djn.fs, dw);
  2946. dir = djn.fs->win+SZ_DIR; /* .. entry */
  2947. if (res == FR_OK && dir[1] == '.') {
  2948. dw = (djn.fs->fs_type == FS_FAT32 && djn.sclust == djn.fs->dirbase) ? 0 : djn.sclust;
  2949. ST_CLUST(dir, dw);
  2950. djn.fs->wflag = 1;
  2951. }
  2952. }
  2953. }
  2954. if (res == FR_OK) {
  2955. res = dir_remove(&djo); /* Remove old entry */
  2956. if (res == FR_OK)
  2957. res = sync(djo.fs);
  2958. }
  2959. }
  2960. /* End critical section */
  2961. }
  2962. }
  2963. }
  2964. FREE_BUF();
  2965. }
  2966. LEAVE_FF(djo.fs, res);
  2967. }
  2968. #endif /* !_FS_READONLY */
  2969. #endif /* _FS_MINIMIZE == 0 */
  2970. #endif /* _FS_MINIMIZE <= 1 */
  2971. #endif /* _FS_MINIMIZE <= 2 */
  2972. /*-----------------------------------------------------------------------*/
  2973. /* Forward data to the stream directly (available on only tiny cfg) */
  2974. /*-----------------------------------------------------------------------*/
  2975. #if _USE_FORWARD && _FS_TINY
  2976. FRESULT f_forward (
  2977. FIL *fp, /* Pointer to the file object */
  2978. UINT (*func)(const BYTE*,UINT), /* Pointer to the streaming function */
  2979. UINT btr, /* Number of bytes to forward */
  2980. UINT *bf /* Pointer to number of bytes forwarded */
  2981. )
  2982. {
  2983. FRESULT res;
  2984. DWORD remain, clst, sect;
  2985. UINT rcnt;
  2986. BYTE csect;
  2987. *bf = 0; /* Initialize byte counter */
  2988. res = validate(fp->fs, fp->id); /* Check validity of the object */
  2989. if (res != FR_OK) LEAVE_FF(fp->fs, res);
  2990. if (fp->flag & FA__ERROR) /* Check error flag */
  2991. LEAVE_FF(fp->fs, FR_INT_ERR);
  2992. if (!(fp->flag & FA_READ)) /* Check access mode */
  2993. LEAVE_FF(fp->fs, FR_DENIED);
  2994. remain = fp->fsize - fp->fptr;
  2995. if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
  2996. for ( ; btr && (*func)(0, 0); /* Repeat until all data transferred or stream becomes busy */
  2997. fp->fptr += rcnt, *bf += rcnt, btr -= rcnt) {
  2998. csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1)); /* Sector offset in the cluster */
  2999. if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
  3000. if (!csect) { /* On the cluster boundary? */
  3001. clst = (fp->fptr == 0) ? /* On the top of the file? */
  3002. fp->sclust : get_fat(fp->fs, fp->clust);
  3003. if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
  3004. if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
  3005. fp->clust = clst; /* Update current cluster */
  3006. }
  3007. }
  3008. sect = clust2sect(fp->fs, fp->clust); /* Get current data sector */
  3009. if (!sect) ABORT(fp->fs, FR_INT_ERR);
  3010. sect += csect;
  3011. if (move_window(fp->fs, sect)) /* Move sector window */
  3012. ABORT(fp->fs, FR_DISK_ERR);
  3013. fp->dsect = sect;
  3014. rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs)); /* Forward data from sector window */
  3015. if (rcnt > btr) rcnt = btr;
  3016. rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
  3017. if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
  3018. }
  3019. LEAVE_FF(fp->fs, FR_OK);
  3020. }
  3021. #endif /* _USE_FORWARD */
  3022. #if _USE_MKFS && !_FS_READONLY
  3023. /*-----------------------------------------------------------------------*/
  3024. /* Create File System on the Drive */
  3025. /*-----------------------------------------------------------------------*/
  3026. #define N_ROOTDIR 512 /* Number of root dir entries for FAT12/16 */
  3027. #define N_FATS 1 /* Number of FAT copies (1 or 2) */
  3028. FRESULT f_mkfs (
  3029. BYTE drv, /* Logical drive number */
  3030. BYTE sfd, /* Partitioning rule 0:FDISK, 1:SFD */
  3031. UINT au /* Allocation unit size [bytes] */
  3032. )
  3033. {
  3034. static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
  3035. static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
  3036. BYTE fmt, md, *tbl;
  3037. DWORD n_clst, vs, n, wsect;
  3038. UINT i;
  3039. DWORD b_vol, b_fat, b_dir, b_data; /* Offset (LBA) */
  3040. DWORD n_vol, n_rsv, n_fat, n_dir; /* Size */
  3041. FATFS *fs;
  3042. DSTATUS stat;
  3043. /* Check mounted drive and clear work area */
  3044. if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
  3045. fs = FatFs[drv];
  3046. if (!fs) return FR_NOT_ENABLED;
  3047. fs->fs_type = 0;
  3048. drv = LD2PD(drv);
  3049. /* Get disk statics */
  3050. stat = disk_initialize(drv);
  3051. if (stat & STA_NOINIT) return FR_NOT_READY;
  3052. if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
  3053. #if _MAX_SS != 512 /* Get disk sector size */
  3054. if (disk_ioctl(drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK)
  3055. return FR_DISK_ERR;
  3056. #endif
  3057. if (disk_ioctl(drv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
  3058. return FR_DISK_ERR;
  3059. b_vol = (sfd) ? 0 : 63; /* Volume start sector */
  3060. n_vol -= b_vol;
  3061. if (au & (au - 1)) au = 0; /* Check validity of the AU size */
  3062. if (!au) { /* AU auto selection */
  3063. vs = n_vol / (2000 / (SS(fs) / 512));
  3064. for (i = 0; vs < vst[i]; i++) ;
  3065. au = cst[i];
  3066. }
  3067. au /= SS(fs); /* Number of sectors per cluster */
  3068. if (au == 0) au = 1;
  3069. if (au > 128) au = 128;
  3070. /* Pre-compute number of clusters and FAT syb-type */
  3071. n_clst = n_vol / au;
  3072. fmt = FS_FAT12;
  3073. if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
  3074. if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
  3075. /* Determine offset and size of FAT structure */
  3076. if (fmt == FS_FAT32) {
  3077. n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
  3078. n_rsv = 32;
  3079. n_dir = 0;
  3080. } else {
  3081. n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
  3082. n_fat = (n_fat + SS(fs) - 1) / SS(fs);
  3083. n_rsv = 1;
  3084. n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
  3085. }
  3086. b_fat = b_vol + n_rsv; /* FAT area start sector */
  3087. b_dir = b_fat + n_fat * N_FATS; /* Directory area start sector */
  3088. b_data = b_dir + n_dir; /* Data area start sector */
  3089. if (n_vol < b_data + au) return FR_MKFS_ABORTED; /* Too small volume */
  3090. /* Align data start sector to erase block boundary (for flash memory media) */
  3091. if (disk_ioctl(drv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
  3092. n = (b_data + n - 1) & ~(n - 1); /* Next nearest erase block from current data start */
  3093. n = (n - b_data) / N_FATS;
  3094. if (fmt == FS_FAT32) { /* FAT32: Move FAT offset */
  3095. n_rsv += n;
  3096. b_fat += n;
  3097. } else { /* FAT12/16: Expand FAT size */
  3098. n_fat += n;
  3099. }
  3100. /* Determine number of clusters and final check of validity of the FAT sub-type */
  3101. n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
  3102. if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
  3103. || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
  3104. return FR_MKFS_ABORTED;
  3105. /* Create partition table if required */
  3106. if (sfd) { /* No patition table (SFD) */
  3107. md = 0xF0;
  3108. } else { /* With patition table (FDISK) */
  3109. DWORD n_disk = b_vol + n_vol;
  3110. mem_set(fs->win, 0, SS(fs));
  3111. tbl = fs->win+MBR_Table;
  3112. ST_DWORD(tbl, 0x00010180); /* Partition start in CHS */
  3113. if (n_disk < 63UL * 255 * 1024) { /* Partition end in CHS */
  3114. n_disk = n_disk / 63 / 255;
  3115. tbl[7] = (BYTE)n_disk;
  3116. tbl[6] = (BYTE)((n_disk >> 2) | 63);
  3117. } else {
  3118. ST_WORD(&tbl[6], 0xFFFF); /* CHS saturated */
  3119. }
  3120. tbl[5] = 254;
  3121. if (fmt != FS_FAT32) /* System ID */
  3122. tbl[4] = (n_vol < 0x10000) ? 0x04 : 0x06;
  3123. else
  3124. tbl[4] = 0x0c;
  3125. ST_DWORD(tbl+8, 63); /* Partition start in LBA */
  3126. ST_DWORD(tbl+12, n_vol); /* Partition size in LBA */
  3127. ST_WORD(fs->win+BS_55AA, 0xAA55); /* MBR signature */
  3128. if (disk_write(drv, fs->win, 0, 1) != RES_OK) /* Put the MBR into first physical sector */
  3129. return FR_DISK_ERR;
  3130. md = 0xF8;
  3131. }
  3132. /* Create volume boot record */
  3133. tbl = fs->win; /* Clear sector */
  3134. mem_set(tbl, 0, SS(fs));
  3135. mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */
  3136. i = SS(fs); /* Sector size */
  3137. ST_WORD(tbl+BPB_BytsPerSec, i);
  3138. tbl[BPB_SecPerClus] = (BYTE)au; /* Sectors per cluster */
  3139. ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv); /* Reserved sectors */
  3140. tbl[BPB_NumFATs] = N_FATS; /* Number of FATs */
  3141. i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR; /* Number of rootdir entries */
  3142. ST_WORD(tbl+BPB_RootEntCnt, i);
  3143. if (n_vol < 0x10000) { /* Number of total sectors */
  3144. ST_WORD(tbl+BPB_TotSec16, n_vol);
  3145. } else {
  3146. ST_DWORD(tbl+BPB_TotSec32, n_vol);
  3147. }
  3148. tbl[BPB_Media] = md; /* Media descriptor */
  3149. ST_WORD(tbl+BPB_SecPerTrk, 63); /* Number of sectors per track */
  3150. ST_WORD(tbl+BPB_NumHeads, 255); /* Number of heads */
  3151. ST_DWORD(tbl+BPB_HiddSec, b_vol); /* Hidden sectors */
  3152. n = get_fattime(); /* Use current time as VSN */
  3153. if (fmt == FS_FAT32) {
  3154. ST_DWORD(tbl+BS_VolID32, n); /* VSN */
  3155. ST_DWORD(tbl+BPB_FATSz32, n_fat); /* Number of sectors per FAT */
  3156. ST_DWORD(tbl+BPB_RootClus, 2); /* Root directory start cluster (2) */
  3157. ST_WORD(tbl+BPB_FSInfo, 1); /* FSInfo record offset (VBR+1) */
  3158. ST_WORD(tbl+BPB_BkBootSec, 6); /* Backup boot record offset (VBR+6) */
  3159. tbl[BS_DrvNum32] = 0x80; /* Drive number */
  3160. tbl[BS_BootSig32] = 0x29; /* Extended boot signature */
  3161. mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
  3162. } else {
  3163. ST_DWORD(tbl+BS_VolID, n); /* VSN */
  3164. ST_WORD(tbl+BPB_FATSz16, n_fat); /* Number of sectors per FAT */
  3165. tbl[BS_DrvNum] = 0x80; /* Drive number */
  3166. tbl[BS_BootSig] = 0x29; /* Extended boot signature */
  3167. mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19); /* Volume label, FAT signature */
  3168. }
  3169. ST_WORD(tbl+BS_55AA, 0xAA55); /* Signature (Offset is fixed here regardless of sector size) */
  3170. if (disk_write(drv, tbl, b_vol, 1) != RES_OK) /* Write VBR */
  3171. return FR_DISK_ERR;
  3172. if (fmt == FS_FAT32) /* Write backup VBR if needed (VBR+6) */
  3173. disk_write(drv, tbl, b_vol + 6, 1);
  3174. /* Initialize FAT area */
  3175. wsect = b_fat;
  3176. for (i = 0; i < N_FATS; i++) { /* Initialize each FAT copy */
  3177. mem_set(tbl, 0, SS(fs)); /* 1st sector of the FAT */
  3178. n = md; /* Media descriptor byte */
  3179. if (fmt != FS_FAT32) {
  3180. n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
  3181. ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT12/16) */
  3182. } else {
  3183. n |= 0xFFFFFF00;
  3184. ST_DWORD(tbl+0, n); /* Reserve cluster #0-1 (FAT32) */
  3185. ST_DWORD(tbl+4, 0xFFFFFFFF);
  3186. ST_DWORD(tbl+8, 0x0FFFFFFF); /* Reserve cluster #2 for root dir */
  3187. }
  3188. if (disk_write(drv, tbl, wsect++, 1) != RES_OK)
  3189. return FR_DISK_ERR;
  3190. mem_set(tbl, 0, SS(fs)); /* Fill following FAT entries with zero */
  3191. for (n = 1; n < n_fat; n++) { /* This loop may take a time on FAT32 volume due to many single sector writes */
  3192. if (disk_write(drv, tbl, wsect++, 1) != RES_OK)
  3193. return FR_DISK_ERR;
  3194. }
  3195. }
  3196. /* Initialize root directory */
  3197. i = (fmt == FS_FAT32) ? au : n_dir;
  3198. do {
  3199. if (disk_write(drv, tbl, wsect++, 1) != RES_OK)
  3200. return FR_DISK_ERR;
  3201. } while (--i);
  3202. #if _USE_ERASE /* Erase data area if needed */
  3203. {
  3204. DWORD eb[2];
  3205. eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
  3206. disk_ioctl(drv, CTRL_ERASE_SECTOR, eb);
  3207. }
  3208. #endif
  3209. /* Create FSInfo if needed */
  3210. if (fmt == FS_FAT32) {
  3211. ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
  3212. ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
  3213. ST_DWORD(tbl+FSI_Free_Count, n_clst - 1); /* Number of free clusters */
  3214. ST_DWORD(tbl+FSI_Nxt_Free, 2); /* Last allocated cluster# */
  3215. ST_WORD(tbl+BS_55AA, 0xAA55);
  3216. disk_write(drv, tbl, b_vol + 1, 1); /* Write original (VBR+1) */
  3217. disk_write(drv, tbl, b_vol + 7, 1); /* Write backup (VBR+7) */
  3218. }
  3219. return (disk_ioctl(drv, CTRL_SYNC, (void*)0) == RES_OK) ? FR_OK : FR_DISK_ERR;
  3220. }
  3221. #endif /* _USE_MKFS && !_FS_READONLY */
  3222. #if _USE_STRFUNC
  3223. /*-----------------------------------------------------------------------*/
  3224. /* Get a string from the file */
  3225. /*-----------------------------------------------------------------------*/
  3226. TCHAR* f_gets (
  3227. TCHAR* buff, /* Pointer to the string buffer to read */
  3228. int len, /* Size of string buffer (characters) */
  3229. FIL* fil /* Pointer to the file object */
  3230. )
  3231. {
  3232. int n = 0;
  3233. TCHAR c, *p = buff;
  3234. BYTE s[2];
  3235. UINT rc;
  3236. while (n < len - 1) { /* Read bytes until buffer gets filled */
  3237. f_read(fil, s, 1, &rc);
  3238. if (rc != 1) break; /* Break on EOF or error */
  3239. c = s[0];
  3240. #if _LFN_UNICODE /* Read a character in UTF-8 encoding */
  3241. if (c >= 0x80) {
  3242. if (c < 0xC0) continue; /* Skip stray trailer */
  3243. if (c < 0xE0) { /* Two-byte sequense */
  3244. f_read(fil, s, 1, &rc);
  3245. if (rc != 1) break;
  3246. c = ((c & 0x1F) << 6) | (s[0] & 0x3F);
  3247. if (c < 0x80) c = '?';
  3248. } else {
  3249. if (c < 0xF0) { /* Three-byte sequense */
  3250. f_read(fil, s, 2, &rc);
  3251. if (rc != 2) break;
  3252. c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F);
  3253. if (c < 0x800) c = '?';
  3254. } else { /* Reject four-byte sequense */
  3255. c = '?';
  3256. }
  3257. }
  3258. }
  3259. #endif
  3260. #if _USE_STRFUNC >= 2
  3261. if (c == '\r') continue; /* Strip '\r' */
  3262. #endif
  3263. *p++ = c;
  3264. n++;
  3265. if (c == '\n') break; /* Break on EOL */
  3266. }
  3267. *p = 0;
  3268. return n ? buff : 0; /* When no data read (eof or error), return with error. */
  3269. }
  3270. #if !_FS_READONLY
  3271. #include <stdarg.h>
  3272. /*-----------------------------------------------------------------------*/
  3273. /* Put a character to the file */
  3274. /*-----------------------------------------------------------------------*/
  3275. int f_putc (
  3276. TCHAR c, /* A character to be output */
  3277. FIL* fil /* Pointer to the file object */
  3278. )
  3279. {
  3280. UINT bw, btw;
  3281. BYTE s[3];
  3282. #if _USE_STRFUNC >= 2
  3283. if (c == '\n') f_putc ('\r', fil); /* LF -> CRLF conversion */
  3284. #endif
  3285. #if _LFN_UNICODE /* Write the character in UTF-8 encoding */
  3286. if (c < 0x80) { /* 7-bit */
  3287. s[0] = (BYTE)c;
  3288. btw = 1;
  3289. } else {
  3290. if (c < 0x800) { /* 11-bit */
  3291. s[0] = (BYTE)(0xC0 | (c >> 6));
  3292. s[1] = (BYTE)(0x80 | (c & 0x3F));
  3293. btw = 2;
  3294. } else { /* 16-bit */
  3295. s[0] = (BYTE)(0xE0 | (c >> 12));
  3296. s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F));
  3297. s[2] = (BYTE)(0x80 | (c & 0x3F));
  3298. btw = 3;
  3299. }
  3300. }
  3301. #else /* Write the character without conversion */
  3302. s[0] = (BYTE)c;
  3303. btw = 1;
  3304. #endif
  3305. f_write(fil, s, btw, &bw); /* Write the char to the file */
  3306. return (bw == btw) ? 1 : EOF; /* Return the result */
  3307. }
  3308. /*-----------------------------------------------------------------------*/
  3309. /* Put a string to the file */
  3310. /*-----------------------------------------------------------------------*/
  3311. int f_puts (
  3312. const TCHAR* str, /* Pointer to the string to be output */
  3313. FIL* fil /* Pointer to the file object */
  3314. )
  3315. {
  3316. int n;
  3317. for (n = 0; *str; str++, n++) {
  3318. if (f_putc(*str, fil) == EOF) return EOF;
  3319. }
  3320. return n;
  3321. }
  3322. /*-----------------------------------------------------------------------*/
  3323. /* Put a formatted string to the file */
  3324. /*-----------------------------------------------------------------------*/
  3325. int f_printf (
  3326. FIL* fil, /* Pointer to the file object */
  3327. const TCHAR* str, /* Pointer to the format string */
  3328. ... /* Optional arguments... */
  3329. )
  3330. {
  3331. va_list arp;
  3332. BYTE f, r;
  3333. UINT i, j, w;
  3334. ULONG v;
  3335. TCHAR c, d, s[16], *p;
  3336. int res, cc;
  3337. va_start(arp, str);
  3338. for (cc = res = 0; cc != EOF; res += cc) {
  3339. c = *str++;
  3340. if (c == 0) break; /* End of string */
  3341. if (c != '%') { /* Non escape character */
  3342. cc = f_putc(c, fil);
  3343. if (cc != EOF) cc = 1;
  3344. continue;
  3345. }
  3346. w = f = 0;
  3347. c = *str++;
  3348. if (c == '0') { /* Flag: '0' padding */
  3349. f = 1; c = *str++;
  3350. } else {
  3351. if (c == '-') { /* Flag: left justified */
  3352. f = 2; c = *str++;
  3353. }
  3354. }
  3355. while (IsDigit(c)) { /* Precision */
  3356. w = w * 10 + c - '0';
  3357. c = *str++;
  3358. }
  3359. if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
  3360. f |= 4; c = *str++;
  3361. }
  3362. if (!c) break;
  3363. d = c;
  3364. if (IsLower(d)) d -= 0x20;
  3365. switch (d) { /* Type is... */
  3366. case 'S' : /* String */
  3367. p = va_arg(arp, TCHAR*);
  3368. for (j = 0; p[j]; j++) ;
  3369. res = 0;
  3370. while (!(f & 2) && j++ < w) res += (cc = f_putc(' ', fil));
  3371. res += (cc = f_puts(p, fil));
  3372. while (j++ < w) res += (cc = f_putc(' ', fil));
  3373. if (cc != EOF) cc = res;
  3374. continue;
  3375. case 'C' : /* Character */
  3376. cc = f_putc((TCHAR)va_arg(arp, int), fil); continue;
  3377. case 'B' : /* Binary */
  3378. r = 2; break;
  3379. case 'O' : /* Octal */
  3380. r = 8; break;
  3381. case 'D' : /* Signed decimal */
  3382. case 'U' : /* Unsigned decimal */
  3383. r = 10; break;
  3384. case 'X' : /* Hexdecimal */
  3385. r = 16; break;
  3386. default: /* Unknown type (passthrough) */
  3387. cc = f_putc(c, fil); continue;
  3388. }
  3389. /* Get an argument and put it in numeral */
  3390. v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : va_arg(arp, unsigned int));
  3391. if (d == 'D' && (v & 0x80000000)) {
  3392. v = 0 - v;
  3393. f |= 8;
  3394. }
  3395. i = 0;
  3396. do {
  3397. d = (TCHAR)(v % r); v /= r;
  3398. if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
  3399. s[i++] = d + '0';
  3400. } while (v && i < sizeof(s) / sizeof(s[0]));
  3401. if (f & 8) s[i++] = '-';
  3402. j = i; d = (f & 1) ? '0' : ' ';
  3403. res = 0;
  3404. while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil));
  3405. do res += (cc = f_putc(s[--i], fil)); while(i);
  3406. while (j++ < w) res += (cc = f_putc(' ', fil));
  3407. if (cc != EOF) cc = res;
  3408. }
  3409. va_end(arp);
  3410. return (cc == EOF) ? cc : res;
  3411. }
  3412. #endif /* !_FS_READONLY */
  3413. #endif /* _USE_STRFUNC */
  3414. #include <rtthread.h>
  3415. #if _VOLUMES > 1
  3416. int elm_get_vol(FATFS *fat)
  3417. {
  3418. int vol;
  3419. for (vol = 0; vol < _VOLUMES; vol ++)
  3420. {
  3421. if (FatFs[vol] == fat) return vol;
  3422. }
  3423. return -1;
  3424. }
  3425. #endif
  3426. int leavefs(FATFS* fs, int res)
  3427. {
  3428. LEAVE_FF(fs, res);
  3429. }