format.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <disk.h>
  5. /*
  6. * disk types (all MFM encoding)
  7. */
  8. typedef struct Type Type;
  9. struct Type
  10. {
  11. char *name;
  12. int bytes; /* bytes/sector */
  13. int sectors; /* sectors/track */
  14. int heads; /* number of heads */
  15. int tracks; /* tracks/disk */
  16. int media; /* media descriptor byte */
  17. int cluster; /* default cluster size */
  18. };
  19. Type floppytype[] =
  20. {
  21. { "3½HD", 512, 18, 2, 80, 0xf0, 1, },
  22. { "3½DD", 512, 9, 2, 80, 0xf9, 2, },
  23. { "3½QD", 512, 36, 2, 80, 0xf9, 2, }, /* invented */
  24. { "5¼HD", 512, 15, 2, 80, 0xf9, 1, },
  25. { "5¼DD", 512, 9, 2, 40, 0xfd, 2, },
  26. { "hard", 512, 0, 0, 0, 0xf8, 4, },
  27. };
  28. #define NTYPES (sizeof(floppytype)/sizeof(Type))
  29. typedef struct Dosboot Dosboot;
  30. struct Dosboot{
  31. uchar magic[3]; /* really an x86 JMP instruction */
  32. uchar version[8];
  33. uchar sectsize[2];
  34. uchar clustsize;
  35. uchar nresrv[2];
  36. uchar nfats;
  37. uchar rootsize[2];
  38. uchar volsize[2];
  39. uchar mediadesc;
  40. uchar fatsize[2];
  41. uchar trksize[2];
  42. uchar nheads[2];
  43. uchar nhidden[4];
  44. uchar bigvolsize[4];
  45. uchar driveno;
  46. uchar reserved0;
  47. uchar bootsig;
  48. uchar volid[4];
  49. uchar label[11];
  50. uchar type[8];
  51. };
  52. #define PUTSHORT(p, v) { (p)[1] = (v)>>8; (p)[0] = (v); }
  53. #define PUTLONG(p, v) { PUTSHORT((p), (v)); PUTSHORT((p)+2, (v)>>16); }
  54. #define GETSHORT(p) (((p)[1]<<8)|(p)[0])
  55. #define GETLONG(p) (((ulong)GETSHORT(p+2)<<16)|(ulong)GETSHORT(p))
  56. typedef struct Dosdir Dosdir;
  57. struct Dosdir
  58. {
  59. uchar name[8];
  60. uchar ext[3];
  61. uchar attr;
  62. uchar reserved[10];
  63. uchar time[2];
  64. uchar date[2];
  65. uchar start[2];
  66. uchar length[4];
  67. };
  68. #define DRONLY 0x01
  69. #define DHIDDEN 0x02
  70. #define DSYSTEM 0x04
  71. #define DVLABEL 0x08
  72. #define DDIR 0x10
  73. #define DARCH 0x20
  74. /*
  75. * the boot program for the boot sector.
  76. */
  77. int nbootprog = 188; /* no. of bytes of boot program, including the first 0x3E */
  78. uchar bootprog[512] =
  79. {
  80. [0x000] 0xEB, 0x3C, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. [0x03E] 0xFA, 0xFC, 0x8C, 0xC8, 0x8E, 0xD8, 0x8E, 0xD0,
  83. 0xBC, 0x00, 0x7C, 0xBE, 0x77, 0x7C, 0xE8, 0x19,
  84. 0x00, 0x33, 0xC0, 0xCD, 0x16, 0xBB, 0x40, 0x00,
  85. 0x8E, 0xC3, 0xBB, 0x72, 0x00, 0xB8, 0x34, 0x12,
  86. 0x26, 0x89, 0x07, 0xEA, 0x00, 0x00, 0xFF, 0xFF,
  87. 0xEB, 0xD6, 0xAC, 0x0A, 0xC0, 0x74, 0x09, 0xB4,
  88. 0x0E, 0xBB, 0x07, 0x00, 0xCD, 0x10, 0xEB, 0xF2,
  89. 0xC3, 'N', 'o', 't', ' ', 'a', ' ', 'b',
  90. 'o', 'o', 't', 'a', 'b', 'l', 'e', ' ',
  91. 'd', 'i', 's', 'c', ' ', 'o', 'r', ' ',
  92. 'd', 'i', 's', 'c', ' ', 'e', 'r', 'r',
  93. 'o', 'r', '\r', '\n', 'P', 'r', 'e', 's',
  94. 's', ' ', 'a', 'l', 'm', 'o', 's', 't',
  95. ' ', 'a', 'n', 'y', ' ', 'k', 'e', 'y',
  96. ' ', 't', 'o', ' ', 'r', 'e', 'b', 'o',
  97. 'o', 't', '.', '.', '.', 0x00, 0x00, 0x00,
  98. [0x1F0] 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA,
  100. };
  101. char *dev;
  102. int clustersize;
  103. uchar *fat; /* the fat */
  104. int fatbits;
  105. int fatsecs;
  106. int fatlast; /* last cluster allocated */
  107. int clusters;
  108. int fatsecs;
  109. vlong volsecs;
  110. uchar *root; /* first block of root */
  111. int rootsecs;
  112. int rootfiles;
  113. int rootnext;
  114. int nresrv = 1;
  115. int chatty;
  116. vlong length;
  117. Type *t;
  118. int fflag;
  119. int hflag;
  120. int xflag;
  121. char *file;
  122. char *pbs;
  123. char *type;
  124. char *bootfile;
  125. int dos;
  126. enum
  127. {
  128. Sof = 1, /* start of file */
  129. Eof = 2, /* end of file */
  130. };
  131. void dosfs(int, int, Disk*, char*, int, char*[], int);
  132. ulong clustalloc(int);
  133. void addrname(uchar*, Dir*, char*, ulong);
  134. void sanitycheck(Disk*);
  135. void
  136. usage(void)
  137. {
  138. fprint(2, "usage: disk/format [-df] [-b bootblock] [-c csize] "
  139. "[-l label] [-r nresrv] [-t type] disk [files ...]\n");
  140. exits("usage");
  141. }
  142. void
  143. fatal(char *fmt, ...)
  144. {
  145. char err[128];
  146. va_list arg;
  147. va_start(arg, fmt);
  148. vsnprint(err, sizeof(err), fmt, arg);
  149. va_end(arg);
  150. fprint(2, "format: %s\n", err);
  151. if(fflag && file)
  152. remove(file);
  153. exits(err);
  154. }
  155. void
  156. main(int argc, char **argv)
  157. {
  158. int fd, n, writepbs;
  159. char buf[512], label[11];
  160. char *a;
  161. Disk *disk;
  162. dos = 0;
  163. type = nil;
  164. clustersize = 0;
  165. writepbs = 0;
  166. memmove(label, "CYLINDRICAL", sizeof(label));
  167. ARGBEGIN {
  168. case 'b':
  169. pbs = EARGF(usage());
  170. writepbs = 1;
  171. break;
  172. case 'c':
  173. clustersize = atoi(EARGF(usage()));
  174. break;
  175. case 'd':
  176. dos = 1;
  177. writepbs = 1;
  178. break;
  179. case 'f':
  180. fflag = 1;
  181. break;
  182. case 'l':
  183. a = EARGF(usage());
  184. n = strlen(a);
  185. if(n > sizeof(label))
  186. n = sizeof(label);
  187. memmove(label, a, n);
  188. while(n < sizeof(label))
  189. label[n++] = ' ';
  190. break;
  191. case 'r':
  192. nresrv = atoi(EARGF(usage()));
  193. break;
  194. case 't':
  195. type = EARGF(usage());
  196. break;
  197. case 'v':
  198. chatty++;
  199. break;
  200. case 'x':
  201. xflag = 1;
  202. break;
  203. default:
  204. usage();
  205. } ARGEND
  206. if(argc < 1)
  207. usage();
  208. disk = opendisk(argv[0], 0, 0);
  209. if(disk == nil) {
  210. if(fflag) {
  211. if((fd = create(argv[0], ORDWR, 0666)) >= 0) {
  212. file = argv[0];
  213. close(fd);
  214. disk = opendisk(argv[0], 0, 0);
  215. }
  216. }
  217. }
  218. if(disk == nil)
  219. fatal("opendisk: %r");
  220. if(disk->type == Tfile)
  221. fflag = 1;
  222. if(type == nil) {
  223. switch(disk->type){
  224. case Tfile:
  225. type = "3½HD";
  226. break;
  227. case Tfloppy:
  228. seek(disk->ctlfd, 0, 0);
  229. n = read(disk->ctlfd, buf, 10);
  230. if(n <= 0 || n >= 10)
  231. fatal("reading floppy type");
  232. buf[n] = 0;
  233. type = strdup(buf);
  234. if(type == nil)
  235. fatal("out of memory");
  236. break;
  237. case Tsd:
  238. type = "hard";
  239. break;
  240. default:
  241. type = "unknown";
  242. break;
  243. }
  244. }
  245. if(!fflag && disk->type == Tfloppy)
  246. if(fprint(disk->ctlfd, "format %s", type) < 0)
  247. fatal("formatting floppy as %s: %r", type);
  248. if(disk->type != Tfloppy)
  249. sanitycheck(disk);
  250. /* check that everything will succeed */
  251. dosfs(dos, writepbs, disk, label, argc-1, argv+1, 0);
  252. /* commit */
  253. dosfs(dos, writepbs, disk, label, argc-1, argv+1, 1);
  254. print("used %lld bytes\n", fatlast*clustersize*disk->secsize);
  255. exits(0);
  256. }
  257. /*
  258. * Look for a partition table on sector 1, as would be the
  259. * case if we were erroneously formatting 9fat without -r 2.
  260. * If it's there and nresrv is not big enough, complain and exit.
  261. * I've blown away my partition table too many times.
  262. */
  263. void
  264. sanitycheck(Disk *disk)
  265. {
  266. char buf[512];
  267. int bad;
  268. if(xflag)
  269. return;
  270. bad = 0;
  271. if(dos && nresrv < 2 && seek(disk->fd, disk->secsize, 0) == disk->secsize
  272. && read(disk->fd, buf, sizeof(buf)) >= 5 && strncmp(buf, "part ", 5) == 0) {
  273. fprint(2,
  274. "there's a plan9 partition on the disk\n"
  275. "and you didn't specify -r 2 (or greater).\n"
  276. "either specify -r 2 or -x to disable this check.\n");
  277. bad = 1;
  278. }
  279. if(disk->type == Tsd && disk->offset == 0LL) {
  280. fprint(2,
  281. "you're attempting to format your disk (/dev/sdXX/data)\n"
  282. "rather than a partition like /dev/sdXX/9fat;\n"
  283. "this is likely a mistake. specify -x to disable this check.\n");
  284. bad = 1;
  285. }
  286. if(bad)
  287. exits("failed disk sanity check");
  288. }
  289. /*
  290. * Return the BIOS drive number for the disk.
  291. * 0x80 is the first fixed disk, 0x81 the next, etc.
  292. * We map sdC0=0x80, sdC1=0x81, sdD0=0x82, sdD1=0x83
  293. */
  294. int
  295. getdriveno(Disk *disk)
  296. {
  297. char buf[64], *p;
  298. if(disk->type != Tsd)
  299. return 0x80; /* first hard disk */
  300. if(fd2path(disk->fd, buf, sizeof(buf)) < 0)
  301. return 0x80;
  302. /*
  303. * The name is of the format #SsdC0/foo
  304. * or /dev/sdC0/foo.
  305. * So that we can just look for /sdC0, turn
  306. * #SsdC0/foo into #/sdC0/foo.
  307. */
  308. if(buf[0] == '#' && buf[1] == 'S')
  309. buf[1] = '/';
  310. for(p=buf; *p; p++)
  311. if(p[0] == 's' && p[1] == 'd' && (p[2]=='C' || p[2]=='D') &&
  312. (p[3]=='0' || p[3]=='1'))
  313. return 0x80 + (p[2]-'C')*2 + (p[3]-'0');
  314. return 0x80;
  315. }
  316. long
  317. writen(int fd, void *buf, long n)
  318. {
  319. long m, tot;
  320. /* write 8k at a time, to be nice to the disk subsystem */
  321. for(tot=0; tot<n; tot+=m){
  322. m = n - tot;
  323. if(m > 8192)
  324. m = 8192;
  325. if(write(fd, (uchar*)buf+tot, m) != m)
  326. break;
  327. }
  328. return tot;
  329. }
  330. void
  331. dosfs(int dofat, int dopbs, Disk *disk, char *label, int argc, char *argv[], int commit)
  332. {
  333. char r[16];
  334. Dosboot *b;
  335. uchar *buf, *pbsbuf, *p;
  336. Dir *d;
  337. int i, data, newclusters, npbs, n, sysfd;
  338. ulong x;
  339. vlong length, secsize;
  340. if(dofat == 0 && dopbs == 0)
  341. return;
  342. for(t = floppytype; t < &floppytype[NTYPES]; t++)
  343. if(strcmp(type, t->name) == 0)
  344. break;
  345. if(t == &floppytype[NTYPES])
  346. fatal("unknown floppy type %s", type);
  347. if(t->sectors == 0 && strcmp(type, "hard") == 0) {
  348. t->sectors = disk->s;
  349. t->heads = disk->h;
  350. t->tracks = disk->c;
  351. }
  352. if(t->sectors == 0 && dofat)
  353. fatal("cannot format fat with type %s: geometry unknown\n", type);
  354. if(fflag){
  355. disk->size = t->bytes*t->sectors*t->heads*t->tracks;
  356. disk->secsize = t->bytes;
  357. disk->secs = disk->size / disk->secsize;
  358. }
  359. secsize = disk->secsize;
  360. length = disk->size;
  361. buf = malloc(secsize);
  362. if(buf == 0)
  363. fatal("out of memory");
  364. /*
  365. * Make disk full size if a file.
  366. */
  367. if(fflag && disk->type == Tfile){
  368. if((d = dirfstat(disk->wfd)) == nil)
  369. fatal("fstat disk: %r");
  370. if(commit && d->length < disk->size) {
  371. if(seek(disk->wfd, disk->size-1, 0) < 0)
  372. fatal("seek to 9: %r");
  373. if(write(disk->wfd, "9", 1) < 0)
  374. fatal("writing 9: @%lld %r", seek(disk->wfd, 0LL, 1));
  375. }
  376. free(d);
  377. }
  378. /*
  379. * Start with initial sector from disk
  380. */
  381. if(seek(disk->fd, 0, 0) < 0)
  382. fatal("seek to boot sector: %r\n");
  383. if(commit && read(disk->fd, buf, secsize) != secsize)
  384. fatal("reading boot sector: %r");
  385. if(dofat)
  386. memset(buf, 0, sizeof(Dosboot));
  387. /*
  388. * Jump instruction and OEM name.
  389. */
  390. b = (Dosboot*)buf;
  391. b->magic[0] = 0xEB;
  392. b->magic[1] = 0x3C;
  393. b->magic[2] = 0x90;
  394. memmove(b->version, "Plan9.00", sizeof(b->version));
  395. /*
  396. * Add bootstrapping code; assume it starts
  397. * at 0x3E (the destination of the jump we just
  398. * wrote to b->magic).
  399. */
  400. if(dopbs) {
  401. pbsbuf = malloc(secsize);
  402. if(pbsbuf == 0)
  403. fatal("out of memory");
  404. if(pbs){
  405. if((sysfd = open(pbs, OREAD)) < 0)
  406. fatal("open %s: %r", pbs);
  407. if((npbs = read(sysfd, pbsbuf, secsize)) < 0)
  408. fatal("read %s: %r", pbs);
  409. if(npbs > secsize-2)
  410. fatal("boot block too large");
  411. close(sysfd);
  412. }
  413. else {
  414. memmove(pbsbuf, bootprog, sizeof(bootprog));
  415. npbs = nbootprog;
  416. }
  417. if(npbs <= 0x3E)
  418. fprint(2, "warning: pbs too small\n");
  419. else
  420. memmove(buf+0x3E, pbsbuf+0x3E, npbs-0x3E);
  421. free(pbsbuf);
  422. }
  423. /*
  424. * Add FAT BIOS parameter block.
  425. */
  426. if(dofat) {
  427. if(commit) {
  428. print("Initializing FAT file system\n");
  429. print("type %s, %d tracks, %d heads, %d sectors/track, %lld bytes/sec\n",
  430. t->name, t->tracks, t->heads, t->sectors, secsize);
  431. }
  432. if(clustersize == 0)
  433. clustersize = t->cluster;
  434. /*
  435. * the number of fat bits depends on how much disk is left
  436. * over after you subtract out the space taken up by the fat tables.
  437. * try both. what a crock.
  438. */
  439. fatbits = 12;
  440. Tryagain:
  441. volsecs = length/secsize;
  442. /*
  443. * here's a crock inside a crock. even having fixed fatbits,
  444. * the number of fat sectors depends on the number of clusters,
  445. * but of course we don't know yet. maybe iterating will get us there.
  446. * or maybe it will cycle.
  447. */
  448. clusters = 0;
  449. for(i=0;; i++){
  450. fatsecs = (fatbits*clusters + 8*secsize - 1)/(8*secsize);
  451. rootsecs = volsecs/200;
  452. rootfiles = rootsecs * (secsize/sizeof(Dosdir));
  453. if(rootfiles > 512){
  454. rootfiles = 512;
  455. rootsecs = rootfiles/(secsize/sizeof(Dosdir));
  456. }
  457. data = nresrv + 2*fatsecs + (rootfiles*sizeof(Dosdir) + secsize-1)/secsize;
  458. newclusters = 2 + (volsecs - data)/clustersize;
  459. if(newclusters == clusters)
  460. break;
  461. clusters = newclusters;
  462. if(i > 10)
  463. fatal("can't decide how many clusters to use (%d? %d?)", clusters, newclusters);
  464. if(chatty) print("clusters %d\n", clusters);
  465. }
  466. if(chatty) print("try %d fatbits => %d clusters of %d\n", fatbits, clusters, clustersize);
  467. switch(fatbits){
  468. case 12:
  469. if(clusters >= 4087){
  470. fatbits = 16;
  471. goto Tryagain;
  472. }
  473. break;
  474. case 16:
  475. if(clusters >= 65527)
  476. fatal("disk too big; implement fat32");
  477. break;
  478. }
  479. PUTSHORT(b->sectsize, secsize);
  480. b->clustsize = clustersize;
  481. PUTSHORT(b->nresrv, nresrv);
  482. b->nfats = 2;
  483. PUTSHORT(b->rootsize, rootfiles);
  484. if(volsecs < (1<<16))
  485. PUTSHORT(b->volsize, volsecs);
  486. b->mediadesc = t->media;
  487. PUTSHORT(b->fatsize, fatsecs);
  488. PUTSHORT(b->trksize, t->sectors);
  489. PUTSHORT(b->nheads, t->heads);
  490. PUTLONG(b->nhidden, disk->offset);
  491. PUTLONG(b->bigvolsize, volsecs);
  492. /*
  493. * Extended BIOS Parameter Block.
  494. */
  495. if(t->media == 0xF8)
  496. b->driveno = getdriveno(disk);
  497. else
  498. b->driveno = 0;
  499. if(chatty) print("driveno = %ux\n", b->driveno);
  500. b->bootsig = 0x29;
  501. x = disk->offset + b->nfats*fatsecs + nresrv;
  502. PUTLONG(b->volid, x);
  503. if(chatty) print("volid = %lux %lux\n", x, GETLONG(b->volid));
  504. memmove(b->label, label, sizeof(b->label));
  505. sprint(r, "FAT%d ", fatbits);
  506. memmove(b->type, r, sizeof(b->type));
  507. }
  508. buf[secsize-2] = 0x55;
  509. buf[secsize-1] = 0xAA;
  510. if(commit) {
  511. if(seek(disk->wfd, 0, 0) < 0)
  512. fatal("seek to boot sector: %r\n");
  513. if(write(disk->wfd, buf, secsize) != secsize)
  514. fatal("writing boot sector: %r");
  515. }
  516. free(buf);
  517. /*
  518. * If we were only called to write the PBS, leave now.
  519. */
  520. if(dofat == 0)
  521. return;
  522. /*
  523. * allocate an in memory fat
  524. */
  525. if(seek(disk->wfd, nresrv*secsize, 0) < 0)
  526. fatal("seek to fat: %r\n");
  527. if(chatty) print("fat @%lluX\n", seek(disk->wfd, 0, 1));
  528. fat = malloc(fatsecs*secsize);
  529. if(fat == 0)
  530. fatal("out of memory");
  531. memset(fat, 0, fatsecs*secsize);
  532. fat[0] = t->media;
  533. fat[1] = 0xff;
  534. fat[2] = 0xff;
  535. if(fatbits == 16)
  536. fat[3] = 0xff;
  537. fatlast = 1;
  538. if(seek(disk->wfd, 2*fatsecs*secsize, 1) < 0) /* 2 fats */
  539. fatal("seek to root: %r");
  540. if(chatty) print("root @%lluX\n", seek(disk->wfd, 0LL, 1));
  541. /*
  542. * allocate an in memory root
  543. */
  544. root = malloc(rootsecs*secsize);
  545. if(root == 0)
  546. fatal("out of memory");
  547. memset(root, 0, rootsecs*secsize);
  548. if(seek(disk->wfd, rootsecs*secsize, 1) < 0) /* rootsecs */
  549. fatal("seek to files: %r");
  550. if(chatty) print("files @%lluX\n", seek(disk->wfd, 0LL, 1));
  551. /*
  552. * Now positioned at the Files Area.
  553. * If we have any arguments, process
  554. * them and write out.
  555. */
  556. for(p = root; argc > 0; argc--, argv++, p += sizeof(Dosdir)){
  557. if(p >= (root+(rootsecs*secsize)))
  558. fatal("too many files in root");
  559. /*
  560. * Open the file and get its length.
  561. */
  562. if((sysfd = open(*argv, OREAD)) < 0)
  563. fatal("open %s: %r", *argv);
  564. if((d = dirfstat(sysfd)) == nil)
  565. fatal("stat %s: %r", *argv);
  566. if(d->length > 0xFFFFFFFFU)
  567. fatal("file %s too big\n", *argv, d->length);
  568. if(commit)
  569. print("Adding file %s, length %lld\n", *argv, d->length);
  570. length = d->length;
  571. if(length){
  572. /*
  573. * Allocate a buffer to read the entire file into.
  574. * This must be rounded up to a cluster boundary.
  575. *
  576. * Read the file and write it out to the Files Area.
  577. */
  578. length += secsize*clustersize - 1;
  579. length /= secsize*clustersize;
  580. length *= secsize*clustersize;
  581. if((buf = malloc(length)) == 0)
  582. fatal("out of memory");
  583. if(readn(sysfd, buf, d->length) != d->length)
  584. fatal("read %s: %r", *argv);
  585. memset(buf+d->length, 0, length-d->length);
  586. if(chatty) print("%s @%lluX\n", d->name, seek(disk->wfd, 0LL, 1));
  587. if(commit && writen(disk->wfd, buf, length) != length)
  588. fatal("write %s: %r", *argv);
  589. free(buf);
  590. close(sysfd);
  591. /*
  592. * Allocate the FAT clusters.
  593. * We're assuming here that where we
  594. * wrote the file is in sync with
  595. * the cluster allocation.
  596. * Save the starting cluster.
  597. */
  598. length /= secsize*clustersize;
  599. x = clustalloc(Sof);
  600. for(n = 0; n < length-1; n++)
  601. clustalloc(0);
  602. clustalloc(Eof);
  603. }
  604. else
  605. x = 0;
  606. /*
  607. * Add the filename to the root.
  608. */
  609. fprint(2, "add %s at clust %lux\n", d->name, x);
  610. addrname(p, d, *argv, x);
  611. free(d);
  612. }
  613. /*
  614. * write the fats and root
  615. */
  616. if(commit) {
  617. if(seek(disk->wfd, nresrv*secsize, 0) < 0)
  618. fatal("seek to fat #1: %r");
  619. if(write(disk->wfd, fat, fatsecs*secsize) < 0)
  620. fatal("writing fat #1: %r");
  621. if(write(disk->wfd, fat, fatsecs*secsize) < 0)
  622. fatal("writing fat #2: %r");
  623. if(write(disk->wfd, root, rootsecs*secsize) < 0)
  624. fatal("writing root: %r");
  625. }
  626. free(fat);
  627. free(root);
  628. }
  629. /*
  630. * allocate a cluster
  631. */
  632. ulong
  633. clustalloc(int flag)
  634. {
  635. ulong o, x;
  636. if(flag != Sof){
  637. x = (flag == Eof) ? 0xffff : (fatlast+1);
  638. if(fatbits == 12){
  639. x &= 0xfff;
  640. o = (3*fatlast)/2;
  641. if(fatlast & 1){
  642. fat[o] = (fat[o]&0x0f) | (x<<4);
  643. fat[o+1] = (x>>4);
  644. } else {
  645. fat[o] = x;
  646. fat[o+1] = (fat[o+1]&0xf0) | ((x>>8) & 0x0F);
  647. }
  648. } else {
  649. o = 2*fatlast;
  650. fat[o] = x;
  651. fat[o+1] = x>>8;
  652. }
  653. }
  654. if(flag == Eof)
  655. return 0;
  656. else{
  657. ++fatlast;
  658. if(fatlast >= clusters)
  659. sysfatal("data does not fit on disk (%d %d)", fatlast, clusters);
  660. return fatlast;
  661. }
  662. }
  663. void
  664. putname(char *p, Dosdir *d)
  665. {
  666. int i;
  667. memset(d->name, ' ', sizeof d->name+sizeof d->ext);
  668. for(i = 0; i< sizeof(d->name); i++){
  669. if(*p == 0 || *p == '.')
  670. break;
  671. d->name[i] = toupper(*p++);
  672. }
  673. p = strrchr(p, '.');
  674. if(p){
  675. for(i = 0; i < sizeof d->ext; i++){
  676. if(*++p == 0)
  677. break;
  678. d->ext[i] = toupper(*p);
  679. }
  680. }
  681. }
  682. void
  683. puttime(Dosdir *d)
  684. {
  685. Tm *t = localtime(time(0));
  686. ushort x;
  687. x = (t->hour<<11) | (t->min<<5) | (t->sec>>1);
  688. d->time[0] = x;
  689. d->time[1] = x>>8;
  690. x = ((t->year-80)<<9) | ((t->mon+1)<<5) | t->mday;
  691. d->date[0] = x;
  692. d->date[1] = x>>8;
  693. }
  694. void
  695. addrname(uchar *entry, Dir *dir, char *name, ulong start)
  696. {
  697. char *s;
  698. Dosdir *d;
  699. s = strrchr(name, '/');
  700. if(s)
  701. s++;
  702. else
  703. s = name;
  704. d = (Dosdir*)entry;
  705. putname(s, d);
  706. if(strcmp(s, "9load") == 0)
  707. d->attr = DSYSTEM;
  708. else
  709. d->attr = 0;
  710. puttime(d);
  711. d->start[0] = start;
  712. d->start[1] = start>>8;
  713. d->length[0] = dir->length;
  714. d->length[1] = dir->length>>8;
  715. d->length[2] = dir->length>>16;
  716. d->length[3] = dir->length>>24;
  717. }