/* * MSINIT.C Program to initialize a disk in MS-DOS format * * Copyright (c) 1991 Shal Farley * Cheshire Engineering Corporation * 650 Sierra Madre Villa Avenue, Suite 201 * Pasadena, California 91107 * (818) 351-5493 * (818) 351-8645 FAX * shal@alumni.caltech.edu * * This software may be used and distributed for any purpose without license or * royalty payments so long as the above copyright notice is preserved. If you * have any comments, bug fixes, improvements, or new programs based upon this * software I'd like to hear from you. */ #include #ifdef decus # include "msasse.h" #else # include # include # define zero(buf,size) memset(buf,0,size) #endif #include "msport.h" #include "msdefs.h" #include "msfio.h" #include "msstat.h" #include "msdir.h" #include "mserrn.h" main(argc,argv) int argc; char *argv[]; { UCHAR buf[512]; /* A copy of the boot block */ MSDEV *pDEV; /* The MS-DOS device */ FILE *pf; /* The corresponding RT-11 file */ USHORT *pSig; /* MS-DOS boot sector signature = 0xAA55 */ MSBS *pBS; /* MS-DOS boot sector */ int size; /* Size of FAT or Dir to allocate */ int temp; /* * Open the device and see what's in it */ if ( argc != 2) { fprintf(stderr, "Usage: MSINIT device-or-file-name"); exit(1); } if ( (pDEV = msdopen(argv[1])) == NULL) { msperror("?MSINIT-F"); exit(1); } if (pDEV->fValid) { fprintf(stderr, "?MSINIT-I-Device has MS-DOS directory\n"); } if (pDEV->fRT11) { fprintf(stderr, "?MSINIT-I-Device has RT-11 directory\n"); } /* * Give the user a chance to back out */ fprintf(stderr, "Initialize %s, Are you sure? ", argv[1]); fflush(stderr); if (toupper(getc(stdin)) != 'Y') exit(0); /* * Fill in the MSDEV structure */ pDEV->fValid = TRUE; pDEV->nsTotal = 2400L; /* Values for 1.2 MB 5.25" diskette */ pDEV->nsBoot = 1; pDEV->nFATs = 2; pDEV->nsFAT = 7; pDEV->nDirEntries = 224; pDEV->nsCluster = 1; /* * Fill in the Boot block */ zero(buf,SECTOR); pBS = &buf[MSVH_BS]; strncpy(pBS->OemName, "MSFIO ",8); /* Oem Name */ pBS->BytesPerSec[0] = LSB(512); pBS->BytesPerSec[1] = MSB(512); pBS->SecPerClust = pDEV->nsCluster; pBS->ResSectors[0] = pDEV->nsBoot; pBS->FATs = pDEV->nFATs; pBS->RootDirEnts[0] = LSB(pDEV->nDirEntries); pBS->RootDirEnts[1] = MSB(pDEV->nDirEntries); pBS->Sectors[0] = LSB(pDEV->nsTotal); pBS->Sectors[1] = MSB(pDEV->nsTotal); pBS->Media = 0xF9; /* for 1.2 MB diskettes */ pBS->FATsecs[0] = pDEV->nsFAT; pBS->SecPerTrack[0] = 15; /* for 1.2 MB diskettes */ pBS->Heads[0] = 2; /* for 1.2 MB diskettes */ pBS->BootSignature = 0x29; /* Extended Boot Block */ strncpy(pBS->VolumeLabel, " ", 11); strncpy(pBS->FileSysType, "FAT12 ", 8); pSig = &buf[MSVH_SYSID]; /* Mark it as MS-DOS Structured */ *pSig = 0xAA55; /* * Get the FILE pointer for the device we're initializing */ pf = pDEV->pFILE; #ifdef decus /* * LIE, CHEAT & STEAL. * * The DECUS-C library does not support update operations. So now we reach * into the guts of the library and do what a simple call to fseek() should * have done. */ pf->_flag &= ~_IOREAD; /* fwrite() doesn't like "r" files... */ pf->_flag |= _IORW; /* (nobody checks this) */ assert(pf->_base != NULL); /* We'll have no accidents, please */ pf->_cnt = pf->io_rbsz; /* The buffer is empty (whole size avail) */ pf->_ptr = pf->_base; /* Point to the base */ pf->io_bnbr = 0; /* WHAM -- the block number we want */ #else if (fseek(pf,0L,SEEK_SET) != 0) die("Seek to zero failed"); #endif /* decus */ if ( (temp=fwrite(&buf[0],1,SECTOR,pf)) != SECTOR) { fprintf(stderr, "fwrite returned %d\n", temp); die("Boot"); } /* This is needed since (in decus CLIB) subsequent seeks won't bother */ if (fflush(pf) != 0) die("Flush boot failed"); /* * FAT -- We set this up and let msdclose() write it out. */ size = pBS->FATsecs[0]*SECTOR; if (pDEV->pFAT == NULL) { pDEV->pFAT = malloc(size); } pDEV->fFAT = TRUE; /* Mark it as "dirty" */ zero(pDEV->pFAT,size); /* Clear it */ pDEV->pFAT[0] = pBS->Media; /* The first two entries are marked */ pDEV->pFAT[1] = 0xFF; /* " */ pDEV->pFAT[2] = 0xFF; /* " */ /* * Dir -- We set this up and let msdclose() write it out. */ size = ((pBS->RootDirEnts[0] & 0xFF) + (pBS->RootDirEnts[1] << 8)) * sizeof(MSDE); if (pDEV->pDir == NULL) { pDEV->pDir = malloc(size); } pDEV->fDir = TRUE; /* Mark it as "dirty" */ zero(pDEV->pDir,size); /* Clear it */ msdclose(pDEV); if ( (pDEV = msdopen(argv[1])) == NULL) { msperror("?MSINIT-F"); exit(1); } exit(0); } die(pch) char *pch; { if (pch != NULL) fprintf(stderr, "?MSINIT-I-%s\n", pch); msperror("?MSINIT-F"); exit(1); }