From 323c7fdda60758506dea9da6c477e42a0dbe86ca Mon Sep 17 00:00:00 2001
From: Samuel Dolt <samuel.dolt@netmodule.com>
Date: Wed, 22 Feb 2023 13:22:27 +0100
Subject: [PATCH] fs: add support to create FAT16 partition

Currently swupdate can create vfat partition using an embedded
copy of the fat_fs library. The partition created by fat_fs is
can be any FAT format (12/16/32) based on the partition size.

As the partition created by fat_fs was not readable by U-Boot,
this commit add a new "fat16" filesystem type that use the
external binary mkfs.fat to always create FAT16 partition.
---
 fs/diskformat.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/fs/diskformat.c b/fs/diskformat.c
index 8d58fc3..f98c9d7 100644
--- a/fs/diskformat.c
+++ b/fs/diskformat.c
@@ -19,6 +19,21 @@ static inline int ext_mkfs_short(const char *device_name, const char *fstype)
 }
 #endif
 
+int fat_mkfs_extern(const char *device_name, const char *fstype)
+{
+	char cmd[100] = "mkfs.fat -F ";
+	if(strcmp(fstype, "fat16") == 0){
+		strcat(cmd, "16 ");
+	} else {
+		ERROR("fat_mkfs_extern: unsupported fstype %s", fstype);
+		return -1;
+	}
+	strncat(cmd, device_name, sizeof(cmd) - strlen(cmd) - 1);
+
+	TRACE("fat_mkfs_extern: running %s", cmd);
+	return system(cmd);
+}
+
 struct supported_filesystems {
 	const char *fstype;
 	int (*mkfs)(const char *device_name, const char *fstype);
@@ -28,6 +43,7 @@ static struct supported_filesystems fs[] = {
 #if defined(CONFIG_FAT_FILESYSTEM)
 	{"vfat", fat_mkfs},
 #endif
+	{"fat16", fat_mkfs_extern},
 #if defined(CONFIG_EXT_FILESYSTEM)
 	{"ext2", ext_mkfs_short},
 	{"ext3", ext_mkfs_short},
