1 /* $OpenBSD: boot.c,v 1.15 2010/12/18 04:57:34 deraadt 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 <stdlib.h> 37 #include <string.h> 38 #include <ctype.h> 39 #include <stdio.h> 40 #include <unistd.h> 41 42 #include "ext.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 off_t o; 52 ssize_t n; 53 54 if ((n = read(dosfs, block, sizeof block)) == -1 || n != sizeof block) { 55 xperror("could not read boot block"); 56 return (FSFATAL); 57 } 58 59 if (block[510] != 0x55 || block[511] != 0xaa) { 60 pfatal("Invalid signature in boot block: %02x%02x\n", 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->BytesPerSec = block[11] + (block[12] << 8); 69 boot->SecPerClust = block[13]; 70 boot->ResSectors = block[14] + (block[15] << 8); 71 boot->FATs = block[16]; 72 boot->RootDirEnts = block[17] + (block[18] << 8); 73 boot->Sectors = block[19] + (block[20] << 8); 74 boot->Media = block[21]; 75 boot->FATsmall = block[22] + (block[23] << 8); 76 boot->SecPerTrack = block[24] + (block[25] << 8); 77 boot->Heads = block[26] + (block[27] << 8); 78 boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); 79 boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); 80 81 boot->FATsecs = boot->FATsmall; 82 83 if (!boot->RootDirEnts) 84 boot->flags |= FAT32; 85 if (boot->flags & FAT32) { 86 boot->FATsecs = block[36] + (block[37] << 8) 87 + (block[38] << 16) + (block[39] << 24); 88 if (block[40] & 0x80) 89 boot->ValidFat = block[40] & 0x0f; 90 91 /* check version number: */ 92 if (block[42] || block[43]) { 93 /* Correct? XXX */ 94 pfatal("Unknown filesystem version: %x.%x\n", 95 block[43], block[42]); 96 return FSFATAL; 97 } 98 boot->RootCl = block[44] + (block[45] << 8) 99 + (block[46] << 16) + (block[47] << 24); 100 boot->FSInfo = block[48] + (block[49] << 8); 101 boot->Backup = block[50] + (block[51] << 8); 102 103 o = boot->FSInfo * boot->BytesPerSec; 104 if ((o = lseek(dosfs, o, SEEK_SET)) == -1 105 || o != boot->FSInfo * boot->BytesPerSec 106 || (n = read(dosfs, fsinfo, sizeof fsinfo)) == -1 107 || n != sizeof fsinfo) { 108 xperror("could not read fsinfo block"); 109 return FSFATAL; 110 } 111 if (memcmp(fsinfo, "RRaA", 4) 112 || memcmp(fsinfo + 0x1e4, "rrAa", 4) 113 || fsinfo[0x1fc] 114 || fsinfo[0x1fd] 115 || fsinfo[0x1fe] != 0x55 116 || fsinfo[0x1ff] != 0xaa 117 || fsinfo[0x3fc] 118 || fsinfo[0x3fd] 119 || fsinfo[0x3fe] != 0x55 120 || fsinfo[0x3ff] != 0xaa) { 121 pwarn("Invalid signature in fsinfo block"); 122 if (ask(0, "fix")) { 123 memcpy(fsinfo, "RRaA", 4); 124 memcpy(fsinfo + 0x1e4, "rrAa", 4); 125 fsinfo[0x1fc] = fsinfo[0x1fd] = 0; 126 fsinfo[0x1fe] = 0x55; 127 fsinfo[0x1ff] = 0xaa; 128 fsinfo[0x3fc] = fsinfo[0x3fd] = 0; 129 fsinfo[0x3fe] = 0x55; 130 fsinfo[0x3ff] = 0xaa; 131 132 o = boot->FSInfo * boot->BytesPerSec; 133 if ((o = lseek(dosfs, o, SEEK_SET)) == -1 134 || o != boot->FSInfo * boot->BytesPerSec 135 || (n = write(dosfs, fsinfo, sizeof fsinfo)) == -1 136 || n != sizeof fsinfo) { 137 xperror("Unable to write FSInfo"); 138 return FSFATAL; 139 } 140 ret = FSBOOTMOD; 141 } else 142 boot->FSInfo = 0; 143 } 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); 151 } 152 153 o = boot->Backup * boot->BytesPerSec; 154 if ((o = lseek(dosfs, o, SEEK_SET)) == -1 155 || o != boot->Backup * boot->BytesPerSec 156 || (n = read(dosfs, backup, sizeof backup)) == -1 157 || n != sizeof backup) { 158 xperror("could not read backup bootblock"); 159 return FSFATAL; 160 } 161 162 /* 163 * Check that the backup boot block matches the primary one. 164 * We don't check every byte, since some vendor utilities 165 * seem to overwrite the boot code when they feel like it, 166 * without changing the backup block. Specifically, we check 167 * the two-byte signature at the end, the BIOS parameter 168 * block (which starts after the 3-byte JMP and the 8-byte 169 * OEM name/version) and the filesystem information that 170 * follows the BPB (bsPBP[53] and bsExt[26] for FAT32, so we 171 * check 79 bytes). 172 */ 173 if (backup[510] != 0x55 || backup[511] != 0xaa) { 174 pfatal("Invalid signature in backup boot block: %02x%02x\n", backup[511], backup[510]); 175 return FSFATAL; 176 } 177 if (memcmp(block + 11, backup + 11, 79)) { 178 pfatal("backup doesn't compare to primary bootblock\n"); 179 return FSFATAL; 180 } 181 /* Check backup FSInfo? XXX */ 182 } 183 184 if (boot->BytesPerSec == 0 || boot->BytesPerSec % DOSBOOTBLOCKSIZE 185 != 0) { 186 pfatal("Invalid sector size: %u\n", boot->BytesPerSec); 187 return (FSFATAL); 188 } 189 if (boot->SecPerClust == 0) { 190 pfatal("Invalid cluster size: %u\n", boot->SecPerClust); 191 return (FSFATAL); 192 } 193 194 boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1) 195 / boot->BytesPerSec 196 + boot->ResSectors 197 + boot->FATs * boot->FATsecs 198 - CLUST_FIRST * boot->SecPerClust; 199 200 if (boot->Sectors) { 201 boot->HugeSectors = 0; 202 boot->NumSectors = boot->Sectors; 203 } else 204 boot->NumSectors = boot->HugeSectors; 205 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust; 206 207 if (boot->flags&FAT32) 208 boot->ClustMask = CLUST32_MASK; 209 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) 210 boot->ClustMask = CLUST12_MASK; 211 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) 212 boot->ClustMask = CLUST16_MASK; 213 else { 214 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition\n", 215 boot->NumClusters); 216 return FSFATAL; 217 } 218 219 switch (boot->ClustMask) { 220 case CLUST32_MASK: 221 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4; 222 break; 223 case CLUST16_MASK: 224 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2; 225 break; 226 default: 227 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3; 228 break; 229 } 230 231 if (boot->NumFatEntries < boot->NumClusters) { 232 pfatal("FAT size too small, %u entries won't fit into %u sectors\n", 233 boot->NumClusters, boot->FATsecs); 234 return (FSFATAL); 235 } 236 boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust; 237 238 boot->NumFiles = 1; 239 boot->NumFree = 0; 240 241 return ret; 242 } 243 244 int 245 writefsinfo(int dosfs, struct bootblock *boot) 246 { 247 u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 248 off_t o; 249 ssize_t n; 250 251 o = boot->FSInfo * boot->BytesPerSec; 252 if ((o = lseek(dosfs, o, SEEK_SET)) == -1 253 || o != boot->FSInfo * boot->BytesPerSec 254 || (n = read(dosfs, fsinfo, sizeof fsinfo)) == -1 255 || n != sizeof fsinfo) { 256 xperror("could not read fsinfo block"); 257 return FSFATAL; 258 } 259 fsinfo[0x1e8] = (u_char)boot->FSFree; 260 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); 261 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); 262 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); 263 fsinfo[0x1ec] = (u_char)boot->FSNext; 264 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); 265 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); 266 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); 267 268 o = boot->FSInfo * boot->BytesPerSec; 269 if ((o = lseek(dosfs, o, SEEK_SET)) == -1 270 || o != boot->FSInfo * boot->BytesPerSec 271 || (n = write(dosfs, fsinfo, sizeof fsinfo)) == -1 272 || n != sizeof fsinfo) { 273 xperror("Unable to write FSInfo"); 274 return FSFATAL; 275 } 276 /* 277 * Technically, we should return FSBOOTMOD here. 278 * 279 * However, since Win95 OSR2 (the first M$ OS that has 280 * support for FAT32) doesn't maintain the FSINFO block 281 * correctly, it has to be fixed pretty often. 282 * 283 * Therefore, we handle the FSINFO block only informally, 284 * fixing it if necessary, but otherwise ignoring the 285 * fact that it was incorrect. 286 */ 287 return 0; 288 } 289