1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 1995, 1997 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 * 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. 26 */ 27 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __RCSID("$NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp "); 32 static const char rcsid[] = 33 "$FreeBSD$"; 34 #endif /* not lint */ 35 36 #include <stdlib.h> 37 #include <string.h> 38 #include <stdio.h> 39 #include <unistd.h> 40 41 #include "ext.h" 42 #include "fsutil.h" 43 44 int 45 readboot(int dosfs, struct bootblock *boot) 46 { 47 u_char block[DOSBOOTBLOCKSIZE]; 48 u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 49 u_char backup[DOSBOOTBLOCKSIZE]; 50 int ret = FSOK; 51 int i; 52 53 if ((size_t)read(dosfs, block, sizeof block) != sizeof block) { 54 perr("could not read boot block"); 55 return FSFATAL; 56 } 57 58 if (block[510] != 0x55 || block[511] != 0xaa) { 59 pfatal("Invalid signature in boot block: %02x%02x", 60 block[511], block[510]); 61 return FSFATAL; 62 } 63 64 memset(boot, 0, sizeof *boot); 65 boot->ValidFat = -1; 66 67 /* decode bios parameter block */ 68 boot->bpbBytesPerSec = block[11] + (block[12] << 8); 69 boot->bpbSecPerClust = block[13]; 70 boot->bpbResSectors = block[14] + (block[15] << 8); 71 boot->bpbFATs = block[16]; 72 boot->bpbRootDirEnts = block[17] + (block[18] << 8); 73 boot->bpbSectors = block[19] + (block[20] << 8); 74 boot->bpbMedia = block[21]; 75 boot->bpbFATsmall = block[22] + (block[23] << 8); 76 boot->SecPerTrack = block[24] + (block[25] << 8); 77 boot->bpbHeads = block[26] + (block[27] << 8); 78 boot->bpbHiddenSecs = block[28] + (block[29] << 8) + 79 (block[30] << 16) + (block[31] << 24); 80 boot->bpbHugeSectors = block[32] + (block[33] << 8) + 81 (block[34] << 16) + (block[35] << 24); 82 83 boot->FATsecs = boot->bpbFATsmall; 84 85 if (boot->bpbBytesPerSec % DOSBOOTBLOCKSIZE_REAL != 0 || 86 boot->bpbBytesPerSec / DOSBOOTBLOCKSIZE_REAL == 0) { 87 pfatal("Invalid sector size: %u", boot->bpbBytesPerSec); 88 return FSFATAL; 89 } 90 if (!boot->bpbRootDirEnts) 91 boot->flags |= FAT32; 92 if (boot->flags & FAT32) { 93 boot->FATsecs = block[36] + (block[37] << 8) 94 + (block[38] << 16) + (block[39] << 24); 95 if (block[40] & 0x80) 96 boot->ValidFat = block[40] & 0x0f; 97 98 /* check version number: */ 99 if (block[42] || block[43]) { 100 /* Correct? XXX */ 101 pfatal("Unknown file system version: %x.%x", 102 block[43], block[42]); 103 return FSFATAL; 104 } 105 boot->bpbRootClust = block[44] + (block[45] << 8) 106 + (block[46] << 16) + (block[47] << 24); 107 boot->bpbFSInfo = block[48] + (block[49] << 8); 108 boot->bpbBackup = block[50] + (block[51] << 8); 109 110 /* If the OEM Name field is EXFAT, it's not FAT32, so bail */ 111 if (!memcmp(&block[3], "EXFAT ", 8)) { 112 pfatal("exFAT filesystem is not supported."); 113 return FSFATAL; 114 } 115 116 /* check basic parameters */ 117 if ((boot->bpbFSInfo == 0) || (boot->bpbSecPerClust == 0)) { 118 /* 119 * Either the BIOS Parameter Block has been corrupted, 120 * or this is not a FAT32 filesystem, most likely an 121 * exFAT filesystem. 122 */ 123 pfatal("Invalid FAT32 Extended BIOS Parameter Block"); 124 return FSFATAL; 125 } 126 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, 127 SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec 128 || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) { 129 perr("could not read fsinfo block"); 130 return FSFATAL; 131 } 132 if (memcmp(fsinfo, "RRaA", 4) 133 || memcmp(fsinfo + 0x1e4, "rrAa", 4) 134 || fsinfo[0x1fc] 135 || fsinfo[0x1fd] 136 || fsinfo[0x1fe] != 0x55 137 || fsinfo[0x1ff] != 0xaa 138 || fsinfo[0x3fc] 139 || fsinfo[0x3fd] 140 || fsinfo[0x3fe] != 0x55 141 || fsinfo[0x3ff] != 0xaa) { 142 pwarn("Invalid signature in fsinfo block\n"); 143 if (ask(0, "Fix")) { 144 memcpy(fsinfo, "RRaA", 4); 145 memcpy(fsinfo + 0x1e4, "rrAa", 4); 146 fsinfo[0x1fc] = fsinfo[0x1fd] = 0; 147 fsinfo[0x1fe] = 0x55; 148 fsinfo[0x1ff] = 0xaa; 149 fsinfo[0x3fc] = fsinfo[0x3fd] = 0; 150 fsinfo[0x3fe] = 0x55; 151 fsinfo[0x3ff] = 0xaa; 152 if (lseek(dosfs, boot->bpbFSInfo * 153 boot->bpbBytesPerSec, SEEK_SET) 154 != boot->bpbFSInfo * boot->bpbBytesPerSec 155 || write(dosfs, fsinfo, sizeof fsinfo) 156 != sizeof fsinfo) { 157 perr("Unable to write bpbFSInfo"); 158 return FSFATAL; 159 } 160 ret = FSBOOTMOD; 161 } else 162 boot->bpbFSInfo = 0; 163 } 164 if (boot->bpbFSInfo) { 165 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8) 166 + (fsinfo[0x1ea] << 16) 167 + (fsinfo[0x1eb] << 24); 168 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8) 169 + (fsinfo[0x1ee] << 16) 170 + (fsinfo[0x1ef] << 24); 171 } 172 173 if (lseek(dosfs, boot->bpbBackup * boot->bpbBytesPerSec, 174 SEEK_SET) 175 != boot->bpbBackup * boot->bpbBytesPerSec 176 || read(dosfs, backup, sizeof backup) != sizeof backup) { 177 perr("could not read backup bootblock"); 178 return FSFATAL; 179 } 180 backup[65] = block[65]; /* XXX */ 181 if (memcmp(block + 11, backup + 11, 79)) { 182 /* 183 * XXX We require a reference that explains 184 * that these bytes need to match, or should 185 * drop the check. gdt@NetBSD has observed 186 * filesystems that work fine under Windows XP 187 * and NetBSD that do not match, so the 188 * requirement is suspect. For now, just 189 * print out useful information and continue. 190 */ 191 pwarn("backup (block %d) mismatch with primary bootblock:\n", 192 boot->bpbBackup); 193 for (i = 11; i < 11 + 90; i++) { 194 if (block[i] != backup[i]) 195 pwarn("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n", 196 i, block[i], backup[i]); 197 } 198 } 199 /* Check backup bpbFSInfo? XXX */ 200 } 201 202 if (boot->bpbSecPerClust == 0) { 203 pfatal("Invalid cluster size: %u", boot->bpbSecPerClust); 204 return FSFATAL; 205 } 206 if (boot->bpbSectors) { 207 boot->bpbHugeSectors = 0; 208 boot->NumSectors = boot->bpbSectors; 209 } else 210 boot->NumSectors = boot->bpbHugeSectors; 211 boot->ClusterOffset = (boot->bpbRootDirEnts * 32 + 212 boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec + 213 boot->bpbResSectors + boot->bpbFATs * boot->FATsecs - 214 CLUST_FIRST * boot->bpbSecPerClust; 215 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / 216 boot->bpbSecPerClust; 217 218 if (boot->flags&FAT32) 219 boot->ClustMask = CLUST32_MASK; 220 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) 221 boot->ClustMask = CLUST12_MASK; 222 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) 223 boot->ClustMask = CLUST16_MASK; 224 else { 225 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition", 226 boot->NumClusters); 227 return FSFATAL; 228 } 229 230 switch (boot->ClustMask) { 231 case CLUST32_MASK: 232 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4; 233 break; 234 case CLUST16_MASK: 235 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2; 236 break; 237 default: 238 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3; 239 break; 240 } 241 242 if (boot->NumFatEntries < boot->NumClusters - CLUST_FIRST) { 243 pfatal("FAT size too small, %u entries won't fit into %u sectors\n", 244 boot->NumClusters, boot->FATsecs); 245 return FSFATAL; 246 } 247 boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust; 248 249 boot->NumFiles = 1; 250 boot->NumFree = 0; 251 252 return ret; 253 } 254 255 int 256 writefsinfo(int dosfs, struct bootblock *boot) 257 { 258 u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 259 260 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET) 261 != boot->bpbFSInfo * boot->bpbBytesPerSec 262 || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) { 263 perr("could not read fsinfo block"); 264 return FSFATAL; 265 } 266 fsinfo[0x1e8] = (u_char)boot->FSFree; 267 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); 268 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); 269 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); 270 fsinfo[0x1ec] = (u_char)boot->FSNext; 271 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); 272 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); 273 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); 274 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET) 275 != boot->bpbFSInfo * boot->bpbBytesPerSec 276 || write(dosfs, fsinfo, sizeof fsinfo) 277 != sizeof fsinfo) { 278 perr("Unable to write bpbFSInfo"); 279 return FSFATAL; 280 } 281 /* 282 * Technically, we should return FSBOOTMOD here. 283 * 284 * However, since Win95 OSR2 (the first M$ OS that has 285 * support for FAT32) doesn't maintain the FSINFO block 286 * correctly, it has to be fixed pretty often. 287 * 288 * Therefor, we handle the FSINFO block only informally, 289 * fixing it if necessary, but otherwise ignoring the 290 * fact that it was incorrect. 291 */ 292 return 0; 293 } 294