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