1 /* $NetBSD: boot.c,v 1.13 2007/03/19 18:30:40 gdt Exp $ */ 2 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Martin Husemann 18 * and Wolfgang Solfrank. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __RCSID("$NetBSD: boot.c,v 1.13 2007/03/19 18:30:40 gdt Exp $"); 39 #endif /* not lint */ 40 41 #include <stdlib.h> 42 #include <string.h> 43 #include <stdio.h> 44 #include <unistd.h> 45 46 #include "ext.h" 47 #include "fsutil.h" 48 49 int 50 readboot(int dosfs, struct bootblock *boot) 51 { 52 u_char block[DOSBOOTBLOCKSIZE]; 53 u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 54 u_char backup[DOSBOOTBLOCKSIZE]; 55 int ret = FSOK; 56 int i; 57 58 if (read(dosfs, block, sizeof block) < sizeof block) { 59 perr("could not read boot block"); 60 return FSFATAL; 61 } 62 63 if (block[510] != 0x55 || block[511] != 0xaa) { 64 pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]); 65 return FSFATAL; 66 } 67 68 memset(boot, 0, sizeof *boot); 69 boot->ValidFat = -1; 70 71 /* decode bios parameter block */ 72 boot->BytesPerSec = block[11] + (block[12] << 8); 73 boot->SecPerClust = block[13]; 74 boot->ResSectors = block[14] + (block[15] << 8); 75 boot->FATs = block[16]; 76 boot->RootDirEnts = block[17] + (block[18] << 8); 77 boot->Sectors = block[19] + (block[20] << 8); 78 boot->Media = block[21]; 79 boot->FATsmall = block[22] + (block[23] << 8); 80 boot->SecPerTrack = block[24] + (block[25] << 8); 81 boot->Heads = block[26] + (block[27] << 8); 82 boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); 83 boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); 84 85 boot->FATsecs = boot->FATsmall; 86 87 if (!boot->RootDirEnts) 88 boot->flags |= FAT32; 89 if (boot->flags & FAT32) { 90 boot->FATsecs = block[36] + (block[37] << 8) 91 + (block[38] << 16) + (block[39] << 24); 92 if (block[40] & 0x80) 93 boot->ValidFat = block[40] & 0x0f; 94 95 /* check version number: */ 96 if (block[42] || block[43]) { 97 /* Correct? XXX */ 98 pfatal("Unknown filesystem version: %x.%x", 99 block[43], block[42]); 100 return FSFATAL; 101 } 102 boot->RootCl = block[44] + (block[45] << 8) 103 + (block[46] << 16) + (block[47] << 24); 104 boot->FSInfo = block[48] + (block[49] << 8); 105 boot->Backup = block[50] + (block[51] << 8); 106 107 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 108 != boot->FSInfo * boot->BytesPerSec 109 || read(dosfs, fsinfo, sizeof fsinfo) 110 != sizeof fsinfo) { 111 perr("could not read fsinfo block"); 112 return FSFATAL; 113 } 114 if (memcmp(fsinfo, "RRaA", 4) 115 || memcmp(fsinfo + 0x1e4, "rrAa", 4) 116 || fsinfo[0x1fc] 117 || fsinfo[0x1fd] 118 || fsinfo[0x1fe] != 0x55 119 || fsinfo[0x1ff] != 0xaa 120 || fsinfo[0x3fc] 121 || fsinfo[0x3fd] 122 || fsinfo[0x3fe] != 0x55 123 || fsinfo[0x3ff] != 0xaa) { 124 pwarn("Invalid signature in fsinfo block"); 125 if (ask(0, "fix")) { 126 memcpy(fsinfo, "RRaA", 4); 127 memcpy(fsinfo + 0x1e4, "rrAa", 4); 128 fsinfo[0x1fc] = fsinfo[0x1fd] = 0; 129 fsinfo[0x1fe] = 0x55; 130 fsinfo[0x1ff] = 0xaa; 131 fsinfo[0x3fc] = fsinfo[0x3fd] = 0; 132 fsinfo[0x3fe] = 0x55; 133 fsinfo[0x3ff] = 0xaa; 134 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 135 != boot->FSInfo * boot->BytesPerSec 136 || write(dosfs, fsinfo, sizeof fsinfo) 137 != sizeof fsinfo) { 138 perr("Unable to write FSInfo"); 139 return FSFATAL; 140 } 141 ret = FSBOOTMOD; 142 } else 143 boot->FSInfo = 0; 144 } 145 if (boot->FSInfo) { 146 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8) 147 + (fsinfo[0x1ea] << 16) 148 + (fsinfo[0x1eb] << 24); 149 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8) 150 + (fsinfo[0x1ee] << 16) 151 + (fsinfo[0x1ef] << 24); 152 } 153 154 if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET) 155 != boot->Backup * boot->BytesPerSec 156 || read(dosfs, backup, sizeof backup) != sizeof backup) { 157 perr("could not read backup bootblock"); 158 return FSFATAL; 159 } 160 backup[65] = block[65]; /* XXX */ 161 if (memcmp(block + 11, backup + 11, 79)) { 162 /* 163 * XXX We require a reference that explains 164 * that these bytes need to match, or should 165 * drop the check. gdt@ has observed 166 * filesystems that work fine under Windows XP 167 * and NetBSD that do not match, so the 168 * requirement is suspect. For now, just 169 * print out useful information and continue. 170 */ 171 pfatal("backup (block %d) mismatch with primary bootblock:\n", 172 boot->Backup); 173 for (i = 11; i < 11 + 90; i++) { 174 if (block[i] != backup[i]) 175 pfatal("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n", 176 i, block[i], backup[i]); 177 } 178 } 179 /* Check backup FSInfo? XXX */ 180 } 181 182 boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1) 183 / boot->BytesPerSec 184 + boot->ResSectors 185 + boot->FATs * boot->FATsecs 186 - CLUST_FIRST * boot->SecPerClust; 187 188 if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) { 189 pfatal("Invalid sector size: %u", boot->BytesPerSec); 190 return FSFATAL; 191 } 192 if (boot->SecPerClust == 0) { 193 pfatal("Invalid cluster size: %u", boot->SecPerClust); 194 return FSFATAL; 195 } 196 if (boot->Sectors) { 197 boot->HugeSectors = 0; 198 boot->NumSectors = boot->Sectors; 199 } else 200 boot->NumSectors = boot->HugeSectors; 201 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust; 202 203 if (boot->flags&FAT32) 204 boot->ClustMask = CLUST32_MASK; 205 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) 206 boot->ClustMask = CLUST12_MASK; 207 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) 208 boot->ClustMask = CLUST16_MASK; 209 else { 210 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition", 211 boot->NumClusters); 212 return FSFATAL; 213 } 214 215 switch (boot->ClustMask) { 216 case CLUST32_MASK: 217 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4; 218 break; 219 case CLUST16_MASK: 220 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2; 221 break; 222 default: 223 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3; 224 break; 225 } 226 227 if (boot->NumFatEntries < boot->NumClusters) { 228 pfatal("FAT size too small, %u entries won't fit into %u sectors\n", 229 boot->NumClusters, boot->FATsecs); 230 return FSFATAL; 231 } 232 boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust; 233 234 boot->NumFiles = 1; 235 boot->NumFree = 0; 236 237 return ret; 238 } 239 240 int 241 writefsinfo(int dosfs, struct bootblock *boot) 242 { 243 u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 244 245 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 246 != boot->FSInfo * boot->BytesPerSec 247 || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) { 248 perr("could not read fsinfo block"); 249 return FSFATAL; 250 } 251 fsinfo[0x1e8] = (u_char)boot->FSFree; 252 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); 253 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); 254 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); 255 fsinfo[0x1ec] = (u_char)boot->FSNext; 256 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); 257 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); 258 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); 259 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 260 != boot->FSInfo * boot->BytesPerSec 261 || write(dosfs, fsinfo, sizeof fsinfo) 262 != sizeof fsinfo) { 263 perr("Unable to write FSInfo"); 264 return FSFATAL; 265 } 266 /* 267 * Technically, we should return FSBOOTMOD here. 268 * 269 * However, since Win95 OSR2 (the first M$ OS that has 270 * support for FAT32) doesn't maintain the FSINFO block 271 * correctly, it has to be fixed pretty often. 272 * 273 * Therefor, we handle the FSINFO block only informally, 274 * fixing it if necessary, but otherwise ignoring the 275 * fact that it was incorrect. 276 */ 277 return 0; 278 } 279