1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999,2000 by Sun Microsystems, Inc. 24*7c478bd9Sstevel@tonic-gate * All rights reserved. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * fsck_pcfs -- routines for manipulating the BPB (BIOS parameter block) 31*7c478bd9Sstevel@tonic-gate * of the file system. 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 35*7c478bd9Sstevel@tonic-gate #include <string.h> 36*7c478bd9Sstevel@tonic-gate #include <unistd.h> 37*7c478bd9Sstevel@tonic-gate #include <libintl.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/fs/pc_fs.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/fs/pc_dir.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/fs/pc_label.h> 43*7c478bd9Sstevel@tonic-gate #include "pcfs_common.h" 44*7c478bd9Sstevel@tonic-gate #include "fsck_pcfs.h" 45*7c478bd9Sstevel@tonic-gate #include "pcfs_bpb.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate extern off64_t FirstClusterOffset; 48*7c478bd9Sstevel@tonic-gate extern off64_t PartitionOffset; 49*7c478bd9Sstevel@tonic-gate extern int32_t BytesPerCluster; 50*7c478bd9Sstevel@tonic-gate extern int32_t TotalClusters; 51*7c478bd9Sstevel@tonic-gate extern int32_t LastCluster; 52*7c478bd9Sstevel@tonic-gate extern int32_t RootDirSize; 53*7c478bd9Sstevel@tonic-gate extern int32_t FATSize; 54*7c478bd9Sstevel@tonic-gate extern short FATEntrySize; 55*7c478bd9Sstevel@tonic-gate extern bpb_t TheBIOSParameterBlock; 56*7c478bd9Sstevel@tonic-gate extern int IsFAT32; 57*7c478bd9Sstevel@tonic-gate extern int Verbose; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static void 60*7c478bd9Sstevel@tonic-gate computeFileAreaSize(void) 61*7c478bd9Sstevel@tonic-gate { 62*7c478bd9Sstevel@tonic-gate int32_t dataSectors; 63*7c478bd9Sstevel@tonic-gate int32_t overhead; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate /* 66*7c478bd9Sstevel@tonic-gate * Compute bytes/cluster for later reference 67*7c478bd9Sstevel@tonic-gate */ 68*7c478bd9Sstevel@tonic-gate BytesPerCluster = TheBIOSParameterBlock.bpb.sectors_per_cluster * 69*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector; 70*7c478bd9Sstevel@tonic-gate 71*7c478bd9Sstevel@tonic-gate /* 72*7c478bd9Sstevel@tonic-gate * First we'll find total number of sectors in the file area... 73*7c478bd9Sstevel@tonic-gate */ 74*7c478bd9Sstevel@tonic-gate if (TheBIOSParameterBlock.bpb.sectors_in_volume > 0) 75*7c478bd9Sstevel@tonic-gate dataSectors = TheBIOSParameterBlock.bpb.sectors_in_volume; 76*7c478bd9Sstevel@tonic-gate else 77*7c478bd9Sstevel@tonic-gate dataSectors = 78*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.sectors_in_logical_volume; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate overhead = TheBIOSParameterBlock.bpb.resv_sectors; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate RootDirSize = TheBIOSParameterBlock.bpb.num_root_entries * 83*7c478bd9Sstevel@tonic-gate sizeof (struct pcdir); 84*7c478bd9Sstevel@tonic-gate overhead += RootDirSize / TheBIOSParameterBlock.bpb.bytes_per_sector; 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate if (TheBIOSParameterBlock.bpb.sectors_per_fat) { 87*7c478bd9Sstevel@tonic-gate /* 88*7c478bd9Sstevel@tonic-gate * Good old FAT12 or FAT16 89*7c478bd9Sstevel@tonic-gate */ 90*7c478bd9Sstevel@tonic-gate overhead += TheBIOSParameterBlock.bpb.num_fats * 91*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.sectors_per_fat; 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * Compute this for later - when we actually pull in a copy 94*7c478bd9Sstevel@tonic-gate * of the FAT 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate FATSize = TheBIOSParameterBlock.bpb.sectors_per_fat * 97*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector; 98*7c478bd9Sstevel@tonic-gate } else { 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * FAT32 101*7c478bd9Sstevel@tonic-gate * I'm unsure if this is always going to work. At one 102*7c478bd9Sstevel@tonic-gate * point during the creation of this program and mkfs_pcfs 103*7c478bd9Sstevel@tonic-gate * it seemed that Windows had created an fs where it had 104*7c478bd9Sstevel@tonic-gate * rounded big_sectors_per_fat up to a cluster boundary. 105*7c478bd9Sstevel@tonic-gate * Later, though, I encountered a problem where I wasn't 106*7c478bd9Sstevel@tonic-gate * finding the root directory because I was looking in the 107*7c478bd9Sstevel@tonic-gate * wrong place by doing that same roundup. So, for now, 108*7c478bd9Sstevel@tonic-gate * I'm backing off on the cluster boundary thing and just 109*7c478bd9Sstevel@tonic-gate * believing what I am told. 110*7c478bd9Sstevel@tonic-gate */ 111*7c478bd9Sstevel@tonic-gate overhead += TheBIOSParameterBlock.bpb.num_fats * 112*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb32.big_sectors_per_fat; 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * Compute this for later - when we actually pull in a copy 115*7c478bd9Sstevel@tonic-gate * of the FAT 116*7c478bd9Sstevel@tonic-gate */ 117*7c478bd9Sstevel@tonic-gate FATSize = TheBIOSParameterBlock.bpb32.big_sectors_per_fat * 118*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector; 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate /* 122*7c478bd9Sstevel@tonic-gate * Now change sectors to clusters. The computed value for 123*7c478bd9Sstevel@tonic-gate * TotalClusters is persistent for the remainder of execution. 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate dataSectors -= overhead; 126*7c478bd9Sstevel@tonic-gate TotalClusters = dataSectors / 127*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.sectors_per_cluster; 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate /* 130*7c478bd9Sstevel@tonic-gate * Also need to compute last cluster and offset of the first cluster 131*7c478bd9Sstevel@tonic-gate */ 132*7c478bd9Sstevel@tonic-gate LastCluster = TotalClusters + FIRST_CLUSTER; 133*7c478bd9Sstevel@tonic-gate FirstClusterOffset = overhead * 134*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector; 135*7c478bd9Sstevel@tonic-gate FirstClusterOffset += PartitionOffset; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* 138*7c478bd9Sstevel@tonic-gate * XXX this should probably be more sophisticated 139*7c478bd9Sstevel@tonic-gate */ 140*7c478bd9Sstevel@tonic-gate if (IsFAT32) 141*7c478bd9Sstevel@tonic-gate FATEntrySize = 32; 142*7c478bd9Sstevel@tonic-gate else { 143*7c478bd9Sstevel@tonic-gate if (TotalClusters <= DOS_F12MAXC) 144*7c478bd9Sstevel@tonic-gate FATEntrySize = 12; 145*7c478bd9Sstevel@tonic-gate else 146*7c478bd9Sstevel@tonic-gate FATEntrySize = 16; 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate if (Verbose) { 150*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 151*7c478bd9Sstevel@tonic-gate gettext("Disk has a file area of %d " 152*7c478bd9Sstevel@tonic-gate "allocation units,\neach with %d sectors = %llu " 153*7c478bd9Sstevel@tonic-gate "bytes.\n"), TotalClusters, 154*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.sectors_per_cluster, 155*7c478bd9Sstevel@tonic-gate (uint64_t)TotalClusters * 156*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.sectors_per_cluster * 157*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector); 158*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 159*7c478bd9Sstevel@tonic-gate gettext("File system overhead of %d sectors.\n"), overhead); 160*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 161*7c478bd9Sstevel@tonic-gate gettext("The last cluster is %d\n"), LastCluster); 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate /* 166*7c478bd9Sstevel@tonic-gate * XXX - right now we aren't attempting to fix anything that looks bad, 167*7c478bd9Sstevel@tonic-gate * instead we just give up. 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate void 170*7c478bd9Sstevel@tonic-gate readBPB(int fd) 171*7c478bd9Sstevel@tonic-gate { 172*7c478bd9Sstevel@tonic-gate boot_sector_t ubpb; 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate /* 175*7c478bd9Sstevel@tonic-gate * The BPB is the first sector of the file system 176*7c478bd9Sstevel@tonic-gate */ 177*7c478bd9Sstevel@tonic-gate if (lseek64(fd, PartitionOffset, SEEK_SET) < 0) { 178*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 179*7c478bd9Sstevel@tonic-gate perror(gettext("Cannot seek to start of disk partition")); 180*7c478bd9Sstevel@tonic-gate (void) close(fd); 181*7c478bd9Sstevel@tonic-gate exit(7); 182*7c478bd9Sstevel@tonic-gate } 183*7c478bd9Sstevel@tonic-gate if (Verbose) 184*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 185*7c478bd9Sstevel@tonic-gate gettext("Reading BIOS parameter block\n")); 186*7c478bd9Sstevel@tonic-gate if (read(fd, ubpb.buf, BPSEC) < BPSEC) { 187*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 188*7c478bd9Sstevel@tonic-gate perror(gettext("Read BIOS parameter block")); 189*7c478bd9Sstevel@tonic-gate (void) close(fd); 190*7c478bd9Sstevel@tonic-gate exit(2); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate if (ltohs(ubpb.mb.signature) != BOOTSECSIG) { 194*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 195*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 196*7c478bd9Sstevel@tonic-gate gettext("Bad signature on BPB. Giving up.\n")); 197*7c478bd9Sstevel@tonic-gate exit(2); 198*7c478bd9Sstevel@tonic-gate } 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN 201*7c478bd9Sstevel@tonic-gate swap_pack_grabbpb(&TheBIOSParameterBlock, &(ubpb.bs)); 202*7c478bd9Sstevel@tonic-gate #else 203*7c478bd9Sstevel@tonic-gate (void) memcpy(&(TheBIOSParameterBlock.bpb), &(ubpb.bs.bs_front.bs_bpb), 204*7c478bd9Sstevel@tonic-gate sizeof (TheBIOSParameterBlock.bpb)); 205*7c478bd9Sstevel@tonic-gate (void) memcpy(&(TheBIOSParameterBlock.ebpb), &(ubpb.bs.bs_ebpb), 206*7c478bd9Sstevel@tonic-gate sizeof (TheBIOSParameterBlock.ebpb)); 207*7c478bd9Sstevel@tonic-gate #endif 208*7c478bd9Sstevel@tonic-gate /* 209*7c478bd9Sstevel@tonic-gate * In general, we would expect the bytes per sector to 210*7c478bd9Sstevel@tonic-gate * equal 256 (BPSEC). I personally have yet to see a file 211*7c478bd9Sstevel@tonic-gate * system where this isn't true but apparently some weird media 212*7c478bd9Sstevel@tonic-gate * have different sector sizes. So we'll accept a couple of 213*7c478bd9Sstevel@tonic-gate * other small multiples of 256 as valid sizes. 214*7c478bd9Sstevel@tonic-gate */ 215*7c478bd9Sstevel@tonic-gate if (TheBIOSParameterBlock.bpb.bytes_per_sector != BPSEC && 216*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector != 2 * BPSEC && 217*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector != 4 * BPSEC) { 218*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 219*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 220*7c478bd9Sstevel@tonic-gate gettext("Bogus bytes per sector value. Giving up.\n")); 221*7c478bd9Sstevel@tonic-gate exit(2); 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate if (!(is_z_a_power_of_x_le_y(2, 128, 224*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.sectors_per_cluster))) { 225*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 226*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 227*7c478bd9Sstevel@tonic-gate gettext("Bogus sectors per cluster value. Giving up.\n")); 228*7c478bd9Sstevel@tonic-gate (void) close(fd); 229*7c478bd9Sstevel@tonic-gate exit(6); 230*7c478bd9Sstevel@tonic-gate } 231*7c478bd9Sstevel@tonic-gate if (TheBIOSParameterBlock.bpb.sectors_per_fat == 0) { 232*7c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN 233*7c478bd9Sstevel@tonic-gate swap_pack_grab32bpb(&TheBIOSParameterBlock, &(ubpb.bs)); 234*7c478bd9Sstevel@tonic-gate #else 235*7c478bd9Sstevel@tonic-gate (void) memcpy(&(TheBIOSParameterBlock.bpb32), 236*7c478bd9Sstevel@tonic-gate &(ubpb.bs32.bs_bpb32), 237*7c478bd9Sstevel@tonic-gate sizeof (TheBIOSParameterBlock.bpb32)); 238*7c478bd9Sstevel@tonic-gate #endif 239*7c478bd9Sstevel@tonic-gate IsFAT32 = 1; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate if (!IsFAT32) { 242*7c478bd9Sstevel@tonic-gate if ((TheBIOSParameterBlock.bpb.num_root_entries == 0) || 243*7c478bd9Sstevel@tonic-gate ((TheBIOSParameterBlock.bpb.num_root_entries * 244*7c478bd9Sstevel@tonic-gate sizeof (struct pcdir)) % 245*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.bytes_per_sector) != 0) { 246*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 247*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 248*7c478bd9Sstevel@tonic-gate gettext("Bogus number of root entries. " 249*7c478bd9Sstevel@tonic-gate "Giving up.\n")); 250*7c478bd9Sstevel@tonic-gate exit(2); 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate } else { 253*7c478bd9Sstevel@tonic-gate if (TheBIOSParameterBlock.bpb.num_root_entries != 0) { 254*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 255*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 256*7c478bd9Sstevel@tonic-gate gettext("Bogus number of root entries. " 257*7c478bd9Sstevel@tonic-gate "Giving up.\n")); 258*7c478bd9Sstevel@tonic-gate exit(2); 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate } 261*7c478bd9Sstevel@tonic-gate /* 262*7c478bd9Sstevel@tonic-gate * In general, we would expect the number of FATs field to 263*7c478bd9Sstevel@tonic-gate * equal 2. Our mkfs and Windows have this as a default 264*7c478bd9Sstevel@tonic-gate * value. I suppose someone could override the default, 265*7c478bd9Sstevel@tonic-gate * though, so we'll sort of arbitrarily accept any number 266*7c478bd9Sstevel@tonic-gate * between 1 and 4 inclusive as reasonable values. 267*7c478bd9Sstevel@tonic-gate * 268*7c478bd9Sstevel@tonic-gate * XXX: Warn, but continue, if value is suspicious? (>2?) 269*7c478bd9Sstevel@tonic-gate */ 270*7c478bd9Sstevel@tonic-gate if (TheBIOSParameterBlock.bpb.num_fats > 4 || 271*7c478bd9Sstevel@tonic-gate TheBIOSParameterBlock.bpb.num_fats < 1) { 272*7c478bd9Sstevel@tonic-gate mountSanityCheckFails(); 273*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 274*7c478bd9Sstevel@tonic-gate gettext("Bogus number of FATs. Giving up.\n")); 275*7c478bd9Sstevel@tonic-gate exit(2); 276*7c478bd9Sstevel@tonic-gate } 277*7c478bd9Sstevel@tonic-gate computeFileAreaSize(); 278*7c478bd9Sstevel@tonic-gate } 279