Support for mkfatfs()

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@800 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2008-08-02 20:59:48 +00:00
parent 360cc0a648
commit 7089a04067
6 changed files with 495 additions and 6 deletions

View File

@ -34,6 +34,13 @@
############################################################################
ifeq ($(CONFIG_FS_FAT),y)
# Files required for FAT file system support
ASRCS +=
CSRCS += fs_fat32.c fs_fat32attrib.c fs_fat32util.c
# File required for mkfatfs utility function
ASRCS +=
CSRCS += fs_initmbr.c
endif

View File

@ -63,9 +63,9 @@
#define BS_RESVDSECCOUNT 14 /* 2@14: Reserved sector count: Usually 32 */
#define BS_NUMFATS 16 /* 1@16: Number of FAT data structures: always 2 */
#define BS_ROOTENTCNT 17 /* 2@17: FAT12/16: Must be 0 for FAT32 */
#define BS_TOTSEC16 19 /* 2@19: FAT12/16: Must be 0, see BS32_totsec32 */
#define BS_TOTSEC16 19 /* 2@19: FAT12/16: Must be 0, see BS_TOTSEC32 */
#define BS_MEDIA 21 /* 1@21: Media code: f0, f8, f9-fa, fc-ff */
#define BS_FATSZ16 22 /* 2@22: FAT12/16: Must be 0, see BS32_fatsz32 */
#define BS_FATSZ16 22 /* 2@22: FAT12/16: Must be 0, see BS_FATSZ32 */
#define BS_SECPERTRK 24 /* 2@24: Sectors per track geometry value */
#define BS_NUMHEADS 26 /* 2@26: Number of heads geometry value */
#define BS_HIDSEC 28 /* 4@28: Count of hidden sectors preceding FAT */
@ -112,6 +112,13 @@
#define BS_SIGNATURE 510 /* 2@510: Valid MBRs have 0x55aa here */
#define BOOT_SIGNATURE16 0xaa55
#define BOOT_SIGNATURE32 0xaa550000
/* The extended boot signature (BS16/32_BOOTSIG) */
#define EXTBOOT_SIGNATURE 0x29
/****************************************************************************
* Each FAT directory entry is 32-bytes long. The following define offsets
* relative to the beginning of a directory entry.
@ -212,7 +219,7 @@
#define DIR_GETNTRES(p) UBYTE_VAL(p,DIR_NTRES)
#define DIR_GETCRTTIMETENTH(p) UBYTE_VAL(p,DIR_CRTTIMETENTH)
#define MBR_PUTSECPERCLUS(p,v) UBYTE_PUT(p,BS_SECPERCLUS),v)
#define MBR_PUTSECPERCLUS(p,v) UBYTE_PUT(p,BS_SECPERCLUS,v)
#define MBR_PUTNUMFATS(p,v) UBYTE_PUT(p,BS_NUMFATS,v)
#define MBR_PUTMEDIA(p,v) UBYTE_PUT(p,BS_MEDIA,v)
#define MBR_PUTDRVNUM16(p,v) UBYTE_PUT(p,BS16_DRVNUM,v)

View File

@ -265,7 +265,7 @@ static int fat_checkfsinfo(struct fat_mountpt_s *fs)
if (FSI_GETLEADSIG(fs->fs_buffer) == 0x41615252 &&
FSI_GETSTRUCTSIG(fs->fs_buffer) == 0x61417272 &&
FSI_GETTRAILSIG(fs->fs_buffer) == 0xaa550000)
FSI_GETTRAILSIG(fs->fs_buffer) == BOOT_SIGNATURE32)
{
fs->fs_fsinextfree = FSI_GETFREECOUNT(fs->fs_buffer);
fs->fs_fsifreecount = FSI_GETNXTFREE(fs->fs_buffer);
@ -295,7 +295,7 @@ static int fat_checkbootrecord(struct fat_mountpt_s *fs)
* match the reported hardware sector size.
*/
if (MBR_GETSIGNATURE(fs->fs_buffer) != 0xaa55 ||
if (MBR_GETSIGNATURE(fs->fs_buffer) != BOOT_SIGNATURE16 ||
MBR_GETBYTESPERSEC(fs->fs_buffer) != fs->fs_hwsectorsize)
{
return -ENODEV;
@ -2322,7 +2322,7 @@ int fat_updatefsinfo(struct fat_mountpt_s *fs)
FSI_PUTSTRUCTSIG(fs->fs_buffer, 0x61417272);
FSI_PUTFREECOUNT(fs->fs_buffer, fs->fs_fsifreecount);
FSI_PUTNXTFREE(fs->fs_buffer, fs->fs_fsinextfree);
FSI_PUTTRAILSIG(fs->fs_buffer, 0xaa550000);
FSI_PUTTRAILSIG(fs->fs_buffer, BOOT_SIGNATURE32);
/* Then flush this to disk */

211
fs/fat/fs_initmbr.c Normal file
View File

@ -0,0 +1,211 @@
/****************************************************************************
* fs/fat/fs_initmbr.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <nuttx/fs.h>
#include <nuttx/fat.h>
#include <nuttx/mkfatfs.h>
#include "fs_internal.h"
#include "fs_fat32.h"
#include "fs_mkfatfs.h"
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Name: mkfatfs_initmbr
*
* Description:
* Initialize the sector image of a masterbood record
*
* Input:
* fmt - User specified format parameters
* var - Other format parameters that are not user specifiable
* sect - Allocated memory to hold the MBR
*
* Return:
* None; caller is responsible for providing valid parameters.
*
****************************************************************************/
void mkfatfs_initmbr(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, ubyte *sect)
{
memset(sect, 0, var->fv_sectorsize);
/* 3@0: Jump instruction to boot code */
memcpy(&sect[BS_JUMP], var->fv_jump, 3);
/* 8@3: Usually "MSWIN4.1" */
strcpy(&sect[BS_OEMNAME], "NUTTX ");
/* 2@11: Bytes per sector: 512, 1024, 2048, 4096 */
MBR_PUTBYTESPERSEC(sect, var->fv_sectorsize);
/* 1@13: Sectors per allocation unit: 2**n, n=0..7 */
MBR_PUTSECPERCLUS(sect, fmt->ff_clustsize);
/* 2@14: Reserved sector count: Usually 32 */
MBR_PUTRESVDSECCOUNT(sect, fmt->ff_rsvdseccount);
/* 1@16: Number of FAT data structures: always 2 */
MBR_PUTNUMFATS(sect, fmt->ff_nfats);
/* 2@17: FAT12/16: Must be 0 for FAT32 */
MBR_PUTROOTENTCNT(sect, fmt->ff_rootdirentries);
/* 2@19: FAT12/16: Must be 0, see BS_TOTSEC32. Handled with 4@32: Total count of sectors on the volume */
if (var->fv_nsectors >= 65536)
{
MBR_PUTTOTSEC32(sect, var->fv_nsectors);
}
else
{
MBR_PUTTOTSEC16(sect, (uint16)var->fv_nsectors);
}
/* 1@21: Media code: f0, f8, f9-fa, fc-ff */
MBR_PUTMEDIA(sect, FAT_DEFAULT_MEDIA_TYPE); /* Only "hard drive" supported */
/* 2@22: FAT12/16: Must be 0, see BS32_FATSZ32 -- handled in FAT specific logic */
/* 2@24: Sectors per track geometry value and 2@26: Number of heads geometry value */
MBR_PUTSECPERTRK(sect, FAT_DEFAULT_SECPERTRK);
MBR_PUTNUMHEADS(sect, FAT_DEFAULT_NUMHEADS);
/* 4@28: Count of hidden sectors preceding FAT */
MBR_PUTHIDSEC(sect, fmt->ff_hidsec);
/* 4@32: Total count of sectors on the volume -- handled above */
/* Most of the rest of the sector depends on the FAT size */
if (fmt->ff_fatsize != 32)
{
/* 2@22: FAT12/16: Must be 0, see BS32_FATSZ32 */
MBR_PUTFATSZ16(sect, (uint16)var->fv_fatlen);
/* The following fields are only valid for FAT12/16 */
/* 1@36: Drive number for MSDOS bootstrap -- left zero */
/* 1@37: Reserved (zero) */
/* 1@38: Extended boot signature: 0x29 if following valid */
MBR_PUTBOOTSIG16(sect, EXTBOOT_SIGNATURE);
/* 4@39: Volume serial number */
MBR_PUTVOLID16(sect, fmt->ff_volumeid);
/* 11@43: Volume label */
memcpy(&sect[BS16_VOLLAB], fmt->ff_volumelabel, 11);
/* 8@54: "FAT12 ", "FAT16 ", or "FAT " */
/* Boot code may be placed in the remainder of the sector */
memcpy(&sect[BS16_BOOTCODE], var->fv_bootcode, var->fv_bootcodesize);
}
else
{
/* The following fields are only valid for FAT32 */
/* 4@36: Count of sectors occupied by one FAT */
MBR_PUTFATSZ32(sect, var->fv_fatlen);
/* 2@40: 0-3:Active FAT, 7=0 both FATS, 7=1 one FAT -- left zero*/
/* 2@42: MSB:Major LSB:Minor revision number (0.0) -- left zero */
/* 4@44: Cluster no. of 1st cluster of root dir */
MBR_PUTROOTCLUS(sect, FAT32_DEFAULT_ROOT_CLUSTER);
/* 2@48: Sector number of fsinfo structure. Usually 1. */
MBR_PUTFSINFO(sect, FAT_DEFAULT_FSINFO_SECTOR);
/* 2@50: Sector number of boot record. Usually 6 */
MBR_PUTBKBOOTSEC(sect, fmt->ff_backupboot);
/* 12@52: Reserved (zero) */
/* 1@64: Drive number for MSDOS bootstrap -- left zero */
/* 1@65: Reserved (zero) */
/* 1@66: Extended boot signature: 0x29 if following valid */
MBR_PUTBOOTSIG32(sect, EXTBOOT_SIGNATURE);
/* 4@67: Volume serial number */
MBR_PUTVOLID32(sect, fmt->ff_volumeid);
/* 11@71: Volume label */
memcpy(&sect[BS32_VOLLAB], fmt->ff_volumelabel, 11);
/* 8@82: "FAT12 ", "FAT16 ", or "FAT " */
/* Boot code may be placed in the remainder of the sector */
memcpy(&sect[BS16_BOOTCODE], var->fv_bootcode, var->fv_bootcodesize);
}
/* The magic bytes at the end of the MBR are common to FAT12/16/32 */
/* 2@510: Valid MBRs have 0x55aa here */
MBR_PUTSIGNATURE(sect, BOOT_SIGNATURE16);
}

116
fs/fat/fs_mkfatfs.h Normal file
View File

@ -0,0 +1,116 @@
/****************************************************************************
* fs/fat/fs_mkfat.h
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 __FS_FAT_FS_MKATFS_H
#define __FS_FAT_FS_MKATFS_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Only the "hard drive" media type is used */
#define FAT_DEFAULT_MEDIA_TYPE 0xf8
/* Default hard driver geometry */
#define FAT_DEFAULT_SECPERTRK 63
#define FAT_DEFAULT_NUMHEADS 255
/* FSINFO is always at this sector */
#define FAT_DEFAULT_FSINFO_SECTOR 1
/* FAT32 foot cluster number */
#define FAT32_DEFAULT_ROOT_CLUSTER 2
/****************************************************************************
* Public Types
****************************************************************************/
/* This structure (plus the user-provided struct fat_format_s) describes
* the format FAT file system. All "global" variables used in the format
* logic are contained in this structure so that is possible to format two
* block devices concurrently.
*/
struct fat_var_s
{
ubyte fv_jump[3]; /* 3-byte boot jump instruction */
ubyte fv_sectshift; /* Log2 of fv_sectorsize */
uint16 fv_bootcodesize; /* Size of array at fv_bootcode */
uint32 fv_createtime; /* Creation time */
uint32 fv_sectorsize; /* Size of one hardware sector */
uint32 fv_nsectors; /* Number of sectors */
uint32 fv_fatlen; /* Size of the FAT */
ubyte *fv_rootdir; /* Allocated root directory sector */
ubyte *fv_mbr; /* Allocated master boot record image */
ubyte *fv_info; /* FAT32 info sector */
const ubyte *fv_bootcode; /* Points to boot code to put into MBR */
};
/****************************************************************************
* Global Variables
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
EXTERN void mkfatfs_initmbr(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, ubyte *sect)
;
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __FS_FAT_FS_MKATFS_H */

148
include/nuttx/mkfatfs.h Normal file
View File

@ -0,0 +1,148 @@
/****************************************************************************
* include/nuttx/mkfat.h
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 NuttX 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 COPYRIGHT HOLDERS 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
* COPYRIGHT OWNER 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 __NUTTX_MKFATFS_H
#define __NUTTX_MKFATFS_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
/****************************************************************************
* Definitions
****************************************************************************/
#define MKFATFS_DEFAULT_BBCHECK FALSE /* FALSE: No bad block check */
#define MKFATFS_DEFAULT_NFATS 2 /* 2: Default number of FATs */
#define MKFATFS_DEFAULT_FATSIZE 0 /* 0: Autoselect FAT size */
#define MKFATFS_DEFAULT_CLUSTSIZE 0 /* 0: Autoselect cluster size */
#define MKFATFS_DEFAULT_VOLUMELABEL { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }
#define MKFATFS_DEFAULT_BKUPBOOT 0 /* 0: Determine sector number of the backup boot sector */
#define MKFATFS_DEFAULT_ROOTDIRENTS 0 /* 0: Autoselect number of root directory entries */
#define MKFATFS_DEFAULT_RSVDSECCOUNT 0 /* 0: Autoselect number reserved sectors (usually 32) */
#define MKFATFS_DEFAULT_HIDSEC 0 /* No hidden sectors */
#define MKFATFS_DEFAULT_VOLUMEID 0 /* No volume ID */
#define MKFATFS_DEFAULT_MAXBLOCKS 0 /* 0: Use all blocks on device */
#define FAT_FORMAT_INITIALIZER \
{ \
MKFATFS_DEFAULT_BBCHECK, \
MKFATFS_DEFAULT_NFAT, \
MKFATFS_DEFAULT_FATSIZE, \
MKFATFS_DEFAULT_CLUSTSIZE, \
MKFATFS_DEFAULT_VOLUMELABEL, \
MKFATFS_DEFAULT_BKUPBOOT, \
MKFATFS_DEFAULT_ROOTDIRENTS, \
MKFATFS_DEFAULT_RSVDSECCOUNT, \
MKFATFS_DEFAULT_HIDSEC, \
MKFATFS_DEFAULT_VOLUMEID, \
MKFATFS_DEFAULT_MAXBLOCKS \
}
/****************************************************************************
* Public Types
****************************************************************************/
/* These are input parameters for the format. On return, these values may be
* overwritted with actual values used in the format.
*/
struct fat_format_s
{
boolean ff_bbcheck; /* TRUE: check for bad blocks */
ubyte ff_nfats; /* Number of FATs */
ubyte ff_fatsize; /* FAT size: 0 (autoselect), 12, 16, or 32 */
ubyte ff_clustsize; /* Number of sectors per cluster: 0 (autoselect) */
ubyte ff_volumelabel[11]; /* Volume label */
uint16 ff_backupboot; /* Sector number of the backup boot sector (0=use default)*/
uint16 ff_rootdirentries; /* Number of root directory entries */
uint16 ff_rsvdseccount; /* Reserved sectors */
uint32 ff_hidsec; /* Count of hidden sectors preceding fat */
uint32 ff_volumeid; /* FAT volume id */
uint32 ff_maxblocks; /* Number of blocks from device to use: 0: Use all */
};
/****************************************************************************
* Global Variables
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: mkfatfs
*
* Description:
* Make a FAT file system image on the specified block device
*
* Inputs:
* pathname - the full path to a registered block driver
* fmt - Describes characteristics of the desired filesystem
*
* Return:
* Zero (OK) on success; -1 (ERROR) on failure with errno set appropriately:
*
* EINVAL - NULL block driver string, bad number of FATS in 'fmt', bad FAT
* size in 'fmt', bad cluster size in 'fmt'
* ENOENT - 'pathname' does not refer to anything in the filesystem.
* ENOTBLK - 'pathname' does not refer to a block driver
* ENOSPC - block driver does not support geometry method
*
* Assumptions:
* - The caller must assure that the block driver is not mounted and not in
* use when this function is called. The result of formatting a mounted
* device is indeterminate (but likely not good).
*
****************************************************************************/
EXTERN int mkfatfs(FAR const char *pathname, FAR struct fat_format_s *fmt);
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __NUTTX_MKFATFS_H */