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