summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSuriyan Ramasami <suriyan.r@gmail.com>2013-10-08 08:45:57 +0400
committerMauro Ribeiro <git@mdrjr.net>2013-10-08 08:45:57 +0400
commit93bd5f6a6e7622feec063e0e739a40f802acf497 (patch)
treedd73510be9af1fa203bf7ef5e6609a7daa91856b
parent4ac7bdc152e8dd755d6b63324526c534bac3b319 (diff)
downloadu-boot-93bd5f6a6e7622feec063e0e739a40f802acf497.tar.xz
Porting generic ls/load command for ext and fat.
-rw-r--r--common/Makefile1
-rw-r--r--common/cmd_fat.c386
-rw-r--r--common/cmd_fs.c51
-rw-r--r--fs/fat/Makefile31
-rw-r--r--fs/fat/fat.c975
-rw-r--r--fs/fat/fat_write.c1079
-rw-r--r--fs/fat/file.c18
-rw-r--r--include/configs/smdk4412.h2
-rw-r--r--include/fat.h153
9 files changed, 1588 insertions, 1108 deletions
diff --git a/common/Makefile b/common/Makefile
index df286e486e..2ec669efc5 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -107,6 +107,7 @@ COBJS-$(CONFIG_CMD_FLASH) += cmd_flash.o
ifdef CONFIG_FPGA
COBJS-$(CONFIG_CMD_FPGA) += cmd_fpga.o
endif
+COBJS-$(CONFIG_CMD_FS_GENERIC) += cmd_fs.o
COBJS-$(CONFIG_CMD_I2C) += cmd_i2c.o
COBJS-$(CONFIG_CMD_IDE) += cmd_ide.o
COBJS-$(CONFIG_CMD_IMMAP) += cmd_immap.o
diff --git a/common/cmd_fat.c b/common/cmd_fat.c
index cb71f3f943..a12d8fa098 100644
--- a/common/cmd_fat.c
+++ b/common/cmd_fat.c
@@ -2,23 +2,7 @@
* (C) Copyright 2002
* Richard Jones, rjones@nexus-tech.net
*
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier: GPL-2.0+
*/
/*
@@ -31,353 +15,119 @@
#include <ata.h>
#include <part.h>
#include <fat.h>
-
+#include <fs.h>
int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
- long size;
- unsigned long offset;
- unsigned long count;
- char buf [12];
- block_dev_desc_t *dev_desc=NULL;
- int dev=0;
- int part=1;
- char *ep;
- struct mmc *mmc;
-
- if (argc < 5) {
- printf( "usage: fatload <interface> <dev[:part]> "
- "<addr> <filename> [bytes]\n");
- return 1;
- }
-
- dev = (int)simple_strtoul(argv[2], &ep, 16);
-
- mmc = find_mmc_device(dev);
- if (mmc_init(mmc)) {
- printf("MMC init is failed.\n");
- return 1;
- }
-
- dev_desc = get_dev(argv[1],dev);
- if (dev_desc == NULL) {
- puts("\n** Invalid boot device **\n");
- return 1;
- }
- if (*ep) {
- if (*ep != ':') {
- puts("\n** Invalid boot device, use `dev[:part]' **\n");
- return 1;
- }
- part = (int)simple_strtoul(++ep, NULL, 16);
- }
- if (fat_register_device(dev_desc,part)!=0) {
- printf("\n** Unable to use %s %d:%d for fatload **\n",
- argv[1], dev, part);
- return 1;
- }
- offset = simple_strtoul(argv[3], NULL, 16);
- if (argc == 6)
- count = simple_strtoul(argv[5], NULL, 16);
- else
- count = 0;
- size = file_fat_read(argv[4], (unsigned char *)offset, count);
-
- if(size==-1) {
- printf("\n** Unable to read \"%s\" from %s %d:%d **\n",
- argv[4], argv[1], dev, part);
- return 1;
- }
-
- printf("\n%ld bytes read\n", size);
-
- sprintf(buf, "%lX", size);
- setenv("filesize", buf);
-
- return 0;
+ return do_load(cmdtp, flag, argc, argv, FS_TYPE_FAT);
}
U_BOOT_CMD(
- fatload, 6, 0, do_fat_fsload,
- "fatload - load binary file from a dos filesystem\n",
- "<interface> <dev[:part]> <addr> <filename> [bytes]\n"
- " - load binary file 'filename' from 'dev' on 'interface'\n"
- " to address 'addr' from dos filesystem"
+ fatload, 7, 0, do_fat_fsload,
+ "load binary file from a dos filesystem",
+ "<interface> [<dev[:part]>] <addr> <filename> [bytes [pos]]\n"
+ " - Load binary file 'filename' from 'dev' on 'interface'\n"
+ " to address 'addr' from dos filesystem.\n"
+ " 'pos' gives the file position to start loading from.\n"
+ " If 'pos' is omitted, 0 is used. 'pos' requires 'bytes'.\n"
+ " 'bytes' gives the size to load. If 'bytes' is 0 or omitted,\n"
+ " the load stops on end of file.\n"
+ " If either 'pos' or 'bytes' are not aligned to\n"
+ " ARCH_DMA_MINALIGN then a misaligned buffer warning will\n"
+ " be printed and performance will suffer for the load."
);
-int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_fat_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
- char *filename = "/";
- int ret;
- int dev=0;
- int part=1;
- char *ep;
- block_dev_desc_t *dev_desc=NULL;
-
- if (argc < 3) {
- printf("usage: fatls <interface> <dev[:part]> [directory]\n");
- return 0;
- }
- dev = (int)simple_strtoul(argv[2], &ep, 16);
- dev_desc = get_dev(argv[1],dev);
- if (dev_desc == NULL) {
- puts("\n** Invalid boot device **\n");
- return 1;
- }
- if (*ep) {
- if (*ep != ':') {
- puts("\n** Invalid boot device, use `dev[:part]' **\n");
- return 1;
- }
- part = (int)simple_strtoul(++ep, NULL, 16);
- }
- if (fat_register_device(dev_desc,part)!=0) {
- printf("\n** Unable to use %s %d:%d for fatls **\n",
- argv[1], dev, part);
- return 1;
- }
- if (argc == 4)
- ret = file_fat_ls(argv[3]);
- else
- ret = file_fat_ls(filename);
-
- if(ret!=0)
- printf("No Fat FS detected\n");
- return ret;
+ return do_ls(cmdtp, flag, argc, argv, FS_TYPE_FAT);
}
U_BOOT_CMD(
fatls, 4, 1, do_fat_ls,
"list files in a directory (default /)",
- "<interface> <dev[:part]> [directory]\n"
+ "<interface> [<dev[:part]>] [directory]\n"
" - list files from 'dev' on 'interface' in a 'directory'"
);
-int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_fat_fsinfo(cmd_tbl_t *cmdtp, int flag, int argc,
+ char * const argv[])
{
- int dev=0;
- int part=1;
- char *ep;
- block_dev_desc_t *dev_desc=NULL;
+ int dev, part;
+ block_dev_desc_t *dev_desc;
+ disk_partition_t info;
if (argc < 2) {
- printf("usage: fatinfo <interface> <dev[:part]>\n");
+ printf("usage: fatinfo <interface> [<dev[:part]>]\n");
return 0;
}
- dev = (int)simple_strtoul(argv[2], &ep, 16);
- dev_desc = get_dev(argv[1],dev);
- if (dev_desc == NULL) {
- puts("\n** Invalid boot device **\n");
+
+ part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
+ if (part < 0)
return 1;
- }
- if (*ep) {
- if (*ep != ':') {
- puts("\n** Invalid boot device, use `dev[:part]' **\n");
- return 1;
- }
- part = (int)simple_strtoul(++ep, NULL, 16);
- }
- int i;
- for (i = 1; i <= 4; i++) {
- printf("-----Partition %d-----\n", i);
- if (fat_register_device(dev_desc,part)!=0) {
- printf ("\n** Unable to use %s %d:%d for fatinfo **\n",argv[1],dev,i);
- }
- printf("------------------------\n");
+ dev = dev_desc->dev;
+ if (fat_set_blk_dev(dev_desc, &info) != 0) {
+ printf("\n** Unable to use %s %d:%d for fatinfo **\n",
+ argv[1], dev, part);
+ return 1;
}
return file_fat_detectfs();
}
U_BOOT_CMD(
fatinfo, 3, 1, do_fat_fsinfo,
- "fatinfo - print information about filesystem",
- "<interface> <dev[:part]>\n"
- " - print information about filesystem from 'dev' on 'interface'\n"
+ "print information about filesystem",
+ "<interface> [<dev[:part]>]\n"
+ " - print information about filesystem from 'dev' on 'interface'"
);
-int do_fat_format(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+#ifdef CONFIG_FAT_WRITE
+static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
+ int argc, char * const argv[])
{
+ long size;
+ unsigned long addr;
+ unsigned long count;
+ block_dev_desc_t *dev_desc = NULL;
+ disk_partition_t info;
int dev = 0;
int part = 1;
- char *ep;
- block_dev_desc_t *dev_desc = NULL;
- if (argc < 2) {
- printf ("usage : fatformat <interface> <dev[:part]>\n");
- return(0);
- }
+ if (argc < 5)
+ return cmd_usage(cmdtp);
+
+ part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info, 1);
+ if (part < 0)
+ return 1;
- dev = (int)simple_strtoul (argv[2], &ep, 16);
- dev_desc = get_dev(argv[1], dev);
+ dev = dev_desc->dev;
- if (dev_desc == NULL) {
- puts ("\n ** Invalid boot device **\n");
+ if (fat_set_blk_dev(dev_desc, &info) != 0) {
+ printf("\n** Unable to use %s %d:%d for fatwrite **\n",
+ argv[1], dev, part);
return 1;
}
+ addr = simple_strtoul(argv[3], NULL, 16);
+ count = simple_strtoul(argv[5], NULL, 16);
- if (*ep) {
- if (*ep != ':') {
- puts ("\n ** Invald boot device, use 'dev[:part]' **\n");
- return 1;
- }
- part = (int)simple_strtoul(++ep, NULL, 16);
- if (part > 4 || part <1) {
- puts ("** Partition Number should be 1 ~ 4 **\n");
- }
- }
- printf("Start format MMC&d partition&d ...\n", dev, part);
- if (fat_format_device(dev_desc, part) != 0) {
- printf("Format failure!!!\n");
+ size = file_fat_write(argv[4], (void *)addr, count);
+ if (size == -1) {
+ printf("\n** Unable to write \"%s\" from %s %d:%d **\n",
+ argv[4], argv[1], dev, part);
+ return 1;
}
+ printf("%ld bytes written\n", size);
+
return 0;
}
U_BOOT_CMD(
- fatformat, 3, 0, do_fat_format,
- "fatformat - disk format by FAT32\n",
- "<interface(only support mmc)> <dev:partition num>\n"
- " - format by FAT32 on 'interface'\n"
+ fatwrite, 6, 0, do_fat_fswrite,
+ "write file into a dos filesystem",
+ "<interface> <dev[:part]> <addr> <filename> <bytes>\n"
+ " - write file 'filename' from the address 'addr' in RAM\n"
+ " to 'dev' on 'interface'"
);
-
-#ifdef NOT_IMPLEMENTED_YET
-/* find first device whose first partition is a DOS filesystem */
-int find_fat_partition (void)
-{
- int i, j;
- block_dev_desc_t *dev_desc;
- unsigned char *part_table;
- unsigned char buffer[ATA_BLOCKSIZE];
-
- for (i = 0; i < CONFIG_SYS_IDE_MAXDEVICE; i++) {
- dev_desc = ide_get_dev (i);
- if (!dev_desc) {
- debug ("couldn't get ide device!\n");
- return (-1);
- }
- if (dev_desc->part_type == PART_TYPE_DOS) {
- if (dev_desc->
- block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
- debug ("can't perform block_read!\n");
- return (-1);
- }
- part_table = &buffer[0x1be]; /* start with partition #4 */
- for (j = 0; j < 4; j++) {
- if ((part_table[4] == 1 || /* 12-bit FAT */
- part_table[4] == 4 || /* 16-bit FAT */
- part_table[4] == 6) && /* > 32Meg part */
- part_table[0] == 0x80) { /* bootable? */
- curr_dev = i;
- part_offset = part_table[11];
- part_offset <<= 8;
- part_offset |= part_table[10];
- part_offset <<= 8;
- part_offset |= part_table[9];
- part_offset <<= 8;
- part_offset |= part_table[8];
- debug ("found partition start at %ld\n", part_offset);
- return (0);
- }
- part_table += 16;
- }
- }
- }
-
- debug ("no valid devices found!\n");
- return (-1);
-}
-
-int
-do_fat_dump (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
-{
- __u8 block[1024];
- int ret;
- int bknum;
-
- ret = 0;
-
- if (argc != 2) {
- printf ("needs an argument!\n");
- return (0);
- }
-
- bknum = simple_strtoul (argv[1], NULL, 10);
-
- if (disk_read (0, bknum, block) != 0) {
- printf ("Error: reading block\n");
- return -1;
- }
- printf ("FAT dump: %d\n", bknum);
- hexdump (512, block);
-
- return (ret);
-}
-
-int disk_read (__u32 startblock, __u32 getsize, __u8 *bufptr)
-{
- ulong tot;
- block_dev_desc_t *dev_desc;
-
- if (curr_dev < 0) {
- if (find_fat_partition () != 0)
- return (-1);
- }
-
- dev_desc = ide_get_dev (curr_dev);
- if (!dev_desc) {
- debug ("couldn't get ide device\n");
- return (-1);
- }
-
- tot = dev_desc->block_read (0, startblock + part_offset,
- getsize, (ulong *) bufptr);
-
- /* should we do this here?
- flush_cache ((ulong)buf, cnt*ide_dev_desc[device].blksz);
- */
-
- if (tot == getsize)
- return (0);
-
- debug ("unable to read from device!\n");
-
- return (-1);
-}
-
-
-static int isprint (unsigned char ch)
-{
- if (ch >= 32 && ch < 127)
- return (1);
-
- return (0);
-}
-
-
-void hexdump (int cnt, unsigned char *data)
-{
- int i;
- int run;
- int offset;
-
- offset = 0;
- while (cnt) {
- printf ("%04X : ", offset);
- if (cnt >= 16)
- run = 16;
- else
- run = cnt;
- cnt -= run;
- for (i = 0; i < run; i++)
- printf ("%02X ", (unsigned int) data[i]);
- printf (": ");
- for (i = 0; i < run; i++)
- printf ("%c", isprint (data[i]) ? data[i] : '.');
- printf ("\n");
- data = &data[16];
- offset += run;
- }
-}
-#endif /* NOT_IMPLEMENTED_YET */
+#endif
diff --git a/common/cmd_fs.c b/common/cmd_fs.c
new file mode 100644
index 0000000000..91a205ac1e
--- /dev/null
+++ b/common/cmd_fs.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
+ *
+ * Inspired by cmd_ext_common.c, cmd_fat.c.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+
+int do_load_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ return do_load(cmdtp, flag, argc, argv, FS_TYPE_ANY);
+}
+
+U_BOOT_CMD(
+ load, 7, 0, do_load_wrapper,
+ "load binary file from a filesystem",
+ "<interface> [<dev[:part]> [<addr> [<filename> [bytes [pos]]]]]\n"
+ " - Load binary file 'filename' from partition 'part' on device\n"
+ " type 'interface' instance 'dev' to address 'addr' in memory.\n"
+ " 'bytes' gives the size to load in bytes.\n"
+ " If 'bytes' is 0 or omitted, the file is read until the end.\n"
+ " 'pos' gives the file byte position to start reading from.\n"
+ " If 'pos' is 0 or omitted, the file is read from the start."
+);
+
+int do_ls_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ return do_ls(cmdtp, flag, argc, argv, FS_TYPE_ANY);
+}
+
+U_BOOT_CMD(
+ ls, 4, 1, do_ls_wrapper,
+ "list files in a directory (default /)",
+ "<interface> [<dev[:part]> [directory]]\n"
+ " - List files in directory 'directory' of partition 'part' on\n"
+ " device type 'interface' instance 'dev'."
+);
diff --git a/fs/fat/Makefile b/fs/fat/Makefile
index c70da23da7..c00681f31b 100644
--- a/fs/fat/Makefile
+++ b/fs/fat/Makefile
@@ -1,22 +1,6 @@
#
#
-# See file CREDITS for list of people who contributed to this
-# project.
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation; either version 2 of
-# the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
-# MA 02111-1307 USA
+# SPDX-License-Identifier: GPL-2.0+
#
include $(TOPDIR)/config.mk
@@ -24,16 +8,23 @@ include $(TOPDIR)/config.mk
LIB = $(obj)libfat.o
AOBJS =
-COBJS-$(CONFIG_CMD_FAT) := fat.o file.o
+COBJS-$(CONFIG_FS_FAT) := fat.o
+COBJS-$(CONFIG_FAT_WRITE):= fat_write.o
+
+ifndef CONFIG_SPL_BUILD
+COBJS-$(CONFIG_FS_FAT) += file.o
+endif
-SRCS := $(AOBJS:.o=.S) $(COBJS-y:.o=.c)
-OBJS := $(addprefix $(obj),$(AOBJS) $(COBJS-y))
+SRCS := $(AOBJS:.o=.S) $(COBJS-y:.o=.c)
+OBJS := $(addprefix $(obj),$(AOBJS) $(COBJS-y))
all: $(LIB) $(AOBJS)
$(LIB): $(obj).depend $(OBJS)
$(call cmd_link_o_target, $(OBJS))
+# SEE README.arm-unaligned-accesses
+$(obj)file.o: CFLAGS += $(PLATFORM_NO_UNALIGNED)
#########################################################################
diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index ed5b8b787f..b41d62e3c3 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -1,10 +1,4 @@
/*
- * Copyright (c) 2010 Samsung Electronics Co., Ltd.
- * http://www.samsung.com/
- *
- * Added fat format.
- * Original license code is as follows:
- *
* fat.c
*
* R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
@@ -12,463 +6,115 @@
* 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
* 2003-03-10 - kharris@nexus-tech.net - ported to uboot
*
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <config.h>
+#include <exports.h>
#include <fat.h>
#include <asm/byteorder.h>
#include <part.h>
+#include <malloc.h>
+#include <linux/compiler.h>
+#include <linux/ctype.h>
-#if defined(CONFIG_CMD_FAT)
-
-#if defined(CONFIG_S5P6450)
-DECLARE_GLOBAL_DATA_PTR;
+#ifdef CONFIG_SUPPORT_VFAT
+static const int vfat_enabled = 1;
+#else
+static const int vfat_enabled = 0;
#endif
/*
* Convert a string to lowercase.
*/
-static void downcase (char *str)
+static void downcase(char *str)
{
while (*str != '\0') {
- TOLOWER(*str);
+ *str = tolower(*str);
str++;
}
}
-static block_dev_desc_t *cur_dev = NULL;
-
-static unsigned long part_offset = 0;
+static block_dev_desc_t *cur_dev;
+static disk_partition_t cur_part_info;
-static int cur_part = 1;
-
-#define BYTE_PER_SEC 512
-#define RESERVED_CNT 32
-#define DOS_PART_TBL_OFFSET 0x1be
-#define DOS_PART_MAGIC_OFFSET 0x1fe
+#define DOS_BOOT_MAGIC_OFFSET 0x1fe
#define DOS_FS_TYPE_OFFSET 0x36
#define DOS_FS32_TYPE_OFFSET 0x52
-static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
+static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
{
- if (cur_dev == NULL)
+ if (!cur_dev || !cur_dev->block_read)
return -1;
- startblock += part_offset;
-
- if (cur_dev->block_read) {
- return cur_dev->block_read(cur_dev->dev, startblock, getsize,
- (unsigned long *) bufptr);
- }
- return -1;
+ return cur_dev->block_read(cur_dev->dev,
+ cur_part_info.start + block, nr_blocks, buf);
}
-int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
+int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
{
- unsigned char buffer[SECTOR_SIZE];
-
- disk_partition_t info;
-
- if (!dev_desc->block_read)
- return -1;
+ ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
cur_dev = dev_desc;
- /* check if we have a MBR (on floppies we have only a PBR) */
- if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)buffer) != 1) {
- printf("** Can't read from device %d **\n",
- dev_desc->dev);
- return -1;
- }
- if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
- buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
- /* no signature found */
- return -1;
- }
-#if (defined(CONFIG_CMD_IDE) || \
- defined(CONFIG_CMD_SCSI) || \
- defined(CONFIG_CMD_USB) || \
- defined(CONFIG_MMC) || \
- defined(CONFIG_SYSTEMACE) )
- /* First we assume there is a MBR */
- if (!get_partition_info(dev_desc, part_no, &info)) {
- part_offset = info.start;
- cur_part = part_no;
- } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
- (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
- /* ok, we assume we are on a PBR only */
- cur_part = 1;
- part_offset = 0;
- } else {
- printf("** Partition %d not valid on device %d **\n",
- part_no, dev_desc->dev);
- return -1;
- }
+ cur_part_info = *info;
-#else
- if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
- (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
- /* ok, we assume we are on a PBR only */
- cur_part = 1;
- part_offset = 0;
- info.start = part_offset;
- } else {
- /* FIXME we need to determine the start block of the
- * partition where the DOS FS resides. This can be done
- * by using the get_partition_info routine. For this
- * purpose the libpart must be included.
- */
- part_offset = 32;
- cur_part = 1;
- }
-#endif
- printf("Partition%d: Start Address(0x%x), Size(0x%x)\n", part_no, info.start, info.size);
- return 0;
-}
-
-/*
- * Copy string, padding with spaces.
- */
-static void
-setstr(u_int8_t *dest, const char *src, size_t len)
-{
- while (len--)
- *dest++ = *src ? *src++ : ' ';
-}
-
-static int write_pbr(block_dev_desc_t *dev_desc, disk_partition_t *info)
-{
- struct bs *bs;
- struct bsbpb *bsbpb;
- struct bsxbpb *bsxbpb;
- struct bsx *bsx;
- __u8 *img;
- int img_offset = 0;
- int reserved_cnt= 0;
- int i;
- int fat_size = 0;
-
- img = malloc(sizeof(__u8)*512);
- if(img == NULL) {
- printf("Can't make img buffer~~!!\n");
+ /* Make sure it has a valid FAT header */
+ if (disk_read(0, 1, buffer) != 1) {
+ cur_dev = NULL;
return -1;
}
- memset(img, 0x0, sizeof(__u8)*512);
-
- /* Erase Reserved Sector(PBR) */
- for (i = 0;i < RESERVED_CNT; i++) {
- if (dev_desc->block_write(dev_desc->dev, info->start + i, 1, (ulong *)img) != 1) {
- printf ("Can't erase reserved sector~~~!!!\n");
- return -1;
- }
- }
-
- /* Set bs */
- bs = (struct bs *)img;
- img_offset += sizeof(struct bs) - 1;
-
- mk1(bs->jmp[0], 0xeb);
- mk1(bs->jmp[1], 0x58);
- mk1(bs->jmp[2], 0x90); /* Jump Boot Code */
- setstr(bs->oem, "SAMSUNG", sizeof(bs->oem)); /* OEM Name */
-
- uint spc;
- /* Set bsbpb */
- bsbpb = (struct bsbpb *)(img + img_offset);
- img_offset += sizeof(struct bsbpb) - 2;
-
- mk2(bsbpb->bps, 512); /* Byte Per Sector */
-
- printf("size checking ...\n");
- /* Sector Per Cluster */
- if (info->size < 0x10000) { /* partition size >= 32Mb */
- printf("Can't format less than 32Mb partition!!\n");
+ /* Check if it's actually a DOS volume */
+ if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
+ cur_dev = NULL;
return -1;
}
- if (info->size <= 0x20000) { /* under 64M -> 512 bytes */
- printf("Under 64M\n");
- mk1(bsbpb->spc, 1);
- spc = 1;
- }
- else if (info->size <= 0x40000) { /* under 128M -> 1K */
- printf("Under 128M\n");
- mk1(bsbpb->spc, 2);
- spc = 2;
- }
- else if (info->size <= 0x80000) { /* under 256M -> 2K */
- printf("Under 256M\n");
- mk1(bsbpb->spc, 4);
- spc = 4;
- }
- else if (info->size <= 0xFA0000) { /* under 8G -> 4K */
- printf("Under 8G\n");
- mk1(bsbpb->spc, 8);
- spc = 8;
- }
- else if (info->size <= 0x1F40000) { /* under 16G -> 8K */
- printf("Under 16G\n");
- mk1(bsbpb->spc, 16);
- spc = 16;
- }
- else {
- printf("16G~\n");
- mk1(bsbpb->spc, 32);
- spc = 32;
- }
- printf("write FAT info: %d\n",RESERVED_CNT);
- mk2(bsbpb->res, RESERVED_CNT); /* Reserved Sector Count */
- mk1(bsbpb->nft, 2); /* Number of FATs */
- mk2(bsbpb->rde, 0); /* Root Directory Entry Count : It's no use in FAT32 */
- mk2(bsbpb->sec, 0); /* Total Sector : It's no use in FAT32 */
- mk1(bsbpb->mid, 0xF8); /* Media */
- mk2(bsbpb->spf, 0); /* FAT Size 16 : It's no use in FAT32 */
- mk2(bsbpb->spt, 0); /* Sector Per Track */
- mk2(bsbpb->hds, 0); /* Number Of Heads */
- mk4(bsbpb->hid, 0); /* Hidden Sector */
- mk4(bsbpb->bsec, info->size); /* Total Sector For FAT32 */
-
- /* Set bsxbpb */
- bsxbpb = (struct bsxbpb *)(img + img_offset);
- img_offset += sizeof(struct bsxbpb);
-
- mk4(bsxbpb->bspf, (info->size / (spc * 128))); /* FAT Size 32 */
- fat_size = info->size / (spc * 128);
- printf("Fat size : 0x%x\n", info->size / (spc * 128));
- mk2(bsxbpb->xflg, 0); /* Ext Flags */
- mk2(bsxbpb->vers, 0); /* File System Version */
- mk4(bsxbpb->rdcl, 2); /* Root Directory Cluster */
- mk2(bsxbpb->infs, 1); /* File System Information */
- mk2(bsxbpb->bkbs, 0); /* Boot Record Backup Sector */
-
- /* Set bsx */
- bsx = (struct bsx *)(img + img_offset);
- mk1(bsx->drv, 0); /* Drive Number */
- mk1(bsx->sig, 0x29); /* Boot Signature */
- mk4(bsx->volid, 0x3333); /* Volume ID : 0x3333 means nothing */
- setstr(bsx->label, "NO NAME ", sizeof(bsx->label)); /* Volume Label */
- setstr(bsx->type, "FAT32", sizeof(bsx->type)); /* File System Type */
-
- /* Set Magic Number */
- mk2(img + BYTE_PER_SEC - 2, 0xaa55); /* Signature */
-
-/*
- printf("Print Boot Recode\n");
- for(i = 0;i<512;i++) {
- if(img[i] == 0)
- printf("00 ");
- else
- printf("%2x ", img[i]);
- if (!((i+1) % 16))
- printf("\n");
- }
-*/
-
- if (dev_desc->block_write(dev_desc->dev, info->start, 1, (ulong *)img) != 1) {
- printf ("Can't write PBR~~~!!!\n");
- return -1;
- }
+ /* Check for FAT12/FAT16/FAT32 filesystem */
+ if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
+ return 0;
+ if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
+ return 0;
- return fat_size;
+ cur_dev = NULL;
+ return -1;
}
-static int write_reserved(block_dev_desc_t *dev_desc, disk_partition_t *info)
-{
- /* Set Reserved Region */
- __u8 *img;
- int i;
- img = malloc(sizeof(__u8)*512);
- if(img == NULL) {
- printf("Can't make img buffer~~(reserved)!!\n");
- return -1;
- }
- memset(img, 0x0, sizeof(__u8)*512);
-
- mk4(img, 0x41615252); /* Lead Signature */
- mk4(img + BYTE_PER_SEC - 28, 0x61417272); /* Struct Signature */
- mk4(img + BYTE_PER_SEC - 24, 0xffffffff); /* Free Cluster Count */
- mk4(img + BYTE_PER_SEC - 20, 0x3); /* Next Free Cluster */
- mk2(img + BYTE_PER_SEC - 2, 0xaa55); /* Trail Signature */
-
- /*
- printf("Print Reserved Region\n");
- for(i = 0;i<512;i++) {
- if(img[i] == 0)
- printf("00 ");
- else
- printf("%2x ", img[i]);
- if (!((i+1) % 16))
- printf("\n");
- }
- */
-
- /* Write Reserved region */
- if (dev_desc->block_write(dev_desc->dev, info->start+1, 1, (ulong *)img) != 1) {
- printf ("Can't write reserved region~~~!!!\n");
- return -1;
- }
-
- return 1;
-}
-static int write_fat(block_dev_desc_t *dev_desc, disk_partition_t *info, int
-fat_size)
+int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
{
- __u8 *dummy;
- __u8 *img;
- int i;
+ disk_partition_t info;
- /* Create buffer for FAT */
- img = malloc(sizeof(__u8)*512);
- if(img == NULL) {
- printf("Can't make img buffer~~!!\n");
- return -1;
- }
- memset(img, 0x0, sizeof(__u8) * 512);
+ /* First close any currently found FAT filesystem */
+ cur_dev = NULL;
- /* Create buffer for erase */
- dummy = malloc(sizeof(__u8) * 8192);
- if(dummy == NULL) {
- printf("Can't make dummy buffer~~!!\n");
- return -1;
- }
- memset(dummy, 0x0, sizeof(__u8) * 8192);
-
- /* Erase FAT Region */
- int erase_block_cnt = (fat_size * 2);
- int temp = 0;
- printf("Erase FAT region");
- for (i = 0;i < erase_block_cnt + 10; i+=16) {
- if (dev_desc->block_write(dev_desc->dev, info->start +
- RESERVED_CNT + i, 16, (ulong *)dummy) != 16) {
- printf ("Can't erase FAT region~~!!!\n");
+ /* Read the partition table, if present */
+ if (get_partition_info(dev_desc, part_no, &info)) {
+ if (part_no != 0) {
+ printf("** Partition %d not valid on device %d **\n",
+ part_no, dev_desc->dev);
+ return -1;
}
- if((i % 160) == 0)
- printf(".");
-
- }
- printf("\n");
-
- mk4(img, 0x0ffffff8);
- mk4(img+4, 0x0fffffff);
- mk4(img+8, 0x0fffffff); /* Root Directory */
-
- /*
- printf("Print FAT Region\n");
- for(i = 0;i<512;i++) {
- if(img[i] == 0)
- printf("00 ");
- else
- printf("%2x ", img[i]);
- if (!((i+1) % 16))
- printf("\n");
- }
- */
- /* Write FAT Region */
- if (dev_desc->block_write(dev_desc->dev, info->start + RESERVED_CNT, 1, (ulong *)img) != 1) {
- printf ("Can't write FAT~~~!!!\n");
- return -1;
- }
- return 1;
-}
-
-/*
- * Make a volume label.
- */
-
-static void
-mklabel(u_int8_t *dest, const char *src)
-{
- int c, i;
-
- for (i = 0; i < 11; i++) {
- c = *src ? __toupper(*src++) : ' ';
- *dest++ = !i && c == '\xe5' ? 5 : c;
- }
-}
-
-/*
- * Format device
- */
-int
-fat_format_device(block_dev_desc_t *dev_desc, int part_no)
-{
- unsigned char buffer[SECTOR_SIZE];
- disk_partition_t info;
-
- if (!dev_desc->block_read)
- return -1;
- cur_dev = dev_desc;
- /* check if we have a MBR (on floppies we have only a PBR) */
- if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
- printf ("** Can't read from device %d **\n", dev_desc->dev);
- return -1;
- }
- if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
- buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
- printf("** MBR is broken **\n");
- /* no signature found */
- return -1;
- }
-
-#if (defined(CONFIG_CMD_IDE) || \
- defined(CONFIG_CMD_SCSI) || \
- defined(CONFIG_CMD_USB) || \
- defined(CONFIG_MMC) || \
- defined(CONFIG_SYSTEMACE) )
- /* First we assume, there is a MBR */
- if (!get_partition_info (dev_desc, part_no, &info)) {
- part_offset = info.start;
- cur_part = part_no;
- } else if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3)) {
- /* ok, we assume we are on a PBR only */
- cur_part = 1;
- part_offset = 0;
- } else {
- printf ("** Partition %d not valid on device %d **\n",
- part_no, dev_desc->dev);
- return -1;
- }
+ info.start = 0;
+ info.size = dev_desc->lba;
+ info.blksz = dev_desc->blksz;
+ info.name[0] = 0;
+ info.type[0] = 0;
+ info.bootable = 0;
+#ifdef CONFIG_PARTITION_UUIDS
+ info.uuid[0] = 0;
#endif
+ }
- printf("Partition%d: Start Address(0x%x), Size(0x%x)\n", part_no, info.start, info.size);
-
- int fat_size;
- fat_size = write_pbr(dev_desc, &info);
- if(fat_size < 0)
- return -1;
- if(write_reserved(dev_desc, &info) < 0)
- return -1;
- if(write_fat(dev_desc, &info, fat_size) < 0)
- return -1;
- printf("Partition%d format complete.\n", part_no);
-
- return 0;
+ return fat_set_blk_dev(dev_desc, &info);
}
/*
* Get the first occurence of a directory delimiter ('/' or '\') in a string.
* Return index into string if found, -1 otherwise.
*/
-static int dirdelim (char *str)
+static int dirdelim(char *str)
{
char *start = str;
@@ -480,32 +126,10 @@ static int dirdelim (char *str)
return -1;
}
-
-/*
- * Match volume_info fs_type strings.
- * Return 0 on match, -1 otherwise.
- */
-static int
-compare_sign(char *str1, char *str2)
-{
- char *end = str1+SIGNLEN;
-
- while (str1 != end) {
- if (*str1 != *str2) {
- return -1;
- }
- str1++;
- str2++;
- }
-
- return 0;
-}
-
-
/*
* Extract zero terminated short name from a directory entry.
*/
-static void get_name (dir_entry *dirent, char *s_name)
+static void get_name(dir_entry *dirent, char *s_name)
{
char *ptr;
@@ -526,7 +150,7 @@ static void get_name (dir_entry *dirent, char *s_name)
if (*s_name == DELETED_FLAG)
*s_name = '\0';
else if (*s_name == aRING)
- *s_name = '?';
+ *s_name = DELETED_FLAG;
downcase(s_name);
}
@@ -534,7 +158,7 @@ static void get_name (dir_entry *dirent, char *s_name)
* Get the entry at index 'entry' in a FAT (12/16/32) table.
* On failure 0x00 is returned.
*/
-static __u32 get_fatent (fsdata *mydata, __u32 entry)
+static __u32 get_fatent(fsdata *mydata, __u32 entry)
{
__u32 bufnum;
__u32 off16, offset;
@@ -565,16 +189,16 @@ static __u32 get_fatent (fsdata *mydata, __u32 entry)
/* Read a new block of FAT entries into the cache. */
if (bufnum != mydata->fatbufnum) {
- int getsize = FATBUFSIZE / FS_BLOCK_SIZE;
+ __u32 getsize = FATBUFBLOCKS;
__u8 *bufptr = mydata->fatbuf;
__u32 fatlength = mydata->fatlength;
__u32 startblock = bufnum * FATBUFBLOCKS;
- fatlength *= SECTOR_SIZE; /* We want it in bytes now */
+ if (startblock + getsize > fatlength)
+ getsize = fatlength - startblock;
+
startblock += mydata->fat_sect; /* Offset from start of disk */
- if (getsize > fatlength)
- getsize = fatlength;
if (disk_read(startblock, getsize, bufptr) < 0) {
debug("Error reading FAT blocks\n");
return ret;
@@ -632,11 +256,11 @@ static __u32 get_fatent (fsdata *mydata, __u32 entry)
* Return 0 on success, -1 otherwise.
*/
static int
-get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
- unsigned long size)
+get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
{
- int idx = 0;
+ __u32 idx = 0;
__u32 startsect;
+ int ret;
if (clustnum > 0) {
startsect = mydata->data_begin +
@@ -647,56 +271,123 @@ get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
- if (disk_read(startsect, size / FS_BLOCK_SIZE, buffer) < 0) {
- debug("Error reading data\n");
- return -1;
+ if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
+ ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
+
+ printf("FAT: Misaligned buffer address (%p)\n", buffer);
+
+ while (size >= mydata->sect_size) {
+ ret = disk_read(startsect++, 1, tmpbuf);
+ if (ret != 1) {
+ debug("Error reading data (got %d)\n", ret);
+ return -1;
+ }
+
+ memcpy(buffer, tmpbuf, mydata->sect_size);
+ buffer += mydata->sect_size;
+ size -= mydata->sect_size;
+ }
+ } else {
+ idx = size / mydata->sect_size;
+ ret = disk_read(startsect, idx, buffer);
+ if (ret != idx) {
+ debug("Error reading data (got %d)\n", ret);
+ return -1;
+ }
+ startsect += idx;
+ idx *= mydata->sect_size;
+ buffer += idx;
+ size -= idx;
}
- if (size % FS_BLOCK_SIZE) {
- __u8 tmpbuf[FS_BLOCK_SIZE];
+ if (size) {
+ ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
- idx = size / FS_BLOCK_SIZE;
- if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
- debug("Error reading data\n");
+ ret = disk_read(startsect, 1, tmpbuf);
+ if (ret != 1) {
+ debug("Error reading data (got %d)\n", ret);
return -1;
}
- buffer += idx * FS_BLOCK_SIZE;
- memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
- return 0;
+ memcpy(buffer, tmpbuf, size);
}
return 0;
}
/*
- * Read at most 'maxsize' bytes from the file associated with 'dentptr'
+ * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
* into 'buffer'.
* Return the number of bytes read or -1 on fatal errors.
*/
+__u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
+ __aligned(ARCH_DMA_MINALIGN);
+
static long
-get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
- unsigned long maxsize)
+get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
+ __u8 *buffer, unsigned long maxsize)
{
unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
- unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
+ unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
__u32 curclust = START(dentptr);
__u32 endclust, newclust;
unsigned long actsize;
debug("Filesize: %ld bytes\n", filesize);
- if (maxsize > 0 && filesize > maxsize)
- filesize = maxsize;
+ if (pos >= filesize) {
+ debug("Read position past EOF: %lu\n", pos);
+ return gotsize;
+ }
+
+ if (maxsize > 0 && filesize > pos + maxsize)
+ filesize = pos + maxsize;
debug("%ld bytes\n", filesize);
actsize = bytesperclust;
- endclust = curclust;
- if(filesize < actsize) {
- printf("Warning : Reads a file that is smaller than the cluster size.\n");
- goto clustread;
- }
+ /* go to cluster at pos */
+ while (actsize <= pos) {
+ curclust = get_fatent(mydata, curclust);
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
+ debug("curclust: 0x%x\n", curclust);
+ debug("Invalid FAT entry\n");
+ return gotsize;
+ }
+ actsize += bytesperclust;
+ }
+
+ /* actsize > pos */
+ actsize -= bytesperclust;
+ filesize -= actsize;
+ pos -= actsize;
+
+ /* align to beginning of next cluster if any */
+ if (pos) {
+ actsize = min(filesize, bytesperclust);
+ if (get_cluster(mydata, curclust, get_contents_vfatname_block,
+ (int)actsize) != 0) {
+ printf("Error reading cluster\n");
+ return -1;
+ }
+ filesize -= actsize;
+ actsize -= pos;
+ memcpy(buffer, get_contents_vfatname_block + pos, actsize);
+ gotsize += actsize;
+ if (!filesize)
+ return gotsize;
+ buffer += actsize;
+
+ curclust = get_fatent(mydata, curclust);
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
+ debug("curclust: 0x%x\n", curclust);
+ debug("Invalid FAT entry\n");
+ return gotsize;
+ }
+ }
+
+ actsize = bytesperclust;
+ endclust = curclust;
do {
/* search for consecutive clusters */
@@ -713,37 +404,14 @@ get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
actsize += bytesperclust;
}
- /* actsize >= file size */
- actsize -= bytesperclust;
-
- /* get remaining clusters */
- if(actsize != 0) {
- if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
- printf("Error! reading remaining clusters.\n");
- printf("actsize = %d, bytesperclust = %d\n", actsize, bytesperclust);
- return -1;
- }
- }
-
/* get remaining bytes */
- gotsize += (int)actsize;
- filesize -= actsize;
- buffer += actsize;
actsize = filesize;
- if (get_cluster(mydata, endclust, buffer, (int)bytesperclust) != 0) {
- printf("Error! reading remaining bytes.\n");
+ if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
+ printf("Error reading cluster\n");
return -1;
}
gotsize += actsize;
return gotsize;
-clustread:
- /* get 1 cluster */
- if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
- printf("Error! reading cluster\n");
- return -1;
- }
- gotsize = filesize;
- return gotsize;
getit:
if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
printf("Error reading cluster\n");
@@ -764,13 +432,12 @@ getit:
} while (1);
}
-#ifdef CONFIG_SUPPORT_VFAT
/*
* Extract the file name information from 'slotptr' into 'l_name',
* starting at l_name[*idx].
* Return 1 if terminator (zero byte) is found, 0 otherwise.
*/
-static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
+static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
{
int j;
@@ -802,18 +469,15 @@ static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
* into 'retdent'
* Return 0 on success, -1 otherwise.
*/
-__u8 get_vfatname_block[MAX_CLUSTSIZE];
-
static int
-get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
- dir_entry *retdent, char *l_name)
+get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
+ dir_entry *retdent, char *l_name)
{
dir_entry *realdent;
dir_slot *slotptr = (dir_slot *)retdent;
- __u8 *buflimit = cluster + ((curclust == 0) ?
- LINEAR_PREFETCH_SIZE :
- (mydata->clust_size * SECTOR_SIZE)
- );
+ __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
+ PREFETCH_BLOCKS :
+ mydata->clust_size);
__u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
int idx = 0;
@@ -843,13 +507,13 @@ get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
return -1;
}
- if (get_cluster(mydata, curclust, get_vfatname_block,
- mydata->clust_size * SECTOR_SIZE) != 0) {
+ if (get_cluster(mydata, curclust, get_contents_vfatname_block,
+ mydata->clust_size * mydata->sect_size) != 0) {
debug("Error: reading directory block\n");
return -1;
}
- slotptr2 = (dir_slot *)get_vfatname_block;
+ slotptr2 = (dir_slot *)get_contents_vfatname_block;
while (counter > 0) {
if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
& 0xff) != counter)
@@ -860,7 +524,7 @@ get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
/* Save the real directory entry */
realdent = (dir_entry *)slotptr2;
- while ((__u8 *)slotptr2 > get_vfatname_block) {
+ while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
slotptr2--;
slot2str(slotptr2, l_name, &idx);
}
@@ -879,7 +543,7 @@ get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
if (*l_name == DELETED_FLAG)
*l_name = '\0';
else if (*l_name == aRING)
- *l_name = '?';
+ *l_name = DELETED_FLAG;
downcase(l_name);
/* Return the real directory entry */
@@ -889,29 +553,30 @@ get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
}
/* Calculate short name checksum */
-static __u8 mkcksum (const char *str)
+static __u8 mkcksum(const char name[8], const char ext[3])
{
int i;
__u8 ret = 0;
- for (i = 0; i < 11; i++) {
- ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
- }
+ for (i = 0; i < 8; i++)
+ ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
+ for (i = 0; i < 3; i++)
+ ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
return ret;
}
-#endif /* CONFIG_SUPPORT_VFAT */
/*
* Get the directory entry associated with 'filename' from the directory
* starting at 'startsect'
*/
-__u8 get_dentfromdir_block[MAX_CLUSTSIZE];
+__u8 get_dentfromdir_block[MAX_CLUSTSIZE]
+ __aligned(ARCH_DMA_MINALIGN);
-static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
- char *filename, dir_entry *retdent,
- int dols)
+static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
+ char *filename, dir_entry *retdent,
+ int dols)
{
__u16 prevcksum = 0xffff;
__u32 curclust = START(retdent);
@@ -925,7 +590,7 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
int i;
if (get_cluster(mydata, curclust, get_dentfromdir_block,
- mydata->clust_size * SECTOR_SIZE) != 0) {
+ mydata->clust_size * mydata->sect_size) != 0) {
debug("Error: reading directory block\n");
return NULL;
}
@@ -941,9 +606,9 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
continue;
}
if ((dentptr->attr & ATTR_VOLUME)) {
-#ifdef CONFIG_SUPPORT_VFAT
- if ((dentptr->attr & ATTR_VFAT) &&
- (dentptr-> name[0] & LAST_LONG_ENTRY_MASK)) {
+ if (vfat_enabled &&
+ (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
+ (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
prevcksum = ((dir_slot *)dentptr)->alias_checksum;
get_vfatname(mydata, curclust,
get_dentfromdir_block,
@@ -982,9 +647,7 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
continue;
}
debug("vfatname: |%s|\n", l_name);
- } else
-#endif
- {
+ } else {
/* Volume label or VFAT entry */
dentptr++;
continue;
@@ -998,12 +661,15 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
debug("Dentname == NULL - %d\n", i);
return NULL;
}
-#ifdef CONFIG_SUPPORT_VFAT
- if (dols && mkcksum(dentptr->name) == prevcksum) {
- dentptr++;
- continue;
+ if (vfat_enabled) {
+ __u8 csum = mkcksum(dentptr->name, dentptr->ext);
+ if (dols && csum == prevcksum) {
+ prevcksum = 0xffff;
+ dentptr++;
+ continue;
+ }
}
-#endif
+
get_name(dentptr, s_name);
if (dols) {
int isdir = (dentptr->attr & ATTR_DIR);
@@ -1070,17 +736,28 @@ static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
* Read boot sector and volume info from a FAT filesystem
*/
static int
-read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
+read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
{
- __u8 block[FS_BLOCK_SIZE];
-
+ __u8 *block;
volume_info *vistart;
+ int ret = 0;
- if (disk_read (0, 1, block) < 0) {
- debug("Error: reading block\n");
+ if (cur_dev == NULL) {
+ debug("Error: no device selected\n");
return -1;
}
+ block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
+ if (block == NULL) {
+ debug("Error: allocating block\n");
+ return -1;
+ }
+
+ if (disk_read(0, 1, block) < 0) {
+ debug("Error: reading block\n");
+ goto fail;
+ }
+
memcpy(bs, block, sizeof(boot_sector));
bs->reserved = FAT2CPU16(bs->reserved);
bs->fat_length = FAT2CPU16(bs->fat_length);
@@ -1104,49 +781,50 @@ read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
}
memcpy(volinfo, vistart, sizeof(volume_info));
- /* Terminate fs_type string. Writing past the end of vistart
- is ok - it's just the buffer. */
- vistart->fs_type[8] = '\0';
-
if (*fatsize == 32) {
- if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
- return 0;
- }
+ if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
+ goto exit;
} else {
- if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
+ if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
*fatsize = 12;
- return 0;
+ goto exit;
}
- if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
+ if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
*fatsize = 16;
- return 0;
+ goto exit;
}
}
debug("Error: broken fs_type sign\n");
- return -1;
+fail:
+ ret = -1;
+exit:
+ free(block);
+ return ret;
}
-__u8 do_fat_read_block[MAX_CLUSTSIZE]; /* Block buffer */
+__u8 do_fat_read_at_block[MAX_CLUSTSIZE]
+ __aligned(ARCH_DMA_MINALIGN);
long
-do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
- int dols)
+do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
+ unsigned long maxsize, int dols)
{
char fnamecopy[2048];
boot_sector bs;
volume_info volinfo;
fsdata datablock;
fsdata *mydata = &datablock;
- dir_entry *dentptr;
+ dir_entry *dentptr = NULL;
__u16 prevcksum = 0xffff;
char *subname = "";
- int cursect;
+ __u32 cursect;
int idx, isdir = 0;
int files = 0, dirs = 0;
- long ret = 0;
+ long ret = -1;
int firsttime;
- int root_cluster;
+ __u32 root_cluster = 0;
+ int rootdir_size = 0;
int j;
if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
@@ -1154,48 +832,58 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
return -1;
}
- root_cluster = bs.root_cluster;
-
- if (mydata->fatsize == 32)
+ if (mydata->fatsize == 32) {
+ root_cluster = bs.root_cluster;
mydata->fatlength = bs.fat32_length;
- else
+ } else {
mydata->fatlength = bs.fat_length;
+ }
mydata->fat_sect = bs.reserved;
cursect = mydata->rootdir_sect
= mydata->fat_sect + mydata->fatlength * bs.fats;
+ mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
mydata->clust_size = bs.cluster_size;
+ if (mydata->sect_size != cur_part_info.blksz) {
+ printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
+ mydata->sect_size, cur_part_info.blksz);
+ return -1;
+ }
if (mydata->fatsize == 32) {
mydata->data_begin = mydata->rootdir_sect -
(mydata->clust_size * 2);
} else {
- int rootdir_size;
-
rootdir_size = ((bs.dir_entries[1] * (int)256 +
bs.dir_entries[0]) *
sizeof(dir_entry)) /
- SECTOR_SIZE;
+ mydata->sect_size;
mydata->data_begin = mydata->rootdir_sect +
rootdir_size -
(mydata->clust_size * 2);
}
mydata->fatbufnum = -1;
+ mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
+ if (mydata->fatbuf == NULL) {
+ debug("Error: allocating memory\n");
+ return -1;
+ }
+
+ if (vfat_enabled)
+ debug("VFAT Support enabled\n");
-#ifdef CONFIG_SUPPORT_VFAT
- debug("VFAT Support enabled\n");
-#endif
debug("FAT%d, fat_sect: %d, fatlength: %d\n",
mydata->fatsize, mydata->fat_sect, mydata->fatlength);
debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
"Data begins at: %d\n",
root_cluster,
mydata->rootdir_sect,
- mydata->rootdir_sect * SECTOR_SIZE, mydata->data_begin);
- debug("Cluster size: %d\n", mydata->clust_size);
+ mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
+ debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
+ mydata->clust_size);
/* "cwd" is always the root... */
while (ISDIRDELIM(*filename))
@@ -1207,7 +895,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
if (*fnamecopy == '\0') {
if (!dols)
- return -1;
+ goto exit;
dols = LS_ROOT;
} else if ((idx = dirdelim(fnamecopy)) >= 0) {
@@ -1226,40 +914,45 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
while (1) {
int i;
- debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
- cursect, mydata->clust_size, DIRENTSPERBLOCK);
+ if (j == 0) {
+ debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
+ cursect, mydata->clust_size, DIRENTSPERBLOCK);
+
+ if (disk_read(cursect,
+ (mydata->fatsize == 32) ?
+ (mydata->clust_size) :
+ PREFETCH_BLOCKS,
+ do_fat_read_at_block) < 0) {
+ debug("Error: reading rootdir block\n");
+ goto exit;
+ }
- if (disk_read(cursect,
- (mydata->fatsize == 32) ?
- (mydata->clust_size) :
- LINEAR_PREFETCH_SIZE / SECTOR_SIZE,
- do_fat_read_block) < 0) {
- debug("Error: reading rootdir block\n");
- return -1;
+ dentptr = (dir_entry *) do_fat_read_at_block;
}
- dentptr = (dir_entry *) do_fat_read_block;
-
for (i = 0; i < DIRENTSPERBLOCK; i++) {
char s_name[14], l_name[VFAT_MAXLEN_BYTES];
+ __u8 csum;
l_name[0] = '\0';
if (dentptr->name[0] == DELETED_FLAG) {
dentptr++;
continue;
}
- if ((dentptr->attr & ATTR_VOLUME)) {
-#ifdef CONFIG_SUPPORT_VFAT
- if ((dentptr->attr & ATTR_VFAT) &&
+
+ if (vfat_enabled)
+ csum = mkcksum(dentptr->name, dentptr->ext);
+
+ if (dentptr->attr & ATTR_VOLUME) {
+ if (vfat_enabled &&
+ (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
(dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
prevcksum =
((dir_slot *)dentptr)->alias_checksum;
get_vfatname(mydata,
- (mydata->fatsize == 32) ?
- root_cluster :
- 0,
- do_fat_read_block,
+ root_cluster,
+ do_fat_read_at_block,
dentptr, l_name);
if (dols == LS_ROOT) {
@@ -1296,9 +989,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
}
debug("Rootvfatname: |%s|\n",
l_name);
- } else
-#endif
- {
+ } else {
/* Volume label or VFAT entry */
dentptr++;
continue;
@@ -1308,17 +999,17 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
if (dols == LS_ROOT) {
printf("\n%d file(s), %d dir(s)\n\n",
files, dirs);
- return 0;
+ ret = 0;
}
- return -1;
+ goto exit;
}
-#ifdef CONFIG_SUPPORT_VFAT
- else if (dols == LS_ROOT &&
- mkcksum(dentptr->name) == prevcksum) {
+ else if (vfat_enabled &&
+ dols == LS_ROOT && csum == prevcksum) {
+ prevcksum = 0xffff;
dentptr++;
continue;
}
-#endif
+
get_name(dentptr, s_name);
if (dols == LS_ROOT) {
@@ -1362,7 +1053,7 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
}
if (isdir && !(dentptr->attr & ATTR_DIR))
- return -1;
+ goto exit;
debug("RootName: %s", s_name);
debug(", start: 0x%x", START(dentptr));
@@ -1380,26 +1071,40 @@ do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
* root directory clusters when a cluster has been
* completely processed.
*/
- if ((mydata->fatsize == 32) && (++j == mydata->clust_size)) {
- int nxtsect;
- int nxt_clust;
+ ++j;
+ int rootdir_end = 0;
+ if (mydata->fatsize == 32) {
+ if (j == mydata->clust_size) {
+ int nxtsect = 0;
+ int nxt_clust = 0;
- nxt_clust = get_fatent(mydata, root_cluster);
+ nxt_clust = get_fatent(mydata, root_cluster);
+ rootdir_end = CHECK_CLUST(nxt_clust, 32);
- nxtsect = mydata->data_begin +
- (nxt_clust * mydata->clust_size);
+ nxtsect = mydata->data_begin +
+ (nxt_clust * mydata->clust_size);
- debug("END LOOP: sect=%d, root_clust=%d, "
- "n_sect=%d, n_clust=%d\n",
- cursect, root_cluster,
- nxtsect, nxt_clust);
+ root_cluster = nxt_clust;
- root_cluster = nxt_clust;
-
- cursect = nxtsect;
- j = 0;
+ cursect = nxtsect;
+ j = 0;
+ }
} else {
- cursect++;
+ if (j == PREFETCH_BLOCKS)
+ j = 0;
+
+ rootdir_end = (++cursect - mydata->rootdir_sect >=
+ rootdir_size);
+ }
+
+ /* If end of rootdir reached */
+ if (rootdir_end) {
+ if (dols == LS_ROOT) {
+ printf("\n%d file(s), %d dir(s)\n\n",
+ files, dirs);
+ ret = 0;
+ }
+ goto exit;
}
}
rootdir_done:
@@ -1436,24 +1141,32 @@ rootdir_done:
if (get_dentfromdir(mydata, startsect, subname, dentptr,
isdir ? 0 : dols) == NULL) {
if (dols && !isdir)
- return 0;
- return -1;
+ ret = 0;
+ goto exit;
}
- if (idx >= 0) {
- if (!(dentptr->attr & ATTR_DIR))
- return -1;
+ if (isdir && !(dentptr->attr & ATTR_DIR))
+ goto exit;
+
+ if (idx >= 0)
subname = nextname;
- }
}
- ret = get_contents(mydata, dentptr, buffer, maxsize);
+ ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
+exit:
+ free(mydata->fatbuf);
return ret;
}
-int file_fat_detectfs ()
+long
+do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
+{
+ return do_fat_read_at(filename, 0, buffer, maxsize, dols);
+}
+
+int file_fat_detectfs(void)
{
boot_sector bs;
volume_info volinfo;
@@ -1466,6 +1179,7 @@ int file_fat_detectfs ()
}
#if defined(CONFIG_CMD_IDE) || \
+ defined(CONFIG_CMD_SATA) || \
defined(CONFIG_CMD_SCSI) || \
defined(CONFIG_CMD_USB) || \
defined(CONFIG_MMC)
@@ -1474,6 +1188,9 @@ int file_fat_detectfs ()
case IF_TYPE_IDE:
printf("IDE");
break;
+ case IF_TYPE_SATA:
+ printf("SATA");
+ break;
case IF_TYPE_SCSI:
printf("SCSI");
break;
@@ -1487,7 +1204,7 @@ int file_fat_detectfs ()
printf("DOC");
break;
case IF_TYPE_MMC:
- printf("SD/MMC");
+ printf("MMC");
break;
default:
printf("Unknown");
@@ -1506,21 +1223,41 @@ int file_fat_detectfs ()
vol_label[11] = '\0';
volinfo.fs_type[5] = '\0';
- printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
- volinfo.fs_type, vol_label);
+ printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
return 0;
}
-int file_fat_ls (const char *dir)
+int file_fat_ls(const char *dir)
{
return do_fat_read(dir, NULL, 0, LS_YES);
}
-long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
+long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
+ unsigned long maxsize)
{
printf("reading %s\n", filename);
- return do_fat_read(filename, buffer, maxsize, LS_NO);
+ return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
}
-#endif
+long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
+{
+ return file_fat_read_at(filename, 0, buffer, maxsize);
+}
+
+int fat_read_file(const char *filename, void *buf, int offset, int len)
+{
+ int len_read;
+
+ len_read = file_fat_read_at(filename, offset, buf, len);
+ if (len_read == -1) {
+ printf("** Unable to read file %s **\n", filename);
+ return -1;
+ }
+
+ return len_read;
+}
+
+void fat_close(void)
+{
+}
diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
new file mode 100644
index 0000000000..b7a21e05bf
--- /dev/null
+++ b/fs/fat/fat_write.c
@@ -0,0 +1,1079 @@
+/*
+ * fat_write.c
+ *
+ * R/W (V)FAT 12/16/32 filesystem implementation by Donggeun Kim
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#include <common.h>
+#include <command.h>
+#include <config.h>
+#include <fat.h>
+#include <asm/byteorder.h>
+#include <part.h>
+#include <linux/ctype.h>
+#include "fat.c"
+
+static void uppercase(char *str, int len)
+{
+ int i;
+
+ for (i = 0; i < len; i++) {
+ *str = toupper(*str);
+ str++;
+ }
+}
+
+static int total_sector;
+static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
+{
+ if (!cur_dev || !cur_dev->block_write)
+ return -1;
+
+ if (cur_part_info.start + block + nr_blocks >
+ cur_part_info.start + total_sector) {
+ printf("error: overflow occurs\n");
+ return -1;
+ }
+
+ return cur_dev->block_write(cur_dev->dev,
+ cur_part_info.start + block, nr_blocks, buf);
+}
+
+/*
+ * Set short name in directory entry
+ */
+static void set_name(dir_entry *dirent, const char *filename)
+{
+ char s_name[VFAT_MAXLEN_BYTES];
+ char *period;
+ int period_location, len, i, ext_num;
+
+ if (filename == NULL)
+ return;
+
+ len = strlen(filename);
+ if (len == 0)
+ return;
+
+ memcpy(s_name, filename, len);
+ uppercase(s_name, len);
+
+ period = strchr(s_name, '.');
+ if (period == NULL) {
+ period_location = len;
+ ext_num = 0;
+ } else {
+ period_location = period - s_name;
+ ext_num = len - period_location - 1;
+ }
+
+ /* Pad spaces when the length of file name is shorter than eight */
+ if (period_location < 8) {
+ memcpy(dirent->name, s_name, period_location);
+ for (i = period_location; i < 8; i++)
+ dirent->name[i] = ' ';
+ } else if (period_location == 8) {
+ memcpy(dirent->name, s_name, period_location);
+ } else {
+ memcpy(dirent->name, s_name, 6);
+ dirent->name[6] = '~';
+ dirent->name[7] = '1';
+ }
+
+ if (ext_num < 3) {
+ memcpy(dirent->ext, s_name + period_location + 1, ext_num);
+ for (i = ext_num; i < 3; i++)
+ dirent->ext[i] = ' ';
+ } else
+ memcpy(dirent->ext, s_name + period_location + 1, 3);
+
+ debug("name : %s\n", dirent->name);
+ debug("ext : %s\n", dirent->ext);
+}
+
+static __u8 num_of_fats;
+/*
+ * Write fat buffer into block device
+ */
+static int flush_fat_buffer(fsdata *mydata)
+{
+ int getsize = FATBUFBLOCKS;
+ __u32 fatlength = mydata->fatlength;
+ __u8 *bufptr = mydata->fatbuf;
+ __u32 startblock = mydata->fatbufnum * FATBUFBLOCKS;
+
+ startblock += mydata->fat_sect;
+
+ if (getsize > fatlength)
+ getsize = fatlength;
+
+ /* Write FAT buf */
+ if (disk_write(startblock, getsize, bufptr) < 0) {
+ debug("error: writing FAT blocks\n");
+ return -1;
+ }
+
+ if (num_of_fats == 2) {
+ /* Update corresponding second FAT blocks */
+ startblock += mydata->fatlength;
+ if (disk_write(startblock, getsize, bufptr) < 0) {
+ debug("error: writing second FAT blocks\n");
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Get the entry at index 'entry' in a FAT (12/16/32) table.
+ * On failure 0x00 is returned.
+ * When bufnum is changed, write back the previous fatbuf to the disk.
+ */
+static __u32 get_fatent_value(fsdata *mydata, __u32 entry)
+{
+ __u32 bufnum;
+ __u32 off16, offset;
+ __u32 ret = 0x00;
+ __u16 val1, val2;
+
+ switch (mydata->fatsize) {
+ case 32:
+ bufnum = entry / FAT32BUFSIZE;
+ offset = entry - bufnum * FAT32BUFSIZE;
+ break;
+ case 16:
+ bufnum = entry / FAT16BUFSIZE;
+ offset = entry - bufnum * FAT16BUFSIZE;
+ break;
+ case 12:
+ bufnum = entry / FAT12BUFSIZE;
+ offset = entry - bufnum * FAT12BUFSIZE;
+ break;
+
+ default:
+ /* Unsupported FAT size */
+ return ret;
+ }
+
+ debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
+ mydata->fatsize, entry, entry, offset, offset);
+
+ /* Read a new block of FAT entries into the cache. */
+ if (bufnum != mydata->fatbufnum) {
+ int getsize = FATBUFBLOCKS;
+ __u8 *bufptr = mydata->fatbuf;
+ __u32 fatlength = mydata->fatlength;
+ __u32 startblock = bufnum * FATBUFBLOCKS;
+
+ if (getsize > fatlength)
+ getsize = fatlength;
+
+ fatlength *= mydata->sect_size; /* We want it in bytes now */
+ startblock += mydata->fat_sect; /* Offset from start of disk */
+
+ /* Write back the fatbuf to the disk */
+ if (mydata->fatbufnum != -1) {
+ if (flush_fat_buffer(mydata) < 0)
+ return -1;
+ }
+
+ if (disk_read(startblock, getsize, bufptr) < 0) {
+ debug("Error reading FAT blocks\n");
+ return ret;
+ }
+ mydata->fatbufnum = bufnum;
+ }
+
+ /* Get the actual entry from the table */
+ switch (mydata->fatsize) {
+ case 32:
+ ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
+ break;
+ case 16:
+ ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
+ break;
+ case 12:
+ off16 = (offset * 3) / 4;
+
+ switch (offset & 0x3) {
+ case 0:
+ ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
+ ret &= 0xfff;
+ break;
+ case 1:
+ val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
+ val1 &= 0xf000;
+ val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
+ val2 &= 0x00ff;
+ ret = (val2 << 4) | (val1 >> 12);
+ break;
+ case 2:
+ val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
+ val1 &= 0xff00;
+ val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
+ val2 &= 0x000f;
+ ret = (val2 << 8) | (val1 >> 8);
+ break;
+ case 3:
+ ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
+ ret = (ret & 0xfff0) >> 4;
+ break;
+ default:
+ break;
+ }
+ break;
+ }
+ debug("FAT%d: ret: %08x, entry: %08x, offset: %04x\n",
+ mydata->fatsize, ret, entry, offset);
+
+ return ret;
+}
+
+/*
+ * Set the file name information from 'name' into 'slotptr',
+ */
+static int str2slot(dir_slot *slotptr, const char *name, int *idx)
+{
+ int j, end_idx = 0;
+
+ for (j = 0; j <= 8; j += 2) {
+ if (name[*idx] == 0x00) {
+ slotptr->name0_4[j] = 0;
+ slotptr->name0_4[j + 1] = 0;
+ end_idx++;
+ goto name0_4;
+ }
+ slotptr->name0_4[j] = name[*idx];
+ (*idx)++;
+ end_idx++;
+ }
+ for (j = 0; j <= 10; j += 2) {
+ if (name[*idx] == 0x00) {
+ slotptr->name5_10[j] = 0;
+ slotptr->name5_10[j + 1] = 0;
+ end_idx++;
+ goto name5_10;
+ }
+ slotptr->name5_10[j] = name[*idx];
+ (*idx)++;
+ end_idx++;
+ }
+ for (j = 0; j <= 2; j += 2) {
+ if (name[*idx] == 0x00) {
+ slotptr->name11_12[j] = 0;
+ slotptr->name11_12[j + 1] = 0;
+ end_idx++;
+ goto name11_12;
+ }
+ slotptr->name11_12[j] = name[*idx];
+ (*idx)++;
+ end_idx++;
+ }
+
+ if (name[*idx] == 0x00)
+ return 1;
+
+ return 0;
+/* Not used characters are filled with 0xff 0xff */
+name0_4:
+ for (; end_idx < 5; end_idx++) {
+ slotptr->name0_4[end_idx * 2] = 0xff;
+ slotptr->name0_4[end_idx * 2 + 1] = 0xff;
+ }
+ end_idx = 5;
+name5_10:
+ end_idx -= 5;
+ for (; end_idx < 6; end_idx++) {
+ slotptr->name5_10[end_idx * 2] = 0xff;
+ slotptr->name5_10[end_idx * 2 + 1] = 0xff;
+ }
+ end_idx = 11;
+name11_12:
+ end_idx -= 11;
+ for (; end_idx < 2; end_idx++) {
+ slotptr->name11_12[end_idx * 2] = 0xff;
+ slotptr->name11_12[end_idx * 2 + 1] = 0xff;
+ }
+
+ return 1;
+}
+
+static int is_next_clust(fsdata *mydata, dir_entry *dentptr);
+static void flush_dir_table(fsdata *mydata, dir_entry **dentptr);
+
+/*
+ * Fill dir_slot entries with appropriate name, id, and attr
+ * The real directory entry is returned by 'dentptr'
+ */
+static void
+fill_dir_slot(fsdata *mydata, dir_entry **dentptr, const char *l_name)
+{
+ dir_slot *slotptr = (dir_slot *)get_contents_vfatname_block;
+ __u8 counter = 0, checksum;
+ int idx = 0, ret;
+ char s_name[16];
+
+ /* Get short file name and checksum value */
+ strncpy(s_name, (*dentptr)->name, 16);
+ checksum = mkcksum((*dentptr)->name, (*dentptr)->ext);
+
+ do {
+ memset(slotptr, 0x00, sizeof(dir_slot));
+ ret = str2slot(slotptr, l_name, &idx);
+ slotptr->id = ++counter;
+ slotptr->attr = ATTR_VFAT;
+ slotptr->alias_checksum = checksum;
+ slotptr++;
+ } while (ret == 0);
+
+ slotptr--;
+ slotptr->id |= LAST_LONG_ENTRY_MASK;
+
+ while (counter >= 1) {
+ if (is_next_clust(mydata, *dentptr)) {
+ /* A new cluster is allocated for directory table */
+ flush_dir_table(mydata, dentptr);
+ }
+ memcpy(*dentptr, slotptr, sizeof(dir_slot));
+ (*dentptr)++;
+ slotptr--;
+ counter--;
+ }
+
+ if (is_next_clust(mydata, *dentptr)) {
+ /* A new cluster is allocated for directory table */
+ flush_dir_table(mydata, dentptr);
+ }
+}
+
+static __u32 dir_curclust;
+
+/*
+ * Extract the full long filename starting at 'retdent' (which is really
+ * a slot) into 'l_name'. If successful also copy the real directory entry
+ * into 'retdent'
+ * If additional adjacent cluster for directory entries is read into memory,
+ * then 'get_contents_vfatname_block' is copied into 'get_dentfromdir_block' and
+ * the location of the real directory entry is returned by 'retdent'
+ * Return 0 on success, -1 otherwise.
+ */
+static int
+get_long_file_name(fsdata *mydata, int curclust, __u8 *cluster,
+ dir_entry **retdent, char *l_name)
+{
+ dir_entry *realdent;
+ dir_slot *slotptr = (dir_slot *)(*retdent);
+ dir_slot *slotptr2 = NULL;
+ __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
+ PREFETCH_BLOCKS :
+ mydata->clust_size);
+ __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
+ int idx = 0, cur_position = 0;
+
+ if (counter > VFAT_MAXSEQ) {
+ debug("Error: VFAT name is too long\n");
+ return -1;
+ }
+
+ while ((__u8 *)slotptr < buflimit) {
+ if (counter == 0)
+ break;
+ if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
+ return -1;
+ slotptr++;
+ counter--;
+ }
+
+ if ((__u8 *)slotptr >= buflimit) {
+ if (curclust == 0)
+ return -1;
+ curclust = get_fatent_value(mydata, dir_curclust);
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
+ debug("curclust: 0x%x\n", curclust);
+ printf("Invalid FAT entry\n");
+ return -1;
+ }
+
+ dir_curclust = curclust;
+
+ if (get_cluster(mydata, curclust, get_contents_vfatname_block,
+ mydata->clust_size * mydata->sect_size) != 0) {
+ debug("Error: reading directory block\n");
+ return -1;
+ }
+
+ slotptr2 = (dir_slot *)get_contents_vfatname_block;
+ while (counter > 0) {
+ if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
+ & 0xff) != counter)
+ return -1;
+ slotptr2++;
+ counter--;
+ }
+
+ /* Save the real directory entry */
+ realdent = (dir_entry *)slotptr2;
+ while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
+ slotptr2--;
+ slot2str(slotptr2, l_name, &idx);
+ }
+ } else {
+ /* Save the real directory entry */
+ realdent = (dir_entry *)slotptr;
+ }
+
+ do {
+ slotptr--;
+ if (slot2str(slotptr, l_name, &idx))
+ break;
+ } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
+
+ l_name[idx] = '\0';
+ if (*l_name == DELETED_FLAG)
+ *l_name = '\0';
+ else if (*l_name == aRING)
+ *l_name = DELETED_FLAG;
+ downcase(l_name);
+
+ /* Return the real directory entry */
+ *retdent = realdent;
+
+ if (slotptr2) {
+ memcpy(get_dentfromdir_block, get_contents_vfatname_block,
+ mydata->clust_size * mydata->sect_size);
+ cur_position = (__u8 *)realdent - get_contents_vfatname_block;
+ *retdent = (dir_entry *) &get_dentfromdir_block[cur_position];
+ }
+
+ return 0;
+}
+
+/*
+ * Set the entry at index 'entry' in a FAT (16/32) table.
+ */
+static int set_fatent_value(fsdata *mydata, __u32 entry, __u32 entry_value)
+{
+ __u32 bufnum, offset;
+
+ switch (mydata->fatsize) {
+ case 32:
+ bufnum = entry / FAT32BUFSIZE;
+ offset = entry - bufnum * FAT32BUFSIZE;
+ break;
+ case 16:
+ bufnum = entry / FAT16BUFSIZE;
+ offset = entry - bufnum * FAT16BUFSIZE;
+ break;
+ default:
+ /* Unsupported FAT size */
+ return -1;
+ }
+
+ /* Read a new block of FAT entries into the cache. */
+ if (bufnum != mydata->fatbufnum) {
+ int getsize = FATBUFBLOCKS;
+ __u8 *bufptr = mydata->fatbuf;
+ __u32 fatlength = mydata->fatlength;
+ __u32 startblock = bufnum * FATBUFBLOCKS;
+
+ fatlength *= mydata->sect_size;
+ startblock += mydata->fat_sect;
+
+ if (getsize > fatlength)
+ getsize = fatlength;
+
+ if (mydata->fatbufnum != -1) {
+ if (flush_fat_buffer(mydata) < 0)
+ return -1;
+ }
+
+ if (disk_read(startblock, getsize, bufptr) < 0) {
+ debug("Error reading FAT blocks\n");
+ return -1;
+ }
+ mydata->fatbufnum = bufnum;
+ }
+
+ /* Set the actual entry */
+ switch (mydata->fatsize) {
+ case 32:
+ ((__u32 *) mydata->fatbuf)[offset] = cpu_to_le32(entry_value);
+ break;
+ case 16:
+ ((__u16 *) mydata->fatbuf)[offset] = cpu_to_le16(entry_value);
+ break;
+ default:
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Determine the entry value at index 'entry' in a FAT (16/32) table
+ */
+static __u32 determine_fatent(fsdata *mydata, __u32 entry)
+{
+ __u32 next_fat, next_entry = entry + 1;
+
+ while (1) {
+ next_fat = get_fatent_value(mydata, next_entry);
+ if (next_fat == 0) {
+ set_fatent_value(mydata, entry, next_entry);
+ break;
+ }
+ next_entry++;
+ }
+ debug("FAT%d: entry: %08x, entry_value: %04x\n",
+ mydata->fatsize, entry, next_entry);
+
+ return next_entry;
+}
+
+/*
+ * Write at most 'size' bytes from 'buffer' into the specified cluster.
+ * Return 0 on success, -1 otherwise.
+ */
+static int
+set_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer,
+ unsigned long size)
+{
+ int idx = 0;
+ __u32 startsect;
+
+ if (clustnum > 0)
+ startsect = mydata->data_begin +
+ clustnum * mydata->clust_size;
+ else
+ startsect = mydata->rootdir_sect;
+
+ debug("clustnum: %d, startsect: %d\n", clustnum, startsect);
+
+ if ((size / mydata->sect_size) > 0) {
+ if (disk_write(startsect, size / mydata->sect_size, buffer) < 0) {
+ debug("Error writing data\n");
+ return -1;
+ }
+ }
+
+ if (size % mydata->sect_size) {
+ __u8 tmpbuf[mydata->sect_size];
+
+ idx = size / mydata->sect_size;
+ buffer += idx * mydata->sect_size;
+ memcpy(tmpbuf, buffer, size % mydata->sect_size);
+
+ if (disk_write(startsect + idx, 1, tmpbuf) < 0) {
+ debug("Error writing data\n");
+ return -1;
+ }
+
+ return 0;
+ }
+
+ return 0;
+}
+
+/*
+ * Find the first empty cluster
+ */
+static int find_empty_cluster(fsdata *mydata)
+{
+ __u32 fat_val, entry = 3;
+
+ while (1) {
+ fat_val = get_fatent_value(mydata, entry);
+ if (fat_val == 0)
+ break;
+ entry++;
+ }
+
+ return entry;
+}
+
+/*
+ * Write directory entries in 'get_dentfromdir_block' to block device
+ */
+static void flush_dir_table(fsdata *mydata, dir_entry **dentptr)
+{
+ int dir_newclust = 0;
+
+ if (set_cluster(mydata, dir_curclust,
+ get_dentfromdir_block,
+ mydata->clust_size * mydata->sect_size) != 0) {
+ printf("error: wrinting directory entry\n");
+ return;
+ }
+ dir_newclust = find_empty_cluster(mydata);
+ set_fatent_value(mydata, dir_curclust, dir_newclust);
+ if (mydata->fatsize == 32)
+ set_fatent_value(mydata, dir_newclust, 0xffffff8);
+ else if (mydata->fatsize == 16)
+ set_fatent_value(mydata, dir_newclust, 0xfff8);
+
+ dir_curclust = dir_newclust;
+
+ if (flush_fat_buffer(mydata) < 0)
+ return;
+
+ memset(get_dentfromdir_block, 0x00,
+ mydata->clust_size * mydata->sect_size);
+
+ *dentptr = (dir_entry *) get_dentfromdir_block;
+}
+
+/*
+ * Set empty cluster from 'entry' to the end of a file
+ */
+static int clear_fatent(fsdata *mydata, __u32 entry)
+{
+ __u32 fat_val;
+
+ while (1) {
+ fat_val = get_fatent_value(mydata, entry);
+ if (fat_val != 0)
+ set_fatent_value(mydata, entry, 0);
+ else
+ break;
+
+ if (fat_val == 0xfffffff || fat_val == 0xffff)
+ break;
+
+ entry = fat_val;
+ }
+
+ /* Flush fat buffer */
+ if (flush_fat_buffer(mydata) < 0)
+ return -1;
+
+ return 0;
+}
+
+/*
+ * Write at most 'maxsize' bytes from 'buffer' into
+ * the file associated with 'dentptr'
+ * Return the number of bytes read or -1 on fatal errors.
+ */
+static int
+set_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
+ unsigned long maxsize)
+{
+ unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
+ unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
+ __u32 curclust = START(dentptr);
+ __u32 endclust = 0, newclust = 0;
+ unsigned long actsize;
+
+ debug("Filesize: %ld bytes\n", filesize);
+
+ if (maxsize > 0 && filesize > maxsize)
+ filesize = maxsize;
+
+ debug("%ld bytes\n", filesize);
+
+ actsize = bytesperclust;
+ endclust = curclust;
+ do {
+ /* search for consecutive clusters */
+ while (actsize < filesize) {
+ newclust = determine_fatent(mydata, endclust);
+
+ if ((newclust - 1) != endclust)
+ goto getit;
+
+ if (CHECK_CLUST(newclust, mydata->fatsize)) {
+ debug("curclust: 0x%x\n", newclust);
+ debug("Invalid FAT entry\n");
+ return gotsize;
+ }
+ endclust = newclust;
+ actsize += bytesperclust;
+ }
+ /* actsize >= file size */
+ actsize -= bytesperclust;
+ /* set remaining clusters */
+ if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
+ debug("error: writing cluster\n");
+ return -1;
+ }
+
+ /* set remaining bytes */
+ gotsize += (int)actsize;
+ filesize -= actsize;
+ buffer += actsize;
+ actsize = filesize;
+
+ if (set_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
+ debug("error: writing cluster\n");
+ return -1;
+ }
+ gotsize += actsize;
+
+ /* Mark end of file in FAT */
+ if (mydata->fatsize == 16)
+ newclust = 0xffff;
+ else if (mydata->fatsize == 32)
+ newclust = 0xfffffff;
+ set_fatent_value(mydata, endclust, newclust);
+
+ return gotsize;
+getit:
+ if (set_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
+ debug("error: writing cluster\n");
+ return -1;
+ }
+ gotsize += (int)actsize;
+ filesize -= actsize;
+ buffer += actsize;
+
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
+ debug("curclust: 0x%x\n", curclust);
+ debug("Invalid FAT entry\n");
+ return gotsize;
+ }
+ actsize = bytesperclust;
+ curclust = endclust = newclust;
+ } while (1);
+}
+
+/*
+ * Fill dir_entry
+ */
+static void fill_dentry(fsdata *mydata, dir_entry *dentptr,
+ const char *filename, __u32 start_cluster, __u32 size, __u8 attr)
+{
+ if (mydata->fatsize == 32)
+ dentptr->starthi =
+ cpu_to_le16((start_cluster & 0xffff0000) >> 16);
+ dentptr->start = cpu_to_le16(start_cluster & 0xffff);
+ dentptr->size = cpu_to_le32(size);
+
+ dentptr->attr = attr;
+
+ set_name(dentptr, filename);
+}
+
+/*
+ * Check whether adding a file makes the file system to
+ * exceed the size of the block device
+ * Return -1 when overflow occurs, otherwise return 0
+ */
+static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
+{
+ __u32 startsect, sect_num;
+
+ if (clustnum > 0) {
+ startsect = mydata->data_begin +
+ clustnum * mydata->clust_size;
+ } else {
+ startsect = mydata->rootdir_sect;
+ }
+
+ sect_num = size / mydata->sect_size;
+ if (size % mydata->sect_size)
+ sect_num++;
+
+ if (startsect + sect_num > cur_part_info.start + total_sector)
+ return -1;
+
+ return 0;
+}
+
+/*
+ * Check if adding several entries exceed one cluster boundary
+ */
+static int is_next_clust(fsdata *mydata, dir_entry *dentptr)
+{
+ int cur_position;
+
+ cur_position = (__u8 *)dentptr - get_dentfromdir_block;
+
+ if (cur_position >= mydata->clust_size * mydata->sect_size)
+ return 1;
+ else
+ return 0;
+}
+
+static dir_entry *empty_dentptr;
+/*
+ * Find a directory entry based on filename or start cluster number
+ * If the directory entry is not found,
+ * the new position for writing a directory entry will be returned
+ */
+static dir_entry *find_directory_entry(fsdata *mydata, int startsect,
+ char *filename, dir_entry *retdent, __u32 start)
+{
+ __u32 curclust = (startsect - mydata->data_begin) / mydata->clust_size;
+
+ debug("get_dentfromdir: %s\n", filename);
+
+ while (1) {
+ dir_entry *dentptr;
+
+ int i;
+
+ if (get_cluster(mydata, curclust, get_dentfromdir_block,
+ mydata->clust_size * mydata->sect_size) != 0) {
+ printf("Error: reading directory block\n");
+ return NULL;
+ }
+
+ dentptr = (dir_entry *)get_dentfromdir_block;
+
+ dir_curclust = curclust;
+
+ for (i = 0; i < DIRENTSPERCLUST; i++) {
+ char s_name[14], l_name[VFAT_MAXLEN_BYTES];
+
+ l_name[0] = '\0';
+ if (dentptr->name[0] == DELETED_FLAG) {
+ dentptr++;
+ if (is_next_clust(mydata, dentptr))
+ break;
+ continue;
+ }
+ if ((dentptr->attr & ATTR_VOLUME)) {
+ if (vfat_enabled &&
+ (dentptr->attr & ATTR_VFAT) &&
+ (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
+ get_long_file_name(mydata, curclust,
+ get_dentfromdir_block,
+ &dentptr, l_name);
+ debug("vfatname: |%s|\n", l_name);
+ } else {
+ /* Volume label or VFAT entry */
+ dentptr++;
+ if (is_next_clust(mydata, dentptr))
+ break;
+ continue;
+ }
+ }
+ if (dentptr->name[0] == 0) {
+ debug("Dentname == NULL - %d\n", i);
+ empty_dentptr = dentptr;
+ return NULL;
+ }
+
+ get_name(dentptr, s_name);
+
+ if (strcmp(filename, s_name)
+ && strcmp(filename, l_name)) {
+ debug("Mismatch: |%s|%s|\n",
+ s_name, l_name);
+ dentptr++;
+ if (is_next_clust(mydata, dentptr))
+ break;
+ continue;
+ }
+
+ memcpy(retdent, dentptr, sizeof(dir_entry));
+
+ debug("DentName: %s", s_name);
+ debug(", start: 0x%x", START(dentptr));
+ debug(", size: 0x%x %s\n",
+ FAT2CPU32(dentptr->size),
+ (dentptr->attr & ATTR_DIR) ?
+ "(DIR)" : "");
+
+ return dentptr;
+ }
+
+ curclust = get_fatent_value(mydata, dir_curclust);
+ if ((curclust >= 0xffffff8) || (curclust >= 0xfff8)) {
+ empty_dentptr = dentptr;
+ return NULL;
+ }
+ if (CHECK_CLUST(curclust, mydata->fatsize)) {
+ debug("curclust: 0x%x\n", curclust);
+ debug("Invalid FAT entry\n");
+ return NULL;
+ }
+ }
+
+ return NULL;
+}
+
+static int do_fat_write(const char *filename, void *buffer,
+ unsigned long size)
+{
+ dir_entry *dentptr, *retdent;
+ __u32 startsect;
+ __u32 start_cluster;
+ boot_sector bs;
+ volume_info volinfo;
+ fsdata datablock;
+ fsdata *mydata = &datablock;
+ int cursect;
+ int ret = -1, name_len;
+ char l_filename[VFAT_MAXLEN_BYTES];
+ int write_size = size;
+
+ dir_curclust = 0;
+
+ if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
+ debug("error: reading boot sector\n");
+ return -1;
+ }
+
+ total_sector = bs.total_sect;
+ if (total_sector == 0)
+ total_sector = cur_part_info.size;
+
+ if (mydata->fatsize == 32)
+ mydata->fatlength = bs.fat32_length;
+ else
+ mydata->fatlength = bs.fat_length;
+
+ mydata->fat_sect = bs.reserved;
+
+ cursect = mydata->rootdir_sect
+ = mydata->fat_sect + mydata->fatlength * bs.fats;
+ num_of_fats = bs.fats;
+
+ mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
+ mydata->clust_size = bs.cluster_size;
+
+ if (mydata->fatsize == 32) {
+ mydata->data_begin = mydata->rootdir_sect -
+ (mydata->clust_size * 2);
+ } else {
+ int rootdir_size;
+
+ rootdir_size = ((bs.dir_entries[1] * (int)256 +
+ bs.dir_entries[0]) *
+ sizeof(dir_entry)) /
+ mydata->sect_size;
+ mydata->data_begin = mydata->rootdir_sect +
+ rootdir_size -
+ (mydata->clust_size * 2);
+ }
+
+ mydata->fatbufnum = -1;
+ mydata->fatbuf = malloc(FATBUFSIZE);
+ if (mydata->fatbuf == NULL) {
+ debug("Error: allocating memory\n");
+ return -1;
+ }
+
+ if (disk_read(cursect,
+ (mydata->fatsize == 32) ?
+ (mydata->clust_size) :
+ PREFETCH_BLOCKS, do_fat_read_at_block) < 0) {
+ debug("Error: reading rootdir block\n");
+ goto exit;
+ }
+ dentptr = (dir_entry *) do_fat_read_at_block;
+
+ name_len = strlen(filename);
+ if (name_len >= VFAT_MAXLEN_BYTES)
+ name_len = VFAT_MAXLEN_BYTES - 1;
+
+ memcpy(l_filename, filename, name_len);
+ l_filename[name_len] = 0; /* terminate the string */
+ downcase(l_filename);
+
+ startsect = mydata->rootdir_sect;
+ retdent = find_directory_entry(mydata, startsect,
+ l_filename, dentptr, 0);
+ if (retdent) {
+ /* Update file size and start_cluster in a directory entry */
+ retdent->size = cpu_to_le32(size);
+ start_cluster = FAT2CPU16(retdent->start);
+ if (mydata->fatsize == 32)
+ start_cluster |=
+ (FAT2CPU16(retdent->starthi) << 16);
+
+ ret = check_overflow(mydata, start_cluster, size);
+ if (ret) {
+ printf("Error: %ld overflow\n", size);
+ goto exit;
+ }
+
+ ret = clear_fatent(mydata, start_cluster);
+ if (ret) {
+ printf("Error: clearing FAT entries\n");
+ goto exit;
+ }
+
+ ret = set_contents(mydata, retdent, buffer, size);
+ if (ret < 0) {
+ printf("Error: writing contents\n");
+ goto exit;
+ }
+ write_size = ret;
+ debug("attempt to write 0x%x bytes\n", write_size);
+
+ /* Flush fat buffer */
+ ret = flush_fat_buffer(mydata);
+ if (ret) {
+ printf("Error: flush fat buffer\n");
+ goto exit;
+ }
+
+ /* Write directory table to device */
+ ret = set_cluster(mydata, dir_curclust,
+ get_dentfromdir_block,
+ mydata->clust_size * mydata->sect_size);
+ if (ret) {
+ printf("Error: writing directory entry\n");
+ goto exit;
+ }
+ } else {
+ /* Set short name to set alias checksum field in dir_slot */
+ set_name(empty_dentptr, filename);
+ fill_dir_slot(mydata, &empty_dentptr, filename);
+
+ ret = start_cluster = find_empty_cluster(mydata);
+ if (ret < 0) {
+ printf("Error: finding empty cluster\n");
+ goto exit;
+ }
+
+ ret = check_overflow(mydata, start_cluster, size);
+ if (ret) {
+ printf("Error: %ld overflow\n", size);
+ goto exit;
+ }
+
+ /* Set attribute as archieve for regular file */
+ fill_dentry(mydata, empty_dentptr, filename,
+ start_cluster, size, 0x20);
+
+ ret = set_contents(mydata, empty_dentptr, buffer, size);
+ if (ret < 0) {
+ printf("Error: writing contents\n");
+ goto exit;
+ }
+ write_size = ret;
+ debug("attempt to write 0x%x bytes\n", write_size);
+
+ /* Flush fat buffer */
+ ret = flush_fat_buffer(mydata);
+ if (ret) {
+ printf("Error: flush fat buffer\n");
+ goto exit;
+ }
+
+ /* Write directory table to device */
+ ret = set_cluster(mydata, dir_curclust,
+ get_dentfromdir_block,
+ mydata->clust_size * mydata->sect_size);
+ if (ret) {
+ printf("Error: writing directory entry\n");
+ goto exit;
+ }
+ }
+
+exit:
+ free(mydata->fatbuf);
+ return ret < 0 ? ret : write_size;
+}
+
+int file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
+{
+ printf("writing %s\n", filename);
+ return do_fat_write(filename, buffer, maxsize);
+}
diff --git a/fs/fat/file.c b/fs/fat/file.c
index 59c5d37cdc..d910c46ddb 100644
--- a/fs/fat/file.c
+++ b/fs/fat/file.c
@@ -6,23 +6,7 @@
* 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
* 2003-03-10 - kharris@nexus-tech.net - ported to uboot
*
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
diff --git a/include/configs/smdk4412.h b/include/configs/smdk4412.h
index d849b9a4e3..edd729e565 100644
--- a/include/configs/smdk4412.h
+++ b/include/configs/smdk4412.h
@@ -217,7 +217,9 @@
#define CONFIG_FS_EXT4
#define CONFIG_CMD_EXT4
+#define CONFIG_FS_FAT
#define CONFIG_CMD_FAT
+#define CONFIG_CMD_FS_GENERIC
#define CONFIG_SYS_NAND_QUIET_TEST
#define CONFIG_SYS_ONENAND_QUIET_TEST
diff --git a/include/fat.h b/include/fat.h
index 7cab14b9c6..2c951e7d79 100644
--- a/include/fat.h
+++ b/include/fat.h
@@ -4,24 +4,7 @@
* 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
* 2003-03-10 - kharris@nexus-tech.net - ported to u-boot
*
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
- *
+ * SPDX-License-Identifier: GPL-2.0+
*/
#ifndef _FAT_H_
@@ -33,22 +16,15 @@
/* Maximum Long File Name length supported here is 128 UTF-16 code units */
#define VFAT_MAXLEN_BYTES 256 /* Maximum LFN buffer in bytes */
#define VFAT_MAXSEQ 9 /* Up to 9 of 13 2-byte UTF-16 entries */
-#define LINEAR_PREFETCH_SIZE (SECTOR_SIZE*2) /* Prefetch buffer size */
-
-#define SECTOR_SIZE FS_BLOCK_SIZE
-
-#define FS_BLOCK_SIZE 512
-
-#if FS_BLOCK_SIZE != SECTOR_SIZE
-#error FS_BLOCK_SIZE != SECTOR_SIZE - This code needs to be fixed!
-#endif
+#define PREFETCH_BLOCKS 2
#define MAX_CLUSTSIZE 65536
-#define DIRENTSPERBLOCK (FS_BLOCK_SIZE/sizeof(dir_entry))
-#define DIRENTSPERCLUST ((mydata->clust_size*SECTOR_SIZE)/sizeof(dir_entry))
+#define DIRENTSPERBLOCK (mydata->sect_size / sizeof(dir_entry))
+#define DIRENTSPERCLUST ((mydata->clust_size * mydata->sect_size) / \
+ sizeof(dir_entry))
#define FATBUFBLOCKS 6
-#define FATBUFSIZE (FS_BLOCK_SIZE*FATBUFBLOCKS)
+#define FATBUFSIZE (mydata->sect_size * FATBUFBLOCKS)
#define FAT12BUFSIZE ((FATBUFSIZE*2)/3)
#define FAT16BUFSIZE (FATBUFSIZE/2)
#define FAT32BUFSIZE (FATBUFSIZE/4)
@@ -71,7 +47,7 @@
#define ATTR_VFAT (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
#define DELETED_FLAG ((char)0xe5) /* Marks deleted files when in name[0] */
-#define aRING 0x05 /* Used to represent '? in name[0] */
+#define aRING 0x05 /* Used as special character in name[0] */
/*
* Indicates that the entry is the last long entry in a set of long
@@ -105,7 +81,6 @@
#endif
#endif
-#define TOLOWER(c) if((c) >= 'A' && (c) <= 'Z'){(c)+=('a' - 'A');}
#define START(dent) (FAT2CPU16((dent)->start) \
+ (mydata->fatsize != 32 ? 0 : \
(FAT2CPU16((dent)->starthi) << 16)))
@@ -181,17 +156,18 @@ typedef struct dir_slot {
* (see FAT32 accesses)
*/
typedef struct {
- __u8 fatbuf[FATBUFSIZE]; /* Current FAT buffer */
+ __u8 *fatbuf; /* Current FAT buffer */
int fatsize; /* Size of FAT in bits */
- __u16 fatlength; /* Length of FAT in sectors */
+ __u32 fatlength; /* Length of FAT in sectors */
__u16 fat_sect; /* Starting sector of the FAT */
- __u16 rootdir_sect; /* Start sector of root directory */
+ __u32 rootdir_sect; /* Start sector of root directory */
+ __u16 sect_size; /* Size of sectors in bytes */
__u16 clust_size; /* Size of clusters in sectors */
- short data_begin; /* The sector of the first cluster, can be negative */
+ int data_begin; /* The sector of the first cluster, can be negative */
int fatbufnum; /* Used by get_fatent, init to -1 */
} fsdata;
-typedef int (file_detectfs_func)();
+typedef int (file_detectfs_func)(void);
typedef int (file_ls_func)(const char *dir);
typedef long (file_read_func)(const char *filename, void *buffer,
unsigned long maxsize);
@@ -203,102 +179,6 @@ struct filesystem {
const char name[12];
};
-/* FAT Boot Recode */
-#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
- (fat) == 16 ? MINCLS16 : \
- MINCLS32)
-
-#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
- (fat) == 16 ? MAXCLS16 : \
- MAXCLS32)
-
-#define mk1(p, x) \
- (p) = (__u8)(x)
-
-#define mk2(p, x) \
- (p)[0] = (__u8)(x), \
- (p)[1] = (__u8)((x) >> 010)
-
-#define mk4(p, x) \
- (p)[0] = (__u8)(x), \
- (p)[1] = (__u8)((x) >> 010), \
- (p)[2] = (__u8)((x) >> 020), \
- (p)[3] = (__u8)((x) >> 030)
-
-#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
-#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
-#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
-#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
-
-struct bs {
- __u8 jmp[3]; /* bootstrap entry point */
- __u8 oem[9]; /* OEM name and version */
-};
-
-struct bsbpb {
- __u8 bps[2]; /* bytes per sector */
- __u8 spc; /* sectors per cluster */
- __u8 res[2]; /* reserved sectors */
- __u8 nft; /* number of FATs */
- __u8 rde[2]; /* root directory entries */
- __u8 sec[2]; /* total sectors */
- __u8 mid; /* media descriptor */
- __u8 spf[2]; /* sectors per FAT */
- __u8 spt[2]; /* sectors per track */
- __u8 hds[2]; /* drive heads */
- __u8 hid[4]; /* hidden sectors */
- __u8 bsec[6]; /* big total sectors */
-};
-
-/* For FAT32 */
-struct bsxbpb {
- __u8 bspf[4]; /* big sectors per FAT */
- __u8 xflg[2]; /* FAT control flags */
- __u8 vers[2]; /* file system version */
- __u8 rdcl[4]; /* root directory start cluster */
- __u8 infs[2]; /* file system info sector */
- __u8 bkbs[2]; /* backup boot sector */
- __u8 rsvd[12]; /* reserved */
-};
-
-struct bsx {
- __u8 drv; /* drive number */
- __u8 rsvd; /* reserved */
- __u8 sig; /* extended boot signature */
- __u8 volid[4]; /* volume ID number */
- __u8 label[11]; /* volume label */
- __u8 type[8]; /* file system type */
-};
-
-struct de {
- __u8 namext[11]; /* name and extension */
- __u8 attr; /* attributes */
- __u8 rsvd[10]; /* reserved */
- __u8 time[2]; /* creation time */
- __u8 date[2]; /* creation date */
- __u8 clus[2]; /* starting cluster */
- __u8 size[4]; /* size */
-};
-
-struct bpb {
- __u32 bps; /* bytes per sector */
- __u32 spc; /* sectors per cluster */
- __u32 res; /* reserved sectors */
- __u32 nft; /* number of FATs */
- __u32 rde; /* root directory entries */
- __u32 sec; /* total sectors */
- __u32 mid; /* media descriptor */
- __u32 spf; /* sectors per FAT */
- __u32 spt; /* sectors per track */
- __u32 hds; /* drive heads */
- __u32 hid; /* hidden sectors */
- __u32 bsec; /* big total sectors */
- __u32 bspf; /* big sectors per FAT */
- __u32 rdcl; /* root directory start cluster */
- __u32 infs; /* file system info sector */
- __u32 bkbs; /* backup boot sector */
-};
-
/* FAT tables */
file_detectfs_func file_fat_detectfs;
file_ls_func file_fat_ls;
@@ -308,9 +188,14 @@ file_read_func file_fat_read;
int file_cd(const char *path);
int file_fat_detectfs(void);
int file_fat_ls(const char *dir);
+long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
+ unsigned long maxsize);
long file_fat_read(const char *filename, void *buffer, unsigned long maxsize);
const char *file_getfsname(int idx);
+int fat_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
int fat_register_device(block_dev_desc_t *dev_desc, int part_no);
-int fat_format_device(block_dev_desc_t *dev_desc, int part_no);
+int file_fat_write(const char *filename, void *buffer, unsigned long maxsize);
+int fat_read_file(const char *filename, void *buf, int offset, int len);
+void fat_close(void);
#endif /* _FAT_H_ */