1 /* $OpenBSD: boot.c,v 1.19 2014/06/14 15:23:45 tobias Exp $ */ 2 /* $NetBSD: boot.c,v 1.5 1997/10/17 11:19:23 ws Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1997 Wolfgang Solfrank 6 * Copyright (c) 1995 Martin Husemann 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Martin Husemann 19 * and Wolfgang Solfrank. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/disklabel.h> 38 39 #include <stdlib.h> 40 #include <string.h> 41 #include <ctype.h> 42 #include <stdio.h> 43 #include <unistd.h> 44 45 #include "ext.h" 46 47 int 48 readboot(int dosfs, struct bootblock *boot) 49 { 50 u_char *block = NULL; 51 u_char *fsinfo = NULL; 52 u_char *backup = NULL; 53 int ret = FSOK, secsize = lab.d_secsize, fsinfosz; 54 off_t o; 55 ssize_t n; 56 57 if (secsize < DOSBOOTBLOCKSIZE) { 58 xperror("sector size < DOSBOOTBLOCKSIZE"); 59 goto fail; 60 } 61 if (DOSBOOTBLOCKSIZE != DEV_BSIZE) { 62 xperror("DOSBOOTBLOCKSIZE != DEV_BSIZE"); 63 goto fail; 64 } 65 66 block = malloc(secsize); 67 if (block == NULL) { 68 xperror("could not malloc boot block"); 69 goto fail; 70 } 71 72 if ((o = lseek(dosfs, 0, SEEK_SET)) == -1) { 73 xperror("could not seek boot block"); 74 goto fail; 75 } 76 77 n = read(dosfs, block, secsize); 78 if (n == -1 || n != secsize) { 79 xperror("could not read boot block"); 80 goto fail; 81 } 82 83 if (block[510] != 0x55 || block[511] != 0xaa) { 84 pfatal("Invalid signature in boot block: %02x%02x\n", 85 block[511], block[510]); 86 } 87 88 memset(boot, 0, sizeof *boot); 89 boot->ValidFat = -1; 90 91 /* decode bios parameter block */ 92 boot->BytesPerSec = block[11] + (block[12] << 8); 93 if (boot->BytesPerSec == 0 || boot->BytesPerSec != secsize) { 94 pfatal("Invalid sector size: %u\n", boot->BytesPerSec); 95 goto fail; 96 } 97 boot->SecPerClust = block[13]; 98 if (boot->SecPerClust == 0) { 99 pfatal("Invalid cluster size: %u\n", boot->SecPerClust); 100 goto fail; 101 } 102 boot->ResSectors = block[14] + (block[15] << 8); 103 boot->FATs = block[16]; 104 boot->RootDirEnts = block[17] + (block[18] << 8); 105 boot->Sectors = block[19] + (block[20] << 8); 106 boot->Media = block[21]; 107 boot->FATsmall = block[22] + (block[23] << 8); 108 boot->SecPerTrack = block[24] + (block[25] << 8); 109 boot->Heads = block[26] + (block[27] << 8); 110 boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); 111 boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); 112 113 boot->FATsecs = boot->FATsmall; 114 115 if (!boot->RootDirEnts) { 116 boot->flags |= FAT32; 117 boot->FATsecs = block[36] + (block[37] << 8) 118 + (block[38] << 16) + (block[39] << 24); 119 if (block[40] & 0x80) 120 boot->ValidFat = block[40] & 0x0f; 121 122 /* check version number: */ 123 if (block[42] || block[43]) { 124 /* Correct? XXX */ 125 pfatal("Unknown filesystem version: %x.%x\n", 126 block[43], block[42]); 127 goto fail; 128 } 129 boot->RootCl = block[44] + (block[45] << 8) 130 + (block[46] << 16) + (block[47] << 24); 131 boot->FSInfo = block[48] + (block[49] << 8); 132 boot->Backup = block[50] + (block[51] << 8); 133 134 o = lseek(dosfs, boot->FSInfo * secsize, SEEK_SET); 135 if (o == -1 || o != boot->FSInfo * secsize) { 136 xperror("could not seek fsinfo block"); 137 goto fail; 138 } 139 140 if ((2 * DOSBOOTBLOCKSIZE) < secsize) 141 fsinfosz = secsize; 142 else 143 fsinfosz = 2 * secsize; 144 fsinfo = malloc(fsinfosz); 145 if (fsinfo == NULL) { 146 xperror("could not malloc fsinfo"); 147 goto fail; 148 } 149 n = read(dosfs, fsinfo, fsinfosz); 150 if (n == -1 || n != fsinfosz) { 151 xperror("could not read fsinfo block"); 152 goto fail; 153 } 154 155 if (memcmp(fsinfo, "RRaA", 4) 156 || memcmp(fsinfo + 0x1e4, "rrAa", 4) 157 || fsinfo[0x1fc] 158 || fsinfo[0x1fd] 159 || fsinfo[0x1fe] != 0x55 160 || fsinfo[0x1ff] != 0xaa 161 || fsinfo[0x3fc] 162 || fsinfo[0x3fd] 163 || fsinfo[0x3fe] != 0x55 164 || fsinfo[0x3ff] != 0xaa) { 165 pwarn("Invalid signature in fsinfo block\n"); 166 if (ask(0, "fix")) { 167 memcpy(fsinfo, "RRaA", 4); 168 memcpy(fsinfo + 0x1e4, "rrAa", 4); 169 fsinfo[0x1fc] = fsinfo[0x1fd] = 0; 170 fsinfo[0x1fe] = 0x55; 171 fsinfo[0x1ff] = 0xaa; 172 fsinfo[0x3fc] = fsinfo[0x3fd] = 0; 173 fsinfo[0x3fe] = 0x55; 174 fsinfo[0x3ff] = 0xaa; 175 176 o = lseek(dosfs, boot->FSInfo * secsize, 177 SEEK_SET); 178 if (o == -1 || o != boot->FSInfo * secsize) { 179 xperror("Unable to seek FSInfo"); 180 goto fail; 181 } 182 n = write(dosfs, fsinfo, fsinfosz); 183 if (n == -1 || n != fsinfosz) { 184 xperror("Unable to write FSInfo"); 185 goto fail; 186 } 187 ret = FSBOOTMOD; 188 } else 189 boot->FSInfo = 0; 190 } 191 if (boot->FSInfo) { 192 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8) 193 + (fsinfo[0x1ea] << 16) 194 + (fsinfo[0x1eb] << 24); 195 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8) 196 + (fsinfo[0x1ee] << 16) 197 + (fsinfo[0x1ef] << 24); 198 } 199 200 o = lseek(dosfs, boot->Backup * secsize, SEEK_SET); 201 if (o == -1 || o != boot->Backup * secsize) { 202 xperror("could not seek backup bootblock"); 203 goto fail; 204 } 205 backup = malloc(2 * secsize); /* In case we check fsinfo. */ 206 if (backup == NULL) { 207 xperror("could not malloc backup boot block"); 208 goto fail; 209 } 210 n = read(dosfs, backup, secsize); 211 if (n == -1 || n != secsize) { 212 xperror("could not read backup bootblock"); 213 goto fail; 214 } 215 216 /* 217 * Check that the backup boot block matches the primary one. 218 * We don't check every byte, since some vendor utilities 219 * seem to overwrite the boot code when they feel like it, 220 * without changing the backup block. Specifically, we check 221 * the two-byte signature at the end, the BIOS parameter 222 * block (which starts after the 3-byte JMP and the 8-byte 223 * OEM name/version) and the filesystem information that 224 * follows the BPB (bsPBP[53] and bsExt[26] for FAT32, so we 225 * check 79 bytes). 226 */ 227 if (backup[510] != 0x55 || backup[511] != 0xaa) { 228 pfatal("Invalid signature in backup boot block: %02x%02x\n", backup[511], backup[510]); 229 } 230 if (memcmp(block + 11, backup + 11, 79)) { 231 pfatal("backup doesn't compare to primary bootblock\n"); 232 goto fail; 233 } 234 /* Check backup FSInfo? XXX */ 235 } 236 237 boot->ClusterOffset = (boot->RootDirEnts * 32 + secsize - 1) 238 / secsize 239 + boot->ResSectors 240 + boot->FATs * boot->FATsecs 241 - CLUST_FIRST * boot->SecPerClust; 242 243 if (boot->Sectors) { 244 boot->HugeSectors = 0; 245 boot->NumSectors = boot->Sectors; 246 } else 247 boot->NumSectors = boot->HugeSectors; 248 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust; 249 250 if (boot->flags&FAT32) 251 boot->ClustMask = CLUST32_MASK; 252 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) 253 boot->ClustMask = CLUST12_MASK; 254 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) 255 boot->ClustMask = CLUST16_MASK; 256 else { 257 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition\n", 258 boot->NumClusters); 259 goto fail; 260 } 261 262 switch (boot->ClustMask) { 263 case CLUST32_MASK: 264 boot->NumFatEntries = (boot->FATsecs * secsize) / 4; 265 break; 266 case CLUST16_MASK: 267 boot->NumFatEntries = (boot->FATsecs * secsize) / 2; 268 break; 269 default: 270 boot->NumFatEntries = (boot->FATsecs * secsize * 2) / 3; 271 break; 272 } 273 274 if (boot->NumFatEntries < boot->NumClusters) { 275 pfatal("FAT size too small, %u entries won't fit into %u sectors\n", 276 boot->NumClusters, boot->FATsecs); 277 goto fail; 278 } 279 boot->ClusterSize = boot->SecPerClust * secsize; 280 281 boot->NumFiles = 1; 282 boot->NumFree = 0; 283 284 free(backup); 285 free(block); 286 free(fsinfo); 287 return ret; 288 fail: 289 free(backup); 290 free(block); 291 free(fsinfo); 292 return FSFATAL; 293 } 294 295 int 296 writefsinfo(int dosfs, struct bootblock *boot) 297 { 298 u_char *fsinfo = NULL; 299 int secsize = lab.d_secsize, fsinfosz; 300 off_t o; 301 ssize_t n; 302 303 if ((2 * DOSBOOTBLOCKSIZE) < secsize) 304 fsinfosz = secsize; 305 else 306 fsinfosz = 2 * secsize; 307 308 fsinfo = malloc(fsinfosz); 309 if (fsinfo == NULL) { 310 xperror("could not malloc fsinfo block"); 311 goto fail; 312 } 313 314 o = lseek(dosfs, boot->FSInfo * secsize, SEEK_SET); 315 if (o == -1 || o != boot->FSInfo * secsize) { 316 xperror("could not seek fsinfo block"); 317 goto fail; 318 } 319 320 n = read(dosfs, fsinfo, fsinfosz); 321 if (n == -1 || n != fsinfosz) { 322 xperror("could not read fsinfo block"); 323 goto fail; 324 } 325 326 fsinfo[0x1e8] = (u_char)boot->FSFree; 327 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); 328 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); 329 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); 330 fsinfo[0x1ec] = (u_char)boot->FSNext; 331 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); 332 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); 333 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); 334 335 o = lseek(dosfs, o, SEEK_SET); 336 if (o == -1 || o != boot->FSInfo * boot->BytesPerSec) { 337 xperror("Unable to seek FSInfo"); 338 goto fail; 339 } 340 n = write(dosfs, fsinfo, fsinfosz); 341 if (n == -1 || n != fsinfosz) { 342 xperror("Unable to write FSInfo"); 343 goto fail; 344 } 345 346 free(fsinfo); 347 348 /* 349 * Technically, we should return FSBOOTMOD here. 350 * 351 * However, since Win95 OSR2 (the first M$ OS that has 352 * support for FAT32) doesn't maintain the FSINFO block 353 * correctly, it has to be fixed pretty often. 354 * 355 * Therefore, we handle the FSINFO block only informally, 356 * fixing it if necessary, but otherwise ignoring the 357 * fact that it was incorrect. 358 */ 359 return 0; 360 fail: 361 free(fsinfo); 362 return FSFATAL; 363 } 364