diff --git a/usr.sbin/fstyp/Makefile b/usr.sbin/fstyp/Makefile index 384e2f7dee60..161cd0cf3764 100644 --- a/usr.sbin/fstyp/Makefile +++ b/usr.sbin/fstyp/Makefile @@ -1,55 +1,53 @@ # $FreeBSD$ .include PROG= fstyp SRCS= apfs.c befs.c cd9660.c exfat.c ext2fs.c fstyp.c geli.c hammer.c \ hammer2.c hfsplus.c msdosfs.c ntfs.c ufs.c .if ${MK_ZFS} != "no" SRCS += zfs.c .endif MAN= fstyp.8 -WARNS?= 2 - .if ${MK_ICONV} == "yes" CFLAGS+= -DWITH_ICONV .endif .include .if ${TARGET_ENDIANNESS} == 1234 HAS_TESTS= SUBDIR.${MK_TESTS}+= tests .endif .if ${MK_ZFS} != "no" IGNORE_PRAGMA= YES CFLAGS+= -DHAVE_ZFS CFLAGS.zfs.c+= -DIN_BASE CFLAGS.zfs.c+= -I${SRCTOP}/sys/contrib/openzfs/include CFLAGS.zfs.c+= -I${SRCTOP}/sys/contrib/openzfs/lib/libspl/include CFLAGS.zfs.c+= -I${SRCTOP}/sys/contrib/openzfs/lib/libspl/include/os/freebsd CFLAGS.zfs.c+= -I${SRCTOP}/sys/contrib/openzfs/module/icp/include CFLAGS.zfs.c+= -include ${SRCTOP}/sys/contrib/openzfs/include/os/freebsd/spl/sys/ccompile.h CFLAGS.zfs.c+= -DHAVE_ISSETUGID CFLAGS.zfs.c+= -include ${SRCTOP}/sys/modules/zfs/zfs_config.h .endif .for src in ${SRCS} .if ${src} != "zfs.c" CFLAGS.${src}+=-I${SRCTOP}/sys .endif .endfor LIBADD= geom md ufs .if ${MK_ZFS} != "no" LIBADD+=nvpair zfs spl .endif .include diff --git a/usr.sbin/fstyp/apfs.c b/usr.sbin/fstyp/apfs.c index 049a9f862f2b..7f8543a5a108 100644 --- a/usr.sbin/fstyp/apfs.c +++ b/usr.sbin/fstyp/apfs.c @@ -1,107 +1,109 @@ /* * Copyright (c) 2019 Conrad Meyer . All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "fstyp.h" /* * This really detects the container format, which might be best supported by * geom_part or a special GEOM class. * * https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf */ #define NX_CKSUM_SZ 8 typedef uint64_t nx_oid_t; typedef uint64_t nx_xid_t; struct nx_obj { uint8_t o_cksum[NX_CKSUM_SZ]; /* Fletcher 64 */ nx_oid_t o_oid; nx_xid_t o_xid; uint32_t o_type; uint32_t o_subtype; }; /* nx_obj::o_oid */ #define OID_NX_SUPERBLOCK 1 /* nx_obj::o_type: */ #define OBJECT_TYPE_MASK 0x0000ffff #define OBJECT_TYPE_NX_SUPERBLOCK 0x00000001 #define OBJECT_TYPE_FLAGS_MASK 0xffff0000 #define OBJ_STORAGETYPE_MASK 0xc0000000 #define OBJECT_TYPE_FLAGS_DEFINED_MASK 0xf8000000 #define OBJ_STORAGE_VIRTUAL 0x00000000 #define OBJ_STORAGE_EPHEMERAL 0x80000000 #define OBJ_STORAGE_PHYSICAL 0x40000000 #define OBJ_NOHEADER 0x20000000 #define OBJ_ENCRYPTED 0x10000000 #define OBJ_NONPERSISTENT 0x08000000 struct nx_superblock { struct nx_obj nx_o; char nx_magic[4]; /* ... other stuff that doesn't matter */ }; int fstyp_apfs(FILE *fp, char *label, size_t size) { struct nx_superblock *csb; int retval; retval = 1; csb = read_buf(fp, 0, sizeof(*csb)); if (csb == NULL) goto fail; /* Ideally, checksum the SB here. */ if (strncmp(csb->nx_magic, "NXSB", 4) != 0 || csb->nx_o.o_oid != OID_NX_SUPERBLOCK || (csb->nx_o.o_type & OBJECT_TYPE_MASK) != OBJECT_TYPE_NX_SUPERBLOCK) goto fail; retval = 0; /* No label support yet. */ + (void)size; + (void)label; fail: free(csb); return (retval); } diff --git a/usr.sbin/fstyp/fstyp.c b/usr.sbin/fstyp/fstyp.c index b39277914aed..91c36d9d9191 100644 --- a/usr.sbin/fstyp/fstyp.c +++ b/usr.sbin/fstyp/fstyp.c @@ -1,269 +1,269 @@ /*- * Copyright (c) 2014 The FreeBSD Foundation * * This software was developed by Edward Tomasz Napierala under sponsorship * from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #ifdef WITH_ICONV #include #endif #include #include #include #include #include #include #include #include #include "fstyp.h" #define LABEL_LEN 256 bool show_label = false; typedef int (*fstyp_function)(FILE *, char *, size_t); static struct { const char *name; fstyp_function function; bool unmountable; - char *precache_encoding; + const char *precache_encoding; } fstypes[] = { { "apfs", &fstyp_apfs, true, NULL }, { "befs", &fstyp_befs, false, NULL }, { "cd9660", &fstyp_cd9660, false, NULL }, { "exfat", &fstyp_exfat, false, EXFAT_ENC }, { "ext2fs", &fstyp_ext2fs, false, NULL }, { "geli", &fstyp_geli, true, NULL }, { "hammer", &fstyp_hammer, true, NULL }, { "hammer2", &fstyp_hammer2, true, NULL }, { "hfs+", &fstyp_hfsp, false, NULL }, { "msdosfs", &fstyp_msdosfs, false, NULL }, { "ntfs", &fstyp_ntfs, false, NTFS_ENC }, { "ufs", &fstyp_ufs, false, NULL }, #ifdef HAVE_ZFS { "zfs", &fstyp_zfs, true, NULL }, #endif { NULL, NULL, NULL, NULL } }; void * read_buf(FILE *fp, off_t off, size_t len) { int error; size_t nread; void *buf; error = fseek(fp, off, SEEK_SET); if (error != 0) { warn("cannot seek to %jd", (uintmax_t)off); return (NULL); } buf = malloc(len); if (buf == NULL) { warn("cannot malloc %zd bytes of memory", len); return (NULL); } nread = fread(buf, len, 1, fp); if (nread != 1) { free(buf); if (feof(fp) == 0) warn("fread"); return (NULL); } return (buf); } char * checked_strdup(const char *s) { char *c; c = strdup(s); if (c == NULL) err(1, "strdup"); return (c); } void rtrim(char *label, size_t size) { ptrdiff_t i; for (i = size - 1; i >= 0; i--) { if (label[i] == '\0') continue; else if (label[i] == ' ') label[i] = '\0'; else break; } } static void usage(void) { fprintf(stderr, "usage: fstyp [-l] [-s] [-u] special\n"); exit(1); } static void type_check(const char *path, FILE *fp) { int error, fd; off_t mediasize; struct stat sb; fd = fileno(fp); error = fstat(fd, &sb); if (error != 0) err(1, "%s: fstat", path); if (S_ISREG(sb.st_mode)) return; error = ioctl(fd, DIOCGMEDIASIZE, &mediasize); if (error != 0) errx(1, "%s: not a disk", path); } int main(int argc, char **argv) { int ch, error, i, nbytes; bool ignore_type = false, show_unmountable = false; char label[LABEL_LEN + 1], strvised[LABEL_LEN * 4 + 1]; char *path; FILE *fp; fstyp_function fstyp_f; while ((ch = getopt(argc, argv, "lsu")) != -1) { switch (ch) { case 'l': show_label = true; break; case 's': ignore_type = true; break; case 'u': show_unmountable = true; break; default: usage(); } } argc -= optind; argv += optind; if (argc != 1) usage(); path = argv[0]; if (setlocale(LC_CTYPE, "") == NULL) err(1, "setlocale"); caph_cache_catpages(); #ifdef WITH_ICONV /* Cache iconv conversion data before entering capability mode. */ if (show_label) { - for (i = 0; i < nitems(fstypes); i++) { + for (i = 0; i < (int)nitems(fstypes); i++) { iconv_t cd; if (fstypes[i].precache_encoding == NULL) continue; cd = iconv_open("", fstypes[i].precache_encoding); if (cd == (iconv_t)-1) err(1, "%s: iconv_open %s", fstypes[i].name, fstypes[i].precache_encoding); /* Iconv keeps a small cache of unused encodings. */ iconv_close(cd); } } #endif fp = fopen(path, "r"); if (fp == NULL) err(1, "%s", path); if (caph_enter() < 0) err(1, "cap_enter"); if (ignore_type == false) type_check(path, fp); memset(label, '\0', sizeof(label)); for (i = 0;; i++) { if (show_unmountable == false && fstypes[i].unmountable == true) continue; fstyp_f = fstypes[i].function; if (fstyp_f == NULL) break; error = fstyp_f(fp, label, sizeof(label)); if (error == 0) break; } if (fstypes[i].name == NULL) { warnx("%s: filesystem not recognized", path); return (1); } if (show_label && label[0] != '\0') { /* * XXX: I'd prefer VIS_HTTPSTYLE, but it unconditionally * encodes spaces. */ nbytes = strsnvis(strvised, sizeof(strvised), label, VIS_GLOB | VIS_NL, "\"'$"); if (nbytes == -1) err(1, "strsnvis"); printf("%s %s\n", fstypes[i].name, strvised); } else { printf("%s\n", fstypes[i].name); } return (0); } diff --git a/usr.sbin/fstyp/hammer.c b/usr.sbin/fstyp/hammer.c index 6fdad1d642a4..777f5d312371 100644 --- a/usr.sbin/fstyp/hammer.c +++ b/usr.sbin/fstyp/hammer.c @@ -1,208 +1,211 @@ /*- * Copyright (c) 2016-2019 The DragonFly Project * Copyright (c) 2016-2019 Tomohiro Kusumi * All rights reserved. * * This software was developed by Edward Tomasz Napierala under sponsorship * from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include "hammer_disk.h" #include "fstyp.h" +extern int fsvtyp_hammer(const char *blkdevs, char *label, size_t size); +extern int fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size); + static hammer_volume_ondisk_t read_ondisk(FILE *fp) { hammer_volume_ondisk_t ondisk; ondisk = read_buf(fp, 0, sizeof(*ondisk)); if (ondisk == NULL) err(1, "failed to read ondisk"); return (ondisk); } static int test_ondisk(const hammer_volume_ondisk_t ondisk) { static int count = 0; static hammer_uuid_t fsid, fstype; static char label[64]; if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME && ondisk->vol_signature != HAMMER_FSBUF_VOLUME_REV) return (1); if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO) return (2); if (ondisk->vol_no < 0 || ondisk->vol_no > HAMMER_MAX_VOLUMES - 1) return (3); if (ondisk->vol_count < 1 || ondisk->vol_count > HAMMER_MAX_VOLUMES) return (4); if (count == 0) { count = ondisk->vol_count; assert(count != 0); memcpy(&fsid, &ondisk->vol_fsid, sizeof(fsid)); memcpy(&fstype, &ondisk->vol_fstype, sizeof(fstype)); strlcpy(label, ondisk->vol_label, sizeof(label)); } else { if (ondisk->vol_count != count) return (5); if (memcmp(&ondisk->vol_fsid, &fsid, sizeof(fsid))) return (6); if (memcmp(&ondisk->vol_fstype, &fstype, sizeof(fstype))) return (7); if (strcmp(ondisk->vol_label, label)) return (8); } return (0); } int fstyp_hammer(FILE *fp, char *label, size_t size) { hammer_volume_ondisk_t ondisk; int error = 1; ondisk = read_ondisk(fp); if (ondisk->vol_no != HAMMER_ROOT_VOLNO) goto fail; if (ondisk->vol_count != 1) goto fail; if (test_ondisk(ondisk)) goto fail; strlcpy(label, ondisk->vol_label, size); error = 0; fail: free(ondisk); return (error); } static int test_volume(const char *volpath) { hammer_volume_ondisk_t ondisk; FILE *fp; int volno = -1; if ((fp = fopen(volpath, "r")) == NULL) err(1, "failed to open %s", volpath); ondisk = read_ondisk(fp); fclose(fp); if (test_ondisk(ondisk)) goto fail; volno = ondisk->vol_no; fail: free(ondisk); return (volno); } static int __fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial) { hammer_volume_ondisk_t ondisk = NULL; FILE *fp; char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES]; int i, volno, error = 1; if (!blkdevs) goto fail; memset(x, 0, sizeof(x)); dup = strdup(blkdevs); p = dup; volpath = NULL; volno = -1; while (p) { volpath = p; if ((p = strchr(p, ':')) != NULL) *p++ = '\0'; if ((volno = test_volume(volpath)) == -1) break; assert(volno >= 0); assert(volno < HAMMER_MAX_VOLUMES); x[volno]++; } if (!volpath) err(1, "invalid path %s", blkdevs); if ((fp = fopen(volpath, "r")) == NULL) err(1, "failed to open %s", volpath); ondisk = read_ondisk(fp); fclose(fp); free(dup); if (volno == -1) goto fail; if (partial) goto success; for (i = 0; i < HAMMER_MAX_VOLUMES; i++) if (x[i] > 1) goto fail; for (i = 0; i < HAMMER_MAX_VOLUMES; i++) if (x[i] == 0) break; if (ondisk->vol_count != i) goto fail; for (; i < HAMMER_MAX_VOLUMES; i++) if (x[i] != 0) goto fail; success: strlcpy(label, ondisk->vol_label, size); error = 0; fail: free(ondisk); return (error); } int fsvtyp_hammer(const char *blkdevs, char *label, size_t size) { return (__fsvtyp_hammer(blkdevs, label, size, 0)); } int fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size) { return (__fsvtyp_hammer(blkdevs, label, size, 1)); } diff --git a/usr.sbin/fstyp/hfsplus.c b/usr.sbin/fstyp/hfsplus.c index 71287465e4ff..3a94faf7766f 100644 --- a/usr.sbin/fstyp/hfsplus.c +++ b/usr.sbin/fstyp/hfsplus.c @@ -1,125 +1,127 @@ /* * Copyright (c) 2019 Conrad Meyer . All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "fstyp.h" /* * https://developer.apple.com/library/archive/technotes/tn/tn1150.html */ #define VOL_HDR_OFF 1024 typedef uint32_t hfsp_cat_nodeid; typedef struct hfsp_ext_desc { uint32_t ex_startBlock; uint32_t ex_blockCount; } hfsp_ext_desc; typedef struct hfsp_fork_data { uint64_t fd_logicalSz; uint32_t fd_clumpSz; uint32_t fd_totalBlocks; hfsp_ext_desc fd_extents[8]; } hfsp_fork_data; struct hfsp_vol_hdr { char hp_signature[2]; uint16_t hp_version; uint32_t hp_attributes; uint32_t hp_lastMounted; uint32_t hp_journalInfoBlock; /* Creation / etc dates. */ uint32_t hp_create; uint32_t hp_modify; uint32_t hp_backup; uint32_t hp_checked; /* Stats */ uint32_t hp_files; uint32_t hp_folders; /* Parameters */ uint32_t hp_blockSize; uint32_t hp_totalBlocks; uint32_t hp_freeBlocks; uint32_t hp_nextAlloc; uint32_t hp_rsrcClumpSz; uint32_t hp_dataClumpSz; hfsp_cat_nodeid hp_nextCatID; uint32_t hp_writeCount; uint64_t hp_encodingsBM; uint32_t hp_finderInfo[8]; hfsp_fork_data hp_allocationFile; hfsp_fork_data hp_extentsFile; hfsp_fork_data hp_catalogFile; hfsp_fork_data hp_attributesFile; hfsp_fork_data hp_startupFile; }; _Static_assert(sizeof(struct hfsp_vol_hdr) == 512, ""); int fstyp_hfsp(FILE *fp, char *label, size_t size) { struct hfsp_vol_hdr *hdr; int retval; retval = 1; hdr = read_buf(fp, VOL_HDR_OFF, sizeof(*hdr)); if (hdr == NULL) goto fail; if ((strncmp(hdr->hp_signature, "H+", 2) != 0 || hdr->hp_version != 4) && (strncmp(hdr->hp_signature, "HX", 2) != 0 || hdr->hp_version != 5)) goto fail; /* This is an HFS+ volume. */ retval = 0; /* No label support yet. */ + (void)size; + (void)label; fail: free(hdr); return (retval); }