[U-Boot] [PATCH v4] cmd_fat: add FAT write command

Donggeun Kim dg77.kim at samsung.com
Fri Jan 6 03:19:54 CET 2012


Once CONFIG_FAT_WRITE is defined,
users can invoke 'fatwrite' command that saves data in RAM as a FAT file.

This patch also removes compile error after patch of
'fs/fat: Fix FAT detection to support non-DOS partition tables'.

Signed-off-by: Donggeun Kim <dg77.kim at samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park at samsung.com>
---
Changes for v4:
  - fix compile error in fs/fat_write.c file
Changes for v3:
  - use cmd_usage function
Changes for v2:
  - merge the patch that fixes compile error when enabling FAT write
  - change do_fat_fswrite to be static function

 common/cmd_fat.c   |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fat/fat_write.c |   20 +++++++-----------
 2 files changed, 65 insertions(+), 12 deletions(-)

diff --git a/common/cmd_fat.c b/common/cmd_fat.c
index 0220494..fef1c82 100644
--- a/common/cmd_fat.c
+++ b/common/cmd_fat.c
@@ -184,3 +184,60 @@ U_BOOT_CMD(
 	"<interface> <dev[:part]>\n"
 	"    - print information about filesystem from 'dev' on 'interface'"
 );
+
+#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;
+	int dev = 0;
+	int part = 1;
+	char *ep;
+
+	if (argc < 5)
+		return cmd_usage(cmdtp);
+
+	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 fatwrite **\n",
+			argv[1], dev, part);
+		return 1;
+	}
+	addr = simple_strtoul(argv[3], NULL, 16);
+	count = simple_strtoul(argv[5], NULL, 16);
+
+	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 write\n", size);
+
+	return 0;
+}
+
+U_BOOT_CMD(
+	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'"
+);
+#endif
diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
index 3bfc1c4..16f8400 100644
--- a/fs/fat/fat_write.c
+++ b/fs/fat/fat_write.c
@@ -41,23 +41,19 @@ static void uppercase(char *str, int len)
 }
 
 static int total_sector;
-static int disk_write(__u32 startblock, __u32 getsize, __u8 *bufptr)
+static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
 {
-	if (cur_dev == NULL)
+	if (!cur_dev || !cur_dev->block_write)
 		return -1;
 
-	if (startblock + getsize > total_sector) {
+	if (cur_part_info.start + block + nr_blocks >
+		cur_part_info.start + total_sector) {
 		printf("error: overflow occurs\n");
 		return -1;
 	}
 
-	startblock += part_offset;
-
-	if (cur_dev->block_read) {
-		return cur_dev->block_write(cur_dev->dev, startblock, getsize,
-					   (unsigned long *) bufptr);
-	}
-	return -1;
+	return cur_dev->block_write(cur_dev->dev,
+			cur_part_info.start + block, nr_blocks,	buf);
 }
 
 /*
@@ -797,7 +793,7 @@ static int check_overflow(fsdata *mydata, __u32 clustnum, unsigned long size)
 	if (size % mydata->sect_size)
 		sect_num++;
 
-	if (startsect + sect_num > total_sector)
+	if (startsect + sect_num > cur_part_info.start + total_sector)
 		return -1;
 
 	return 0;
@@ -947,7 +943,7 @@ static int do_fat_write(const char *filename, void *buffer,
 
 	total_sector = bs.total_sect;
 	if (total_sector == 0)
-		total_sector = part_size;
+		total_sector = cur_part_info.size;
 
 	root_cluster = bs.root_cluster;
 
-- 
1.7.4.1



More information about the U-Boot mailing list