ff.c 131 KB

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