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