1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606 |
- #include "ff.h"
- #include "diskio.h"
- #if _FS_REENTRANT
- #include "rtos.h"
- #include "semphr.h"
- extern volatile xSemaphoreHandle Mutex_fatfs;
- #endif
- #if _FATFS != 8051
- #error Wrong include file (ff.h).
- #endif
- #if _FS_REENTRANT
- #if _USE_LFN == 1
- #error Static LFN work area cannot be used at thread-safe configuration.
- #endif
- #define ENTER_FF(fs) { if (!lock_fs(fs)) return FR_TIMEOUT; }
- #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; }
- #else
- #define ENTER_FF(fs)
- #define LEAVE_FF(fs, res) return res
- #endif
- #define ABORT(fs, res) { fp->err = (BYTE)(res); LEAVE_FF(fs, res); }
- #if (_MAX_SS < _MIN_SS) || (_MAX_SS != 512 && _MAX_SS != 1024 && _MAX_SS != 2048 && _MAX_SS != 4096) || (_MIN_SS != 512 && _MIN_SS != 1024 && _MIN_SS != 2048 && _MIN_SS != 4096)
- #error Wrong sector size configuration.
- #endif
- #if _MAX_SS == _MIN_SS
- #define SS(fs) ((UINT)_MAX_SS)
- #else
- #define SS(fs) ((fs)->ssize)
- #endif
- #if _FS_LOCK
- #if _FS_READONLY
- #error _FS_LOCK must be 0 at read-only cfg.
- #endif
- typedef struct {
- FATFS *fs;
- DWORD clu;
- WORD idx;
- WORD ctr;
- } FILESEM;
- #endif
- #if _CODE_PAGE == 932
- #define _DF1S 0x81
- #define _DF1E 0x9F
- #define _DF2S 0xE0
- #define _DF2E 0xFC
- #define _DS1S 0x40
- #define _DS1E 0x7E
- #define _DS2S 0x80
- #define _DS2E 0xFC
- #elif _CODE_PAGE == 936
- #define _DF1S 0x81
- #define _DF1E 0xFE
- #define _DS1S 0x40
- #define _DS1E 0x7E
- #define _DS2S 0x80
- #define _DS2E 0xFE
- #elif _CODE_PAGE == 949
- #define _DF1S 0x81
- #define _DF1E 0xFE
- #define _DS1S 0x41
- #define _DS1E 0x5A
- #define _DS2S 0x61
- #define _DS2E 0x7A
- #define _DS3S 0x81
- #define _DS3E 0xFE
- #elif _CODE_PAGE == 950
- #define _DF1S 0x81
- #define _DF1E 0xFE
- #define _DS1S 0x40
- #define _DS1E 0x7E
- #define _DS2S 0xA1
- #define _DS2E 0xFE
- #elif _CODE_PAGE == 437
- #define _DF1S 0
- #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, \
- 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,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0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF}
- #elif _CODE_PAGE == 737
- #define _DF1S 0
- #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, \
- 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, \
- 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, \
- 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}
- #elif _CODE_PAGE == 775
- #define _DF1S 0
- #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, \
- 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,0x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xDD,0xDE,0xDF, \
- 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}
- #elif _CODE_PAGE == 1257
- #define _DF1S 0
- #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, \
- 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, \
- 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, \
- 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}
- #elif _CODE_PAGE == 1258
- #define _DF1S 0
- #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, \
- 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, \
- 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, \
- 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}
- #elif _CODE_PAGE == 1
- #if _USE_LFN
- #error Cannot use LFN feature without valid code page.
- #endif
- #define _DF1S 0
- #else
- #error Unknown code page
- #endif
- #define IsUpper(c) (((c)>='A')&&((c)<='Z'))
- #define IsLower(c) (((c)>='a')&&((c)<='z'))
- #define IsDigit(c) (((c)>='0')&&((c)<='9'))
- #if _DF1S
- #ifdef _DF2S
- #define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
- #else
- #define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
- #endif
- #ifdef _DS3S
- #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
- #else
- #define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
- #endif
- #else
- #define IsDBCS1(c) 0
- #define IsDBCS2(c) 0
- #endif
- #define NS 11
- #define NS_LOSS 0x01
- #define NS_LFN 0x02
- #define NS_LAST 0x04
- #define NS_BODY 0x08
- #define NS_EXT 0x10
- #define NS_DOT 0x20
- #define MIN_FAT16 4086U
- #define MIN_FAT32 65526U
- #define BS_jmpBoot 0
- #define BS_OEMName 3
- #define BPB_BytsPerSec 11
- #define BPB_SecPerClus 13
- #define BPB_RsvdSecCnt 14
- #define BPB_NumFATs 16
- #define BPB_RootEntCnt 17
- #define BPB_TotSec16 19
- #define BPB_Media 21
- #define BPB_FATSz16 22
- #define BPB_SecPerTrk 24
- #define BPB_NumHeads 26
- #define BPB_HiddSec 28
- #define BPB_TotSec32 32
- #define BS_DrvNum 36
- #define BS_BootSig 38
- #define BS_VolID 39
- #define BS_VolLab 43
- #define BS_FilSysType 54
- #define BPB_FATSz32 36
- #define BPB_ExtFlags 40
- #define BPB_FSVer 42
- #define BPB_RootClus 44
- #define BPB_FSInfo 48
- #define BPB_BkBootSec 50
- #define BS_DrvNum32 64
- #define BS_BootSig32 66
- #define BS_VolID32 67
- #define BS_VolLab32 71
- #define BS_FilSysType32 82
- #define FSI_LeadSig 0
- #define FSI_StrucSig 484
- #define FSI_Free_Count 488
- #define FSI_Nxt_Free 492
- #define MBR_Table 446
- #define SZ_PTE 16
- #define BS_55AA 510
- #define DIR_Name 0
- #define DIR_Attr 11
- #define DIR_NTres 12
- #define DIR_CrtTimeTenth 13
- #define DIR_CrtTime 14
- #define DIR_CrtDate 16
- #define DIR_LstAccDate 18
- #define DIR_FstClusHI 20
- #define DIR_WrtTime 22
- #define DIR_WrtDate 24
- #define DIR_FstClusLO 26
- #define DIR_FileSize 28
- #define LDIR_Ord 0
- #define LDIR_Attr 11
- #define LDIR_Type 12
- #define LDIR_Chksum 13
- #define LDIR_FstClusLO 26
- #define SZ_DIR 32
- #define LLE 0x40
- #define DDE 0xE5
- #define NDDE 0x05
- #if _VOLUMES >= 1 || _VOLUMES <= 10
- static
- FATFS *FatFs[_VOLUMES];
- #else
- #error Number of volumes must be 1 to 10.
- #endif
- static
- WORD Fsid;
- #if _FS_RPATH && _VOLUMES >= 2
- static
- BYTE CurrVol;
- #endif
- #if _FS_LOCK
- static
- FILESEM Files[_FS_LOCK];
- #endif
- #if _USE_LFN == 0
- #define DEF_NAMEBUF BYTE sfn[12]
- #define INIT_BUF(dobj) (dobj).fn = sfn
- #define FREE_BUF()
- #elif _USE_LFN == 1
- static
- WCHAR LfnBuf[_MAX_LFN+1];
- #define DEF_NAMEBUF BYTE sfn[12]
- #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = LfnBuf; }
- #define FREE_BUF()
- #elif _USE_LFN == 2
- #define DEF_NAMEBUF BYTE sfn[12]; WCHAR lbuf[_MAX_LFN+1]
- #define INIT_BUF(dobj) { (dobj).fn = sfn; (dobj).lfn = lbuf; }
- #define FREE_BUF()
- #elif _USE_LFN == 3
- #define DEF_NAMEBUF BYTE sfn[12]; WCHAR *lfn
- #define INIT_BUF(dobj) { lfn = ff_memalloc((_MAX_LFN + 1) * 2); \
- if (!lfn) LEAVE_FF((dobj).fs, FR_NOT_ENOUGH_CORE); \
- (dobj).lfn = lfn; (dobj).fn = sfn; }
- #define FREE_BUF() ff_memfree(lfn)
- #else
- #error Wrong LFN configuration.
- #endif
- #ifdef _EXCVT
- static
- const BYTE ExCvt[] = _EXCVT;
- #endif
- DWORD get_fattime ()
- {
- return
- ((DWORD)(2000 - 1980) << 25)
- | ((DWORD)1 << 21)
- | ((DWORD)1 << 16)
- | ((DWORD)1 << 11)
- | ((DWORD)1 << 5)
- | ((DWORD)1 >> 1);
- }
- static
- void mem_cpy (void* dst, const void* src, UINT cnt) {
- BYTE *d = (BYTE*)dst;
- const BYTE *s = (const BYTE*)src;
- #if _WORD_ACCESS == 1
- while (cnt >= sizeof (int)) {
- *(int*)d = *(int*)s;
- d += sizeof (int); s += sizeof (int);
- cnt -= sizeof (int);
- }
- #endif
- while (cnt--)
- *d++ = *s++;
- }
- static
- void mem_set (void* dst, int val, UINT cnt) {
- BYTE *d = (BYTE*)dst;
- while (cnt--)
- *d++ = (BYTE)val;
- }
- static
- int mem_cmp (const void* dst, const void* src, UINT cnt) {
- const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
- int r = 0;
- while (cnt-- && (r = *d++ - *s++) == 0) ;
- return r;
- }
- static
- int chk_chr (const char* str, int chr) {
- while (*str && *str != chr) str++;
- return *str;
- }
- #if _FS_REENTRANT
- static
- int lock_fs (
- FATFS* fs
- )
- {
- return ff_req_grant(fs->sobj);
- }
- static
- void unlock_fs (
- FATFS* fs,
- FRESULT res
- )
- {
- if (fs &&
- res != FR_NOT_ENABLED &&
- res != FR_INVALID_DRIVE &&
- res != FR_INVALID_OBJECT &&
- res != FR_TIMEOUT) {
- ff_rel_grant(fs->sobj);
- }
- }
- #endif
- #if _FS_LOCK
- static
- FRESULT chk_lock (
- DIR* dp,
- int acc
- )
- {
- UINT i, be;
-
- for (i = be = 0; i < _FS_LOCK; i++) {
- if (Files[i].fs) {
- if (Files[i].fs == dp->fs &&
- Files[i].clu == dp->sclust &&
- Files[i].idx == dp->index) break;
- } else {
- be = 1;
- }
- }
- if (i == _FS_LOCK)
- return (be || acc == 2) ? FR_OK : FR_TOO_MANY_OPEN_FILES;
-
- return (acc || Files[i].ctr == 0x100) ? FR_LOCKED : FR_OK;
- }
- static
- int enq_lock (void)
- {
- UINT i;
- for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
- return (i == _FS_LOCK) ? 0 : 1;
- }
- static
- UINT inc_lock (
- DIR* dp,
- int acc
- )
- {
- UINT i;
- for (i = 0; i < _FS_LOCK; i++) {
- if (Files[i].fs == dp->fs &&
- Files[i].clu == dp->sclust &&
- Files[i].idx == dp->index) break;
- }
- if (i == _FS_LOCK) {
- for (i = 0; i < _FS_LOCK && Files[i].fs; i++) ;
- if (i == _FS_LOCK) return 0;
- Files[i].fs = dp->fs;
- Files[i].clu = dp->sclust;
- Files[i].idx = dp->index;
- Files[i].ctr = 0;
- }
- if (acc && Files[i].ctr) return 0;
- Files[i].ctr = acc ? 0x100 : Files[i].ctr + 1;
- return i + 1;
- }
- static
- FRESULT dec_lock (
- UINT i
- )
- {
- WORD n;
- FRESULT res;
- if (--i < _FS_LOCK) {
- n = Files[i].ctr;
- if (n == 0x100) n = 0;
- if (n) n--;
- Files[i].ctr = n;
- if (!n) Files[i].fs = 0;
- res = FR_OK;
- } else {
- res = FR_INT_ERR;
- }
- return res;
- }
- static
- void clear_lock (
- FATFS *fs
- )
- {
- UINT i;
- for (i = 0; i < _FS_LOCK; i++) {
- if (Files[i].fs == fs) Files[i].fs = 0;
- }
- }
- #endif
- #if !_FS_READONLY
- static
- FRESULT sync_window (
- FATFS* fs
- )
- {
- DWORD wsect;
- UINT nf;
- if (fs->wflag) {
- wsect = fs->winsect;
- if (disk_write(fs->drv, fs->win, wsect, 1))
- return FR_DISK_ERR;
- fs->wflag = 0;
- if (wsect - fs->fatbase < fs->fsize) {
- for (nf = fs->n_fats; nf >= 2; nf--) {
- wsect += fs->fsize;
- disk_write(fs->drv, fs->win, wsect, 1);
- }
- }
- }
- return FR_OK;
- }
- #endif
- static
- FRESULT move_window (
- FATFS* fs,
- DWORD sector
- )
- {
- if (sector != fs->winsect) {
- #if !_FS_READONLY
- if (sync_window(fs) != FR_OK)
- return FR_DISK_ERR;
- #endif
- if (disk_read(fs->drv, fs->win, sector, 1))
- return FR_DISK_ERR;
- fs->winsect = sector;
- }
- return FR_OK;
- }
- #if !_FS_READONLY
- static
- FRESULT sync_fs (
- FATFS* fs
- )
- {
- FRESULT res;
- res = sync_window(fs);
- if (res == FR_OK) {
-
- if (fs->fs_type == FS_FAT32 && fs->fsi_flag == 1) {
-
- mem_set(fs->win, 0, SS(fs));
- ST_WORD(fs->win+BS_55AA, 0xAA55);
- ST_DWORD(fs->win+FSI_LeadSig, 0x41615252);
- ST_DWORD(fs->win+FSI_StrucSig, 0x61417272);
- ST_DWORD(fs->win+FSI_Free_Count, fs->free_clust);
- ST_DWORD(fs->win+FSI_Nxt_Free, fs->last_clust);
-
- fs->winsect = fs->volbase + 1;
- disk_write(fs->drv, fs->win, fs->winsect, 1);
- fs->fsi_flag = 0;
- }
-
- if (disk_ioctl(fs->drv, CTRL_SYNC, 0) != RES_OK)
- res = FR_DISK_ERR;
- }
- return res;
- }
- #endif
- DWORD clust2sect (
- FATFS* fs,
- DWORD clst
- )
- {
- clst -= 2;
- if (clst >= (fs->n_fatent - 2)) return 0;
- return clst * fs->csize + fs->database;
- }
- DWORD get_fat (
- FATFS* fs,
- DWORD clst
- )
- {
- UINT wc, bc;
- BYTE *p;
- if (clst < 2 || clst >= fs->n_fatent)
- return 1;
- switch (fs->fs_type) {
- case FS_FAT12 :
- bc = (UINT)clst; bc += bc / 2;
- if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
- wc = fs->win[bc % SS(fs)]; bc++;
- if (move_window(fs, fs->fatbase + (bc / SS(fs)))) break;
- wc |= fs->win[bc % SS(fs)] << 8;
- return clst & 1 ? wc >> 4 : (wc & 0xFFF);
- case FS_FAT16 :
- if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)))) break;
- p = &fs->win[clst * 2 % SS(fs)];
- return LD_WORD(p);
- case FS_FAT32 :
- if (move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)))) break;
- p = &fs->win[clst * 4 % SS(fs)];
- return LD_DWORD(p) & 0x0FFFFFFF;
- default:
- return 1;
- }
- return 0xFFFFFFFF;
- }
- #if !_FS_READONLY
- FRESULT put_fat (
- FATFS* fs,
- DWORD clst,
- DWORD val
- )
- {
- UINT bc;
- BYTE *p;
- FRESULT res;
- if (clst < 2 || clst >= fs->n_fatent) {
- res = FR_INT_ERR;
- } else {
- switch (fs->fs_type) {
- case FS_FAT12 :
- bc = (UINT)clst; bc += bc / 2;
- res = move_window(fs, fs->fatbase + (bc / SS(fs)));
- if (res != FR_OK) break;
- p = &fs->win[bc % SS(fs)];
- *p = (clst & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val;
- bc++;
- fs->wflag = 1;
- res = move_window(fs, fs->fatbase + (bc / SS(fs)));
- if (res != FR_OK) break;
- p = &fs->win[bc % SS(fs)];
- *p = (clst & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F));
- break;
- case FS_FAT16 :
- res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 2)));
- if (res != FR_OK) break;
- p = &fs->win[clst * 2 % SS(fs)];
- ST_WORD(p, (WORD)val);
- break;
- case FS_FAT32 :
- res = move_window(fs, fs->fatbase + (clst / (SS(fs) / 4)));
- if (res != FR_OK) break;
- p = &fs->win[clst * 4 % SS(fs)];
- val |= LD_DWORD(p) & 0xF0000000;
- ST_DWORD(p, val);
- break;
- default :
- res = FR_INT_ERR;
- }
- fs->wflag = 1;
- }
- return res;
- }
- #endif
- #if !_FS_READONLY
- static
- FRESULT remove_chain (
- FATFS* fs,
- DWORD clst
- )
- {
- FRESULT res;
- DWORD nxt;
- #if _USE_ERASE
- DWORD scl = clst, ecl = clst, rt[2];
- #endif
- if (clst < 2 || clst >= fs->n_fatent) {
- res = FR_INT_ERR;
- } else {
- res = FR_OK;
- while (clst < fs->n_fatent) {
- nxt = get_fat(fs, clst);
- if (nxt == 0) break;
- if (nxt == 1) { res = FR_INT_ERR; break; }
- if (nxt == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
- res = put_fat(fs, clst, 0);
- if (res != FR_OK) break;
- if (fs->free_clust != 0xFFFFFFFF) {
- fs->free_clust++;
- fs->fsi_flag |= 1;
- }
- #if _USE_ERASE
- if (ecl + 1 == nxt) {
- ecl = nxt;
- } else {
- rt[0] = clust2sect(fs, scl);
- rt[1] = clust2sect(fs, ecl) + fs->csize - 1;
- disk_ioctl(fs->drv, CTRL_ERASE_SECTOR, rt);
- scl = ecl = nxt;
- }
- #endif
- clst = nxt;
- }
- }
- return res;
- }
- #endif
- #if !_FS_READONLY
- static
- DWORD create_chain (
- FATFS* fs,
- DWORD clst
- )
- {
- DWORD cs, ncl, scl;
- FRESULT res;
- if (clst == 0) {
- scl = fs->last_clust;
- if (!scl || scl >= fs->n_fatent) scl = 1;
- }
- else {
- cs = get_fat(fs, clst);
- if (cs < 2) return 1;
- if (cs == 0xFFFFFFFF) return cs;
- if (cs < fs->n_fatent) return cs;
- scl = clst;
- }
- ncl = scl;
- for (;;) {
- ncl++;
- if (ncl >= fs->n_fatent) {
- ncl = 2;
- if (ncl > scl) return 0;
- }
- cs = get_fat(fs, ncl);
- if (cs == 0) break;
- if (cs == 0xFFFFFFFF || cs == 1)
- return cs;
- if (ncl == scl) return 0;
- }
- res = put_fat(fs, ncl, 0x0FFFFFFF);
- if (res == FR_OK && clst != 0) {
- res = put_fat(fs, clst, ncl);
- }
- if (res == FR_OK) {
- fs->last_clust = ncl;
- if (fs->free_clust != 0xFFFFFFFF) {
- fs->free_clust--;
- fs->fsi_flag |= 1;
- }
- } else {
- ncl = (res == FR_DISK_ERR) ? 0xFFFFFFFF : 1;
- }
- return ncl;
- }
- #endif
- #if _USE_FASTSEEK
- static
- DWORD clmt_clust (
- FIL* fp,
- DWORD ofs
- )
- {
- DWORD cl, ncl, *tbl;
- tbl = fp->cltbl + 1;
- cl = ofs / SS(fp->fs) / fp->fs->csize;
- for (;;) {
- ncl = *tbl++;
- if (!ncl) return 0;
- if (cl < ncl) break;
- cl -= ncl; tbl++;
- }
- return cl + *tbl;
- }
- #endif
- static
- FRESULT dir_sdi (
- DIR* dp,
- UINT idx
- )
- {
- DWORD clst, sect;
- UINT ic;
- dp->index = (WORD)idx;
- clst = dp->sclust;
- if (clst == 1 || clst >= dp->fs->n_fatent)
- return FR_INT_ERR;
- if (!clst && dp->fs->fs_type == FS_FAT32)
- clst = dp->fs->dirbase;
- if (clst == 0) {
- if (idx >= dp->fs->n_rootdir)
- return FR_INT_ERR;
- sect = dp->fs->dirbase;
- }
- else {
- ic = SS(dp->fs) / SZ_DIR * dp->fs->csize;
- while (idx >= ic) {
- clst = get_fat(dp->fs, clst);
- if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
- if (clst < 2 || clst >= dp->fs->n_fatent)
- return FR_INT_ERR;
- idx -= ic;
- }
- sect = clust2sect(dp->fs, clst);
- }
- dp->clust = clst;
- if (!sect) return FR_INT_ERR;
- dp->sect = sect + idx / (SS(dp->fs) / SZ_DIR);
- dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIR)) * SZ_DIR;
- return FR_OK;
- }
- static
- FRESULT dir_next (
- DIR* dp,
- int stretch
- )
- {
- DWORD clst;
- UINT i;
- i = dp->index + 1;
- if (!(i & 0xFFFF) || !dp->sect)
- return FR_NO_FILE;
- if (!(i % (SS(dp->fs) / SZ_DIR))) {
- dp->sect++;
- if (!dp->clust) {
- if (i >= dp->fs->n_rootdir)
- return FR_NO_FILE;
- }
- else {
- if (((i / (SS(dp->fs) / SZ_DIR)) & (dp->fs->csize - 1)) == 0) {
- clst = get_fat(dp->fs, dp->clust);
- if (clst <= 1) return FR_INT_ERR;
- if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
- if (clst >= dp->fs->n_fatent) {
- #if !_FS_READONLY
- UINT c;
- if (!stretch) return FR_NO_FILE;
- clst = create_chain(dp->fs, dp->clust);
- if (clst == 0) return FR_DENIED;
- if (clst == 1) return FR_INT_ERR;
- if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
-
- if (sync_window(dp->fs)) return FR_DISK_ERR;
- mem_set(dp->fs->win, 0, SS(dp->fs));
- dp->fs->winsect = clust2sect(dp->fs, clst);
- for (c = 0; c < dp->fs->csize; c++) {
- dp->fs->wflag = 1;
- if (sync_window(dp->fs)) return FR_DISK_ERR;
- dp->fs->winsect++;
- }
- dp->fs->winsect -= c;
- #else
- if (!stretch) return FR_NO_FILE;
- return FR_NO_FILE;
- #endif
- }
- dp->clust = clst;
- dp->sect = clust2sect(dp->fs, clst);
- }
- }
- }
- dp->index = (WORD)i;
- dp->dir = dp->fs->win + (i % (SS(dp->fs) / SZ_DIR)) * SZ_DIR;
- return FR_OK;
- }
- #if !_FS_READONLY
- static
- FRESULT dir_alloc (
- DIR* dp,
- UINT nent
- )
- {
- FRESULT res;
- UINT n;
- res = dir_sdi(dp, 0);
- if (res == FR_OK) {
- n = 0;
- do {
- res = move_window(dp->fs, dp->sect);
- if (res != FR_OK) break;
- if (dp->dir[0] == DDE || dp->dir[0] == 0) {
- if (++n == nent) break;
- } else {
- n = 0;
- }
- res = dir_next(dp, 1);
- } while (res == FR_OK);
- }
- if (res == FR_NO_FILE) res = FR_DENIED;
- return res;
- }
- #endif
- static
- DWORD ld_clust (
- FATFS* fs,
- BYTE* dir
- )
- {
- DWORD cl;
- cl = LD_WORD(dir+DIR_FstClusLO);
- if (fs->fs_type == FS_FAT32)
- cl |= (DWORD)LD_WORD(dir+DIR_FstClusHI) << 16;
- return cl;
- }
- #if !_FS_READONLY
- static
- void st_clust (
- BYTE* dir,
- DWORD cl
- )
- {
- ST_WORD(dir+DIR_FstClusLO, cl);
- ST_WORD(dir+DIR_FstClusHI, cl >> 16);
- }
- #endif
- #if _USE_LFN
- static
- const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30};
- static
- int cmp_lfn (
- WCHAR* lfnbuf,
- BYTE* dir
- )
- {
- UINT i, s;
- WCHAR wc, uc;
- i = ((dir[LDIR_Ord] & ~LLE) - 1) * 13;
- s = 0; wc = 1;
- do {
- uc = LD_WORD(dir+LfnOfs[s]);
- if (wc) {
- wc = ff_wtoupper(uc);
- if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++]))
- return 0;
- } else {
- if (uc != 0xFFFF) return 0;
- }
- } while (++s < 13);
- if ((dir[LDIR_Ord] & LLE) && wc && lfnbuf[i])
- return 0;
- return 1;
- }
- static
- int pick_lfn (
- WCHAR* lfnbuf,
- BYTE* dir
- )
- {
- UINT i, s;
- WCHAR wc, uc;
- i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13;
- s = 0; wc = 1;
- do {
- uc = LD_WORD(dir+LfnOfs[s]);
- if (wc) {
- if (i >= _MAX_LFN) return 0;
- lfnbuf[i++] = wc = uc;
- } else {
- if (uc != 0xFFFF) return 0;
- }
- } while (++s < 13);
- if (dir[LDIR_Ord] & LLE) {
- if (i >= _MAX_LFN) return 0;
- lfnbuf[i] = 0;
- }
- return 1;
- }
- #if !_FS_READONLY
- static
- void fit_lfn (
- const WCHAR* lfnbuf,
- BYTE* dir,
- BYTE ord,
- BYTE sum
- )
- {
- UINT i, s;
- WCHAR wc;
- dir[LDIR_Chksum] = sum;
- dir[LDIR_Attr] = AM_LFN;
- dir[LDIR_Type] = 0;
- ST_WORD(dir+LDIR_FstClusLO, 0);
- i = (ord - 1) * 13;
- s = wc = 0;
- do {
- if (wc != 0xFFFF) wc = lfnbuf[i++];
- ST_WORD(dir+LfnOfs[s], wc);
- if (!wc) wc = 0xFFFF;
- } while (++s < 13);
- if (wc == 0xFFFF || !lfnbuf[i]) ord |= LLE;
- dir[LDIR_Ord] = ord;
- }
- #endif
- #endif
- #if _USE_LFN
- static
- void gen_numname (
- BYTE* dst,
- const BYTE* src,
- const WCHAR* lfn,
- UINT seq
- )
- {
- BYTE ns[8], c;
- UINT i, j;
- mem_cpy(dst, src, 11);
- if (seq > 5) {
- WCHAR wc;
- DWORD sr = seq;
- while (*lfn) {
- wc = *lfn++;
- for (i = 0; i < 16; i++) {
- sr = (sr << 1) + (wc & 1);
- wc >>= 1;
- if (sr & 0x10000) sr ^= 0x11021;
- }
- }
- seq = (UINT)sr;
- }
-
- i = 7;
- do {
- c = (seq % 16) + '0';
- if (c > '9') c += 7;
- ns[i--] = c;
- seq /= 16;
- } while (seq);
- ns[i] = '~';
-
- for (j = 0; j < i && dst[j] != ' '; j++) {
- if (IsDBCS1(dst[j])) {
- if (j == i - 1) break;
- j++;
- }
- }
- do {
- dst[j++] = (i < 8) ? ns[i++] : ' ';
- } while (j < 8);
- }
- #endif
- #if _USE_LFN
- static
- BYTE sum_sfn (
- const BYTE* dir
- )
- {
- BYTE sum = 0;
- UINT n = 11;
- do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
- return sum;
- }
- #endif
- static
- FRESULT dir_find (
- DIR* dp
- )
- {
- FRESULT res;
- BYTE c, *dir;
- #if _USE_LFN
- BYTE a, ord, sum;
- #endif
- res = dir_sdi(dp, 0);
- if (res != FR_OK) return res;
- #if _USE_LFN
- ord = sum = 0xFF; dp->lfn_idx = 0xFFFF;
- #endif
- do {
- res = move_window(dp->fs, dp->sect);
- if (res != FR_OK) break;
- dir = dp->dir;
- c = dir[DIR_Name];
- if (c == 0) { res = FR_NO_FILE; break; }
- #if _USE_LFN
- a = dir[DIR_Attr] & AM_MASK;
- if (c == DDE || ((a & AM_VOL) && a != AM_LFN)) {
- ord = 0xFF; dp->lfn_idx = 0xFFFF;
- } else {
- if (a == AM_LFN) {
- if (dp->lfn) {
- if (c & LLE) {
- sum = dir[LDIR_Chksum];
- c &= ~LLE; ord = c;
- dp->lfn_idx = dp->index;
- }
-
- ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
- }
- } else {
- if (!ord && sum == sum_sfn(dir)) break;
- if (!(dp->fn[NS] & NS_LOSS) && !mem_cmp(dir, dp->fn, 11)) break;
- ord = 0xFF; dp->lfn_idx = 0xFFFF;
- }
- }
- #else
- if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dp->fn, 11))
- break;
- #endif
- res = dir_next(dp, 0);
- } while (res == FR_OK);
- return res;
- }
- #if _FS_MINIMIZE <= 1 || _USE_LABEL || _FS_RPATH >= 2
- static
- FRESULT dir_read (
- DIR* dp,
- int vol
- )
- {
- FRESULT res;
- BYTE a, c, *dir;
- #if _USE_LFN
- BYTE ord = 0xFF, sum = 0xFF;
- #endif
- res = FR_NO_FILE;
- while (dp->sect) {
- res = move_window(dp->fs, dp->sect);
- if (res != FR_OK) break;
- dir = dp->dir;
- c = dir[DIR_Name];
- if (c == 0) { res = FR_NO_FILE; break; }
- a = dir[DIR_Attr] & AM_MASK;
- #if _USE_LFN
- if (c == DDE || (!_FS_RPATH && c == '.') || (int)(a == AM_VOL) != vol) {
- ord = 0xFF;
- } else {
- if (a == AM_LFN) {
- if (c & LLE) {
- sum = dir[LDIR_Chksum];
- c &= ~LLE; ord = c;
- dp->lfn_idx = dp->index;
- }
-
- ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dp->lfn, dir)) ? ord - 1 : 0xFF;
- } else {
- if (ord || sum != sum_sfn(dir))
- dp->lfn_idx = 0xFFFF;
- break;
- }
- }
- #else
- if (c != DDE && (_FS_RPATH || c != '.') && a != AM_LFN && (int)(a == AM_VOL) == vol)
- break;
- #endif
- res = dir_next(dp, 0);
- if (res != FR_OK) break;
- }
- if (res != FR_OK) dp->sect = 0;
- return res;
- }
- #endif
- #if !_FS_READONLY
- static
- FRESULT dir_register (
- DIR* dp
- )
- {
- FRESULT res;
- #if _USE_LFN
- UINT n, nent;
- BYTE sn[12], *fn, sum;
- WCHAR *lfn;
- fn = dp->fn; lfn = dp->lfn;
- mem_cpy(sn, fn, 12);
- if (_FS_RPATH && (sn[NS] & NS_DOT))
- return FR_INVALID_NAME;
- if (sn[NS] & NS_LOSS) {
- fn[NS] = 0; dp->lfn = 0;
- for (n = 1; n < 100; n++) {
- gen_numname(fn, sn, lfn, n);
- res = dir_find(dp);
- if (res != FR_OK) break;
- }
- if (n == 100) return FR_DENIED;
- if (res != FR_NO_FILE) return res;
- fn[NS] = sn[NS]; dp->lfn = lfn;
- }
- if (sn[NS] & NS_LFN) {
- for (n = 0; lfn[n]; n++) ;
- nent = (n + 25) / 13;
- } else {
- nent = 1;
- }
- res = dir_alloc(dp, nent);
- if (res == FR_OK && --nent) {
- res = dir_sdi(dp, dp->index - nent);
- if (res == FR_OK) {
- sum = sum_sfn(dp->fn);
- do {
- res = move_window(dp->fs, dp->sect);
- if (res != FR_OK) break;
- fit_lfn(dp->lfn, dp->dir, (BYTE)nent, sum);
- dp->fs->wflag = 1;
- res = dir_next(dp, 0);
- } while (res == FR_OK && --nent);
- }
- }
- #else
- res = dir_alloc(dp, 1);
- #endif
- if (res == FR_OK) {
- res = move_window(dp->fs, dp->sect);
- if (res == FR_OK) {
- mem_set(dp->dir, 0, SZ_DIR);
- mem_cpy(dp->dir, dp->fn, 11);
- #if _USE_LFN
- dp->dir[DIR_NTres] = dp->fn[NS] & (NS_BODY | NS_EXT);
- #endif
- dp->fs->wflag = 1;
- }
- }
- return res;
- }
- #endif
- #if !_FS_READONLY && !_FS_MINIMIZE
- static
- FRESULT dir_remove (
- DIR* dp
- )
- {
- FRESULT res;
- #if _USE_LFN
- UINT i;
- i = dp->index;
- res = dir_sdi(dp, (dp->lfn_idx == 0xFFFF) ? i : dp->lfn_idx);
- if (res == FR_OK) {
- do {
- res = move_window(dp->fs, dp->sect);
- if (res != FR_OK) break;
- mem_set(dp->dir, 0, SZ_DIR);
- *dp->dir = DDE;
- dp->fs->wflag = 1;
- if (dp->index >= i) break;
- res = dir_next(dp, 0);
- } while (res == FR_OK);
- if (res == FR_NO_FILE) res = FR_INT_ERR;
- }
- #else
- res = dir_sdi(dp, dp->index);
- if (res == FR_OK) {
- res = move_window(dp->fs, dp->sect);
- if (res == FR_OK) {
- mem_set(dp->dir, 0, SZ_DIR);
- *dp->dir = DDE;
- dp->fs->wflag = 1;
- }
- }
- #endif
- return res;
- }
- #endif
- #if _FS_MINIMIZE <= 1 || _FS_RPATH >= 2
- static
- void get_fileinfo (
- DIR* dp,
- FILINFO* fno
- )
- {
- UINT i;
- TCHAR *p, c;
- p = fno->fname;
- if (dp->sect) {
- BYTE *dir = dp->dir;
- i = 0;
- while (i < 11) {
- c = (TCHAR)dir[i++];
- if (c == ' ') continue;
- if (c == NDDE) c = (TCHAR)DDE;
- if (i == 9) *p++ = '.';
- #if _USE_LFN
- if (IsUpper(c) && (dir[DIR_NTres] & (i >= 9 ? NS_EXT : NS_BODY)))
- c += 0x20;
- #if _LFN_UNICODE
- if (IsDBCS1(c) && i != 8 && i != 11 && IsDBCS2(dir[i]))
- c = c << 8 | dir[i++];
- c = ff_convert(c, 1);
- if (!c) c = '?';
- #endif
- #endif
- *p++ = c;
- }
- fno->fattrib = dir[DIR_Attr];
- fno->fsize = LD_DWORD(dir+DIR_FileSize);
- fno->fdate = LD_WORD(dir+DIR_WrtDate);
- fno->ftime = LD_WORD(dir+DIR_WrtTime);
- }
- *p = 0;
- #if _USE_LFN
- if (fno->lfname) {
- WCHAR w, *lfn;
- i = 0; p = fno->lfname;
- if (dp->sect && fno->lfsize && dp->lfn_idx != 0xFFFF) {
- lfn = dp->lfn;
- while ((w = *lfn++) != 0) {
- #if !_LFN_UNICODE
- w = ff_convert(w, 0);
- if (!w) { i = 0; break; }
- if (_DF1S && w >= 0x100)
- p[i++] = (TCHAR)(w >> 8);
- #endif
- if (i >= fno->lfsize - 1) { i = 0; break; }
- p[i++] = (TCHAR)w;
- }
- }
- p[i] = 0;
- }
- #endif
- }
- #endif
- static
- FRESULT create_name (
- DIR* dp,
- const TCHAR** path
- )
- {
- #if _USE_LFN
- BYTE b, cf;
- WCHAR w, *lfn;
- UINT i, ni, si, di;
- const TCHAR *p;
-
- for (p = *path; *p == '/' || *p == '\\'; p++) ;
- lfn = dp->lfn;
- si = di = 0;
- for (;;) {
- w = p[si++];
- if (w < ' ' || w == '/' || w == '\\') break;
- if (di >= _MAX_LFN)
- return FR_INVALID_NAME;
- #if !_LFN_UNICODE
- w &= 0xFF;
- if (IsDBCS1(w)) {
- b = (BYTE)p[si++];
- if (!IsDBCS2(b))
- return FR_INVALID_NAME;
- w = (w << 8) + b;
- }
- w = ff_convert(w, 1);
- if (!w) return FR_INVALID_NAME;
- #endif
- if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w))
- return FR_INVALID_NAME;
- lfn[di++] = w;
- }
- *path = &p[si];
- cf = (w < ' ') ? NS_LAST : 0;
- #if _FS_RPATH
- if ((di == 1 && lfn[di-1] == '.') ||
- (di == 2 && lfn[di-1] == '.' && lfn[di-2] == '.')) {
- lfn[di] = 0;
- for (i = 0; i < 11; i++)
- dp->fn[i] = (i < di) ? '.' : ' ';
- dp->fn[i] = cf | NS_DOT;
- return FR_OK;
- }
- #endif
- while (di) {
- w = lfn[di-1];
- if (w != ' ' && w != '.') break;
- di--;
- }
- if (!di) return FR_INVALID_NAME;
- lfn[di] = 0;
-
- mem_set(dp->fn, ' ', 11);
- for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ;
- if (si) cf |= NS_LOSS | NS_LFN;
- while (di && lfn[di - 1] != '.') di--;
- b = i = 0; ni = 8;
- for (;;) {
- w = lfn[si++];
- if (!w) break;
- if (w == ' ' || (w == '.' && si != di)) {
- cf |= NS_LOSS | NS_LFN; continue;
- }
- if (i >= ni || si == di) {
- if (ni == 11) {
- cf |= NS_LOSS | NS_LFN; break;
- }
- if (si != di) cf |= NS_LOSS | NS_LFN;
- if (si > di) break;
- si = di; i = 8; ni = 11;
- b <<= 2; continue;
- }
- if (w >= 0x80) {
- #ifdef _EXCVT
- w = ff_convert(w, 0);
- if (w) w = ExCvt[w - 0x80];
- #else
- w = ff_convert(ff_wtoupper(w), 0);
- #endif
- cf |= NS_LFN;
- }
- if (_DF1S && w >= 0x100) {
- if (i >= ni - 1) {
- cf |= NS_LOSS | NS_LFN; i = ni; continue;
- }
- dp->fn[i++] = (BYTE)(w >> 8);
- } else {
- if (!w || chk_chr("+,;=[]", w)) {
- w = '_'; cf |= NS_LOSS | NS_LFN;
- } else {
- if (IsUpper(w)) {
- b |= 2;
- } else {
- if (IsLower(w)) {
- b |= 1; w -= 0x20;
- }
- }
- }
- }
- dp->fn[i++] = (BYTE)w;
- }
- if (dp->fn[0] == DDE) dp->fn[0] = NDDE;
- if (ni == 8) b <<= 2;
- if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03)
- cf |= NS_LFN;
- if (!(cf & NS_LFN)) {
- if ((b & 0x03) == 0x01) cf |= NS_EXT;
- if ((b & 0x0C) == 0x04) cf |= NS_BODY;
- }
- dp->fn[NS] = cf;
- return FR_OK;
- #else
- BYTE b, c, d, *sfn;
- UINT ni, si, i;
- const char *p;
-
- for (p = *path; *p == '/' || *p == '\\'; p++) ;
- sfn = dp->fn;
- mem_set(sfn, ' ', 11);
- si = i = b = 0; ni = 8;
- #if _FS_RPATH
- if (p[si] == '.') {
- for (;;) {
- c = (BYTE)p[si++];
- if (c != '.' || si >= 3) break;
- sfn[i++] = c;
- }
- if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
- *path = &p[si];
- sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT;
- return FR_OK;
- }
- #endif
- for (;;) {
- c = (BYTE)p[si++];
- if (c <= ' ' || c == '/' || c == '\\') break;
- if (c == '.' || i >= ni) {
- if (ni != 8 || c != '.') return FR_INVALID_NAME;
- i = 8; ni = 11;
- b <<= 2; continue;
- }
- if (c >= 0x80) {
- b |= 3;
- #ifdef _EXCVT
- c = ExCvt[c - 0x80];
- #else
- #if !_DF1S
- return FR_INVALID_NAME;
- #endif
- #endif
- }
- if (IsDBCS1(c)) {
- d = (BYTE)p[si++];
- if (!IsDBCS2(d) || i >= ni - 1)
- return FR_INVALID_NAME;
- sfn[i++] = c;
- sfn[i++] = d;
- } else {
- if (chk_chr("\"*+,:;<=>\?[]|\x7F", c))
- return FR_INVALID_NAME;
- if (IsUpper(c)) {
- b |= 2;
- } else {
- if (IsLower(c)) {
- b |= 1; c -= 0x20;
- }
- }
- sfn[i++] = c;
- }
- }
- *path = &p[si];
- c = (c <= ' ') ? NS_LAST : 0;
- if (!i) return FR_INVALID_NAME;
- if (sfn[0] == DDE) sfn[0] = NDDE;
- if (ni == 8) b <<= 2;
- if ((b & 0x03) == 0x01) c |= NS_EXT;
- if ((b & 0x0C) == 0x04) c |= NS_BODY;
- sfn[NS] = c;
- return FR_OK;
- #endif
- }
- static
- FRESULT follow_path (
- DIR* dp,
- const TCHAR* path
- )
- {
- FRESULT res;
- BYTE *dir, ns;
- #if _FS_RPATH
- if (*path == '/' || *path == '\\') {
- path++; dp->sclust = 0;
- } else {
- dp->sclust = dp->fs->cdir;
- }
- #else
- if (*path == '/' || *path == '\\')
- path++;
- dp->sclust = 0;
- #endif
- if ((UINT)*path < ' ') {
- res = dir_sdi(dp, 0);
- dp->dir = 0;
- } else {
- for (;;) {
- res = create_name(dp, &path);
- if (res != FR_OK) break;
- res = dir_find(dp);
- ns = dp->fn[NS];
- if (res != FR_OK) {
- if (res == FR_NO_FILE) {
- if (_FS_RPATH && (ns & NS_DOT)) {
- dp->sclust = 0; dp->dir = 0;
- if (!(ns & NS_LAST)) continue;
- res = FR_OK;
- } else {
- if (!(ns & NS_LAST)) res = FR_NO_PATH;
- }
- }
- break;
- }
- if (ns & NS_LAST) break;
- dir = dp->dir;
- if (!(dir[DIR_Attr] & AM_DIR)) {
- res = FR_NO_PATH; break;
- }
- dp->sclust = ld_clust(dp->fs, dir);
- }
- }
- return res;
- }
- static
- int get_ldnumber (
- const TCHAR** path
- )
- {
- const TCHAR *tp, *tt;
- UINT i;
- int vol = -1;
- if (*path) {
- for (tt = *path; (UINT)*tt >= (_USE_LFN ? ' ' : '!') && *tt != ':'; tt++) ;
- if (*tt == ':') {
- tp = *path;
- i = *tp++ - '0';
- if (i < 10 && tp == tt) {
- if (i < _VOLUMES) {
- vol = (int)i;
- *path = ++tt;
- }
- } else {
- #if _STR_VOLUME_ID
- static const char* const str[] = {_VOLUME_STRS};
- const char *sp;
- char c;
- TCHAR tc;
- i = 0; tt++;
- do {
- sp = str[i]; tp = *path;
- do {
- c = *sp++; tc = *tp++;
- if (IsLower(tc)) tc -= 0x20;
- } while (c && (TCHAR)c == tc);
- } while ((c || tp != tt) && ++i < _VOLUMES);
- if (i < _VOLUMES) {
- vol = (int)i;
- *path = tt;
- }
- #endif
- }
- return vol;
- }
- #if _FS_RPATH && _VOLUMES >= 2
- vol = CurrVol;
- #else
- vol = 0;
- #endif
- }
- return vol;
- }
- static
- BYTE check_fs (
- FATFS* fs,
- DWORD sect
- )
- {
- fs->wflag = 0; fs->winsect = 0xFFFFFFFF;
- if (move_window(fs, sect) != FR_OK)
- return 3;
- if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55)
- return 2;
- if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146)
- return 0;
- if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)
- return 0;
- return 1;
- }
- static
- FRESULT find_volume (
- FATFS** rfs,
- const TCHAR** path,
- BYTE wmode
- )
- {
- BYTE fmt;
- int vol;
- DSTATUS stat;
- DWORD bsect, fasize, tsect, sysect, nclst, szbfat;
- WORD nrsv;
- FATFS *fs;
-
- *rfs = 0;
- vol = get_ldnumber(path);
- if (vol < 0) return FR_INVALID_DRIVE;
-
- fs = FatFs[vol];
- if (!fs) return FR_NOT_ENABLED;
- ENTER_FF(fs);
- *rfs = fs;
- if (fs->fs_type) {
- stat = disk_status(fs->drv);
- if (!(stat & STA_NOINIT)) {
- if (!_FS_READONLY && wmode && (stat & STA_PROTECT))
- return FR_WRITE_PROTECTED;
- return FR_OK;
- }
- }
-
-
- fs->fs_type = 0;
- fs->drv = LD2PD(vol);
- stat = disk_initialize(fs->drv);
- if (stat & STA_NOINIT)
- return FR_NOT_READY;
- if (!_FS_READONLY && wmode && (stat & STA_PROTECT))
- return FR_WRITE_PROTECTED;
- #if _MAX_SS != _MIN_SS
- if (disk_ioctl(fs->drv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK
- || SS(fs) < _MIN_SS || SS(fs) > _MAX_SS) return FR_DISK_ERR;
- #endif
-
- bsect = 0;
- fmt = check_fs(fs, bsect);
- if (fmt == 1 || (!fmt && (LD2PT(vol)))) {
- UINT i;
- DWORD br[4];
- for (i = 0; i < 4; i++) {
- BYTE *pt = fs->win+MBR_Table + i * SZ_PTE;
- br[i] = pt[4] ? LD_DWORD(&pt[8]) : 0;
- }
- i = LD2PT(vol);
- if (i) i--;
- do {
- bsect = br[i];
- fmt = bsect ? check_fs(fs, bsect) : 2;
- } while (!LD2PT(vol) && fmt && ++i < 4);
- }
- if (fmt == 3) return FR_DISK_ERR;
- if (fmt) return FR_NO_FILESYSTEM;
-
- if (LD_WORD(fs->win+BPB_BytsPerSec) != SS(fs))
- return FR_NO_FILESYSTEM;
- fasize = LD_WORD(fs->win+BPB_FATSz16);
- if (!fasize) fasize = LD_DWORD(fs->win+BPB_FATSz32);
- fs->fsize = fasize;
- fs->n_fats = fs->win[BPB_NumFATs];
- if (fs->n_fats != 1 && fs->n_fats != 2)
- return FR_NO_FILESYSTEM;
- fasize *= fs->n_fats;
- fs->csize = fs->win[BPB_SecPerClus];
- if (!fs->csize || (fs->csize & (fs->csize - 1)))
- return FR_NO_FILESYSTEM;
- fs->n_rootdir = LD_WORD(fs->win+BPB_RootEntCnt);
- if (fs->n_rootdir % (SS(fs) / SZ_DIR))
- return FR_NO_FILESYSTEM;
- tsect = LD_WORD(fs->win+BPB_TotSec16);
- if (!tsect) tsect = LD_DWORD(fs->win+BPB_TotSec32);
- nrsv = LD_WORD(fs->win+BPB_RsvdSecCnt);
- if (!nrsv) return FR_NO_FILESYSTEM;
-
- sysect = nrsv + fasize + fs->n_rootdir / (SS(fs) / SZ_DIR);
- if (tsect < sysect) return FR_NO_FILESYSTEM;
- nclst = (tsect - sysect) / fs->csize;
- if (!nclst) return FR_NO_FILESYSTEM;
- fmt = FS_FAT12;
- if (nclst >= MIN_FAT16) fmt = FS_FAT16;
- if (nclst >= MIN_FAT32) fmt = FS_FAT32;
-
- fs->n_fatent = nclst + 2;
- fs->volbase = bsect;
- fs->fatbase = bsect + nrsv;
- fs->database = bsect + sysect;
- if (fmt == FS_FAT32) {
- if (fs->n_rootdir) return FR_NO_FILESYSTEM;
- fs->dirbase = LD_DWORD(fs->win+BPB_RootClus);
- szbfat = fs->n_fatent * 4;
- } else {
- if (!fs->n_rootdir) return FR_NO_FILESYSTEM;
- fs->dirbase = fs->fatbase + fasize;
- szbfat = (fmt == FS_FAT16) ?
- fs->n_fatent * 2 : fs->n_fatent * 3 / 2 + (fs->n_fatent & 1);
- }
- if (fs->fsize < (szbfat + (SS(fs) - 1)) / SS(fs))
- return FR_NO_FILESYSTEM;
- #if !_FS_READONLY
-
- fs->last_clust = fs->free_clust = 0xFFFFFFFF;
-
- fs->fsi_flag = 0x80;
- #if (_FS_NOFSINFO & 3) != 3
- if (fmt == FS_FAT32
- && LD_WORD(fs->win+BPB_FSInfo) == 1
- && move_window(fs, bsect + 1) == FR_OK)
- {
- fs->fsi_flag = 0;
- if (LD_WORD(fs->win+BS_55AA) == 0xAA55
- && LD_DWORD(fs->win+FSI_LeadSig) == 0x41615252
- && LD_DWORD(fs->win+FSI_StrucSig) == 0x61417272)
- {
- #if (_FS_NOFSINFO & 1) == 0
- fs->free_clust = LD_DWORD(fs->win+FSI_Free_Count);
- #endif
- #if (_FS_NOFSINFO & 2) == 0
- fs->last_clust = LD_DWORD(fs->win+FSI_Nxt_Free);
- #endif
- }
- }
- #endif
- #endif
- fs->fs_type = fmt;
- fs->id = ++Fsid;
- #if _FS_RPATH
- fs->cdir = 0;
- #endif
- #if _FS_LOCK
- clear_lock(fs);
- #endif
- return FR_OK;
- }
- static
- FRESULT validate (
- void* obj
- )
- {
- FIL *fil = (FIL*)obj;
- if (!fil || !fil->fs || !fil->fs->fs_type || fil->fs->id != fil->id)
- return FR_INVALID_OBJECT;
- ENTER_FF(fil->fs);
- if (disk_status(fil->fs->drv) & STA_NOINIT)
- return FR_NOT_READY;
- return FR_OK;
- }
- FRESULT f_mount (
- FATFS* fs,
- const TCHAR* path,
- BYTE opt
- )
- {
- FATFS *cfs;
- int vol;
- FRESULT res;
- const TCHAR *rp = path;
- vol = get_ldnumber(&rp);
- if (vol < 0) return FR_INVALID_DRIVE;
- cfs = FatFs[vol];
- if (cfs) {
- #if _FS_LOCK
- clear_lock(cfs);
- #endif
- #if _FS_REENTRANT
- if (!ff_del_syncobj(cfs->sobj)) return FR_INT_ERR;
- #endif
- cfs->fs_type = 0;
- }
- if (fs) {
- fs->fs_type = 0;
- #if _FS_REENTRANT
- if (!ff_cre_syncobj((BYTE)vol, &fs->sobj)) return FR_INT_ERR;
- #endif
- }
- FatFs[vol] = fs;
- if (!fs || opt != 1) return FR_OK;
- res = find_volume(&fs, &path, 0);
- LEAVE_FF(fs, res);
- }
- FRESULT f_open (
- FIL* fp,
- const TCHAR* path,
- BYTE mode
- )
- {
- FRESULT res;
- DIR dj;
- BYTE *dir;
- DEF_NAMEBUF;
- if (!fp) return FR_INVALID_OBJECT;
- fp->fs = 0;
-
- #if !_FS_READONLY
- mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
- res = find_volume(&dj.fs, &path, (BYTE)(mode & ~FA_READ));
- #else
- mode &= FA_READ;
- res = find_volume(&dj.fs, &path, 0);
- #endif
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- dir = dj.dir;
- #if !_FS_READONLY
- if (res == FR_OK) {
- if (!dir)
- res = FR_INVALID_NAME;
- #if _FS_LOCK
- else
- res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
- #endif
- }
-
- if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
- DWORD dw, cl;
- if (res != FR_OK) {
- if (res == FR_NO_FILE)
- #if _FS_LOCK
- res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
- #else
- res = dir_register(&dj);
- #endif
- mode |= FA_CREATE_ALWAYS;
- dir = dj.dir;
- }
- else {
- if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) {
- res = FR_DENIED;
- } else {
- if (mode & FA_CREATE_NEW)
- res = FR_EXIST;
- }
- }
- if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {
- dw = get_fattime();
- ST_DWORD(dir+DIR_CrtTime, dw);
- dir[DIR_Attr] = 0;
- ST_DWORD(dir+DIR_FileSize, 0);
- cl = ld_clust(dj.fs, dir);
- st_clust(dir, 0);
- dj.fs->wflag = 1;
- if (cl) {
- dw = dj.fs->winsect;
- res = remove_chain(dj.fs, cl);
- if (res == FR_OK) {
- dj.fs->last_clust = cl - 1;
- res = move_window(dj.fs, dw);
- }
- }
- }
- }
- else {
- if (res == FR_OK) {
- if (dir[DIR_Attr] & AM_DIR) {
- res = FR_NO_FILE;
- } else {
- if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO))
- res = FR_DENIED;
- }
- }
- }
- if (res == FR_OK) {
- if (mode & FA_CREATE_ALWAYS)
- mode |= FA__WRITTEN;
- fp->dir_sect = dj.fs->winsect;
- fp->dir_ptr = dir;
- #if _FS_LOCK
- fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
- if (!fp->lockid) res = FR_INT_ERR;
- #endif
- }
- #else
- if (res == FR_OK) {
- dir = dj.dir;
- if (!dir) {
- res = FR_INVALID_NAME;
- } else {
- if (dir[DIR_Attr] & AM_DIR)
- res = FR_NO_FILE;
- }
- }
- #endif
- FREE_BUF();
- if (res == FR_OK) {
- fp->flag = mode;
- fp->err = 0;
- fp->sclust = ld_clust(dj.fs, dir);
- fp->fsize = LD_DWORD(dir+DIR_FileSize);
- fp->fptr = 0;
- fp->dsect = 0;
- #if _USE_FASTSEEK
- fp->cltbl = 0;
- #endif
- fp->fs = dj.fs;
- fp->id = fp->fs->id;
- }
- }
- LEAVE_FF(dj.fs, res);
- }
- FRESULT f_read (
- FIL* fp,
- void* buff,
- UINT btr,
- UINT* br
- )
- {
- FRESULT res;
- DWORD clst, sect, remain;
- UINT rcnt, cc;
- BYTE csect, *rbuff = (BYTE*)buff;
- *br = 0;
- res = validate(fp);
- if (res != FR_OK) LEAVE_FF(fp->fs, res);
- if (fp->err)
- LEAVE_FF(fp->fs, (FRESULT)fp->err);
- if (!(fp->flag & FA_READ))
- LEAVE_FF(fp->fs, FR_DENIED);
- remain = fp->fsize - fp->fptr;
- if (btr > remain) btr = (UINT)remain;
- for ( ; btr;
- rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
- if ((fp->fptr % SS(fp->fs)) == 0) {
- csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));
- if (!csect) {
- if (fp->fptr == 0) {
- clst = fp->sclust;
- } else {
- #if _USE_FASTSEEK
- if (fp->cltbl)
- clst = clmt_clust(fp, fp->fptr);
- else
- #endif
- clst = get_fat(fp->fs, fp->clust);
- }
- if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
- if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
- fp->clust = clst;
- }
- sect = clust2sect(fp->fs, fp->clust);
- if (!sect) ABORT(fp->fs, FR_INT_ERR);
- sect += csect;
- cc = btr / SS(fp->fs);
- if (cc) {
- if (csect + cc > fp->fs->csize)
- cc = fp->fs->csize - csect;
- if (disk_read(fp->fs->drv, rbuff, sect, cc))
- ABORT(fp->fs, FR_DISK_ERR);
- #if !_FS_READONLY && _FS_MINIMIZE <= 2
- #if _FS_TINY
- if (fp->fs->wflag && fp->fs->winsect - sect < cc)
- mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
- #else
- if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
- mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
- #endif
- #endif
- rcnt = SS(fp->fs) * cc;
- continue;
- }
- #if !_FS_TINY
- if (fp->dsect != sect) {
- #if !_FS_READONLY
- if (fp->flag & FA__DIRTY) {
- if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- fp->flag &= ~FA__DIRTY;
- }
- #endif
- if (disk_read(fp->fs->drv, fp->buf, sect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- }
- #endif
- fp->dsect = sect;
- }
- rcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));
- if (rcnt > btr) rcnt = btr;
- #if _FS_TINY
- if (move_window(fp->fs, fp->dsect))
- ABORT(fp->fs, FR_DISK_ERR);
- mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt);
- #else
- mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt);
- #endif
- }
- LEAVE_FF(fp->fs, FR_OK);
- }
- #if !_FS_READONLY
- FRESULT f_write (
- FIL* fp,
- const void *buff,
- UINT btw,
- UINT* bw
- )
- {
- FRESULT res;
- DWORD clst, sect;
- UINT wcnt, cc;
- const BYTE *wbuff = (const BYTE*)buff;
- BYTE csect;
- *bw = 0;
- res = validate(fp);
- if (res != FR_OK) LEAVE_FF(fp->fs, res);
- if (fp->err)
- LEAVE_FF(fp->fs, (FRESULT)fp->err);
- if (!(fp->flag & FA_WRITE))
- LEAVE_FF(fp->fs, FR_DENIED);
- if (fp->fptr + btw < fp->fptr) btw = 0;
- for ( ; btw;
- wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
- if ((fp->fptr % SS(fp->fs)) == 0) {
- csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));
- if (!csect) {
- if (fp->fptr == 0) {
- clst = fp->sclust;
- if (clst == 0)
- clst = create_chain(fp->fs, 0);
- } else {
- #if _USE_FASTSEEK
- if (fp->cltbl)
- clst = clmt_clust(fp, fp->fptr);
- else
- #endif
- clst = create_chain(fp->fs, fp->clust);
- }
- if (clst == 0) break;
- if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
- if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
- fp->clust = clst;
- if (fp->sclust == 0) fp->sclust = clst;
- }
- #if _FS_TINY
- if (fp->fs->winsect == fp->dsect && sync_window(fp->fs))
- ABORT(fp->fs, FR_DISK_ERR);
- #else
- if (fp->flag & FA__DIRTY) {
- if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- fp->flag &= ~FA__DIRTY;
- }
- #endif
- sect = clust2sect(fp->fs, fp->clust);
- if (!sect) ABORT(fp->fs, FR_INT_ERR);
- sect += csect;
- cc = btw / SS(fp->fs);
- if (cc) {
- if (csect + cc > fp->fs->csize)
- cc = fp->fs->csize - csect;
- if (disk_write(fp->fs->drv, wbuff, sect, cc))
- ABORT(fp->fs, FR_DISK_ERR);
- #if _FS_MINIMIZE <= 2
- #if _FS_TINY
- if (fp->fs->winsect - sect < cc) {
- mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
- fp->fs->wflag = 0;
- }
- #else
- if (fp->dsect - sect < cc) {
- mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
- fp->flag &= ~FA__DIRTY;
- }
- #endif
- #endif
- wcnt = SS(fp->fs) * cc;
- continue;
- }
- #if _FS_TINY
- if (fp->fptr >= fp->fsize) {
- if (sync_window(fp->fs)) ABORT(fp->fs, FR_DISK_ERR);
- fp->fs->winsect = sect;
- }
- #else
- if (fp->dsect != sect) {
- if (fp->fptr < fp->fsize &&
- disk_read(fp->fs->drv, fp->buf, sect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- }
- #endif
- fp->dsect = sect;
- }
- wcnt = SS(fp->fs) - ((UINT)fp->fptr % SS(fp->fs));
- if (wcnt > btw) wcnt = btw;
- #if _FS_TINY
- if (move_window(fp->fs, fp->dsect))
- ABORT(fp->fs, FR_DISK_ERR);
- mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt);
- fp->fs->wflag = 1;
- #else
- mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt);
- fp->flag |= FA__DIRTY;
- #endif
- }
- if (fp->fptr > fp->fsize) fp->fsize = fp->fptr;
- fp->flag |= FA__WRITTEN;
- LEAVE_FF(fp->fs, FR_OK);
- }
- FRESULT f_sync (
- FIL* fp
- )
- {
- FRESULT res;
- DWORD tm;
- BYTE *dir;
- res = validate(fp);
- if (res == FR_OK) {
- if (fp->flag & FA__WRITTEN) {
-
- #if !_FS_TINY
- if (fp->flag & FA__DIRTY) {
- if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
- LEAVE_FF(fp->fs, FR_DISK_ERR);
- fp->flag &= ~FA__DIRTY;
- }
- #endif
-
- res = move_window(fp->fs, fp->dir_sect);
- if (res == FR_OK) {
- dir = fp->dir_ptr;
- dir[DIR_Attr] |= AM_ARC;
- ST_DWORD(dir+DIR_FileSize, fp->fsize);
- st_clust(dir, fp->sclust);
- tm = get_fattime();
- ST_DWORD(dir+DIR_WrtTime, tm);
- ST_WORD(dir+DIR_LstAccDate, 0);
- fp->flag &= ~FA__WRITTEN;
- fp->fs->wflag = 1;
- res = sync_fs(fp->fs);
- }
- }
- }
- LEAVE_FF(fp->fs, res);
- }
- #endif
- FRESULT f_close (
- FIL *fp
- )
- {
- FRESULT res;
- #if !_FS_READONLY
- res = f_sync(fp);
- if (res == FR_OK)
- #endif
- {
- res = validate(fp);
- if (res == FR_OK) {
- #if _FS_REENTRANT
- FATFS *fs = fp->fs;
- #endif
- #if _FS_LOCK
- res = dec_lock(fp->lockid);
- if (res == FR_OK)
- #endif
- fp->fs = 0;
- #if _FS_REENTRANT
- unlock_fs(fs, FR_OK);
- #endif
- }
- }
- return res;
- }
- #if _FS_RPATH >= 1
- #if _VOLUMES >= 2
- FRESULT f_chdrive (
- const TCHAR* path
- )
- {
- int vol;
- vol = get_ldnumber(&path);
- if (vol < 0) return FR_INVALID_DRIVE;
- CurrVol = (BYTE)vol;
- return FR_OK;
- }
- #endif
- FRESULT f_chdir (
- const TCHAR* path
- )
- {
- FRESULT res;
- DIR dj;
- DEF_NAMEBUF;
-
- res = find_volume(&dj.fs, &path, 0);
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- FREE_BUF();
- if (res == FR_OK) {
- if (!dj.dir) {
- dj.fs->cdir = dj.sclust;
- } else {
- if (dj.dir[DIR_Attr] & AM_DIR)
- dj.fs->cdir = ld_clust(dj.fs, dj.dir);
- else
- res = FR_NO_PATH;
- }
- }
- if (res == FR_NO_FILE) res = FR_NO_PATH;
- }
- LEAVE_FF(dj.fs, res);
- }
- #if _FS_RPATH >= 2
- FRESULT f_getcwd (
- TCHAR* buff,
- UINT len
- )
- {
- FRESULT res;
- DIR dj;
- UINT i, n;
- DWORD ccl;
- TCHAR *tp;
- FILINFO fno;
- DEF_NAMEBUF;
- *buff = 0;
-
- res = find_volume(&dj.fs, (const TCHAR**)&buff, 0);
- if (res == FR_OK) {
- INIT_BUF(dj);
- i = len;
- dj.sclust = dj.fs->cdir;
- while ((ccl = dj.sclust) != 0) {
- res = dir_sdi(&dj, 1);
- if (res != FR_OK) break;
- res = dir_read(&dj, 0);
- if (res != FR_OK) break;
- dj.sclust = ld_clust(dj.fs, dj.dir);
- res = dir_sdi(&dj, 0);
- if (res != FR_OK) break;
- do {
- res = dir_read(&dj, 0);
- if (res != FR_OK) break;
- if (ccl == ld_clust(dj.fs, dj.dir)) break;
- res = dir_next(&dj, 0);
- } while (res == FR_OK);
- if (res == FR_NO_FILE) res = FR_INT_ERR;
- if (res != FR_OK) break;
- #if _USE_LFN
- fno.lfname = buff;
- fno.lfsize = i;
- #endif
- get_fileinfo(&dj, &fno);
- tp = fno.fname;
- #if _USE_LFN
- if (*buff) tp = buff;
- #endif
- for (n = 0; tp[n]; n++) ;
- if (i < n + 3) {
- res = FR_NOT_ENOUGH_CORE; break;
- }
- while (n) buff[--i] = tp[--n];
- buff[--i] = '/';
- }
- tp = buff;
- if (res == FR_OK) {
- #if _VOLUMES >= 2
- *tp++ = '0' + CurrVol;
- *tp++ = ':';
- #endif
- if (i == len) {
- *tp++ = '/';
- } else {
- do
- *tp++ = buff[i++];
- while (i < len);
- }
- }
- *tp = 0;
- FREE_BUF();
- }
- LEAVE_FF(dj.fs, res);
- }
- #endif
- #endif
- #if _FS_MINIMIZE <= 2
- FRESULT f_lseek (
- FIL* fp,
- DWORD ofs
- )
- {
- FRESULT res;
- res = validate(fp);
- if (res != FR_OK) LEAVE_FF(fp->fs, res);
- if (fp->err)
- LEAVE_FF(fp->fs, (FRESULT)fp->err);
- #if _USE_FASTSEEK
- if (fp->cltbl) {
- DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;
- if (ofs == CREATE_LINKMAP) {
- tbl = fp->cltbl;
- tlen = *tbl++; ulen = 2;
- cl = fp->sclust;
- if (cl) {
- do {
-
- tcl = cl; ncl = 0; ulen += 2;
- do {
- pcl = cl; ncl++;
- cl = get_fat(fp->fs, cl);
- if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
- if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
- } while (cl == pcl + 1);
- if (ulen <= tlen) {
- *tbl++ = ncl; *tbl++ = tcl;
- }
- } while (cl < fp->fs->n_fatent);
- }
- *fp->cltbl = ulen;
- if (ulen <= tlen)
- *tbl = 0;
- else
- res = FR_NOT_ENOUGH_CORE;
- } else {
- if (ofs > fp->fsize)
- ofs = fp->fsize;
- fp->fptr = ofs;
- if (ofs) {
- fp->clust = clmt_clust(fp, ofs - 1);
- dsc = clust2sect(fp->fs, fp->clust);
- if (!dsc) ABORT(fp->fs, FR_INT_ERR);
- dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
- if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) {
- #if !_FS_TINY
- #if !_FS_READONLY
- if (fp->flag & FA__DIRTY) {
- if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- fp->flag &= ~FA__DIRTY;
- }
- #endif
- if (disk_read(fp->fs->drv, fp->buf, dsc, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- #endif
- fp->dsect = dsc;
- }
- }
- }
- } else
- #endif
-
- {
- DWORD clst, bcs, nsect, ifptr;
- if (ofs > fp->fsize
- #if !_FS_READONLY
- && !(fp->flag & FA_WRITE)
- #endif
- ) ofs = fp->fsize;
- ifptr = fp->fptr;
- fp->fptr = nsect = 0;
- if (ofs) {
- bcs = (DWORD)fp->fs->csize * SS(fp->fs);
- if (ifptr > 0 &&
- (ofs - 1) / bcs >= (ifptr - 1) / bcs) {
- fp->fptr = (ifptr - 1) & ~(bcs - 1);
- ofs -= fp->fptr;
- clst = fp->clust;
- } else {
- clst = fp->sclust;
- #if !_FS_READONLY
- if (clst == 0) {
- clst = create_chain(fp->fs, 0);
- if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
- if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
- fp->sclust = clst;
- }
- #endif
- fp->clust = clst;
- }
- if (clst != 0) {
- while (ofs > bcs) {
- #if !_FS_READONLY
- if (fp->flag & FA_WRITE) {
- clst = create_chain(fp->fs, clst);
- if (clst == 0) {
- ofs = bcs; break;
- }
- } else
- #endif
- clst = get_fat(fp->fs, clst);
- if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
- if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
- fp->clust = clst;
- fp->fptr += bcs;
- ofs -= bcs;
- }
- fp->fptr += ofs;
- if (ofs % SS(fp->fs)) {
- nsect = clust2sect(fp->fs, clst);
- if (!nsect) ABORT(fp->fs, FR_INT_ERR);
- nsect += ofs / SS(fp->fs);
- }
- }
- }
- if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) {
- #if !_FS_TINY
- #if !_FS_READONLY
- if (fp->flag & FA__DIRTY) {
- if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- fp->flag &= ~FA__DIRTY;
- }
- #endif
- if (disk_read(fp->fs->drv, fp->buf, nsect, 1))
- ABORT(fp->fs, FR_DISK_ERR);
- #endif
- fp->dsect = nsect;
- }
- #if !_FS_READONLY
- if (fp->fptr > fp->fsize) {
- fp->fsize = fp->fptr;
- fp->flag |= FA__WRITTEN;
- }
- #endif
- }
- LEAVE_FF(fp->fs, res);
- }
- #if _FS_MINIMIZE <= 1
- FRESULT f_opendir (
- DIR* dp,
- const TCHAR* path
- )
- {
- FRESULT res;
- FATFS* fs;
- DEF_NAMEBUF;
- if (!dp) return FR_INVALID_OBJECT;
-
- res = find_volume(&fs, &path, 0);
- if (res == FR_OK) {
- dp->fs = fs;
- INIT_BUF(*dp);
- res = follow_path(dp, path);
- FREE_BUF();
- if (res == FR_OK) {
- if (dp->dir) {
- if (dp->dir[DIR_Attr] & AM_DIR)
- dp->sclust = ld_clust(fs, dp->dir);
- else
- res = FR_NO_PATH;
- }
- if (res == FR_OK) {
- dp->id = fs->id;
- res = dir_sdi(dp, 0);
- #if _FS_LOCK
- if (res == FR_OK) {
- if (dp->sclust) {
- dp->lockid = inc_lock(dp, 0);
- if (!dp->lockid)
- res = FR_TOO_MANY_OPEN_FILES;
- } else {
- dp->lockid = 0;
- }
- }
- #endif
- }
- }
- if (res == FR_NO_FILE) res = FR_NO_PATH;
- }
- if (res != FR_OK) dp->fs = 0;
- LEAVE_FF(fs, res);
- }
- FRESULT f_closedir (
- DIR *dp
- )
- {
- FRESULT res;
- res = validate(dp);
- if (res == FR_OK) {
- #if _FS_REENTRANT
- FATFS *fs = dp->fs;
- #endif
- #if _FS_LOCK
- if (dp->lockid)
- res = dec_lock(dp->lockid);
- if (res == FR_OK)
- #endif
- dp->fs = 0;
- #if _FS_REENTRANT
- unlock_fs(fs, FR_OK);
- #endif
- }
- return res;
- }
- FRESULT f_readdir (
- DIR* dp,
- FILINFO* fno
- )
- {
- FRESULT res;
- DEF_NAMEBUF;
- res = validate(dp);
- if (res == FR_OK) {
- if (!fno) {
- res = dir_sdi(dp, 0);
- } else {
- INIT_BUF(*dp);
- res = dir_read(dp, 0);
- if (res == FR_NO_FILE) {
- dp->sect = 0;
- res = FR_OK;
- }
- if (res == FR_OK) {
- get_fileinfo(dp, fno);
- res = dir_next(dp, 0);
- if (res == FR_NO_FILE) {
- dp->sect = 0;
- res = FR_OK;
- }
- }
- FREE_BUF();
- }
- }
- LEAVE_FF(dp->fs, res);
- }
- #if _FS_MINIMIZE == 0
- FRESULT f_stat (
- const TCHAR* path,
- FILINFO* fno
- )
- {
- FRESULT res;
- DIR dj;
- DEF_NAMEBUF;
-
- res = find_volume(&dj.fs, &path, 0);
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- if (res == FR_OK) {
- if (dj.dir) {
- if (fno) get_fileinfo(&dj, fno);
- } else {
- res = FR_INVALID_NAME;
- }
- }
- FREE_BUF();
- }
- LEAVE_FF(dj.fs, res);
- }
- #if !_FS_READONLY
- FRESULT f_getfree (
- const TCHAR* path,
- DWORD* nclst,
- FATFS** fatfs
- )
- {
- FRESULT res;
- FATFS *fs;
- DWORD n, clst, sect, stat;
- UINT i;
- BYTE fat, *p;
-
- res = find_volume(fatfs, &path, 0);
- fs = *fatfs;
- if (res == FR_OK) {
-
- if (fs->free_clust <= fs->n_fatent - 2) {
- *nclst = fs->free_clust;
- } else {
-
- fat = fs->fs_type;
- n = 0;
- if (fat == FS_FAT12) {
- clst = 2;
- do {
- stat = get_fat(fs, clst);
- if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
- if (stat == 1) { res = FR_INT_ERR; break; }
- if (stat == 0) n++;
- } while (++clst < fs->n_fatent);
- } else {
- clst = fs->n_fatent;
- sect = fs->fatbase;
- i = 0; p = 0;
- do {
- if (!i) {
- res = move_window(fs, sect++);
- if (res != FR_OK) break;
- p = fs->win;
- i = SS(fs);
- }
- if (fat == FS_FAT16) {
- if (LD_WORD(p) == 0) n++;
- p += 2; i -= 2;
- } else {
- if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
- p += 4; i -= 4;
- }
- } while (--clst);
- }
- fs->free_clust = n;
- fs->fsi_flag |= 1;
- *nclst = n;
- }
- }
- LEAVE_FF(fs, res);
- }
- FRESULT f_truncate (
- FIL* fp
- )
- {
- FRESULT res;
- DWORD ncl;
- res = validate(fp);
- if (res == FR_OK) {
- if (fp->err) {
- res = (FRESULT)fp->err;
- } else {
- if (!(fp->flag & FA_WRITE))
- res = FR_DENIED;
- }
- }
- if (res == FR_OK) {
- if (fp->fsize > fp->fptr) {
- fp->fsize = fp->fptr;
- fp->flag |= FA__WRITTEN;
- if (fp->fptr == 0) {
- res = remove_chain(fp->fs, fp->sclust);
- fp->sclust = 0;
- } else {
- ncl = get_fat(fp->fs, fp->clust);
- res = FR_OK;
- if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
- if (ncl == 1) res = FR_INT_ERR;
- if (res == FR_OK && ncl < fp->fs->n_fatent) {
- res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
- if (res == FR_OK) res = remove_chain(fp->fs, ncl);
- }
- }
- #if !_FS_TINY
- if (res == FR_OK && (fp->flag & FA__DIRTY)) {
- if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1))
- res = FR_DISK_ERR;
- else
- fp->flag &= ~FA__DIRTY;
- }
- #endif
- }
- if (res != FR_OK) fp->err = (FRESULT)res;
- }
- LEAVE_FF(fp->fs, res);
- }
- FRESULT f_unlink (
- const TCHAR* path
- )
- {
- FRESULT res;
- DIR dj, sdj;
- BYTE *dir;
- DWORD dclst;
- DEF_NAMEBUF;
-
- res = find_volume(&dj.fs, &path, 1);
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
- res = FR_INVALID_NAME;
- #if _FS_LOCK
- if (res == FR_OK) res = chk_lock(&dj, 2);
- #endif
- if (res == FR_OK) {
- dir = dj.dir;
- if (!dir) {
- res = FR_INVALID_NAME;
- } else {
- if (dir[DIR_Attr] & AM_RDO)
- res = FR_DENIED;
- }
- dclst = ld_clust(dj.fs, dir);
- if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) {
- if (dclst < 2) {
- res = FR_INT_ERR;
- } else {
- mem_cpy(&sdj, &dj, sizeof (DIR));
- sdj.sclust = dclst;
- res = dir_sdi(&sdj, 2);
- if (res == FR_OK) {
- res = dir_read(&sdj, 0);
- if (res == FR_OK
- #if _FS_RPATH
- || dclst == dj.fs->cdir
- #endif
- ) res = FR_DENIED;
- if (res == FR_NO_FILE) res = FR_OK;
- }
- }
- }
- if (res == FR_OK) {
- res = dir_remove(&dj);
- if (res == FR_OK) {
- if (dclst)
- res = remove_chain(dj.fs, dclst);
- if (res == FR_OK) res = sync_fs(dj.fs);
- }
- }
- }
- FREE_BUF();
- }
- LEAVE_FF(dj.fs, res);
- }
- FRESULT f_mkdir (
- const TCHAR* path
- )
- {
- FRESULT res;
- DIR dj;
- BYTE *dir, n;
- DWORD dsc, dcl, pcl, tm = get_fattime();
- DEF_NAMEBUF;
-
- res = find_volume(&dj.fs, &path, 1);
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- if (res == FR_OK) res = FR_EXIST;
- if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
- res = FR_INVALID_NAME;
- if (res == FR_NO_FILE) {
- dcl = create_chain(dj.fs, 0);
- res = FR_OK;
- if (dcl == 0) res = FR_DENIED;
- if (dcl == 1) res = FR_INT_ERR;
- if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
- if (res == FR_OK)
- res = sync_window(dj.fs);
- if (res == FR_OK) {
- dsc = clust2sect(dj.fs, dcl);
- dir = dj.fs->win;
- mem_set(dir, 0, SS(dj.fs));
- mem_set(dir+DIR_Name, ' ', 11);
- dir[DIR_Name] = '.';
- dir[DIR_Attr] = AM_DIR;
- ST_DWORD(dir+DIR_WrtTime, tm);
- st_clust(dir, dcl);
- mem_cpy(dir+SZ_DIR, dir, SZ_DIR);
- dir[SZ_DIR+1] = '.'; pcl = dj.sclust;
- if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
- pcl = 0;
- st_clust(dir+SZ_DIR, pcl);
- for (n = dj.fs->csize; n; n--) {
- dj.fs->winsect = dsc++;
- dj.fs->wflag = 1;
- res = sync_window(dj.fs);
- if (res != FR_OK) break;
- mem_set(dir, 0, SS(dj.fs));
- }
- }
- if (res == FR_OK) res = dir_register(&dj);
- if (res != FR_OK) {
- remove_chain(dj.fs, dcl);
- } else {
- dir = dj.dir;
- dir[DIR_Attr] = AM_DIR;
- ST_DWORD(dir+DIR_WrtTime, tm);
- st_clust(dir, dcl);
- dj.fs->wflag = 1;
- res = sync_fs(dj.fs);
- }
- }
- FREE_BUF();
- }
- LEAVE_FF(dj.fs, res);
- }
- FRESULT f_chmod (
- const TCHAR* path,
- BYTE value,
- BYTE mask
- )
- {
- FRESULT res;
- DIR dj;
- BYTE *dir;
- DEF_NAMEBUF;
-
- res = find_volume(&dj.fs, &path, 1);
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- FREE_BUF();
- if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
- res = FR_INVALID_NAME;
- if (res == FR_OK) {
- dir = dj.dir;
- if (!dir) {
- res = FR_INVALID_NAME;
- } else {
- mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC;
- dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask);
- dj.fs->wflag = 1;
- res = sync_fs(dj.fs);
- }
- }
- }
- LEAVE_FF(dj.fs, res);
- }
- FRESULT f_utime (
- const TCHAR* path,
- const FILINFO* fno
- )
- {
- FRESULT res;
- DIR dj;
- BYTE *dir;
- DEF_NAMEBUF;
-
- res = find_volume(&dj.fs, &path, 1);
- if (res == FR_OK) {
- INIT_BUF(dj);
- res = follow_path(&dj, path);
- FREE_BUF();
- if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
- res = FR_INVALID_NAME;
- if (res == FR_OK) {
- dir = dj.dir;
- if (!dir) {
- res = FR_INVALID_NAME;
- } else {
- ST_WORD(dir+DIR_WrtTime, fno->ftime);
- ST_WORD(dir+DIR_WrtDate, fno->fdate);
- dj.fs->wflag = 1;
- res = sync_fs(dj.fs);
- }
- }
- }
- LEAVE_FF(dj.fs, res);
- }
- FRESULT f_rename (
- const TCHAR* path_old,
- const TCHAR* path_new
- )
- {
- FRESULT res;
- DIR djo, djn;
- BYTE buf[21], *dir;
- DWORD dw;
- DEF_NAMEBUF;
-
- res = find_volume(&djo.fs, &path_old, 1);
- if (res == FR_OK) {
- djn.fs = djo.fs;
- INIT_BUF(djo);
- res = follow_path(&djo, path_old);
- if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))
- res = FR_INVALID_NAME;
- #if _FS_LOCK
- if (res == FR_OK) res = chk_lock(&djo, 2);
- #endif
- if (res == FR_OK) {
- if (!djo.dir) {
- res = FR_NO_FILE;
- } else {
- mem_cpy(buf, djo.dir+DIR_Attr, 21);
- mem_cpy(&djn, &djo, sizeof (DIR));
- if (get_ldnumber(&path_new) >= 0)
- res = follow_path(&djn, path_new);
- else
- res = FR_INVALID_DRIVE;
- if (res == FR_OK) res = FR_EXIST;
- if (res == FR_NO_FILE) {
-
- res = dir_register(&djn);
- if (res == FR_OK) {
- dir = djn.dir;
- mem_cpy(dir+13, buf+2, 19);
- dir[DIR_Attr] = buf[0] | AM_ARC;
- djo.fs->wflag = 1;
- if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) {
- dw = clust2sect(djo.fs, ld_clust(djo.fs, dir));
- if (!dw) {
- res = FR_INT_ERR;
- } else {
- res = move_window(djo.fs, dw);
- dir = djo.fs->win+SZ_DIR;
- if (res == FR_OK && dir[1] == '.') {
- dw = (djo.fs->fs_type == FS_FAT32 && djn.sclust == djo.fs->dirbase) ? 0 : djn.sclust;
- st_clust(dir, dw);
- djo.fs->wflag = 1;
- }
- }
- }
- if (res == FR_OK) {
- res = dir_remove(&djo);
- if (res == FR_OK)
- res = sync_fs(djo.fs);
- }
- }
-
- }
- }
- }
- FREE_BUF();
- }
- LEAVE_FF(djo.fs, res);
- }
- #endif
- #endif
- #endif
- #endif
- #if _USE_LABEL
- FRESULT f_getlabel (
- const TCHAR* path,
- TCHAR* label,
- DWORD* vsn
- )
- {
- FRESULT res;
- DIR dj;
- UINT i, j;
-
- res = find_volume(&dj.fs, &path, 0);
-
- if (res == FR_OK && label) {
- dj.sclust = 0;
- res = dir_sdi(&dj, 0);
- if (res == FR_OK) {
- res = dir_read(&dj, 1);
- if (res == FR_OK) {
- #if _USE_LFN && _LFN_UNICODE
- WCHAR w;
- i = j = 0;
- do {
- w = (i < 11) ? dj.dir[i++] : ' ';
- if (IsDBCS1(w) && i < 11 && IsDBCS2(dj.dir[i]))
- w = w << 8 | dj.dir[i++];
- label[j++] = ff_convert(w, 1);
- } while (j < 11);
- #else
- mem_cpy(label, dj.dir, 11);
- #endif
- j = 11;
- do {
- label[j] = 0;
- if (!j) break;
- } while (label[--j] == ' ');
- }
- if (res == FR_NO_FILE) {
- label[0] = 0;
- res = FR_OK;
- }
- }
- }
-
- if (res == FR_OK && vsn) {
- res = move_window(dj.fs, dj.fs->volbase);
- if (res == FR_OK) {
- i = dj.fs->fs_type == FS_FAT32 ? BS_VolID32 : BS_VolID;
- *vsn = LD_DWORD(&dj.fs->win[i]);
- }
- }
- LEAVE_FF(dj.fs, res);
- }
- #if !_FS_READONLY
- FRESULT f_setlabel (
- const TCHAR* label
- )
- {
- FRESULT res;
- DIR dj;
- BYTE vn[11];
- UINT i, j, sl;
- WCHAR w;
- DWORD tm;
-
- res = find_volume(&dj.fs, &label, 1);
- if (res) LEAVE_FF(dj.fs, res);
-
- vn[0] = 0;
- for (sl = 0; label[sl]; sl++) ;
- for ( ; sl && label[sl-1] == ' '; sl--) ;
- if (sl) {
- i = j = 0;
- do {
- #if _USE_LFN && _LFN_UNICODE
- w = ff_convert(ff_wtoupper(label[i++]), 0);
- #else
- w = (BYTE)label[i++];
- if (IsDBCS1(w))
- w = (j < 10 && i < sl && IsDBCS2(label[i])) ? w << 8 | (BYTE)label[i++] : 0;
- #if _USE_LFN
- w = ff_convert(ff_wtoupper(ff_convert(w, 1)), 0);
- #else
- if (IsLower(w)) w -= 0x20;
- #ifdef _EXCVT
- if (w >= 0x80) w = ExCvt[w - 0x80];
- #else
- if (!_DF1S && w >= 0x80) w = 0;
- #endif
- #endif
- #endif
- if (!w || chk_chr("\"*+,.:;<=>\?[]|\x7F", w) || j >= (UINT)((w >= 0x100) ? 10 : 11))
- LEAVE_FF(dj.fs, FR_INVALID_NAME);
- if (w >= 0x100) vn[j++] = (BYTE)(w >> 8);
- vn[j++] = (BYTE)w;
- } while (i < sl);
- while (j < 11) vn[j++] = ' ';
- }
-
- dj.sclust = 0;
- res = dir_sdi(&dj, 0);
- if (res == FR_OK) {
- res = dir_read(&dj, 1);
- if (res == FR_OK) {
- if (vn[0]) {
- mem_cpy(dj.dir, vn, 11);
- tm = get_fattime();
- ST_DWORD(dj.dir+DIR_WrtTime, tm);
- } else {
- dj.dir[0] = DDE;
- }
- dj.fs->wflag = 1;
- res = sync_fs(dj.fs);
- } else {
- if (res == FR_NO_FILE) {
- res = FR_OK;
- if (vn[0]) {
- res = dir_alloc(&dj, 1);
- if (res == FR_OK) {
- mem_set(dj.dir, 0, SZ_DIR);
- mem_cpy(dj.dir, vn, 11);
- dj.dir[DIR_Attr] = AM_VOL;
- tm = get_fattime();
- ST_DWORD(dj.dir+DIR_WrtTime, tm);
- dj.fs->wflag = 1;
- res = sync_fs(dj.fs);
- }
- }
- }
- }
- }
- LEAVE_FF(dj.fs, res);
- }
- #endif
- #endif
- #if _USE_FORWARD && _FS_TINY
- FRESULT f_forward (
- FIL* fp,
- UINT (*func)(const BYTE*,UINT),
- UINT btf,
- UINT* bf
- )
- {
- FRESULT res;
- DWORD remain, clst, sect;
- UINT rcnt;
- BYTE csect;
- *bf = 0;
- res = validate(fp);
- if (res != FR_OK) LEAVE_FF(fp->fs, res);
- if (fp->err)
- LEAVE_FF(fp->fs, (FRESULT)fp->err);
- if (!(fp->flag & FA_READ))
- LEAVE_FF(fp->fs, FR_DENIED);
- remain = fp->fsize - fp->fptr;
- if (btf > remain) btf = (UINT)remain;
- for ( ; btf && (*func)(0, 0);
- fp->fptr += rcnt, *bf += rcnt, btf -= rcnt) {
- csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));
- if ((fp->fptr % SS(fp->fs)) == 0) {
- if (!csect) {
- clst = (fp->fptr == 0) ?
- fp->sclust : get_fat(fp->fs, fp->clust);
- if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
- if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
- fp->clust = clst;
- }
- }
- sect = clust2sect(fp->fs, fp->clust);
- if (!sect) ABORT(fp->fs, FR_INT_ERR);
- sect += csect;
- if (move_window(fp->fs, sect))
- ABORT(fp->fs, FR_DISK_ERR);
- fp->dsect = sect;
- rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs));
- if (rcnt > btf) rcnt = btf;
- rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
- if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
- }
- LEAVE_FF(fp->fs, FR_OK);
- }
- #endif
- #if _USE_MKFS && !_FS_READONLY
- #define N_ROOTDIR 512
- #define N_FATS 1
- FRESULT f_mkfs (
- const TCHAR* path,
- BYTE sfd,
- UINT au
- )
- {
- static const WORD vst[] = { 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 0};
- static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
- int vol;
- BYTE fmt, md, sys, *tbl, pdrv, part;
- DWORD n_clst, vs, n, wsect;
- UINT i;
- DWORD b_vol, b_fat, b_dir, b_data;
- DWORD n_vol, n_rsv, n_fat, n_dir;
- FATFS *fs;
- DSTATUS stat;
-
- vol = get_ldnumber(&path);
- if (vol < 0) return FR_INVALID_DRIVE;
- if (sfd > 1) return FR_INVALID_PARAMETER;
- if (au & (au - 1)) return FR_INVALID_PARAMETER;
- fs = FatFs[vol];
- if (!fs) return FR_NOT_ENABLED;
- fs->fs_type = 0;
- pdrv = LD2PD(vol);
- part = LD2PT(vol);
-
- stat = disk_initialize(pdrv);
- if (stat & STA_NOINIT) return FR_NOT_READY;
- if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
- #if _MAX_SS != _MIN_SS
- if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS || SS(fs) < _MIN_SS)
- return FR_DISK_ERR;
- #endif
- if (_MULTI_PARTITION && part) {
-
- if (disk_read(pdrv, fs->win, 0, 1)) return FR_DISK_ERR;
- if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
- tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
- if (!tbl[4]) return FR_MKFS_ABORTED;
- b_vol = LD_DWORD(tbl+8);
- n_vol = LD_DWORD(tbl+12);
- } else {
-
- if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
- return FR_DISK_ERR;
- b_vol = (sfd) ? 0 : 63;
- n_vol -= b_vol;
- }
- if (!au) {
- vs = n_vol / (2000 / (SS(fs) / 512));
- for (i = 0; vs < vst[i]; i++) ;
- au = cst[i];
- }
- au /= SS(fs);
- if (au == 0) au = 1;
- if (au > 128) au = 128;
-
- n_clst = n_vol / au;
- fmt = FS_FAT12;
- if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
- if (n_clst >= MIN_FAT32) fmt = FS_FAT32;
-
- if (fmt == FS_FAT32) {
- n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
- n_rsv = 32;
- n_dir = 0;
- } else {
- n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
- n_fat = (n_fat + SS(fs) - 1) / SS(fs);
- n_rsv = 1;
- n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
- }
- b_fat = b_vol + n_rsv;
- b_dir = b_fat + n_fat * N_FATS;
- b_data = b_dir + n_dir;
- if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED;
-
- if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
- n = (b_data + n - 1) & ~(n - 1);
- n = (n - b_data) / N_FATS;
- if (fmt == FS_FAT32) {
- n_rsv += n;
- b_fat += n;
- } else {
- n_fat += n;
- }
-
- n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
- if ( (fmt == FS_FAT16 && n_clst < MIN_FAT16)
- || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
- return FR_MKFS_ABORTED;
-
- if (fmt == FS_FAT32) {
- sys = 0x0C;
- } else {
- if (fmt == FS_FAT12 && n_vol < 0x10000) {
- sys = 0x01;
- } else {
- sys = (n_vol < 0x10000) ? 0x04 : 0x06;
- }
- }
- if (_MULTI_PARTITION && part) {
-
- tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
- tbl[4] = sys;
- if (disk_write(pdrv, fs->win, 0, 1))
- return FR_DISK_ERR;
- md = 0xF8;
- } else {
- if (sfd) {
- md = 0xF0;
- } else {
- mem_set(fs->win, 0, SS(fs));
- tbl = fs->win+MBR_Table;
- tbl[1] = 1;
- tbl[2] = 1;
- tbl[3] = 0;
- tbl[4] = sys;
- tbl[5] = 254;
- n = (b_vol + n_vol) / 63 / 255;
- tbl[6] = (BYTE)(n >> 2 | 63);
- tbl[7] = (BYTE)n;
- ST_DWORD(tbl+8, 63);
- ST_DWORD(tbl+12, n_vol);
- ST_WORD(fs->win+BS_55AA, 0xAA55);
- if (disk_write(pdrv, fs->win, 0, 1))
- return FR_DISK_ERR;
- md = 0xF8;
- }
- }
-
- tbl = fs->win;
- mem_set(tbl, 0, SS(fs));
- mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);
- i = SS(fs);
- ST_WORD(tbl+BPB_BytsPerSec, i);
- tbl[BPB_SecPerClus] = (BYTE)au;
- ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv);
- tbl[BPB_NumFATs] = N_FATS;
- i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR;
- ST_WORD(tbl+BPB_RootEntCnt, i);
- if (n_vol < 0x10000) {
- ST_WORD(tbl+BPB_TotSec16, n_vol);
- } else {
- ST_DWORD(tbl+BPB_TotSec32, n_vol);
- }
- tbl[BPB_Media] = md;
- ST_WORD(tbl+BPB_SecPerTrk, 63);
- ST_WORD(tbl+BPB_NumHeads, 255);
- ST_DWORD(tbl+BPB_HiddSec, b_vol);
- n = get_fattime();
- if (fmt == FS_FAT32) {
- ST_DWORD(tbl+BS_VolID32, n);
- ST_DWORD(tbl+BPB_FATSz32, n_fat);
- ST_DWORD(tbl+BPB_RootClus, 2);
- ST_WORD(tbl+BPB_FSInfo, 1);
- ST_WORD(tbl+BPB_BkBootSec, 6);
- tbl[BS_DrvNum32] = 0x80;
- tbl[BS_BootSig32] = 0x29;
- mem_cpy(tbl+BS_VolLab32, "NO NAME " "FAT32 ", 19);
- } else {
- ST_DWORD(tbl+BS_VolID, n);
- ST_WORD(tbl+BPB_FATSz16, n_fat);
- tbl[BS_DrvNum] = 0x80;
- tbl[BS_BootSig] = 0x29;
- mem_cpy(tbl+BS_VolLab, "NO NAME " "FAT ", 19);
- }
- ST_WORD(tbl+BS_55AA, 0xAA55);
- if (disk_write(pdrv, tbl, b_vol, 1))
- return FR_DISK_ERR;
- if (fmt == FS_FAT32)
- disk_write(pdrv, tbl, b_vol + 6, 1);
-
- wsect = b_fat;
- for (i = 0; i < N_FATS; i++) {
- mem_set(tbl, 0, SS(fs));
- n = md;
- if (fmt != FS_FAT32) {
- n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
- ST_DWORD(tbl+0, n);
- } else {
- n |= 0xFFFFFF00;
- ST_DWORD(tbl+0, n);
- ST_DWORD(tbl+4, 0xFFFFFFFF);
- ST_DWORD(tbl+8, 0x0FFFFFFF);
- }
- if (disk_write(pdrv, tbl, wsect++, 1))
- return FR_DISK_ERR;
- mem_set(tbl, 0, SS(fs));
- for (n = 1; n < n_fat; n++) {
- if (disk_write(pdrv, tbl, wsect++, 1))
- return FR_DISK_ERR;
- }
- }
-
- i = (fmt == FS_FAT32) ? au : (UINT)n_dir;
- do {
- if (disk_write(pdrv, tbl, wsect++, 1))
- return FR_DISK_ERR;
- } while (--i);
- #if _USE_ERASE
- {
- DWORD eb[2];
- eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
- disk_ioctl(pdrv, CTRL_ERASE_SECTOR, eb);
- }
- #endif
-
- if (fmt == FS_FAT32) {
- ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
- ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
- ST_DWORD(tbl+FSI_Free_Count, n_clst - 1);
- ST_DWORD(tbl+FSI_Nxt_Free, 2);
- ST_WORD(tbl+BS_55AA, 0xAA55);
- disk_write(pdrv, tbl, b_vol + 1, 1);
- disk_write(pdrv, tbl, b_vol + 7, 1);
- }
- return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
- }
- #if _MULTI_PARTITION
- FRESULT f_fdisk (
- BYTE pdrv,
- const DWORD szt[],
- void* work
- )
- {
- UINT i, n, sz_cyl, tot_cyl, b_cyl, e_cyl, p_cyl;
- BYTE s_hd, e_hd, *p, *buf = (BYTE*)work;
- DSTATUS stat;
- DWORD sz_disk, sz_part, s_part;
- stat = disk_initialize(pdrv);
- if (stat & STA_NOINIT) return FR_NOT_READY;
- if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
- if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_disk)) return FR_DISK_ERR;
-
- for (n = 16; n < 256 && sz_disk / n / 63 > 1024; n *= 2) ;
- if (n == 256) n--;
- e_hd = n - 1;
- sz_cyl = 63 * n;
- tot_cyl = sz_disk / sz_cyl;
-
- mem_set(buf, 0, _MAX_SS);
- p = buf + MBR_Table; b_cyl = 0;
- for (i = 0; i < 4; i++, p += SZ_PTE) {
- p_cyl = (szt[i] <= 100U) ? (DWORD)tot_cyl * szt[i] / 100 : szt[i] / sz_cyl;
- if (!p_cyl) continue;
- s_part = (DWORD)sz_cyl * b_cyl;
- sz_part = (DWORD)sz_cyl * p_cyl;
- if (i == 0) {
- s_hd = 1;
- s_part += 63; sz_part -= 63;
- } else {
- s_hd = 0;
- }
- e_cyl = b_cyl + p_cyl - 1;
- if (e_cyl >= tot_cyl) return FR_INVALID_PARAMETER;
-
- p[1] = s_hd;
- p[2] = (BYTE)((b_cyl >> 2) + 1);
- p[3] = (BYTE)b_cyl;
- p[4] = 0x06;
- p[5] = e_hd;
- p[6] = (BYTE)((e_cyl >> 2) + 63);
- p[7] = (BYTE)e_cyl;
- ST_DWORD(p + 8, s_part);
- ST_DWORD(p + 12, sz_part);
-
- b_cyl += p_cyl;
- }
- ST_WORD(p, 0xAA55);
-
- return (disk_write(pdrv, buf, 0, 1) || disk_ioctl(pdrv, CTRL_SYNC, 0)) ? FR_DISK_ERR : FR_OK;
- }
- #endif
- #endif
- #if _USE_STRFUNC
- TCHAR* f_gets (
- TCHAR* buff,
- int len,
- FIL* fp
- )
- {
- int n = 0;
- TCHAR c, *p = buff;
- BYTE s[2];
- UINT rc;
- while (n < len - 1) {
- #if _USE_LFN && _LFN_UNICODE
- #if _STRF_ENCODE == 3
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
- c = s[0];
- if (c >= 0x80) {
- if (c < 0xC0) continue;
- if (c < 0xE0) {
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
- c = (c & 0x1F) << 6 | (s[0] & 0x3F);
- if (c < 0x80) c = '?';
- } else {
- if (c < 0xF0) {
- f_read(fp, s, 2, &rc);
- if (rc != 2) break;
- c = c << 12 | (s[0] & 0x3F) << 6 | (s[1] & 0x3F);
- if (c < 0x800) c = '?';
- } else {
- c = '?';
- }
- }
- }
- #elif _STRF_ENCODE == 2
- f_read(fp, s, 2, &rc);
- if (rc != 2) break;
- c = s[1] + (s[0] << 8);
- #elif _STRF_ENCODE == 1
- f_read(fp, s, 2, &rc);
- if (rc != 2) break;
- c = s[0] + (s[1] << 8);
- #else
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
- c = s[0];
- if (IsDBCS1(c)) {
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
- c = (c << 8) + s[0];
- }
- c = ff_convert(c, 1);
- if (!c) c = '?';
- #endif
- #else
- f_read(fp, s, 1, &rc);
- if (rc != 1) break;
- c = s[0];
- #endif
- if (_USE_STRFUNC == 2 && c == '\r') continue;
- *p++ = c;
- n++;
- if (c == '\n') break;
- }
- *p = 0;
- return n ? buff : 0;
- }
- #if !_FS_READONLY
- #include <stdarg.h>
- typedef struct {
- FIL* fp;
- int idx, nchr;
- BYTE buf[64];
- } putbuff;
- static
- void putc_bfd (
- putbuff* pb,
- TCHAR c
- )
- {
- UINT bw;
- int i;
- if (_USE_STRFUNC == 2 && c == '\n')
- putc_bfd(pb, '\r');
- i = pb->idx;
- if (i < 0) return;
- #if _USE_LFN && _LFN_UNICODE
- #if _STRF_ENCODE == 3
- if (c < 0x80) {
- pb->buf[i++] = (BYTE)c;
- } else {
- if (c < 0x800) {
- pb->buf[i++] = (BYTE)(0xC0 | c >> 6);
- } else {
- pb->buf[i++] = (BYTE)(0xE0 | c >> 12);
- pb->buf[i++] = (BYTE)(0x80 | (c >> 6 & 0x3F));
- }
- pb->buf[i++] = (BYTE)(0x80 | (c & 0x3F));
- }
- #elif _STRF_ENCODE == 2
- pb->buf[i++] = (BYTE)(c >> 8);
- pb->buf[i++] = (BYTE)c;
- #elif _STRF_ENCODE == 1
- pb->buf[i++] = (BYTE)c;
- pb->buf[i++] = (BYTE)(c >> 8);
- #else
- c = ff_convert(c, 0);
- if (!c) c = '?';
- if (c >= 0x100)
- pb->buf[i++] = (BYTE)(c >> 8);
- pb->buf[i++] = (BYTE)c;
- #endif
- #else
- pb->buf[i++] = (BYTE)c;
- #endif
- if (i >= (int)(sizeof pb->buf) - 3) {
- f_write(pb->fp, pb->buf, (UINT)i, &bw);
- i = (bw == (UINT)i) ? 0 : -1;
- }
- pb->idx = i;
- pb->nchr++;
- }
- int f_putc (
- TCHAR c,
- FIL* fp
- )
- {
- putbuff pb;
- UINT nw;
- pb.fp = fp;
- pb.nchr = pb.idx = 0;
- putc_bfd(&pb, c);
- if ( pb.idx >= 0
- && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
- && (UINT)pb.idx == nw) return pb.nchr;
- return EOF;
- }
- int f_puts (
- const TCHAR* str,
- FIL* fp
- )
- {
- putbuff pb;
- UINT nw;
- pb.fp = fp;
- pb.nchr = pb.idx = 0;
- while (*str)
- putc_bfd(&pb, *str++);
- if ( pb.idx >= 0
- && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
- && (UINT)pb.idx == nw) return pb.nchr;
- return EOF;
- }
- int f_printf (
- FIL* fp,
- const TCHAR* fmt,
- ...
- )
- {
- va_list arp;
- BYTE f, r;
- UINT nw, i, j, w;
- DWORD v;
- TCHAR c, d, s[16], *p;
- putbuff pb;
- pb.fp = fp;
- pb.nchr = pb.idx = 0;
- va_start(arp, fmt);
- for (;;) {
- c = *fmt++;
- if (c == 0) break;
- if (c != '%') {
- putc_bfd(&pb, c);
- continue;
- }
- w = f = 0;
- c = *fmt++;
- if (c == '0') {
- f = 1; c = *fmt++;
- } else {
- if (c == '-') {
- f = 2; c = *fmt++;
- }
- }
- while (IsDigit(c)) {
- w = w * 10 + c - '0';
- c = *fmt++;
- }
- if (c == 'l' || c == 'L') {
- f |= 4; c = *fmt++;
- }
- if (!c) break;
- d = c;
- if (IsLower(d)) d -= 0x20;
- switch (d) {
- case 'S' :
- p = va_arg(arp, TCHAR*);
- for (j = 0; p[j]; j++) ;
- if (!(f & 2)) {
- while (j++ < w) putc_bfd(&pb, ' ');
- }
- while (*p) putc_bfd(&pb, *p++);
- while (j++ < w) putc_bfd(&pb, ' ');
- continue;
- case 'C' :
- putc_bfd(&pb, (TCHAR)va_arg(arp, int)); continue;
- case 'B' :
- r = 2; break;
- case 'O' :
- r = 8; break;
- case 'D' :
- case 'U' :
- r = 10; break;
- case 'X' :
- r = 16; break;
- default:
- putc_bfd(&pb, c); continue;
- }
-
- v = (f & 4) ? (DWORD)va_arg(arp, long) : ((d == 'D') ? (DWORD)(long)va_arg(arp, int) : (DWORD)va_arg(arp, unsigned int));
- if (d == 'D' && (v & 0x80000000)) {
- v = 0 - v;
- f |= 8;
- }
- i = 0;
- do {
- d = (TCHAR)(v % r); v /= r;
- if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
- s[i++] = d + '0';
- } while (v && i < sizeof s / sizeof s[0]);
- if (f & 8) s[i++] = '-';
- j = i; d = (f & 1) ? '0' : ' ';
- while (!(f & 2) && j++ < w) putc_bfd(&pb, d);
- do putc_bfd(&pb, s[--i]); while (i);
- while (j++ < w) putc_bfd(&pb, d);
- }
- va_end(arp);
- if ( pb.idx >= 0
- && f_write(pb.fp, pb.buf, (UINT)pb.idx, &nw) == FR_OK
- && (UINT)pb.idx == nw) return pb.nchr;
- return EOF;
- }
- #endif
- #endif
|