From 9b31a81b00ca5496e6a7dd79a28727a0fd920306 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 20 Oct 2017 12:36:25 -0600 Subject: [PATCH] Squashed commit of the following: fs/fat: Remove mkfatfs from the OS. This is a user-space application and belongs in apps, not in the OS. --- Documentation/NuttShell.html | 4 +- Documentation/NuttxUserGuide.html | 89 +- TODO | 7 - configs/ea3131/src/Makefile | 3 - configs/ea3131/src/lpc31_usbmsc.c | 127 --- configs/ea3152/src/Makefile | 3 - configs/ea3152/src/lpc31_usbmsc.c | 128 --- configs/stm3220g-eval/ide/nsh/iar/libfs.ewp | 9 - .../ide/nsh/uvision/libfs.uvproj | 15 - configs/stm32l476vg-disco/src/stm32_appinit.c | 1 - fs/fat/Make.defs | 7 +- fs/fat/fs_configfat.c | 998 ------------------ fs/fat/fs_mkfatfs.c | 343 ------ fs/fat/fs_mkfatfs.h | 169 --- fs/fat/fs_writefat.c | 544 ---------- include/nuttx/fs/mkfatfs.h | 149 --- 16 files changed, 7 insertions(+), 2589 deletions(-) delete mode 100644 configs/ea3131/src/lpc31_usbmsc.c delete mode 100644 configs/ea3152/src/lpc31_usbmsc.c delete mode 100644 fs/fat/fs_configfat.c delete mode 100644 fs/fat/fs_mkfatfs.c delete mode 100644 fs/fat/fs_mkfatfs.h delete mode 100644 fs/fat/fs_writefat.c delete mode 100644 include/nuttx/fs/mkfatfs.h diff --git a/Documentation/NuttShell.html b/Documentation/NuttShell.html index 5e66ce4ae23..7414b5bbdf4 100644 --- a/Documentation/NuttShell.html +++ b/Documentation/NuttShell.html @@ -8,7 +8,7 @@

NuttShell (NSH)

-

Last Updated: August 11, 2017

+

Last Updated: October 20, 2017

@@ -3481,7 +3481,7 @@ nsh> mkfatfs - !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FS_FAT + !CONFIG_DISABLE_MOUNTPOINT && CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_FSUTILS_MKFATFS CONFIG_NSH_DISABLE_MKFATFS diff --git a/Documentation/NuttxUserGuide.html b/Documentation/NuttxUserGuide.html index 113193e3c28..92054fec8eb 100644 --- a/Documentation/NuttxUserGuide.html +++ b/Documentation/NuttxUserGuide.html @@ -13,7 +13,7 @@

NuttX Operating System

User's Manual

by

Gregory Nutt

-

Last Updated: October 2, 2017

+

Last Updated: October 20, 2017

