commit e926da88667b3230a846ec7178168d5b07343a44
parent 8f3f1ee9fa5f7720f39ba31e94f091c68be2df71
Author: cowmonk <cowmonk@based.pt>
Date: Sat, 28 Nov 2026 13:42:26 -0700
mkfs.fat: vendor from AOSP fsck_msdos
some custom glue: fix fat32 fsinfo read/write false warnings (boot.c),
and checking if the device is mounted before actually doing any repairs,
etc. (check.c)
Everything else is just intergration with aruu. And documentation.
Diffstat:
14 files changed, 4319 insertions(+), 11 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -69,4 +69,5 @@ Portions of this software (specifically utils under net/) are partially
based on code written by Rob Landley
mkfs.fat is a port of OpenBSD's newfs_msdos (BSD 2-Clause).
mkfs.ext4 is based on lwext4's formatter (BSD 3-Clause).
+fsck.fat is from Android's BSD fsck_msdos. See cmd/extra/fsck.fat/NOTICE for its license notices.
The rest are based on SBASE/UBASE source code
diff --git a/README b/README
@@ -152,8 +152,8 @@ xsi:
standalone and installed with setuid
extra:
- b3sum, blkid, dc, fdisk, kmap, lsblk, mkfs.ext4, mkfs.fat, partplan,
- pgrep, signify, sync, yap
+ b3sum, blkid, dc, fdisk, fsck.fat, kmap, lsblk, mkfs.ext4, mkfs.fat,
+ partplan, pgrep, signify, sync, yap
TODOs / PENDING RELEASE
=======================
@@ -224,6 +224,7 @@ Portions of this software (specifically utils under net/) are partially based on
Our line editing library is partially based on work by Salvatore Sanfilippo (linenoise) and JC Wang (crossline)
mkfs.ext4 is based on lwext4's formatter (BSD 3-Clause).
mkfs.fat is a port of OpenBSD's newfs_msdos (BSD 2-Clause).
+fsck.fat is from Android's BSD fsck_msdos. See cmd/extra/fsck.fat/NOTICE for its license notices.
The rest are based on SBASE/UBASE source code
=~= Argentino Roca Unix Utilities =~= https://github.com/xplshn/aruu =~=
diff --git a/cmd/extra/fsck.fat/NOTICE b/cmd/extra/fsck.fat/NOTICE
@@ -0,0 +1,289 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (C) 1995, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2019 Google LLC
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ * Some structure declaration borrowed from Paul Popelka
+ * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
+ *
+ * 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 ``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 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ * Some structure declaration borrowed from Paul Popelka
+ * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
+ *
+ * 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 ``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 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Google LLC
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 1982, 1986, 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * 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.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
+ * 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. 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.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 1996 Christos Zoulas. 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 ``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 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.
+ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1995 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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.
+ */
+
diff --git a/cmd/extra/fsck.fat/boot.c b/cmd/extra/fsck.fat/boot.c
@@ -0,0 +1,367 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1995, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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 "freebsd-compat.h"
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: boot.c,v 1.22 2020/01/11 16:29:07 christos Exp $");
+#endif /* not lint */
+
+#include <sys/param.h>
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "ext.h"
+#include "fsutil.h"
+
+int
+readboot(int dosfs, struct bootblock *boot)
+{
+ u_char block[DOSBOOTBLOCKSIZE];
+ u_char fsinfo[DOSBOOTBLOCKSIZE];
+ int ret = FSOK;
+
+ if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
+ perr("could not read boot block");
+ return FSFATAL;
+ }
+
+ if (block[510] != 0x55 || block[511] != 0xaa) {
+ pfatal("Invalid signature in boot block: %02x%02x",
+ block[511], block[510]);
+ return FSFATAL;
+ }
+
+ memset(boot, 0, sizeof *boot);
+ boot->ValidFat = -1;
+
+ /* Decode BIOS Parameter Block */
+
+ /* Bytes per sector: can only be 512, 1024, 2048 and 4096. */
+ boot->bpbBytesPerSec = block[11] + (block[12] << 8);
+ if (boot->bpbBytesPerSec < DOSBOOTBLOCKSIZE_REAL ||
+ boot->bpbBytesPerSec > DOSBOOTBLOCKSIZE ||
+ !powerof2(boot->bpbBytesPerSec)) {
+ pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
+ return FSFATAL;
+ }
+
+ /* Sectors per cluster: can only be: 1, 2, 4, 8, 16, 32, 64, 128. */
+ boot->bpbSecPerClust = block[13];
+ if (boot->bpbSecPerClust == 0 || !powerof2(boot->bpbSecPerClust)) {
+ pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
+ return FSFATAL;
+ }
+
+ /* Reserved sectors: must be non-zero */
+ boot->bpbResSectors = block[14] + (block[15] << 8);
+ if (boot->bpbResSectors < 1) {
+ pfatal("Invalid reserved sectors: %u",
+ boot->bpbResSectors);
+ return FSFATAL;
+ }
+
+ /* Number of FATs */
+ boot->bpbFATs = block[16];
+ if (boot->bpbFATs == 0) {
+ pfatal("Invalid number of FATs: %u", boot->bpbFATs);
+ return FSFATAL;
+ }
+
+ /* Root directory entries for FAT12 and FAT16 */
+ boot->bpbRootDirEnts = block[17] + (block[18] << 8);
+ if (!boot->bpbRootDirEnts) {
+ /* bpbRootDirEnts = 0 suggests that we are FAT32 */
+ boot->flags |= FAT32;
+ }
+
+ /* Total sectors (16 bits) */
+ boot->bpbSectors = block[19] + (block[20] << 8);
+ if (boot->bpbSectors != 0 && (boot->flags & FAT32)) {
+ pfatal("Invalid 16-bit total sector count on FAT32: %u",
+ boot->bpbSectors);
+ return FSFATAL;
+ }
+
+ /* Media type: ignored */
+ boot->bpbMedia = block[21];
+
+ /* FAT12/FAT16: 16-bit count of sectors per FAT */
+ boot->bpbFATsmall = block[22] + (block[23] << 8);
+ if (boot->bpbFATsmall != 0 && (boot->flags & FAT32)) {
+ pfatal("Invalid 16-bit FAT sector count on FAT32: %u",
+ boot->bpbFATsmall);
+ return FSFATAL;
+ }
+
+ /* Legacy CHS geometry numbers: ignored */
+ boot->SecPerTrack = block[24] + (block[25] << 8);
+ boot->bpbHeads = block[26] + (block[27] << 8);
+
+ /* Hidden sectors: ignored */
+ boot->bpbHiddenSecs = block[28] + (block[29] << 8) +
+ (block[30] << 16) + (block[31] << 24);
+
+ /* Total sectors (32 bits) */
+ boot->bpbHugeSectors = block[32] + (block[33] << 8) +
+ (block[34] << 16) + (block[35] << 24);
+ if (boot->bpbHugeSectors == 0) {
+ if (boot->flags & FAT32) {
+ pfatal("FAT32 with sector count of zero");
+ return FSFATAL;
+ } else if (boot->bpbSectors == 0) {
+ pfatal("FAT with sector count of zero");
+ return FSFATAL;
+ }
+ boot->NumSectors = boot->bpbSectors;
+ } else {
+ if (boot->bpbSectors != 0) {
+ pfatal("Invalid FAT sector count");
+ return FSFATAL;
+ }
+ boot->NumSectors = boot->bpbHugeSectors;
+ }
+
+ if (boot->flags & FAT32) {
+ /* If the OEM Name field is EXFAT, it's not FAT32, so bail */
+ if (!memcmp(&block[3], "EXFAT ", 8)) {
+ pfatal("exFAT filesystem is not supported.");
+ return FSFATAL;
+ }
+
+ /* 32-bit count of sectors per FAT */
+ boot->FATsecs = block[36] + (block[37] << 8)
+ + (block[38] << 16) + (block[39] << 24);
+
+ if (block[40] & 0x80)
+ boot->ValidFat = block[40] & 0x0f;
+
+ /* FAT32 version, bail out if not 0.0 */
+ if (block[42] || block[43]) {
+ pfatal("Unknown file system version: %x.%x",
+ block[43], block[42]);
+ return FSFATAL;
+ }
+
+ /*
+ * Cluster number of the first cluster of root directory.
+ *
+ * Should be 2 but do not require it.
+ */
+ boot->bpbRootClust = block[44] + (block[45] << 8)
+ + (block[46] << 16) + (block[47] << 24);
+
+ /* Sector number of the FSInfo structure, usually 1 */
+ boot->bpbFSInfo = block[48] + (block[49] << 8);
+
+ /* Sector number of the backup boot block, ignored */
+ boot->bpbBackup = block[50] + (block[51] << 8);
+
+ /* Check basic parameters */
+ if (boot->bpbFSInfo == 0) {
+ /*
+ * Either the BIOS Parameter Block has been corrupted,
+ * or this is not a FAT32 filesystem, most likely an
+ * exFAT filesystem.
+ */
+ pfatal("Invalid FAT32 Extended BIOS Parameter Block");
+ return FSFATAL;
+ }
+
+ /* FSInfo occupies one logical sector. */
+ if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
+ SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
+ || read(dosfs, fsinfo, boot->bpbBytesPerSec) !=
+ boot->bpbBytesPerSec) {
+ perr("could not read fsinfo block");
+ return FSFATAL;
+ }
+ if (memcmp(fsinfo, "RRaA", 4)
+ || memcmp(fsinfo + 0x1e4, "rrAa", 4)
+ || fsinfo[0x1fc]
+ || fsinfo[0x1fd]
+ || fsinfo[0x1fe] != 0x55
+ || fsinfo[0x1ff] != 0xaa) {
+ pwarn("Invalid signature in fsinfo block\n");
+ if (ask(0, "Fix")) {
+ memcpy(fsinfo, "RRaA", 4);
+ memcpy(fsinfo + 0x1e4, "rrAa", 4);
+ fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
+ fsinfo[0x1fe] = 0x55;
+ fsinfo[0x1ff] = 0xaa;
+ if (lseek(dosfs, boot->bpbFSInfo *
+ boot->bpbBytesPerSec, SEEK_SET)
+ != boot->bpbFSInfo * boot->bpbBytesPerSec
+ || write(dosfs, fsinfo, boot->bpbBytesPerSec)
+ != boot->bpbBytesPerSec) {
+ perr("Unable to write bpbFSInfo");
+ return FSFATAL;
+ }
+ ret = FSBOOTMOD;
+ } else
+ boot->bpbFSInfo = 0;
+ } else {
+ /* We appear to have a valid FSInfo block, decode */
+ boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
+ + (fsinfo[0x1ea] << 16)
+ + (fsinfo[0x1eb] << 24);
+ boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
+ + (fsinfo[0x1ee] << 16)
+ + (fsinfo[0x1ef] << 24);
+ }
+ } else {
+ /* !FAT32: FAT12/FAT16 */
+ boot->FATsecs = boot->bpbFATsmall;
+ }
+
+ if (boot->FATsecs < 1 || boot->FATsecs > UINT32_MAX / boot->bpbFATs) {
+ pfatal("Invalid FATs(%u) with FATsecs(%zu)",
+ boot->bpbFATs, (size_t)boot->FATsecs);
+ return FSFATAL;
+ }
+
+ boot->FirstCluster = (boot->bpbRootDirEnts * 32 +
+ boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
+ boot->bpbResSectors + boot->bpbFATs * boot->FATsecs;
+
+ if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) {
+ pfatal("Cluster offset too large (%u clusters)\n",
+ boot->FirstCluster);
+ return FSFATAL;
+ }
+
+ /*
+ * The number of clusters is derived from available data sectors,
+ * divided by sectors per cluster.
+ */
+ boot->NumClusters =
+ (boot->NumSectors - boot->FirstCluster) / boot->bpbSecPerClust;
+
+ if (boot->flags & FAT32) {
+ if (boot->NumClusters > (CLUST_RSRVD & CLUST32_MASK)) {
+ pfatal("Filesystem too big (%u clusters) for FAT32 partition",
+ boot->NumClusters);
+ return FSFATAL;
+ }
+ if (boot->NumClusters < (CLUST_RSRVD & CLUST16_MASK)) {
+ pfatal("Filesystem too small (%u clusters) for FAT32 partition",
+ boot->NumClusters);
+ return FSFATAL;
+ }
+ boot->ClustMask = CLUST32_MASK;
+
+ if (boot->bpbRootClust < CLUST_FIRST ||
+ boot->bpbRootClust >= boot->NumClusters) {
+ pfatal("Root directory starts with cluster out of range(%u)",
+ boot->bpbRootClust);
+ return FSFATAL;
+ }
+ } else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) {
+ boot->ClustMask = CLUST12_MASK;
+ } else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) {
+ boot->ClustMask = CLUST16_MASK;
+ } else {
+ pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
+ boot->NumClusters);
+ return FSFATAL;
+ }
+
+ switch (boot->ClustMask) {
+ case CLUST32_MASK:
+ boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
+ break;
+ case CLUST16_MASK:
+ boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
+ break;
+ default:
+ boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
+ break;
+ }
+
+ if (boot->NumFatEntries < boot->NumClusters) {
+ pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
+ boot->NumClusters, boot->FATsecs);
+ return FSFATAL;
+ }
+
+ /*
+ * There are two reserved clusters. To avoid adding CLUST_FIRST every
+ * time we perform boundary checks, we increment the NumClusters by 2,
+ * which is CLUST_FIRST to denote the first out-of-range cluster number.
+ */
+ boot->NumClusters += CLUST_FIRST;
+
+ boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
+
+ boot->NumFiles = 1;
+ boot->NumFree = 0;
+
+ return ret;
+}
+
+int
+writefsinfo(int dosfs, struct bootblock *boot)
+{
+ u_char fsinfo[DOSBOOTBLOCKSIZE];
+
+ if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
+ != boot->bpbFSInfo * boot->bpbBytesPerSec
+ || read(dosfs, fsinfo, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) {
+ perr("could not read fsinfo block");
+ return FSFATAL;
+ }
+ fsinfo[0x1e8] = (u_char)boot->FSFree;
+ fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
+ fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
+ fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
+ fsinfo[0x1ec] = (u_char)boot->FSNext;
+ fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
+ fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
+ fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
+ if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
+ != boot->bpbFSInfo * boot->bpbBytesPerSec
+ || write(dosfs, fsinfo, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) {
+ perr("Unable to write bpbFSInfo");
+ return FSFATAL;
+ }
+ /*
+ * Technically, we should return FSBOOTMOD here.
+ *
+ * However, since Win95 OSR2 (the first M$ OS that has
+ * support for FAT32) doesn't maintain the FSINFO block
+ * correctly, it has to be fixed pretty often.
+ *
+ * Therefore, we handle the FSINFO block only informally,
+ * fixing it if necessary, but otherwise ignoring the
+ * fact that it was incorrect.
+ */
+ return 0;
+}
diff --git a/cmd/extra/fsck.fat/check.c b/cmd/extra/fsck.fat/check.c
@@ -0,0 +1,203 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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 "freebsd-compat.h"
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: check.c,v 1.14 2006/06/05 16:51:18 christos Exp $");
+#endif /* not lint */
+
+#ifdef HAVE_LIBUTIL_H
+#include <libutil.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "ext.h"
+#include "fsutil.h"
+
+int
+checkfilesys(const char *fname)
+{
+ int dosfs;
+ struct bootblock boot;
+ struct fat_descriptor *fat = NULL;
+ int finish_dosdirsection=0;
+ int flags = O_RDWR;
+ int mod = 0;
+ int ret = 8;
+ int64_t freebytes;
+ int64_t badbytes;
+
+ rdonly = alwaysno;
+ if (!preen)
+ printf("** %s", fname);
+
+#if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
+ /* Claim the block device before repairing it. */
+ flags |= O_EXCL;
+#endif
+ dosfs = open(fname, rdonly ? O_RDONLY : flags);
+ if (dosfs < 0 && !rdonly && errno == EBUSY) {
+ if (!preen)
+ printf("\n");
+ perr("`%s' is in use\n", fname);
+ return 8;
+ }
+ if (dosfs < 0 && !rdonly) {
+ dosfs = open(fname, O_RDONLY, 0);
+ if (dosfs >= 0)
+ pwarn(" (NO WRITE)\n");
+ else if (!preen)
+ printf("\n");
+ rdonly = 1;
+ } else if (!preen)
+ printf("\n");
+
+ if (dosfs < 0) {
+ perr("Can't open `%s'", fname);
+ printf("\n");
+ return 8;
+ }
+
+ if (readboot(dosfs, &boot) == FSFATAL) {
+ close(dosfs);
+ printf("\n");
+ return 8;
+ }
+
+ if (skipclean && preen && checkdirty(dosfs, &boot)) {
+ printf("%s: ", fname);
+ printf("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
+ ret = 0;
+ goto out;
+ }
+
+ if (!preen) {
+ printf("** Phase 1 - Read FAT and checking connectivity\n");
+ }
+
+ mod |= readfat(dosfs, &boot, &fat);
+ if (mod & FSFATAL) {
+ close(dosfs);
+ return 8;
+ }
+
+ if (!preen)
+ printf("** Phase 2 - Checking Directories\n");
+
+ mod |= resetDosDirSection(fat);
+ finish_dosdirsection = 1;
+ if (mod & FSFATAL)
+ goto out;
+ /* delay writing FATs */
+
+ mod |= handleDirTree(fat);
+ if (mod & FSFATAL)
+ goto out;
+
+ if (!preen)
+ printf("** Phase 3 - Checking for Lost Files\n");
+
+ mod |= checklost(fat);
+ if (mod & FSFATAL)
+ goto out;
+
+ /* now write the FATs */
+ if (mod & FSFATMOD) {
+ if (ask(1, "Update FATs")) {
+ mod |= writefat(fat);
+ if (mod & FSFATAL)
+ goto out;
+ } else
+ mod |= FSERROR;
+ }
+
+ freebytes = (int64_t)boot.NumFree * boot.ClusterSize;
+ badbytes = (int64_t)boot.NumBad * boot.ClusterSize;
+
+#ifdef HAVE_LIBUTIL_H
+ char freestr[7], badstr[7];
+
+ humanize_number(freestr, sizeof(freestr), freebytes, "",
+ HN_AUTOSCALE, HN_DECIMAL | HN_IEC_PREFIXES);
+ if (boot.NumBad) {
+ humanize_number(badstr, sizeof(badstr), badbytes, "",
+ HN_AUTOSCALE, HN_B | HN_DECIMAL | HN_IEC_PREFIXES);
+
+ pwarn("%d files, %sB free (%d clusters), %sB bad (%d clusters)\n",
+ boot.NumFiles, freestr, boot.NumFree,
+ badstr, boot.NumBad);
+ } else {
+ pwarn("%d files, %sB free (%d clusters)\n",
+ boot.NumFiles, freestr, boot.NumFree);
+ }
+#else
+ if (boot.NumBad)
+ pwarn("%d files, %jd KiB free (%d clusters), %jd KiB bad (%d clusters)\n",
+ boot.NumFiles, (intmax_t)freebytes / 1024, boot.NumFree,
+ (intmax_t)badbytes / 1024, boot.NumBad);
+ else
+ pwarn("%d files, %jd KiB free (%d clusters)\n",
+ boot.NumFiles, (intmax_t)freebytes / 1024, boot.NumFree);
+#endif
+
+ if (mod && (mod & FSERROR) == 0) {
+ if (mod & FSDIRTY) {
+ if (ask(1, "MARK FILE SYSTEM CLEAN") == 0)
+ mod &= ~FSDIRTY;
+
+ if (mod & FSDIRTY) {
+ pwarn("MARKING FILE SYSTEM CLEAN\n");
+ mod |= cleardirty(fat);
+ } else {
+ pwarn("\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n");
+ mod |= FSERROR; /* file system not clean */
+ }
+ }
+ }
+
+ if (mod & (FSFATAL | FSERROR))
+ goto out;
+
+ ret = 0;
+
+ out:
+ if (finish_dosdirsection)
+ finishDosDirSection();
+ free(fat);
+ close(dosfs);
+
+ if (mod & (FSFATMOD|FSDIRMOD))
+ pwarn("\n***** FILE SYSTEM WAS MODIFIED *****\n");
+
+ return ret;
+}
diff --git a/cmd/extra/fsck.fat/dir.c b/cmd/extra/fsck.fat/dir.c
@@ -0,0 +1,1180 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Google LLC
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ * Some structure declaration borrowed from Paul Popelka
+ * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
+ *
+ * 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 ``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 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 "freebsd-compat.h"
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: dir.c,v 1.20 2006/06/05 16:51:18 christos Exp $");
+#endif /* not lint */
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <sys/param.h>
+
+#include "ext.h"
+#include "fsutil.h"
+#include "util.h"
+
+#define SLOT_EMPTY 0x00 /* slot has never been used */
+#define SLOT_E5 0x05 /* the real value is 0xe5 */
+#define SLOT_DELETED 0xe5 /* file in this slot deleted */
+
+#define ATTR_NORMAL 0x00 /* normal file */
+#define ATTR_READONLY 0x01 /* file is readonly */
+#define ATTR_HIDDEN 0x02 /* file is hidden */
+#define ATTR_SYSTEM 0x04 /* file is a system file */
+#define ATTR_VOLUME 0x08 /* entry is a volume label */
+#define ATTR_DIRECTORY 0x10 /* entry is a directory name */
+#define ATTR_ARCHIVE 0x20 /* file is new or modified */
+
+#define ATTR_WIN95 0x0f /* long name record */
+
+/*
+ * This is the format of the contents of the deTime field in the direntry
+ * structure.
+ * We don't use bitfields because we don't know how compilers for
+ * arbitrary machines will lay them out.
+ */
+#define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
+#define DT_2SECONDS_SHIFT 0
+#define DT_MINUTES_MASK 0x7E0 /* minutes */
+#define DT_MINUTES_SHIFT 5
+#define DT_HOURS_MASK 0xF800 /* hours */
+#define DT_HOURS_SHIFT 11
+
+/*
+ * This is the format of the contents of the deDate field in the direntry
+ * structure.
+ */
+#define DD_DAY_MASK 0x1F /* day of month */
+#define DD_DAY_SHIFT 0
+#define DD_MONTH_MASK 0x1E0 /* month */
+#define DD_MONTH_SHIFT 5
+#define DD_YEAR_MASK 0xFE00 /* year - 1980 */
+#define DD_YEAR_SHIFT 9
+
+
+/* dir.c */
+static struct dosDirEntry *newDosDirEntry(void);
+static void freeDosDirEntry(struct dosDirEntry *);
+static struct dirTodoNode *newDirTodo(void);
+static void freeDirTodo(struct dirTodoNode *);
+static char *fullpath(struct dosDirEntry *);
+static u_char calcShortSum(u_char *);
+static int delete(struct fat_descriptor *, cl_t, int, cl_t, int, int);
+static int removede(struct fat_descriptor *, u_char *, u_char *,
+ cl_t, cl_t, cl_t, char *, int);
+static int checksize(struct fat_descriptor *, u_char *, struct dosDirEntry *);
+static int readDosDirSection(struct fat_descriptor *, struct dosDirEntry *);
+
+/*
+ * Manage free dosDirEntry structures.
+ */
+static struct dosDirEntry *freede;
+
+static struct dosDirEntry *
+newDosDirEntry(void)
+{
+ struct dosDirEntry *de;
+
+ if (!(de = freede)) {
+ if (!(de = malloc(sizeof *de)))
+ return (NULL);
+ } else
+ freede = de->next;
+ return de;
+}
+
+static void
+freeDosDirEntry(struct dosDirEntry *de)
+{
+ de->next = freede;
+ freede = de;
+}
+
+/*
+ * The same for dirTodoNode structures.
+ */
+static struct dirTodoNode *freedt;
+
+static struct dirTodoNode *
+newDirTodo(void)
+{
+ struct dirTodoNode *dt;
+
+ if (!(dt = freedt)) {
+ if (!(dt = malloc(sizeof *dt)))
+ return 0;
+ } else
+ freedt = dt->next;
+ return dt;
+}
+
+static void
+freeDirTodo(struct dirTodoNode *dt)
+{
+ dt->next = freedt;
+ freedt = dt;
+}
+
+/*
+ * The stack of unread directories
+ */
+static struct dirTodoNode *pendingDirectories = NULL;
+
+/*
+ * Return the full pathname for a directory entry.
+ */
+static char *
+fullpath(struct dosDirEntry *dir)
+{
+ static char namebuf[MAXPATHLEN + 1];
+ char *cp, *np;
+ int nl;
+
+ cp = namebuf + sizeof namebuf;
+ *--cp = '\0';
+
+ for(;;) {
+ np = dir->lname[0] ? dir->lname : dir->name;
+ nl = strlen(np);
+ if (cp <= namebuf + 1 + nl) {
+ *--cp = '?';
+ break;
+ }
+ cp -= nl;
+ memcpy(cp, np, nl);
+ dir = dir->parent;
+ if (!dir)
+ break;
+ *--cp = '/';
+ }
+
+ return cp;
+}
+
+/*
+ * Calculate a checksum over an 8.3 alias name
+ */
+static inline u_char
+calcShortSum(u_char *p)
+{
+ u_char sum = 0;
+ int i;
+
+ for (i = 0; i < 11; i++) {
+ sum = (sum << 7)|(sum >> 1); /* rotate right */
+ sum += p[i];
+ }
+
+ return sum;
+}
+
+/*
+ * Global variables temporarily used during a directory scan
+ */
+static char longName[DOSLONGNAMELEN] = "";
+static u_char *buffer = NULL;
+static u_char *delbuf = NULL;
+
+static struct dosDirEntry *rootDir;
+static struct dosDirEntry *lostDir;
+
+/*
+ * Init internal state for a new directory scan.
+ */
+int
+resetDosDirSection(struct fat_descriptor *fat)
+{
+ int rootdir_size, cluster_size;
+ int ret = FSOK;
+ size_t len;
+ struct bootblock *boot;
+
+ boot = fat_get_boot(fat);
+
+ rootdir_size = boot->bpbRootDirEnts * 32;
+ cluster_size = boot->bpbSecPerClust * boot->bpbBytesPerSec;
+
+ if ((buffer = malloc(len = MAX(rootdir_size, cluster_size))) == NULL) {
+ perr("No space for directory buffer (%zu)", len);
+ return FSFATAL;
+ }
+
+ if ((delbuf = malloc(len = cluster_size)) == NULL) {
+ free(buffer);
+ perr("No space for directory delbuf (%zu)", len);
+ return FSFATAL;
+ }
+
+ if ((rootDir = newDosDirEntry()) == NULL) {
+ free(buffer);
+ free(delbuf);
+ perr("No space for directory entry");
+ return FSFATAL;
+ }
+
+ memset(rootDir, 0, sizeof *rootDir);
+ if (boot->flags & FAT32) {
+ if (!fat_is_cl_head(fat, boot->bpbRootClust)) {
+ pfatal("Root directory doesn't start a cluster chain");
+ return FSFATAL;
+ }
+ rootDir->head = boot->bpbRootClust;
+ }
+
+ return ret;
+}
+
+/*
+ * Cleanup after a directory scan
+ */
+void
+finishDosDirSection(void)
+{
+ struct dirTodoNode *p, *np;
+ struct dosDirEntry *d, *nd;
+
+ for (p = pendingDirectories; p; p = np) {
+ np = p->next;
+ freeDirTodo(p);
+ }
+ pendingDirectories = NULL;
+ for (d = rootDir; d; d = nd) {
+ if ((nd = d->child) != NULL) {
+ d->child = 0;
+ continue;
+ }
+ if (!(nd = d->next))
+ nd = d->parent;
+ freeDosDirEntry(d);
+ }
+ rootDir = lostDir = NULL;
+ free(buffer);
+ free(delbuf);
+ buffer = NULL;
+ delbuf = NULL;
+}
+
+/*
+ * Delete directory entries between startcl, startoff and endcl, endoff.
+ */
+static int
+delete(struct fat_descriptor *fat, cl_t startcl,
+ int startoff, cl_t endcl, int endoff, int notlast)
+{
+ u_char *s, *e;
+ off_t off;
+ int clsz, fd;
+ struct bootblock *boot;
+
+ boot = fat_get_boot(fat);
+ fd = fat_get_fd(fat);
+ clsz = boot->bpbSecPerClust * boot->bpbBytesPerSec;
+
+ s = delbuf + startoff;
+ e = delbuf + clsz;
+ while (fat_is_valid_cl(fat, startcl)) {
+ if (startcl == endcl) {
+ if (notlast)
+ break;
+ e = delbuf + endoff;
+ }
+ off = (startcl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
+
+ off *= boot->bpbBytesPerSec;
+ if (lseek(fd, off, SEEK_SET) != off) {
+ perr("Unable to lseek to %" PRId64, off);
+ return FSFATAL;
+ }
+ if (read(fd, delbuf, clsz) != clsz) {
+ perr("Unable to read directory");
+ return FSFATAL;
+ }
+ while (s < e) {
+ *s = SLOT_DELETED;
+ s += 32;
+ }
+ if (lseek(fd, off, SEEK_SET) != off) {
+ perr("Unable to lseek to %" PRId64, off);
+ return FSFATAL;
+ }
+ if (write(fd, delbuf, clsz) != clsz) {
+ perr("Unable to write directory");
+ return FSFATAL;
+ }
+ if (startcl == endcl)
+ break;
+ startcl = fat_get_cl_next(fat, startcl);
+ s = delbuf;
+ }
+ return FSOK;
+}
+
+static int
+removede(struct fat_descriptor *fat, u_char *start,
+ u_char *end, cl_t startcl, cl_t endcl, cl_t curcl,
+ char *path, int type)
+{
+ switch (type) {
+ case 0:
+ pwarn("Invalid long filename entry for %s\n", path);
+ break;
+ case 1:
+ pwarn("Invalid long filename entry at end of directory %s\n",
+ path);
+ break;
+ case 2:
+ pwarn("Invalid long filename entry for volume label\n");
+ break;
+ }
+ if (ask(0, "Remove")) {
+ if (startcl != curcl) {
+ if (delete(fat,
+ startcl, start - buffer,
+ endcl, end - buffer,
+ endcl == curcl) == FSFATAL)
+ return FSFATAL;
+ start = buffer;
+ }
+ /* startcl is < CLUST_FIRST for !FAT32 root */
+ if ((endcl == curcl) || (startcl < CLUST_FIRST))
+ for (; start < end; start += 32)
+ *start = SLOT_DELETED;
+ return FSDIRMOD;
+ }
+ return FSERROR;
+}
+
+/*
+ * Check an in-memory file entry
+ */
+static int
+checksize(struct fat_descriptor *fat, u_char *p, struct dosDirEntry *dir)
+{
+ int ret = FSOK;
+ size_t chainsize;
+ u_int64_t physicalSize;
+ struct bootblock *boot;
+
+ boot = fat_get_boot(fat);
+
+ /*
+ * Check size on ordinary files
+ */
+ if (dir->head == CLUST_FREE) {
+ physicalSize = 0;
+ } else {
+ if (!fat_is_valid_cl(fat, dir->head) || !fat_is_cl_head(fat, dir->head)) {
+ pwarn("Directory entry %s of size %u referencing invalid cluster %u\n",
+ fullpath(dir), dir->size, dir->head);
+ if (ask(1, "Truncate")) {
+ p[28] = p[29] = p[30] = p[31] = 0;
+ p[26] = p[27] = 0;
+ if (boot->ClustMask == CLUST32_MASK)
+ p[20] = p[21] = 0;
+ dir->size = 0;
+ dir->head = CLUST_FREE;
+ return FSDIRMOD;
+ } else {
+ return FSERROR;
+ }
+ }
+ ret = checkchain(fat, dir->head, &chainsize);
+ /*
+ * Upon return, chainsize would hold the chain length
+ * that checkchain() was able to validate, but if the user
+ * refused the proposed repair, it would be unsafe to
+ * proceed with directory entry fix, so bail out in that
+ * case.
+ */
+ if (ret == FSERROR) {
+ return (FSERROR);
+ }
+ /*
+ * The maximum file size on FAT32 is 4GiB - 1, which
+ * will occupy a cluster chain of exactly 4GiB in
+ * size. On 32-bit platforms, since size_t is 32-bit,
+ * it would wrap back to 0.
+ */
+ physicalSize = (u_int64_t)chainsize * boot->ClusterSize;
+ }
+ if (physicalSize < dir->size) {
+ pwarn("size of %s is %u, should at most be %ju\n",
+ fullpath(dir), dir->size, (uintmax_t)physicalSize);
+ if (ask(1, "Truncate")) {
+ dir->size = physicalSize;
+ p[28] = (u_char)physicalSize;
+ p[29] = (u_char)(physicalSize >> 8);
+ p[30] = (u_char)(physicalSize >> 16);
+ p[31] = (u_char)(physicalSize >> 24);
+ return FSDIRMOD;
+ } else
+ return FSERROR;
+ } else if (physicalSize - dir->size >= boot->ClusterSize) {
+ pwarn("%s has too many clusters allocated\n",
+ fullpath(dir));
+ if (ask(1, "Drop superfluous clusters")) {
+ cl_t cl;
+ u_int32_t sz, len;
+
+ for (cl = dir->head, len = sz = 0;
+ (sz += boot->ClusterSize) < dir->size; len++)
+ cl = fat_get_cl_next(fat, cl);
+ clearchain(fat, fat_get_cl_next(fat, cl));
+ ret = fat_set_cl_next(fat, cl, CLUST_EOF);
+ return (FSFATMOD | ret);
+ } else
+ return FSERROR;
+ }
+ return FSOK;
+}
+
+static const u_char dot_name[11] = ". ";
+static const u_char dotdot_name[11] = ".. ";
+
+/*
+ * Basic sanity check if the subdirectory have good '.' and '..' entries,
+ * and they are directory entries. Further sanity checks are performed
+ * when we traverse into it.
+ */
+static int
+check_subdirectory(struct fat_descriptor *fat, struct dosDirEntry *dir)
+{
+ u_char *buf, *cp;
+ off_t off;
+ cl_t cl;
+ int retval = FSOK;
+ int fd;
+ struct bootblock *boot;
+
+ boot = fat_get_boot(fat);
+ fd = fat_get_fd(fat);
+
+ cl = dir->head;
+ if (dir->parent && !fat_is_valid_cl(fat, cl)) {
+ return FSERROR;
+ }
+
+ if (!(boot->flags & FAT32) && !dir->parent) {
+ off = boot->bpbResSectors + boot->bpbFATs *
+ boot->FATsecs;
+ } else {
+ off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
+ }
+
+ /*
+ * We only need to check the first two entries of the directory,
+ * which is found in the first sector of the directory entry,
+ * so read in only the first sector.
+ */
+ buf = malloc(boot->bpbBytesPerSec);
+ if (buf == NULL) {
+ perr("No space for directory buffer (%u)",
+ boot->bpbBytesPerSec);
+ return FSFATAL;
+ }
+
+ off *= boot->bpbBytesPerSec;
+ if (lseek(fd, off, SEEK_SET) != off ||
+ read(fd, buf, boot->bpbBytesPerSec) != (ssize_t)boot->bpbBytesPerSec) {
+ perr("Unable to read directory");
+ free(buf);
+ return FSFATAL;
+ }
+
+ /*
+ * Both `.' and `..' must be present and be the first two entries
+ * and be ATTR_DIRECTORY of a valid subdirectory.
+ */
+ cp = buf;
+ if (memcmp(cp, dot_name, sizeof(dot_name)) != 0 ||
+ (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
+ pwarn("%s: Incorrect `.' for %s.\n", __func__, dir->name);
+ retval |= FSERROR;
+ }
+ cp += 32;
+ if (memcmp(cp, dotdot_name, sizeof(dotdot_name)) != 0 ||
+ (cp[11] & ATTR_DIRECTORY) != ATTR_DIRECTORY) {
+ pwarn("%s: Incorrect `..' for %s. \n", __func__, dir->name);
+ retval |= FSERROR;
+ }
+
+ free(buf);
+ return retval;
+}
+
+/*
+ * Read a directory and
+ * - resolve long name records
+ * - enter file and directory records into the parent's list
+ * - push directories onto the todo-stack
+ */
+static int
+readDosDirSection(struct fat_descriptor *fat, struct dosDirEntry *dir)
+{
+ struct bootblock *boot;
+ struct dosDirEntry dirent, *d;
+ u_char *p, *vallfn, *invlfn, *empty;
+ off_t off;
+ int fd, i, j, k, iosize, entries;
+ bool is_legacyroot;
+ cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
+ char *t;
+ u_int lidx = 0;
+ int shortSum;
+ int mod = FSOK;
+ size_t dirclusters;
+#define THISMOD 0x8000 /* Only used within this routine */
+
+ boot = fat_get_boot(fat);
+ fd = fat_get_fd(fat);
+
+ cl = dir->head;
+ if (dir->parent && (!fat_is_valid_cl(fat, cl))) {
+ /*
+ * Already handled somewhere else.
+ */
+ return FSOK;
+ }
+ shortSum = -1;
+ vallfn = invlfn = empty = NULL;
+
+ /*
+ * If we are checking the legacy root (for FAT12/FAT16),
+ * we will operate on the whole directory; otherwise, we
+ * will operate on one cluster at a time, and also take
+ * this opportunity to examine the chain.
+ *
+ * Derive how many entries we are going to encounter from
+ * the I/O size.
+ */
+ is_legacyroot = (dir->parent == NULL && !(boot->flags & FAT32));
+ if (is_legacyroot) {
+ iosize = boot->bpbRootDirEnts * 32;
+ entries = boot->bpbRootDirEnts;
+ } else {
+ iosize = boot->bpbSecPerClust * boot->bpbBytesPerSec;
+ entries = iosize / 32;
+ mod |= checkchain(fat, dir->head, &dirclusters);
+ }
+
+ do {
+ if (is_legacyroot) {
+ /*
+ * Special case for FAT12/FAT16 root -- read
+ * in the whole root directory.
+ */
+ off = boot->bpbResSectors + boot->bpbFATs *
+ boot->FATsecs;
+ } else {
+ /*
+ * Otherwise, read in a cluster of the
+ * directory.
+ */
+ off = (cl - CLUST_FIRST) * boot->bpbSecPerClust + boot->FirstCluster;
+ }
+
+ off *= boot->bpbBytesPerSec;
+ if (lseek(fd, off, SEEK_SET) != off ||
+ read(fd, buffer, iosize) != iosize) {
+ perr("Unable to read directory");
+ return FSFATAL;
+ }
+
+ for (p = buffer, i = 0; i < entries; i++, p += 32) {
+ if (dir->fsckflags & DIREMPWARN) {
+ *p = SLOT_EMPTY;
+ continue;
+ }
+
+ if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
+ if (*p == SLOT_EMPTY) {
+ dir->fsckflags |= DIREMPTY;
+ empty = p;
+ empcl = cl;
+ }
+ continue;
+ }
+
+ if (dir->fsckflags & DIREMPTY) {
+ if (!(dir->fsckflags & DIREMPWARN)) {
+ pwarn("%s has entries after end of directory\n",
+ fullpath(dir));
+ if (ask(1, "Extend")) {
+ u_char *q;
+
+ dir->fsckflags &= ~DIREMPTY;
+ if (delete(fat,
+ empcl, empty - buffer,
+ cl, p - buffer, 1) == FSFATAL)
+ return FSFATAL;
+ q = ((empcl == cl) ? empty : buffer);
+ assert(q != NULL);
+ for (; q < p; q += 32)
+ *q = SLOT_DELETED;
+ mod |= THISMOD|FSDIRMOD;
+ } else if (ask(0, "Truncate"))
+ dir->fsckflags |= DIREMPWARN;
+ }
+ if (dir->fsckflags & DIREMPWARN) {
+ *p = SLOT_DELETED;
+ mod |= THISMOD|FSDIRMOD;
+ continue;
+ } else if (dir->fsckflags & DIREMPTY)
+ mod |= FSERROR;
+ empty = NULL;
+ }
+
+ if (p[11] == ATTR_WIN95) {
+ if (*p & LRFIRST) {
+ if (shortSum != -1) {
+ if (!invlfn) {
+ invlfn = vallfn;
+ invcl = valcl;
+ }
+ }
+ memset(longName, 0, sizeof longName);
+ shortSum = p[13];
+ vallfn = p;
+ valcl = cl;
+ } else if (shortSum != p[13]
+ || lidx != (*p & LRNOMASK)) {
+ if (!invlfn) {
+ invlfn = vallfn;
+ invcl = valcl;
+ }
+ if (!invlfn) {
+ invlfn = p;
+ invcl = cl;
+ }
+ vallfn = NULL;
+ }
+ lidx = *p & LRNOMASK;
+ if (lidx == 0) {
+ pwarn("invalid long name\n");
+ if (!invlfn) {
+ invlfn = vallfn;
+ invcl = valcl;
+ }
+ vallfn = NULL;
+ continue;
+ }
+ t = longName + --lidx * 13;
+ for (k = 1; k < 11 && t < longName +
+ sizeof(longName); k += 2) {
+ if (!p[k] && !p[k + 1])
+ break;
+ *t++ = p[k];
+ /*
+ * Warn about those unusable chars in msdosfs here? XXX
+ */
+ if (p[k + 1])
+ t[-1] = '?';
+ }
+ if (k >= 11)
+ for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
+ if (!p[k] && !p[k + 1])
+ break;
+ *t++ = p[k];
+ if (p[k + 1])
+ t[-1] = '?';
+ }
+ if (k >= 26)
+ for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
+ if (!p[k] && !p[k + 1])
+ break;
+ *t++ = p[k];
+ if (p[k + 1])
+ t[-1] = '?';
+ }
+ if (t >= longName + sizeof(longName)) {
+ pwarn("long filename too long\n");
+ if (!invlfn) {
+ invlfn = vallfn;
+ invcl = valcl;
+ }
+ vallfn = NULL;
+ }
+ if (p[26] | (p[27] << 8)) {
+ pwarn("long filename record cluster start != 0\n");
+ if (!invlfn) {
+ invlfn = vallfn;
+ invcl = cl;
+ }
+ vallfn = NULL;
+ }
+ continue; /* long records don't carry further
+ * information */
+ }
+
+ /*
+ * This is a standard msdosfs directory entry.
+ */
+ memset(&dirent, 0, sizeof dirent);
+
+ /*
+ * it's a short name record, but we need to know
+ * more, so get the flags first.
+ */
+ dirent.flags = p[11];
+
+ /*
+ * Translate from 850 to ISO here XXX
+ */
+ for (j = 0; j < 8; j++)
+ dirent.name[j] = p[j];
+ dirent.name[8] = '\0';
+ for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
+ dirent.name[k] = '\0';
+ if (k < 0 || dirent.name[k] != '\0')
+ k++;
+ if (dirent.name[0] == SLOT_E5)
+ dirent.name[0] = 0xe5;
+
+ if (dirent.flags & ATTR_VOLUME) {
+ if (vallfn || invlfn) {
+ mod |= removede(fat,
+ invlfn ? invlfn : vallfn, p,
+ invlfn ? invcl : valcl, -1, 0,
+ fullpath(dir), 2);
+ vallfn = NULL;
+ invlfn = NULL;
+ }
+ continue;
+ }
+
+ if (p[8] != ' ')
+ dirent.name[k++] = '.';
+ for (j = 0; j < 3; j++)
+ dirent.name[k++] = p[j+8];
+ dirent.name[k] = '\0';
+ for (k--; k >= 0 && dirent.name[k] == ' '; k--)
+ dirent.name[k] = '\0';
+
+ if (vallfn && shortSum != calcShortSum(p)) {
+ if (!invlfn) {
+ invlfn = vallfn;
+ invcl = valcl;
+ }
+ vallfn = NULL;
+ }
+ dirent.head = p[26] | (p[27] << 8);
+ if (boot->ClustMask == CLUST32_MASK)
+ dirent.head |= (p[20] << 16) | (p[21] << 24);
+ dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
+ if (vallfn) {
+ strlcpy(dirent.lname, longName,
+ sizeof(dirent.lname));
+ longName[0] = '\0';
+ shortSum = -1;
+ }
+
+ dirent.parent = dir;
+ dirent.next = dir->child;
+
+ if (invlfn) {
+ mod |= k = removede(fat,
+ invlfn, vallfn ? vallfn : p,
+ invcl, vallfn ? valcl : cl, cl,
+ fullpath(&dirent), 0);
+ if (mod & FSFATAL)
+ return FSFATAL;
+ if (vallfn
+ ? (valcl == cl && vallfn != buffer)
+ : p != buffer)
+ if (k & FSDIRMOD)
+ mod |= THISMOD;
+ }
+
+ vallfn = NULL; /* not used any longer */
+ invlfn = NULL;
+
+ /*
+ * Check if the directory entry is sane.
+ *
+ * '.' and '..' are skipped, their sanity is
+ * checked somewhere else.
+ *
+ * For everything else, check if we have a new,
+ * valid cluster chain (beginning of a file or
+ * directory that was never previously claimed
+ * by another file) when it's a non-empty file
+ * or a directory. The sanity of the cluster
+ * chain is checked at a later time when we
+ * traverse into the directory, or examine the
+ * file's directory entry.
+ *
+ * The only possible fix is to delete the entry
+ * if it's a directory; for file, we have to
+ * truncate the size to 0.
+ */
+ if (!(dirent.flags & ATTR_DIRECTORY) ||
+ (strcmp(dirent.name, ".") != 0 &&
+ strcmp(dirent.name, "..") != 0)) {
+ if ((dirent.size != 0 || (dirent.flags & ATTR_DIRECTORY)) &&
+ ((!fat_is_valid_cl(fat, dirent.head) ||
+ !fat_is_cl_head(fat, dirent.head)))) {
+ if (!fat_is_valid_cl(fat, dirent.head)) {
+ pwarn("%s starts with cluster out of range(%u)\n",
+ fullpath(&dirent),
+ dirent.head);
+ } else {
+ pwarn("%s doesn't start a new cluster chain\n",
+ fullpath(&dirent));
+ }
+
+ if (dirent.flags & ATTR_DIRECTORY) {
+ if (ask(0, "Remove")) {
+ *p = SLOT_DELETED;
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ continue;
+ } else {
+ if (ask(1, "Truncate")) {
+ p[28] = p[29] = p[30] = p[31] = 0;
+ p[26] = p[27] = 0;
+ if (boot->ClustMask == CLUST32_MASK)
+ p[20] = p[21] = 0;
+ dirent.size = 0;
+ dirent.head = 0;
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ }
+ }
+ }
+ if (dirent.flags & ATTR_DIRECTORY) {
+ /*
+ * gather more info for directories
+ */
+ struct dirTodoNode *n;
+
+ if (dirent.size) {
+ pwarn("Directory %s has size != 0\n",
+ fullpath(&dirent));
+ if (ask(1, "Correct")) {
+ p[28] = p[29] = p[30] = p[31] = 0;
+ dirent.size = 0;
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ }
+ /*
+ * handle `.' and `..' specially
+ */
+ if (strcmp(dirent.name, ".") == 0) {
+ if (dirent.head != dir->head) {
+ pwarn("`.' entry in %s has incorrect start cluster\n",
+ fullpath(dir));
+ if (ask(1, "Correct")) {
+ dirent.head = dir->head;
+ p[26] = (u_char)dirent.head;
+ p[27] = (u_char)(dirent.head >> 8);
+ if (boot->ClustMask == CLUST32_MASK) {
+ p[20] = (u_char)(dirent.head >> 16);
+ p[21] = (u_char)(dirent.head >> 24);
+ }
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ }
+ continue;
+ } else if (strcmp(dirent.name, "..") == 0) {
+ if (dir->parent) { /* XXX */
+ if (!dir->parent->parent) {
+ if (dirent.head) {
+ pwarn("`..' entry in %s has non-zero start cluster\n",
+ fullpath(dir));
+ if (ask(1, "Correct")) {
+ dirent.head = 0;
+ p[26] = p[27] = 0;
+ if (boot->ClustMask == CLUST32_MASK)
+ p[20] = p[21] = 0;
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ }
+ } else if (dirent.head != dir->parent->head) {
+ pwarn("`..' entry in %s has incorrect start cluster\n",
+ fullpath(dir));
+ if (ask(1, "Correct")) {
+ dirent.head = dir->parent->head;
+ p[26] = (u_char)dirent.head;
+ p[27] = (u_char)(dirent.head >> 8);
+ if (boot->ClustMask == CLUST32_MASK) {
+ p[20] = (u_char)(dirent.head >> 16);
+ p[21] = (u_char)(dirent.head >> 24);
+ }
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ }
+ }
+ continue;
+ } else {
+ /*
+ * Only one directory entry can point
+ * to dir->head, it's '.'.
+ */
+ if (dirent.head == dir->head) {
+ pwarn("%s entry in %s has incorrect start cluster\n",
+ dirent.name, fullpath(dir));
+ if (ask(1, "Remove")) {
+ *p = SLOT_DELETED;
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ continue;
+ } else if ((check_subdirectory(fat,
+ &dirent) & FSERROR) == FSERROR) {
+ /*
+ * A subdirectory should have
+ * a dot (.) entry and a dot-dot
+ * (..) entry of ATTR_DIRECTORY,
+ * we will inspect further when
+ * traversing into it.
+ */
+ if (ask(1, "Remove")) {
+ *p = SLOT_DELETED;
+ mod |= THISMOD|FSDIRMOD;
+ } else
+ mod |= FSERROR;
+ continue;
+ }
+ }
+
+ /* create directory tree node */
+ if (!(d = newDosDirEntry())) {
+ perr("No space for directory");
+ return FSFATAL;
+ }
+ memcpy(d, &dirent, sizeof(struct dosDirEntry));
+ /* link it into the tree */
+ dir->child = d;
+
+ /* Enter this directory into the todo list */
+ if (!(n = newDirTodo())) {
+ perr("No space for todo list");
+ return FSFATAL;
+ }
+ n->next = pendingDirectories;
+ n->dir = d;
+ pendingDirectories = n;
+ } else if (!(mod & FSERROR)) {
+ mod |= k = checksize(fat, p, &dirent);
+ if (k & FSDIRMOD)
+ mod |= THISMOD;
+ }
+ boot->NumFiles++;
+ }
+
+ if (is_legacyroot) {
+ /*
+ * Don't bother to write back right now because
+ * we may continue to make modification to the
+ * non-FAT32 root directory below.
+ */
+ break;
+ } else if (mod & THISMOD) {
+ if (lseek(fd, off, SEEK_SET) != off
+ || write(fd, buffer, iosize) != iosize) {
+ perr("Unable to write directory");
+ return FSFATAL;
+ }
+ mod &= ~THISMOD;
+ }
+ } while (fat_is_valid_cl(fat, (cl = fat_get_cl_next(fat, cl))));
+ if (invlfn || vallfn)
+ mod |= removede(fat,
+ invlfn ? invlfn : vallfn, p,
+ invlfn ? invcl : valcl, -1, 0,
+ fullpath(dir), 1);
+
+ /*
+ * The root directory of non-FAT32 filesystems is in a special
+ * area and may have been modified above removede() without
+ * being written out.
+ */
+ if ((mod & FSDIRMOD) && is_legacyroot) {
+ if (lseek(fd, off, SEEK_SET) != off
+ || write(fd, buffer, iosize) != iosize) {
+ perr("Unable to write directory");
+ return FSFATAL;
+ }
+ mod &= ~THISMOD;
+ }
+ return mod & ~THISMOD;
+}
+
+int
+handleDirTree(struct fat_descriptor *fat)
+{
+ int mod;
+
+ mod = readDosDirSection(fat, rootDir);
+ if (mod & FSFATAL)
+ return FSFATAL;
+
+ /*
+ * process the directory todo list
+ */
+ while (pendingDirectories) {
+ struct dosDirEntry *dir = pendingDirectories->dir;
+ struct dirTodoNode *n = pendingDirectories->next;
+
+ /*
+ * remove TODO entry now, the list might change during
+ * directory reads
+ */
+ freeDirTodo(pendingDirectories);
+ pendingDirectories = n;
+
+ /*
+ * handle subdirectory
+ */
+ mod |= readDosDirSection(fat, dir);
+ if (mod & FSFATAL)
+ return FSFATAL;
+ }
+
+ return mod;
+}
+
+/*
+ * Try to reconnect a FAT chain into dir
+ */
+static u_char *lfbuf;
+static cl_t lfcl;
+static off_t lfoff;
+
+int
+reconnect(struct fat_descriptor *fat, cl_t head, size_t length)
+{
+ struct bootblock *boot = fat_get_boot(fat);
+ struct dosDirEntry d;
+ int len, dosfs;
+ u_char *p;
+
+ dosfs = fat_get_fd(fat);
+
+ if (!ask(1, "Reconnect"))
+ return FSERROR;
+
+ if (!lostDir) {
+ for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
+ if (!strcmp(lostDir->name, LOSTDIR))
+ break;
+ }
+ if (!lostDir) { /* Create LOSTDIR? XXX */
+ pwarn("No %s directory\n", LOSTDIR);
+ return FSERROR;
+ }
+ }
+ if (!lfbuf) {
+ lfbuf = malloc(boot->ClusterSize);
+ if (!lfbuf) {
+ perr("No space for buffer");
+ return FSFATAL;
+ }
+ p = NULL;
+ } else
+ p = lfbuf;
+ while (1) {
+ if (p)
+ for (; p < lfbuf + boot->ClusterSize; p += 32)
+ if (*p == SLOT_EMPTY
+ || *p == SLOT_DELETED)
+ break;
+ if (p && p < lfbuf + boot->ClusterSize)
+ break;
+ lfcl = p ? fat_get_cl_next(fat, lfcl) : lostDir->head;
+ if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
+ /* Extend LOSTDIR? XXX */
+ pwarn("No space in %s\n", LOSTDIR);
+ lfcl = (lostDir->head < boot->NumClusters) ? lostDir->head : 0;
+ return FSERROR;
+ }
+ lfoff = (lfcl - CLUST_FIRST) * boot->ClusterSize
+ + boot->FirstCluster * boot->bpbBytesPerSec;
+
+ if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
+ || (size_t)read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
+ perr("could not read LOST.DIR");
+ return FSFATAL;
+ }
+ p = lfbuf;
+ }
+
+ boot->NumFiles++;
+ /* Ensure uniqueness of entry here! XXX */
+ memset(&d, 0, sizeof d);
+ /* worst case -1 = 4294967295, 10 digits */
+ len = snprintf(d.name, sizeof(d.name), "%u", head);
+ d.flags = 0;
+ d.head = head;
+ d.size = length * boot->ClusterSize;
+
+ memcpy(p, d.name, len);
+ memset(p + len, ' ', 11 - len);
+ memset(p + 11, 0, 32 - 11);
+ p[26] = (u_char)d.head;
+ p[27] = (u_char)(d.head >> 8);
+ if (boot->ClustMask == CLUST32_MASK) {
+ p[20] = (u_char)(d.head >> 16);
+ p[21] = (u_char)(d.head >> 24);
+ }
+ p[28] = (u_char)d.size;
+ p[29] = (u_char)(d.size >> 8);
+ p[30] = (u_char)(d.size >> 16);
+ p[31] = (u_char)(d.size >> 24);
+ if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
+ || (size_t)write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
+ perr("could not write LOST.DIR");
+ return FSFATAL;
+ }
+ return FSDIRMOD;
+}
+
+void
+finishlf(void)
+{
+ if (lfbuf)
+ free(lfbuf);
+ lfbuf = NULL;
+}
diff --git a/cmd/extra/fsck.fat/dosfs.h b/cmd/extra/fsck.fat/dosfs.h
@@ -0,0 +1,132 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ * Some structure declaration borrowed from Paul Popelka
+ * (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
+ *
+ * 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 ``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 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.
+ * $NetBSD: dosfs.h,v 1.4 1997/01/03 14:32:48 ws Exp $
+ */
+
+#ifndef DOSFS_H
+#define DOSFS_H
+
+/* support 4Kn disk reads */
+#define DOSBOOTBLOCKSIZE_REAL 512
+#define DOSBOOTBLOCKSIZE 4096
+
+typedef u_int32_t cl_t; /* type holding a cluster number */
+
+/*
+ * architecture independent description of all the info stored in a
+ * FAT boot block.
+ */
+struct bootblock {
+ u_int bpbBytesPerSec; /* bytes per sector */
+ u_int bpbSecPerClust; /* sectors per cluster */
+ u_int bpbResSectors; /* number of reserved sectors */
+ u_int bpbFATs; /* number of bpbFATs */
+ u_int bpbRootDirEnts; /* number of root directory entries */
+ u_int32_t bpbSectors; /* total number of sectors */
+ u_int bpbMedia; /* media descriptor */
+ u_int bpbFATsmall; /* number of sectors per FAT */
+ u_int SecPerTrack; /* sectors per track */
+ u_int bpbHeads; /* number of heads */
+ u_int32_t bpbHiddenSecs; /* # of hidden sectors */
+ u_int32_t bpbHugeSectors; /* # of sectors if bpbbpbSectors == 0 */
+ cl_t bpbRootClust; /* Start of Root Directory */
+ u_int bpbFSInfo; /* FSInfo sector */
+ u_int bpbBackup; /* Backup of Bootblocks */
+ cl_t FSFree; /* Number of free clusters acc. FSInfo */
+ cl_t FSNext; /* Next free cluster acc. FSInfo */
+
+ /* and some more calculated values */
+ u_int flags; /* some flags: */
+#define FAT32 1 /* this is a FAT32 file system */
+ /*
+ * Maybe, we should separate out
+ * various parts of FAT32? XXX
+ */
+ int ValidFat; /* valid fat if FAT32 non-mirrored */
+ cl_t ClustMask; /* mask for entries in FAT */
+ cl_t NumClusters; /* # of entries in a FAT */
+ u_int32_t NumSectors; /* how many sectors are there */
+ u_int32_t FATsecs; /* how many sectors are in FAT */
+ u_int32_t NumFatEntries; /* how many entries really are there */
+ u_int FirstCluster; /* at what sector is Cluster CLUST_FIRST */
+ u_int ClusterSize; /* Cluster size in bytes */
+
+ /* Now some statistics: */
+ u_int NumFiles; /* # of plain files */
+ u_int NumFree; /* # of free clusters */
+ u_int NumBad; /* # of bad clusters */
+};
+
+#define CLUST_FREE 0 /* 0 means cluster is free */
+#define CLUST_FIRST 2 /* 2 is the minimum valid cluster number */
+#define CLUST_RSRVD 0xfffffff6 /* start of reserved clusters */
+#define CLUST_BAD 0xfffffff7 /* a cluster with a defect */
+#define CLUST_EOFS 0xfffffff8 /* start of EOF indicators */
+#define CLUST_EOF 0xffffffff /* standard value for last cluster */
+#define CLUST_DEAD 0xfdeadc0d /* error encountered */
+
+/*
+ * Masks for cluster values
+ */
+#define CLUST12_MASK 0xfff
+#define CLUST16_MASK 0xffff
+#define CLUST32_MASK 0xfffffff
+
+#define DOSLONGNAMELEN 256 /* long name maximal length */
+#define LRFIRST 0x40 /* first long name record */
+#define LRNOMASK 0x1f /* mask to extract long record
+ * sequence number */
+
+/*
+ * Architecture independent description of a directory entry
+ */
+struct dosDirEntry {
+ struct dosDirEntry
+ *parent, /* previous tree level */
+ *next, /* next brother */
+ *child; /* if this is a directory */
+ char name[8+1+3+1]; /* alias name first part */
+ char lname[DOSLONGNAMELEN]; /* real name */
+ uint flags; /* attributes */
+ cl_t head; /* cluster no */
+ u_int32_t size; /* filesize in bytes */
+ uint fsckflags; /* flags during fsck */
+};
+/* Flags in fsckflags: */
+#define DIREMPTY 1
+#define DIREMPWARN 2
+
+/*
+ * TODO-list of unread directories
+ */
+struct dirTodoNode {
+ struct dosDirEntry *dir;
+ struct dirTodoNode *next;
+};
+
+#endif
diff --git a/cmd/extra/fsck.fat/ext.h b/cmd/extra/fsck.fat/ext.h
@@ -0,0 +1,153 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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.
+ * $NetBSD: ext.h,v 1.6 2000/04/25 23:02:51 jdolecek Exp $
+ */
+
+#ifndef EXT_H
+#define EXT_H
+
+#include <sys/types.h>
+
+#include <stdbool.h>
+
+#include "dosfs.h"
+
+#define LOSTDIR "LOST.DIR"
+
+/*
+ * Options:
+ */
+extern int alwaysno; /* assume "no" for all questions */
+extern int alwaysyes; /* assume "yes" for all questions */
+extern int preen; /* we are preening */
+extern int rdonly; /* device is opened read only (supersedes above) */
+extern int skipclean; /* skip clean file systems if preening */
+extern int allow_mmap; /* allow the use of mmap() */
+
+/*
+ * function declarations
+ */
+int ask(int, const char *, ...) __printflike(2, 3);
+
+/*
+ * Check the dirty flag. If the file system is clean, then return 1.
+ * Otherwise, return 0 (this includes the case of FAT12 file systems --
+ * they have no dirty flag, so they must be assumed to be unclean).
+ */
+int checkdirty(int, struct bootblock *);
+
+/*
+ * Check file system given as arg
+ */
+int checkfilesys(const char *);
+
+/*
+ * Return values of various functions
+ */
+#define FSOK 0 /* Check was OK */
+#define FSBOOTMOD 1 /* Boot block was modified */
+#define FSDIRMOD 2 /* Some directory was modified */
+#define FSFATMOD 4 /* The FAT was modified */
+#define FSERROR 8 /* Some unrecovered error remains */
+#define FSFATAL 16 /* Some unrecoverable error occurred */
+#define FSDIRTY 32 /* File system is dirty */
+
+/*
+ * read a boot block in a machine independent fashion and translate
+ * it into our struct bootblock.
+ */
+int readboot(int, struct bootblock *);
+
+/*
+ * Correct the FSInfo block.
+ */
+int writefsinfo(int, struct bootblock *);
+
+/* Opaque type */
+struct fat_descriptor;
+
+int cleardirty(struct fat_descriptor *);
+
+void fat_clear_cl_head(struct fat_descriptor *, cl_t);
+bool fat_is_cl_head(struct fat_descriptor *, cl_t);
+
+cl_t fat_get_cl_next(struct fat_descriptor *, cl_t);
+
+int fat_set_cl_next(struct fat_descriptor *, cl_t, cl_t);
+
+cl_t fat_allocate_cluster(struct fat_descriptor *fat);
+
+struct bootblock* fat_get_boot(struct fat_descriptor *);
+int fat_get_fd(struct fat_descriptor *);
+bool fat_is_valid_cl(struct fat_descriptor *, cl_t);
+
+/*
+ * Read the FAT 0 and return a pointer to the newly allocated
+ * descriptor of it.
+ */
+int readfat(int, struct bootblock *, struct fat_descriptor **);
+
+/*
+ * Write back FAT entries
+ */
+int writefat(struct fat_descriptor *);
+
+/*
+ * Read a directory
+ */
+int resetDosDirSection(struct fat_descriptor *);
+void finishDosDirSection(void);
+int handleDirTree(struct fat_descriptor *);
+
+/*
+ * Cross-check routines run after everything is completely in memory
+ */
+int checkchain(struct fat_descriptor *, cl_t, size_t *);
+
+/*
+ * Check for lost cluster chains
+ */
+int checklost(struct fat_descriptor *);
+/*
+ * Try to reconnect a lost cluster chain
+ */
+int reconnect(struct fat_descriptor *, cl_t, size_t);
+void finishlf(void);
+
+/*
+ * Small helper functions
+ */
+/*
+ * Return the type of a reserved cluster as text
+ */
+const char *rsrvdcltype(cl_t);
+
+/*
+ * Clear a cluster chain in a FAT
+ */
+void clearchain(struct fat_descriptor *, cl_t);
+
+#endif
diff --git a/cmd/extra/fsck.fat/fat.c b/cmd/extra/fsck.fat/fat.c
@@ -0,0 +1,1326 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Google LLC
+ * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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 "freebsd-compat.h"
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $");
+#endif /* not lint */
+
+#include "queue.h"
+
+#include <limits.h>
+#include <sys/mman.h>
+#include <sys/param.h>
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "ext.h"
+#include "fsutil.h"
+
+static int _readfat(struct fat_descriptor *);
+static inline struct bootblock* boot_of_(struct fat_descriptor *);
+static inline int fd_of_(struct fat_descriptor *);
+static inline bool valid_cl(struct fat_descriptor *, cl_t);
+
+
+/*
+ * Head bitmap for FAT scanning.
+ *
+ * FAT32 have up to 2^28 = 256M entries, and FAT16/12 have much less.
+ * For each cluster, we use 1 bit to represent if it's a head cluster
+ * (the first cluster of a cluster chain).
+ *
+ * Head bitmap
+ * ===========
+ * Initially, we set all bits to 1. In readfat(), we traverse the
+ * whole FAT and mark each cluster identified as "next" cluster as
+ * 0. After the scan, we have a bitmap with 1's to indicate the
+ * corresponding cluster was a "head" cluster.
+ *
+ * We use head bitmap to identify lost chains: a head cluster that was
+ * not being claimed by any file or directories is the head cluster of
+ * a lost chain.
+ *
+ * Handle of lost chains
+ * =====================
+ * At the end of scanning, we can easily find all lost chain's heads
+ * by finding out the 1's in the head bitmap.
+ */
+
+typedef struct long_bitmap {
+ unsigned long *map;
+ size_t count; /* Total set bits in the map */
+} long_bitmap_t;
+
+static inline void
+bitmap_clear(long_bitmap_t *lbp, cl_t cl)
+{
+ cl_t i = cl / LONG_BIT;
+ unsigned long clearmask = ~(1UL << (cl % LONG_BIT));
+
+ assert((lbp->map[i] & ~clearmask) != 0);
+ lbp->map[i] &= clearmask;
+ lbp->count--;
+}
+
+static inline bool
+bitmap_get(long_bitmap_t *lbp, cl_t cl)
+{
+ cl_t i = cl / LONG_BIT;
+ unsigned long usedbit = 1UL << (cl % LONG_BIT);
+
+ return ((lbp->map[i] & usedbit) == usedbit);
+}
+
+static inline bool
+bitmap_none_in_range(long_bitmap_t *lbp, cl_t cl)
+{
+ cl_t i = cl / LONG_BIT;
+
+ return (lbp->map[i] == 0);
+}
+
+static inline size_t
+bitmap_count(long_bitmap_t *lbp)
+{
+ return (lbp->count);
+}
+
+static int
+bitmap_ctor(long_bitmap_t *lbp, size_t bits, bool allone)
+{
+ size_t bitmap_size = roundup2(bits, LONG_BIT) / (LONG_BIT / 8);
+
+ free(lbp->map);
+ lbp->map = calloc(1, bitmap_size);
+ if (lbp->map == NULL)
+ return FSFATAL;
+
+ if (allone) {
+ memset(lbp->map, 0xff, bitmap_size);
+ lbp->count = bits;
+ } else {
+ lbp->count = 0;
+ }
+ return FSOK;
+}
+
+static void
+bitmap_dtor(long_bitmap_t *lbp)
+{
+ free(lbp->map);
+ lbp->map = NULL;
+}
+
+/*
+ * FAT32 can be as big as 256MiB (2^26 entries * 4 bytes), when we
+ * can not ask the kernel to manage the access, use a simple LRU
+ * cache with chunk size of 128 KiB to manage it.
+ */
+struct fat32_cache_entry {
+ TAILQ_ENTRY(fat32_cache_entry) entries;
+ uint8_t *chunk; /* pointer to chunk */
+ off_t addr; /* offset */
+ bool dirty; /* dirty bit */
+};
+
+static const size_t fat32_cache_chunk_size = 131072; /* MAXPHYS */
+static const size_t fat32_cache_size = 4194304;
+static const size_t fat32_cache_entries = 32; /* XXXgcc: cache_size / cache_chunk_size */
+
+/*
+ * FAT table descriptor, represents a FAT table that is already loaded
+ * into memory.
+ */
+struct fat_descriptor {
+ struct bootblock *boot;
+ uint8_t *fatbuf;
+ cl_t (*get)(struct fat_descriptor *, cl_t);
+ int (*set)(struct fat_descriptor *, cl_t, cl_t);
+ long_bitmap_t headbitmap;
+ int fd;
+ bool is_mmapped;
+ bool use_cache;
+ size_t fatsize;
+
+ size_t fat32_cached_chunks;
+ TAILQ_HEAD(cachehead, fat32_cache_entry) fat32_cache_head;
+ struct fat32_cache_entry *fat32_cache_allentries;
+ off_t fat32_offset;
+ off_t fat32_lastaddr;
+};
+
+void
+fat_clear_cl_head(struct fat_descriptor *fat, cl_t cl)
+{
+ bitmap_clear(&fat->headbitmap, cl);
+}
+
+bool
+fat_is_cl_head(struct fat_descriptor *fat, cl_t cl)
+{
+ return (bitmap_get(&fat->headbitmap, cl));
+}
+
+static inline bool
+fat_is_cl_head_in_range(struct fat_descriptor *fat, cl_t cl)
+{
+ return (!(bitmap_none_in_range(&fat->headbitmap, cl)));
+}
+
+static size_t
+fat_get_head_count(struct fat_descriptor *fat)
+{
+ return (bitmap_count(&fat->headbitmap));
+}
+
+/*
+ * FAT12 accessors.
+ *
+ * FAT12s are sufficiently small, expect it to always fit in the RAM.
+ */
+static inline uint8_t *
+fat_get_fat12_ptr(struct fat_descriptor *fat, cl_t cl)
+{
+ return (fat->fatbuf + ((cl + (cl >> 1))));
+}
+
+static cl_t
+fat_get_fat12_next(struct fat_descriptor *fat, cl_t cl)
+{
+ const uint8_t *p;
+ cl_t retval;
+
+ p = fat_get_fat12_ptr(fat, cl);
+ retval = le16dec(p);
+ /* Odd cluster: lower 4 bits belongs to the subsequent cluster */
+ if ((cl & 1) == 1)
+ retval >>= 4;
+ retval &= CLUST12_MASK;
+
+ if (retval >= (CLUST_BAD & CLUST12_MASK))
+ retval |= ~CLUST12_MASK;
+
+ return (retval);
+}
+
+static int
+fat_set_fat12_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
+{
+ uint8_t *p;
+
+ /* Truncate 'nextcl' value, if needed */
+ nextcl &= CLUST12_MASK;
+
+ p = fat_get_fat12_ptr(fat, cl);
+
+ /*
+ * Read in the 4 bits from the subsequent (for even clusters)
+ * or the preceding (for odd clusters) cluster and combine
+ * it to the nextcl value for encoding
+ */
+ if ((cl & 1) == 0) {
+ nextcl |= ((p[1] & 0xf0) << 8);
+ } else {
+ nextcl <<= 4;
+ nextcl |= (p[0] & 0x0f);
+ }
+
+ le16enc(p, (uint16_t)nextcl);
+
+ return (0);
+}
+
+/*
+ * FAT16 accessors.
+ *
+ * FAT16s are sufficiently small, expect it to always fit in the RAM.
+ */
+static inline uint8_t *
+fat_get_fat16_ptr(struct fat_descriptor *fat, cl_t cl)
+{
+ return (fat->fatbuf + (cl << 1));
+}
+
+static cl_t
+fat_get_fat16_next(struct fat_descriptor *fat, cl_t cl)
+{
+ const uint8_t *p;
+ cl_t retval;
+
+ p = fat_get_fat16_ptr(fat, cl);
+ retval = le16dec(p) & CLUST16_MASK;
+
+ if (retval >= (CLUST_BAD & CLUST16_MASK))
+ retval |= ~CLUST16_MASK;
+
+ return (retval);
+}
+
+static int
+fat_set_fat16_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
+{
+ uint8_t *p;
+
+ /* Truncate 'nextcl' value, if needed */
+ nextcl &= CLUST16_MASK;
+
+ p = fat_get_fat16_ptr(fat, cl);
+
+ le16enc(p, (uint16_t)nextcl);
+
+ return (0);
+}
+
+/*
+ * FAT32 accessors.
+ */
+static inline uint8_t *
+fat_get_fat32_ptr(struct fat_descriptor *fat, cl_t cl)
+{
+ return (fat->fatbuf + (cl << 2));
+}
+
+static cl_t
+fat_get_fat32_next(struct fat_descriptor *fat, cl_t cl)
+{
+ const uint8_t *p;
+ cl_t retval;
+
+ p = fat_get_fat32_ptr(fat, cl);
+ retval = le32dec(p) & CLUST32_MASK;
+
+ if (retval >= (CLUST_BAD & CLUST32_MASK))
+ retval |= ~CLUST32_MASK;
+
+ return (retval);
+}
+
+static int
+fat_set_fat32_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
+{
+ uint8_t *p;
+
+ /* Truncate 'nextcl' value, if needed */
+ nextcl &= CLUST32_MASK;
+
+ p = fat_get_fat32_ptr(fat, cl);
+
+ le32enc(p, (uint32_t)nextcl);
+
+ return (0);
+}
+
+static inline size_t
+fat_get_iosize(struct fat_descriptor *fat, off_t address)
+{
+
+ if (address == fat->fat32_lastaddr) {
+ return (fat->fatsize & ((off_t)fat32_cache_chunk_size - 1));
+ } else {
+ return (fat32_cache_chunk_size);
+ }
+}
+
+static int
+fat_flush_fat32_cache_entry(struct fat_descriptor *fat,
+ struct fat32_cache_entry *entry)
+{
+ int fd;
+ off_t fat_addr;
+ size_t writesize;
+
+ fd = fd_of_(fat);
+
+ if (!entry->dirty)
+ return (FSOK);
+
+ writesize = fat_get_iosize(fat, entry->addr);
+
+ fat_addr = fat->fat32_offset + entry->addr;
+ if (lseek(fd, fat_addr, SEEK_SET) != fat_addr ||
+ (size_t)write(fd, entry->chunk, writesize) != writesize) {
+ pfatal("Unable to write FAT");
+ return (FSFATAL);
+ }
+
+ entry->dirty = false;
+ return (FSOK);
+}
+
+static struct fat32_cache_entry *
+fat_get_fat32_cache_entry(struct fat_descriptor *fat, off_t addr,
+ bool writing)
+{
+ int fd;
+ struct fat32_cache_entry *entry, *first;
+ off_t fat_addr;
+ size_t rwsize;
+
+ addr &= ~(fat32_cache_chunk_size - 1);
+
+ first = TAILQ_FIRST(&fat->fat32_cache_head);
+
+ /*
+ * Cache hit: if we already have the chunk, move it to list head
+ */
+ TAILQ_FOREACH(entry, &fat->fat32_cache_head, entries) {
+ if (entry->addr == addr) {
+ if (writing) {
+ entry->dirty = true;
+ }
+ if (entry != first) {
+
+ TAILQ_REMOVE(&fat->fat32_cache_head, entry, entries);
+ TAILQ_INSERT_HEAD(&fat->fat32_cache_head, entry, entries);
+ }
+ return (entry);
+ }
+ }
+
+ /*
+ * Cache miss: detach the chunk at tail of list, overwrite with
+ * the located chunk, and populate with data from disk.
+ */
+ entry = TAILQ_LAST(&fat->fat32_cache_head, cachehead);
+ TAILQ_REMOVE(&fat->fat32_cache_head, entry, entries);
+ if (fat_flush_fat32_cache_entry(fat, entry) != FSOK) {
+ return (NULL);
+ }
+
+ rwsize = fat_get_iosize(fat, addr);
+ fat_addr = fat->fat32_offset + addr;
+ entry->addr = addr;
+ fd = fd_of_(fat);
+ if (lseek(fd, fat_addr, SEEK_SET) != fat_addr ||
+ (size_t)read(fd, entry->chunk, rwsize) != rwsize) {
+ pfatal("Unable to read FAT");
+ return (NULL);
+ }
+ if (writing) {
+ entry->dirty = true;
+ }
+ TAILQ_INSERT_HEAD(&fat->fat32_cache_head, entry, entries);
+
+ return (entry);
+}
+
+static inline uint8_t *
+fat_get_fat32_cached_ptr(struct fat_descriptor *fat, cl_t cl, bool writing)
+{
+ off_t addr, off;
+ struct fat32_cache_entry *entry;
+
+ addr = cl << 2;
+ entry = fat_get_fat32_cache_entry(fat, addr, writing);
+
+ if (entry != NULL) {
+ off = addr & (fat32_cache_chunk_size - 1);
+ return (entry->chunk + off);
+ } else {
+ return (NULL);
+ }
+}
+
+
+static cl_t
+fat_get_fat32_cached_next(struct fat_descriptor *fat, cl_t cl)
+{
+ const uint8_t *p;
+ cl_t retval;
+
+ p = fat_get_fat32_cached_ptr(fat, cl, false);
+ if (p != NULL) {
+ retval = le32dec(p) & CLUST32_MASK;
+ if (retval >= (CLUST_BAD & CLUST32_MASK))
+ retval |= ~CLUST32_MASK;
+ } else {
+ retval = CLUST_DEAD;
+ }
+
+ return (retval);
+}
+
+static int
+fat_set_fat32_cached_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
+{
+ uint8_t *p;
+
+ /* Truncate 'nextcl' value, if needed */
+ nextcl &= CLUST32_MASK;
+
+ p = fat_get_fat32_cached_ptr(fat, cl, true);
+ if (p != NULL) {
+ le32enc(p, (uint32_t)nextcl);
+ return FSOK;
+ } else {
+ return FSFATAL;
+ }
+}
+
+cl_t fat_get_cl_next(struct fat_descriptor *fat, cl_t cl)
+{
+
+ if (!valid_cl(fat, cl)) {
+ pfatal("Invalid cluster: %ud", cl);
+ return CLUST_DEAD;
+ }
+
+ return (fat->get(fat, cl));
+}
+
+int fat_set_cl_next(struct fat_descriptor *fat, cl_t cl, cl_t nextcl)
+{
+
+ if (rdonly) {
+ pwarn(" (NO WRITE)\n");
+ return FSFATAL;
+ }
+
+ if (!valid_cl(fat, cl)) {
+ pfatal("Invalid cluster: %ud", cl);
+ return FSFATAL;
+ }
+
+ return (fat->set(fat, cl, nextcl));
+}
+
+static inline struct bootblock*
+boot_of_(struct fat_descriptor *fat) {
+
+ return (fat->boot);
+}
+
+struct bootblock*
+fat_get_boot(struct fat_descriptor *fat) {
+
+ return (boot_of_(fat));
+}
+
+static inline int
+fd_of_(struct fat_descriptor *fat)
+{
+ return (fat->fd);
+}
+
+int
+fat_get_fd(struct fat_descriptor * fat)
+{
+ return (fd_of_(fat));
+}
+
+/*
+ * Whether a cl is in valid data range.
+ */
+bool
+fat_is_valid_cl(struct fat_descriptor *fat, cl_t cl)
+{
+
+ return (valid_cl(fat, cl));
+}
+
+static inline bool
+valid_cl(struct fat_descriptor *fat, cl_t cl)
+{
+ const struct bootblock *boot = boot_of_(fat);
+
+ return (cl >= CLUST_FIRST && cl < boot->NumClusters);
+}
+
+/*
+ * The first 2 FAT entries contain pseudo-cluster numbers with the following
+ * layout:
+ *
+ * 31...... ........ ........ .......0
+ * rrrr1111 11111111 11111111 mmmmmmmm FAT32 entry 0
+ * rrrrsh11 11111111 11111111 11111xxx FAT32 entry 1
+ *
+ * 11111111 mmmmmmmm FAT16 entry 0
+ * sh111111 11111xxx FAT16 entry 1
+ *
+ * r = reserved
+ * m = BPB media ID byte
+ * s = clean flag (1 = dismounted; 0 = still mounted)
+ * h = hard error flag (1 = ok; 0 = I/O error)
+ * x = any value ok
+ */
+int
+checkdirty(int fs, struct bootblock *boot)
+{
+ off_t off;
+ u_char *buffer;
+ int ret = 0;
+ size_t len;
+
+ if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK)
+ return 0;
+
+ off = boot->bpbResSectors;
+ off *= boot->bpbBytesPerSec;
+
+ buffer = malloc(len = boot->bpbBytesPerSec);
+ if (buffer == NULL) {
+ perr("No space for FAT sectors (%zu)", len);
+ return 1;
+ }
+
+ if (lseek(fs, off, SEEK_SET) != off) {
+ perr("Unable to read FAT");
+ goto err;
+ }
+
+ if ((size_t)read(fs, buffer, boot->bpbBytesPerSec) !=
+ boot->bpbBytesPerSec) {
+ perr("Unable to read FAT");
+ goto err;
+ }
+
+ /*
+ * If we don't understand the FAT, then the file system must be
+ * assumed to be unclean.
+ */
+ if (buffer[0] != boot->bpbMedia || buffer[1] != 0xff)
+ goto err;
+ if (boot->ClustMask == CLUST16_MASK) {
+ if ((buffer[2] & 0xf8) != 0xf8 || (buffer[3] & 0x3f) != 0x3f)
+ goto err;
+ } else {
+ if (buffer[2] != 0xff || (buffer[3] & 0x0f) != 0x0f
+ || (buffer[4] & 0xf8) != 0xf8 || buffer[5] != 0xff
+ || buffer[6] != 0xff || (buffer[7] & 0x03) != 0x03)
+ goto err;
+ }
+
+ /*
+ * Now check the actual clean flag (and the no-error flag).
+ */
+ if (boot->ClustMask == CLUST16_MASK) {
+ if ((buffer[3] & 0xc0) == 0xc0)
+ ret = 1;
+ } else {
+ if ((buffer[7] & 0x0c) == 0x0c)
+ ret = 1;
+ }
+
+err:
+ free(buffer);
+ return ret;
+}
+
+int
+cleardirty(struct fat_descriptor *fat)
+{
+ int fd, ret = FSERROR;
+ struct bootblock *boot;
+ u_char *buffer;
+ size_t len;
+ off_t off;
+
+ boot = boot_of_(fat);
+ fd = fd_of_(fat);
+
+ if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK)
+ return 0;
+
+ off = boot->bpbResSectors;
+ off *= boot->bpbBytesPerSec;
+
+ buffer = malloc(len = boot->bpbBytesPerSec);
+ if (buffer == NULL) {
+ perr("No memory for FAT sectors (%zu)", len);
+ return 1;
+ }
+
+ if ((size_t)pread(fd, buffer, len, off) != len) {
+ perr("Unable to read FAT");
+ goto err;
+ }
+
+ if (boot->ClustMask == CLUST16_MASK) {
+ buffer[3] |= 0x80;
+ } else {
+ buffer[7] |= 0x08;
+ }
+
+ if ((size_t)pwrite(fd, buffer, len, off) != len) {
+ perr("Unable to write FAT");
+ goto err;
+ }
+
+ ret = FSOK;
+
+err:
+ free(buffer);
+ return ret;
+}
+
+/*
+ * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
+ */
+static int
+_readfat(struct fat_descriptor *fat)
+{
+ int fd;
+ size_t i;
+ off_t off;
+ size_t readsize;
+ struct bootblock *boot;
+ struct fat32_cache_entry *entry;
+
+ boot = boot_of_(fat);
+ fd = fd_of_(fat);
+ fat->fatsize = boot->FATsecs * boot->bpbBytesPerSec;
+
+ off = boot->bpbResSectors;
+ off *= boot->bpbBytesPerSec;
+
+ fat->is_mmapped = false;
+ fat->use_cache = false;
+
+ /* Attempt to mmap() first */
+ if (allow_mmap) {
+ fat->fatbuf = mmap(NULL, fat->fatsize,
+ PROT_READ | (rdonly ? 0 : PROT_WRITE),
+ MAP_SHARED, fd_of_(fat), off);
+ if (fat->fatbuf != MAP_FAILED) {
+ fat->is_mmapped = true;
+ return 1;
+ }
+ }
+
+ /*
+ * Unfortunately, we were unable to mmap().
+ *
+ * Only use the cache manager when it's necessary, that is,
+ * when the FAT is sufficiently large; in that case, only
+ * read in the first 4 MiB of FAT into memory, and split the
+ * buffer into chunks and insert to the LRU queue to populate
+ * the cache with data.
+ */
+ if (boot->ClustMask == CLUST32_MASK &&
+ fat->fatsize >= fat32_cache_size) {
+ readsize = fat32_cache_size;
+ fat->use_cache = true;
+
+ fat->fat32_offset = boot->bpbResSectors * boot->bpbBytesPerSec;
+ fat->fat32_lastaddr = fat->fatsize & ~(fat32_cache_chunk_size);
+ } else {
+ readsize = fat->fatsize;
+ }
+ fat->fatbuf = malloc(readsize);
+ if (fat->fatbuf == NULL) {
+ perr("No space for FAT (%zu)", readsize);
+ return 0;
+ }
+
+ if (lseek(fd, off, SEEK_SET) != off) {
+ perr("Unable to read FAT");
+ goto err;
+ }
+ if ((size_t)read(fd, fat->fatbuf, readsize) != readsize) {
+ perr("Unable to read FAT");
+ goto err;
+ }
+
+ /*
+ * When cache is used, split the buffer into chunks, and
+ * connect the buffer into the cache.
+ */
+ if (fat->use_cache) {
+ TAILQ_INIT(&fat->fat32_cache_head);
+ entry = calloc(fat32_cache_entries, sizeof(*entry));
+ if (entry == NULL) {
+ perr("No space for FAT cache (%zu of %zu)",
+ fat32_cache_entries, sizeof(entry));
+ goto err;
+ }
+ for (i = 0; i < fat32_cache_entries; i++) {
+ entry[i].addr = fat32_cache_chunk_size * i;
+ entry[i].chunk = &fat->fatbuf[entry[i].addr];
+ TAILQ_INSERT_TAIL(&fat->fat32_cache_head,
+ &entry[i], entries);
+ }
+ fat->fat32_cache_allentries = entry;
+ }
+
+ return 1;
+
+err:
+ free(fat->fatbuf);
+ fat->fatbuf = NULL;
+ return 0;
+}
+
+static void
+releasefat(struct fat_descriptor *fat)
+{
+ if (fat->is_mmapped) {
+ munmap(fat->fatbuf, fat->fatsize);
+ } else {
+ if (fat->use_cache) {
+ free(fat->fat32_cache_allentries);
+ fat->fat32_cache_allentries = NULL;
+ }
+ free(fat->fatbuf);
+ }
+ fat->fatbuf = NULL;
+ bitmap_dtor(&fat->headbitmap);
+}
+
+/*
+ * Read or map a FAT and populate head bitmap
+ */
+int
+readfat(int fs, struct bootblock *boot, struct fat_descriptor **fp)
+{
+ struct fat_descriptor *fat;
+ u_char *buffer, *p;
+ cl_t cl, nextcl;
+ int ret = FSOK;
+
+ boot->NumFree = boot->NumBad = 0;
+
+ fat = calloc(1, sizeof(struct fat_descriptor));
+ if (fat == NULL) {
+ perr("No space for FAT descriptor");
+ return FSFATAL;
+ }
+
+ fat->fd = fs;
+ fat->boot = boot;
+
+ if (!_readfat(fat)) {
+ free(fat);
+ return FSFATAL;
+ }
+ buffer = fat->fatbuf;
+
+ /* Populate accessors */
+ switch(boot->ClustMask) {
+ case CLUST12_MASK:
+ fat->get = fat_get_fat12_next;
+ fat->set = fat_set_fat12_next;
+ break;
+ case CLUST16_MASK:
+ fat->get = fat_get_fat16_next;
+ fat->set = fat_set_fat16_next;
+ break;
+ case CLUST32_MASK:
+ if (fat->is_mmapped || !fat->use_cache) {
+ fat->get = fat_get_fat32_next;
+ fat->set = fat_set_fat32_next;
+ } else {
+ fat->get = fat_get_fat32_cached_next;
+ fat->set = fat_set_fat32_cached_next;
+ }
+ break;
+ default:
+ pfatal("Invalid ClustMask: %d", boot->ClustMask);
+ releasefat(fat);
+ free(fat);
+ return FSFATAL;
+ }
+
+ if (bitmap_ctor(&fat->headbitmap, boot->NumClusters,
+ true) != FSOK) {
+ perr("No space for head bitmap for FAT clusters (%zu)",
+ (size_t)boot->NumClusters);
+ releasefat(fat);
+ free(fat);
+ return FSFATAL;
+ }
+
+ if (buffer[0] != boot->bpbMedia
+ || buffer[1] != 0xff || buffer[2] != 0xff
+ || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
+ || (boot->ClustMask == CLUST32_MASK
+ && ((buffer[3]&0x0f) != 0x0f
+ || buffer[4] != 0xff || buffer[5] != 0xff
+ || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
+
+ /* Windows 95 OSR2 (and possibly any later) changes
+ * the FAT signature to 0xXXffff7f for FAT16 and to
+ * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
+ * file system is dirty if it doesn't reboot cleanly.
+ * Check this special condition before errorring out.
+ */
+ if (buffer[0] == boot->bpbMedia && buffer[1] == 0xff
+ && buffer[2] == 0xff
+ && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
+ || (boot->ClustMask == CLUST32_MASK
+ && buffer[3] == 0x0f && buffer[4] == 0xff
+ && buffer[5] == 0xff && buffer[6] == 0xff
+ && buffer[7] == 0x07)))
+ ret |= FSDIRTY;
+ else {
+ /* just some odd byte sequence in FAT */
+
+ switch (boot->ClustMask) {
+ case CLUST32_MASK:
+ pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
+ "FAT starts with odd byte sequence",
+ buffer[0], buffer[1], buffer[2], buffer[3],
+ buffer[4], buffer[5], buffer[6], buffer[7]);
+ break;
+ case CLUST16_MASK:
+ pwarn("%s (%02x%02x%02x%02x)\n",
+ "FAT starts with odd byte sequence",
+ buffer[0], buffer[1], buffer[2], buffer[3]);
+ break;
+ default:
+ pwarn("%s (%02x%02x%02x)\n",
+ "FAT starts with odd byte sequence",
+ buffer[0], buffer[1], buffer[2]);
+ break;
+ }
+
+ if (ask(1, "Correct")) {
+ ret |= FSFATMOD;
+ p = buffer;
+
+ *p++ = (u_char)boot->bpbMedia;
+ *p++ = 0xff;
+ *p++ = 0xff;
+ switch (boot->ClustMask) {
+ case CLUST16_MASK:
+ *p++ = 0xff;
+ break;
+ case CLUST32_MASK:
+ *p++ = 0x0f;
+ *p++ = 0xff;
+ *p++ = 0xff;
+ *p++ = 0xff;
+ *p++ = 0x0f;
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+ /*
+ * Traverse the FAT table and populate head map. Initially, we
+ * consider all clusters as possible head cluster (beginning of
+ * a file or directory), and traverse the whole allocation table
+ * by marking every non-head nodes as such (detailed below) and
+ * fix obvious issues while we walk.
+ *
+ * For each "next" cluster, the possible values are:
+ *
+ * a) CLUST_FREE or CLUST_BAD. The *current* cluster can't be a
+ * head node.
+ * b) An out-of-range value. The only fix would be to truncate at
+ * the cluster.
+ * c) A valid cluster. It means that cluster (nextcl) is not a
+ * head cluster. Note that during the scan, every cluster is
+ * expected to be seen for at most once, and when we saw them
+ * twice, it means a cross-linked chain which should be
+ * truncated at the current cluster.
+ *
+ * After scan, the remaining set bits indicates all possible
+ * head nodes, because they were never claimed by any other
+ * node as the next node, but we do not know if these chains
+ * would end with a valid EOF marker. We will check that in
+ * checkchain() at a later time when checking directories,
+ * where these head nodes would be marked as non-head.
+ *
+ * In the final pass, all head nodes should be cleared, and if
+ * there is still head nodes, these would be leaders of lost
+ * chain.
+ */
+ for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
+ nextcl = fat_get_cl_next(fat, cl);
+
+ /* Check if the next cluster number is valid */
+ if (nextcl == CLUST_FREE) {
+ /* Save a hint for next free cluster */
+ if (boot->FSNext == 0) {
+ boot->FSNext = cl;
+ }
+ if (fat_is_cl_head(fat, cl)) {
+ fat_clear_cl_head(fat, cl);
+ }
+ boot->NumFree++;
+ } else if (nextcl == CLUST_BAD) {
+ if (fat_is_cl_head(fat, cl)) {
+ fat_clear_cl_head(fat, cl);
+ }
+ boot->NumBad++;
+ } else if (!valid_cl(fat, nextcl) && nextcl < CLUST_RSRVD) {
+ pwarn("Cluster %u continues with out of range "
+ "cluster number %u\n",
+ cl,
+ nextcl & boot->ClustMask);
+ if (ask(0, "Truncate")) {
+ ret |= fat_set_cl_next(fat, cl, CLUST_EOF);
+ ret |= FSFATMOD;
+ }
+ } else if (valid_cl(fat, nextcl)) {
+ if (fat_is_cl_head(fat, nextcl)) {
+ fat_clear_cl_head(fat, nextcl);
+ } else {
+ pwarn("Cluster %u crossed another chain at %u\n",
+ cl, nextcl);
+ if (ask(0, "Truncate")) {
+ ret |= fat_set_cl_next(fat, cl, CLUST_EOF);
+ ret |= FSFATMOD;
+ }
+ }
+ }
+
+ }
+
+ if (ret & FSFATAL) {
+ releasefat(fat);
+ free(fat);
+ *fp = NULL;
+ } else
+ *fp = fat;
+ return ret;
+}
+
+/*
+ * Get type of reserved cluster
+ */
+const char *
+rsrvdcltype(cl_t cl)
+{
+ if (cl == CLUST_FREE)
+ return "free";
+ if (cl < CLUST_BAD)
+ return "reserved";
+ if (cl > CLUST_BAD)
+ return "as EOF";
+ return "bad";
+}
+
+/*
+ * Examine a cluster chain for errors and count its size.
+ */
+int
+checkchain(struct fat_descriptor *fat, cl_t head, size_t *chainsize)
+{
+ cl_t prev_cl, current_cl, next_cl;
+ const char *op;
+
+ /*
+ * We expect that the caller to give us a real, unvisited 'head'
+ * cluster, and it must be a valid cluster. While scanning the
+ * FAT table, we already excluded all clusters that was claimed
+ * as a "next" cluster. Assert all the three conditions.
+ */
+ assert(valid_cl(fat, head));
+ assert(fat_is_cl_head(fat, head));
+
+ /*
+ * Immediately mark the 'head' cluster that we are about to visit.
+ */
+ fat_clear_cl_head(fat, head);
+
+ /*
+ * The allocation of a non-zero sized file or directory is
+ * represented as a singly linked list, and the tail node
+ * would be the EOF marker (>=CLUST_EOFS).
+ *
+ * With a valid head node at hand, we expect all subsequent
+ * cluster to be either a not yet seen and valid cluster (we
+ * would continue counting), or the EOF marker (we conclude
+ * the scan of this chain).
+ *
+ * For all other cases, the chain is invalid, and the only
+ * viable fix would be to truncate at the current node (mark
+ * it as EOF) when the next node violates that.
+ */
+ *chainsize = 0;
+ prev_cl = current_cl = head;
+ for (next_cl = fat_get_cl_next(fat, current_cl);
+ valid_cl(fat, next_cl);
+ prev_cl = current_cl, current_cl = next_cl, next_cl = fat_get_cl_next(fat, current_cl))
+ (*chainsize)++;
+
+ /* A natural end */
+ if (next_cl >= CLUST_EOFS) {
+ (*chainsize)++;
+ return FSOK;
+ }
+
+ /*
+ * The chain ended with an out-of-range cluster number.
+ *
+ * If the current node is e.g. CLUST_FREE, CLUST_BAD, etc.,
+ * it should not be present in a chain and we has to truncate
+ * at the previous node.
+ *
+ * If the current cluster points to an invalid cluster, the
+ * current cluster might have useful data and we truncate at
+ * the current cluster instead.
+ */
+ if (next_cl == CLUST_FREE || next_cl >= CLUST_RSRVD) {
+ pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
+ head, rsrvdcltype(next_cl));
+ current_cl = prev_cl;
+ } else {
+ pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
+ head,
+ next_cl & boot_of_(fat)->ClustMask);
+ (*chainsize)++;
+ }
+
+ if (*chainsize > 0) {
+ op = "Truncate";
+ next_cl = CLUST_EOF;
+ } else {
+ op = "Clear";
+ next_cl = CLUST_FREE;
+ }
+ if (ask(0, "%s", op)) {
+ return (fat_set_cl_next(fat, current_cl, next_cl) | FSFATMOD);
+ } else {
+ return (FSERROR);
+ }
+}
+
+/*
+ * Clear cluster chain from head.
+ */
+void
+clearchain(struct fat_descriptor *fat, cl_t head)
+{
+ cl_t current_cl, next_cl;
+ struct bootblock *boot = boot_of_(fat);
+
+ current_cl = head;
+
+ while (valid_cl(fat, current_cl)) {
+ next_cl = fat_get_cl_next(fat, current_cl);
+ (void)fat_set_cl_next(fat, current_cl, CLUST_FREE);
+ boot->NumFree++;
+ current_cl = next_cl;
+ }
+
+}
+
+/*
+ * Overwrite the n-th FAT with FAT0
+ */
+static int
+copyfat(struct fat_descriptor *fat, int n)
+{
+ size_t rwsize, tailsize, blobs, i;
+ off_t dst_off, src_off;
+ struct bootblock *boot;
+ int ret, fd;
+
+ ret = FSOK;
+ fd = fd_of_(fat);
+ boot = boot_of_(fat);
+
+ blobs = howmany(fat->fatsize, fat32_cache_size);
+ tailsize = fat->fatsize % fat32_cache_size;
+ if (tailsize == 0) {
+ tailsize = fat32_cache_size;
+ }
+ rwsize = fat32_cache_size;
+
+ src_off = fat->fat32_offset;
+ dst_off = boot->bpbResSectors + n * boot->FATsecs;
+ dst_off *= boot->bpbBytesPerSec;
+
+ for (i = 0; i < blobs;
+ i++, src_off += fat32_cache_size, dst_off += fat32_cache_size) {
+ if (i == blobs - 1) {
+ rwsize = tailsize;
+ }
+ if ((lseek(fd, src_off, SEEK_SET) != src_off ||
+ (size_t)read(fd, fat->fatbuf, rwsize) != rwsize) &&
+ ret == FSOK) {
+ perr("Unable to read FAT0");
+ ret = FSFATAL;
+ continue;
+ }
+ if ((lseek(fd, dst_off, SEEK_SET) != dst_off ||
+ (size_t)write(fd, fat->fatbuf, rwsize) != rwsize) &&
+ ret == FSOK) {
+ perr("Unable to write FAT %d", n);
+ ret = FSERROR;
+ }
+ }
+ return (ret);
+}
+
+/*
+ * Write out FAT
+ */
+int
+writefat(struct fat_descriptor *fat)
+{
+ u_int i;
+ size_t writesz;
+ off_t dst_base;
+ int ret = FSOK, fd;
+ struct bootblock *boot;
+ struct fat32_cache_entry *entry;
+
+ boot = boot_of_(fat);
+ fd = fd_of_(fat);
+
+ if (fat->use_cache) {
+ /*
+ * Attempt to flush all in-flight cache, and bail out
+ * if we encountered an error (but only emit error
+ * message once). Stop proceeding with copyfat()
+ * if any flush failed.
+ */
+ TAILQ_FOREACH(entry, &fat->fat32_cache_head, entries) {
+ if (fat_flush_fat32_cache_entry(fat, entry) != FSOK) {
+ if (ret == FSOK) {
+ perr("Unable to write FAT");
+ ret = FSFATAL;
+ }
+ }
+ }
+ if (ret != FSOK)
+ return (ret);
+
+ /* Update backup copies of FAT, error is not fatal */
+ for (i = 1; i < boot->bpbFATs; i++) {
+ if (copyfat(fat, i) != FSOK)
+ ret = FSERROR;
+ }
+ } else {
+ writesz = fat->fatsize;
+
+ for (i = fat->is_mmapped ? 1 : 0; i < boot->bpbFATs; i++) {
+ dst_base = boot->bpbResSectors + i * boot->FATsecs;
+ dst_base *= boot->bpbBytesPerSec;
+ if ((lseek(fd, dst_base, SEEK_SET) != dst_base ||
+ (size_t)write(fd, fat->fatbuf, writesz) != writesz) &&
+ ret == FSOK) {
+ perr("Unable to write FAT %d", i);
+ ret = ((i == 0) ? FSFATAL : FSERROR);
+ }
+ }
+ }
+
+ return ret;
+}
+
+/*
+ * Check a complete in-memory FAT for lost cluster chains
+ */
+int
+checklost(struct fat_descriptor *fat)
+{
+ cl_t head;
+ int mod = FSOK;
+ int dosfs, ret;
+ size_t chains, chainlength;
+ struct bootblock *boot;
+
+ dosfs = fd_of_(fat);
+ boot = boot_of_(fat);
+
+ /*
+ * At this point, we have already traversed all directories.
+ * All remaining chain heads in the bitmap are heads of lost
+ * chains.
+ */
+ chains = fat_get_head_count(fat);
+ for (head = CLUST_FIRST;
+ chains > 0 && head < boot->NumClusters;
+ ) {
+ /*
+ * We expect the bitmap to be very sparse, so skip if
+ * the range is full of 0's
+ */
+ if (head % LONG_BIT == 0 &&
+ !fat_is_cl_head_in_range(fat, head)) {
+ head += LONG_BIT;
+ continue;
+ }
+ if (fat_is_cl_head(fat, head)) {
+ ret = checkchain(fat, head, &chainlength);
+ if (ret != FSERROR && chainlength > 0) {
+ pwarn("Lost cluster chain at cluster %u\n"
+ "%zd Cluster(s) lost\n",
+ head, chainlength);
+ mod |= ret = reconnect(fat, head,
+ chainlength);
+ }
+ if (mod & FSFATAL)
+ break;
+ if (ret == FSERROR && ask(0, "Clear")) {
+ clearchain(fat, head);
+ mod |= FSFATMOD;
+ }
+ chains--;
+ }
+ head++;
+ }
+
+ finishlf();
+
+ if (boot->bpbFSInfo) {
+ ret = 0;
+ if (boot->FSFree != 0xffffffffU &&
+ boot->FSFree != boot->NumFree) {
+ pwarn("Free space in FSInfo block (%u) not correct (%u)\n",
+ boot->FSFree, boot->NumFree);
+ if (ask(1, "Fix")) {
+ boot->FSFree = boot->NumFree;
+ ret = 1;
+ }
+ }
+ if (boot->FSNext != 0xffffffffU &&
+ (boot->FSNext >= boot->NumClusters ||
+ (boot->NumFree && fat_get_cl_next(fat, boot->FSNext) != CLUST_FREE))) {
+ pwarn("Next free cluster in FSInfo block (%u) %s\n",
+ boot->FSNext,
+ (boot->FSNext >= boot->NumClusters) ? "invalid" : "not free");
+ if (ask(1, "Fix"))
+ for (head = CLUST_FIRST; head < boot->NumClusters; head++)
+ if (fat_get_cl_next(fat, head) == CLUST_FREE) {
+ boot->FSNext = head;
+ ret = 1;
+ break;
+ }
+ }
+ if (ret)
+ mod |= writefsinfo(dosfs, boot);
+ }
+
+ return mod;
+}
diff --git a/cmd/extra/fsck.fat/freebsd-compat.h b/cmd/extra/fsck.fat/freebsd-compat.h
@@ -0,0 +1,132 @@
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 1982, 1986, 1989, 1993
+ * The Regents of the University of California. All rights reserved.
+ * (c) UNIX System Laboratories, Inc.
+ * All or some portions of this file are derived from material licensed
+ * to the University of California by American Telephone and Telegraph
+ * Co. or Unix System Laboratories, Inc. and are reproduced herein with
+ * the permission of UNIX System Laboratories, Inc.
+ *
+ * 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.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ */
+
+#ifndef FSCK_FAT_COMPAT_H
+#define FSCK_FAT_COMPAT_H
+
+#include <limits.h>
+#include <sys/param.h>
+
+#ifndef __dead2
+#define __dead2 __attribute__((__noreturn__))
+#endif
+#ifndef __printflike
+#define __printflike(f, a) __attribute__((__format__(__printf__, f, a)))
+#endif
+#ifndef LONG_BIT
+#define LONG_BIT (sizeof(long) * CHAR_BIT)
+#endif
+
+/* From FreeBSD sys/sys/param.h */
+
+#ifndef roundup2
+#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
+#endif
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
+ * 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 <stdint.h>
+
+#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#include <sys/endian.h>
+#else
+
+/* From FreeBSD sys/sys/endian.h */
+
+static __inline void
+le16enc(void *pp, uint16_t u)
+{
+ uint8_t *p = (uint8_t *)pp;
+
+ p[0] = u & 0xff;
+ p[1] = (u >> 8) & 0xff;
+}
+
+static __inline void
+le32enc(void *pp, uint32_t u)
+{
+ uint8_t *p = (uint8_t *)pp;
+
+ p[0] = u & 0xff;
+ p[1] = (u >> 8) & 0xff;
+ p[2] = (u >> 16) & 0xff;
+ p[3] = (u >> 24) & 0xff;
+}
+
+static __inline uint16_t
+le16dec(const void *pp)
+{
+ uint8_t const *p = (uint8_t const *)pp;
+
+ return ((p[1] << 8) | p[0]);
+}
+
+static __inline uint32_t
+le32dec(const void *pp)
+{
+ uint8_t const *p = (uint8_t const *)pp;
+
+ return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
+}
+#endif
+
+#endif
diff --git a/cmd/extra/fsck.fat/fsck.fat.c b/cmd/extra/fsck.fat/fsck.fat.c
@@ -0,0 +1,163 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1995 Wolfgang Solfrank
+ * Copyright (c) 1995 Martin Husemann
+ *
+ * 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 ``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 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 "freebsd-compat.h"
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: main.c,v 1.10 1997/10/01 02:18:14 enami Exp $");
+#endif /* not lint */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdarg.h>
+
+#include "fsutil.h"
+#include "ext.h"
+#include "util.h"
+
+int alwaysno; /* assume "no" for all questions */
+int alwaysyes; /* assume "yes" for all questions */
+int preen; /* set when preening */
+int rdonly; /* device is opened read only (supersedes above) */
+int skipclean; /* skip clean file systems if preening */
+int allow_mmap; /* Allow the use of mmap(), if possible */
+
+static void usage(void) __dead2;
+
+static void
+usage(void)
+{
+
+ eprintf("usage: %s [-fMnpy] filesystem ...\n", argv0);
+}
+
+// ?man fsck.fat: check and repair FAT12, FAT16, and FAT32 filesystems
+// ?man arguments: filesystem ...
+// ?man -f:check even a clean filesystem in preen mode
+// ?man -M:read the FAT without mapping it into memory
+// ?man -n:check read-only and answer no to all repair questions
+// ?man -p:automatically fix problems that can be repaired safely
+// ?man -y:answer yes to all repair questions
+int
+main(int argc, char **argv)
+{
+ int ret = 0, erg;
+
+ skipclean = 1;
+ allow_mmap = 1;
+ ARGBEGIN {
+ case 'C': /* for fsck_ffs compatibility */
+ break;
+ case 'f':
+ skipclean = 0;
+ break;
+ case 'F':
+ /*
+ * We can never run in the background. We must exit
+ * silently with a nonzero exit code so that fsck(8)
+ * can probe our support for -F. The exit code
+ * doesn't really matter, but we use an unusual one
+ * in case someone tries -F directly. The -F flag
+ * is intentionally left out of the usage message.
+ */
+ exit(5);
+ case 'n':
+ alwaysno = 1;
+ alwaysyes = 0;
+ break;
+ case 'y':
+ alwaysyes = 1;
+ alwaysno = 0;
+ break;
+
+ case 'p':
+ preen = 1;
+ break;
+
+ case 'M':
+ allow_mmap = 0;
+ break;
+
+ default:
+ usage();
+ break;
+ } ARGEND
+
+ if (!argc)
+ usage();
+
+ while (--argc >= 0) {
+ setcdevname(*argv, preen);
+ erg = checkfilesys(*argv++);
+ if (erg > ret)
+ ret = erg;
+ }
+
+ if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>"))
+ ret = 8;
+ return ret;
+}
+
+
+/*VARARGS*/
+int
+ask(int def, const char *fmt, ...)
+{
+ va_list ap;
+
+ char prompt[256];
+ int c;
+
+ if (alwaysyes || alwaysno || rdonly)
+ def = (alwaysyes && !rdonly && !alwaysno);
+
+ if (preen) {
+ if (def)
+ printf("FIXED\n");
+ return def;
+ }
+
+ va_start(ap, fmt);
+ vsnprintf(prompt, sizeof(prompt), fmt, ap);
+ va_end(ap);
+ if (alwaysyes || alwaysno || rdonly) {
+ printf("%s? %s\n", prompt, def ? "yes" : "no");
+ return def;
+ }
+ do {
+ printf("%s? [yn] ", prompt);
+ fflush(stdout);
+ c = getchar();
+ while (c != '\n' && getchar() != '\n')
+ if (feof(stdin))
+ return 0;
+ } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
+ return c == 'y' || c == 'Y';
+}
diff --git a/cmd/extra/fsck.fat/fsutil.c b/cmd/extra/fsck.fat/fsutil.c
@@ -0,0 +1,271 @@
+/* $NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $ */
+
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California. 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.
+ * 3. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 "freebsd-compat.h"
+#if defined(__RCSID) && !defined(lint)
+__RCSID("$NetBSD: fsutil.c,v 1.15 2006/06/05 16:52:05 christos Exp $");
+#endif /* not lint */
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+#include <err.h>
+#include <paths.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "fsutil.h"
+#include "util.h"
+
+static const char *dev = NULL;
+static int preen = 0;
+
+static void vmsg(int, const char *, va_list) __printflike(2, 0);
+
+#ifdef HAVE_FSTAB_H
+#include <fstab.h>
+#include <strings.h>
+
+/*
+ * The getfsopt() function checks whether an option is present in
+ * an fstab(5) fs_mntops entry. There are six possible cases:
+ *
+ * fs_mntops getfsopt result
+ * rw,foo foo true
+ * rw,nofoo nofoo true
+ * rw,nofoo foo false
+ * rw,foo nofoo false
+ * rw foo false
+ * rw nofoo false
+ *
+ * This function should be part of and documented in getfsent(3).
+ */
+int
+getfsopt(struct fstab *fs, const char *option)
+{
+ int negative, found;
+ char *opt, *optbuf;
+
+ if (option[0] == 'n' && option[1] == 'o') {
+ negative = 1;
+ option += 2;
+ } else
+ negative = 0;
+ optbuf = strdup(fs->fs_mntops);
+ found = 0;
+ for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
+ if (opt[0] == 'n' && opt[1] == 'o') {
+ if (!strcasecmp(opt + 2, option))
+ found = negative;
+ } else if (!strcasecmp(opt, option))
+ found = !negative;
+ }
+ free(optbuf);
+ return (found);
+}
+#endif
+
+void
+setcdevname(const char *cd, int pr)
+{
+ dev = cd;
+ preen = pr;
+}
+
+const char *
+cdevname(void)
+{
+ return dev;
+}
+
+static void
+vmsg(int fatal, const char *fmt, va_list ap)
+{
+ if (!fatal && preen)
+ (void) printf("%s: ", dev);
+
+ (void) vprintf(fmt, ap);
+
+ if (fatal && preen)
+ (void) printf("\n");
+
+ if (fatal && preen) {
+ (void) printf(
+ "%s: UNEXPECTED INCONSISTENCY; RUN %s MANUALLY.\n",
+ dev, argv0);
+ exit(8);
+ }
+}
+
+/*VARARGS*/
+void
+pfatal(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(1, fmt, ap);
+ va_end(ap);
+}
+
+/*VARARGS*/
+void
+pwarn(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(0, fmt, ap);
+ va_end(ap);
+}
+
+void
+perr(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(1, fmt, ap);
+ va_end(ap);
+}
+
+void
+panic(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ vmsg(1, fmt, ap);
+ va_end(ap);
+ exit(8);
+}
+
+const char *
+devcheck(const char *origname)
+{
+ struct stat stslash, stchar;
+
+ if (stat("/", &stslash) < 0) {
+ perr("Can't stat `/'");
+ return (origname);
+ }
+ if (stat(origname, &stchar) < 0) {
+ perr("Can't stat %s\n", origname);
+ return (origname);
+ }
+ if (!S_ISCHR(stchar.st_mode)) {
+ perr("%s is not a char device\n", origname);
+ }
+ return (origname);
+}
+
+#ifdef HAVE_GETMNTINFO
+/*
+ * Get the mount point information for name.
+ */
+struct statfs *
+getmntpt(const char *name)
+{
+ struct stat devstat, mntdevstat;
+ char device[sizeof(_PATH_DEV) - 1 + MNAMELEN];
+ char *dev_name;
+ struct statfs *mntbuf, *statfsp;
+ int i, mntsize, isdev;
+ if (stat(name, &devstat) != 0)
+ return (NULL);
+ if (S_ISCHR(devstat.st_mode) || S_ISBLK(devstat.st_mode))
+ isdev = 1;
+ else
+ isdev = 0;
+ mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
+ for (i = 0; i < mntsize; i++) {
+ statfsp = &mntbuf[i];
+ dev_name = statfsp->f_mntfromname;
+ if (*dev_name != '/') {
+ if (strlen(_PATH_DEV) + strlen(dev_name) + 1 >
+ sizeof(statfsp->f_mntfromname))
+ continue;
+ strcpy(device, _PATH_DEV);
+ strcat(device, dev_name);
+ strcpy(statfsp->f_mntfromname, device);
+ }
+ if (isdev == 0) {
+ if (strcmp(name, statfsp->f_mntonname))
+ continue;
+ return (statfsp);
+ }
+ if (stat(dev_name, &mntdevstat) == 0 &&
+ mntdevstat.st_rdev == devstat.st_rdev)
+ return (statfsp);
+ }
+ statfsp = NULL;
+ return (statfsp);
+}
+#endif
+
+void *
+emalloc(size_t s)
+{
+ void *p;
+
+ p = malloc(s);
+ if (p == NULL)
+ err(1, "malloc failed");
+ return (p);
+}
+
+
+void *
+erealloc(void *p, size_t s)
+{
+ void *q;
+
+ q = realloc(p, s);
+ if (q == NULL)
+ err(1, "realloc failed");
+ return (q);
+}
+
+
+char *
+estrdup(const char *s)
+{
+ char *p;
+
+ p = strdup(s);
+ if (p == NULL)
+ err(1, "strdup failed");
+ return (p);
+}
diff --git a/cmd/extra/fsck.fat/fsutil.h b/cmd/extra/fsck.fat/fsutil.h
@@ -0,0 +1,55 @@
+/* $NetBSD: fsutil.h,v 1.114 2009/10/21 01:07:46 snj Exp $ */
+
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 1996 Christos Zoulas. 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 ``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 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 <sys/types.h>
+
+#ifdef __ANDROID__
+#define __dead2 __attribute__((__noreturn__))
+#endif
+
+struct fstab;
+int checkfstab(int, int (*)(struct fstab *),
+ int (*) (const char *, const char *, const char *, const char *, pid_t *));
+int getfsopt(struct fstab *, const char *);
+void pfatal(const char *, ...) __printflike(1, 2);
+void pwarn(const char *, ...) __printflike(1, 2);
+void perr(const char *, ...) __printflike(1, 2);
+void panic(const char *, ...) __dead2 __printflike(1, 2);
+const char *devcheck(const char *);
+const char *cdevname(void);
+void setcdevname(const char *, int);
+void *emalloc(size_t);
+void *erealloc(void *, size_t);
+char *estrdup(const char *);
+
+#define CHECK_PREEN 0x0001
+#define CHECK_VERBOSE 0x0002
+#define CHECK_DEBUG 0x0004
+#define CHECK_BACKGRD 0x0008
+#define DO_BACKGRD 0x0010
+#define CHECK_CLEAN 0x0020
diff --git a/ninqu.rules b/ninqu.rules
@@ -180,7 +180,7 @@
(group net lib NET_BIN)
(group xsi lib XSI_BIN)
(group pseudo lib PSEUDO_BIN)
- (group extra lib EXTRA_BIN EXTRA_YAP)
+ (group extra lib EXTRA_BIN EXTRA_YAP EXTRA_FSCK_FAT)
; codegen: generate sources consumed by the build rules below. ungated
; ones always run. conditional ones use (req) so they only run when
@@ -299,6 +299,20 @@
(out "cmd/extra/yap/yap")
(exec $(CC) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS) -ltinfo))
+ (rule EXTRA_FSCK_FAT_OBJ
+ (glob "cmd/extra/fsck.fat/*.c")
+ (gate build_extra_fsck_fat)
+ (dep $(MATCH) FEATURES_H "glob:cmd/extra/fsck.fat/*.h" "shared/queue.h")
+ (out "$(STEM).o")
+ (exec $(CC) $(CPPFLAGS) -U_POSIX_C_SOURCE -D_DEFAULT_SOURCE $(CFLAGS) -c -o $(OUT) $(MATCH)))
+
+ (rule EXTRA_FSCK_FAT
+ (gate build_extra_fsck_fat)
+ (member extra fsck.fat)
+ (dep EXTRA_FSCK_FAT_OBJ LIBUTIL)
+ (out "cmd/extra/fsck.fat/fsck.fat")
+ (exec $(CC) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS)))
+
; (member GROUP ALIAS) gives a rule a qualified user-facing name
; (e.g. posix/bc) while keeping its internal name (POSIX_BC) for deps,
; gates, and other references
@@ -504,6 +518,7 @@
(cmd/xsi 1)
(cmd/pseudo 1)
(cmd/extra 1)
+ (cmd/extra/fsck.fat 1)
(cmd/dev 1))
(group man MAN_MDOC MAN_TXT)
@@ -518,7 +533,7 @@
find shared -name '*.a' -exec rm -f {} +
printf ' CLEAN compiled binaries\n'
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 -exec rm -f {} +
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 -exec rm -f {} +
sh scripts/genconfig.sh clean
rm -f scripts/mk/config.mk scripts/mk/config.kv scripts/mk/rules.mk shared/libutil/nofork_list.h
@@ -556,7 +571,7 @@
m=\"$DESTDIR$(MANPREFIX)\"
mkdir -p \"$d/bin\" \"$m/man1\" \"$m/man8\" \"$d/lib\" \"$d/include/aruu\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 | while read -r bin; do
name=${bin##*/}
printf ' INSTALL %s/bin/%s\n' '$(PREFIX)' \"$name\"
cp \"$bin\" \"$d/bin/$name\"
@@ -578,6 +593,11 @@
mkdir -p \"$d/share\"
cp -R share/kmap \"$d/share/kmap\"
+ if [ -x cmd/extra/fsck.fat/fsck.fat ]; then
+ mkdir -p \"$d/share/licenses/aruu\"
+ cp cmd/extra/fsck.fat/NOTICE \"$d/share/licenses/aruu/fsck.fat\"
+ fi
+
printf ' INSTALL %s/lib/libaruu.a\n' '$(PREFIX)'
cp shared/libutil/libutil.a \"$d/lib/libaruu.a\"
chmod 644 \"$d/lib/libaruu.a\"
@@ -597,7 +617,7 @@
(rule INSTALL_BASE
(phony)
(after POSIX_BIN LINUX_BIN NET_BIN XSI_BIN PSEUDO_BIN EXTRA_BIN
- POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP
+ POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP EXTRA_FSCK_FAT
LIBUTIL MAN_MDOC MAN_TXT)
(shell "
[ -n \"${DESTDIR:-}\" ] || { printf 'install-base requires DESTDIR\n' >&2; exit 1; }
@@ -607,7 +627,7 @@
m=\"$DESTDIR$(MANPREFIX)\"
mkdir -p \"$d/bin\" \"$m/man1\" \"$m/man8\" \"$d/lib\" \"$d/include/aruu\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 ! -path 'cmd/dev/*' | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 ! -path 'cmd/dev/*' | while read -r bin; do
name=${bin##*/}
printf ' INSTALL %s/bin/%s\n' '$(PREFIX)' \"$name\"
cp \"$bin\" \"$d/bin/$name\"
@@ -629,6 +649,11 @@
mkdir -p \"$d/share\"
cp -R share/kmap \"$d/share/kmap\"
+ if [ -x cmd/extra/fsck.fat/fsck.fat ]; then
+ mkdir -p \"$d/share/licenses/aruu\"
+ cp cmd/extra/fsck.fat/NOTICE \"$d/share/licenses/aruu/fsck.fat\"
+ fi
+
printf ' INSTALL %s/lib/libaruu.a\n' '$(PREFIX)'
cp shared/libutil/libutil.a \"$d/lib/libaruu.a\"
chmod 644 \"$d/lib/libaruu.a\"
@@ -651,7 +676,7 @@
case \"$DESTDIR\" in /usr|/usr/*) printf 'ARUU cannot remove from usr\n' >&2; exit 1 ;; esac
d=\"$DESTDIR$(PREFIX)\"
m=\"$DESTDIR$(MANPREFIX)\"
- find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' ')' -perm -100 | while read -r bin; do
+ find cmd -type f '(' ! -name '*.*' -o -name 'mkfs.fat' -o -name 'mkfs.ext4' -o -name 'fsck.fat' ')' -perm -100 | while read -r bin; do
name=${bin##*/}
printf ' REMOVE %s/bin/%s\n' '$(PREFIX)' \"$name\"
rm -f \"$d/bin/$name\"
@@ -664,6 +689,8 @@
rm -f \"$m/$sec/$name\"
done
+ rm -f \"$d/share/licenses/aruu/fsck.fat\"
+
printf ' REMOVE %s/lib/libaruu.a\n' '$(PREFIX)'
rm -f \"$d/lib/libaruu.a\"
for h in util.h wexec.h arg.h compat.h filesystem.h net.h sysinfo.h; do
@@ -685,6 +712,7 @@
! -path 'cmd/posix/sh/*' \
! -path 'cmd/posix/make/*' \
! -path 'cmd/extra/yap/*' \
+ ! -path 'cmd/extra/fsck.fat/*' \
! -path 'cmd/posix/bc.c' \
-print | xargs -r clang-format -i
"))
@@ -840,6 +868,13 @@
(shell "$(LD) -r -o $(OUT) $(IN) && $(OBJCOPY) --redefine-sym main=yap_main $(OUT) && $(OBJCOPY) --keep-global-symbol=yap_main $(OUT)")
(group box))
+ (rule EXTRA_FSCK_FAT_BOX
+ (gate build_extra_fsck_fat)
+ (dep EXTRA_FSCK_FAT_OBJ)
+ (out ".box/cmd/extra/fsck.fat.o")
+ (shell "$(LD) -r -o $(OUT) $(IN) && $(OBJCOPY) --redefine-sym main=fsck_fat_main $(OUT) && $(OBJCOPY) --keep-global-symbol=fsck_fat_main $(OUT)")
+ (group box))
+
; genbox.sh relies on arguments passed by ninqu, where each argument matches
; an output from BOX_OBJ or a custom box rule. the script derives categories
; and tool names directly from these paths and outputs the dispatch code
@@ -860,7 +895,7 @@
(group box))
(rule BOX_DISPATCH
- (dep BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX NINQU_BOX)
+ (dep BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX EXTRA_FSCK_FAT_BOX NINQU_BOX)
(out ".box/aruu-box.c")
(description "dispatch table and main() for aruu-box, generated from the enabled tool list")
(exec scripts/genbox.sh $(IN))
@@ -880,7 +915,7 @@
; instead, same as LIBUTIL/LIBUTF/LIBREDLINE
;
(rule BOX
- (dep BOX_DISPATCH BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX NINQU_BOX LIBDISK LIBUTIL LIBUTF LIBREDLINE LIBBLAKE3 LIBMAGIC)
+ (dep BOX_DISPATCH BOX_OBJ POSIX_BC_BOX POSIX_AWK_BOX POSIX_SH_BOX POSIX_MAKE_BOX EXTRA_YAP_BOX EXTRA_FSCK_FAT_BOX NINQU_BOX LIBDISK LIBUTIL LIBUTF LIBREDLINE LIBBLAKE3 LIBMAGIC)
(out "aruu-box")
(description "single multi-call binary, one dispatch table for every enabled tool")
(shell "$(CC) $(CPPFLAGS) $(CFLAGS) -o $(OUT) $(IN) $(LDFLAGS) $(LDLIBS) $(LDLIBS_TLS) $(BOX_EXTRA_LIBS)")
@@ -901,7 +936,7 @@
(after LIBUTIL LIBUTF LIBREDLINE
POSIX_BIN LINUX_BIN NET_BIN XSI_BIN
PSEUDO_BIN EXTRA_BIN
- POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP
+ POSIX_BC POSIX_AWK POSIX_SH POSIX_MAKE EXTRA_YAP EXTRA_FSCK_FAT
AR XELF AS LD CC1 CPP CCDRV)
(exec true))