1 /* $NetBSD: boot.c,v 1.3 1996/09/27 23:22:51 christos 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 #ifndef lint 37 static char rcsid[] = "$NetBSD: boot.c,v 1.3 1996/09/27 23:22:51 christos Exp $"; 38 #endif /* not lint */ 39 40 #include <stdlib.h> 41 #include <string.h> 42 #include <ctype.h> 43 #include <stdio.h> 44 #include <unistd.h> 45 46 #include "ext.h" 47 #include "fsutil.h" 48 49 int 50 readboot(dosfs, boot) 51 int dosfs; 52 struct bootblock *boot; 53 { 54 u_char block[DOSBOOTBLOCKSIZE]; 55 int n; 56 57 if ((n = read(dosfs, block, sizeof block)) < (int)sizeof block) { 58 if (n < 0) 59 perror("could not read boot block"); 60 else 61 pfatal("Short bootblock?"); 62 return FSFATAL; 63 } 64 65 /* decode bios parameter block */ 66 boot->BytesPerSec = block[11] + (block[12] << 8); 67 boot->SecPerClust = block[13]; 68 boot->ResSectors = block[14] + (block[15] << 8); 69 boot->FATs = block[16]; 70 boot->RootDirEnts = block[17] + (block[18] << 8); 71 boot->Sectors = block[19] + (block[20] << 8); 72 boot->Media = block[21]; 73 boot->FATsecs = block[22] + (block[23] << 8); 74 boot->SecPerTrack = block[24] + (block[25] << 8); 75 boot->Heads = block[26] + (block[27] << 8); 76 boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); 77 boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); 78 boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1) 79 / boot->BytesPerSec 80 + boot->ResSectors 81 + boot->FATs * boot->FATsecs 82 - CLUST_FIRST * boot->SecPerClust; 83 84 if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) { 85 pfatal("Invalid sector size: %u\n", boot->BytesPerSec); 86 return FSFATAL; 87 } 88 if (boot->SecPerClust == 0) { 89 pfatal("Invalid cluster size: %u\n", boot->SecPerClust); 90 return FSFATAL; 91 } 92 if (boot->Sectors) { 93 boot->HugeSectors = 0; 94 boot->NumSectors = boot->Sectors; 95 } else 96 boot->NumSectors = boot->HugeSectors; 97 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust; 98 if (boot->NumClusters >= MAX12BITCLUSTERS) 99 boot->Is16BitFat = 1; 100 else 101 boot->Is16BitFat = 0; 102 103 if (boot->Is16BitFat) 104 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2; 105 else 106 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3; 107 if (boot->NumFatEntries < boot->NumClusters) { 108 pfatal("FAT size too small, %d entries won't fit into %u sectors\n", 109 boot->NumClusters, boot->FATsecs); 110 return FSFATAL; 111 } 112 boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust; 113 114 boot->NumFiles = 1; 115 boot->NumFree = 0; 116 117 return FSOK; 118 } 119