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