Home | History | Annotate | Line # | Download | only in fsck_msdos
boot.c revision 1.4
      1 /*	$NetBSD: boot.c,v 1.4 1997/09/14 14:40:10 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995 Wolfgang Solfrank
      5  * Copyright (c) 1995 Martin Husemann
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Martin Husemann
     18  *	and Wolfgang Solfrank.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __RCSID("$NetBSD: boot.c,v 1.4 1997/09/14 14:40:10 lukem Exp $");
     39 #endif /* not lint */
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <ctype.h>
     44 #include <stdio.h>
     45 #include <unistd.h>
     46 
     47 #include "ext.h"
     48 #include "fsutil.h"
     49 
     50 int
     51 readboot(dosfs, boot)
     52 	int dosfs;
     53 	struct bootblock *boot;
     54 {
     55 	u_char block[DOSBOOTBLOCKSIZE];
     56 	int n;
     57 
     58 	if ((n = read(dosfs, block, sizeof block)) < (int)sizeof block) {
     59 		if (n < 0)
     60 			perror("could not read boot block");
     61 		else
     62 			pfatal("Short bootblock?");
     63 		return FSFATAL;
     64 	}
     65 
     66 	/* decode bios parameter block */
     67 	boot->BytesPerSec = block[11] + (block[12] << 8);
     68 	boot->SecPerClust = block[13];
     69 	boot->ResSectors = block[14] + (block[15] << 8);
     70 	boot->FATs = block[16];
     71 	boot->RootDirEnts = block[17] + (block[18] << 8);
     72 	boot->Sectors = block[19] + (block[20] << 8);
     73 	boot->Media = block[21];
     74 	boot->FATsecs = block[22] + (block[23] << 8);
     75 	boot->SecPerTrack = block[24] + (block[25] << 8);
     76 	boot->Heads = block[26] + (block[27] << 8);
     77 	boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
     78 	boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
     79 	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
     80 	    / boot->BytesPerSec
     81 	    + boot->ResSectors
     82 	    + boot->FATs * boot->FATsecs
     83 	    - CLUST_FIRST * boot->SecPerClust;
     84 
     85 	if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
     86 		pfatal("Invalid sector size: %u\n", boot->BytesPerSec);
     87 		return FSFATAL;
     88 	}
     89 	if (boot->SecPerClust == 0) {
     90 		pfatal("Invalid cluster size: %u\n", boot->SecPerClust);
     91 		return FSFATAL;
     92 	}
     93 	if (boot->Sectors) {
     94 		boot->HugeSectors = 0;
     95 		boot->NumSectors = boot->Sectors;
     96 	} else
     97 		boot->NumSectors = boot->HugeSectors;
     98 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
     99 	if (boot->NumClusters >= MAX12BITCLUSTERS)
    100 		boot->Is16BitFat = 1;
    101 	else
    102 		boot->Is16BitFat = 0;
    103 
    104 	if (boot->Is16BitFat)
    105 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
    106 	else
    107 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
    108 	if (boot->NumFatEntries < boot->NumClusters) {
    109 		pfatal("FAT size too small, %d entries won't fit into %u sectors\n",
    110 		       boot->NumClusters, boot->FATsecs);
    111 		return FSFATAL;
    112 	}
    113 	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
    114 
    115 	boot->NumFiles = 1;
    116 	boot->NumFree = 0;
    117 
    118 	return FSOK;
    119 }
    120