@@ -8404,8 +8404,7 @@ interface of the same name.
  • 2.10.7 Asynchronous I/O
  • 2.10.8 Standard String Operations
  • 2.10.9 Pipes and FIFOs
  • -
  • 2.10.10 FAT File System Support
  • -
  • 2.10.11 mmap() and eXecute In Place (XIP)
  • +
  • 2.10.10 mmap() and eXecute In Place (XIP)
  • 2.10.1 NuttX File System Overview

    @@ -8957,85 +8956,7 @@ int mkfifo(FAR const char *pathname, mode_t mode);

    -

    2.10.10 FAT File System Support

    -

    2.10.10.1 mkfatfs

    -

    - Function Prototype: -

    - -

    - Description: -

    -

    -

    - Input Parameters: -

    -

    -

    - Returned Value: -

    -

    - -

    2.10.11 mmap() and eXecute In Place (XIP)

    +

    2.10.10 mmap() and eXecute In Place (XIP)

    NuttX operates in a flat open address space and is focused on MCUs that do support Memory Management Units (MMUs). Therefore, NuttX generally does not @@ -9170,8 +9091,7 @@ struct fat_format_s

    Description: @@ -10525,7 +10445,6 @@ notify a task when a message is available on a queue.

  • lseek
  • Named Message Queue Interfaces
  • mkdir
  • -
  • mkfatfs
  • mkfifo
  • mktime
  • mq_close
  • diff --git a/TODO b/TODO index 74336aef692..46c18a49228 100644 --- a/TODO +++ b/TODO @@ -645,17 +645,10 @@ o Kernel/Protected Build COMMAND KERNEL INTERFACE(s) -------- ---------------------------------------------- - mkfatfs mkfatfs mkrd ramdisk_register() ping icmp_ping() mount foreach_mountpoint() - The busybox mkfatfs does not involve any OS calls; it does - its job by simply opening the block driver (using open/xopen) - and modifying it with write operations. See: - - http://git.busybox.net/busybox/tree/util-linux/mkfs_vfat.c - Status: Open Priority: Medium/High -- the kernel build configuration is not fully fielded yet. diff --git a/configs/ea3131/src/Makefile b/configs/ea3131/src/Makefile index 3685f09453e..28556dab9cc 100644 --- a/configs/ea3131/src/Makefile +++ b/configs/ea3131/src/Makefile @@ -66,9 +66,6 @@ ifeq ($(CONFIG_LPC31_USBOTG),y) ifeq ($(CONFIG_USBHOST),y) CSRCS += lpc31_usbhost.c endif -ifeq ($(CONFIG_USBMSC),y) -CSRCS += lpc31_usbmsc.c -endif endif include $(TOPDIR)/configs/Board.mk diff --git a/configs/ea3131/src/lpc31_usbmsc.c b/configs/ea3131/src/lpc31_usbmsc.c deleted file mode 100644 index 0965cf6a57a..00000000000 --- a/configs/ea3131/src/lpc31_usbmsc.c +++ /dev/null @@ -1,127 +0,0 @@ -/**************************************************************************** - * configs/ea3131/src/lpc31_usbmsc.c - * - * Copyright (C) 2010, 2013, 2015-2016 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Configure and register the SAM3U MMC/SD SDIO block driver. - * - * 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 - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* Configuration ************************************************************/ - -#ifndef CONFIG_SYSTEM_USBMSC_DEVMINOR1 -# define CONFIG_SYSTEM_USBMSC_DEVMINOR1 0 -#endif - -#ifndef CONFIG_SYSTEM_USBMSC_DEVPATH1 -# define CONFIG_SYSTEM_USBMSC_DEVPATH1 "/dev/ram" -#endif - -static const char g_source[] = CONFIG_SYSTEM_USBMSC_DEVPATH1; -static struct fat_format_s g_fmt = FAT_FORMAT_INITIALIZER; - -#define USBMSC_NSECTORS 64 -#define USBMSC_SECTORSIZE 512 -#define BUFFER_SIZE (USBMSC_NSECTORS*USBMSC_SECTORSIZE) - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: board_usbmsc_initialize - * - * Description: - * Perform architecture specific initialization of the USB MSC device. - * - ****************************************************************************/ - -int board_usbmsc_initialize(int port) -{ - uint8_t *pbuffer; - int ret; - - pbuffer = (uint8_t *)kmm_malloc(BUFFER_SIZE); - if (!pbuffer) - { - err("ERROR: Failed to allocate ramdisk of size %d\n", BUFFER_SIZE); - return -ENOMEM; - } - - /* Register a RAMDISK device to manage this RAM image */ - - ret = ramdisk_register(CONFIG_SYSTEM_USBMSC_DEVMINOR1, - pbuffer, - USBMSC_NSECTORS, - USBMSC_SECTORSIZE, - RDFLAG_WRENABLED | RDFLAG_FUNLINK); - if (ret < 0) - { - err("ERROR: create_ramdisk: Failed to register ramdisk at %s: %d\n", - g_source, -ret); - kmm_free(pbuffer); - return ret; - } - - /* Create a FAT filesystem on the ramdisk */ - - ret = mkfatfs(g_source, &g_fmt); - if (ret < 0) - { - err("ERROR: create_ramdisk: Failed to create FAT filesystem on ramdisk at %s\n", - g_source); - /* kmm_free(pbuffer); -- RAM disk is registered */ - return ret; - } - - return 0; -} diff --git a/configs/ea3152/src/Makefile b/configs/ea3152/src/Makefile index 2b5a68725a1..6b445f6187e 100644 --- a/configs/ea3152/src/Makefile +++ b/configs/ea3152/src/Makefile @@ -55,8 +55,5 @@ endif ifeq ($(CONFIG_PAGING),y) CSRCS += lpc31_fillpage.c endif -ifeq ($(CONFIG_USBMSC),y) -CSRCS += lpc31_usbmsc.c -endif include $(TOPDIR)/configs/Board.mk diff --git a/configs/ea3152/src/lpc31_usbmsc.c b/configs/ea3152/src/lpc31_usbmsc.c deleted file mode 100644 index 7e4909c838d..00000000000 --- a/configs/ea3152/src/lpc31_usbmsc.c +++ /dev/null @@ -1,128 +0,0 @@ -/**************************************************************************** - * configs/ea3152/src/lpc31_usbmsc.c - * - * Copyright (C) 2011, 2015-2016 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Configure and register the SAM3U MMC/SD SDIO block driver. - * - * 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 - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/* Configuration ************************************************************/ - -#ifndef CONFIG_SYSTEM_USBMSC_DEVMINOR1 -# define CONFIG_SYSTEM_USBMSC_DEVMINOR1 0 -#endif - -#ifndef CONFIG_SYSTEM_USBMSC_DEVPATH1 -# define CONFIG_SYSTEM_USBMSC_DEVPATH1 "/dev/ram" -#endif - -static const char g_source[] = CONFIG_SYSTEM_USBMSC_DEVPATH1; -static struct fat_format_s g_fmt = FAT_FORMAT_INITIALIZER; - -#define USBMSC_NSECTORS 64 -#define USBMSC_SECTORSIZE 512 -#define BUFFER_SIZE (USBMSC_NSECTORS*USBMSC_SECTORSIZE) - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: board_usbmsc_initialize - * - * Description: - * Perform architecture specific initialization of the USB MSC device. - * - ****************************************************************************/ - -int board_usbmsc_initialize(int port) -{ - uint8_t *pbuffer; - int ret; - - pbuffer = (uint8_t *)kmm_malloc(BUFFER_SIZE); - if (!pbuffer) - { - err("ERROR: board_usbmsc_initialize: Failed to allocate ramdisk of size %d\n", - BUFFER_SIZE); - return -ENOMEM; - } - - /* Register a RAMDISK device to manage this RAM image */ - - ret = ramdisk_register(CONFIG_SYSTEM_USBMSC_DEVMINOR1, - pbuffer, - USBMSC_NSECTORS, - USBMSC_SECTORSIZE, - RDFLAG_WRENABLED | RDFLAG_FUNLINK); - if (ret < 0) - { - err("ERROR: create_ramdisk: Failed to register ramdisk at %s: %d\n", - g_source, -ret); - kmm_free(pbuffer); - return ret; - } - - /* Create a FAT filesystem on the ramdisk */ - - ret = mkfatfs(g_source, &g_fmt); - if (ret < 0) - { - err("ERROR: create_ramdisk: Failed to create FAT filesystem on ramdisk at %s\n", - g_source); - /* kmm_free(pbuffer); -- RAM disk is registered */ - return ret; - } - - return 0; -} diff --git a/configs/stm3220g-eval/ide/nsh/iar/libfs.ewp b/configs/stm3220g-eval/ide/nsh/iar/libfs.ewp index 34cae3da185..9edeaf6a4d8 100644 --- a/configs/stm3220g-eval/ide/nsh/iar/libfs.ewp +++ b/configs/stm3220g-eval/ide/nsh/iar/libfs.ewp @@ -2106,14 +2106,5 @@ $PROJ_DIR$/../../../../../fs/fat/fs_fat32util.c - - $PROJ_DIR$/../../../../../fs/fat/fs_mkfatfs.c - - - $PROJ_DIR$/../../../../../fs/fat/fs_configfat.c - - - $PROJ_DIR$/../../../../../fs/fat/fs_writefat.c - diff --git a/configs/stm3220g-eval/ide/nsh/uvision/libfs.uvproj b/configs/stm3220g-eval/ide/nsh/uvision/libfs.uvproj index 1945e26f34b..7ce92b3285c 100644 --- a/configs/stm3220g-eval/ide/nsh/uvision/libfs.uvproj +++ b/configs/stm3220g-eval/ide/nsh/uvision/libfs.uvproj @@ -608,21 +608,6 @@ 1 ../../../../../fs/fat/fs_fat32util.c - - fs_mkfatfs.c - 1 - ../../../../../fs/fat/fs_mkfatfs.c - - - fs_configfat.c - 1 - ../../../../../fs/fat/fs_configfat.c - - - fs_writefat.c - 1 - ../../../../../fs/fat/fs_writefat.c - diff --git a/configs/stm32l476vg-disco/src/stm32_appinit.c b/configs/stm32l476vg-disco/src/stm32_appinit.c index 00c41a1985d..f921d940b18 100644 --- a/configs/stm32l476vg-disco/src/stm32_appinit.c +++ b/configs/stm32l476vg-disco/src/stm32_appinit.c @@ -61,7 +61,6 @@ #include #include #include -#include #include #include diff --git a/fs/fat/Make.defs b/fs/fat/Make.defs index 191a1fcf65c..e70038696d4 100644 --- a/fs/fat/Make.defs +++ b/fs/fat/Make.defs @@ -1,7 +1,7 @@ ############################################################################ # Make.defs # -# Copyright (C) 2008, 2011, 2013 Gregory Nutt. All rights reserved. +# Copyright (C) 2008, 2011, 2013, 2017 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -39,11 +39,6 @@ ifeq ($(CONFIG_FS_FAT),y) ASRCS += CSRCS += fs_fat32.c fs_fat32dirent.c fs_fat32attrib.c fs_fat32util.c -# Files required for mkfatfs utility function - -ASRCS += -CSRCS += fs_mkfatfs.c fs_configfat.c fs_writefat.c - # Include FAT build support DEPPATH += --dep-path fat diff --git a/fs/fat/fs_configfat.c b/fs/fat/fs_configfat.c deleted file mode 100644 index 11fc358e2ee..00000000000 --- a/fs/fat/fs_configfat.c +++ /dev/null @@ -1,998 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_configfat.c - * - * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 - -#include -#include -#include -#include - -#include -#include -#include - -#include "inode/inode.h" -#include "fs_fat32.h" -#include "fs_mkfatfs.h" - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define NDX12 0 -#define NDX16 1 -#define NDX32 2 - -#define fatconfig12 fatconfig[NDX12] -#define fatconfig16 fatconfig[NDX16] -#define fatconfig32 fatconfig[NDX32] - -/* JMP rel8 and NOP opcodes */ - -#define OPCODE_JMP_REL8 0xeb -#define OPCODE_NOP 0x90 - -#define BOOTCODE_MSGOFFSET 29 - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -struct fat_config_s -{ - uint32_t fc_navailsects; /* The number of available sectors */ - uint32_t fc_nfatsects; /* The number of sectors in one FAT */ - uint32_t fc_nclusters; /* The number of clusters in the filesystem */ - uint32_t fc_rsvdseccount; /* The number of reserved sectors */ -}; - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/* Reverse engineered, generic boot message logic for non-bootable disk. - * Message begins at offset 29; Sector relative offset must be poked into - * offset 3. - */ - -static uint8_t g_bootcodeblob[] = -{ - 0x0e, 0x1f, 0xbe, 0x00, 0x7c, 0xac, 0x22, 0xc0, 0x74, 0x0b, 0x56, - 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0x5e, 0xeb, 0xf0, 0x32, - 0xe4, 0xcd, 0x16, 0xcd, 0x19, 0xeb, 0xfe, 0x54, 0x68, 0x69, 0x73, - 0x20, 0x69, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x61, 0x20, 0x62, - 0x6f, 0x6f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x64, 0x69, 0x73, - 0x6b, 0x2e, 0x20, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, - 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x61, 0x20, 0x62, 0x6f, - 0x6f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x6c, 0x6f, 0x70, - 0x70, 0x79, 0x20, 0x61, 0x6e, 0x64, 0x0d, 0x0a, 0x70, 0x72, 0x65, - 0x73, 0x73, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, - 0x74, 0x6f, 0x20, 0x74, 0x72, 0x79, 0x20, 0x61, 0x67, 0x61, 0x69, - 0x6e, 0x20, 0x2e, 0x2e, 0x2e, 0x0d, 0x0a, 0x00 -}; - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_nfatsect12 - * - * Description: - * Calculate the number of sectors need for one fat in a FAT12 file system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * navailsects - The number of sectors available for both FAT and data. - * This is a precalculated value equal to the total number of sectors - * minus the number of root directory sectors and minus the number of - * reserved sectors. - * - * Return: - * 0: That calculation would have overflowed - * >0: The size of one FAT in sectors. - * - ****************************************************************************/ - -static inline uint32_t -mkfatfs_nfatsect12(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - uint32_t navailsects) -{ -#ifdef CONFIG_HAVE_LONG_LONG - uint64_t denom; - uint64_t numer; -#else - uint32_t denom; - uint32_t numer; -#endif - - /* For FAT12, the cluster number is held in a 12-bit number or 1.5 bytes per - * cluster reference. So each FAT sector will hold sectorsize/1.5 cluster - * references (except for the first sector of each FAT which has two reserved - * 12-bit values). And the total number of FAT sectors needed is: - * - * nfatsects = (1.5 * (ndataclust + 2) / sectorsize) - * - * where: - * - * ndataclust = ndatasect / clustsize - * nvailsects = nfatsects + ndatasect - * - * The solution to this set of linear equations is: - * - * nfatsects = (3 * navailsects + 6 * clustersize) / - * (3 * nfats + 2 * sectorsize * clustersize) - * - * The numerator would overflow uint32_t if: - * - * 3 * navailsects + 6 * clustersize > 0xffffffff - * - * Or - * - * navailsects > 0x55555555 - 2 * clustersize - */ - -#ifndef CONFIG_HAVE_LONG_LONG - if (navailsects <= (0x55555555 - (1 << (fmt->ff_clustshift + 1)))) - { -#endif - - denom = (fmt->ff_nfats << 1) + fmt->ff_nfats - + (var->fv_sectorsize << (fmt->ff_clustshift + 1)); - numer = (navailsects << 1) + navailsects - + (1 << (fmt->ff_clustshift + 2)) + (1 << (fmt->ff_clustshift + 1)); - return (uint32_t)((numer + denom - 1) / denom); - -#ifndef CONFIG_HAVE_LONG_LONG - } - else - { - return 0; - } -#endif -} - -/**************************************************************************** - * Name: mkfatfs_nfatsect16 - * - * Description: - * Calculate the number of sectors need for one fat in a FAT16 file system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * navailsects - The number of sectors available for both FAT and data. - * This is a precalculated value equal to the total number of sectors - * minus the number of root directory sectors and minus the number of - * reserved sectors. - * - * Return: - * The size of one FAT in sectors. - * - ****************************************************************************/ - -static inline uint32_t -mkfatfs_nfatsect16(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - uint32_t navailsects) -{ -#ifdef CONFIG_HAVE_LONG_LONG - uint64_t denom; - uint64_t numer; -#else - uint32_t denom; - uint32_t numer; -#endif - - /* For FAT16, the cluster number is held in a 16-bit number or 2 bytes per - * cluster reference. So each FAT sector will hold sectorsize/2 cluster - * references (except for the first sector of each FAT which has two reserved - * 16-bit values). And the total number of FAT sectors needed is: - * - * nfatsects = (2 * (ndataclust + 2) / sectorsize) - * - * where: - * - * ndataclust = ndatasect / clustsize - * nvailsects = nfatsects + ndatasect - * - * The solution to this set of linear equations is: - * - * nfatsects = (navailsects + 2 * clustersize) / - * (nfats + sectorsize * clustersize / 2) - * - * Overflow in the calculation of the numerator could occur if: - * - * navailsects > 0xffffffff - 2 * clustersize - */ - - if (fmt->ff_clustshift == 0) - { - denom = fmt->ff_nfats + (var->fv_sectorsize >> 1); - numer = navailsects + 2; - } - else - { - denom = fmt->ff_nfats + (var->fv_sectorsize << (fmt->ff_clustshift - 1)); - numer = navailsects + (1 << (fmt->ff_clustshift + 1)); - } - - return (uint32_t)((numer + denom - 1) / denom); -} - -/**************************************************************************** - * Name: mkfatfs_nfatsect32 - * - * Description: - * Calculate the number of sectors need for one fat in a FAT32 file system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * navailsects - The number of sectors available for both FAT and data. - * This is a precalculated value equal to the total number of sectors - * minus the number of root directory sectors and minus the number of - * reserved sectors. - * - * Return: - * The size of one FAT in sectors. - * - ****************************************************************************/ - -static inline uint32_t -mkfatfs_nfatsect32(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - uint32_t navailsects) -{ -#ifdef CONFIG_HAVE_LONG_LONG - uint64_t denom; - uint64_t numer; -#else - uint32_t denom; - uint32_t numer; -#endif - - /* For FAT32, the cluster number is held in a 32-bit number or 4 bytes per - * cluster reference. So each FAT sector will hold sectorsize/4 cluster - * references (except for the first sector of each FAT which has three reserved - * 32-bit values). And the total number of FAT sectors needed is: - * - * nfatsects = (4 * (ndataclust + 3) / sectorsize) - * - * where: - * - * ndataclust = ndatasect / clustsize - * nvailsects = nfatsects + ndatasect - * - * The solution to this set of linear equations is: - * - * nfatsects = (navailsects + 3 * clustersize) / - * (nfats + sectorsize * clustersize / 4) - * - * Overflow in the 32-bit calculation of the numerator could occur if: - * - * navailsects > 0xffffffff - 3 * clustersize - */ - - if (fmt->ff_clustshift == 0) - { - denom = fmt->ff_nfats + (var->fv_sectorsize >> 2); - numer = navailsects + 3; - } - else if (fmt->ff_clustshift == 1) - { - denom = fmt->ff_nfats + (var->fv_sectorsize >> 1); - numer = navailsects + 6; - } - else - { - denom = fmt->ff_nfats + (var->fv_sectorsize << (fmt->ff_clustshift - 2)); - numer = navailsects + (1 << (fmt->ff_clustshift + 1)) + (1 << fmt->ff_clustshift); - } - - return (uint32_t)((numer + denom - 1) / denom); -} - -/**************************************************************************** - * Name: mkfatfs_clustersearchlimits - * - * Description: - * Pick the starting and ending cluster size to use in the search for the - * the optimal cluster size. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Starting cluster size is set in fmt->ff_clustshift; Final cluster - * size is the returned value. - * - ****************************************************************************/ - -static inline uint8_t -mkfatfs_clustersearchlimits(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var) -{ - uint8_t mxclustshift; - - /* Did the caller already pick the cluster size? If not, the clustshift value - * will be 0xff - */ - - if (fmt->ff_clustshift == 0xff) - { - /* Pick a starting size based on the number of sectors on the device */ - - if (fmt->ff_nsectors < 2048) - { - /* 2k sectors, start wit 1 sector/cluster. */ - fmt->ff_clustshift = 0; - } - else if (fmt->ff_nsectors < 4096) - { - /* 4k sectors, start with 2 sector/cluster. */ - fmt->ff_clustshift = 1; - } - else if (fmt->ff_nsectors < 8192) - { - /* 8k sectors, start with 4 sector/cluster. */ - fmt->ff_clustshift = 2; - } - else if (fmt->ff_nsectors < 16384) - { - /* 16k sectors, start with 8 sector/cluster. */ - fmt->ff_clustshift = 3; - } - else if (fmt->ff_nsectors < 32768) - { - /* 32k sectors, start with 16 sector/cluster. */ - fmt->ff_clustshift = 4; - } - else - { - /* Otherwise, 32 sector/cluster. */ - fmt->ff_clustshift = 5; - } - - /* Wherever the search starts, it will end with the maximum of - * 128 sectors per cluster - */ - - mxclustshift = 7; - } - else - { - /* The caller has selected a cluster size. There will be no search! - * Just set the maximum to the caller specificed value. - */ - - mxclustshift = fmt->ff_clustshift; - } - - return mxclustshift; -} - -/**************************************************************************** - * Name: mkfatfs_tryfat12 - * - * Description: - * Try to define a FAT12 filesystem on the device using the candidate - * sectors per cluster - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * config - FAT12-specific configuration - * - * Return: - * Zero on success configuration of a FAT12 file system; negated errno - * on failure - * - ****************************************************************************/ - -static inline int -mkfatfs_tryfat12(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - FAR struct fat_config_s *config) -{ - uint32_t maxnclusters; - - /* Calculate the number sectors in one FAT required to access all of the - * available sectors. - */ - - config->fc_nfatsects = mkfatfs_nfatsect12(fmt, var, config->fc_navailsects); - if (config->fc_nfatsects > 0) - { - /* Calculate the number of clusters available given the number of available - * sectors and the number of those that will be used for FAT: - */ - - config->fc_nclusters = - (config->fc_navailsects - - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; - - /* Calculate the maximum number of clusters that could be supported by a - * FAT of this size. - * - * maxnclusters = nfatsects * sectorsize / 1.5 - 2 - */ - - maxnclusters = (config->fc_nfatsects << (var->fv_sectshift + 1)) / 3; - if (maxnclusters > FAT_MAXCLUST12) - { - maxnclusters = FAT_MAXCLUST12; - } - - finfo("nfatsects=%u nclusters=%u (max=%u)\n", - config->fc_nfatsects, config->fc_nclusters, maxnclusters); - - /* Check if this number of clusters would overflow the maximum for - * FAT12 (remembering that two FAT cluster slots are reserved). - */ - - if (config->fc_nclusters + 2 > maxnclusters) - { - ferr("ERROR: Too many clusters for FAT12: %d > %d\n", - config->fc_nclusters, maxnclusters - 2); - - return -ENFILE; - } - } - - return 0; -} - -/**************************************************************************** - * Name: mkfatfs_tryfat16 - * - * Description: - * Try to define a FAT16 filesystem on the device using the candidate - * sectors per cluster - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * config - FAT16-specific configuration - * - * Return: - * Zero on success configuration of a FAT16 file system; negated errno - * on failure - * - ****************************************************************************/ - -static inline int -mkfatfs_tryfat16(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - FAR struct fat_config_s *config) -{ - uint32_t maxnclusters; - - /* Calculate the number sectors in one FAT required to access all of the - * available sectors. - */ - - config->fc_nfatsects = mkfatfs_nfatsect16(fmt, var, config->fc_navailsects); - if (config->fc_nfatsects > 0) - { - /* Calculate the number of clusters available given the number of available - * sectors and the number of those that will be used for FAT: - */ - - config->fc_nclusters = - (config->fc_navailsects - - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; - - /* Calculate the maximum number of clusters that could be supported by a - * FAT of this size. - * - * maxnclusters = nfatsects * sectorsize / 2 - 2 - */ - - maxnclusters = config->fc_nfatsects << (var->fv_sectshift - 1); - if (maxnclusters > FAT_MAXCLUST16) - { - maxnclusters = FAT_MAXCLUST16; - } - - finfo("nfatsects=%u nclusters=%u (min=%u max=%u)\n", - config->fc_nfatsects, config->fc_nclusters, FAT_MINCLUST16, - maxnclusters); - - /* Check if this number of clusters would overflow the maximum for - * FAT16 (remembering that two FAT cluster slots are reserved). - * Check the lower limit as well. The FAT12 is distinguished from FAT16 - * by comparing the number of clusters on the device agains a known - * threshold. If a small FAT16 file system were created, then it would - * be confused as a FAT12 at mount time. - */ - - if ((config->fc_nclusters + 2 > maxnclusters) || - (config->fc_nclusters < FAT_MINCLUST16)) - { - ferr("ERROR: Too few or too many clusters for FAT16: %d < %d < %d\n", - FAT_MINCLUST16, config->fc_nclusters, maxnclusters - 2); - - return -ENFILE; - } - } - - return 0; -} - -/**************************************************************************** - * Name: mkfatfs_tryfat32 - * - * Description: - * Try to define a FAT32 filesystem on the device using the candidate - * sectors per cluster - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * config - FAT32-specific configuration - * - * Return: - * Zero on success configuration of a FAT32 file system; negated errno - * on failure - * - ****************************************************************************/ - -static inline int -mkfatfs_tryfat32(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var, - FAR struct fat_config_s *config) -{ - uint32_t maxnclusters; - - /* Calculate the number sectors in one FAT required to access all of the - * available sectors. - */ - - config->fc_nfatsects = mkfatfs_nfatsect32(fmt, var, config->fc_navailsects); - if (config->fc_nfatsects > 0) - { - /* Calculate the number of clusters available given the number of available - * sectors and the number of those that will be used for FAT: - */ - - config->fc_nclusters = - (config->fc_navailsects - - fmt->ff_nfats * config->fc_nfatsects) >> fmt->ff_clustshift; - - /* Calculate the maximum number of clusters that could be supported by a - * FAT of this size. - * - * maxnclusters = nfatsects * sectorsize / 4 - 2 - */ - - maxnclusters = (config->fc_nfatsects << (var->fv_sectshift - 2)); - if (maxnclusters > FAT_MAXCLUST32) - { - maxnclusters = FAT_MAXCLUST32; - } - - finfo("nfatsects=%u nclusters=%u (max=%u)\n", - config->fc_nfatsects, config->fc_nclusters, maxnclusters); - - /* Check if this number of clusters would overflow the maximum for - * FAT32 (remembering that two FAT cluster slots are reserved). - */ - - if ((config->fc_nclusters + 3 > maxnclusters) || - (config->fc_nclusters < FAT_MINCLUST32)) - { - ferr("ERROR: Too few or too many clusters for FAT32: %d < %d < %d\n", - FAT_MINCLUST32, config->fc_nclusters, maxnclusters - 3); - - return -ENFILE; - } - } - - return 0; -} - -/**************************************************************************** - * Name: mkfatfs_selectfat - * - * Description: - * The cluster search has succeeded, select the specified FAT FS - * - * Input: - * fattype - The FAT size selected - * fmt - Caller specified format parameters - * var - Format parameters that are not caller specifiable. - * - * Return: - * None - * - ****************************************************************************/ - -static inline void -mkfatfs_selectfat(int fattype, FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var, FAR struct fat_config_s *config) -{ - /* Return the appropriate information about the selected file system. */ - - finfo("Selected FAT%d\n", fattype); - - var->fv_fattype = fattype; - var->fv_nclusters = config->fc_nclusters; - var->fv_nfatsects = config->fc_nfatsects; - fmt->ff_rsvdseccount = config->fc_rsvdseccount; -} - -/**************************************************************************** - * Name: mkfatfs_clustersearch - * - * Description: - * Search to find the smallest (reasonable) cluster size for the FAT file - * system. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int -mkfatfs_clustersearch(FAR struct fat_format_s *fmt, FAR struct fat_var_s *var) -{ - struct fat_config_s fatconfig[3]; - uint8_t mxclustshift; - - memset(fatconfig, 0, 3*sizeof(struct fat_config_s)); - - /* Select the reserved sector count for each FAT size */ - - if (fmt->ff_rsvdseccount) - { - fatconfig12.fc_rsvdseccount = fmt->ff_rsvdseccount; - fatconfig16.fc_rsvdseccount = fmt->ff_rsvdseccount; - - if (fmt->ff_rsvdseccount < 2) - { - ferr("ERROR: At least 2 reserved sectors needed by FAT32\n"); - fatconfig32.fc_rsvdseccount = 2; - } - else - { - fatconfig32.fc_rsvdseccount = fmt->ff_rsvdseccount; - } - } - else - { - fatconfig12.fc_rsvdseccount = 1; - fatconfig16.fc_rsvdseccount = 1; - fatconfig32.fc_rsvdseccount = 32; - } - - /* Determine the number of sectors needed by the root directory. - * This is a constant value, independent of cluster size for FAT12/16 - */ - - if (var->fv_fattype != 32) - { - /* Calculate the number of sectors reqired to contain the selected - * number of root directory entries. This value is save in the var - * structure but will be overwritten if FAT32 is selected. FAT32 uses - * a cluster chain for the root directory, so the concept of the number - * of root directory entries does not apply to FAT32 - */ - - var->fv_nrootdirsects = - ((fmt->ff_rootdirentries << DIR_SHIFT) + var->fv_sectorsize - 1) >> var->fv_sectshift; - - /* The number of data sectors available (includes the fat itself) - * This value is a constant for FAT12/16, but not FAT32 because the - * size of the root directory cluster changes - */ - - fatconfig12.fc_navailsects = - fatconfig16.fc_navailsects = - fmt->ff_nsectors - var->fv_nrootdirsects - fatconfig12.fc_rsvdseccount; - } - - /* Select an initial and terminal cluster size to use in the search (if these - * values were not provided by the caller) - */ - - mxclustshift = mkfatfs_clustersearchlimits(fmt, var); - - do - { - finfo("Configuring with %d sectors/cluster...\n", - 1 << fmt->ff_clustshift); - - /* Check if FAT12 has not been excluded */ - - if (var->fv_fattype == 0 || var->fv_fattype == 12) - { - /* Try to configure a FAT12 file system with this cluster size */ - - if (mkfatfs_tryfat12(fmt, var, &fatconfig12) != 0) - { - ferr("ERROR: Cannot format FAT12 at %u sectors/cluster\n", - 1 << fmt->ff_clustshift); - - fatconfig12.fc_nfatsects = 0; - fatconfig12.fc_nclusters = 0; - } - } - - /* Check if FAT16 has not been excluded */ - - if (var->fv_fattype == 0 || var->fv_fattype == 16) - { - /* Try to configure a FAT16 file system with this cluster size */ - - if (mkfatfs_tryfat16(fmt, var, &fatconfig16) != 0) - { - ferr("ERROR: Cannot format FAT16 at %u sectors/cluster\n", - 1 << fmt->ff_clustshift); - - fatconfig16.fc_nfatsects = 0; - fatconfig16.fc_nclusters = 0; - } - } - - /* If either FAT12 or 16 was configured at this sector/cluster setting, - * then finish the configuration and break out now - */ - - if (fatconfig12.fc_nclusters || fatconfig16.fc_nclusters) - { - if ((!var->fv_fattype && fatconfig16.fc_nclusters > fatconfig12.fc_nclusters) || - (var ->fv_fattype == 16)) - { - /* The caller has selected FAT16 -OR- no FAT type has been selected, but - * the FAT16 selection has more clusters. Select FAT16. - */ - - mkfatfs_selectfat(16, fmt, var, &fatconfig16); - } - else - { - /* The caller has selected FAT12 -OR- no FAT type has been selected, but - * the FAT12 selected has more clusters. Selected FAT12 - */ - - mkfatfs_selectfat(12, fmt, var, &fatconfig12); - } - - return OK; - } - -#if 0 - /* Check if FAT32 has not been excluded */ - - if (var->fv_fattype == 0 || var->fv_fattype == 32) -#else - /* FAT32 must be explicitly requested */ - - if (var->fv_fattype == 32) -#endif - { - /* The number of data sectors available (includes the fat itself) - * This value is a constant with respect to cluster sizefor FAT12/16, but not FAT32 - * because the size of the root directory cluster changes with cluster size. - */ - - fatconfig32.fc_navailsects = fmt->ff_nsectors - (1 << fmt->ff_clustshift) - fatconfig32.fc_rsvdseccount; - - /* Try to configure a FAT32 file system with this cluster size */ - - if (mkfatfs_tryfat32(fmt, var, &fatconfig32) != 0) - { - ferr("ERROR: Cannot format FAT32 at %u sectors/cluster\n", - 1 << fmt->ff_clustshift); - - fatconfig32.fc_nfatsects = 0; - fatconfig32.fc_nclusters = 0; - } - else - { - /* Select FAT32 if we have not already done so */ - - mkfatfs_selectfat(32, fmt, var, &fatconfig32); - return OK; - } - } - - /* Otherwise, bump up the sectors/cluster for the next time around the loop. */ - - fmt->ff_clustshift++; - } - while (fmt->ff_clustshift <= mxclustshift); - - return -ENFILE; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_configfatfs - * - * Description: - * Based on the geometry of the block device and upon the caller-selected - * values, configure the FAT filesystem for the device. - * - * Input: - * fmt - Caller specified format parameters - * var - Holds disk geomtry data. Also, the location to return FAT - * configuration data - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -int mkfatfs_configfatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - int ret; - - /* Select the number of root directory entries (FAT12/16 only). If FAT32 is selected, - * this value will be cleared later - */ - - if (!fmt->ff_rootdirentries) - { - /* The caller did not specify the number of root directory entries; use a default of 512. */ - - fmt->ff_rootdirentries = 512; - } - - /* Search to determine the smallest (reasonable) cluster size. A by-product - * of this search will be the selection of the FAT size (12/16/32) if the - * caller has not specified the FAT size - */ - - ret = mkfatfs_clustersearch(fmt, var); - if (ret < 0) - { - ferr("ERROR: Failed to set cluster size\n"); - return ret; - } - - /* Perform FAT specific initialization */ - - /* Set up boot jump assuming FAT 12/16 offset to bootcode */ - - var->fv_jump[0] = OPCODE_JMP_REL8; - var->fv_jump[2] = OPCODE_NOP; - var->fv_bootcode = g_bootcodeblob; - var->fv_bootcodesize = sizeof(g_bootcodeblob); - - if (var->fv_fattype != 32) - { - /* Set up additional, non-zero FAT12/16 fields */ - - /* Patch in the correct offset to the boot code */ - - var->fv_jump[1] = BS16_BOOTCODE - 2; - g_bootcodeblob[3] = BS16_BOOTCODE + BOOTCODE_MSGOFFSET; - } - else - { - /* Patch in the correct offset to the boot code */ - - var->fv_jump[1] = BS32_BOOTCODE - 2; - g_bootcodeblob[3] = BS32_BOOTCODE + BOOTCODE_MSGOFFSET; - - /* The root directory is a cluster chain... its is initialize size is one cluster */ - - var->fv_nrootdirsects = 1 << fmt->ff_clustshift; - - /* The number of reported root directory entries should should be zero for - * FAT32 because the root directory is a cluster chain. - */ - - fmt->ff_rootdirentries = 0; - - /* Verify the caller's backupboot selection */ - - if (fmt->ff_backupboot <= 1 || fmt->ff_backupboot >= fmt->ff_rsvdseccount) - { - ferr("ERROR: Invalid backup boot sector: %d\n", fmt->ff_backupboot); - fmt->ff_backupboot = 0; - } - - /* Check if the caller has selected a location for the backup boot record */ - - if (!fmt->ff_backupboot) - { - /* There must be reserved sectors in order to have a backup boot sector */ - - if (fmt->ff_rsvdseccount >= 2) - { - /* Sector 0 is the MBR; 1... ff_rsvdseccount are reserved. Try the next - * the last reserved sector. - */ - - fmt->ff_backupboot = fmt->ff_rsvdseccount - 1; - if (fmt->ff_backupboot > 6) - { - /* Limit the location to within the first 7 */ - - fmt->ff_backupboot = 6; - } - } - } - } - - /* Report the selected fat type */ - - fmt->ff_fattype = var->fv_fattype; - - /* Describe the configured filesystem */ - -#ifdef CONFIG_DEBUG_FEATURES - finfo("Sector size: %d bytes\n", var->fv_sectorsize); - finfo("Number of sectors: %d sectors\n", fmt->ff_nsectors); - finfo("FAT size: %d bits\n", var->fv_fattype); - finfo("Number FATs: %d\n", fmt->ff_nfats); - finfo("Sectors per cluster: %d sectors\n", 1 << fmt->ff_clustshift); - finfo("FS size: %d sectors\n", var->fv_nfatsects); - finfo(" %d clusters\n", var->fv_nclusters); - - if (var->fv_fattype != 32) - { - finfo("Root directory slots: %d\n", fmt->ff_rootdirentries); - } - - finfo("Volume ID: %08x\n", fmt->ff_volumeid); - finfo("Volume Label: \"%c%c%c%c%c%c%c%c%c%c%c\"\n", - fmt->ff_volumelabel[0], fmt->ff_volumelabel[1], fmt->ff_volumelabel[2], - fmt->ff_volumelabel[3], fmt->ff_volumelabel[4], fmt->ff_volumelabel[5], - fmt->ff_volumelabel[6], fmt->ff_volumelabel[7], fmt->ff_volumelabel[8], - fmt->ff_volumelabel[9], fmt->ff_volumelabel[10]); -#endif - return OK; -} diff --git a/fs/fat/fs_mkfatfs.c b/fs/fat/fs_mkfatfs.c deleted file mode 100644 index 75269a602ba..00000000000 --- a/fs/fat/fs_mkfatfs.c +++ /dev/null @@ -1,343 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_writefat.c - * - * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "inode/inode.h" -#include "fs_fat32.h" -#include "fs_mkfatfs.h" - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_getgeometry - * - * Description: - * Get the sector size and number of sectors of the underlying block - * device. - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_getgeometry(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - struct geometry geometry; - int ret; - - /* Get the device geometry */ - - ret = DEV_GEOMETRY(geometry); - if (ret < 0) - { - ferr("ERROR: geometry() returned %d\n", ret); - return ret; - } - - if (!geometry.geo_available || !geometry.geo_writeenabled) - { - ferr("ERROR: Media is not available\n", ret); - return -ENODEV; - } - - /* Check if the user provided maxblocks was provided and, if so, that is it less than - * the actual number of blocks on the device. - */ - - if (fmt->ff_nsectors != 0) - { - if (fmt->ff_nsectors > geometry.geo_nsectors) - { - ferr("ERROR: User maxblocks (%d) exceeds blocks on device (%d)\n", - fmt->ff_nsectors, geometry.geo_nsectors); - - return -EINVAL; - } - } - else - { - /* Use the actual number of blocks on the device */ - - fmt->ff_nsectors = geometry.geo_nsectors; - } - - /* Verify that we can handle this sector size */ - - var->fv_sectorsize = geometry.geo_sectorsize; - switch (var->fv_sectorsize) - { - case 512: - var->fv_sectshift = 9; - break; - - case 1024: - var->fv_sectshift = 10; - break; - - case 2048: - var->fv_sectshift = 11; - break; - - case 4096: - var->fv_sectshift = 12; - break; - - default: - ferr("ERROR: Unsupported sector size: %d\n", var->fv_sectorsize); - return -EPERM; - } - - return 0; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs - * - * Description: - * Make a FAT file system image on the specified block device. This - * function can automatically format a FAT12 or FAT16 file system. By - * tradition, FAT32 will only be selected is explicitly requested. - * - * 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 - * EACCES - block driver does not support wrie or geometry methods - * - * 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). - * - ****************************************************************************/ - -int mkfatfs(FAR const char *pathname, FAR struct fat_format_s *fmt) -{ - struct fat_var_s var; - int ret; - - /* Initialize */ - - memset(&var, 0, sizeof(struct fat_var_s)); - - /* Get the filesystem creation time */ - - var.fv_createtime = fat_systime2fattime(); - - /* Verify format options (only when DEBUG enabled) */ - -#ifdef CONFIG_DEBUG_FEATURES - if (!pathname) - { - ferr("ERROR: No block driver path\n"); - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_nfats < 1 || fmt->ff_nfats > 4) - { - ferr("ERROR: Invalid number of fats: %d\n", fmt->ff_nfats); - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_fattype != 0 && fmt->ff_fattype != 12 && - fmt->ff_fattype != 16 && fmt->ff_fattype != 32) - { - ferr("ERROR: Invalid FAT size: %d\n", fmt->ff_fattype); - ret = -EINVAL; - goto errout; - } -#endif - - /* 0 will auto-selected by FAT12 and FAT16 (only). Otherwise, - * fv_fattype will specify the exact format to use. - */ - - var.fv_fattype = fmt->ff_fattype; - - /* The valid range off ff_clustshift is {0,1,..7} corresponding to - * cluster sizes of {1,2,..128} sectors. The special value of 0xff - * means that we should autoselect the cluster sizel. - */ - -#ifdef CONFIG_DEBUG_FEATURES - if (fmt->ff_clustshift > 7 && fmt->ff_clustshift != 0xff) - { - ferr("ERROR: Invalid cluster shift value: %d\n", fmt->ff_clustshift); - - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_rootdirentries != 0 && - (fmt->ff_rootdirentries < 16 || fmt->ff_rootdirentries > 32767)) - { - ferr("ERROR: Invalid number of root dir entries: %d\n", - fmt->ff_rootdirentries); - - ret = -EINVAL; - goto errout; - } - - if (fmt->ff_rsvdseccount != 0 && (fmt->ff_rsvdseccount < 1 || - fmt->ff_rsvdseccount > 32767)) - { - ferr("ERROR: Invalid number of reserved sectors: %d\n", - fmt->ff_rsvdseccount); - - ret = -EINVAL; - goto errout; - } -#endif - - /* Find the inode of the block driver indentified by 'source' */ - - ret = open_blockdriver(pathname, 0, &var.fv_inode); - if (ret < 0) - { - ferr("ERROR: Failed to open %s\n", pathname); - goto errout; - } - - /* Make sure that the inode supports the write and geometry methods at a minimum */ - - if (!var.fv_inode->u.i_bops->write || !var.fv_inode->u.i_bops->geometry) - { - ferr("ERROR: %s does not support write or geometry methods\n", - pathname); - - ret = -EACCES; - goto errout_with_driver; - } - - /* Determine the volume configuration based upon the input values and upon the - * reported device geometry. - */ - - ret = mkfatfs_getgeometry(fmt, &var); - if (ret < 0) - { - goto errout_with_driver; - } - - /* Configure the file system */ - - ret = mkfatfs_configfatfs(fmt, &var); - if (ret < 0) - { - goto errout_with_driver; - } - - /* Allocate a buffer that will be working sector memory */ - -#ifdef CONFIG_FAT_DMAMEMORY - var.fv_sect = (FAR uint8_t *)fat_dma_alloc(var.fv_sectorsize); -#else - var.fv_sect = (FAR uint8_t *)kmm_malloc(var.fv_sectorsize); -#endif - - if (!var.fv_sect) - { - ferr("ERROR: Failed to allocate working buffers\n"); - goto errout_with_driver; - } - - /* Write the filesystem to media */ - - ret = mkfatfs_writefatfs(fmt, &var); - -errout_with_driver: - /* Close the driver */ - - (void)close_blockdriver(var.fv_inode); - -errout: - /* Release all allocated memory */ - - if (var.fv_sect) - { -#ifdef CONFIG_FAT_DMAMEMORY - fat_dma_free(var.fv_sect, var.fv_sectorsize); -#else - kmm_free(var.fv_sect); -#endif - } - - /* Return any reported errors */ - - if (ret < 0) - { - set_errno(-ret); - return ERROR; - } - - return OK; -} diff --git a/fs/fat/fs_mkfatfs.h b/fs/fat/fs_mkfatfs.h deleted file mode 100644 index ce170296c64..00000000000 --- a/fs/fat/fs_mkfatfs.h +++ /dev/null @@ -1,169 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_mkfat.h - * - * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 -#include - -/**************************************************************************** - * Pre-processor 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 - -/* Macros to simplify direct block driver access */ - -#define DEV_OPEN() \ - var->fb_inode->u.i_bops->open ? \ - var->fv_inode->u.i_bops->open(var->fv_inode) : \ - 0 -#define DEV_CLOSE() \ - var->fb_inode->u.i_bops->close ? \ - var->fv_inode->u.i_bops->close(var->fv_inode) : \ - 0 -#define DEV_READ(buf, sect, nsect) \ - var->fv_inode->u.i_bops->read(var->fv_inode, buf, sect, nsect) -#define DEV_WRITE(buf, sect, nsect) \ - var->fv_inode->u.i_bops->write(var->fv_inode, buf, sect, nsect) -#define DEV_GEOMETRY(geo) \ - var->fv_inode->u.i_bops->geometry(var->fv_inode, &geo) -#define DEV_IOCTL(cmd, arg) \ - var->fv_inode->u.i_bops->ioctl(var->fv_inode, cmd, arg) - -/**************************************************************************** - * 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 -{ - struct inode *fv_inode; /* The block driver "handle" */ - uint8_t fv_jump[3]; /* 3-byte boot jump instruction */ - uint8_t fv_sectshift; /* Log2 of fv_sectorsize */ - uint8_t fv_nrootdirsects; /* Number of root directory sectors */ - uint8_t fv_fattype; /* FAT size: 0 (not determined), 12, 16, or 32 */ - uint16_t fv_bootcodesize; /* Size of array at fv_bootcode */ - uint32_t fv_createtime; /* Creation time */ - uint32_t fv_sectorsize; /* Size of one hardware sector */ - uint32_t fv_nfatsects; /* Number of sectors in each FAT */ - uint32_t fv_nclusters; /* Number of clusters */ - uint8_t *fv_sect; /* Allocated working sector buffer */ - const uint8_t *fv_bootcode; /* Points to boot code to put into MBR */ -}; - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -#undef EXTERN -#if defined(__cplusplus) -#define EXTERN extern "C" -extern "C" -{ -#else -#define EXTERN extern -#endif - -/**************************************************************************** - * Name: mkfatfs_configfatfs - * - * Description: - * Based on the geometry of the block device and upon the caller-selected - * values, configure the FAT filesystem for the device. - * - * Input: - * fmt - Caller specified format parameters - * var - Holds disk geomtry data. Also, the location to return FAT - * configuration data - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -int mkfatfs_configfatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var); - -/**************************************************************************** - * Name: mkfatfs_writefat - * - * Description: - * Write the configured fat filesystem to the block device - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -int mkfatfs_writefatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var); - -#undef EXTERN -#if defined(__cplusplus) -} -#endif - -#endif /* __FS_FAT_FS_MKATFS_H */ diff --git a/fs/fat/fs_writefat.c b/fs/fat/fs_writefat.c deleted file mode 100644 index a484cbac673..00000000000 --- a/fs/fat/fs_writefat.c +++ /dev/null @@ -1,544 +0,0 @@ -/**************************************************************************** - * fs/fat/fs_writefat.c - * - * Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 - -#include -#include -#include -#include - -#include -#include -#include - -#include "inode/inode.h" -#include "fs_fat32.h" -#include "fs_mkfatfs.h" - -/**************************************************************************** - * Private 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 - * - * Return: - * None; caller is responsible for providing valid parameters. - * - ****************************************************************************/ -static inline void mkfatfs_initmbr(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - memset(var->fv_sect, 0, var->fv_sectorsize); - - /* 3@0: Jump instruction to boot code */ - - memcpy(&var->fv_sect[BS_JUMP], var->fv_jump, 3); - - /* 8@3: Usually "MSWIN4.1" */ - - strcpy((FAR char *)&var->fv_sect[BS_OEMNAME], "NUTTX "); - - /* 2@11: Bytes per sector: 512, 1024, 2048, 4096 */ - - MBR_PUTBYTESPERSEC(var->fv_sect, var->fv_sectorsize); - - /* 1@13: Sectors per allocation unit: 2**n, n=0..7 */ - - MBR_PUTSECPERCLUS(var->fv_sect, (1 << fmt->ff_clustshift)); - - /* 2@14: Reserved sector count: Usually 32 */ - - MBR_PUTRESVDSECCOUNT(var->fv_sect, fmt->ff_rsvdseccount); - - /* 1@16: Number of FAT data structures: always 2 */ - - MBR_PUTNUMFATS(var->fv_sect, fmt->ff_nfats); - - /* 2@17: FAT12/16: Must be 0 for FAT32 */ - - MBR_PUTROOTENTCNT(var->fv_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 (fmt->ff_nsectors >= 65536) - { - MBR_PUTTOTSEC32(var->fv_sect, fmt->ff_nsectors); - } - else - { - MBR_PUTTOTSEC16(var->fv_sect, (uint16_t)fmt->ff_nsectors); - } - - /* 1@21: Media code: f0, f8, f9-fa, fc-ff */ - - MBR_PUTMEDIA(var->fv_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(var->fv_sect, FAT_DEFAULT_SECPERTRK); - MBR_PUTNUMHEADS(var->fv_sect, FAT_DEFAULT_NUMHEADS); - - /* 4@28: Count of hidden sectors preceding FAT */ - - MBR_PUTHIDSEC(var->fv_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_fattype != 32) - { - /* 2@22: FAT12/16: Must be 0, see BS32_FATSZ32 */ - - MBR_PUTFATSZ16(var->fv_sect, (uint16_t)var->fv_nfatsects); - - /* 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(var->fv_sect, EXTBOOT_SIGNATURE); - - /* 4@39: Volume serial number */ - - MBR_PUTVOLID16(var->fv_sect, fmt->ff_volumeid); - - /* 11@43: Volume label */ - - memcpy(&var->fv_sect[BS16_VOLLAB], fmt->ff_volumelabel, 11); - - /* 8@54: "FAT12 ", "FAT16 ", or "FAT " */ - - if (fmt->ff_fattype == 12) - { - memcpy(&var->fv_sect[BS16_FILESYSTYPE], "FAT12 ", 8); - } - else /* if (fmt->ff_fattype == 16) */ - { - memcpy(&var->fv_sect[BS16_FILESYSTYPE], "FAT16 ", 8); - } - - /* Boot code may be placed in the remainder of the sector */ - - memcpy(&var->fv_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(var->fv_sect, var->fv_nfatsects); - - /* 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(var->fv_sect, FAT32_DEFAULT_ROOT_CLUSTER); - - /* 2@48: Sector number of fsinfo structure. Usually 1. */ - - MBR_PUTFSINFO(var->fv_sect, FAT_DEFAULT_FSINFO_SECTOR); - - /* 2@50: Sector number of boot record. Usually 6 */ - - MBR_PUTBKBOOTSEC(var->fv_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(var->fv_sect, EXTBOOT_SIGNATURE); - - /* 4@67: Volume serial number */ - - MBR_PUTVOLID32(var->fv_sect, fmt->ff_volumeid); - - /* 11@71: Volume label */ - - memcpy(&var->fv_sect[BS32_VOLLAB], fmt->ff_volumelabel, 11); - - /* 8@82: "FAT12 ", "FAT16 ", or "FAT " */ - - memcpy(&var->fv_sect[BS32_FILESYSTYPE], "FAT32 ", 8); - - /* Boot code may be placed in the remainder of the sector */ - - memcpy(&var->fv_sect[BS32_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(var->fv_sect, BOOT_SIGNATURE16); -} - -/**************************************************************************** - * Name: mkfatfs_initfsinfo - * - * Description: - * Initialize the FAT32 FSINFO sector image - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * None; caller is responsible for providing valid parameters. - * - ****************************************************************************/ -static inline void mkfatfs_initfsinfo(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - memset(var->fv_sect, 0, var->fv_sectorsize); - - /* 4@0: 0x41615252 = "RRaA" */ - - FSI_PUTLEADSIG(var->fv_sect, 0x41615252); - - /* 480@4: Reserved (zero) */ - /* 4@484: 0x61417272 = "rrAa" */ - - FSI_PUTSTRUCTSIG(var->fv_sect, 0x61417272); - - /* 4@488: Last free cluster count on volume */ - - FSI_PUTFREECOUNT(var->fv_sect, var->fv_nclusters - 1); - - /* 4@492: Cluster number of 1st free cluster */ - - FSI_PUTNXTFREE(var->fv_sect, FAT32_DEFAULT_ROOT_CLUSTER); - - /* 12@496: Reserved (zero) */ - /* 4@508: 0xaa550000 */ - - FSI_PUTTRAILSIG(var->fv_sect, BOOT_SIGNATURE32); -} - -/**************************************************************************** - * Name: mkfatfs_initrootdir - * - * Description: - * Initialize one root directory sector image - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * sectno - On FAT32, the root directory is a cluster chain. - * This value indicates which sector of the cluster should be produced. - * - * Return: - * None; caller is responsible for providing valid parameters. - * - ****************************************************************************/ -static inline void mkfatfs_initrootdir(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var, int sectno) -{ - memset(var->fv_sect, 0, var->fv_sectorsize); - if (sectno == 0) - { - /* It is only necessary to set data in the first sector of the directory */ - - if (memcmp(fmt->ff_volumelabel, " ", 11)) - { - memcpy(&var->fv_sect[DIR_NAME], fmt->ff_volumelabel, 11); - } - - DIR_PUTATTRIBUTES(var->fv_sect, FATATTR_VOLUMEID); - DIR_PUTCRTIME(var->fv_sect, var->fv_createtime & 0xffff); - DIR_PUTWRTTIME(var->fv_sect, var->fv_createtime & 0xffff); - DIR_PUTCRDATE(var->fv_sect, var->fv_createtime >> 16); - DIR_PUTWRTDATE(var->fv_sect, var->fv_createtime >> 16); - } -} - -/**************************************************************************** - * Name: mkfatfs_writembr - * - * Description: - * Write the master boot record and, for FAT32, the backup boot record and - * the fsinfo sector. - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_writembr(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - int sectno; - int ret; - - /* Create an image of the configured master boot record */ - - mkfatfs_initmbr(fmt, var); - - /* Write the master boot record as sector zero */ - - ret = DEV_WRITE(var->fv_sect, 0, 1); - - /* Write all of the reserved sectors */ - - memset(var->fv_sect, 0, var->fv_sectorsize); - for (sectno = 1; sectno < fmt->ff_rsvdseccount && ret >= 0; sectno++) - { - ret = DEV_WRITE(var->fv_sect, sectno, 1); - } - - /* Write FAT32-specific sectors */ - - if (ret >= 0 && fmt->ff_fattype == 32) - { - /* Write the backup master boot record */ - - if (fmt->ff_backupboot != 0) - { - /* Create another copy of the configured master boot record */ - - mkfatfs_initmbr(fmt, var); - - /* Write it to the backup location */ - - ret = DEV_WRITE(var->fv_sect, fmt->ff_backupboot, 1); - } - - if (ret >= 0) - { - /* Create an image of the fsinfo sector */ - - mkfatfs_initfsinfo(fmt, var); - - /* Write the fsinfo sector */ - - ret = DEV_WRITE(var->fv_sect, FAT_DEFAULT_FSINFO_SECTOR, 1); - } - } - - return ret; -} - -/**************************************************************************** - * Name: mkfatfs_writefat - * - * Description: - * Write the FAT sectors - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_writefat(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - off_t offset = fmt->ff_rsvdseccount; - int fatno; - int sectno; - int ret; - - /* Loop for each FAT copy */ - - for (fatno = 0; fatno < fmt->ff_nfats; fatno++) - { - /* Loop for each sector in the FAT */ - - for (sectno = 0; sectno < var->fv_nfatsects; sectno++) - { - memset(var->fv_sect, 0, var->fv_sectorsize); - - /* Mark cluster allocations in sector one of each FAT */ - - if (sectno == 0) - { - memset(var->fv_sect, 0, var->fv_sectorsize); - switch (fmt->ff_fattype) - { - case 12: - /* Mark the first two full FAT entries -- 24 bits, 3 bytes total */ - - memset(var->fv_sect, 0xff, 3); - break; - - case 16: - /* Mark the first two full FAT entries -- 32 bits, 4 bytes total */ - - memset(var->fv_sect, 0xff, 4); - break; - - case 32: - default: /* Shouldn't happen */ - /* Mark the first two full FAT entries -- 64 bits, 8 bytes total */ - - memset(var->fv_sect, 0xff, 8); - - /* Cluster 2 is used as the root directory. Mark as EOF */ - - var->fv_sect[8] = 0xf8; - memset(&var->fv_sect[9], 0xff, 3); - break; - } - - /* Save the media type in the first byte of the FAT */ - - var->fv_sect[0] = FAT_DEFAULT_MEDIA_TYPE; - } - - /* Write the FAT sector */ - - ret = DEV_WRITE(var->fv_sect, offset, 1); - if (ret < 0) - { - return ret; - } - offset++; - } - } - return OK; -} - -/**************************************************************************** - * Name: mkfatfs_writerootdir - * - * Description: - * Write the root directory sectors - * - * Input: - * fmt - User specified format parameters - * var - Other format parameters that are not user specifiable - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -static inline int mkfatfs_writerootdir(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - off_t offset = fmt->ff_rsvdseccount + fmt->ff_nfats * var->fv_nfatsects; - int ret; - int i; - - /* Write the root directory after the last FAT. This is the root directory - * area for FAT12/16, and the first cluster on FAT32. - */ - - for (i = 0; i < var->fv_nrootdirsects; i++) - { - /* Format the next sector of the root directory */ - - mkfatfs_initrootdir(fmt, var, i); - - /* Write the next sector of the root directory */ - - ret = DEV_WRITE(var->fv_sect, offset, 1); - if (ret < 0) - { - return ret; - } - offset++; - } - return 0; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: mkfatfs_writefat - * - * Description: - * Write the configured fat filesystem to the block device - * - * Input: - * fmt - Caller specified format parameters - * var - Other format parameters that are not caller specifiable. (Most - * set by mkfatfs_configfatfs()). - * - * Return: - * Zero on success; negated errno on failure - * - ****************************************************************************/ - -int mkfatfs_writefatfs(FAR struct fat_format_s *fmt, - FAR struct fat_var_s *var) -{ - int ret; - - /* Write the master boot record (also the backup and fsinfo sectors) */ - - ret = mkfatfs_writembr(fmt, var); - - /* Write FATs */ - - if (ret >= 0) - { - ret = mkfatfs_writefat(fmt, var); - } - - /* Write the root directory after the last FAT. */ - - if (ret >= 0) - { - ret = mkfatfs_writerootdir(fmt, var); - } - return ret; -} - diff --git a/include/nuttx/fs/mkfatfs.h b/include/nuttx/fs/mkfatfs.h deleted file mode 100644 index 03ca6ef956a..00000000000 --- a/include/nuttx/fs/mkfatfs.h +++ /dev/null @@ -1,149 +0,0 @@ -/**************************************************************************** - * include/nuttx/fs/mkfatfs.h - * - * Copyright (C) 2008-2009, 2012, 2015 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 __INCLUDE_NUTTX_FS_MKFATFS_H -#define __INCLUDE_NUTTX_FS_MKFATFS_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -#define MKFATFS_DEFAULT_NFATS 2 /* 2: Default number of FATs */ -#define MKFATFS_DEFAULT_FATTYPE 0 /* 0: Autoselect FAT size */ -#define MKFATFS_DEFAULT_CLUSTSHIFT 0xff /* 0xff: 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_NSECTORS 0 /* 0: Use all sectors on device */ - -#define FAT_FORMAT_INITIALIZER \ -{ \ - MKFATFS_DEFAULT_NFATS, \ - MKFATFS_DEFAULT_FATTYPE, \ - MKFATFS_DEFAULT_CLUSTSHIFT, \ - MKFATFS_DEFAULT_VOLUMELABEL, \ - MKFATFS_DEFAULT_BKUPBOOT, \ - MKFATFS_DEFAULT_ROOTDIRENTS, \ - MKFATFS_DEFAULT_RSVDSECCOUNT, \ - MKFATFS_DEFAULT_HIDSEC, \ - MKFATFS_DEFAULT_VOLUMEID, \ - MKFATFS_DEFAULT_NSECTORS \ -} - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* These are input parameters for the format. On return, these values may be - * overwritten with actual values used in the format. - */ - -struct fat_format_s -{ - uint8_t ff_nfats; /* Number of FATs */ - uint8_t ff_fattype; /* FAT size: 0 (autoselect), 12, 16, or 32 */ - uint8_t ff_clustshift; /* Log2 of sectors per cluster: 0-5, 0xff (autoselect) */ - uint8_t ff_volumelabel[11]; /* Volume label */ - uint16_t ff_backupboot; /* Sector number of the backup boot sector (0=use default)*/ - uint16_t ff_rootdirentries; /* Number of root directory entries */ - uint16_t ff_rsvdseccount; /* Reserved sectors */ - uint32_t ff_hidsec; /* Count of hidden sectors preceding fat */ - uint32_t ff_volumeid; /* FAT volume id */ - uint32_t ff_nsectors; /* Number of sectors from device to use: 0: Use all */ -}; - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/**************************************************************************** - * 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. This - * function can automatically format a FAT12 or FAT16 file system. By - * tradition, FAT32 will only be selected is explicitly requested. - * - * 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 - * EACCESS - block driver does not support write or geometry methods - * - * 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). - * - ****************************************************************************/ - -int mkfatfs(FAR const char *pathname, FAR struct fat_format_s *fmt); - -#undef EXTERN -#if defined(__cplusplus) -} -#endif - -#endif /* __INCLUDE_NUTTX_FS_MKFATFS_H */