dossubs.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <auth.h>
  13. #include <fcall.h>
  14. #include "iotrack.h"
  15. #include "dat.h"
  16. #include "fns.h"
  17. static uint8_t isdos[256];
  18. int
  19. isdosfs(uint8_t *buf)
  20. {
  21. /*
  22. * When dynamic disc managers move the disc partition,
  23. * they make it start with 0xE9.
  24. */
  25. if(buf[0] == 0xE9)
  26. return 1;
  27. /*
  28. * Check if the jump displacement (magic[1]) is too short for a FAT.
  29. *
  30. * check now omitted due to digital cameras that use a 0 jump.
  31. * the ecma-107 standard says this is okay and that interoperable fat
  32. * implementations shouldn't assume this:
  33. * http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-107.pdf,
  34. * page 11.
  35. */
  36. if(buf[0] == 0xEB && buf[2] == 0x90 /* && buf[1] >= 0x30 */)
  37. return 1;
  38. if(chatty)
  39. fprint(2, "bad sig %.2ux %.2ux %.2uxn", buf[0], buf[1], buf[2]);
  40. return 0;
  41. }
  42. int
  43. dosfs(Xfs *xf)
  44. {
  45. Iosect *p, *p1;
  46. Dosboot *b;
  47. Fatinfo *fi;
  48. Dosboot32 *b32;
  49. Dosbpb *bp;
  50. int32_t fisec, extflags;
  51. int i;
  52. if(!isdos['a']){
  53. for(i = 'a'; i <= 'z'; i++)
  54. isdos[i] = 1;
  55. for(i = 'A'; i <= 'Z'; i++)
  56. isdos[i] = 1;
  57. for(i = '0'; i <= '9'; i++)
  58. isdos[i] = 1;
  59. isdos['$'] = 1;
  60. isdos['%'] = 1;
  61. isdos['\''] = 1;
  62. isdos['-'] = 1;
  63. isdos['_'] = 1;
  64. isdos['@'] = 1;
  65. isdos['~'] = 1;
  66. isdos['`'] = 1;
  67. isdos['!'] = 1;
  68. isdos['('] = 1;
  69. isdos[')'] = 1;
  70. isdos['{'] = 1;
  71. isdos['}'] = 1;
  72. isdos['^'] = 1;
  73. isdos['#'] = 1;
  74. isdos['&'] = 1;
  75. }
  76. p = getsect(xf, 0);
  77. if(p == 0)
  78. return -1;
  79. b = (Dosboot*)p->iobuf;
  80. if(b->clustsize == 0 || isdosfs(p->iobuf) == 0){
  81. putsect(p);
  82. return -1;
  83. }
  84. bp = malloc(sizeof(Dosbpb));
  85. memset(bp, 0, sizeof(Dosbpb)); /* clear lock */
  86. xf->ptr = bp;
  87. xf->fmt = 1;
  88. bp->sectsize = GSHORT(b->sectsize);
  89. bp->clustsize = b->clustsize;
  90. bp->nresrv = GSHORT(b->nresrv);
  91. bp->nfats = b->nfats;
  92. bp->rootsize = GSHORT(b->rootsize);
  93. bp->volsize = GSHORT(b->volsize);
  94. if(bp->volsize == 0)
  95. bp->volsize = GLONG(b->bigvolsize);
  96. bp->mediadesc = b->mediadesc;
  97. bp->fatsize = GSHORT(b->fatsize);
  98. bp->fataddr = GSHORT(b->nresrv);
  99. bp->fatinfo = 0;
  100. if(bp->fatsize == 0){ /* is FAT32 */
  101. if(chatty)
  102. bootsecdump32(2, xf, (Dosboot32*)b);
  103. xf->isfat32 = 1;
  104. b32 = (Dosboot32*)b;
  105. bp->fatsize = GLONG(b32->fatsize32);
  106. if(bp->fatsize == 0){
  107. putsect(p);
  108. if(chatty)
  109. fprint(2, "fatsize 0\n");
  110. return -1;
  111. }
  112. bp->dataaddr = bp->fataddr + bp->nfats*bp->fatsize;
  113. bp->rootaddr = 0;
  114. bp->rootstart = GLONG(b32->rootstart);
  115. /*
  116. * disable fat mirroring?
  117. */
  118. extflags = GSHORT(b32->extflags);
  119. if(extflags & 0x0080){
  120. for(i = 0; i < 4; i++){
  121. if(extflags & (1 << i)){
  122. bp->fataddr += i * bp->fatsize;
  123. bp->nfats = 1;
  124. break;
  125. }
  126. }
  127. }
  128. /*
  129. * fat free list info
  130. */
  131. bp->freeptr = FATRESRV;
  132. fisec = GSHORT(b32->infospec);
  133. if(fisec != 0 && fisec < GSHORT(b32->nresrv)){
  134. p1 = getsect(xf, fisec);
  135. if(p1 != nil){
  136. fi = (Fatinfo*)p1->iobuf;
  137. if(GLONG(fi->sig1) == FATINFOSIG1 && GLONG(fi->sig) == FATINFOSIG){
  138. bp->fatinfo = fisec;
  139. bp->freeptr = GLONG(fi->nextfree);
  140. bp->freeclusters = GLONG(fi->freeclust);
  141. chat("fat info: %ld free clusters, next free %ld\n", bp->freeclusters, bp->freeptr);
  142. }
  143. putsect(p1);
  144. }
  145. }
  146. }else{
  147. if(chatty)
  148. bootdump(2, b);
  149. bp->rootaddr = bp->fataddr + bp->nfats*bp->fatsize;
  150. bp->rootstart = 0;
  151. i = bp->rootsize*DOSDIRSIZE + bp->sectsize-1;
  152. i /= bp->sectsize;
  153. bp->dataaddr = bp->rootaddr + i;
  154. bp->freeptr = FATRESRV;
  155. }
  156. bp->fatclusters = FATRESRV+(bp->volsize - bp->dataaddr)/bp->clustsize;
  157. if(xf->isfat32)
  158. bp->fatbits = 32;
  159. else if(bp->fatclusters < 4087)
  160. bp->fatbits = 12;
  161. else
  162. bp->fatbits = 16;
  163. chat("fatbits=%d (%d clusters)...", bp->fatbits, bp->fatclusters);
  164. for(i=0; i<b->nfats; i++)
  165. chat("fat %d: %ld...", i, bp->fataddr+i*bp->fatsize);
  166. chat("root: %ld...", bp->rootaddr);
  167. chat("data: %ld...", bp->dataaddr);
  168. putsect(p);
  169. return 0;
  170. }
  171. /*
  172. * initialize f to the root directory
  173. * this file has no Dosdir entry,
  174. * so we special case it all over.
  175. */
  176. void
  177. rootfile(Xfile *f)
  178. {
  179. Dosptr *dp;
  180. dp = f->ptr;
  181. memset(dp, 0, sizeof(Dosptr));
  182. dp->prevaddr = -1;
  183. }
  184. int
  185. isroot(uint32_t addr)
  186. {
  187. return addr == 0;
  188. }
  189. int
  190. getfile(Xfile *f)
  191. {
  192. Dosptr *dp;
  193. Iosect *p;
  194. dp = f->ptr;
  195. if(dp->p)
  196. panic("getfile");
  197. p = getsect(f->xf, dp->addr);
  198. if(p == nil)
  199. return -1;
  200. /*
  201. * we could also make up a Dosdir for the root
  202. */
  203. dp->d = nil;
  204. if(!isroot(dp->addr)){
  205. if(f->qid.path != QIDPATH(dp)){
  206. chat("qid mismatch f=%#llux d=%#lux...", f->qid.path, QIDPATH(dp));
  207. putsect(p);
  208. errno = Enonexist;
  209. return -1;
  210. }
  211. dp->d = (Dosdir *)&p->iobuf[dp->offset];
  212. }
  213. dp->p = p;
  214. return 0;
  215. }
  216. void
  217. putfile(Xfile *f)
  218. {
  219. Dosptr *dp;
  220. dp = f->ptr;
  221. if(!dp->p)
  222. panic("putfile");
  223. putsect(dp->p);
  224. dp->p = nil;
  225. dp->d = nil;
  226. }
  227. int32_t
  228. getstart(Xfs *xf, Dosdir *d)
  229. {
  230. int32_t start;
  231. start = GSHORT(d->start);
  232. if(xf->isfat32)
  233. start |= GSHORT(d->hstart)<<16;
  234. return start;
  235. }
  236. void
  237. putstart(Xfs *xf, Dosdir *d, int32_t start)
  238. {
  239. PSHORT(d->start, start);
  240. if(xf->isfat32)
  241. PSHORT(d->hstart, start>>16);
  242. }
  243. /*
  244. * return the disk cluster for the iclust cluster in f
  245. */
  246. int32_t
  247. fileclust(Xfile *f, int32_t iclust, int cflag)
  248. {
  249. Dosbpb *bp;
  250. Dosptr *dp;
  251. Dosdir *d;
  252. int32_t start, clust, nskip, next;
  253. bp = f->xf->ptr;
  254. dp = f->ptr;
  255. d = dp->d;
  256. next = 0;
  257. /*
  258. * asking for the cluster of the root directory
  259. * is not a well-formed question, since the root directory
  260. * does not begin on a cluster boundary.
  261. */
  262. if(!f->xf->isfat32 && isroot(dp->addr))
  263. return -1;
  264. if(f->xf->isfat32 && isroot(dp->addr)){
  265. start = bp->rootstart;
  266. }else{
  267. start = getstart(f->xf, d);
  268. if(start == 0){
  269. if(!cflag)
  270. return -1;
  271. mlock(&bp->MLock);
  272. start = falloc(f->xf);
  273. unmlock(&bp->MLock);
  274. if(start <= 0)
  275. return -1;
  276. puttime(d, 0);
  277. putstart(f->xf, d, start);
  278. dp->p->flags |= BMOD;
  279. dp->clust = 0;
  280. }
  281. }
  282. if(dp->clust == 0 || iclust < dp->iclust){
  283. clust = start;
  284. nskip = iclust;
  285. }else{
  286. clust = dp->clust;
  287. nskip = iclust - dp->iclust;
  288. }
  289. if(chatty > 1 && nskip > 0)
  290. chat("clust %#lx, skip %ld...", clust, nskip);
  291. if(clust <= 0)
  292. return -1;
  293. if(nskip > 0){
  294. mlock(&bp->MLock);
  295. while(--nskip >= 0){
  296. next = getfat(f->xf, clust);
  297. if(chatty > 1)
  298. chat("->%#lx", next);
  299. if(next > 0){
  300. clust = next;
  301. continue;
  302. }else if(!cflag)
  303. break;
  304. if(d && (d->attr&DSYSTEM)){
  305. next = cfalloc(f);
  306. if(next < 0)
  307. break;
  308. /* cfalloc will call putfat for us, since clust may change */
  309. } else {
  310. next = falloc(f->xf);
  311. if(next < 0)
  312. break;
  313. putfat(f->xf, clust, next);
  314. }
  315. clust = next;
  316. }
  317. unmlock(&bp->MLock);
  318. if(next <= 0)
  319. return -1;
  320. dp->clust = clust;
  321. dp->iclust = iclust;
  322. }
  323. if(chatty > 1)
  324. chat(" clust(%#lx)=%#lx...", iclust, clust);
  325. return clust;
  326. }
  327. /*
  328. * return the disk sector for the isect disk sector in f
  329. */
  330. int32_t
  331. fileaddr(Xfile *f, int32_t isect, int cflag)
  332. {
  333. Dosbpb *bp;
  334. Dosptr *dp;
  335. int32_t clust;
  336. bp = f->xf->ptr;
  337. dp = f->ptr;
  338. if(!f->xf->isfat32 && isroot(dp->addr)){
  339. if(isect*bp->sectsize >= bp->rootsize*DOSDIRSIZE)
  340. return -1;
  341. return bp->rootaddr + isect;
  342. }
  343. clust = fileclust(f, isect/bp->clustsize, cflag);
  344. if(clust < 0)
  345. return -1;
  346. return clust2sect(bp, clust) + isect%bp->clustsize;
  347. }
  348. /*
  349. * translate names
  350. */
  351. void
  352. fixname(char *buf)
  353. {
  354. int c;
  355. char *p;
  356. p = buf;
  357. while(c = *p){
  358. if(c == ':' && trspaces)
  359. *p = ' ';
  360. p++;
  361. }
  362. }
  363. /*
  364. * classify the file name as one of
  365. * Invalid - contains a bad character
  366. * Short - short valid 8.3 name, no lowercase letters
  367. * ShortLower - short valid 8.3 name except for lowercase letters
  368. * Long - long name
  369. */
  370. int
  371. classifyname(char *buf)
  372. {
  373. char *p, *dot;
  374. int c, isextended, is8dot3, islower, ndot;
  375. p = buf;
  376. isextended = 0;
  377. islower = 0;
  378. dot = nil;
  379. ndot = 0;
  380. while(c = (uint8_t)*p){
  381. if(c&0x80) /* UTF8 */
  382. isextended = 1;
  383. else if(c == '.'){
  384. dot = p;
  385. ndot++;
  386. }else if(strchr("+,:;=[] ", c))
  387. isextended = 1;
  388. else if(!isdos[c])
  389. return Invalid;
  390. if('a' <= c && c <= 'z')
  391. islower = 1;
  392. p++;
  393. }
  394. is8dot3 = (ndot==0 && p-buf <= 8) || (ndot==1 && dot-buf <= 8 && p-(dot+1) <= 3);
  395. if(!isextended && is8dot3){
  396. if(islower)
  397. return ShortLower;
  398. return Short;
  399. }
  400. return Long;
  401. }
  402. /*
  403. * make an alias for a valid long file name
  404. */
  405. void
  406. mkalias(char *name, char *sname, int id)
  407. {
  408. Rune r;
  409. char *s, *e, sid[10];
  410. int i, esuf, v;
  411. e = strrchr(name, '.');
  412. if(e == nil)
  413. e = strchr(name, '\0');
  414. s = name;
  415. i = 0;
  416. while(s < e && i < 6){
  417. if(isdos[(uint8_t)*s])
  418. sname[i++] = *s++;
  419. else
  420. s += chartorune(&r, s);
  421. }
  422. v = snprint(sid, 10, "%d", id);
  423. if(i + 1 + v > 8)
  424. i = 8 - 1 - v;
  425. sname[i++] = '~';
  426. strcpy(&sname[i], sid);
  427. i += v;
  428. sname[i++] = '.';
  429. esuf = i + 3;
  430. if(esuf > 12)
  431. panic("bad mkalias");
  432. while(*e && i < esuf){
  433. if(isdos[(uint8_t)*e])
  434. sname[i++] = *e++;
  435. else
  436. e += chartorune(&r, e);
  437. }
  438. if(sname[i-1] == '.')
  439. i--;
  440. sname[i] = '\0';
  441. }
  442. /*
  443. * check for valid plan 9 names,
  444. * rewrite ' ' to ':'
  445. */
  446. char isfrog[256]={
  447. /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
  448. /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
  449. /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
  450. /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
  451. /* [' '] 1, let's try this -rsc */
  452. ['/'] 1,
  453. [0x7f] 1,
  454. };
  455. int
  456. nameok(char *elem)
  457. {
  458. while(*elem) {
  459. if(*elem == ' ' && trspaces)
  460. *elem = ':';
  461. if(isfrog[*(uint8_t*)elem])
  462. return 0;
  463. elem++;
  464. }
  465. return 1;
  466. }
  467. /*
  468. * look for a directory entry matching name
  469. * always searches for long names which match a short name
  470. */
  471. int
  472. searchdir(Xfile *f, char *name, Dosptr *dp, int cflag, int longtype)
  473. {
  474. Xfs *xf;
  475. Iosect *p;
  476. Dosbpb *bp;
  477. Dosdir *d;
  478. char buf[261], *bname;
  479. int isect, addr, o, addr1, addr2, prevaddr, prevaddr1, o1, islong, have, need, sum;
  480. xf = f->xf;
  481. bp = xf->ptr;
  482. addr1 = -1;
  483. addr2 = -1;
  484. prevaddr1 = -1;
  485. o1 = 0;
  486. islong = 0;
  487. sum = -1;
  488. need = 1;
  489. if(longtype!=Short && cflag)
  490. need += (utflen(name) + DOSRUNE-1) / DOSRUNE;
  491. memset(dp, 0, sizeof(Dosptr));
  492. dp->prevaddr = -1;
  493. dp->naddr = -1;
  494. dp->paddr = ((Dosptr *)f->ptr)->addr;
  495. dp->poffset = ((Dosptr *)f->ptr)->offset;
  496. have = 0;
  497. addr = -1;
  498. bname = nil;
  499. for(isect=0;; isect++){
  500. prevaddr = addr;
  501. addr = fileaddr(f, isect, cflag);
  502. if(addr < 0)
  503. break;
  504. p = getsect(xf, addr);
  505. if(p == 0)
  506. break;
  507. for(o=0; o<bp->sectsize; o+=DOSDIRSIZE){
  508. d = (Dosdir *)&p->iobuf[o];
  509. if(d->name[0] == 0x00){
  510. chat("end dir(0)...");
  511. putsect(p);
  512. if(!cflag)
  513. return -1;
  514. /*
  515. * addr1 & o1 are the start of the dirs
  516. * addr2 is the optional second cluster used if the long name
  517. * entry does not fit within the addr1 cluster
  518. *
  519. * have tells us the number of contiguous free dirs
  520. * starting at addr1.o1; need are necessary to hold the long name.
  521. */
  522. if(addr1 < 0){
  523. addr1 = addr;
  524. prevaddr1 = prevaddr;
  525. o1 = o;
  526. }
  527. if(addr2 < 0 && (bp->sectsize-o)/DOSDIRSIZE + have < need){
  528. addr2 = fileaddr(f, isect+1, cflag);
  529. if(addr2 < 0)
  530. goto breakout;
  531. }else if(addr2 < 0)
  532. addr2 = addr;
  533. if(addr2 == addr1)
  534. addr2 = -1;
  535. dp->addr = addr1;
  536. dp->offset = o1;
  537. dp->prevaddr = prevaddr1;
  538. dp->naddr = addr2;
  539. return 0;
  540. }
  541. if(d->name[0] == DOSEMPTY){
  542. if(chatty)
  543. fprint(2, "empty dir\n");
  544. have++;
  545. if(addr1 == -1){
  546. addr1 = addr;
  547. o1 = o;
  548. prevaddr1 = prevaddr;
  549. }
  550. if(addr2 == -1 && have >= need)
  551. addr2 = addr;
  552. continue;
  553. }
  554. have = 0;
  555. if(addr2 == -1)
  556. addr1 = -1;
  557. dirdump(d);
  558. if((d->attr & 0xf) == 0xf){
  559. bname = getnamesect(buf, bname, p->iobuf + o, &islong, &sum, 1);
  560. continue;
  561. }
  562. if(d->attr & DVLABEL){
  563. islong = 0;
  564. continue;
  565. }
  566. if(islong != 1 || sum != aliassum(d) || cistrcmp(bname, name) != 0){
  567. bname = buf;
  568. getname(buf, d);
  569. }
  570. islong = 0;
  571. if(cistrcmp(bname, name) != 0)
  572. continue;
  573. if(chatty)
  574. fprint(2, "found\n");
  575. if(cflag){
  576. putsect(p);
  577. return -1;
  578. }
  579. dp->addr = addr;
  580. dp->prevaddr = prevaddr;
  581. dp->offset = o;
  582. dp->p = p;
  583. dp->d = d;
  584. return 0;
  585. }
  586. putsect(p);
  587. }
  588. breakout:
  589. chat("end dir(1)...");
  590. return -1;
  591. }
  592. int
  593. emptydir(Xfile *f)
  594. {
  595. Xfs *xf = f->xf;
  596. Dosbpb *bp = xf->ptr;
  597. int isect, addr, o;
  598. Iosect *p;
  599. Dosdir *d;
  600. for(isect=0;; isect++){
  601. addr = fileaddr(f, isect, 0);
  602. if(addr < 0)
  603. break;
  604. p = getsect(xf, addr);
  605. if(p == 0)
  606. return -1;
  607. for(o=0; o<bp->sectsize; o+=DOSDIRSIZE){
  608. d = (Dosdir *)&p->iobuf[o];
  609. if(d->name[0] == 0x00){
  610. putsect(p);
  611. return 0;
  612. }
  613. if(d->name[0] == DOSEMPTY)
  614. continue;
  615. if(d->name[0] == '.')
  616. continue;
  617. if(d->attr&DVLABEL)
  618. continue;
  619. putsect(p);
  620. return -1;
  621. }
  622. putsect(p);
  623. }
  624. return 0;
  625. }
  626. int32_t
  627. readdir(Xfile *f, void *vbuf, int32_t offset, int32_t count)
  628. {
  629. Xfs *xf;
  630. Dosbpb *bp;
  631. Dir dir;
  632. int isect, addr, o, islong, sum;
  633. Iosect *p;
  634. Dosdir *d;
  635. int32_t rcnt, n;
  636. char *name, snamebuf[8+1+3+1], namebuf[DOSNAMELEN];
  637. uint8_t *buf;
  638. buf = vbuf;
  639. rcnt = 0;
  640. xf = f->xf;
  641. bp = xf->ptr;
  642. if(count <= 0)
  643. return 0;
  644. islong = 0;
  645. sum = -1;
  646. name = nil;
  647. for(isect=0;; isect++){
  648. addr = fileaddr(f, isect, 0);
  649. if(addr < 0)
  650. break;
  651. p = getsect(xf, addr);
  652. if(p == 0)
  653. return -1;
  654. for(o=0; o<bp->sectsize; o+=DOSDIRSIZE){
  655. d = (Dosdir *)&p->iobuf[o];
  656. if(d->name[0] == 0x00){
  657. putsect(p);
  658. return rcnt;
  659. }
  660. if(d->name[0] == DOSEMPTY)
  661. continue;
  662. dirdump(d);
  663. if(d->name[0] == '.'){
  664. if(d->name[1] == ' ' || d->name[1] == 0)
  665. continue;
  666. if(d->name[1] == '.' &&
  667. (d->name[2] == ' ' || d->name[2] == 0))
  668. continue;
  669. }
  670. if((d->attr & 0xf) == 0xf){
  671. name = getnamesect(namebuf, name, p->iobuf+o, &islong, &sum, 1);
  672. continue;
  673. }
  674. if(d->attr & DVLABEL){
  675. islong = 0;
  676. continue;
  677. }
  678. dir.name = snamebuf;
  679. getdir(xf, &dir, d, addr, o);
  680. if(islong == 1 && nameok(name) && sum == aliassum(d))
  681. dir.name = name;
  682. islong = 0;
  683. n = convD2M(&dir, &buf[rcnt], count - rcnt);
  684. name = nil;
  685. if(n <= BIT16SZ){ /* no room for next entry */
  686. putsect(p);
  687. return rcnt;
  688. }
  689. rcnt += n;
  690. if(offset > 0){
  691. offset -= rcnt;
  692. rcnt = 0;
  693. islong = 0;
  694. continue;
  695. }
  696. if(rcnt == count){
  697. putsect(p);
  698. return rcnt;
  699. }
  700. }
  701. putsect(p);
  702. }
  703. return rcnt;
  704. }
  705. /*
  706. * set up ndp for a directory's parent
  707. * the hardest part is setting up paddr
  708. */
  709. int
  710. walkup(Xfile *f, Dosptr *ndp)
  711. {
  712. Dosbpb *bp;
  713. Dosptr *dp;
  714. Dosdir *xd;
  715. Iosect *p;
  716. int32_t k, o, so, start, pstart, ppstart, st, ppclust;
  717. bp = f->xf->ptr;
  718. dp = f->ptr;
  719. memset(ndp, 0, sizeof(Dosptr));
  720. ndp->prevaddr = -1;
  721. ndp->naddr = -1;
  722. ndp->addr = dp->paddr;
  723. ndp->offset = dp->poffset;
  724. chat("walkup: paddr=%#lx...", dp->paddr);
  725. /*
  726. * root's paddr is always itself
  727. */
  728. if(isroot(dp->paddr))
  729. return 0;
  730. /*
  731. * find the start of our parent's directory
  732. */
  733. p = getsect(f->xf, dp->paddr);
  734. if(p == nil)
  735. goto error;
  736. xd = (Dosdir *)&p->iobuf[dp->poffset];
  737. dirdump(xd);
  738. start = getstart(f->xf, xd);
  739. chat("start=%#lx...", start);
  740. if(start == 0)
  741. goto error;
  742. putsect(p);
  743. /*
  744. * verify that parent's . points to itself
  745. */
  746. p = getsect(f->xf, clust2sect(bp, start));
  747. if(p == nil)
  748. goto error;
  749. xd = (Dosdir *)p->iobuf;
  750. dirdump(xd);
  751. st = getstart(f->xf, xd);
  752. if(xd->name[0]!='.' || xd->name[1]!=' ' || start!=st)
  753. goto error;
  754. /*
  755. * parent's .. is the next entry, and has start of parent's parent
  756. */
  757. xd++;
  758. dirdump(xd);
  759. if(xd->name[0] != '.' || xd->name[1] != '.')
  760. goto error;
  761. pstart = getstart(f->xf, xd);
  762. putsect(p);
  763. /*
  764. * we're done if parent is root
  765. */
  766. if(pstart == 0 || f->xf->isfat32 && pstart == bp->rootstart)
  767. return 0;
  768. /*
  769. * verify that parent's . points to itself
  770. */
  771. p = getsect(f->xf, clust2sect(bp, pstart));
  772. if(p == 0){
  773. chat("getsect %ld failed\n", pstart);
  774. goto error;
  775. }
  776. xd = (Dosdir *)p->iobuf;
  777. dirdump(xd);
  778. st = getstart(f->xf, xd);
  779. if(xd->name[0]!='.' || xd->name[1]!=' ' || pstart!=st)
  780. goto error;
  781. /*
  782. * parent's parent's .. is the next entry, and has start of parent's parent's parent
  783. */
  784. xd++;
  785. dirdump(xd);
  786. if(xd->name[0] != '.' || xd->name[1] != '.')
  787. goto error;
  788. ppstart = getstart(f->xf, xd);
  789. putsect(p);
  790. /*
  791. * open parent's parent's parent, and walk through it until parent's parent is found
  792. * need this to find parent's parent's addr and offset
  793. */
  794. ppclust = ppstart;
  795. if(f->xf->isfat32 && ppclust == 0){
  796. ppclust = bp->rootstart;
  797. chat("ppclust 0, resetting to rootstart\n");
  798. }
  799. k = ppclust ? clust2sect(bp, ppclust) : bp->rootaddr;
  800. p = getsect(f->xf, k);
  801. if(p == nil){
  802. chat("getsect %ld failed\n", k);
  803. goto error;
  804. }
  805. xd = (Dosdir *)p->iobuf;
  806. dirdump(xd);
  807. if(ppstart){
  808. st = getstart(f->xf, xd);
  809. if(xd->name[0]!='.' || xd->name[1]!=' ' || ppstart!=st)
  810. goto error;
  811. }
  812. for(so=1;; so++){
  813. for(o=0; o<bp->sectsize; o+=DOSDIRSIZE){
  814. xd = (Dosdir *)&p->iobuf[o];
  815. if(xd->name[0] == 0x00){
  816. chat("end dir\n");
  817. goto error;
  818. }
  819. if(xd->name[0] == DOSEMPTY)
  820. continue;
  821. st = getstart(f->xf, xd);
  822. if(st == pstart)
  823. goto out;
  824. }
  825. if(ppclust){
  826. if(so%bp->clustsize == 0){
  827. mlock(&bp->MLock);
  828. ppclust = getfat(f->xf, ppclust);
  829. unmlock(&bp->MLock);
  830. if(ppclust < 0){
  831. chat("getfat %ld failed\n", ppclust);
  832. goto error;
  833. }
  834. }
  835. k = clust2sect(bp, ppclust) +
  836. so%bp->clustsize;
  837. }else{
  838. if(so*bp->sectsize >= bp->rootsize*DOSDIRSIZE)
  839. goto error;
  840. k = bp->rootaddr + so;
  841. }
  842. putsect(p);
  843. p = getsect(f->xf, k);
  844. if(p == 0){
  845. chat("getsect %ld failed\n", k);
  846. goto error;
  847. }
  848. }
  849. out:
  850. putsect(p);
  851. ndp->paddr = k;
  852. ndp->poffset = o;
  853. return 0;
  854. error:
  855. if(p)
  856. putsect(p);
  857. return -1;
  858. }
  859. int32_t
  860. readfile(Xfile *f, void *vbuf, int32_t offset, int32_t count)
  861. {
  862. Xfs *xf = f->xf;
  863. Dosbpb *bp = xf->ptr;
  864. Dosptr *dp = f->ptr;
  865. Dosdir *d = dp->d;
  866. int isect, addr, o, c;
  867. Iosect *p;
  868. uint8_t *buf;
  869. int32_t length, rcnt;
  870. rcnt = 0;
  871. length = GLONG(d->length);
  872. buf = vbuf;
  873. if(offset >= length)
  874. return 0;
  875. if(offset+count >= length)
  876. count = length - offset;
  877. isect = offset/bp->sectsize;
  878. o = offset%bp->sectsize;
  879. while(count > 0){
  880. addr = fileaddr(f, isect++, 0);
  881. if(addr < 0)
  882. break;
  883. c = bp->sectsize - o;
  884. if(c > count)
  885. c = count;
  886. p = getsect(xf, addr);
  887. if(p == 0)
  888. return -1;
  889. memmove(&buf[rcnt], &p->iobuf[o], c);
  890. putsect(p);
  891. count -= c;
  892. rcnt += c;
  893. o = 0;
  894. }
  895. return rcnt;
  896. }
  897. int32_t
  898. writefile(Xfile *f, void *vbuf, int32_t offset, int32_t count)
  899. {
  900. Xfs *xf = f->xf;
  901. Dosbpb *bp = xf->ptr;
  902. Dosptr *dp = f->ptr;
  903. Dosdir *d = dp->d;
  904. int isect, addr = 0, o, c;
  905. Iosect *p;
  906. uint8_t *buf;
  907. int32_t length, rcnt = 0, dlen;
  908. buf = vbuf;
  909. isect = offset/bp->sectsize;
  910. o = offset%bp->sectsize;
  911. while(count > 0){
  912. addr = fileaddr(f, isect++, 1);
  913. if(addr < 0)
  914. break;
  915. c = bp->sectsize - o;
  916. if(c > count)
  917. c = count;
  918. if(c == bp->sectsize){
  919. p = getosect(xf, addr);
  920. p->flags = 0;
  921. }else{
  922. p = getsect(xf, addr);
  923. if(p == nil)
  924. return -1;
  925. }
  926. memmove(&p->iobuf[o], &buf[rcnt], c);
  927. p->flags |= BMOD;
  928. putsect(p);
  929. count -= c;
  930. rcnt += c;
  931. o = 0;
  932. }
  933. if(rcnt <= 0 && addr < 0)
  934. return -1;
  935. length = 0;
  936. dlen = GLONG(d->length);
  937. if(rcnt > 0)
  938. length = offset+rcnt;
  939. else if(dp->addr && dp->clust){
  940. c = bp->clustsize*bp->sectsize;
  941. if(dp->iclust > (dlen+c-1)/c)
  942. length = c*dp->iclust;
  943. }
  944. if(length > dlen)
  945. PLONG(d->length, length);
  946. puttime(d, 0);
  947. dp->p->flags |= BMOD;
  948. return rcnt;
  949. }
  950. int
  951. truncfile(Xfile *f, int32_t length)
  952. {
  953. Xfs *xf = f->xf;
  954. Dosbpb *bp = xf->ptr;
  955. Dosptr *dp = f->ptr;
  956. Dosdir *d = dp->d;
  957. int32_t clust, next, n;
  958. mlock(&bp->MLock);
  959. clust = getstart(f->xf, d);
  960. n = length;
  961. if(n <= 0)
  962. putstart(f->xf, d, 0);
  963. else
  964. n -= bp->sectsize;
  965. while(clust > 0){
  966. next = getfat(xf, clust);
  967. if(n <= 0)
  968. putfat(xf, clust, 0);
  969. else
  970. n -= bp->clustsize*bp->sectsize;
  971. clust = next;
  972. }
  973. unmlock(&bp->MLock);
  974. PLONG(d->length, length);
  975. dp->iclust = 0;
  976. dp->clust = 0;
  977. dp->p->flags |= BMOD;
  978. return 0;
  979. }
  980. void
  981. putdir(Dosdir *d, Dir *dp)
  982. {
  983. if(dp->mode != ~0){
  984. if(dp->mode & 2)
  985. d->attr &= ~DRONLY;
  986. else
  987. d->attr |= DRONLY;
  988. if(dp->mode & DMEXCL)
  989. d->attr |= DSYSTEM;
  990. else
  991. d->attr &= ~DSYSTEM;
  992. }
  993. if(dp->mtime != ~0)
  994. puttime(d, dp->mtime);
  995. }
  996. /*
  997. * should extend this to deal with
  998. * creation and access dates
  999. */
  1000. void
  1001. getdir(Xfs *xfs, Dir *dp, Dosdir *d, int addr, int offset)
  1002. {
  1003. if(d == nil || addr == 0)
  1004. panic("getdir on root");
  1005. dp->type = 0;
  1006. dp->dev = 0;
  1007. getname(dp->name, d);
  1008. dp->qid.path = addr*(Sectorsize/DOSDIRSIZE) +
  1009. offset/DOSDIRSIZE;
  1010. dp->qid.vers = 0;
  1011. if(d->attr & DRONLY)
  1012. dp->mode = 0444;
  1013. else
  1014. dp->mode = 0666;
  1015. dp->atime = gtime(d);
  1016. dp->mtime = dp->atime;
  1017. dp->qid.type = QTFILE;
  1018. if(d->attr & DDIR){
  1019. dp->qid.type = QTDIR;
  1020. dp->mode |= DMDIR|0111;
  1021. dp->length = 0;
  1022. }else
  1023. dp->length = GLONG(d->length);
  1024. if(d->attr & DSYSTEM){
  1025. dp->mode |= DMEXCL;
  1026. if(iscontig(xfs, d))
  1027. dp->mode |= DMAPPEND;
  1028. }
  1029. dp->uid = "bill";
  1030. dp->muid = "bill";
  1031. dp->gid = "trog";
  1032. }
  1033. void
  1034. getname(char *p, Dosdir *d)
  1035. {
  1036. int c, i;
  1037. for(i=0; i<8; i++){
  1038. c = d->name[i];
  1039. if(c == '\0' || c == ' ')
  1040. break;
  1041. if(i == 0 && c == 0x05)
  1042. c = 0xe5;
  1043. *p++ = c;
  1044. }
  1045. for(i=0; i<3; i++){
  1046. c = d->ext[i];
  1047. if(c == '\0' || c == ' ')
  1048. break;
  1049. if(i == 0)
  1050. *p++ = '.';
  1051. *p++ = c;
  1052. }
  1053. *p = 0;
  1054. }
  1055. static char*
  1056. getnamerunes(char *dst, uint8_t *buf, int step)
  1057. {
  1058. int i;
  1059. Rune r;
  1060. char dbuf[DOSRUNE * UTFmax + 1], *d;
  1061. d = dbuf;
  1062. r = 1;
  1063. for(i = 1; r && i < 11; i += 2){
  1064. r = buf[i] | (buf[i+1] << 8);
  1065. d += runetochar(d, &r);
  1066. }
  1067. for(i = 14; r && i < 26; i += 2){
  1068. r = buf[i] | (buf[i+1] << 8);
  1069. d += runetochar(d, &r);
  1070. }
  1071. for(i = 28; r && i < 32; i += 2){
  1072. r = buf[i] | (buf[i+1] << 8);
  1073. d += runetochar(d, &r);
  1074. }
  1075. if(step == 1)
  1076. dst -= d - dbuf;
  1077. memmove(dst, dbuf, d - dbuf);
  1078. if(step == -1){
  1079. dst += d - dbuf;
  1080. *dst = '\0';
  1081. }
  1082. return dst;
  1083. }
  1084. char*
  1085. getnamesect(char *dbuf, char *d, uint8_t *buf, int *islong, int *sum,
  1086. int step)
  1087. {
  1088. /*
  1089. * validation checks to make sure we're
  1090. * making up a consistent name
  1091. */
  1092. if(buf[11] != 0xf || buf[12] != 0){
  1093. *islong = 0;
  1094. return nil;
  1095. }
  1096. if(step == 1){
  1097. if((buf[0] & 0xc0) == 0x40){
  1098. *islong = buf[0] & 0x3f;
  1099. *sum = buf[13];
  1100. d = dbuf + DOSNAMELEN;
  1101. *--d = '\0';
  1102. }else if(*islong && *islong == buf[0] + 1 && *sum == buf[13]){
  1103. *islong = buf[0];
  1104. }else{
  1105. *islong = 0;
  1106. return nil;
  1107. }
  1108. }else{
  1109. if(*islong + 1 == (buf[0] & 0xbf) && *sum == buf[13]){
  1110. *islong = buf[0] & 0x3f;
  1111. if(buf[0] & 0x40)
  1112. *sum = -1;
  1113. }else{
  1114. *islong = 0;
  1115. *sum = -1;
  1116. return nil;
  1117. }
  1118. }
  1119. if(*islong > 20){
  1120. *islong = 0;
  1121. *sum = -1;
  1122. return nil;
  1123. }
  1124. return getnamerunes(d, buf, step);
  1125. }
  1126. void
  1127. putname(char *p, Dosdir *d)
  1128. {
  1129. int i;
  1130. memset(d->name, ' ', sizeof d->name+sizeof d->ext);
  1131. for(i=0; i<sizeof d->name; i++){
  1132. if(*p == 0 || *p == '.')
  1133. break;
  1134. d->name[i] = toupper(*p++);
  1135. }
  1136. p = strrchr(p, '.');
  1137. if(p){
  1138. for(i=0; i<sizeof d->ext; i++){
  1139. if(*++p == 0)
  1140. break;
  1141. d->ext[i] = toupper(*p);
  1142. }
  1143. }
  1144. }
  1145. static void
  1146. putnamesect(uint8_t *slot, Rune *longname, int curslot, int first, int sum)
  1147. {
  1148. Rune r;
  1149. Dosdir ds;
  1150. int i, j;
  1151. memset(&ds, 0xff, sizeof ds);
  1152. ds.attr = 0xf;
  1153. ds.reserved[0] = 0;
  1154. ds.reserved[1] = sum;
  1155. ds.start[0] = 0;
  1156. ds.start[1] = 0;
  1157. if(first)
  1158. ds.name[0] = 0x40 | curslot;
  1159. else
  1160. ds.name[0] = curslot;
  1161. memmove(slot, &ds, sizeof ds);
  1162. j = (curslot-1) * DOSRUNE;
  1163. for(i = 1; i < 11; i += 2){
  1164. r = longname[j++];
  1165. slot[i] = r;
  1166. slot[i+1] = r >> 8;
  1167. if(r == 0)
  1168. return;
  1169. }
  1170. for(i = 14; i < 26; i += 2){
  1171. r = longname[j++];
  1172. slot[i] = r;
  1173. slot[i+1] = r >> 8;
  1174. if(r == 0)
  1175. return;
  1176. }
  1177. for(i = 28; i < 32; i += 2){
  1178. r = longname[j++];
  1179. slot[i] = r;
  1180. slot[i+1] = r >> 8;
  1181. if(r == 0)
  1182. return;
  1183. }
  1184. }
  1185. int
  1186. aliassum(Dosdir *d)
  1187. {
  1188. int i, sum;
  1189. if(d == nil)
  1190. return -1;
  1191. sum = 0;
  1192. for(i = 0; i < 11; i++)
  1193. sum = (((sum&1)<<7) | ((sum&0xfe)>>1)) + d->name[i];
  1194. return sum & 0xff;
  1195. }
  1196. int
  1197. putlongname(Xfs *xf, Dosptr *ndp, char *name, char sname[13])
  1198. {
  1199. Dosbpb *bp;
  1200. Dosdir tmpd;
  1201. Rune longname[DOSNAMELEN+1];
  1202. int i, first, sum, nds, len;
  1203. /* calculate checksum */
  1204. putname(sname, &tmpd);
  1205. sum = aliassum(&tmpd);
  1206. bp = xf->ptr;
  1207. first = 1;
  1208. len = utftorunes(longname, name, DOSNAMELEN);
  1209. if(chatty){
  1210. chat("utftorunes %s =", name);
  1211. for(i=0; i<len; i++)
  1212. chat(" %.4X", longname[i]);
  1213. chat("\n");
  1214. }
  1215. for(nds = (len + DOSRUNE-1) / DOSRUNE; nds > 0; nds--){
  1216. putnamesect(&ndp->p->iobuf[ndp->offset], longname, nds, first, sum);
  1217. first = 0;
  1218. ndp->offset += 32;
  1219. if(ndp->offset == bp->sectsize){
  1220. chat("long name moving over sector boundary\n");
  1221. ndp->p->flags |= BMOD;
  1222. putsect(ndp->p);
  1223. ndp->p = nil;
  1224. /*
  1225. * switch to the next cluster for a long entry
  1226. * naddr should be set up correctly by searchdir
  1227. */
  1228. ndp->prevaddr = ndp->addr;
  1229. ndp->addr = ndp->naddr;
  1230. ndp->naddr = -1;
  1231. if(ndp->addr == -1)
  1232. return -1;
  1233. ndp->p = getsect(xf, ndp->addr);
  1234. if(ndp->p == nil)
  1235. return -1;
  1236. ndp->offset = 0;
  1237. ndp->d = (Dosdir *)&ndp->p->iobuf[ndp->offset];
  1238. }
  1239. }
  1240. return 0;
  1241. }
  1242. int32_t
  1243. getfat(Xfs *xf, int n)
  1244. {
  1245. Dosbpb *bp = xf->ptr;
  1246. Iosect *p;
  1247. uint32_t k, sect;
  1248. int o, fb;
  1249. if(n < FATRESRV || n >= bp->fatclusters)
  1250. return -1;
  1251. fb = bp->fatbits;
  1252. k = (fb * n) >> 3;
  1253. if(k >= bp->fatsize*bp->sectsize)
  1254. panic("getfat");
  1255. sect = k/bp->sectsize + bp->fataddr;
  1256. o = k%bp->sectsize;
  1257. p = getsect(xf, sect);
  1258. if(p == nil)
  1259. return -1;
  1260. k = p->iobuf[o++];
  1261. if(o >= bp->sectsize){
  1262. putsect(p);
  1263. p = getsect(xf, sect+1);
  1264. if(p == nil)
  1265. return -1;
  1266. o = 0;
  1267. }
  1268. k |= p->iobuf[o++]<<8;
  1269. if(fb == 32){
  1270. /* fat32 is really fat28 */
  1271. k |= p->iobuf[o++] << 16;
  1272. k |= (p->iobuf[o] & 0x0f) << 24;
  1273. fb = 28;
  1274. }
  1275. putsect(p);
  1276. if(fb == 12){
  1277. if(n&1)
  1278. k >>= 4;
  1279. else
  1280. k &= 0xfff;
  1281. }
  1282. if(chatty > 1)
  1283. chat("fat(%#x)=%#lx...", n, k);
  1284. /*
  1285. * This is a very strange check for out of range.
  1286. * As a concrete example, for a 16-bit FAT,
  1287. * FFF8 through FFFF all signify ``end of cluster chain.''
  1288. * This generalizes to other-sized FATs.
  1289. */
  1290. if(k >= (1 << fb) - 8)
  1291. return -1;
  1292. return k;
  1293. }
  1294. void
  1295. putfat(Xfs *xf, int n, uint32_t val)
  1296. {
  1297. Fatinfo *fi;
  1298. Dosbpb *bp;
  1299. Iosect *p;
  1300. uint32_t k, sect, esect;
  1301. int o;
  1302. bp = xf->ptr;
  1303. if(n < FATRESRV || n >= bp->fatclusters)
  1304. panic("putfat n=%d", n);
  1305. k = (bp->fatbits * n) >> 3;
  1306. if(k >= bp->fatsize*bp->sectsize)
  1307. panic("putfat");
  1308. sect = k/bp->sectsize + bp->fataddr;
  1309. esect = sect + bp->nfats * bp->fatsize;
  1310. for(; sect<esect; sect+=bp->fatsize){
  1311. o = k%bp->sectsize;
  1312. p = getsect(xf, sect);
  1313. if(p == nil)
  1314. continue;
  1315. switch(bp->fatbits){
  1316. case 12:
  1317. if(n&1){
  1318. p->iobuf[o] &= 0x0f;
  1319. p->iobuf[o++] |= val<<4;
  1320. if(o >= bp->sectsize){
  1321. p->flags |= BMOD;
  1322. putsect(p);
  1323. p = getsect(xf, sect+1);
  1324. if(p == nil)
  1325. continue;
  1326. o = 0;
  1327. }
  1328. p->iobuf[o] = val>>4;
  1329. }else{
  1330. p->iobuf[o++] = val;
  1331. if(o >= bp->sectsize){
  1332. p->flags |= BMOD;
  1333. putsect(p);
  1334. p = getsect(xf, sect+1);
  1335. if(p == nil)
  1336. continue;
  1337. o = 0;
  1338. }
  1339. p->iobuf[o] &= 0xf0;
  1340. p->iobuf[o] |= (val>>8) & 0x0f;
  1341. }
  1342. break;
  1343. case 16:
  1344. p->iobuf[o++] = val;
  1345. p->iobuf[o] = val>>8;
  1346. break;
  1347. case 32: /* fat32 is really fat28 */
  1348. p->iobuf[o++] = val;
  1349. p->iobuf[o++] = val>>8;
  1350. p->iobuf[o++] = val>>16;
  1351. p->iobuf[o] = (p->iobuf[o] & 0xf0) | ((val>>24) & 0x0f);
  1352. break;
  1353. default:
  1354. panic("putfat fatbits");
  1355. }
  1356. p->flags |= BMOD;
  1357. putsect(p);
  1358. }
  1359. if(val == 0)
  1360. bp->freeclusters++;
  1361. else
  1362. bp->freeclusters--;
  1363. if(bp->fatinfo){
  1364. p = getsect(xf, bp->fatinfo);
  1365. if(p != nil){
  1366. fi = (Fatinfo*)p->iobuf;
  1367. PLONG(fi->nextfree, bp->freeptr);
  1368. PLONG(fi->freeclust, bp->freeclusters);
  1369. p->flags |= BMOD;
  1370. putsect(p);
  1371. }
  1372. }
  1373. }
  1374. /*
  1375. * Contiguous falloc; if we can, use lastclust+1.
  1376. * Otherwise, move the file to get some space.
  1377. * If there just isn't enough contiguous space
  1378. * anywhere on disk, fail.
  1379. */
  1380. int
  1381. cfalloc(Xfile *f)
  1382. {
  1383. int l;
  1384. if((l=makecontig(f, 8)) >= 0)
  1385. return l;
  1386. return makecontig(f, 1);
  1387. }
  1388. /*
  1389. * Check whether a file is contiguous.
  1390. */
  1391. int
  1392. iscontig(Xfs *xf, Dosdir *d)
  1393. {
  1394. int32_t clust, next;
  1395. clust = getstart(xf, d);
  1396. if(clust <= 0)
  1397. return 1;
  1398. for(;;) {
  1399. next = getfat(xf, clust);
  1400. if(next < 0)
  1401. return 1;
  1402. if(next != clust+1)
  1403. return 0;
  1404. clust = next;
  1405. }
  1406. }
  1407. /*
  1408. * Make a file contiguous, with nextra clusters of
  1409. * free space after it for later expansion.
  1410. * Return the number of the first new cluster.
  1411. */
  1412. int
  1413. makecontig(Xfile *f, int nextra)
  1414. {
  1415. Dosbpb *bp;
  1416. Dosdir *d;
  1417. Dosptr *dp;
  1418. Xfs *xf;
  1419. Iosect *wp, *rp;
  1420. int32_t clust, next, last, start, rclust, wclust, eclust, ostart;
  1421. int isok, i, n, nclust, nrun, rs, ws;
  1422. xf = f->xf;
  1423. bp = xf->ptr;
  1424. dp = f->ptr;
  1425. d = dp->d;
  1426. isok = 1;
  1427. nclust = 0;
  1428. clust = fileclust(f, 0, 0);
  1429. chat("clust %#lux", clust);
  1430. if(clust != -1) {
  1431. for(;;) {
  1432. nclust++;
  1433. chat(".");
  1434. next = getfat(xf, clust);
  1435. if(next <= 0)
  1436. break;
  1437. if(next != clust+1)
  1438. isok = 0;
  1439. clust = next;
  1440. }
  1441. }
  1442. chat("nclust %d\n", nclust);
  1443. if(isok && clust != -1) {
  1444. eclust = clust+1; /* eclust = first cluster past file */
  1445. assert(eclust == fileclust(f, 0, 0)+nclust);
  1446. for(i=0; i<nextra; i++)
  1447. if(getfat(xf, eclust+i) != 0)
  1448. break;
  1449. if(i == nextra) { /* they were all free */
  1450. chat("eclust=%#lx, getfat eclust-1 = %#lux\n", eclust, getfat(xf, eclust-1));
  1451. assert(getfat(xf, eclust-1) == 0xffffffff);
  1452. putfat(xf, eclust-1, eclust);
  1453. putfat(xf, eclust, 0xffffffff);
  1454. bp->freeptr = clust+1; /* to help keep the blocks free */
  1455. return eclust;
  1456. }
  1457. }
  1458. /* need to search for nclust+nextra contiguous free blocks */
  1459. last = -1;
  1460. n = bp->freeptr;
  1461. nrun = 0;
  1462. for(;;){
  1463. if(getfat(xf, n) == 0) {
  1464. if(last+1 == n)
  1465. nrun++;
  1466. else
  1467. nrun = 1;
  1468. if(nrun >= nclust+nextra)
  1469. break;
  1470. last = n;
  1471. }
  1472. if(++n >= bp->fatclusters)
  1473. n = FATRESRV;
  1474. if(n == bp->freeptr) {
  1475. errno = Econtig;
  1476. return -1;
  1477. }
  1478. }
  1479. bp->freeptr = n+1;
  1480. /* copy old data over */
  1481. start = n+1 - nrun;
  1482. /* sanity check */
  1483. for(i=0; i<nclust+nextra; i++)
  1484. assert(getfat(xf, start+i) == 0);
  1485. chat("relocate chain %lx -> 0x%lx len %d\n", fileclust(f, 0, 0), start, nclust);
  1486. wclust = start;
  1487. for(rclust = fileclust(f, 0, 0); rclust > 0; rclust = next){
  1488. rs = clust2sect(bp, rclust);
  1489. ws = clust2sect(bp, wclust);
  1490. for(i=0; i<bp->clustsize; i++, rs++, ws++){
  1491. rp = getsect(xf, rs);
  1492. if(rp == nil)
  1493. return -1;
  1494. wp = getosect(xf, ws);
  1495. assert(wp != nil);
  1496. memmove(wp->iobuf, rp->iobuf, bp->sectsize);
  1497. wp->flags = BMOD;
  1498. putsect(rp);
  1499. putsect(wp);
  1500. }
  1501. chat("move cluster %#lx -> %#lx...", rclust, wclust);
  1502. next = getfat(xf, rclust);
  1503. putfat(xf, wclust, wclust+1);
  1504. wclust++;
  1505. }
  1506. /* now wclust points at the first new cluster; chain it in */
  1507. chat("wclust 0x%lx start 0x%lx (fat->0x%lx) nclust %d\n", wclust, start, getfat(xf, start), nclust);
  1508. assert(wclust == start+nclust);
  1509. putfat(xf, wclust, 0xffffffff); /* end of file */
  1510. /* update directory entry to point at new start */
  1511. ostart = fileclust(f, 0, 0);
  1512. putstart(xf, d, start);
  1513. /* check our work */
  1514. i = 0;
  1515. clust = fileclust(f, 0, 0);
  1516. if(clust != -1) {
  1517. for(;;) {
  1518. i++;
  1519. next = getfat(xf, clust);
  1520. if(next <= 0)
  1521. break;
  1522. assert(next == clust+1);
  1523. clust = next;
  1524. }
  1525. }
  1526. chat("chain check: len %d\n", i);
  1527. assert(i == nclust+1);
  1528. /* succeeded; remove old chain. */
  1529. for(rclust = ostart; rclust > 0; rclust = next){
  1530. next = getfat(xf, rclust);
  1531. putfat(xf, rclust, 0); /* free cluster */
  1532. }
  1533. return start+nclust;
  1534. }
  1535. int
  1536. falloc(Xfs *xf)
  1537. {
  1538. Dosbpb *bp = xf->ptr;
  1539. Iosect *p;
  1540. int n, i, k;
  1541. n = bp->freeptr;
  1542. for(;;){
  1543. if(getfat(xf, n) == 0)
  1544. break;
  1545. if(++n >= bp->fatclusters)
  1546. n = FATRESRV;
  1547. if(n == bp->freeptr)
  1548. return -1;
  1549. }
  1550. bp->freeptr = n+1;
  1551. if(bp->freeptr >= bp->fatclusters)
  1552. bp->freeptr = FATRESRV;
  1553. putfat(xf, n, 0xffffffff);
  1554. k = clust2sect(bp, n);
  1555. for(i=0; i<bp->clustsize; i++){
  1556. p = getosect(xf, k+i);
  1557. memset(p->iobuf, 0, bp->sectsize);
  1558. p->flags = BMOD;
  1559. putsect(p);
  1560. }
  1561. return n;
  1562. }
  1563. void
  1564. ffree(Xfs *xf, int32_t start)
  1565. {
  1566. putfat(xf, start, 0);
  1567. }
  1568. int32_t
  1569. clust2sect(Dosbpb *bp, int32_t clust)
  1570. {
  1571. return bp->dataaddr + (clust - FATRESRV) * bp->clustsize;
  1572. }
  1573. int32_t
  1574. sect2clust(Dosbpb *bp, int32_t sect)
  1575. {
  1576. int32_t c;
  1577. c = (sect - bp->dataaddr) / bp->clustsize + FATRESRV;
  1578. assert(sect == clust2sect(bp, c));
  1579. return c;
  1580. }
  1581. void
  1582. puttime(Dosdir *d, int32_t s)
  1583. {
  1584. Tm *t;
  1585. uint16_t x;
  1586. if(s == 0)
  1587. s = time(0);
  1588. t = localtime(s);
  1589. x = (t->hour<<11) | (t->min<<5) | (t->sec>>1);
  1590. PSHORT(d->time, x);
  1591. x = ((t->year-80)<<9) | ((t->mon+1)<<5) | t->mday;
  1592. PSHORT(d->date, x);
  1593. }
  1594. int32_t
  1595. gtime(Dosdir *dp)
  1596. {
  1597. Tm tm;
  1598. int i;
  1599. i = GSHORT(dp->time);
  1600. tm.hour = i >> 11;
  1601. tm.min = (i >> 5) & 63;
  1602. tm.sec = (i & 31) << 1;
  1603. i = GSHORT(dp->date);
  1604. tm.year = 80 + (i >> 9);
  1605. tm.mon = ((i >> 5) & 15) - 1;
  1606. tm.mday = i & 31;
  1607. tm.zone[0] = '\0';
  1608. tm.tzoff = 0;
  1609. tm.yday = 0;
  1610. return tm2sec(&tm);
  1611. }
  1612. /*
  1613. * structure dumps for debugging
  1614. */
  1615. void
  1616. bootdump(int fd, Dosboot *b)
  1617. {
  1618. Biobuf bp;
  1619. Binit(&bp, fd, OWRITE);
  1620. Bprint(&bp, "magic: 0x%2.2x 0x%2.2x 0x%2.2x\n",
  1621. b->magic[0], b->magic[1], b->magic[2]);
  1622. Bprint(&bp, "version: \"%8.8s\"\n", (char*)b->version);
  1623. Bprint(&bp, "sectsize: %d\n", GSHORT(b->sectsize));
  1624. Bprint(&bp, "clustsize: %d\n", b->clustsize);
  1625. Bprint(&bp, "nresrv: %d\n", GSHORT(b->nresrv));
  1626. Bprint(&bp, "nfats: %d\n", b->nfats);
  1627. Bprint(&bp, "rootsize: %d\n", GSHORT(b->rootsize));
  1628. Bprint(&bp, "volsize: %d\n", GSHORT(b->volsize));
  1629. Bprint(&bp, "mediadesc: 0x%2.2x\n", b->mediadesc);
  1630. Bprint(&bp, "fatsize: %d\n", GSHORT(b->fatsize));
  1631. Bprint(&bp, "trksize: %d\n", GSHORT(b->trksize));
  1632. Bprint(&bp, "nheads: %d\n", GSHORT(b->nheads));
  1633. Bprint(&bp, "nhidden: %ld\n", GLONG(b->nhidden));
  1634. Bprint(&bp, "bigvolsize: %ld\n", GLONG(b->bigvolsize));
  1635. Bprint(&bp, "driveno: %d\n", b->driveno);
  1636. Bprint(&bp, "reserved0: 0x%2.2x\n", b->reserved0);
  1637. Bprint(&bp, "bootsig: 0x%2.2x\n", b->bootsig);
  1638. Bprint(&bp, "volid: 0x%8.8lux\n", GLONG(b->volid));
  1639. Bprint(&bp, "label: \"%11.11s\"\n", (char*)b->label);
  1640. Bterm(&bp);
  1641. }
  1642. void
  1643. bootdump32(int fd, Dosboot32 *b)
  1644. {
  1645. Biobuf bp;
  1646. Binit(&bp, fd, OWRITE);
  1647. Bprint(&bp, "magic: 0x%2.2x 0x%2.2x 0x%2.2x\n",
  1648. b->magic[0], b->magic[1], b->magic[2]);
  1649. Bprint(&bp, "version: \"%8.8s\"\n", (char*)b->version);
  1650. Bprint(&bp, "sectsize: %d\n", GSHORT(b->sectsize));
  1651. Bprint(&bp, "clustsize: %d\n", b->clustsize);
  1652. Bprint(&bp, "nresrv: %d\n", GSHORT(b->nresrv));
  1653. Bprint(&bp, "nfats: %d\n", b->nfats);
  1654. Bprint(&bp, "rootsize: %d\n", GSHORT(b->rootsize));
  1655. Bprint(&bp, "volsize: %d\n", GSHORT(b->volsize));
  1656. Bprint(&bp, "mediadesc: 0x%2.2x\n", b->mediadesc);
  1657. Bprint(&bp, "fatsize: %d\n", GSHORT(b->fatsize));
  1658. Bprint(&bp, "trksize: %d\n", GSHORT(b->trksize));
  1659. Bprint(&bp, "nheads: %d\n", GSHORT(b->nheads));
  1660. Bprint(&bp, "nhidden: %ld\n", GLONG(b->nhidden));
  1661. Bprint(&bp, "bigvolsize: %ld\n", GLONG(b->bigvolsize));
  1662. Bprint(&bp, "fatsize32: %ld\n", GLONG(b->fatsize32));
  1663. Bprint(&bp, "extflags: %d\n", GSHORT(b->extflags));
  1664. Bprint(&bp, "version: %d\n", GSHORT(b->version1));
  1665. Bprint(&bp, "rootstart: %ld\n", GLONG(b->rootstart));
  1666. Bprint(&bp, "infospec: %d\n", GSHORT(b->infospec));
  1667. Bprint(&bp, "backupboot: %d\n", GSHORT(b->backupboot));
  1668. Bprint(&bp, "reserved: %d %d %d %d %d %d %d %d %d %d %d %d\n",
  1669. b->reserved[0], b->reserved[1], b->reserved[2], b->reserved[3],
  1670. b->reserved[4], b->reserved[5], b->reserved[6], b->reserved[7],
  1671. b->reserved[8], b->reserved[9], b->reserved[10], b->reserved[11]);
  1672. Bterm(&bp);
  1673. }
  1674. void
  1675. bootsecdump32(int fd, Xfs *xf, Dosboot32 *b32)
  1676. {
  1677. Fatinfo *fi;
  1678. Iosect *p1;
  1679. int fisec, bsec, res;
  1680. fprint(fd, "\nfat32\n");
  1681. bootdump32(fd, b32);
  1682. res = GSHORT(b32->nresrv);
  1683. bsec = GSHORT(b32->backupboot);
  1684. if(bsec < res && bsec != 0){
  1685. p1 = getsect(xf, bsec);
  1686. if(p1 == nil)
  1687. fprint(fd, "\ncouldn't get backup boot sector: %r\n");
  1688. else{
  1689. fprint(fd, "\nbackup boot\n");
  1690. bootdump32(fd, (Dosboot32*)p1->iobuf);
  1691. putsect(p1);
  1692. }
  1693. }else if(bsec != 0xffff)
  1694. fprint(fd, "bad backup boot sector: %d reserved %d\n", bsec, res);
  1695. fisec = GSHORT(b32->infospec);
  1696. if(fisec < res && fisec != 0){
  1697. p1 = getsect(xf, fisec);
  1698. if(p1 == nil)
  1699. fprint(fd, "\ncouldn't get fat info sector: %r\n");
  1700. else{
  1701. fprint(fd, "\nfat info %d\n", fisec);
  1702. fi = (Fatinfo*)p1->iobuf;
  1703. fprint(fd, "sig1: 0x%lx sb 0x%lx\n", GLONG(fi->sig1), FATINFOSIG1);
  1704. fprint(fd, "sig: 0x%lx sb 0x%lx\n", GLONG(fi->sig), FATINFOSIG);
  1705. fprint(fd, "freeclust: %lu\n", GLONG(fi->freeclust));
  1706. fprint(fd, "nextfree: %lu\n", GLONG(fi->nextfree));
  1707. fprint(fd, "reserved: %lu %lu %lu\n", GLONG(fi->resrv), GLONG(fi->resrv+4), GLONG(fi->resrv+8));
  1708. putsect(p1);
  1709. }
  1710. }else if(fisec != 0xffff)
  1711. fprint(2, "bad fat info sector: %d reserved %d\n", bsec, res);
  1712. }
  1713. void
  1714. dirdump(void *vdbuf)
  1715. {
  1716. static char attrchar[] = "rhsvda67";
  1717. Dosdir *d;
  1718. char *name, namebuf[DOSNAMELEN];
  1719. char buf[128], *s, *ebuf;
  1720. uint8_t *dbuf;
  1721. int i;
  1722. if(!chatty)
  1723. return;
  1724. d = vdbuf;
  1725. ebuf = buf + sizeof(buf);
  1726. if(d->attr == 0xf){
  1727. dbuf = vdbuf;
  1728. name = namebuf + DOSNAMELEN;
  1729. *--name = '\0';
  1730. name = getnamerunes(name, dbuf, 1);
  1731. seprint(buf, ebuf, "\"%s\" %2.2x %2.2ux %2.2ux %d", name, dbuf[0], dbuf[12], dbuf[13], GSHORT(d->start));
  1732. }else{
  1733. s = seprint(buf, ebuf, "\"%.8s.%.3s\" ", (char*)d->name,
  1734. (char*)d->ext);
  1735. for(i=7; i>=0; i--)
  1736. *s++ = d->attr&(1<<i) ? attrchar[i] : '-';
  1737. i = GSHORT(d->time);
  1738. s = seprint(s, ebuf, " %2.2d:%2.2d:%2.2d", i>>11, (i>>5)&63, (i&31)<<1);
  1739. i = GSHORT(d->date);
  1740. s = seprint(s, ebuf, " %2.2d.%2.2d.%2.2d", 80+(i>>9), (i>>5)&15, i&31);
  1741. i = GSHORT(d->ctime);
  1742. s = seprint(s, ebuf, " %2.2d:%2.2d:%2.2d", i>>11, (i>>5)&63, (i&31)<<1);
  1743. i = GSHORT(d->cdate);
  1744. s = seprint(s, ebuf, " %2.2d.%2.2d.%2.2d", 80+(i>>9), (i>>5)&15, i&31);
  1745. i = GSHORT(d->adate);
  1746. s = seprint(s, ebuf, " %2.2d.%2.2d.%2.2d", 80+(i>>9), (i>>5)&15, i&31);
  1747. seprint(s, ebuf, " %d %d", GSHORT(d->start), GSHORT(d->length));
  1748. }
  1749. chat("%s\n", buf);
  1750. }
  1751. int
  1752. cistrcmp(char *s1, char *s2)
  1753. {
  1754. int c1, c2;
  1755. while(*s1){
  1756. c1 = *s1++;
  1757. c2 = *s2++;
  1758. if(c1 >= 'A' && c1 <= 'Z')
  1759. c1 -= 'A' - 'a';
  1760. if(c2 >= 'A' && c2 <= 'Z')
  1761. c2 -= 'A' - 'a';
  1762. if(c1 != c2)
  1763. return c1 - c2;
  1764. }
  1765. return -*s2;
  1766. }
  1767. int
  1768. utftorunes(Rune *rr, char *s, int n)
  1769. {
  1770. Rune *r, *re;
  1771. int c;
  1772. r = rr;
  1773. re = r + n - 1;
  1774. while(c = (uint8_t)*s){
  1775. if(c < Runeself){
  1776. *r = c;
  1777. s++;
  1778. }else
  1779. s += chartorune(r, s);
  1780. r++;
  1781. if(r >= re)
  1782. break;
  1783. }
  1784. *r = 0;
  1785. return r - rr;
  1786. }