ff.c 131 KB

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