diff mbox series

[v2,4/5] handlers: diskpart: add filesystem creation support

Message ID 20210325154623.7717-4-roland.gaudig-oss@weidmueller.com
State Accepted
Headers show
Series [v2,1/5] fatfs: add FatFs library R0.14a by ChaN | expand

Commit Message

Roland Gaudig March 25, 2021, 3:46 p.m. UTC
From: Roland Gaudig <roland.gaudig@weidmueller.com>

Signed-off-by: Roland Gaudig <roland.gaudig@weidmueller.com>
---
 Kconfig                     |  1 -
 handlers/Config.in          | 18 +++++++++++++++++
 handlers/diskpart_handler.c | 40 +++++++++++++++++++++++++++++++++++--
 3 files changed, 56 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/Kconfig b/Kconfig
index 83d2857..75f9eaa 100644
--- a/Kconfig
+++ b/Kconfig
@@ -498,4 +498,3 @@  config ZSTD
 
 source parser/Config.in
 source handlers/Config.in
-source fatfs/Config.in
diff --git a/handlers/Config.in b/handlers/Config.in
index 8571c0c..7a2a021 100644
--- a/handlers/Config.in
+++ b/handlers/Config.in
@@ -102,6 +102,24 @@  config DISKPART
 comment "diskpart support needs libfdisk"
 	depends on !HAVE_LIBFDISK
 
+if DISKPART
+
+menuconfig DISKFORMAT
+        bool "diskpart extension for creating file systems"
+	depends on DISKPART
+	default n
+	help
+	  This extension of the diskpart handler allows creating filesystems
+	  on empty partitions.
+
+if DISKFORMAT
+
+source fatfs/Config.in
+
+endif
+
+endif
+
 config UNIQUEUUID
 	bool "uniqueuuid"
 	depends on HAVE_LIBBLKID
diff --git a/handlers/diskpart_handler.c b/handlers/diskpart_handler.c
index 16c9834..91fdcd5 100644
--- a/handlers/diskpart_handler.c
+++ b/handlers/diskpart_handler.c
@@ -18,6 +18,7 @@ 
 #include "swupdate.h"
 #include "handler.h"
 #include "util.h"
+#include "fatfs_interface.h"
 
 void diskpart_handler(void);
 
@@ -37,14 +38,16 @@  enum partfield {
 	PART_SIZE = 0,
 	PART_START,
 	PART_TYPE,
-	PART_NAME
+	PART_NAME,
+	PART_FSTYPE
 };
 
 const char *fields[] = {
 	[PART_SIZE] = "size",
 	[PART_START] = "start",
 	[PART_TYPE] = "type",
-	[PART_NAME] = "name"
+	[PART_NAME] = "name",
+	[PART_FSTYPE] = "fstype"
 };
 
 struct partition_data {
@@ -53,6 +56,7 @@  struct partition_data {
 	size_t start;
 	char type[SWUPDATE_GENERAL_STRING_SIZE];
 	char name[SWUPDATE_GENERAL_STRING_SIZE];
+	char fstype[SWUPDATE_GENERAL_STRING_SIZE];
 	LIST_ENTRY(partition_data) next;
 };
 LIST_HEAD(listparts, partition_data);
@@ -219,6 +223,9 @@  static int diskpart(struct img_type *img,
 					case PART_NAME:
 						strncpy(part->name, equal, sizeof(part->name)); 
 						break;
+					case PART_FSTYPE:
+						strncpy(part->fstype, equal, sizeof(part->fstype));
+						break;
 					}
 				}
 			}
@@ -380,6 +387,35 @@  static int diskpart(struct img_type *img,
 		TRACE("Same partition table on disk, do not touch partition table !");
 	}
 
+#ifdef CONFIG_DISKFORMAT
+	/* Create filesystems */
+	LIST_FOREACH(part, &priv.listparts, next) {
+		/*
+		 * priv.listparts counts partitions starting with 0,
+		 * but fdisk_partname expects the first partition having
+		 * the number 1.
+		 */
+		size_t partno = part->partno + 1;
+
+		if (!strlen(part->fstype))
+			continue;  /* Don't touch partitions without fstype */
+
+#ifdef CONFIG_FAT_FILESYSTEM
+		if (!strcmp(part->fstype, "vfat")) {
+			char *device = NULL;
+			device = fdisk_partname(img->device, partno);
+			TRACE("Creating vfat file system on partition-%lu, device %s", partno, device);
+			ret = fat_mkfs(device);
+			if (ret)
+				ERROR("creating vfat file system failed. %d", ret);
+			free(device);
+			continue;
+		}
+#endif
+		ERROR("partition-%lu %s filesystem type not supported.", partno, part->fstype);
+	}
+#endif
+
 handler_exit:
 	if (tb)
 		fdisk_unref_table(tb);