dosboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. sect = (k/(dos->sectsize*dos->clustsize))*dos->clustsize + dos->fataddr;
  168. o = k%(dos->sectsize*dos->clustsize);
  169. p = getclust(dos, sect);
  170. k = p->iobuf[o++];
  171. if(o >= dos->sectsize*dos->clustsize){
  172. p = getclust(dos, sect+dos->clustsize);
  173. o = 0;
  174. }
  175. k |= p->iobuf[o++]<<8;
  176. if(dos->fatbits == 12){
  177. if(n&1)
  178. k >>= 4;
  179. else
  180. k &= 0xfff;
  181. if(k >= 0xff8)
  182. k = -1;
  183. }
  184. else if (dos->fatbits == 32){
  185. if(o >= dos->sectsize*dos->clustsize){
  186. p = getclust(dos, sect+dos->clustsize);
  187. o = 0;
  188. }
  189. k |= p->iobuf[o++]<<16;
  190. k |= p->iobuf[o]<<24;
  191. if (k >= 0xfffffff8)
  192. k = -1;
  193. }
  194. else
  195. k = k < 0xfff8 ? k : -1;
  196. chat("fatwalk %d -> %lud\n", n, k);
  197. return k;
  198. }
  199. /*
  200. * map a file's logical cluster address to a physical sector address
  201. */
  202. static long
  203. fileaddr(Dosfile *fp, long ltarget)
  204. {
  205. Dos *dos = fp->dos;
  206. long l;
  207. long p;
  208. chat("fileaddr %8.8s %ld\n", fp->name, ltarget);
  209. /*
  210. * root directory is contiguous and easy (unless FAT32)
  211. */
  212. if(fp->pstart == 0 && dos->rootsize != 0) {
  213. if(ltarget*dos->sectsize*dos->clustsize >= dos->rootsize*sizeof(Dosdir))
  214. return -1;
  215. l = dos->rootaddr + ltarget*dos->clustsize;
  216. chat("fileaddr %ld -> %ld\n", ltarget, l);
  217. return l;
  218. }
  219. /*
  220. * anything else requires a walk through the fat
  221. */
  222. if(ltarget >= fp->lcurrent && fp->pcurrent){
  223. /* start at the currrent point */
  224. l = fp->lcurrent;
  225. p = fp->pcurrent;
  226. } else {
  227. /* go back to the beginning */
  228. l = 0;
  229. p = fp->pstart;
  230. }
  231. while(l != ltarget){
  232. /* walk the fat */
  233. p = fatwalk(dos, p);
  234. if(p < 0)
  235. return -1;
  236. l++;
  237. }
  238. fp->lcurrent = l;
  239. fp->pcurrent = p;
  240. /*
  241. * clusters start at 2 instead of 0 (why? - presotto)
  242. */
  243. l = dos->dataaddr + (p-2)*dos->clustsize;
  244. chat("fileaddr %ld -> %ld\n", ltarget, l);
  245. return l;
  246. }
  247. /*
  248. * read from a dos file
  249. */
  250. long
  251. dosread(Dosfile *fp, void *a, long n)
  252. {
  253. long addr;
  254. long rv;
  255. int i;
  256. int off;
  257. Clustbuf *p;
  258. uchar *from, *to;
  259. if((fp->attr & DOSDIR) == 0){
  260. if(fp->offset >= fp->length)
  261. return 0;
  262. if(fp->offset+n > fp->length)
  263. n = fp->length - fp->offset;
  264. }
  265. to = a;
  266. for(rv = 0; rv < n; rv+=i){
  267. /*
  268. * read the cluster
  269. */
  270. addr = fileaddr(fp, fp->offset/fp->dos->clustbytes);
  271. if(addr < 0)
  272. return -1;
  273. p = getclust(fp->dos, addr);
  274. if(p == 0)
  275. return -1;
  276. /*
  277. * copy the bytes we need
  278. */
  279. off = fp->offset % fp->dos->clustbytes;
  280. from = &p->iobuf[off];
  281. i = n - rv;
  282. if(i > fp->dos->clustbytes - off)
  283. i = fp->dos->clustbytes - off;
  284. memmove(to, from, i);
  285. to += i;
  286. fp->offset += i;
  287. }
  288. return rv;
  289. }
  290. /*
  291. * walk a directory returns
  292. * -1 if something went wrong
  293. * 0 if not found
  294. * 1 if found
  295. */
  296. int
  297. doswalk(File *f, char *name)
  298. {
  299. Dosdir d;
  300. long n;
  301. Dosfile *file;
  302. chat("doswalk %s\n", name);
  303. file = &f->dos;
  304. if((file->attr & DOSDIR) == 0){
  305. chat("walking non-directory!\n");
  306. return -1;
  307. }
  308. setname(file, name);
  309. file->offset = 0; /* start at the beginning */
  310. while((n = dosread(file, &d, sizeof(d))) == sizeof(d)){
  311. chat("comparing to %8.8s.%3.3s\n", (char*)d.name, (char*)d.ext);
  312. if(memcmp(file->name, d.name, sizeof(d.name)) != 0)
  313. continue;
  314. if(memcmp(file->ext, d.ext, sizeof(d.ext)) != 0)
  315. continue;
  316. if(d.attr & DOSVLABEL){
  317. chat("%8.8s.%3.3s is a LABEL\n", (char*)d.name, (char*)d.ext);
  318. continue;
  319. }
  320. file->attr = d.attr;
  321. file->pstart = GSHORT(d.start);
  322. if (file->dos->fatbits == 32)
  323. file->pstart |= GSHORT(d.highstart) << 16;
  324. file->length = GLONG(d.length);
  325. file->pcurrent = 0;
  326. file->lcurrent = 0;
  327. file->offset = 0;
  328. return 1;
  329. }
  330. return n >= 0 ? 0 : -1;
  331. }
  332. /*
  333. * instructions that boot blocks can start with
  334. */
  335. #define JMPSHORT 0xeb
  336. #define JMPNEAR 0xe9
  337. /*
  338. * read in a segment
  339. */
  340. long
  341. dosreadseg(File *f, void *va, long len)
  342. {
  343. char *a;
  344. long n, sofar;
  345. Dosfile *fp;
  346. fp = &f->dos;
  347. a = va;
  348. for(sofar = 0; sofar < len; sofar += n){
  349. n = 8*1024;
  350. if(len - sofar < n)
  351. n = len - sofar;
  352. n = dosread(fp, a + sofar, n);
  353. if(n <= 0)
  354. break;
  355. print(".");
  356. }
  357. return sofar;
  358. }
  359. int
  360. dosinit(Fs *fs)
  361. {
  362. Clustbuf *p;
  363. Dosboot *b;
  364. int i;
  365. Dos *dos;
  366. Dosfile *root;
  367. chat("dosinit0 %p %p %p\n", fs, fs->diskseek, fs->diskread);
  368. dos = &fs->dos;
  369. /* defaults till we know better */
  370. dos->sectsize = 512;
  371. dos->clustsize = 1;
  372. /* get first sector */
  373. p = getclust(dos, 0);
  374. if(p == 0){
  375. chat("can't read boot block\n");
  376. return -1;
  377. }
  378. chat("dosinit0a\n");
  379. p->dos = 0;
  380. b = (Dosboot *)p->iobuf;
  381. if(b->magic[0] != JMPNEAR && (b->magic[0] != JMPSHORT || b->magic[2] != 0x90)){
  382. chat("no dos file system %x %x %x %x\n",
  383. b->magic[0], b->magic[1], b->magic[2], b->magic[3]);
  384. return -1;
  385. }
  386. if(chatty)
  387. bootdump(b);
  388. if(b->clustsize == 0) {
  389. unreasonable:
  390. if(chatty){
  391. print("unreasonable FAT BPB: ");
  392. for(i=0; i<3+8+2+1; i++)
  393. print(" %.2ux", p->iobuf[i]);
  394. print("\n");
  395. }
  396. return -1;
  397. }
  398. chat("dosinit1\n");
  399. /*
  400. * Determine the systems' wondrous properties.
  401. * There are heuristics here, but there's no real way
  402. * of knowing if this is a reasonable FAT.
  403. */
  404. dos->fatbits = 0;
  405. dos->sectsize = GSHORT(b->sectsize);
  406. if(dos->sectsize & 0xFF)
  407. goto unreasonable;
  408. dos->clustsize = b->clustsize;
  409. dos->clustbytes = dos->sectsize*dos->clustsize;
  410. dos->nresrv = GSHORT(b->nresrv);
  411. dos->nfats = b->nfats;
  412. dos->fatsize = GSHORT(b->fatsize);
  413. dos->rootsize = GSHORT(b->rootsize);
  414. dos->volsize = GSHORT(b->volsize);
  415. if(dos->volsize == 0)
  416. dos->volsize = GLONG(b->bigvolsize);
  417. dos->mediadesc = b->mediadesc;
  418. if(dos->fatsize == 0) {
  419. chat("fat32\n");
  420. dos->rootsize = 0;
  421. dos->fatsize = GLONG(b->bigfatsize);
  422. dos->fatbits = 32;
  423. }
  424. dos->fataddr = dos->nresrv;
  425. if (dos->rootsize == 0) {
  426. dos->rootaddr = 0;
  427. dos->rootclust = GLONG(b->rootdirstartclust);
  428. dos->dataaddr = dos->fataddr + dos->nfats*dos->fatsize;
  429. } else {
  430. dos->rootaddr = dos->fataddr + dos->nfats*dos->fatsize;
  431. i = dos->rootsize*sizeof(Dosdir) + dos->sectsize - 1;
  432. i = i/dos->sectsize;
  433. dos->dataaddr = dos->rootaddr + i;
  434. }
  435. dos->fatclusters = 2+(dos->volsize - dos->dataaddr)/dos->clustsize;
  436. if(dos->fatbits != 32) {
  437. if(dos->fatclusters < 4087)
  438. dos->fatbits = 12;
  439. else
  440. dos->fatbits = 16;
  441. }
  442. dos->freeptr = 2;
  443. if(dos->clustbytes < 512 || dos->clustbytes > 64*1024)
  444. goto unreasonable;
  445. chat("dosinit2\n");
  446. /*
  447. * set up the root
  448. */
  449. fs->root.fs = fs;
  450. root = &fs->root.dos;
  451. root->dos = dos;
  452. root->pstart = dos->rootsize == 0 ? dos->rootclust : 0;
  453. root->pcurrent = root->lcurrent = 0;
  454. root->offset = 0;
  455. root->attr = DOSDIR;
  456. root->length = dos->rootsize*sizeof(Dosdir);
  457. chat("dosinit3\n");
  458. fs->read = dosreadseg;
  459. fs->walk = doswalk;
  460. return 0;
  461. }
  462. static void
  463. bootdump(Dosboot *b)
  464. {
  465. if(chatty == 0)
  466. return;
  467. print("magic: 0x%2.2x 0x%2.2x 0x%2.2x ",
  468. b->magic[0], b->magic[1], b->magic[2]);
  469. print("version: \"%8.8s\"\n", (char*)b->version);
  470. print("sectsize: %d ", GSHORT(b->sectsize));
  471. print("allocsize: %d ", b->clustsize);
  472. print("nresrv: %d ", GSHORT(b->nresrv));
  473. print("nfats: %d\n", b->nfats);
  474. print("rootsize: %d ", GSHORT(b->rootsize));
  475. print("volsize: %d ", GSHORT(b->volsize));
  476. print("mediadesc: 0x%2.2x\n", b->mediadesc);
  477. print("fatsize: %d ", GSHORT(b->fatsize));
  478. print("trksize: %d ", GSHORT(b->trksize));
  479. print("nheads: %d ", GSHORT(b->nheads));
  480. print("nhidden: %d ", GLONG(b->nhidden));
  481. print("bigvolsize: %d\n", GLONG(b->bigvolsize));
  482. /*
  483. print("driveno: %d\n", b->driveno);
  484. print("reserved0: 0x%2.2x\n", b->reserved0);
  485. print("bootsig: 0x%2.2x\n", b->bootsig);
  486. print("volid: 0x%8.8x\n", GLONG(b->volid));
  487. print("label: \"%11.11s\"\n", b->label);
  488. */
  489. }
  490. /*
  491. * set up a dos file name
  492. */
  493. static void
  494. setname(Dosfile *fp, char *from)
  495. {
  496. char *to;
  497. to = fp->name;
  498. for(; *from && to-fp->name < 8; from++, to++){
  499. if(*from == '.'){
  500. from++;
  501. break;
  502. }
  503. if(*from >= 'a' && *from <= 'z')
  504. *to = *from + 'A' - 'a';
  505. else
  506. *to = *from;
  507. }
  508. while(to - fp->name < 8)
  509. *to++ = ' ';
  510. /* from might be 12345678.123: don't save the '.' in ext */
  511. if(*from == '.')
  512. from++;
  513. to = fp->ext;
  514. for(; *from && to-fp->ext < 3; from++, to++){
  515. if(*from >= 'a' && *from <= 'z')
  516. *to = *from + 'A' - 'a';
  517. else
  518. *to = *from;
  519. }
  520. while(to-fp->ext < 3)
  521. *to++ = ' ';
  522. chat("name is %8.8s.%3.3s\n", fp->name, fp->ext);
  523. }