dosboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "fs.h"
  7. struct Dosboot{
  8. uchar magic[3];
  9. uchar version[8];
  10. uchar sectsize[2];
  11. uchar clustsize;
  12. uchar nresrv[2];
  13. uchar nfats;
  14. uchar rootsize[2];
  15. uchar volsize[2];
  16. uchar mediadesc;
  17. uchar fatsize[2];
  18. uchar trksize[2];
  19. uchar nheads[2];
  20. uchar nhidden[4];
  21. uchar bigvolsize[4];
  22. /* fat 32 */
  23. uchar bigfatsize[4];
  24. uchar extflags[2];
  25. uchar fsversion[2];
  26. uchar rootdirstartclust[4];
  27. uchar fsinfosect[2];
  28. uchar backupbootsect[2];
  29. /* ???
  30. uchar driveno;
  31. uchar reserved0;
  32. uchar bootsig;
  33. uchar volid[4];
  34. uchar label[11];
  35. uchar reserved1[8];
  36. */
  37. };
  38. struct Dosdir{
  39. uchar name[8];
  40. uchar ext[3];
  41. uchar attr;
  42. uchar lowercase;
  43. uchar hundredth;
  44. uchar ctime[2];
  45. uchar cdate[2];
  46. uchar adate[2];
  47. uchar highstart[2];
  48. uchar mtime[2];
  49. uchar mdate[2];
  50. uchar start[2];
  51. uchar length[4];
  52. };
  53. #define DOSRONLY 0x01
  54. #define DOSHIDDEN 0x02
  55. #define DOSSYSTEM 0x04
  56. #define DOSVLABEL 0x08
  57. #define DOSDIR 0x10
  58. #define DOSARCH 0x20
  59. /*
  60. * predeclared
  61. */
  62. static void bootdump(Dosboot*);
  63. static void setname(Dosfile*, char*);
  64. /*
  65. * debugging
  66. */
  67. #define chatty 0
  68. #define chat if(chatty)print
  69. /*
  70. * block io buffers
  71. */
  72. enum
  73. {
  74. Nbio= 16,
  75. };
  76. typedef struct Clustbuf Clustbuf;
  77. struct Clustbuf
  78. {
  79. int age;
  80. long sector;
  81. uchar *iobuf;
  82. Dos *dos;
  83. int size;
  84. };
  85. Clustbuf bio[Nbio];
  86. /*
  87. * get an io block from an io buffer
  88. */
  89. Clustbuf*
  90. getclust(Dos *dos, long sector)
  91. {
  92. Fs *fs;
  93. Clustbuf *p, *oldest;
  94. int size;
  95. chat("getclust @ %ld\n", sector);
  96. /*
  97. * if we have it, just return it
  98. */
  99. for(p = bio; p < &bio[Nbio]; p++){
  100. if(sector == p->sector && dos == p->dos){
  101. p->age = m->ticks;
  102. chat("getclust %ld in cache\n", sector);
  103. return p;
  104. }
  105. }
  106. /*
  107. * otherwise, reuse the oldest entry
  108. */
  109. oldest = bio;
  110. for(p = &bio[1]; p < &bio[Nbio]; p++){
  111. if(p->age <= oldest->age)
  112. oldest = p;
  113. }
  114. p = oldest;
  115. /*
  116. * make sure the buffer is big enough
  117. */
  118. size = dos->clustsize*dos->sectsize;
  119. if(p->iobuf==0 || p->size < size)
  120. p->iobuf = ialloc(size, 0);
  121. p->size = size;
  122. /*
  123. * read in the cluster
  124. */
  125. fs = (Fs*)dos;
  126. chat("getclust addr %lud %p %p %p\n", (ulong)((sector+dos->start)*(vlong)dos->sectsize),
  127. fs, fs->diskseek, fs->diskread);
  128. if(fs->diskseek(fs, (sector+dos->start)*(vlong)dos->sectsize) < 0){
  129. chat("can't seek block\n");
  130. return 0;
  131. }
  132. if(fs->diskread(fs, p->iobuf, size) != size){
  133. chat("can't read block\n");
  134. return 0;
  135. }
  136. p->age = m->ticks;
  137. p->dos = dos;
  138. p->sector = sector;
  139. chat("getclust %ld read\n", sector);
  140. return p;
  141. }
  142. /*
  143. * walk the fat one level ( n is a current cluster number ).
  144. * return the new cluster number or -1 if no more.
  145. */
  146. static long
  147. fatwalk(Dos *dos, int n)
  148. {
  149. ulong k, sect;
  150. Clustbuf *p;
  151. int o;
  152. chat("fatwalk %d\n", n);
  153. if(n < 2 || n >= dos->fatclusters)
  154. return -1;
  155. switch(dos->fatbits){
  156. case 12:
  157. k = (3*n)/2; break;
  158. case 16:
  159. k = 2*n; break;
  160. case 32:
  161. k = 4*n; break;
  162. default:
  163. return -1;
  164. }
  165. if(k >= dos->fatsize*dos->sectsize)
  166. panic("getfat");
  167. if (dos->sectsize == 0 || dos->clustsize == 0)
  168. panic("fatwalk: zero sector or cluster size");
  169. sect = (k/(dos->sectsize*dos->clustsize))*dos->clustsize + dos->fataddr;
  170. o = k%(dos->sectsize*dos->clustsize);
  171. p = getclust(dos, sect);
  172. k = p->iobuf[o++];
  173. if(o >= dos->sectsize*dos->clustsize){
  174. p = getclust(dos, sect+dos->clustsize);
  175. o = 0;
  176. }
  177. k |= p->iobuf[o++]<<8;
  178. if(dos->fatbits == 12){
  179. if(n&1)
  180. k >>= 4;
  181. else
  182. k &= 0xfff;
  183. if(k >= 0xff8)
  184. k = -1;
  185. }
  186. else if (dos->fatbits == 32){
  187. if(o >= dos->sectsize*dos->clustsize){
  188. p = getclust(dos, sect+dos->clustsize);
  189. o = 0;
  190. }
  191. k |= p->iobuf[o++]<<16;
  192. k |= p->iobuf[o]<<24;
  193. if (k >= 0xfffffff8)
  194. k = -1;
  195. }
  196. else
  197. k = k < 0xfff8 ? k : -1;
  198. chat("fatwalk %d -> %lud\n", n, k);
  199. return k;
  200. }
  201. /*
  202. * map a file's logical cluster address to a physical sector address
  203. */
  204. static long
  205. fileaddr(Dosfile *fp, long ltarget)
  206. {
  207. Dos *dos = fp->dos;
  208. long l;
  209. long p;
  210. chat("fileaddr %8.8s %ld\n", fp->name, ltarget);
  211. /*
  212. * root directory is contiguous and easy (unless FAT32)
  213. */
  214. if(fp->pstart == 0 && dos->rootsize != 0) {
  215. if(ltarget*dos->sectsize*dos->clustsize >= dos->rootsize*sizeof(Dosdir))
  216. return -1;
  217. l = dos->rootaddr + ltarget*dos->clustsize;
  218. chat("fileaddr %ld -> %ld\n", ltarget, l);
  219. return l;
  220. }
  221. /*
  222. * anything else requires a walk through the fat
  223. */
  224. if(ltarget >= fp->lcurrent && fp->pcurrent){
  225. /* start at the currrent point */
  226. l = fp->lcurrent;
  227. p = fp->pcurrent;
  228. } else {
  229. /* go back to the beginning */
  230. l = 0;
  231. p = fp->pstart;
  232. }
  233. while(l != ltarget){
  234. /* walk the fat */
  235. p = fatwalk(dos, p);
  236. if(p < 0)
  237. return -1;
  238. l++;
  239. }
  240. fp->lcurrent = l;
  241. fp->pcurrent = p;
  242. /*
  243. * clusters start at 2 instead of 0 (why? - presotto)
  244. */
  245. l = dos->dataaddr + (p-2)*dos->clustsize;
  246. chat("fileaddr %ld -> %ld\n", ltarget, l);
  247. return l;
  248. }
  249. /*
  250. * read from a dos file
  251. */
  252. long
  253. dosread(Dosfile *fp, void *a, long n)
  254. {
  255. long addr;
  256. long rv;
  257. int i;
  258. int off;
  259. Clustbuf *p;
  260. uchar *from, *to;
  261. if((fp->attr & DOSDIR) == 0){
  262. if(fp->offset >= fp->length)
  263. return 0;
  264. if(fp->offset+n > fp->length)
  265. n = fp->length - fp->offset;
  266. }
  267. to = a;
  268. for(rv = 0; rv < n; rv+=i){
  269. /*
  270. * read the cluster
  271. */
  272. addr = fileaddr(fp, fp->offset/fp->dos->clustbytes);
  273. if(addr < 0)
  274. return -1;
  275. p = getclust(fp->dos, addr);
  276. if(p == 0)
  277. return -1;
  278. /*
  279. * copy the bytes we need
  280. */
  281. off = fp->offset % fp->dos->clustbytes;
  282. from = &p->iobuf[off];
  283. i = n - rv;
  284. if(i > fp->dos->clustbytes - off)
  285. i = fp->dos->clustbytes - off;
  286. memmove(to, from, i);
  287. to += i;
  288. fp->offset += i;
  289. }
  290. return rv;
  291. }
  292. /*
  293. * walk a directory returns
  294. * -1 if something went wrong
  295. * 0 if not found
  296. * 1 if found
  297. */
  298. int
  299. doswalk(File *f, char *name)
  300. {
  301. Dosdir d;
  302. long n;
  303. Dosfile *file;
  304. chat("doswalk %s\n", name);
  305. file = &f->dos;
  306. if((file->attr & DOSDIR) == 0){
  307. chat("walking non-directory!\n");
  308. return -1;
  309. }
  310. setname(file, name);
  311. file->offset = 0; /* start at the beginning */
  312. while((n = dosread(file, &d, sizeof(d))) == sizeof(d)){
  313. chat("comparing to %8.8s.%3.3s\n", (char*)d.name, (char*)d.ext);
  314. if(memcmp(file->name, d.name, sizeof(d.name)) != 0)
  315. continue;
  316. if(memcmp(file->ext, d.ext, sizeof(d.ext)) != 0)
  317. continue;
  318. if(d.attr & DOSVLABEL){
  319. chat("%8.8s.%3.3s is a LABEL\n", (char*)d.name, (char*)d.ext);
  320. continue;
  321. }
  322. file->attr = d.attr;
  323. file->pstart = GSHORT(d.start);
  324. if (file->dos->fatbits == 32)
  325. file->pstart |= GSHORT(d.highstart) << 16;
  326. file->length = GLONG(d.length);
  327. file->pcurrent = 0;
  328. file->lcurrent = 0;
  329. file->offset = 0;
  330. return 1;
  331. }
  332. return n >= 0 ? 0 : -1;
  333. }
  334. /*
  335. * instructions that boot blocks can start with
  336. */
  337. #define JMPSHORT 0xeb
  338. #define JMPNEAR 0xe9
  339. /*
  340. * read in a segment
  341. */
  342. long
  343. dosreadseg(File *f, void *va, long len)
  344. {
  345. char *a;
  346. long n, sofar;
  347. Dosfile *fp;
  348. fp = &f->dos;
  349. a = va;
  350. for(sofar = 0; sofar < len; sofar += n){
  351. n = 8*1024;
  352. if(len - sofar < n)
  353. n = len - sofar;
  354. n = dosread(fp, a + sofar, n);
  355. if(n <= 0)
  356. break;
  357. print(".");
  358. }
  359. return sofar;
  360. }
  361. int
  362. dosinit(Fs *fs)
  363. {
  364. Clustbuf *p;
  365. Dosboot *b;
  366. int i;
  367. Dos *dos;
  368. Dosfile *root;
  369. chat("dosinit0 %p %p %p\n", fs, fs->diskseek, fs->diskread);
  370. dos = &fs->dos;
  371. /* defaults till we know better */
  372. dos->sectsize = 512;
  373. dos->clustsize = 1;
  374. /* get first sector */
  375. p = getclust(dos, 0);
  376. if(p == 0){
  377. chat("can't read boot block\n");
  378. return -1;
  379. }
  380. chat("dosinit0a\n");
  381. p->dos = 0;
  382. b = (Dosboot *)p->iobuf;
  383. if(b->magic[0] != JMPNEAR && (b->magic[0] != JMPSHORT || b->magic[2] != 0x90)){
  384. chat("no dos file system %x %x %x %x\n",
  385. b->magic[0], b->magic[1], b->magic[2], b->magic[3]);
  386. return -1;
  387. }
  388. if(chatty)
  389. bootdump(b);
  390. if(b->clustsize == 0) {
  391. unreasonable:
  392. if(chatty){
  393. print("unreasonable FAT BPB: ");
  394. for(i=0; i<3+8+2+1; i++)
  395. print(" %.2ux", p->iobuf[i]);
  396. print("\n");
  397. }
  398. return -1;
  399. }
  400. chat("dosinit1\n");
  401. /*
  402. * Determine the systems' wondrous properties.
  403. * There are heuristics here, but there's no real way
  404. * of knowing if this is a reasonable FAT.
  405. */
  406. dos->fatbits = 0;
  407. dos->sectsize = GSHORT(b->sectsize);
  408. if(dos->sectsize & 0xFF)
  409. goto unreasonable;
  410. dos->clustsize = b->clustsize;
  411. dos->clustbytes = dos->sectsize*dos->clustsize;
  412. dos->nresrv = GSHORT(b->nresrv);
  413. dos->nfats = b->nfats;
  414. dos->fatsize = GSHORT(b->fatsize);
  415. dos->rootsize = GSHORT(b->rootsize);
  416. dos->volsize = GSHORT(b->volsize);
  417. if(dos->volsize == 0)
  418. dos->volsize = GLONG(b->bigvolsize);
  419. dos->mediadesc = b->mediadesc;
  420. if(dos->fatsize == 0) {
  421. chat("fat32\n");
  422. dos->rootsize = 0;
  423. dos->fatsize = GLONG(b->bigfatsize);
  424. dos->fatbits = 32;
  425. }
  426. dos->fataddr = dos->nresrv;
  427. if (dos->rootsize == 0) {
  428. dos->rootaddr = 0;
  429. dos->rootclust = GLONG(b->rootdirstartclust);
  430. dos->dataaddr = dos->fataddr + dos->nfats*dos->fatsize;
  431. } else {
  432. dos->rootaddr = dos->fataddr + dos->nfats*dos->fatsize;
  433. i = dos->rootsize*sizeof(Dosdir) + dos->sectsize - 1;
  434. i = i/dos->sectsize;
  435. dos->dataaddr = dos->rootaddr + i;
  436. }
  437. dos->fatclusters = 2+(dos->volsize - dos->dataaddr)/dos->clustsize;
  438. if(dos->fatbits != 32) {
  439. if(dos->fatclusters < 4087)
  440. dos->fatbits = 12;
  441. else
  442. dos->fatbits = 16;
  443. }
  444. dos->freeptr = 2;
  445. if(dos->clustbytes < 512 || dos->clustbytes > 64*1024)
  446. goto unreasonable;
  447. chat("dosinit2\n");
  448. /*
  449. * set up the root
  450. */
  451. fs->root.fs = fs;
  452. root = &fs->root.dos;
  453. root->dos = dos;
  454. root->pstart = dos->rootsize == 0 ? dos->rootclust : 0;
  455. root->pcurrent = root->lcurrent = 0;
  456. root->offset = 0;
  457. root->attr = DOSDIR;
  458. root->length = dos->rootsize*sizeof(Dosdir);
  459. chat("dosinit3\n");
  460. fs->read = dosreadseg;
  461. fs->walk = doswalk;
  462. return 0;
  463. }
  464. static void
  465. bootdump(Dosboot *b)
  466. {
  467. if(chatty == 0)
  468. return;
  469. print("magic: 0x%2.2x 0x%2.2x 0x%2.2x ",
  470. b->magic[0], b->magic[1], b->magic[2]);
  471. print("version: \"%8.8s\"\n", (char*)b->version);
  472. print("sectsize: %d ", GSHORT(b->sectsize));
  473. print("allocsize: %d ", b->clustsize);
  474. print("nresrv: %d ", GSHORT(b->nresrv));
  475. print("nfats: %d\n", b->nfats);
  476. print("rootsize: %d ", GSHORT(b->rootsize));
  477. print("volsize: %d ", GSHORT(b->volsize));
  478. print("mediadesc: 0x%2.2x\n", b->mediadesc);
  479. print("fatsize: %d ", GSHORT(b->fatsize));
  480. print("trksize: %d ", GSHORT(b->trksize));
  481. print("nheads: %d ", GSHORT(b->nheads));
  482. print("nhidden: %d ", GLONG(b->nhidden));
  483. print("bigvolsize: %d\n", GLONG(b->bigvolsize));
  484. /*
  485. print("driveno: %d\n", b->driveno);
  486. print("reserved0: 0x%2.2x\n", b->reserved0);
  487. print("bootsig: 0x%2.2x\n", b->bootsig);
  488. print("volid: 0x%8.8x\n", GLONG(b->volid));
  489. print("label: \"%11.11s\"\n", b->label);
  490. */
  491. }
  492. /*
  493. * set up a dos file name
  494. */
  495. static void
  496. setname(Dosfile *fp, char *from)
  497. {
  498. char *to;
  499. to = fp->name;
  500. for(; *from && to-fp->name < 8; from++, to++){
  501. if(*from == '.'){
  502. from++;
  503. break;
  504. }
  505. if(*from >= 'a' && *from <= 'z')
  506. *to = *from + 'A' - 'a';
  507. else
  508. *to = *from;
  509. }
  510. while(to - fp->name < 8)
  511. *to++ = ' ';
  512. /* from might be 12345678.123: don't save the '.' in ext */
  513. if(*from == '.')
  514. from++;
  515. to = fp->ext;
  516. for(; *from && to-fp->ext < 3; from++, to++){
  517. if(*from >= 'a' && *from <= 'z')
  518. *to = *from + 'A' - 'a';
  519. else
  520. *to = *from;
  521. }
  522. while(to-fp->ext < 3)
  523. *to++ = ' ';
  524. chat("name is %8.8s.%3.3s\n", fp->name, fp->ext);
  525. }