ff.c 154 KB

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