git: 68f5e5b1244d - main - nanobsd: embedded: Calculate FAT type and scheme

From: Jose Luis Duran <jlduran_at_FreeBSD.org>
Date: Thu, 19 Mar 2026 17:15:25 UTC
The branch main has been updated by jlduran:

URL: https://cgit.FreeBSD.org/src/commit/?id=68f5e5b1244d1285f0ec5ea3f88ff63f2c9554dd

commit 68f5e5b1244d1285f0ec5ea3f88ff63f2c9554dd
Author:     Jose Luis Duran <jlduran@FreeBSD.org>
AuthorDate: 2026-03-19 17:12:26 +0000
Commit:     Jose Luis Duran <jlduran@FreeBSD.org>
CommitDate: 2026-03-19 17:12:26 +0000

    nanobsd: embedded: Calculate FAT type and scheme
    
    Determine the FAT (MS-DOS) type (FAT32, FAT16, or FAT12) and partition
    scheme (fat32lba or fat16b) based on partition size.
    
    Accept any (NetBSD) strsuftoll(3)-compatible string, as the value will
    be fed to makefs(8) "-s" in a future commit.
    
    NANO_SLICE_FAT_SIZEs defined in sectors (ending with an "s") will not be
    supported.  As a workaround, the letter "b" (blocks) may be used.
    
    Reviewed by:    imp
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D54975
---
 tools/tools/nanobsd/embedded/common | 49 ++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/tools/tools/nanobsd/embedded/common b/tools/tools/nanobsd/embedded/common
index 87dad52fcaca..ebbe764f9028 100644
--- a/tools/tools/nanobsd/embedded/common
+++ b/tools/tools/nanobsd/embedded/common
@@ -171,6 +171,31 @@ WITHOUT_INSTALLLIB=true
 
 NANO_PACKAGE_ONLY=1
 
+# makefs(8)-size compatible
+# See NetBSD's strsuftoll(3)
+strsuftoll() {
+	local num result unit
+
+	num=${1%?}
+	unit=${1#"${num}"}
+
+	case "$unit" in
+	[bB]) result="${num}x512" ;;
+	[kK]) result="${num}x1024" ;;
+	[mM]) result="${num}x1024x1024" ;;
+	[gG]) result="${num}x1024x1024x1024" ;;
+	[tT]) result="${num}x1024x1024x1024x1024" ;;
+	[wW]) result="${num}x4" ;; # sizeof(int)
+	[0-9]) result="$1" ;;
+	*)
+		printf "%s\n" "'$1': illegal number"
+		exit 1
+		;;
+	esac
+
+	printf "%s" "$(echo "scale=0; $result" | tr 'x' '*' | bc)"
+}
+
 # Creates images for all the formats that use MBR / GPT
 # split later if the #ifdef soup gets too bad.
 create_diskimage_gpt() {
@@ -190,7 +215,8 @@ create_diskimage_mbr() {
 	pprint 3 "image in: ${NANO_DISKIMGDIR}/_.disk.image.${NANO_NAME}${fmt}"
 
 	(
-	local extra i sz fmt fmtarg bootmbr bootbsd skiparg
+	local extra i sz fat_scheme fat_size fat_type fmt fmtarg bootmbr \
+	    bootbsd skiparg
 	set -o xtrace
 	# Tell mtools not to be too picky
 	export MTOOLS_SKIP_CHECK=1
@@ -208,7 +234,24 @@ create_diskimage_mbr() {
 	# Populate the FAT partition, if needed
 	if [ -n "${NANO_SLICE_FAT}" ]; then
 		echo Creating MSDOS partition for kernel
-		newfs_msdos -C ${NANO_SLICE_FAT_SIZE} -F 16 -L ${NANO_NAME} \
+
+		# Minimum size of FAT filesystems
+		FAT16MIN=2150400
+		FAT32MIN=34091008
+
+		fat_size=$(strsuftoll "$NANO_SLICE_FAT_SIZE")
+		if [ "$fat_size" -ge "$FAT32MIN" ]; then
+			fat_type=32
+			fat_scheme=fat32lba
+		elif [ "$fat_size" -ge "$FAT16MIN" ]; then
+			fat_type=16
+			fat_scheme=fat16b
+		else
+			fat_type=12
+			fat_scheme=fat16b
+		fi
+
+		newfs_msdos -C ${NANO_SLICE_FAT_SIZE} -F "$fat_type" -L ${NANO_NAME} \
 			${NANO_LOG}/_.${NANO_SLICE_FAT}
 		if [ -d ${NANO_FAT_DIR} ]; then
 			# Need to copy files from ${NANO_FATDIR} with mtools, or use
@@ -256,7 +299,7 @@ create_diskimage_mbr() {
 
 	# Now shuffle all the slices together into the proper layout
 	if [ -n "$NANO_SLICE_FAT" ]; then
-		eval $NANO_SLICE_FAT=fat16b
+		eval $NANO_SLICE_FAT=$fat_scheme
 	fi
 
 	out=${NANO_DISKIMGDIR}/_.disk.image.${NANO_NAME}${fmt}