San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1995, 1997 Wolfgang Solfrank |
| 3 | * Copyright (c) 1995 Martin Husemann |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by Martin Husemann |
| 16 | * and Wolfgang Solfrank. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 24 | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | #include <sys/cdefs.h> |
| 35 | #ifndef lint |
| 36 | __RCSID("$NetBSD: boot.c,v 1.9 2003/07/24 19:25:46 ws Exp $"); |
| 37 | static const char rcsid[] = |
| 38 | "$FreeBSD: src/sbin/fsck_msdosfs/boot.c,v 1.4.28.1 2009/04/15 03:14:26 kensmith Exp $"; |
| 39 | #endif /* not lint */ |
| 40 | |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <ctype.h> |
| 44 | #include <stdio.h> |
| 45 | #include <unistd.h> |
| 46 | |
| 47 | #include "ext.h" |
jianfeng | 594baf1 | 2012-06-19 19:53:15 +0800 | [diff] [blame] | 48 | #include "fatcache.h" |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 49 | #include "fsutil.h" |
| 50 | |
| 51 | int |
| 52 | readboot(dosfs, boot) |
| 53 | int dosfs; |
| 54 | struct bootblock *boot; |
| 55 | { |
| 56 | u_char block[DOSBOOTBLOCKSIZE]; |
| 57 | u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; |
| 58 | u_char backup[DOSBOOTBLOCKSIZE]; |
| 59 | int ret = FSOK; |
| 60 | |
| 61 | if (read(dosfs, block, sizeof block) < sizeof block) { |
| 62 | perror("could not read boot block"); |
| 63 | exit(2); |
| 64 | } |
| 65 | |
| 66 | if (block[510] != 0x55 || block[511] != 0xaa) { |
| 67 | pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]); |
| 68 | exit(2); |
| 69 | } |
| 70 | |
| 71 | memset(boot, 0, sizeof *boot); |
| 72 | boot->ValidFat = -1; |
| 73 | |
| 74 | /* decode bios parameter block */ |
| 75 | boot->BytesPerSec = block[11] + (block[12] << 8); |
| 76 | boot->SecPerClust = block[13]; |
jianfeng | 594baf1 | 2012-06-19 19:53:15 +0800 | [diff] [blame] | 77 | fsck_debug("sectors Per cluster :%d \n",boot->SecPerClust); |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 78 | boot->ResSectors = block[14] + (block[15] << 8); |
| 79 | boot->FATs = block[16]; |
| 80 | boot->RootDirEnts = block[17] + (block[18] << 8); |
| 81 | boot->Sectors = block[19] + (block[20] << 8); |
| 82 | boot->Media = block[21]; |
| 83 | boot->FATsmall = block[22] + (block[23] << 8); |
| 84 | boot->SecPerTrack = block[24] + (block[25] << 8); |
| 85 | boot->Heads = block[26] + (block[27] << 8); |
| 86 | boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); |
| 87 | boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); |
| 88 | |
| 89 | boot->FATsecs = boot->FATsmall; |
| 90 | |
jianfeng | 594baf1 | 2012-06-19 19:53:15 +0800 | [diff] [blame] | 91 | /* This variable is 0 in some filesystems such as exFat. |
| 92 | * It will cause divided by zero error in the following |
| 93 | * steps. Just return in this case. |
| 94 | */ |
| 95 | if (boot->BytesPerSec == 0) { |
| 96 | fsck_err("Invalid sector size: %u", boot->BytesPerSec); |
| 97 | exit(2); |
| 98 | return FSFATAL; |
| 99 | } |
| 100 | |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 101 | if (!boot->RootDirEnts) |
| 102 | boot->flags |= FAT32; |
| 103 | if (boot->flags & FAT32) { |
| 104 | boot->FATsecs = block[36] + (block[37] << 8) |
| 105 | + (block[38] << 16) + (block[39] << 24); |
| 106 | if (block[40] & 0x80) |
| 107 | boot->ValidFat = block[40] & 0x0f; |
| 108 | |
| 109 | /* check version number: */ |
| 110 | if (block[42] || block[43]) { |
| 111 | /* Correct? XXX */ |
| 112 | pfatal("Unknown file system version: %x.%x", |
| 113 | block[43], block[42]); |
| 114 | exit(2); |
| 115 | } |
| 116 | boot->RootCl = block[44] + (block[45] << 8) |
| 117 | + (block[46] << 16) + (block[47] << 24); |
| 118 | boot->FSInfo = block[48] + (block[49] << 8); |
| 119 | boot->Backup = block[50] + (block[51] << 8); |
| 120 | |
Ken Sumrall | c0a43e4 | 2013-06-25 20:02:00 -0700 | [diff] [blame] | 121 | /* If the OEM Name field is EXFAT, it's not FAT32, so bail */ |
| 122 | if (!memcmp(&block[3], "EXFAT ", 8)) { |
| 123 | pfatal("exFAT filesystem is not supported."); |
| 124 | return FSFATAL; |
| 125 | } |
| 126 | |
| 127 | /* check basic parameters */ |
| 128 | if ((boot->FSInfo == 0) || |
| 129 | (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) || |
| 130 | (boot->BytesPerSec / DOSBOOTBLOCKSIZE == 0) || |
| 131 | (boot->SecPerClust == 0)) { |
| 132 | /* |
| 133 | * Either the BIOS Parameter Block has been corrupted, |
| 134 | * or this is not a FAT32 filesystem, most likely an |
| 135 | * exFAT filesystem. |
| 136 | */ |
| 137 | pfatal("Invalid FAT32 Extended BIOS Parameter Block"); |
| 138 | return FSFATAL; |
| 139 | } |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 140 | if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) |
| 141 | != boot->FSInfo * boot->BytesPerSec |
| 142 | || read(dosfs, fsinfo, sizeof fsinfo) |
| 143 | != sizeof fsinfo) { |
| 144 | perror("could not read fsinfo block"); |
| 145 | return FSFATAL; |
| 146 | } |
| 147 | if (memcmp(fsinfo, "RRaA", 4) |
| 148 | || memcmp(fsinfo + 0x1e4, "rrAa", 4) |
| 149 | || fsinfo[0x1fc] |
| 150 | || fsinfo[0x1fd] |
| 151 | || fsinfo[0x1fe] != 0x55 |
| 152 | || fsinfo[0x1ff] != 0xaa |
| 153 | || fsinfo[0x3fc] |
| 154 | || fsinfo[0x3fd] |
| 155 | || fsinfo[0x3fe] != 0x55 |
| 156 | || fsinfo[0x3ff] != 0xaa) { |
| 157 | pwarn("Invalid signature in fsinfo block\n"); |
| 158 | if (ask(1, "fix")) { |
| 159 | memcpy(fsinfo, "RRaA", 4); |
| 160 | memcpy(fsinfo + 0x1e4, "rrAa", 4); |
| 161 | fsinfo[0x1fc] = fsinfo[0x1fd] = 0; |
| 162 | fsinfo[0x1fe] = 0x55; |
| 163 | fsinfo[0x1ff] = 0xaa; |
| 164 | fsinfo[0x3fc] = fsinfo[0x3fd] = 0; |
| 165 | fsinfo[0x3fe] = 0x55; |
| 166 | fsinfo[0x3ff] = 0xaa; |
| 167 | if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) |
| 168 | != boot->FSInfo * boot->BytesPerSec |
| 169 | || write(dosfs, fsinfo, sizeof fsinfo) |
| 170 | != sizeof fsinfo) { |
| 171 | perror("Unable to write FSInfo"); |
| 172 | return FSFATAL; |
| 173 | } |
| 174 | ret = FSBOOTMOD; |
| 175 | } else |
| 176 | boot->FSInfo = 0; |
| 177 | } |
| 178 | if (boot->FSInfo) { |
| 179 | boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8) |
| 180 | + (fsinfo[0x1ea] << 16) |
| 181 | + (fsinfo[0x1eb] << 24); |
| 182 | boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8) |
| 183 | + (fsinfo[0x1ee] << 16) |
| 184 | + (fsinfo[0x1ef] << 24); |
| 185 | } |
| 186 | |
| 187 | if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET) |
| 188 | != boot->Backup * boot->BytesPerSec |
| 189 | || read(dosfs, backup, sizeof backup) != sizeof backup) { |
| 190 | perror("could not read backup bootblock"); |
| 191 | return FSFATAL; |
| 192 | } |
| 193 | backup[65] = block[65]; /* XXX */ |
| 194 | if (memcmp(block + 11, backup + 11, 79)) { |
San Mehat | b47b163 | 2009-06-27 09:36:53 -0700 | [diff] [blame] | 195 | char tmp[255]; |
| 196 | int i; |
| 197 | |
| 198 | /* |
| 199 | * For now, lets not bail out if they don't match |
| 200 | * It seems a lot of sdcards are formatted with |
| 201 | * the backup either empty or containing garbage. |
| 202 | */ |
| 203 | |
| 204 | pwarn("Primary/Backup bootblock miscompare\n"); |
| 205 | |
| 206 | strcpy(tmp, ""); |
| 207 | pwarn("Primary:\n"); |
| 208 | for (i = 0; i < 79; i++) { |
| 209 | char tmp2[16]; |
| 210 | snprintf(tmp2, sizeof(tmp2), "%.2x ", block[11 + i]); |
| 211 | strcat(tmp, tmp2); |
| 212 | } |
| 213 | pwarn("%s\n", tmp); |
| 214 | |
| 215 | strcpy(tmp, ""); |
| 216 | pwarn("Backup:\n"); |
| 217 | for (i = 0; i < 79; i++) { |
| 218 | char tmp2[16]; |
| 219 | snprintf(tmp2, sizeof(tmp2), "%.2x ", backup[11 + i]); |
| 220 | strcat(tmp, tmp2); |
| 221 | } |
| 222 | pwarn("%s\n", tmp); |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 223 | } |
| 224 | /* Check backup FSInfo? XXX */ |
| 225 | } |
| 226 | |
jianfeng | 594baf1 | 2012-06-19 19:53:15 +0800 | [diff] [blame] | 227 | boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1) |
| 228 | / boot->BytesPerSec |
| 229 | + boot->ResSectors |
| 230 | + boot->FATs * boot->FATsecs |
| 231 | - CLUST_FIRST * boot->SecPerClust; |
| 232 | fsck_debug("boot->ClusterOffset :%d \n",boot->ClusterOffset); |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 233 | if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) { |
| 234 | pfatal("Invalid sector size: %u", boot->BytesPerSec); |
| 235 | return FSFATAL; |
| 236 | } |
| 237 | if (boot->SecPerClust == 0) { |
| 238 | pfatal("Invalid cluster size: %u", boot->SecPerClust); |
| 239 | return FSFATAL; |
| 240 | } |
Sebastian Rasmussen | 6c29bbe | 2011-02-08 16:57:27 +0100 | [diff] [blame] | 241 | if (boot->FATs == 0) { |
| 242 | pfatal("Invalid number of FATs: %u", boot->FATs); |
| 243 | return FSFATAL; |
| 244 | } |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 245 | if (boot->Sectors) { |
| 246 | boot->HugeSectors = 0; |
| 247 | boot->NumSectors = boot->Sectors; |
| 248 | } else |
| 249 | boot->NumSectors = boot->HugeSectors; |
San Mehat | 58d4c6c | 2009-06-25 08:08:05 -0700 | [diff] [blame] | 250 | boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust; |
| 251 | |
| 252 | if (boot->flags&FAT32) |
| 253 | boot->ClustMask = CLUST32_MASK; |
| 254 | else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) |
| 255 | boot->ClustMask = CLUST12_MASK; |
| 256 | else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) |
| 257 | boot->ClustMask = CLUST16_MASK; |
| 258 | else { |
| 259 | pfatal("Filesystem too big (%u clusters) for non-FAT32 partition", |
| 260 | boot->NumClusters); |
| 261 | return FSFATAL; |
| 262 | } |
| 263 | |
| 264 | switch (boot->ClustMask) { |
| 265 | case CLUST32_MASK: |
| 266 | boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4; |
| 267 | break; |
| 268 | case CLUST16_MASK: |
| 269 | boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2; |
| 270 | break; |
| 271 | default: |
| 272 | boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3; |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | if (boot->NumFatEntries < boot->NumClusters) { |
| 277 | pfatal("FAT size too small, %u entries won't fit into %u sectors\n", |
| 278 | boot->NumClusters, boot->FATsecs); |
| 279 | return FSFATAL; |
| 280 | } |
| 281 | boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust; |
| 282 | |
| 283 | boot->NumFiles = 1; |
| 284 | boot->NumFree = 0; |
| 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | int |
| 290 | writefsinfo(dosfs, boot) |
| 291 | int dosfs; |
| 292 | struct bootblock *boot; |
| 293 | { |
| 294 | u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; |
| 295 | |
| 296 | if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) |
| 297 | != boot->FSInfo * boot->BytesPerSec |
| 298 | || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) { |
| 299 | perror("could not read fsinfo block"); |
| 300 | return FSFATAL; |
| 301 | } |
| 302 | fsinfo[0x1e8] = (u_char)boot->FSFree; |
| 303 | fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); |
| 304 | fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); |
| 305 | fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); |
| 306 | fsinfo[0x1ec] = (u_char)boot->FSNext; |
| 307 | fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); |
| 308 | fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); |
| 309 | fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); |
| 310 | if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) |
| 311 | != boot->FSInfo * boot->BytesPerSec |
| 312 | || write(dosfs, fsinfo, sizeof fsinfo) |
| 313 | != sizeof fsinfo) { |
| 314 | perror("Unable to write FSInfo"); |
| 315 | return FSFATAL; |
| 316 | } |
| 317 | /* |
| 318 | * Technically, we should return FSBOOTMOD here. |
| 319 | * |
| 320 | * However, since Win95 OSR2 (the first M$ OS that has |
| 321 | * support for FAT32) doesn't maintain the FSINFO block |
| 322 | * correctly, it has to be fixed pretty often. |
| 323 | * |
| 324 | * Therefor, we handle the FSINFO block only informally, |
| 325 | * fixing it if necessary, but otherwise ignoring the |
| 326 | * fact that it was incorrect. |
| 327 | */ |
| 328 | return 0; |
| 329 | } |