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