1 /* $NetBSD: boot.c,v 1.14 2008/06/13 20:46:09 martin Exp $ */
4 * Copyright (C) 1995, 1997 Wolfgang Solfrank
5 * Copyright (c) 1995 Martin Husemann
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: boot.c,v 1.14 2008/06/13 20:46:09 martin Exp $");
43 readboot(int dosfs
, struct bootblock
*boot
)
45 u_char block
[DOSBOOTBLOCKSIZE
];
46 u_char fsinfo
[2 * DOSBOOTBLOCKSIZE
];
47 u_char backup
[DOSBOOTBLOCKSIZE
];
51 if ((size_t)read(dosfs
, block
, sizeof block
) != sizeof block
) {
52 perr("could not read boot block");
56 if (block
[510] != 0x55 || block
[511] != 0xaa) {
57 pfatal("Invalid signature in boot block: %02x%02x", block
[511], block
[510]);
61 memset(boot
, 0, sizeof *boot
);
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
->FATsmall
= 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);
78 boot
->FATsecs
= boot
->FATsmall
;
80 if (!boot
->RootDirEnts
)
82 if (boot
->flags
& FAT32
) {
83 boot
->FATsecs
= block
[36] + (block
[37] << 8)
84 + (block
[38] << 16) + (block
[39] << 24);
86 boot
->ValidFat
= block
[40] & 0x0f;
88 /* check version number: */
89 if (block
[42] || block
[43]) {
91 pfatal("Unknown filesystem version: %x.%x",
92 block
[43], block
[42]);
95 boot
->RootCl
= block
[44] + (block
[45] << 8)
96 + (block
[46] << 16) + (block
[47] << 24);
97 boot
->FSInfo
= block
[48] + (block
[49] << 8);
98 boot
->Backup
= block
[50] + (block
[51] << 8);
100 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
101 != boot
->FSInfo
* boot
->BytesPerSec
102 || read(dosfs
, fsinfo
, sizeof fsinfo
)
104 perr("could not read fsinfo block");
107 if (memcmp(fsinfo
, "RRaA", 4)
108 || memcmp(fsinfo
+ 0x1e4, "rrAa", 4)
111 || fsinfo
[0x1fe] != 0x55
112 || fsinfo
[0x1ff] != 0xaa
115 || fsinfo
[0x3fe] != 0x55
116 || fsinfo
[0x3ff] != 0xaa) {
117 pwarn("Invalid signature in fsinfo block");
119 memcpy(fsinfo
, "RRaA", 4);
120 memcpy(fsinfo
+ 0x1e4, "rrAa", 4);
121 fsinfo
[0x1fc] = fsinfo
[0x1fd] = 0;
122 fsinfo
[0x1fe] = 0x55;
123 fsinfo
[0x1ff] = 0xaa;
124 fsinfo
[0x3fc] = fsinfo
[0x3fd] = 0;
125 fsinfo
[0x3fe] = 0x55;
126 fsinfo
[0x3ff] = 0xaa;
127 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
128 != boot
->FSInfo
* boot
->BytesPerSec
129 || write(dosfs
, fsinfo
, sizeof fsinfo
)
131 perr("Unable to write FSInfo");
139 boot
->FSFree
= fsinfo
[0x1e8] + (fsinfo
[0x1e9] << 8)
140 + (fsinfo
[0x1ea] << 16)
141 + (fsinfo
[0x1eb] << 24);
142 boot
->FSNext
= fsinfo
[0x1ec] + (fsinfo
[0x1ed] << 8)
143 + (fsinfo
[0x1ee] << 16)
144 + (fsinfo
[0x1ef] << 24);
147 if (lseek(dosfs
, boot
->Backup
* boot
->BytesPerSec
, SEEK_SET
)
148 != boot
->Backup
* boot
->BytesPerSec
149 || read(dosfs
, backup
, sizeof backup
) != sizeof backup
) {
150 perr("could not read backup bootblock");
153 backup
[65] = block
[65]; /* XXX */
154 if (memcmp(block
+ 11, backup
+ 11, 79)) {
156 * XXX We require a reference that explains
157 * that these bytes need to match, or should
158 * drop the check. gdt@ has observed
159 * filesystems that work fine under Windows XP
160 * and NetBSD that do not match, so the
161 * requirement is suspect. For now, just
162 * print out useful information and continue.
164 pfatal("backup (block %d) mismatch with primary bootblock:\n",
166 for (i
= 11; i
< 11 + 90; i
++) {
167 if (block
[i
] != backup
[i
])
168 pfatal("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n",
169 i
, block
[i
], backup
[i
]);
172 /* Check backup FSInfo? XXX */
175 boot
->ClusterOffset
= (boot
->RootDirEnts
* 32 + boot
->BytesPerSec
- 1)
178 + boot
->FATs
* boot
->FATsecs
179 - CLUST_FIRST
* boot
->SecPerClust
;
181 if (boot
->BytesPerSec
% DOSBOOTBLOCKSIZE
!= 0) {
182 pfatal("Invalid sector size: %u", boot
->BytesPerSec
);
185 if (boot
->SecPerClust
== 0) {
186 pfatal("Invalid cluster size: %u", boot
->SecPerClust
);
190 boot
->HugeSectors
= 0;
191 boot
->NumSectors
= boot
->Sectors
;
193 boot
->NumSectors
= boot
->HugeSectors
;
194 boot
->NumClusters
= (boot
->NumSectors
- boot
->ClusterOffset
) / boot
->SecPerClust
;
196 if (boot
->flags
&FAT32
)
197 boot
->ClustMask
= CLUST32_MASK
;
198 else if (boot
->NumClusters
< (CLUST_RSRVD
&CLUST12_MASK
))
199 boot
->ClustMask
= CLUST12_MASK
;
200 else if (boot
->NumClusters
< (CLUST_RSRVD
&CLUST16_MASK
))
201 boot
->ClustMask
= CLUST16_MASK
;
203 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
208 switch (boot
->ClustMask
) {
210 boot
->NumFatEntries
= (boot
->FATsecs
* boot
->BytesPerSec
) / 4;
213 boot
->NumFatEntries
= (boot
->FATsecs
* boot
->BytesPerSec
) / 2;
216 boot
->NumFatEntries
= (boot
->FATsecs
* boot
->BytesPerSec
* 2) / 3;
220 if (boot
->NumFatEntries
< boot
->NumClusters
) {
221 pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
222 boot
->NumClusters
, boot
->FATsecs
);
225 boot
->ClusterSize
= boot
->BytesPerSec
* boot
->SecPerClust
;
234 writefsinfo(int dosfs
, struct bootblock
*boot
)
236 u_char fsinfo
[2 * DOSBOOTBLOCKSIZE
];
238 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
239 != boot
->FSInfo
* boot
->BytesPerSec
240 || read(dosfs
, fsinfo
, sizeof fsinfo
) != sizeof fsinfo
) {
241 perr("could not read fsinfo block");
244 fsinfo
[0x1e8] = (u_char
)boot
->FSFree
;
245 fsinfo
[0x1e9] = (u_char
)(boot
->FSFree
>> 8);
246 fsinfo
[0x1ea] = (u_char
)(boot
->FSFree
>> 16);
247 fsinfo
[0x1eb] = (u_char
)(boot
->FSFree
>> 24);
248 fsinfo
[0x1ec] = (u_char
)boot
->FSNext
;
249 fsinfo
[0x1ed] = (u_char
)(boot
->FSNext
>> 8);
250 fsinfo
[0x1ee] = (u_char
)(boot
->FSNext
>> 16);
251 fsinfo
[0x1ef] = (u_char
)(boot
->FSNext
>> 24);
252 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
253 != boot
->FSInfo
* boot
->BytesPerSec
254 || write(dosfs
, fsinfo
, sizeof fsinfo
)
256 perr("Unable to write FSInfo");
260 * Technically, we should return FSBOOTMOD here.
262 * However, since Win95 OSR2 (the first M$ OS that has
263 * support for FAT32) doesn't maintain the FSINFO block
264 * correctly, it has to be fixed pretty often.
266 * Therefor, we handle the FSINFO block only informally,
267 * fixing it if necessary, but otherwise ignoring the
268 * fact that it was incorrect.