boot.c (11827B)
1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #include "freebsd-compat.h" 29 #if defined(__RCSID) && !defined(lint) 30 __RCSID("$NetBSD: boot.c,v 1.22 2020/01/11 16:29:07 christos Exp $"); 31 #endif /* not lint */ 32 33 #include <sys/param.h> 34 35 #include <stdint.h> 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[DOSBOOTBLOCKSIZE]; 49 int ret = FSOK; 50 51 if ((size_t)read(dosfs, block, sizeof block) != sizeof block) { 52 perr("could not read boot block"); 53 return FSFATAL; 54 } 55 56 if (block[510] != 0x55 || block[511] != 0xaa) { 57 pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]); 58 return FSFATAL; 59 } 60 61 memset(boot, 0, sizeof *boot); 62 boot->ValidFat = -1; 63 64 /* Decode BIOS Parameter Block */ 65 66 /* Bytes per sector: can only be 512, 1024, 2048 and 4096. */ 67 boot->bpbBytesPerSec = block[11] + (block[12] << 8); 68 if (boot->bpbBytesPerSec < DOSBOOTBLOCKSIZE_REAL || boot->bpbBytesPerSec > DOSBOOTBLOCKSIZE 69 || !powerof2(boot->bpbBytesPerSec)) { 70 pfatal("Invalid sector size: %u", boot->bpbBytesPerSec); 71 return FSFATAL; 72 } 73 74 /* Sectors per cluster: can only be: 1, 2, 4, 8, 16, 32, 64, 128. */ 75 boot->bpbSecPerClust = block[13]; 76 if (boot->bpbSecPerClust == 0 || !powerof2(boot->bpbSecPerClust)) { 77 pfatal("Invalid cluster size: %u", boot->bpbSecPerClust); 78 return FSFATAL; 79 } 80 81 /* Reserved sectors: must be non-zero */ 82 boot->bpbResSectors = block[14] + (block[15] << 8); 83 if (boot->bpbResSectors < 1) { 84 pfatal("Invalid reserved sectors: %u", boot->bpbResSectors); 85 return FSFATAL; 86 } 87 88 /* Number of FATs */ 89 boot->bpbFATs = block[16]; 90 if (boot->bpbFATs == 0) { 91 pfatal("Invalid number of FATs: %u", boot->bpbFATs); 92 return FSFATAL; 93 } 94 95 /* Root directory entries for FAT12 and FAT16 */ 96 boot->bpbRootDirEnts = block[17] + (block[18] << 8); 97 if (!boot->bpbRootDirEnts) { 98 /* bpbRootDirEnts = 0 suggests that we are FAT32 */ 99 boot->flags |= FAT32; 100 } 101 102 /* Total sectors (16 bits) */ 103 boot->bpbSectors = block[19] + (block[20] << 8); 104 if (boot->bpbSectors != 0 && (boot->flags & FAT32)) { 105 pfatal("Invalid 16-bit total sector count on FAT32: %u", boot->bpbSectors); 106 return FSFATAL; 107 } 108 109 /* Media type: ignored */ 110 boot->bpbMedia = block[21]; 111 112 /* FAT12/FAT16: 16-bit count of sectors per FAT */ 113 boot->bpbFATsmall = block[22] + (block[23] << 8); 114 if (boot->bpbFATsmall != 0 && (boot->flags & FAT32)) { 115 pfatal("Invalid 16-bit FAT sector count on FAT32: %u", boot->bpbFATsmall); 116 return FSFATAL; 117 } 118 119 /* Legacy CHS geometry numbers: ignored */ 120 boot->SecPerTrack = block[24] + (block[25] << 8); 121 boot->bpbHeads = block[26] + (block[27] << 8); 122 123 /* Hidden sectors: ignored */ 124 boot->bpbHiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); 125 126 /* Total sectors (32 bits) */ 127 boot->bpbHugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); 128 if (boot->bpbHugeSectors == 0) { 129 if (boot->flags & FAT32) { 130 pfatal("FAT32 with sector count of zero"); 131 return FSFATAL; 132 } else if (boot->bpbSectors == 0) { 133 pfatal("FAT with sector count of zero"); 134 return FSFATAL; 135 } 136 boot->NumSectors = boot->bpbSectors; 137 } else { 138 if (boot->bpbSectors != 0) { 139 pfatal("Invalid FAT sector count"); 140 return FSFATAL; 141 } 142 boot->NumSectors = boot->bpbHugeSectors; 143 } 144 145 if (boot->flags & FAT32) { 146 /* If the OEM Name field is EXFAT, it's not FAT32, so bail */ 147 if (!memcmp(&block[3], "EXFAT ", 8)) { 148 pfatal("exFAT filesystem is not supported."); 149 return FSFATAL; 150 } 151 152 /* 32-bit count of sectors per FAT */ 153 boot->FATsecs = block[36] + (block[37] << 8) + (block[38] << 16) + (block[39] << 24); 154 155 if (block[40] & 0x80) 156 boot->ValidFat = block[40] & 0x0f; 157 158 /* FAT32 version, bail out if not 0.0 */ 159 if (block[42] || block[43]) { 160 pfatal("Unknown file system version: %x.%x", block[43], block[42]); 161 return FSFATAL; 162 } 163 164 /* 165 * Cluster number of the first cluster of root directory. 166 * 167 * Should be 2 but do not require it. 168 */ 169 boot->bpbRootClust = block[44] + (block[45] << 8) + (block[46] << 16) + (block[47] << 24); 170 171 /* Sector number of the FSInfo structure, usually 1 */ 172 boot->bpbFSInfo = block[48] + (block[49] << 8); 173 174 /* Sector number of the backup boot block, ignored */ 175 boot->bpbBackup = block[50] + (block[51] << 8); 176 177 /* Check basic parameters */ 178 if (boot->bpbFSInfo == 0) { 179 /* 180 * Either the BIOS Parameter Block has been corrupted, 181 * or this is not a FAT32 filesystem, most likely an 182 * exFAT filesystem. 183 */ 184 pfatal("Invalid FAT32 Extended BIOS Parameter Block"); 185 return FSFATAL; 186 } 187 188 /* FSInfo occupies one logical sector. */ 189 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET) 190 != boot->bpbFSInfo * boot->bpbBytesPerSec 191 || read(dosfs, fsinfo, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) { 192 perr("could not read fsinfo block"); 193 return FSFATAL; 194 } 195 if (memcmp(fsinfo, "RRaA", 4) || memcmp(fsinfo + 0x1e4, "rrAa", 4) || fsinfo[0x1fc] 196 || fsinfo[0x1fd] || fsinfo[0x1fe] != 0x55 || fsinfo[0x1ff] != 0xaa) { 197 pwarn("Invalid signature in fsinfo block\n"); 198 if (ask(0, "Fix")) { 199 memcpy(fsinfo, "RRaA", 4); 200 memcpy(fsinfo + 0x1e4, "rrAa", 4); 201 fsinfo[0x1fc] = fsinfo[0x1fd] = 0; 202 fsinfo[0x1fe] = 0x55; 203 fsinfo[0x1ff] = 0xaa; 204 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET) 205 != boot->bpbFSInfo * boot->bpbBytesPerSec 206 || write(dosfs, fsinfo, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) { 207 perr("Unable to write bpbFSInfo"); 208 return FSFATAL; 209 } 210 ret = FSBOOTMOD; 211 } else 212 boot->bpbFSInfo = 0; 213 } else { 214 /* We appear to have a valid FSInfo block, decode */ 215 boot->FSFree = 216 fsinfo[0x1e8] + (fsinfo[0x1e9] << 8) + (fsinfo[0x1ea] << 16) + (fsinfo[0x1eb] << 24); 217 boot->FSNext = 218 fsinfo[0x1ec] + (fsinfo[0x1ed] << 8) + (fsinfo[0x1ee] << 16) + (fsinfo[0x1ef] << 24); 219 } 220 } else { 221 /* !FAT32: FAT12/FAT16 */ 222 boot->FATsecs = boot->bpbFATsmall; 223 } 224 225 if (boot->FATsecs < 1 || boot->FATsecs > UINT32_MAX / boot->bpbFATs) { 226 pfatal("Invalid FATs(%u) with FATsecs(%zu)", boot->bpbFATs, (size_t)boot->FATsecs); 227 return FSFATAL; 228 } 229 230 boot->FirstCluster = (boot->bpbRootDirEnts * 32 + boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec 231 + boot->bpbResSectors + boot->bpbFATs * boot->FATsecs; 232 233 if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) { 234 pfatal("Cluster offset too large (%u clusters)\n", boot->FirstCluster); 235 return FSFATAL; 236 } 237 238 /* 239 * The number of clusters is derived from available data sectors, 240 * divided by sectors per cluster. 241 */ 242 boot->NumClusters = (boot->NumSectors - boot->FirstCluster) / boot->bpbSecPerClust; 243 244 if (boot->flags & FAT32) { 245 if (boot->NumClusters > (CLUST_RSRVD & CLUST32_MASK)) { 246 pfatal("Filesystem too big (%u clusters) for FAT32 partition", boot->NumClusters); 247 return FSFATAL; 248 } 249 if (boot->NumClusters < (CLUST_RSRVD & CLUST16_MASK)) { 250 pfatal("Filesystem too small (%u clusters) for FAT32 partition", boot->NumClusters); 251 return FSFATAL; 252 } 253 boot->ClustMask = CLUST32_MASK; 254 255 if (boot->bpbRootClust < CLUST_FIRST || boot->bpbRootClust >= boot->NumClusters) { 256 pfatal("Root directory starts with cluster out of range(%u)", boot->bpbRootClust); 257 return FSFATAL; 258 } 259 } else if (boot->NumClusters < (CLUST_RSRVD & CLUST12_MASK)) { 260 boot->ClustMask = CLUST12_MASK; 261 } else if (boot->NumClusters < (CLUST_RSRVD & CLUST16_MASK)) { 262 boot->ClustMask = CLUST16_MASK; 263 } else { 264 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition", boot->NumClusters); 265 return FSFATAL; 266 } 267 268 switch (boot->ClustMask) { 269 case CLUST32_MASK: 270 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4; 271 break; 272 case CLUST16_MASK: 273 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2; 274 break; 275 default: 276 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3; 277 break; 278 } 279 280 if (boot->NumFatEntries < boot->NumClusters) { 281 pfatal( 282 "FAT size too small, %u entries won't fit into %u sectors\n", 283 boot->NumClusters, 284 boot->FATsecs 285 ); 286 return FSFATAL; 287 } 288 289 /* 290 * There are two reserved clusters. To avoid adding CLUST_FIRST every 291 * time we perform boundary checks, we increment the NumClusters by 2, 292 * which is CLUST_FIRST to denote the first out-of-range cluster number. 293 */ 294 boot->NumClusters += CLUST_FIRST; 295 296 boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust; 297 298 boot->NumFiles = 1; 299 boot->NumFree = 0; 300 301 return ret; 302 } 303 304 int 305 writefsinfo(int dosfs, struct bootblock *boot) 306 { 307 u_char fsinfo[DOSBOOTBLOCKSIZE]; 308 309 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET) 310 != boot->bpbFSInfo * boot->bpbBytesPerSec 311 || read(dosfs, fsinfo, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) { 312 perr("could not read fsinfo block"); 313 return FSFATAL; 314 } 315 fsinfo[0x1e8] = (u_char)boot->FSFree; 316 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); 317 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); 318 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); 319 fsinfo[0x1ec] = (u_char)boot->FSNext; 320 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); 321 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); 322 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); 323 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET) 324 != boot->bpbFSInfo * boot->bpbBytesPerSec 325 || write(dosfs, fsinfo, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) { 326 perr("Unable to write bpbFSInfo"); 327 return FSFATAL; 328 } 329 /* 330 * Technically, we should return FSBOOTMOD here. 331 * 332 * However, since Win95 OSR2 (the first M$ OS that has 333 * support for FAT32) doesn't maintain the FSINFO block 334 * correctly, it has to be fixed pretty often. 335 * 336 * Therefore, we handle the FSINFO block only informally, 337 * fixing it if necessary, but otherwise ignoring the 338 * fact that it was incorrect. 339 */ 340 return 0; 341 }