1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
28
29 /*
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
32 */
33
34 #pragma ident "%Z%%M% %I% %E% SMI"
35
36 /*
37 * libfstyp module for pcfs
38 */
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/mntent.h>
42 #include <errno.h>
43 #include <sys/fs/pc_fs.h>
44 #include <sys/fs/pc_label.h>
45 #include <sys/fs/pc_dir.h>
46 #include <sys/stat.h>
47 #include <sys/vfs.h>
48 #include <stdio.h>
49 #include <unistd.h>
50 #include <string.h>
51 #include <strings.h>
52 #include <sys/mnttab.h>
53 #include <locale.h>
54 #include <libfstyp_module.h>
55
56 #define PC_LABEL_SIZE 11
57
58 /* for the <sys/fs/pc_dir.h> PCDL_IS_LFN macro */
59 int enable_long_filenames = 1;
60
61 struct fstyp_fat16_bs {
62 uint8_t f_drvnum;
63 uint8_t f_reserved1;
64 uint8_t f_bootsig;
65 uint8_t f_volid[4];
66 uint8_t f_label[11];
67 uint8_t f_typestring[8];
68 };
69
70 struct fstyp_fat32_bs {
71 uint32_t f_fatlength;
72 uint16_t f_flags;
73 uint8_t f_major;
74 uint8_t f_minor;
75 uint32_t f_rootcluster;
76 uint16_t f_infosector;
77 uint16_t f_backupboot;
78 uint8_t f_reserved2[12];
79 uint8_t f_drvnum;
80 uint8_t f_reserved1;
81 uint8_t f_bootsig;
82 uint8_t f_volid[4];
83 uint8_t f_label[11];
84 uint8_t f_typestring[8];
85 };
86
87 typedef struct fstyp_pcfs {
88 int fd;
89 off_t offset;
90 nvlist_t *attr;
91 struct bootsec bs;
92 struct fstyp_fat16_bs bs16;
93 struct fstyp_fat32_bs bs32;
94 ushort_t bps;
95 int fattype;
96 char volume_label[PC_LABEL_SIZE + 1];
97
98 /* parameters derived or calculated per FAT spec */
99 ulong_t FATSz;
100 ulong_t TotSec;
101 ulong_t RootDirSectors;
102 ulong_t FirstDataSector;
103 ulong_t DataSec;
104 ulong_t CountOfClusters;
105 } fstyp_pcfs_t;
106
107
108 /* We should eventually make the structs "packed" so these won't be needed */
109 #define PC_BPSEC(h) ltohs((h)->bs.bps[0])
110 #define PC_RESSEC(h) ltohs((h)->bs.res_sec[0])
111 #define PC_NROOTENT(h) ltohs((h)->bs.rdirents[0])
112 #define PC_NSEC(h) ltohs((h)->bs.numsect[0])
113 #define PC_DRVNUM(h) (FSTYP_IS_32(h) ? (h)->bs32.f_drvnum : \
114 (h)->bs16.f_drvnum)
115 #define PC_VOLID(a) (FSTYP_IS_32(h) ? ltohi((h)->bs32.f_volid[0]) : \
116 ltohi((h)->bs16.f_volid[0]))
117 #define PC_LABEL_ADDR(a) (FSTYP_IS_32(h) ? \
118 &((h)->bs32.f_label[0]) : &((h)->bs16.f_label[0]))
119
120 #define FSTYP_IS_32(h) ((h)->fattype == 32)
121
122 #define FSTYP_MAX_CLUSTER_SIZE (64 * 1024) /* though officially 32K */
123 #define FSTYP_MAX_DIR_SIZE (65536 * 32)
124
125 static int read_bootsec(fstyp_pcfs_t *h);
126 static int valid_media(fstyp_pcfs_t *h);
127 static int well_formed(fstyp_pcfs_t *h);
128 static void calculate_parameters(fstyp_pcfs_t *h);
129 static void determine_fattype(fstyp_pcfs_t *h);
130 static void get_label(fstyp_pcfs_t *h);
131 static void get_label_16(fstyp_pcfs_t *h);
132 static void get_label_32(fstyp_pcfs_t *h);
133 static int next_cluster_32(fstyp_pcfs_t *h, int n);
134 static boolean_t dir_find_label(fstyp_pcfs_t *h, struct pcdir *d, int nent);
135 static int is_pcfs(fstyp_pcfs_t *h);
136 static int dumpfs(fstyp_pcfs_t *h, FILE *fout, FILE *ferr);
137 static int get_attr(fstyp_pcfs_t *h);
138
139 int fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle);
140 void fstyp_mod_fini(fstyp_mod_handle_t handle);
141 int fstyp_mod_ident(fstyp_mod_handle_t handle);
142 int fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp);
143 int fstyp_mod_dump(fstyp_mod_handle_t handle, FILE *fout, FILE *ferr);
144
145 int
fstyp_mod_init(int fd,off_t offset,fstyp_mod_handle_t * handle)146 fstyp_mod_init(int fd, off_t offset, fstyp_mod_handle_t *handle)
147 {
148 struct fstyp_pcfs *h;
149
150 if ((h = calloc(1, sizeof (struct fstyp_pcfs))) == NULL) {
151 return (FSTYP_ERR_NOMEM);
152 }
153 h->fd = fd;
154 h->offset = offset;
155
156 *handle = (fstyp_mod_handle_t)h;
157 return (0);
158 }
159
160 void
fstyp_mod_fini(fstyp_mod_handle_t handle)161 fstyp_mod_fini(fstyp_mod_handle_t handle)
162 {
163 struct fstyp_pcfs *h = (struct fstyp_pcfs *)handle;
164
165 if (h->attr == NULL) {
166 nvlist_free(h->attr);
167 h->attr = NULL;
168 }
169 free(h);
170 }
171
172 int
fstyp_mod_ident(fstyp_mod_handle_t handle)173 fstyp_mod_ident(fstyp_mod_handle_t handle)
174 {
175 struct fstyp_pcfs *h = (struct fstyp_pcfs *)handle;
176
177 return (is_pcfs(h));
178 }
179
180 int
fstyp_mod_get_attr(fstyp_mod_handle_t handle,nvlist_t ** attrp)181 fstyp_mod_get_attr(fstyp_mod_handle_t handle, nvlist_t **attrp)
182 {
183 struct fstyp_pcfs *h = (struct fstyp_pcfs *)handle;
184 int error;
185
186 if (h->attr == NULL) {
187 if (nvlist_alloc(&h->attr, NV_UNIQUE_NAME_TYPE, 0)) {
188 return (FSTYP_ERR_NOMEM);
189 }
190 if ((error = get_attr(h)) != 0) {
191 nvlist_free(h->attr);
192 h->attr = NULL;
193 return (error);
194 }
195 }
196
197 *attrp = h->attr;
198 return (0);
199 }
200
201 int
fstyp_mod_dump(fstyp_mod_handle_t handle,FILE * fout,FILE * ferr)202 fstyp_mod_dump(fstyp_mod_handle_t handle, FILE *fout, FILE *ferr)
203 {
204 struct fstyp_pcfs *h = (struct fstyp_pcfs *)handle;
205
206 return (dumpfs(h, fout, ferr));
207 }
208
209
210 /*
211 * Read in boot sector. Convert into host endianness where possible.
212 */
213 static int
read_bootsec(fstyp_pcfs_t * h)214 read_bootsec(fstyp_pcfs_t *h)
215 {
216 char buf[PC_SECSIZE];
217
218 (void) lseek(h->fd, h->offset, SEEK_SET);
219 if (read(h->fd, buf, sizeof (buf)) != sizeof (buf)) {
220 return (FSTYP_ERR_IO);
221 }
222
223 bcopy(buf, &h->bs, sizeof (h->bs));
224 bcopy(buf + sizeof (struct bootsec), &h->bs16, sizeof (h->bs16));
225 bcopy(buf + sizeof (struct bootsec), &h->bs32, sizeof (h->bs32));
226
227 h->bs.fatsec = ltohs(h->bs.fatsec);
228 h->bs.spt = ltohs(h->bs.spt);
229 h->bs.nhead = ltohs(h->bs.nhead);
230 h->bs.hiddensec = ltohi(h->bs.hiddensec);
231 h->bs.totalsec = ltohi(h->bs.totalsec);
232
233 h->bs32.f_fatlength = ltohi(h->bs32.f_fatlength);
234 h->bs32.f_flags = ltohs(h->bs32.f_flags);
235 h->bs32.f_rootcluster = ltohi(h->bs32.f_rootcluster);
236 h->bs32.f_infosector = ltohs(h->bs32.f_infosector);
237 h->bs32.f_backupboot = ltohs(h->bs32.f_backupboot);
238
239 h->bps = PC_BPSEC(h);
240
241 return (0);
242 }
243
244 static int
valid_media(fstyp_pcfs_t * h)245 valid_media(fstyp_pcfs_t *h)
246 {
247 switch (h->bs.mediadesriptor) {
248 case MD_FIXED:
249 case SS8SPT:
250 case DS8SPT:
251 case SS9SPT:
252 case DS9SPT:
253 case DS18SPT:
254 case DS9_15SPT:
255 return (1);
256 default:
257 return (0);
258 }
259 }
260
261 static int
well_formed(fstyp_pcfs_t * h)262 well_formed(fstyp_pcfs_t *h)
263 {
264 int fatmatch;
265
266 if (h->bs16.f_bootsig == 0x29) {
267 fatmatch = ((h->bs16.f_typestring[0] == 'F' &&
268 h->bs16.f_typestring[1] == 'A' &&
269 h->bs16.f_typestring[2] == 'T') &&
270 (h->bs.fatsec > 0) &&
271 ((PC_NSEC(h) == 0 && h->bs.totalsec > 0) ||
272 PC_NSEC(h) > 0));
273 } else if (h->bs32.f_bootsig == 0x29) {
274 fatmatch = ((h->bs32.f_typestring[0] == 'F' &&
275 h->bs32.f_typestring[1] == 'A' &&
276 h->bs32.f_typestring[2] == 'T') &&
277 (h->bs.fatsec == 0 && h->bs32.f_fatlength > 0) &&
278 ((PC_NSEC(h) == 0 && h->bs.totalsec > 0) ||
279 PC_NSEC(h) > 0));
280 } else {
281 fatmatch = (PC_NSEC(h) > 0 && h->bs.fatsec > 0);
282 }
283
284 return (fatmatch && h->bps > 0 && h->bps % 512 == 0 &&
285 h->bs.spcl > 0 && PC_RESSEC(h) >= 1 && h->bs.nfat > 0);
286 }
287
288 static void
calculate_parameters(fstyp_pcfs_t * h)289 calculate_parameters(fstyp_pcfs_t *h)
290 {
291 if (PC_NSEC(h) != 0) {
292 h->TotSec = PC_NSEC(h);
293 } else {
294 h->TotSec = h->bs.totalsec;
295 }
296 if (h->bs.fatsec != 0) {
297 h->FATSz = h->bs.fatsec;
298 } else {
299 h->FATSz = h->bs32.f_fatlength;
300 }
301 if ((h->bps == 0) || (h->bs.spcl == 0)) {
302 return;
303 }
304 h->RootDirSectors =
305 ((PC_NROOTENT(h) * 32) + (h->bps - 1)) / h->bps;
306 h->FirstDataSector =
307 PC_RESSEC(h) + h->bs.nfat * h->FATSz + h->RootDirSectors;
308 h->DataSec = h->TotSec - h->FirstDataSector;
309 h->CountOfClusters = h->DataSec / h->bs.spcl;
310 }
311
312 static void
determine_fattype(fstyp_pcfs_t * h)313 determine_fattype(fstyp_pcfs_t *h)
314 {
315 if ((h->CountOfClusters >= 4085 && h->CountOfClusters <= 4095) ||
316 (h->CountOfClusters >= 65525 && h->CountOfClusters <= 65535)) {
317 h->fattype = 0;
318 } else if (h->CountOfClusters < 4085) {
319 h->fattype = 12;
320 } else if (h->CountOfClusters < 65525) {
321 h->fattype = 16;
322 } else {
323 h->fattype = 32;
324 }
325 }
326
327 static void
get_label(fstyp_pcfs_t * h)328 get_label(fstyp_pcfs_t *h)
329 {
330 /*
331 * Use label from the boot sector by default.
332 * Can overwrite later with the one from root directory.
333 */
334 (void) memcpy(h->volume_label, PC_LABEL_ADDR(h), PC_LABEL_SIZE);
335 h->volume_label[PC_LABEL_SIZE] = '\0';
336
337 if (h->fattype == 0) {
338 return;
339 } else if (FSTYP_IS_32(h)) {
340 get_label_32(h);
341 } else {
342 get_label_16(h);
343 }
344 }
345
346 /*
347 * Get volume label from the root directory entry.
348 * In FAT12/16 the root directory is of fixed size.
349 * It immediately follows the FATs
350 */
351 static void
get_label_16(fstyp_pcfs_t * h)352 get_label_16(fstyp_pcfs_t *h)
353 {
354 ulong_t FirstRootDirSecNum;
355 int secsize;
356 off_t offset;
357 uint8_t buf[PC_SECSIZE * 4];
358 int i;
359 int nent, resid;
360
361 if ((secsize = h->bps) > sizeof (buf)) {
362 return;
363 }
364
365 FirstRootDirSecNum = PC_RESSEC(h) + h->bs.nfat * h->bs.fatsec;
366 offset = h->offset + FirstRootDirSecNum * secsize;
367 resid = PC_NROOTENT(h);
368
369 for (i = 0; i < h->RootDirSectors; i++) {
370 (void) lseek(h->fd, offset, SEEK_SET);
371 if (read(h->fd, buf, secsize) != secsize) {
372 return;
373 }
374
375 nent = secsize / sizeof (struct pcdir);
376 if (nent > resid) {
377 nent = resid;
378 }
379 if (dir_find_label(h, (struct pcdir *)buf, nent)) {
380 return;
381 }
382
383 resid -= nent;
384 offset += PC_SECSIZE;
385 }
386 }
387
388 /*
389 * Get volume label from the root directory entry.
390 * In FAT32 root is a usual directory, a cluster chain.
391 * It starts at BPB_RootClus.
392 */
393 static void
get_label_32(fstyp_pcfs_t * h)394 get_label_32(fstyp_pcfs_t *h)
395 {
396 off_t offset;
397 int clustersize;
398 int n;
399 ulong_t FirstSectorofCluster;
400 uint8_t *buf;
401 int nent;
402 int cnt = 0;
403
404 clustersize = h->bs.spcl * h->bps;
405 if ((clustersize == 0) || (clustersize > FSTYP_MAX_CLUSTER_SIZE) ||
406 ((buf = calloc(1, clustersize)) == NULL)) {
407 return;
408 }
409
410 for (n = h->bs32.f_rootcluster; n != 0; n = next_cluster_32(h, n)) {
411 FirstSectorofCluster =
412 (n - 2) * h->bs.spcl + h->FirstDataSector;
413 offset = h->offset + FirstSectorofCluster * h->bps;
414 (void) lseek(h->fd, offset, SEEK_SET);
415 if (read(h->fd, buf, clustersize) != clustersize) {
416 break;
417 }
418
419 nent = clustersize / sizeof (struct pcdir);
420 if (dir_find_label(h, (struct pcdir *)buf, nent)) {
421 break;
422 }
423
424 if (++cnt > FSTYP_MAX_DIR_SIZE / clustersize) {
425 break;
426 }
427 }
428
429 free(buf);
430 }
431
432 /*
433 * Get a FAT entry pointing to the next file cluster
434 */
435 int
next_cluster_32(fstyp_pcfs_t * h,int n)436 next_cluster_32(fstyp_pcfs_t *h, int n)
437 {
438 uint8_t buf[PC_SECSIZE];
439 ulong_t ThisFATSecNum;
440 ulong_t ThisFATEntOffset;
441 off_t offset;
442 uint32_t val;
443 int next = 0;
444
445 ThisFATSecNum = PC_RESSEC(h) + (n * 4) / h->bps;
446 ThisFATEntOffset = (n * 4) % h->bps;
447 offset = h->offset + ThisFATSecNum * h->bps;
448
449 (void) lseek(h->fd, offset, SEEK_SET);
450 if (read(h->fd, buf, sizeof (buf)) == sizeof (buf)) {
451 val = buf[ThisFATEntOffset] & 0x0fffffff;
452 next = ltohi(val);
453 }
454
455 return (next);
456 }
457
458 /*
459 * Given an array of pcdir structs, find one containing volume label.
460 */
461 static boolean_t
dir_find_label(fstyp_pcfs_t * h,struct pcdir * d,int nent)462 dir_find_label(fstyp_pcfs_t *h, struct pcdir *d, int nent)
463 {
464 int i;
465
466 for (i = 0; i < nent; i++, d++) {
467 if (PCDL_IS_LFN(d))
468 continue;
469 if ((d->pcd_filename[0] != PCD_UNUSED) &&
470 (d->pcd_filename[0] != PCD_ERASED) &&
471 ((d->pcd_attr & (PCA_LABEL | PCA_DIR)) == PCA_LABEL) &&
472 (d->un.pcd_scluster_hi == 0) &&
473 (d->pcd_scluster_lo == 0)) {
474 (void) memcpy(h->volume_label, d->pcd_filename,
475 PC_LABEL_SIZE);
476 h->volume_label[PC_LABEL_SIZE] = '\0';
477 return (B_TRUE);
478 }
479 }
480 return (B_FALSE);
481 }
482
483 static int
is_pcfs(fstyp_pcfs_t * h)484 is_pcfs(fstyp_pcfs_t *h)
485 {
486 int error;
487
488 if ((error = read_bootsec(h)) != 0) {
489 return (error);
490 }
491 if (!valid_media(h)) {
492 return (FSTYP_ERR_NO_MATCH);
493 }
494 if (!well_formed(h)) {
495 return (FSTYP_ERR_NO_MATCH);
496 }
497
498 calculate_parameters(h);
499 determine_fattype(h);
500 get_label(h);
501
502 return (0);
503 }
504
505 /* ARGSUSED */
506 static int
dumpfs(fstyp_pcfs_t * h,FILE * fout,FILE * ferr)507 dumpfs(fstyp_pcfs_t *h, FILE *fout, FILE *ferr)
508 {
509 (void) fprintf(fout,
510 "Bytes Per Sector %d\t\tSectors Per Cluster %d\n",
511 h->bps, h->bs.spcl);
512 (void) fprintf(fout,
513 "Reserved Sectors %d\t\tNumber of FATs %d\n",
514 (unsigned short)PC_RESSEC(h), h->bs.nfat);
515 (void) fprintf(fout,
516 "Root Dir Entries %d\t\tNumber of Sectors %d\n",
517 (unsigned short)PC_NROOTENT(h), (unsigned short)PC_NSEC(h));
518 (void) fprintf(fout,
519 "Sectors Per FAT %d\t\tSectors Per Track %d\n",
520 h->bs.fatsec, h->bs.spt);
521 (void) fprintf(fout,
522 "Number of Heads %d\t\tNumber Hidden Sectors %d\n",
523 h->bs.nhead, h->bs.hiddensec);
524 (void) fprintf(fout, "Volume ID: 0x%x\n", PC_VOLID(h));
525 (void) fprintf(fout, "Volume Label: %s\n", h->volume_label);
526 (void) fprintf(fout, "Drive Number: 0x%x\n", PC_DRVNUM(h));
527 (void) fprintf(fout, "Media Type: 0x%x ", h->bs.mediadesriptor);
528
529 switch (h->bs.mediadesriptor) {
530 case MD_FIXED:
531 (void) fprintf(fout, "\"Fixed\" Disk\n");
532 break;
533 case SS8SPT:
534 (void) fprintf(fout, "Single Sided, 8 Sectors Per Track\n");
535 break;
536 case DS8SPT:
537 (void) fprintf(fout, "Double Sided, 8 Sectors Per Track\n");
538 break;
539 case SS9SPT:
540 (void) fprintf(fout, "Single Sided, 9 Sectors Per Track\n");
541 break;
542 case DS9SPT:
543 (void) fprintf(fout, "Double Sided, 9 Sectors Per Track\n");
544 break;
545 case DS18SPT:
546 (void) fprintf(fout, "Double Sided, 18 Sectors Per Track\n");
547 break;
548 case DS9_15SPT:
549 (void) fprintf(fout, "Double Sided, 9-15 Sectors Per Track\n");
550 break;
551 default:
552 (void) fprintf(fout, "Unknown Media Type\n");
553 }
554
555 return (0);
556 }
557
558 #define ADD_STRING(h, name, value) \
559 if (nvlist_add_string(h->attr, name, value) != 0) { \
560 return (FSTYP_ERR_NOMEM); \
561 }
562
563 #define ADD_UINT32(h, name, value) \
564 if (nvlist_add_uint32(h->attr, name, value) != 0) { \
565 return (FSTYP_ERR_NOMEM); \
566 }
567
568 #define ADD_UINT64(h, name, value) \
569 if (nvlist_add_uint64(h->attr, name, value) != 0) { \
570 return (FSTYP_ERR_NOMEM); \
571 }
572
573 #define ADD_BOOL(h, name, value) \
574 if (nvlist_add_boolean_value(h->attr, name, value) != 0) { \
575 return (FSTYP_ERR_NOMEM); \
576 }
577
578 static int
get_attr(fstyp_pcfs_t * h)579 get_attr(fstyp_pcfs_t *h)
580 {
581 char s[64];
582
583 ADD_UINT32(h, "bytes_per_sector", h->bps);
584 ADD_UINT32(h, "sectors_per_cluster", h->bs.spcl);
585 ADD_UINT32(h, "reserved_sectors", PC_RESSEC(h));
586 ADD_UINT32(h, "fats", h->bs.nfat);
587 ADD_UINT32(h, "root_entry_count", PC_NROOTENT(h));
588 ADD_UINT32(h, "total_sectors_16", PC_NSEC(h));
589 ADD_UINT32(h, "media", h->bs.mediadesriptor);
590 ADD_UINT32(h, "fat_size_16", h->bs.fatsec);
591 ADD_UINT32(h, "sectors_per_track", h->bs.spt);
592 ADD_UINT32(h, "heads", h->bs.nhead);
593 ADD_UINT32(h, "hidden_sectors", h->bs.hiddensec);
594 ADD_UINT32(h, "total_sectors_32", h->bs.totalsec);
595 ADD_UINT32(h, "drive_number", PC_DRVNUM(h));
596 ADD_UINT32(h, "volume_id", PC_VOLID(h));
597 ADD_STRING(h, "volume_label", h->volume_label);
598 if (FSTYP_IS_32(h)) {
599 ADD_UINT32(h, "fat_size_32", h->bs32.f_fatlength);
600 }
601 ADD_UINT32(h, "total_sectors", h->TotSec);
602 ADD_UINT32(h, "fat_size", h->FATSz);
603 ADD_UINT32(h, "count_of_clusters", h->CountOfClusters);
604 ADD_UINT32(h, "fat_entry_size", h->fattype);
605
606 ADD_BOOL(h, "gen_clean", B_TRUE);
607 if (PC_VOLID(a) != 0) {
608 (void) snprintf(s, sizeof (s), "%08x", PC_VOLID(a));
609 ADD_STRING(h, "gen_guid", s);
610 }
611 (void) snprintf(s, sizeof (s), "%d", h->fattype);
612 ADD_STRING(h, "gen_version", s);
613 ADD_STRING(h, "gen_volume_label", h->volume_label);
614
615 return (0);
616 }
617