boot.c revision 175287
11553Srgrimes/* 21553Srgrimes * Copyright (C) 1995, 1997 Wolfgang Solfrank 31553Srgrimes * Copyright (c) 1995 Martin Husemann 41553Srgrimes * 51553Srgrimes * Redistribution and use in source and binary forms, with or without 61553Srgrimes * modification, are permitted provided that the following conditions 71553Srgrimes * are met: 81553Srgrimes * 1. Redistributions of source code must retain the above copyright 91553Srgrimes * notice, this list of conditions and the following disclaimer. 101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111553Srgrimes * notice, this list of conditions and the following disclaimer in the 121553Srgrimes * documentation and/or other materials provided with the distribution. 131553Srgrimes * 3. All advertising materials mentioning features or use of this software 141553Srgrimes * must display the following acknowledgement: 151553Srgrimes * This product includes software developed by Martin Husemann 161553Srgrimes * and Wolfgang Solfrank. 171553Srgrimes * 4. Neither the name of the University nor the names of its contributors 181553Srgrimes * may be used to endorse or promote products derived from this software 191553Srgrimes * without specific prior written permission. 201553Srgrimes * 211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 221553Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 231553Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 241553Srgrimes * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251553Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 261553Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 271553Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 281553Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2950479Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 301553Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31115952Smtm */ 321553Srgrimes 3379537Sru 341553Srgrimes#include <sys/cdefs.h> 351553Srgrimes#ifndef lint 361553Srgrimes__RCSID("$NetBSD: boot.c,v 1.9 2003/07/24 19:25:46 ws Exp $"); 371553Srgrimesstatic const char rcsid[] = 3868965Sru "$FreeBSD: head/sbin/fsck_msdosfs/boot.c 175287 2008-01-13 07:56:36Z yar $"; 39115952Smtm#endif /* not lint */ 40115952Smtm 41115952Smtm#include <stdlib.h> 421553Srgrimes#include <string.h> 431553Srgrimes#include <ctype.h> 441553Srgrimes#include <stdio.h> 451553Srgrimes#include <unistd.h> 4629448Scharnier 4799968Scharnier#include "ext.h" 481553Srgrimes#include "fsutil.h" 4992326Sru 501553Srgrimesint 51115952Smtmreadboot(dosfs, boot) 52115952Smtm int dosfs; 53115952Smtm struct bootblock *boot; 54115952Smtm{ 55115952Smtm u_char block[DOSBOOTBLOCKSIZE]; 56115952Smtm u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 57115952Smtm u_char backup[DOSBOOTBLOCKSIZE]; 58115952Smtm int ret = FSOK; 59115952Smtm 60115952Smtm if (read(dosfs, block, sizeof block) < sizeof block) { 61115952Smtm perror("could not read boot block"); 62115952Smtm return FSFATAL; 63241330Sjoel } 64115952Smtm 651553Srgrimes if (block[510] != 0x55 || block[511] != 0xaa) { 661553Srgrimes pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]); 6768965Sru return FSFATAL; 6892326Sru } 691553Srgrimes 701553Srgrimes memset(boot, 0, sizeof *boot); 711553Srgrimes boot->ValidFat = -1; 721553Srgrimes 731553Srgrimes /* decode bios parameter block */ 741553Srgrimes boot->BytesPerSec = block[11] + (block[12] << 8); 751553Srgrimes boot->SecPerClust = block[13]; 761553Srgrimes boot->ResSectors = block[14] + (block[15] << 8); 771553Srgrimes boot->FATs = block[16]; 781553Srgrimes boot->RootDirEnts = block[17] + (block[18] << 8); 791553Srgrimes boot->Sectors = block[19] + (block[20] << 8); 801553Srgrimes boot->Media = block[21]; 811553Srgrimes boot->FATsmall = block[22] + (block[23] << 8); 821553Srgrimes boot->SecPerTrack = block[24] + (block[25] << 8); 831553Srgrimes boot->Heads = block[26] + (block[27] << 8); 84115952Smtm boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24); 85115952Smtm boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24); 86115952Smtm 87115952Smtm boot->FATsecs = boot->FATsmall; 8888572Smpp 8988572Smpp if (!boot->RootDirEnts) 901553Srgrimes boot->flags |= FAT32; 911553Srgrimes if (boot->flags & FAT32) { 9229448Scharnier boot->FATsecs = block[36] + (block[37] << 8) 9317797Smpp + (block[38] << 16) + (block[39] << 24); 9417797Smpp if (block[40] & 0x80) 95 boot->ValidFat = block[40] & 0x0f; 96 97 /* check version number: */ 98 if (block[42] || block[43]) { 99 /* Correct? XXX */ 100 pfatal("Unknown file system version: %x.%x", 101 block[43], block[42]); 102 return FSFATAL; 103 } 104 boot->RootCl = block[44] + (block[45] << 8) 105 + (block[46] << 16) + (block[47] << 24); 106 boot->FSInfo = block[48] + (block[49] << 8); 107 boot->Backup = block[50] + (block[51] << 8); 108 109 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 110 != boot->FSInfo * boot->BytesPerSec 111 || read(dosfs, fsinfo, sizeof fsinfo) 112 != sizeof fsinfo) { 113 perror("could not read fsinfo block"); 114 return FSFATAL; 115 } 116 if (memcmp(fsinfo, "RRaA", 4) 117 || memcmp(fsinfo + 0x1e4, "rrAa", 4) 118 || fsinfo[0x1fc] 119 || fsinfo[0x1fd] 120 || fsinfo[0x1fe] != 0x55 121 || fsinfo[0x1ff] != 0xaa 122 || fsinfo[0x3fc] 123 || fsinfo[0x3fd] 124 || fsinfo[0x3fe] != 0x55 125 || fsinfo[0x3ff] != 0xaa) { 126 pwarn("Invalid signature in fsinfo block, "); 127 if (ask(0, "fix")) { 128 memcpy(fsinfo, "RRaA", 4); 129 memcpy(fsinfo + 0x1e4, "rrAa", 4); 130 fsinfo[0x1fc] = fsinfo[0x1fd] = 0; 131 fsinfo[0x1fe] = 0x55; 132 fsinfo[0x1ff] = 0xaa; 133 fsinfo[0x3fc] = fsinfo[0x3fd] = 0; 134 fsinfo[0x3fe] = 0x55; 135 fsinfo[0x3ff] = 0xaa; 136 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 137 != boot->FSInfo * boot->BytesPerSec 138 || write(dosfs, fsinfo, sizeof fsinfo) 139 != sizeof fsinfo) { 140 perror("Unable to write FSInfo"); 141 return FSFATAL; 142 } 143 ret = FSBOOTMOD; 144 } else 145 boot->FSInfo = 0; 146 } 147 if (boot->FSInfo) { 148 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8) 149 + (fsinfo[0x1ea] << 16) 150 + (fsinfo[0x1eb] << 24); 151 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8) 152 + (fsinfo[0x1ee] << 16) 153 + (fsinfo[0x1ef] << 24); 154 } 155 156 if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET) 157 != boot->Backup * boot->BytesPerSec 158 || read(dosfs, backup, sizeof backup) != sizeof backup) { 159 perror("could not read backup bootblock"); 160 return FSFATAL; 161 } 162 backup[65] = block[65]; /* XXX */ 163 if (memcmp(block + 11, backup + 11, 79)) { 164 /* Correct? XXX */ 165 pfatal("backup doesn't compare to primary bootblock"); 166 if (alwaysno) 167 pfatal("\n"); 168 else 169 return FSFATAL; 170 } 171 /* Check backup FSInfo? XXX */ 172 } 173 174 boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1) 175 / boot->BytesPerSec 176 + boot->ResSectors 177 + boot->FATs * boot->FATsecs 178 - CLUST_FIRST * boot->SecPerClust; 179 180 if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) { 181 pfatal("Invalid sector size: %u", boot->BytesPerSec); 182 return FSFATAL; 183 } 184 if (boot->SecPerClust == 0) { 185 pfatal("Invalid cluster size: %u", boot->SecPerClust); 186 return FSFATAL; 187 } 188 if (boot->Sectors) { 189 boot->HugeSectors = 0; 190 boot->NumSectors = boot->Sectors; 191 } else 192 boot->NumSectors = boot->HugeSectors; 193 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust; 194 195 if (boot->flags&FAT32) 196 boot->ClustMask = CLUST32_MASK; 197 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) 198 boot->ClustMask = CLUST12_MASK; 199 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) 200 boot->ClustMask = CLUST16_MASK; 201 else { 202 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition", 203 boot->NumClusters); 204 return FSFATAL; 205 } 206 207 switch (boot->ClustMask) { 208 case CLUST32_MASK: 209 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4; 210 break; 211 case CLUST16_MASK: 212 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2; 213 break; 214 default: 215 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3; 216 break; 217 } 218 219 if (boot->NumFatEntries < boot->NumClusters) { 220 pfatal("FAT size too small, %u entries won't fit into %u sectors\n", 221 boot->NumClusters, boot->FATsecs); 222 return FSFATAL; 223 } 224 boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust; 225 226 boot->NumFiles = 1; 227 boot->NumFree = 0; 228 229 return ret; 230} 231 232int 233writefsinfo(dosfs, boot) 234 int dosfs; 235 struct bootblock *boot; 236{ 237 u_char fsinfo[2 * DOSBOOTBLOCKSIZE]; 238 239 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 240 != boot->FSInfo * boot->BytesPerSec 241 || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) { 242 perror("could not read fsinfo block"); 243 return FSFATAL; 244 } 245 fsinfo[0x1e8] = (u_char)boot->FSFree; 246 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8); 247 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16); 248 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24); 249 fsinfo[0x1ec] = (u_char)boot->FSNext; 250 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8); 251 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16); 252 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24); 253 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET) 254 != boot->FSInfo * boot->BytesPerSec 255 || write(dosfs, fsinfo, sizeof fsinfo) 256 != sizeof fsinfo) { 257 perror("Unable to write FSInfo"); 258 return FSFATAL; 259 } 260 /* 261 * Technically, we should return FSBOOTMOD here. 262 * 263 * However, since Win95 OSR2 (the first M$ OS that has 264 * support for FAT32) doesn't maintain the FSINFO block 265 * correctly, it has to be fixed pretty often. 266 * 267 * Therefor, we handle the FSINFO block only informally, 268 * fixing it if necessary, but otherwise ignoring the 269 * fact that it was incorrect. 270 */ 271 return 0; 272} 273