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; assume it starts
446 * at 0x3E (the destination of the jump we just
447 * wrote to b->magic).
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 fprint(2, "warning: pbs too small\n");
472 memmove(buf+0x3E, pbsbuf+0x3E, npbs-0x3E);
478 * Add FAT BIOS parameter block.
482 print("Initializing FAT file system\n");
483 print("type %s, %d tracks, %d heads, %d sectors/track, %lld bytes/sec\n",
484 t->name, t->tracks, t->heads, t->sectors, secsize);
488 clustersize = t->cluster;
490 * the number of fat bits depends on how much disk is left
491 * over after you subtract out the space taken up by the fat tables.
492 * try both. what a crock.
496 volsecs = length/secsize;
498 * here's a crock inside a crock. even having fixed fatbits,
499 * the number of fat sectors depends on the number of clusters,
500 * but of course we don't know yet. maybe iterating will get us there.
501 * or maybe it will cycle.
505 fatsecs = (fatbits*clusters + 8*secsize - 1)/(8*secsize);
506 rootsecs = volsecs/200;
507 rootfiles = rootsecs * (secsize/sizeof(Dosdir));
510 rootsecs = rootfiles/(secsize/sizeof(Dosdir));
512 data = nresrv + 2*fatsecs + (rootfiles*sizeof(Dosdir) + secsize-1)/secsize;
513 newclusters = 2 + (volsecs - data)/clustersize;
514 if(newclusters == clusters)
516 clusters = newclusters;
518 fatal("can't decide how many clusters to use (%d? %d?)", clusters, newclusters);
519 if(chatty) print("clusters %d\n", clusters);
522 if(chatty) print("try %d fatbits => %d clusters of %d\n", fatbits, clusters, clustersize);
525 if(clusters >= 4087){
531 if(clusters >= 65527)
532 fatal("disk too big; implement fat32");
535 PUTSHORT(b->sectsize, secsize);
536 b->clustsize = clustersize;
537 PUTSHORT(b->nresrv, nresrv);
539 PUTSHORT(b->rootsize, rootfiles);
540 if(volsecs < (1<<16))
541 PUTSHORT(b->volsize, volsecs);
542 b->mediadesc = t->media;
543 PUTSHORT(b->fatsize, fatsecs);
544 PUTSHORT(b->trksize, t->sectors);
545 PUTSHORT(b->nheads, t->heads);
546 PUTLONG(b->nhidden, disk->offset);
547 PUTLONG(b->bigvolsize, volsecs);
550 * Extended BIOS Parameter Block.
553 b->driveno = getdriveno(disk);
556 if(chatty) print("driveno = %ux\n", b->driveno);
559 x = disk->offset + b->nfats*fatsecs + nresrv;
560 PUTLONG(b->volid, x);
561 if(chatty) print("volid = %lux %lux\n", x, GETLONG(b->volid));
562 memmove(b->label, label, sizeof(b->label));
563 sprint(r, "FAT%d ", fatbits);
564 memmove(b->type, r, sizeof(b->type));
567 buf[secsize-2] = 0x55;
568 buf[secsize-1] = 0xAA;
571 if(seek(disk->wfd, 0, 0) < 0)
572 fatal("seek to boot sector: %r\n");
573 if(write(disk->wfd, buf, secsize) != secsize)
574 fatal("writing boot sector: %r");
580 * If we were only called to write the PBS, leave now.
586 * allocate an in memory fat
588 if(seek(disk->wfd, nresrv*secsize, 0) < 0)
589 fatal("seek to fat: %r\n");
590 if(chatty) print("fat @%lluX\n", seek(disk->wfd, 0, 1));
591 fat = malloc(fatsecs*secsize);
593 fatal("out of memory");
594 memset(fat, 0, fatsecs*secsize);
601 if(seek(disk->wfd, 2*fatsecs*secsize, 1) < 0) /* 2 fats */
602 fatal("seek to root: %r");
603 if(chatty) print("root @%lluX\n", seek(disk->wfd, 0LL, 1));
606 * allocate an in memory root
608 root = malloc(rootsecs*secsize);
610 fatal("out of memory");
611 memset(root, 0, rootsecs*secsize);
612 if(seek(disk->wfd, rootsecs*secsize, 1) < 0) /* rootsecs */
613 fatal("seek to files: %r");
614 if(chatty) print("files @%lluX\n", seek(disk->wfd, 0LL, 1));
617 * Now positioned at the Files Area.
618 * If we have any arguments, process
619 * them and write out.
621 for(p = root; argc > 0; argc--, argv++, p += sizeof(Dosdir)){
622 if(p >= (root+(rootsecs*secsize)))
623 fatal("too many files in root");
625 * Open the file and get its length.
627 if((sysfd = open(*argv, OREAD)) < 0)
628 fatal("open %s: %r", *argv);
629 if((d = dirfstat(sysfd)) == nil)
630 fatal("stat %s: %r", *argv);
631 if(d->length > 0xFFFFFFFFU)
632 fatal("file %s too big\n", *argv, d->length);
634 print("Adding file %s, length %lld\n", *argv, d->length);
639 * Allocate a buffer to read the entire file into.
640 * This must be rounded up to a cluster boundary.
642 * Read the file and write it out to the Files Area.
644 length += secsize*clustersize - 1;
645 length /= secsize*clustersize;
646 length *= secsize*clustersize;
647 if((buf = malloc(length)) == 0)
648 fatal("out of memory");
650 if(readn(sysfd, buf, d->length) != d->length)
651 fatal("read %s: %r", *argv);
652 memset(buf+d->length, 0, length-d->length);
653 if(chatty) print("%s @%lluX\n", d->name, seek(disk->wfd, 0LL, 1));
654 if(commit && writen(disk->wfd, buf, length) != length)
655 fatal("write %s: %r", *argv);
661 * Allocate the FAT clusters.
662 * We're assuming here that where we
663 * wrote the file is in sync with
664 * the cluster allocation.
665 * Save the starting cluster.
667 length /= secsize*clustersize;
669 for(n = 0; n < length-1; n++)
677 * Add the filename to the root.
679 fprint(2, "add %s at clust %lux\n", d->name, x);
680 addrname(p, d, *argv, x);
685 * write the fats and root
688 if(seek(disk->wfd, nresrv*secsize, 0) < 0)
689 fatal("seek to fat #1: %r");
690 if(write(disk->wfd, fat, fatsecs*secsize) < 0)
691 fatal("writing fat #1: %r");
692 if(write(disk->wfd, fat, fatsecs*secsize) < 0)
693 fatal("writing fat #2: %r");
694 if(write(disk->wfd, root, rootsecs*secsize) < 0)
695 fatal("writing root: %r");
711 x = (flag == Eof) ? 0xffff : (fatlast+1);
716 fat[o] = (fat[o]&0x0f) | (x<<4);
720 fat[o+1] = (fat[o+1]&0xf0) | ((x>>8) & 0x0F);
733 if(fatlast >= clusters)
734 sysfatal("data does not fit on disk (%d %d)", fatlast, clusters);
740 putname(char *p, Dosdir *d)
744 memset(d->name, ' ', sizeof d->name+sizeof d->ext);
745 for(i = 0; i< sizeof(d->name); i++){
746 if(*p == 0 || *p == '.')
748 d->name[i] = toupper(*p++);
752 for(i = 0; i < sizeof d->ext; i++){
755 d->ext[i] = toupper(*p);
763 Tm *t = localtime(time(0));
766 x = (t->hour<<11) | (t->min<<5) | (t->sec>>1);
769 x = ((t->year-80)<<9) | ((t->mon+1)<<5) | t->mday;
775 addrname(uchar *entry, Dir *dir, char *name, ulong start)
780 s = strrchr(name, '/');
788 if(strcmp(s, "9load") == 0)
794 d->start[1] = start>>8;
795 d->length[0] = dir->length;
796 d->length[1] = dir->length>>8;
797 d->length[2] = dir->length>>16;
798 d->length[3] = dir->length>>24;