From haakh%socrates@socrates.st.bauing.th-darmstadt.de  Tue Oct 31 09:33:14 1995
Received: from rs2.hrz.th-darmstadt.de (rs2.hrz.th-darmstadt.de [130.83.22.63])
          by freefall.freebsd.org (8.6.12/8.6.6) with SMTP id JAA08594
          for <FreeBSD-gnats-submit@freebsd.org>; Tue, 31 Oct 1995 09:25:54 -0800
Received: from socrates.st.bauing.th-darmst (socrates.st.bauing.th-darmstadt.de) by rs2.hrz.th-darmstadt.de with SMTP id AA13412
  (5.65c/IDA-1.4.4 for <FreeBSD-gnats-submit@freebsd.org>); Tue, 31 Oct 1995 18:25:18 +0100
Received: by socrates.st.bauing.th-darmstadt.de (5.57/Ultrix3.0-C)
	id AA21194; Tue, 31 Oct 95 18:25:01 +0100
Message-Id: <9510311725.AA21194@socrates.st.bauing.th-darmstadt.de>
Date: Tue, 31 Oct 95 18:25:01 +0100
From: Andreas Haakh <haakh@socrates.st.bauing.th-darmstadt.de>
Reply-To: ah@alvman.robin.de
To: FreeBSD-gnats-submit@freebsd.org, ah@alvman.robin.de,
        haakh%socrates@socrates.st.bauing.th-darmstadt.de
Subject: fdformat - patch to add DOS-filsystem
X-Send-Pr-Version: 3.2

>Number:         804
>Category:       misc
>Synopsis:       fdformat did not create DOS-filesystem
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Oct 31 09:40:01 PST 1995
>Closed-Date:    Sun Nov 5 17:02:14 MET 1995
>Last-Modified:  Sun Nov  5 17:04:41 MET 1995
>Originator:     Andreas Haakh
>Release:        FreeBSD 2.0.5-RELEASE i386
>Organization:
not at all... 
>Environment:

	irrelevant

>Description:

	fdformat just did a raw format on floppies. You could not
	mount_msdos them.

>How-To-Repeat:

	obvious

>Fix:
	
	I added an option (-D) to write a dos-filesystem to the floppies.
	The changes are based on msdosfs and mformat (mtools).
	Files included from /sys/msdosfs/ should be moved to /usr/include/...
	The conext-diffs are included and change the following files
	relative to /usr/src/ :
	sys/msdosfs/bootsect.h		(a bug - has probably no 
					 influence on msdosfs)
	usr.sbin/fdformat/fdformat.c
	usr.sbin/fdformat/fdformat.1


//////////////////////START OF PATCH////////////////////////////////////////
*** sys/msdosfs/bootsect.h.orig	Fri Oct 27 12:56:37 1995
--- sys/msdosfs/bootsect.h	Fri Oct 27 12:56:19 1995
***************
*** 28,32 ****
  	char bsBPB[19];		/* BIOS parameter block */
  	char bsDriveNumber;	/* drive number (0x80) */
! 	char bsBootCode[474];	/* pad so structure is 512 bytes long */
  	u_short bsBootSectSig;
  #define	BOOTSIG	0xaa55
--- 28,32 ----
  	char bsBPB[19];		/* BIOS parameter block */
  	char bsDriveNumber;	/* drive number (0x80) */
! 	char bsBootCode[479];	/* pad so structure is 512 bytes long */
  	u_short bsBootSectSig;
  #define	BOOTSIG	0xaa55
*** usr.sbin/fdformat/fdformat.c.orig	Tue May 30 05:47:39 1995
--- usr.sbin/fdformat/fdformat.c	Fri Oct 27 18:10:59 1995
***************
*** 37,40 ****
--- 37,44 ----
   * Andrew A. Chernov, ache@astral.msk.su
   * Thu Jan 27 00:47:24 MSK 1994
+  *
+  * Added creation of DOS filesystem
+  * Andreas Haakh, ah@alman.RoBIN.de
+  * Fri Oct 27 18:10:59 1995
   */
  
***************
*** 48,51 ****
--- 52,57 ----
  #include <errno.h>
  #include <machine/ioctl_fd.h>
+ #include "/sys/msdosfs/bootsect.h"	/* for DOS filesystem */
+ #include "/sys/msdosfs/bpb.h"
  
  static void
