7 * disk types (all MFM encoding)
9 typedef struct Type Type;
13 int bytes; /* bytes/sector */
14 int sectors; /* sectors/track */
15 int heads; /* number of heads */
16 int tracks; /* tracks/disk */
17 int media; /* media descriptor byte */
18 int cluster; /* default cluster size */
22 { "3½HD", 512, 18, 2, 80, 0xf0, 1, },
23 { "3½DD", 512, 9, 2, 80, 0xf9, 2, },
24 { "3½QD", 512, 36, 2, 80, 0xf9, 2, }, /* invented */
25 { "5¼HD", 512, 15, 2, 80, 0xf9, 1, },
26 { "5¼DD", 512, 9, 2, 40, 0xfd, 2, },
27 { "hard", 512, 0, 0, 0, 0xf8, 4, },
30 #define NTYPES (sizeof(floppytype)/sizeof(Type))
32 typedef struct Dosboot Dosboot;
34 uchar magic[3]; /* really an x86 JMP instruction */
55 #define PUTSHORT(p, v) { (p)[1] = (v)>>8; (p)[0] = (v); }
56 #define PUTLONG(p, v) { PUTSHORT((p), (v)); PUTSHORT((p)+2, (v)>>16); }
57 #define GETSHORT(p) (((p)[1]<<8)|(p)[0])
58 #define GETLONG(p) (((ulong)GETSHORT(p+2)<<16)|(ulong)GETSHORT(p))
60 typedef struct Dosdir Dosdir;
81 * the boot program for the boot sector.
83 int nbootprog = 188; /* no. of bytes of boot program, including the first 0x3E */
86 [0x000] 0xEB, 0x3C, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 [0x03E] 0xFA, 0xFC, 0x8C, 0xC8, 0x8E, 0xD8, 0x8E, 0xD0,
89 0xBC, 0x00, 0x7C, 0xBE, 0x77, 0x7C, 0xE8, 0x19,
90 0x00, 0x33, 0xC0, 0xCD, 0x16, 0xBB, 0x40, 0x00,
91 0x8E, 0xC3, 0xBB, 0x72, 0x00, 0xB8, 0x34, 0x12,
92 0x26, 0x89, 0x07, 0xEA, 0x00, 0x00, 0xFF, 0xFF,
93 0xEB, 0xD6, 0xAC, 0x0A, 0xC0, 0x74, 0x09, 0xB4,
94 0x0E, 0xBB, 0x07, 0x00, 0xCD, 0x10, 0xEB, 0xF2,
95 0xC3, 'N', 'o', 't', ' ', 'a', ' ', 'b',
96 'o', 'o', 't', 'a', 'b', 'l', 'e', ' ',
97 'd', 'i', 's', 'c', ' ', 'o', 'r', ' ',
98 'd', 'i', 's', 'c', ' ', 'e', 'r', 'r',
99 'o', 'r', '\r', '\n', 'P', 'r', 'e', 's',
100 's', ' ', 'a', 'l', 'm', 'o', 's', 't',
101 ' ', 'a', 'n', 'y', ' ', 'k', 'e', 'y',
102 ' ', 't', 'o', ' ', 'r', 'e', 'b', 'o',
103 'o', 't', '.', '.', '.', 0x00, 0x00, 0x00,
104 [0x1F0] 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA,
110 uchar *fat; /* the fat */
113 int fatlast; /* last cluster allocated */
117 uchar *root; /* first block of root */
136 Sof = 1, /* start of file */
137 Eof = 2, /* end of file */
140 void dosfs(int, int, Disk*, char*, int, char*[], int);
141 ulong clustalloc(int);
142 void addrname(uchar*, Dir*, char*, ulong);
143 void sanitycheck(Disk*);
148 fprint(2, "usage: disk/format [-df] [-b bootblock] [-c csize] "
149 "[-l label] [-r nresrv] [-t type] disk [files ...]\n");
154 fatal(char *fmt, ...)
160 vsnprint(err, sizeof(err), fmt, arg);
162 fprint(2, "format: %s\n", err);
169 main(int argc, char **argv)
172 char buf[512], label[11];
180 memmove(label, "CYLINDRICAL", sizeof(label));
183 pbs = EARGF(usage());
187 clustersize = atoi(EARGF(usage()));
199 if(n > sizeof(label))
201 memmove(label, a, n);
202 while(n < sizeof(label))
206 nresrv = atoi(EARGF(usage()));
209 type = EARGF(usage());
224 disk = opendisk(argv[0], 0, 0);
227 if((fd = create(argv[0], ORDWR, 0666)) >= 0) {
230 disk = opendisk(argv[0], 0, 0);
235 fatal("opendisk: %r");
237 if(disk->type == Tfile)
246 seek(disk->ctlfd, 0, 0);
247 n = read(disk->ctlfd, buf, 10);
248 if(n <= 0 || n >= 10)
249 fatal("reading floppy type");
253 fatal("out of memory");
264 if(!fflag && disk->type == Tfloppy)
265 if(fprint(disk->ctlfd, "format %s", type) < 0)
266 fatal("formatting floppy as %s: %r", type);
268 if(disk->type != Tfloppy)
271 /* check that everything will succeed */
272 dosfs(dos, writepbs, disk, label, argc-1, argv+1, 0);
275 dosfs(dos, writepbs, disk, label, argc-1, argv+1, 1);
277 print("used %lld bytes\n", fatlast*clustersize*disk->secsize);
282 * Look for a partition table on sector 1, as would be the
283 * case if we were erroneously formatting 9fat without -r 2.
284 * If it's there and nresrv is not big enough, complain and exit.
285 * I've blown away my partition table too many times.
288 sanitycheck(Disk *disk)
297 if(dos && nresrv < 2 && seek(disk->fd, disk->secsize, 0) == disk->secsize
298 && read(disk->fd, buf, sizeof(buf)) >= 5 && strncmp(buf, "part ", 5) == 0) {
300 "there's a plan9 partition on the disk\n"
301 "and you didn't specify -r 2 (or greater).\n"
302 "either specify -r 2 or -x to disable this check.\n");
306 if(disk->type == Tsd && disk->offset == 0LL) {
308 "you're attempting to format your disk (/dev/sdXX/data)\n"
309 "rather than a partition like /dev/sdXX/9fat;\n"
310 "this is likely a mistake. specify -x to disable this check.\n");
315 exits("failed disk sanity check");
319 * Return the BIOS drive number for the disk.
320 * 0x80 is the first fixed disk, 0x81 the next, etc.
321 * We map sdC0=0x80, sdC1=0x81, sdD0=0x82, sdD1=0x83
324 getdriveno(Disk *disk)
328 if(disk->type != Tsd)
329 return 0x80; /* first hard disk */
331 if(fd2path(disk->fd, buf, sizeof(buf)) < 0)
335 * The name is of the format #SsdC0/foo
337 * So that we can just look for /sdC0, turn
338 * #SsdC0/foo into #/sdC0/foo.
340 if(buf[0] == '#' && buf[1] == 'S')
344 if(p[0] == 's' && p[1] == 'd' && (p[2]=='C' || p[2]=='D') &&
345 (p[3]=='0' || p[3]=='1'))
346 return 0x80 + (p[2]-'C')*2 + (p[3]-'0');
352 writen(int fd, void *buf, long n)
356 /* write 8k at a time, to be nice to the disk subsystem */
357 for(tot=0; tot<n; tot+=m){
361 if(write(fd, (uchar*)buf+tot, m) != m)
368 dosfs(int dofat, int dopbs, Disk *disk, char *label, int argc, char *argv[], int commit)
372 uchar *buf, *pbsbuf, *p;
374 int i, data, newclusters, npbs, n, sysfd;
376 vlong length, secsize;
378 if(dofat == 0 && dopbs == 0)
381 for(t = floppytype; t < &floppytype[NTYPES]; t++)
382 if(strcmp(type, t->name) == 0)
384 if(t == &floppytype[NTYPES])
385 fatal("unknown floppy type %s", type);
387 if(t->sectors == 0 && strcmp(type, "hard") == 0) {
388 t->sectors = disk->s;
393 if(t->sectors == 0 && dofat)
394 fatal("cannot format fat with type %s: geometry unknown\n", type);
397 disk->size = t->bytes*t->sectors*t->heads*t->tracks;
398 disk->secsize = t->bytes;
399 disk->secs = disk->size / disk->secsize;
402 secsize = disk->secsize;
405 buf = malloc(secsize);
407 fatal("out of memory");
410 * Make disk full size if a file.
412 if(fflag && disk->type == Tfile){
413 if((d = dirfstat(disk->wfd)) == nil)
414 fatal("fstat disk: %r");
415 if(commit && d->length < disk->size) {
416 if(seek(disk->wfd, disk->size-1, 0) < 0)
417 fatal("seek to 9: %r");
418 if(write(disk->wfd, "9", 1) < 0)
419 fatal("writing 9: @%lld %r", seek(disk->wfd, 0LL, 1));
425 * Start with initial sector from disk
427 if(seek(disk->fd, 0, 0) < 0)
428 fatal("seek to boot sector: %r\n");
429 if(commit && read(disk->fd, buf, secsize) != secsize)
430 fatal("reading boot sector: %r");
433 memset(buf, 0, sizeof(Dosboot));
436 * Jump instruction and OEM name.
442 memmove(b->version, "Plan9.00", sizeof(b->version));
445 * Add bootstrapping code; offset is
446 * determined from short jump (0xEB 0x??)
450 pbsbuf = malloc(secsize);
452 fatal("out of memory");
455 if((sysfd = open(pbs, OREAD)) < 0)
456 fatal("open %s: %r", pbs);
457 if((npbs = read(sysfd, pbsbuf, secsize)) < 0)
458 fatal("read %s: %r", pbs);
461 fatal("boot block too large");
466 memmove(pbsbuf, bootprog, sizeof(bootprog));
470 if(npbs <= 0x3 || npbs < n)
471 fprint(2, "warning: pbs too small\n");
472 else if(buf[0] != 0xEB)
473 fprint(2, "warning: pbs doesn't start with short jump\n");
475 memmove(buf, pbsbuf, 3);
476 memmove(buf+n, pbsbuf+n, npbs-n);
482 * Add FAT BIOS parameter block.
486 print("Initializing FAT file system\n");
487 print("type %s, %d tracks, %d heads, %d sectors/track, %lld bytes/sec\n",
488 t->name, t->tracks, t->heads, t->sectors, secsize);
492 clustersize = t->cluster;
494 * the number of fat bits depends on how much disk is left
495 * over after you subtract out the space taken up by the fat tables.
496 * try both. what a crock.
500 volsecs = length/secsize;
502 * here's a crock inside a crock. even having fixed fatbits,
503 * the number of fat sectors depends on the number of clusters,
504 * but of course we don't know yet. maybe iterating will get us there.
505 * or maybe it will cycle.
509 fatsecs = (fatbits*clusters + 8*secsize - 1)/(8*secsize);
510 rootsecs = volsecs/200;
511 rootfiles = rootsecs * (secsize/sizeof(Dosdir));
514 rootsecs = rootfiles/(secsize/sizeof(Dosdir));
516 data = nresrv + 2*fatsecs + (rootfiles*sizeof(Dosdir) + secsize-1)/secsize;
517 newclusters = 2 + (volsecs - data)/clustersize;
518 if(newclusters == clusters)
520 clusters = newclusters;
522 fatal("can't decide how many clusters to use (%d? %d?)", clusters, newclusters);
523 if(chatty) print("clusters %d\n", clusters);
526 if(chatty) print("try %d fatbits => %d clusters of %d\n", fatbits, clusters, clustersize);
529 if(clusters >= 4087){
535 if(clusters >= 65527)
536 fatal("disk too big; implement fat32");
539 PUTSHORT(b->sectsize, secsize);
540 b->clustsize = clustersize;
541 PUTSHORT(b->nresrv, nresrv);
543 PUTSHORT(b->rootsize, rootfiles);
544 if(volsecs < (1<<16))
545 PUTSHORT(b->volsize, volsecs);
546 b->mediadesc = t->media;
547 PUTSHORT(b->fatsize, fatsecs);
548 PUTSHORT(b->trksize, t->sectors);
549 PUTSHORT(b->nheads, t->heads);
550 PUTLONG(b->nhidden, disk->offset);
551 PUTLONG(b->bigvolsize, volsecs);
554 * Extended BIOS Parameter Block.
557 b->driveno = getdriveno(disk);
560 if(chatty) print("driveno = %ux\n", b->driveno);
563 memmove(b->label, label, sizeof(b->label));
564 sprint(r, "FAT%d ", fatbits);
565 memmove(b->type, r, sizeof(b->type));
568 buf[secsize-2] = 0x55;
569 buf[secsize-1] = 0xAA;
572 if(seek(disk->wfd, 0, 0) < 0)
573 fatal("seek to boot sector: %r\n");
574 if(write(disk->wfd, buf, secsize) != secsize)
575 fatal("writing boot sector: %r");
581 * If we were only called to write the PBS, leave now.
587 * allocate an in memory fat
589 if(seek(disk->wfd, nresrv*secsize, 0) < 0)
590 fatal("seek to fat: %r\n");
591 if(chatty) print("fat @%lluX\n", seek(disk->wfd, 0, 1));
592 fat = malloc(fatsecs*secsize);
594 fatal("out of memory");
595 memset(fat, 0, fatsecs*secsize);
602 if(seek(disk->wfd, 2*fatsecs*secsize, 1) < 0) /* 2 fats */
603 fatal("seek to root: %r");
604 if(chatty) print("root @%lluX\n", seek(disk->wfd, 0LL, 1));
607 * allocate an in memory root
609 root = malloc(rootsecs*secsize);
611 fatal("out of memory");
612 memset(root, 0, rootsecs*secsize);
613 if(seek(disk->wfd, rootsecs*secsize, 1) < 0) /* rootsecs */
614 fatal("seek to files: %r");
615 if(chatty) print("files @%lluX\n", seek(disk->wfd, 0LL, 1));
618 * Now positioned at the Files Area.
619 * If we have any arguments, process
620 * them and write out.
622 for(p = root; argc > 0; argc--, argv++, p += sizeof(Dosdir)){
623 if(p >= (root+(rootsecs*secsize)))
624 fatal("too many files in root");
626 * Open the file and get its length.
628 if((sysfd = open(*argv, OREAD)) < 0)
629 fatal("open %s: %r", *argv);
630 if((d = dirfstat(sysfd)) == nil)
631 fatal("stat %s: %r", *argv);
632 if(d->length > 0xFFFFFFFFU)
633 fatal("file %s too big\n", *argv, d->length);
635 print("Adding file %s, length %lld\n", *argv, d->length);
640 * Allocate a buffer to read the entire file into.
641 * This must be rounded up to a cluster boundary.
643 * Read the file and write it out to the Files Area.
645 length += secsize*clustersize - 1;
646 length /= secsize*clustersize;
647 length *= secsize*clustersize;
648 if((buf = malloc(length)) == 0)
649 fatal("out of memory");
651 if(readn(sysfd, buf, d->length) != d->length)
652 fatal("read %s: %r", *argv);
653 memset(buf+d->length, 0, length-d->length);
654 if(chatty) print("%s @%lluX\n", d->name, seek(disk->wfd, 0LL, 1));
655 if(commit && writen(disk->wfd, buf, length) != length)
656 fatal("write %s: %r", *argv);
662 * Allocate the FAT clusters.
663 * We're assuming here that where we
664 * wrote the file is in sync with
665 * the cluster allocation.
666 * Save the starting cluster.
668 length /= secsize*clustersize;
670 for(n = 0; n < length-1; n++)
678 * Add the filename to the root.
680 fprint(2, "add %s at clust %lux\n", d->name, x);
681 addrname(p, d, *argv, x);
686 * write the fats and root
689 if(seek(disk->wfd, nresrv*secsize, 0) < 0)
690 fatal("seek to fat #1: %r");
691 if(write(disk->wfd, fat, fatsecs*secsize) < 0)
692 fatal("writing fat #1: %r");
693 if(write(disk->wfd, fat, fatsecs*secsize) < 0)
694 fatal("writing fat #2: %r");
695 if(write(disk->wfd, root, rootsecs*secsize) < 0)
696 fatal("writing root: %r");
712 x = (flag == Eof) ? 0xffff : (fatlast+1);
717 fat[o] = (fat[o]&0x0f) | (x<<4);
721 fat[o+1] = (fat[o+1]&0xf0) | ((x>>8) & 0x0F);
734 if(fatlast >= clusters)
735 sysfatal("data does not fit on disk (%d %d)", fatlast, clusters);
741 putname(char *p, Dosdir *d)
745 memset(d->name, ' ', sizeof d->name+sizeof d->ext);
746 for(i = 0; i< sizeof(d->name); i++){
747 if(*p == 0 || *p == '.')
749 d->name[i] = toupper(*p++);
753 for(i = 0; i < sizeof d->ext; i++){
756 d->ext[i] = toupper(*p);
764 Tm *t = localtime(time(0));
767 x = (t->hour<<11) | (t->min<<5) | (t->sec>>1);
770 x = ((t->year-80)<<9) | ((t->mon+1)<<5) | t->mday;
776 addrname(uchar *entry, Dir *dir, char *name, ulong start)
781 s = strrchr(name, '/');
789 if(strcmp(s, "9load") == 0)
795 d->start[1] = start>>8;
796 d->length[0] = dir->length;
797 d->length[1] = dir->length>>8;
798 d->length[2] = dir->length>>16;
799 d->length[3] = dir->length>>24;