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