[U-Boot] [PATCH 2/2] fat: New abstract function (do_fat_safe) for FAT subsystem

Lukasz Majewski l.majewski at samsung.com
Fri Jul 27 10:35:16 CEST 2012


The new do_fat_safe function has been added to the FAT handling command.
It is necessary to decouple the core fat command read/write operation from
code responsible for parsing user input.

The do_fat_safe function shall be used from other subsystems willing to
read/write data to FAT file system.

Due to code reorganization a common code has been removed from env_fat.c.

Signed-off-by: Lukasz Majewski <l.majewski at samsung.com>
Signed-off-by: Kyungmin Park <kyungmin.park at samsung.com>
---
 common/cmd_fat.c |   89 +++++++++++++++++++++++++++++++-----------------------
 common/env_fat.c |   20 +-----------
 include/fat.h    |    7 ++++
 3 files changed, 60 insertions(+), 56 deletions(-)

diff --git a/common/cmd_fat.c b/common/cmd_fat.c
index 559a16d..a197393 100644
--- a/common/cmd_fat.c
+++ b/common/cmd_fat.c
@@ -32,6 +32,48 @@
 #include <part.h>
 #include <fat.h>
 
+long do_fat_safe(enum fat_op op, char *iname, char *file, int dev, int part,
+		       void *addr, unsigned long count)
+{
+	block_dev_desc_t *dev_desc = NULL;
+	long size = 0;
+
+	dev_desc = get_dev(iname, dev);
+	if (dev_desc == NULL) {
+		puts("\n** Invalid boot device **\n");
+		return -1;
+	}
+
+	if (fat_register_device(dev_desc, part) != 0) {
+		printf("\n** Unable to use %s %d:%d for fat%s **\n",
+		       iname, dev, part, op == FAT_READ ? "load" : "write");
+		return -1;
+	}
+
+	switch (op) {
+	case FAT_READ:
+		size = file_fat_read(file, addr, count);
+		break;
+#ifdef CONFIG_FAT_WRITE
+	case FAT_WRITE:
+		size = file_fat_write(file, addr, count);
+		break;
+#endif
+	default:
+		puts("Operation not supported!\n");
+		return -1;
+	}
+
+	if (size == -1) {
+		printf("\n** Unable to %s \"%s\" from %s %d:%d **\n",
+		       op == FAT_READ ? "read" : "write",
+		       file, iname, dev, part);
+		return -1;
+	}
+	printf("%ld bytes %s\n", size, op == FAT_READ ? "read" : "written");
+
+	return size;
+}
 
 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
@@ -39,7 +81,6 @@ int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	unsigned long offset;
 	unsigned long count;
 	char buf [12];
-	block_dev_desc_t *dev_desc=NULL;
 	int dev=0;
 	int part=1;
 	char *ep;
@@ -51,11 +92,6 @@ int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	}
 
 	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");
@@ -63,25 +99,17 @@ int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		}
 		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;
-	}
+	size = do_fat_safe(FAT_READ, argv[1], argv[4], dev, part,
+			     (void *)offset, count);
 
-	printf("\n%ld bytes read\n", size);
+	if (size == -1)
+		return 1;
 
 	sprintf(buf, "%lX", size);
 	setenv("filesize", buf);
@@ -134,7 +162,7 @@ int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	else
 		ret = file_fat_ls(filename);
 
-	if(ret!=0)
+	if (ret != 0)
 		printf("No Fat FS detected\n");
 	return ret;
 }
@@ -189,10 +217,9 @@ U_BOOT_CMD(
 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;
@@ -201,11 +228,6 @@ static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
 		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");
@@ -213,22 +235,13 @@ static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
 		}
 		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);
+	if (do_fat_safe(FAT_WRITE, argv[1], argv[4], dev, part,
+			      (void *)addr, count) == -1)
 		return 1;
-	}
-
-	printf("%ld bytes written\n", size);
 
 	return 0;
 }
diff --git a/common/env_fat.c b/common/env_fat.c
index bad92aa..35d8ac0 100644
--- a/common/env_fat.c
+++ b/common/env_fat.c
@@ -85,25 +85,9 @@ int saveenv(void)
 	}
 #endif /* CONFIG_MMC */
 
-	dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
-	if (dev_desc == NULL) {
-		printf("Failed to find %s%d\n",
-			FAT_ENV_INTERFACE, dev);
-		return 1;
-	}
-	if (fat_register_device(dev_desc, part) != 0) {
-		printf("Failed to register %s%d:%d\n",
-			FAT_ENV_INTERFACE, dev, part);
-		return 1;
-	}
-
 	env_new.crc = crc32(0, env_new.data, ENV_SIZE);
-	if (file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t)) == -1) {
-		printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
-			FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
-		return 1;
-	}
-
+	do_fat_safe(FAT_WRITE, FAT_ENV_INTERFACE, FAT_ENV_FILE, dev, part,
+			  (void *)&env_new, sizeof(env_t));
 	puts("done\n");
 	return 0;
 }
diff --git a/include/fat.h b/include/fat.h
index f1b4a0d..20a0682 100644
--- a/include/fat.h
+++ b/include/fat.h
@@ -199,6 +199,11 @@ struct filesystem {
 	const char		name[12];
 };
 
+enum fat_op {
+	FAT_READ = 1,
+	FAT_WRITE
+};
+
 /* FAT tables */
 file_detectfs_func	file_fat_detectfs;
 file_ls_func		file_fat_ls;
@@ -213,4 +218,6 @@ const char *file_getfsname(int idx);
 int fat_register_device(block_dev_desc_t *dev_desc, int part_no);
 
 int file_fat_write(const char *filename, void *buffer, unsigned long maxsize);
+long do_fat_safe(enum fat_op op, char *iname, char *file, int dev, int part,
+		       void *addr, unsigned long count);
 #endif /* _FAT_H_ */
-- 
1.7.2.3



More information about the U-Boot mailing list