fdisk - Use heads = 255 on file images
[dragonfly.git] / sbin / fsck_msdosfs / boot.c
blob2df6a21d8db43f0742e6f60eaf0715369860d75c
1 /*
2 * Copyright (C) 1995, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Martin Husemann
16 * and Wolfgang Solfrank.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $NetBSD: boot.c,v 1.5 1997/10/17 11:19:23 ws Exp $
33 * $FreeBSD: src/sbin/fsck_msdosfs/boot.c,v 1.1.2.1 2001/08/01 05:47:55 obrien Exp $
34 * $DragonFly: src/sbin/fsck_msdosfs/boot.c,v 1.3 2003/09/28 14:39:17 hmp Exp $
38 #include <sys/cdefs.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <unistd.h>
46 #include "ext.h"
47 #include "fsutil.h"
49 int
50 readboot(int dosfs, struct bootblock *boot)
52 u_char block[DOSBOOTBLOCKSIZE];
53 u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
54 u_char backup[DOSBOOTBLOCKSIZE];
55 int ret = FSOK;
57 if (read(dosfs, block, sizeof block) < sizeof block) {
58 perror("could not read boot block");
59 return FSFATAL;
62 if (block[510] != 0x55 || block[511] != 0xaa) {
63 pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]);
64 return FSFATAL;
67 memset(boot, 0, sizeof *boot);
68 boot->ValidFat = -1;
70 /* decode bios parameter block */
71 boot->BytesPerSec = block[11] + (block[12] << 8);
72 boot->SecPerClust = block[13];
73 boot->ResSectors = block[14] + (block[15] << 8);
74 boot->FATs = block[16];
75 boot->RootDirEnts = block[17] + (block[18] << 8);
76 boot->Sectors = block[19] + (block[20] << 8);
77 boot->Media = block[21];
78 boot->FATsmall = block[22] + (block[23] << 8);
79 boot->SecPerTrack = block[24] + (block[25] << 8);
80 boot->Heads = block[26] + (block[27] << 8);
81 boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
82 boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
84 boot->FATsecs = boot->FATsmall;
86 if (!boot->RootDirEnts)
87 boot->flags |= FAT32;
88 if (boot->flags & FAT32) {
89 boot->FATsecs = block[36] + (block[37] << 8)
90 + (block[38] << 16) + (block[39] << 24);
91 if (block[40] & 0x80)
92 boot->ValidFat = block[40] & 0x0f;
94 /* check version number: */
95 if (block[42] || block[43]) {
96 /* Correct? XXX */
97 pfatal("Unknown filesystem version: %x.%x",
98 block[43], block[42]);
99 return FSFATAL;
101 boot->RootCl = block[44] + (block[45] << 8)
102 + (block[46] << 16) + (block[47] << 24);
103 boot->FSInfo = block[48] + (block[49] << 8);
104 boot->Backup = block[50] + (block[51] << 8);
106 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
107 != boot->FSInfo * boot->BytesPerSec
108 || read(dosfs, fsinfo, sizeof fsinfo)
109 != sizeof fsinfo) {
110 perror("could not read fsinfo block");
111 return FSFATAL;
113 if (memcmp(fsinfo, "RRaA", 4)
114 || memcmp(fsinfo + 0x1e4, "rrAa", 4)
115 || fsinfo[0x1fc]
116 || fsinfo[0x1fd]
117 || fsinfo[0x1fe] != 0x55
118 || fsinfo[0x1ff] != 0xaa
119 || fsinfo[0x3fc]
120 || fsinfo[0x3fd]
121 || fsinfo[0x3fe] != 0x55
122 || fsinfo[0x3ff] != 0xaa) {
123 pwarn("Invalid signature in fsinfo block");
124 if (ask(0, "fix")) {
125 memcpy(fsinfo, "RRaA", 4);
126 memcpy(fsinfo + 0x1e4, "rrAa", 4);
127 fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
128 fsinfo[0x1fe] = 0x55;
129 fsinfo[0x1ff] = 0xaa;
130 fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
131 fsinfo[0x3fe] = 0x55;
132 fsinfo[0x3ff] = 0xaa;
133 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
134 != boot->FSInfo * boot->BytesPerSec
135 || write(dosfs, fsinfo, sizeof fsinfo)
136 != sizeof fsinfo) {
137 perror("Unable to write FSInfo");
138 return FSFATAL;
140 ret = FSBOOTMOD;
141 } else
142 boot->FSInfo = 0;
144 if (boot->FSInfo) {
145 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
146 + (fsinfo[0x1ea] << 16)
147 + (fsinfo[0x1eb] << 24);
148 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
149 + (fsinfo[0x1ee] << 16)
150 + (fsinfo[0x1ef] << 24);
153 if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
154 != boot->Backup * boot->BytesPerSec
155 || read(dosfs, backup, sizeof backup) != sizeof backup) {
156 perror("could not read backup bootblock");
157 return FSFATAL;
159 if (memcmp(block, backup, DOSBOOTBLOCKSIZE)) {
160 /* Correct? XXX */
161 pfatal("backup doesn't compare to primary bootblock");
162 return FSFATAL;
164 /* Check backup FSInfo? XXX */
167 boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
168 / boot->BytesPerSec
169 + boot->ResSectors
170 + boot->FATs * boot->FATsecs
171 - CLUST_FIRST * boot->SecPerClust;
173 if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
174 pfatal("Invalid sector size: %u", boot->BytesPerSec);
175 return FSFATAL;
177 if (boot->SecPerClust == 0) {
178 pfatal("Invalid cluster size: %u", boot->SecPerClust);
179 return FSFATAL;
181 if (boot->Sectors) {
182 boot->HugeSectors = 0;
183 boot->NumSectors = boot->Sectors;
184 } else
185 boot->NumSectors = boot->HugeSectors;
186 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
188 if (boot->flags&FAT32)
189 boot->ClustMask = CLUST32_MASK;
190 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
191 boot->ClustMask = CLUST12_MASK;
192 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
193 boot->ClustMask = CLUST16_MASK;
194 else {
195 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
196 boot->NumClusters);
197 return FSFATAL;
200 switch (boot->ClustMask) {
201 case CLUST32_MASK:
202 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
203 break;
204 case CLUST16_MASK:
205 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
206 break;
207 default:
208 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
209 break;
212 if (boot->NumFatEntries < boot->NumClusters) {
213 pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
214 boot->NumClusters, boot->FATsecs);
215 return FSFATAL;
217 boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
219 boot->NumFiles = 1;
220 boot->NumFree = 0;
222 return ret;
226 writefsinfo(int dosfs, struct bootblock *boot)
228 u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
230 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
231 != boot->FSInfo * boot->BytesPerSec
232 || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
233 perror("could not read fsinfo block");
234 return FSFATAL;
236 fsinfo[0x1e8] = (u_char)boot->FSFree;
237 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
238 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
239 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
240 fsinfo[0x1ec] = (u_char)boot->FSNext;
241 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
242 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
243 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
244 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
245 != boot->FSInfo * boot->BytesPerSec
246 || write(dosfs, fsinfo, sizeof fsinfo)
247 != sizeof fsinfo) {
248 perror("Unable to write FSInfo");
249 return FSFATAL;
252 * Technically, we should return FSBOOTMOD here.
254 * However, since Win95 OSR2 (the first M$ OS that has
255 * support for FAT32) doesn't maintain the FSINFO block
256 * correctly, it has to be fixed pretty often.
258 * Therefor, we handle the FSINFO block only informally,
259 * fixing it if neccessary, but otherwise ignoring the
260 * fact that it was incorrect.
262 return 0;