***************
*** 139,146 ****
  }
  
  static void
  usage (void)
  {
! 	printf("Usage:\n\tfdformat [-q] [-n | -v] [-f #] [-c #] [-s #] [-h #]\n");
  	printf("\t\t [-r #] [-g #] [-i #] [-S #] [-F #] [-t #] devname\n");
  	printf("Options:\n");
--- 145,355 ----
  }
  
+ static int
+ write_FAT(int fd, struct fd_type *fdt) {
+ 
+ 	int i, Media, SecPerClust, RootDirBlks, FATsecs;
+ 	struct bootsector33 bs33;
+ 	struct byte_bpb33  *b33;
+ 
+ 
+ 	b33 = (struct byte_bpb33 *) bs33.bsBPB;
+ 
+ 	if (sizeof(bs33) != 512) {
+ 		fprintf(stderr,"Check definition of struct bootsector33!\n");
+ 		fprintf(stderr,"Sizof (struct bootsector33) != 512.\n");
+ 		exit (3);
+ 	}
+ 
+ 
+ 	/* The following code is heavily borrowed from msdosfs
+ 	 * and mformat from mtools.
+ 	 *
+ 	 * I have no documentation about the FAT. So I just rely
+ 	 * on those two sources :-))
+ 	 *
+ 	 * This function returns the number of written boot, FAT
+ 	 * and root-dir blocks.
+ 	 */
+ 
+ 	switch(fdt->size/2) {		/* size in KBytes */
+ 		int FatGuess, NumClus;
+ 	case 160:
+ 		Media = 0xfe;
+ 		SecPerClust = 1;
+ 		RootDirBlks = 4;
+ 		FATsecs = 1;
+ 		break;
+ 	cas 180:
+ 		Media = 0xfc;
+ 		SecPerClust = 1;
+ 		RootDirBlks = 4;
+ 		FATsecs = 2;
+ 		break;
+	case 320:
+ 		Media = 0xff;
+ 		SecPerClust = 2;
+ 		RootDirBlks = 7;
+ 		FATsecs = 1;+ 		break;
+ 	case 360:
+ 		Media = 0xfd;
+ 		SecPerClust = 2;
+ 		RootDirBlks = 7;
+ 		FATsec = 2;
+ 		break;
+ 	case 720:
+ 		Media = 0xf9;
+ 		SecPerClust = 2;
+ 		RootDirBlks = 7;
+ 		FTsecs = 3;
+ 		break;
+ 	case 1200:
+ 		Media = 0xf9;
+ 		SecPerClust = 1;
+ 		RootDirBlks = 14
+ 		FATsecs = 7;
+ 		break;
+ 	case 1440:
+ 		Media = 0xf0;
+ 		SecPerClust = 1;
+ 		RootDirBls = 14;
+ 		FATsecs = 9;
+ 		break;
+ 	case 800:
+ 	case 820:
+ 	case 1480:
+ 	default:	/* non standard format */
+ 		Media = 0xf0;
+ 		if (fdt->heads == 1) {
+ 			SecPerClust = 1;
+			RootDirBlks = 4;
+ 		}else {
+ 			if (fdt->size > 2000) {
+ 				SecPerClust = 1;
+ 				RootDrBlks = 14;
+ 			} else {
+ 				SecPerClust = 2;
+ 				RootDirBlks = 7;
+ 			}
+ 		}
+ 		/* fatlength. 341 is the number of 12 bit fat entries
+ 		 * per sector */
+ 		FatGuess = ((fdt->size/ SecPerClust) / 341.0) + 0.95;
+ 		NumClus = (fdt->size - RootDirBlks - 2 * FatGuess - 1)
+ 		/ SecPerClust;
+ 		FATsecs = (NumClus / 341.) + 1;
+ 		break;
+ 	}
+ 
+ 	/* Set up the boot secor */
+ 
+ 	bs33.bsJump[0] = 0xeb;
+ 	bs33.bsJump[1] = 0x44;
+ 	bs33.bsJump[2] = 0x90;
+ 	strncy(bs33.bsOemName,"4.4BSD  ",8);
+ 
+ 	putushort(b33->bpbBytesPerSec, 512);
+ 	b33->bpbSecPerClut = SecPerClust;
+ 	putushort(b33->bpbResSectors,1);
+ 	b33->bpbFATs = 2;
+ 	putushort(b33->bpbootDirEnts, RootDirBlks * 16);
+ 	putushort(b33->bpbSectors, fdt->size);
+ 	b33->bpbMedia = Meda;
+ 	putushort(b33->bpbFATsecs, FATsecs);
+ 	putushort(b33->bpbSecPerTrack, fdt->sectrc);
+ 	putushort(b33->bpbHeads, fdt->heads);
+ 	putushort(b33->bpbHiddenSecs, 0);
+ 
+ 	bs33.bsriveNumber = 0x00;
+ 	/* clear the bootcode */
+ 	memset(bs33.bsBootCode, 0, sizeof bs33.bsBootode);
+ 	bs33.bsBootSectSig = BOOTSIG;
+ 
+ 
+ #ifdef TEST_DOS_FAT
+ 	printf("jmp: %02x%02x%02xn",bs33.bsJump[0], 
+ 		bs33.bsJump[1], bs33.bsJump[2]);
+ 	printf("OEM: %s\n",bs33.bsOemName);+ 	printf("Drv: %04x\n", bs33.bsDriveNumber);
+ 	printf("Sig: %04x\n", bs33.bsBootSectSig);
+ 
 	printf("Bytes/sector     = %4d\n", getushort(b33->bpbBytesPerSec));
+ 	printf("Sectors/cluser  = %4d\n", b33->bpbSecPerClust);
+ 	printf("Reserved sectors = %4d\n", getushort(b33->bpbResectors));
+ 	printf("Sectors/track    = %4d\n", getushort(b33->bpbSecPerTrack));
+ 	printf("Numer of FATs   = %4d\n", b33->bpbFATs);
+ 	printf("Root dir entries = %4d\n", getushort(b33->bpbRotDirEnts));
+ 	printf("Total # of secs  = %4d\n", getushort(b33->bpbSectors));
+ 	printf("Medi descriptor = %4d %c\n", b33->bpbMedia, b33->bpbMedia);
+ 	printf("Sectors/FAT      = %4d\n", gtushort(b33->bpbFATsecs));
+ 	printf("Number of heads  = %4d\n", getushort(b33->bpbHeads));
+ 	rintf("Hidden sectors   = %4d\n", getushort(b33->bpbHiddenSecs));
+ #endif
+ 
+ 	/* write bootsctor to disk */
+ 	lseek(fd, (off_t) 0, SEEK_SET);
+ 	if (512 != write(fd, &bs33, 512) ) {
+ 		error("Error writing DOS-block0.\n");
+ 		exit (3);
+ 	}
+ 
+ 
+ 	/*
+ 	 * now we use bs33 for AT and root directory
+ 	 */
+ 	memset(&bs33, 0, (size_t) 512);
+ 
+ 	/* first FAT first block /
+ 	bs33.bsJump[0] = Media;
+ 	bs33.bsJump[1] = 0xff;
+ 	bs33.bsJump[2] = 0xff;
+ 	if (512 != rite(fd, &bs33, 512) ) {
+ 		perror("Error writing FAT.\n");
+ 		exit (3);
+ 	}
+ 
+ 	/* first AT remaining blocks */
+ 	bs33.bsJump[0] = 0x00;
+ 	bs33.bsJump[1] = 0x00;
+ 	bs33.bsJup[2] = 0x00;
+ 	for (i = 1; i < FATsecs; i++)
+ 		if (512 != write(fd, &bs33, 512) ) {
+ 			perror(Error writing FAT.\n");
+ 			exit (3);
+ 		}
+ 
+ 	/* second FAT first block */
+ 	bs33.bsJump[] = Media;
+ 	bs33.bsJump[1] = 0xff;
+ 	bs33.bsJump[2] = 0xff;
+ 	if (512 != write(fd, &bs33, 52) ) {
+ 		perror("Error writing FAT.\n");
+ 		exit (3);
+ 	}
+ 
+ 	/* second FAT remaining bloks */
+ 	bs33.bsJump[0] = 0x00;
+ 	bs33.bsJump[1] = 0x00;
+ 	bs33.bsJump[2] = 0x00;
+ 	for (i =1; i < FATsecs; i++)
+ 		if (512 != write(fd, &bs33, 512) ) {
+ 			perror("Error writin FAT.\n");
+ 			exit (3);
+ 		}
+ 
+ 	/* the root directory */
+ 	for (i = 0; i < RootDirBlks; ++)
+ 		if (512 != write(fd, &bs33, 512) ) {
+ 			perror("Error writing FAT.\n");
+ 			exit (3);
+ 	}
+ 
+ 	return (1 + 2 * FATsecs + RootDirBlks);
+ }
+ 
  static void
  usage (void)
  {
! 	pritf("Usage:\n\tfdformat [-q] [-n | -v] [-D] [-f #] [-c #] [-s #] [-h #]\n");
  	printf("\t\t [-r#] [-g #] [-i #] [-S #] [-F #] [-t #] devname\n");
  	printf("Options:\n");
**************
*** 161,164 ****
--- 370,374 ----
  	printf("\t-F #\tspecify fill byte\n");
  	printf("\t-t#\tnumber of steps per track\n");
+ 	printf("\t-D #\twrite a DOS filsystem on floppy\n");
  	ext(2);
  }
***************
*** 189,198 ****
  	int format = -1, cyls = -1, secs = -1, heads = -1 intleave = -1;
  	int rate = -1, gaplen = -1, secsize = -1, steps = -1;
! 	int fill = 0xf6, qiet = 0, verify = 1, verify_only = 0;
  	int fd, c, track, error, tracks_per_dot, bytes_per_trak, errs;
  	const char *devname, *suffix;
  	struct fd_type fdt;
  
! 	while((c = getopt(argc, rgv, "f:c:s:h:r:g:S:F:t:i:qvn")) != -1)
  		switch(c) {
  		case 'f':	/* format in kilobytes */--- 399,408 ----
  	int format = -1, cyls = -1, secs = -1, heads = -1, intleave = -1;
  	int rae = -1, gaplen = -1, secsize = -1, steps = -1;
! 	int fill = 0xf6, quiet = 0, verify = 1, verif_only = 0, dosfat = 0;
  	int fd, c, track, error, tracks_per_dot, bytes_per_track, errs;
  	cost char *devname, *suffix;
  	struct fd_type fdt;
  
! 	while((c = getopt(argc, argv, "f:c:sh:r:g:S:F:t:i:qvnD")) != -1)
  		switch(c) {
  		case 'f':	/* format in kilobytes */
**************
*** 243,246 ****
--- 453,459 ----
  			verify = 0;
  			break;
+ 		case 'D':
+ 			dosfat =1;
+ 			break;
  
  		case 'v':
***************
*** 309,312 ****
--- 522,526 ----
  
  	if (verfy_only) {
+ 		dosfat = 0;
  		if(!quiet)
  			printf("Verify %dK floppy `%s'.\n",
**************
*** 361,366 ****
  		}
  	}
! 	if(!quiet)
  		printf(" done.\n");
  
  	return errs;
--- 575585 ----
  		}
  	}
! 	if(!quiet) {
  		printf(" done.\n");
+ 		if (dosfat)
+ 			printf("Creatig DOS filesystem.\n");
+ 	}
+ 	if (dosfat)
+ 		write_FAT(fd, &fdt);
  
  	return errs;
*** ur.sbin/fdformat/fdformat.1.orig	Fri Oct 28 18:07:32 1994
--- usr.sbin/fdformat/fdformat.1	Fri Ot 27 16:17:19 1995
***************
*** 34,37 ****
--- 34,38 ----
  .Bq Fl v
  .Bq Fl n
+ .Bq FlD
  .Bq Fl f Ar capacity
  .Bq Fl c Ar cyls
***************
*** 81,84 ****
--- 82,87 ----
  .ItFl v
  Don't format, verify only.
+ .It Fl D
+ write a DOS filesystem on floppy after frmatting.
  .It Fl c Ar cyls
  .It Fl s Ar secs
***************
*** 116,120 ****
  1 is returne on any errors during floppy formatting, and an exit status
  of 2 reflects invalid arguments gven to the program (along with an
! appropriate information written to diagnostic output).
  .Sh SE ALSO
  .Xr fdc 4 .
--- 119,124 ----
  1 is returned on any errors during floppy formatting, ad an exit status
  of 2 reflects invalid arguments given to the program (along with an
! appropiate information written to diagnostic output). An exit status
! of 3 indicates an error writin the DOS filsystem.
  .Sh SEE ALSO
  .Xr fdc 4 .
***************
*** 131,133 ****
  .ifn Joerg Wunsch,
  .if t J\(:org Wunsch,
! Dresden, with changes by Serge Vakulenko and Andrew A Chernov, Moscow.
--- 135,138 ----
  .if n Joerg Wunsch,
  .if t J\(:org Wunsch,
! Dresden, wit changes by Serge Vakulenko,  Andrew A. Chernov, Moscow and
! Andreas Haakh, Darmstadt.
/////////////////////END OF PATCH////////////////////////////////////////

--
  Andreas Haakh                \           ah@alvman.RoBIN.de
  Kirschberg 12 b                 \           +49 6155 6615
  64347 Griesheim                   \_________________________________________




>Release-Note:
>Audit-Trail:

From: Luigi Rizzo <luigi@labinfo.iet.unipi.it>
To: ah@alvman.robin.de
Cc: FreeBSD-gnats-submit@freebsd.org,
        haakh%socrates@socrates.st.bauing.th-darmstadt.de
Subject: Re: misc/804: fdformat - patch to add DOS-filsystem
Date: Tue, 31 Oct 1995 19:01:28 +0100 (MET)

 > 	I added an option (-D) to write a dos-filesystem to the floppies.
 > 	The changes are based on msdosfs and mformat (mtools).
 
 hmmm...
 
 I don't like very much the idea of duplicating code (and possibly
 have to mantain multiple copies). Why don't you just make fdformat
 invoke mformat with the appropriate (possibly configurable) arguments
 ? This way, once mtools 2.XX (or hfstools or whatever) are upgraded
 to write a W95 (or HFS) filesystem, you get the new capabilities
 for free.
 
 	Luigi
 ====================================================================
 Luigi Rizzo                     Dip. di Ingegneria dell'Informazione
 email: luigi@iet.unipi.it       Universita' di Pisa
 tel: +39-50-568533              via Diotisalvi 2, 56126 PISA (Italy)
 fax: +39-50-568522              http://www.iet.unipi.it/~luigi/
 ====================================================================

From: Andreas Haakh <ah@alvman.RoBIN.de>
To: ah@alvman.RoBIN.de, luigi@labinfo.iet.unipi.it
Cc: FreeBSD-gnats-submit@freebsd.org,
        haakh%socrates@socrates.st.bauing.th-darmstadt.de
Subject: Re: misc/804: fdformat - patch to add DOS-filsystem
Date: Wed, 1 Nov 1995 23:02:00 +0100

 Hi Luigi,
 
 } > 	I added an option (-D) to write a dos-filesystem to the floppies.
 } > 	The changes are based on msdosfs and mformat (mtools).
 } 
 } hmmm...
 } 
 } I don't like very much the idea of duplicating code (and possibly
 } have to mantain multiple copies). Why don't you just make fdformat
 } invoke mformat with the appropriate (possibly configurable) arguments
 } ? This way, once mtools 2.XX (or hfstools or whatever) are upgraded
 } to write a W95 (or HFS) filesystem, you get the new capabilities
 } for free.
 } 
 } 	Luigi
 
 Well, the situation was the following: I needed an MSDOS floppy and
 I had both of my computers running FreeBSD (one is FreeBSD-server, the
 other usually runs OS/2 and from time to time FreeBSD if needed). I 
 usually don't need the mtools and I don't have them installed.
 
 My idea was the following: MDSOS for floppies _is_ standard if
 you want to exchange data between different oses. So, having no docs
 on FAT available I started examining msdosfs and added something to
 fdformat which worked for me (a kludge ;-). Then I thought about the
 questions which arise in thee newsgroups from time to time about
 fdformat not beeing able to create MSDOS-floppies and decided to
 invest some more time in order to change the kludge to something
 real ;-))  I remembered the mtools and looked into mformat in oder
 to retrieve the parameters used for the biosparameterblock. That was
 all. If You don't like it this way you can add and #ifdef MSDOSFS
 around the additions and let the user decide weather he wants to
 activate MSDOSFS or you can hide it and make it the default to
 add the FAT-filesystem. It doesn't hurt if you use newfs afterwards.
 
 And you don't have to maintain several copies of the code. I don't
 think FAT will ever change so the addition will be static. Even OS/2
 uses FAT for floppies so there is no need to supply HPFS. And if
 some day somebody actually should write a filesystem for W95, well
 then this somebody will probably modify fdformat because there is
 already support for FAT. 
 
 Mformat is part of mtools and whoever needs them will install them.
 Others will use mount_msdos if necessary and appreciate the addition
 to fdformat.
 I persomally dislike mformat because it only supplies the other half
 of a 'real MSDOS-format' the creation of the filesystem.
 
 Andreas
 --
   Andreas Haakh                 \           ah@alvman.RoBIN.de
   Kirschberg 12 b                 \           +49 6155 62615
   64347 Griesheim                   \_________________________________________
State-Changed-From-To: open->closed 
State-Changed-By: joerg 
State-Changed-When: Sun Nov 5 17:02:14 MET 1995 
State-Changed-Why:  
Instead of polluting fdformat(1) with high-level file system creation, 
this task is now up to the new utility mkdosfs(1) (or to newfs(8) for 
creation of an ufs file system). 
>Unformatted:
