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