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