Commit d1b48971 authored by Mike Hibler's avatar Mike Hibler

Update FAT code.

parent 1bc17c5f
......@@ -12,13 +12,6 @@
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Martin Husemann
* and Wolfgang Solfrank.
* 4. 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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
......@@ -31,7 +24,7 @@
* (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 $
* $FreeBSD: src/sbin/fsck_msdosfs/dosfs.h,v 1.1.2.1 2001/08/01 05:47:56 obrien Exp $
* $FreeBSD: releng/10.2/sbin/fsck_msdosfs/dosfs.h 203874 2010-02-14 12:31:28Z kib $
*/
#ifndef DOSFS_H
......@@ -46,27 +39,27 @@ typedef u_int32_t cl_t; /* type holding a cluster number */
* FAT boot block.
*/
struct bootblock {
u_int BytesPerSec; /* bytes per sector */
u_int SecPerClust; /* sectors per cluster */
u_int ResSectors; /* number of reserved sectors */
u_int FATs; /* number of FATs */
u_int RootDirEnts; /* number of root directory entries */
u_int Media; /* media descriptor */
u_int FATsmall; /* number of sectors per FAT */
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 Heads; /* number of heads */
u_int32_t Sectors; /* total number of sectors */
u_int32_t HiddenSecs; /* # of hidden sectors */
u_int32_t HugeSectors; /* # of sectors if bpbSectors == 0 */
u_int FSInfo; /* FSInfo sector */
u_int Backup; /* Backup of Bootblocks */
cl_t RootCl; /* Start of Root Directory */
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 filesystem */
#define FAT32 1 /* this is a FAT32 file system */
/*
* Maybe, we should separate out
* various parts of FAT32? XXX
......
......@@ -17,13 +17,6 @@
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Martin Husemann
* and Wolfgang Solfrank.
* 4. 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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
......@@ -40,36 +33,35 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: boot.c,v 1.5 1997/10/17 11:19:23 ws Exp $");
__RCSID("$NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp ");
static const char rcsid[] =
"$FreeBSD: src/sbin/fsck_msdosfs/boot.c,v 1.1.2.1 2001/08/01 05:47:55 obrien Exp $";
"$FreeBSD: releng/10.2/sbin/fsck_msdosfs/boot.c 241807 2012-10-21 12:01:19Z uqs $";
#endif /* not lint */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include "fat_glue.h"
int
readboot(dosfs, boot)
int dosfs;
struct bootblock *boot;
readboot(int dosfs, struct bootblock *boot)
{
u_char block[DOSBOOTBLOCKSIZE];
u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
u_char backup[DOSBOOTBLOCKSIZE];
int ret = FSOK;
int i;
if (read(dosfs, block, sizeof block) < sizeof block) {
if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
perror("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]);
pfatal("Invalid signature in boot block: %02x%02x",
block[511], block[510]);
return FSFATAL;
}
......@@ -77,22 +69,24 @@ readboot(dosfs, boot)
boot->ValidFat = -1;
/* decode bios parameter block */
boot->BytesPerSec = block[11] + (block[12] << 8);
boot->SecPerClust = block[13];
boot->ResSectors = block[14] + (block[15] << 8);
boot->FATs = block[16];
boot->RootDirEnts = block[17] + (block[18] << 8);
boot->Sectors = block[19] + (block[20] << 8);
boot->Media = block[21];
boot->FATsmall = block[22] + (block[23] << 8);
boot->bpbBytesPerSec = block[11] + (block[12] << 8);
boot->bpbSecPerClust = block[13];
boot->bpbResSectors = block[14] + (block[15] << 8);
boot->bpbFATs = block[16];
boot->bpbRootDirEnts = block[17] + (block[18] << 8);
boot->bpbSectors = block[19] + (block[20] << 8);
boot->bpbMedia = block[21];
boot->bpbFATsmall = block[22] + (block[23] << 8);
boot->SecPerTrack = block[24] + (block[25] << 8);
boot->Heads = block[26] + (block[27] << 8);
boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
boot->bpbHeads = block[26] + (block[27] << 8);
boot->bpbHiddenSecs = block[28] + (block[29] << 8) +
(block[30] << 16) + (block[31] << 24);
boot->bpbHugeSectors = block[32] + (block[33] << 8) +
(block[34] << 16) + (block[35] << 24);
boot->FATsecs = boot->FATsmall;
boot->FATsecs = boot->bpbFATsmall;
if (!boot->RootDirEnts)
if (!boot->bpbRootDirEnts)
boot->flags |= FAT32;
if (boot->flags & FAT32) {
boot->FATsecs = block[36] + (block[37] << 8)
......@@ -103,19 +97,18 @@ readboot(dosfs, boot)
/* check version number: */
if (block[42] || block[43]) {
/* Correct? XXX */
pfatal("Unknown filesystem version: %x.%x",
pfatal("Unknown file system version: %x.%x",
block[43], block[42]);
return FSFATAL;
}
boot->RootCl = block[44] + (block[45] << 8)
boot->bpbRootClust = block[44] + (block[45] << 8)
+ (block[46] << 16) + (block[47] << 24);
boot->FSInfo = block[48] + (block[49] << 8);
boot->Backup = block[50] + (block[51] << 8);
boot->bpbFSInfo = block[48] + (block[49] << 8);
boot->bpbBackup = block[50] + (block[51] << 8);
if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
!= boot->FSInfo * boot->BytesPerSec
|| read(dosfs, fsinfo, sizeof fsinfo)
!= sizeof fsinfo) {
if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
|| read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
perror("could not read fsinfo block");
return FSFATAL;
}
......@@ -129,10 +122,10 @@ readboot(dosfs, boot)
|| fsinfo[0x3fd]
|| fsinfo[0x3fe] != 0x55
|| fsinfo[0x3ff] != 0xaa) {
pwarn("Invalid signature in fsinfo block");
pwarn("Invalid signature in fsinfo block\n");
return FSFATAL;
}
if (boot->FSInfo) {
if (boot->bpbFSInfo) {
boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
+ (fsinfo[0x1ea] << 16)
+ (fsinfo[0x1eb] << 24);
......@@ -141,41 +134,55 @@ readboot(dosfs, boot)
+ (fsinfo[0x1ef] << 24);
}
if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
!= boot->Backup * boot->BytesPerSec
if (lseek(dosfs, boot->bpbBackup * boot->bpbBytesPerSec,
SEEK_SET)
!= boot->bpbBackup * boot->bpbBytesPerSec
|| read(dosfs, backup, sizeof backup) != sizeof backup) {
perror("could not read backup bootblock");
return FSFATAL;
}
backup[65] = block[65]; /* XXX */
if (memcmp(block + 11, backup + 11, 79)) {
/* Correct? XXX */
pfatal("backup doesn't compare to primary bootblock");
return FSFATAL;
/*
* XXX We require a reference that explains
* that these bytes need to match, or should
* drop the check. gdt@NetBSD has observed
* filesystems that work fine under Windows XP
* and NetBSD that do not match, so the
* requirement is suspect. For now, just
* print out useful information and continue.
*/
pfatal("backup (block %d) mismatch with primary bootblock:\n",
boot->bpbBackup);
for (i = 11; i < 11 + 90; i++) {
if (block[i] != backup[i])
pfatal("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n",
i, block[i], backup[i]);
}
}
/* Check backup FSInfo? XXX */
/* Check backup bpbFSInfo? XXX */
}
boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
/ boot->BytesPerSec
+ boot->ResSectors
+ boot->FATs * boot->FATsecs
- CLUST_FIRST * boot->SecPerClust;
boot->ClusterOffset = (boot->bpbRootDirEnts * 32 +
boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
boot->bpbResSectors + boot->bpbFATs * boot->FATsecs -
CLUST_FIRST * boot->bpbSecPerClust;
if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
pfatal("Invalid sector size: %u", boot->BytesPerSec);
if (boot->bpbBytesPerSec % DOSBOOTBLOCKSIZE != 0) {
pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
return FSFATAL;
}
if (boot->SecPerClust == 0) {
pfatal("Invalid cluster size: %u", boot->SecPerClust);
if (boot->bpbSecPerClust == 0) {
pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
return FSFATAL;
}
if (boot->Sectors) {
boot->HugeSectors = 0;
boot->NumSectors = boot->Sectors;
if (boot->bpbSectors) {
boot->bpbHugeSectors = 0;
boot->NumSectors = boot->bpbSectors;
} else
boot->NumSectors = boot->HugeSectors;
boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
boot->NumSectors = boot->bpbHugeSectors;
boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) /
boot->bpbSecPerClust;
if (boot->flags&FAT32)
boot->ClustMask = CLUST32_MASK;
......@@ -191,13 +198,13 @@ readboot(dosfs, boot)
switch (boot->ClustMask) {
case CLUST32_MASK:
boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
break;
case CLUST16_MASK:
boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
break;
default:
boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
break;
}
......@@ -206,7 +213,7 @@ readboot(dosfs, boot)
boot->NumClusters, boot->FATsecs);
return FSFATAL;
}
boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
boot->NumFiles = 1;
boot->NumFree = 0;
......
......@@ -17,13 +17,6 @@
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Martin Husemann
* and Wolfgang Solfrank.
* 4. 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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
......@@ -40,9 +33,9 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");
__RCSID("$NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $");
static const char rcsid[] =
"$FreeBSD: src/sbin/fsck_msdosfs/fat.c,v 1.1.2.1 2001/08/01 05:47:56 obrien Exp $";
"$FreeBSD: releng/10.2/sbin/fsck_msdosfs/fat.c 268968 2014-07-21 23:23:20Z pfg $";
#endif /* not lint */
#include <stdlib.h>
......@@ -53,18 +46,13 @@ static const char rcsid[] =
#include "fat_glue.h"
static int checkclnum __P((struct bootblock *, int, cl_t, cl_t *));
static int _readfat __P((int, struct bootblock *, int, u_char **));
static int checkclnum(struct bootblock *, u_int, cl_t, cl_t *);
/*
* Check a cluster number for valid value
*/
static int
checkclnum(boot, fat, cl, next)
struct bootblock *boot;
int fat;
cl_t cl;
cl_t *next;
checkclnum(struct bootblock *boot, u_int fat, cl_t cl, cl_t *next)
{
static cl_t first = 0;
......@@ -87,30 +75,26 @@ checkclnum(boot, fat, cl, next)
* Read a FAT from disk. Returns 1 if successful, 0 otherwise.
*/
static int
_readfat(fs, boot, no, buffer)
int fs;
struct bootblock *boot;
int no;
u_char **buffer;
_readfat(int fs, struct bootblock *boot, u_int no, u_char **buffer)
{
off_t off;
*buffer = malloc(boot->FATsecs * boot->BytesPerSec);
*buffer = malloc(boot->FATsecs * boot->bpbBytesPerSec);
if (*buffer == NULL) {
perror("No space for FAT");
perror("No space for FAT sectors");
return 0;
}
off = boot->ResSectors + no * boot->FATsecs;
off *= boot->BytesPerSec;
off = boot->bpbResSectors + no * boot->FATsecs;
off *= boot->bpbBytesPerSec;
if (lseek(fs, off, SEEK_SET) != off) {
perror("Unable to read FAT");
goto err;
}
if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
!= boot->FATsecs * boot->BytesPerSec) {
if ((size_t)read(fs, *buffer, boot->FATsecs * boot->bpbBytesPerSec)
!= boot->FATsecs * boot->bpbBytesPerSec) {
perror("Unable to read FAT");
goto err;
}
......@@ -126,30 +110,28 @@ _readfat(fs, boot, no, buffer)
* Read a FAT and decode it into internal format
*/
int
readfat(fs, boot, no, fp)
int fs;
struct bootblock *boot;
int no;
struct fatEntry **fp;
readfat(int fs, struct bootblock *boot, u_int no, struct fatEntry **fp)
{
struct fatEntry *fat;
u_char *buffer, *p;
cl_t cl;
int ret = FSOK;
size_t len;
boot->NumFree = boot->NumBad = 0;
if (!_readfat(fs, boot, no, &buffer))
return FSFATAL;
fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
fat = malloc(len = boot->NumClusters * sizeof(struct fatEntry));
if (fat == NULL) {
perror("No space for FAT");
perror("No space for FAT clusters");
free(buffer);
return FSFATAL;
}
(void)memset(fat, 0, len);
if (buffer[0] != boot->Media
if (buffer[0] != boot->bpbMedia
|| buffer[1] != 0xff || buffer[2] != 0xff
|| (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
|| (boot->ClustMask == CLUST32_MASK
......@@ -160,10 +142,10 @@ readfat(fs, boot, no, fp)
/* 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
* filesystem is dirty if it doesn't reboot cleanly.
* file system is dirty if it doesn't reboot cleanly.
* Check this special condition before errorring out.
*/
if (buffer[0] == boot->Media && buffer[1] == 0xff
if (buffer[0] == boot->bpbMedia && buffer[1] == 0xff
&& buffer[2] == 0xff
&& ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
|| (boot->ClustMask == CLUST32_MASK
......@@ -176,7 +158,7 @@ readfat(fs, boot, no, fp)
}
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",
......@@ -249,9 +231,10 @@ readfat(fs, boot, no, fp)
done:
free(buffer);
if (ret)
if (ret) {
free(fat);
else
*fp = NULL;
} else
*fp = fat;
return ret;
}
/*
* Copyright (c) 2003-2014 University of Utah and the Flux Group.
* Copyright (c) 2003-2016 University of Utah and the Flux Group.
*
* {{{EMULAB-LICENSE
*
......@@ -63,10 +63,10 @@ read_fatslice(int slice, iz_type stype, iz_lba start, iz_size size,
boot.ClustMask == CLUST32_MASK ? 32 :
boot.ClustMask == CLUST16_MASK ? 16 : 12);
fatsecpersec = boot.BytesPerSec / secsize;
if (fatsecpersec * secsize != boot.BytesPerSec) {
fatsecpersec = boot.bpbBytesPerSec / secsize;
if (fatsecpersec * secsize != boot.bpbBytesPerSec) {
warnx("FAT Slice %d: FAT sector size (%d) not a multiple of %d",
slice+1, boot.BytesPerSec, secsize);
slice+1, boot.bpbBytesPerSec, secsize);
return 1;
}
......@@ -86,8 +86,8 @@ fat_addskip(struct bootblock *boot, int startcl, int ncl)
{
uint32_t start, size;
start = startcl * boot->SecPerClust + boot->ClusterOffset;
size = ncl * boot->SecPerClust;
start = startcl * boot->bpbSecPerClust + boot->ClusterOffset;
size = ncl * boot->bpbSecPerClust;
if (fatsecpersec != 1) {
start /= fatsecpersec;
size /= fatsecpersec;
......
/*
* Copyright (c) 2003 University of Utah and the Flux Group.
* Copyright (c) 2003-2016 University of Utah and the Flux Group.
*
* {{{EMULAB-LICENSE
*
......@@ -39,7 +39,7 @@
#define FSFIXFAT 64 /* Fix file system FAT */
int readboot(int fd, struct bootblock *boot);
int readfat(int fd, struct bootblock *boot, int no, struct fatEntry **fp);
int readfat(int fd, struct bootblock *boot, u_int no, struct fatEntry **fp);
void fat_addskip(struct bootblock *boot, int startcl, int ncl);
off_t fat_lseek(int fd, off_t off, int whence);
ssize_t devread(int fd, void *buf, size_t nbytes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment