From 332f6f8f3dda3328a76a21d7304383b481277c09 Mon Sep 17 00:00:00 2001
From: Bartlomiej Grzeskowiak <bartlomiej.grzeskowiak@iopsys.eu>
Date: Tue, 30 Nov 2021 12:24:44 +0100
Subject: [PATCH] iopsys-econet: Add overlay support for mtd_squashfs layout

Mtd_squashfs layout is used to workaround nand flash issues on some flash
chips. Overlay in Ubi support is added also to this configuration.

Signed-off-by: Bartlomiej Grzeskowiak <bartlomiej.grzeskowiak@iopsys.eu>
---
 .../base-files/etc/system_layout/mtd.json     | 27 +++++++++++++++++++
 .../lib/preinit/05_mount_tmpfs_overlay        |  8 ++++--
 iopsys-econet/image/upgrade-bundle/upgrade    | 20 +++++++++++---
 3 files changed, 49 insertions(+), 6 deletions(-)
 create mode 100644 iopsys-econet/base-files/etc/system_layout/mtd.json

diff --git a/iopsys-econet/base-files/etc/system_layout/mtd.json b/iopsys-econet/base-files/etc/system_layout/mtd.json
new file mode 100644
index 000000000..405ee64bb
--- /dev/null
+++ b/iopsys-econet/base-files/etc/system_layout/mtd.json
@@ -0,0 +1,27 @@
+{
+  "banks": [
+    {
+      "id": 1,
+      "name": "A",
+      "rootfs": "/dev/mtdblock3",
+      "overlay": "ubi:overlay1"
+    },
+    {
+      "id": 2,
+      "name": "B",
+      "rootfs": "/dev/mtdblock4",
+      "overlay": "ubi:overlay2"
+    }
+  ],
+  "overlay_scope": [
+    "/"
+  ],
+  "mounts": [
+    {
+      "mountpoint": "/data",
+      "device": "ubi:data",
+      "type": "ubifs",
+      "options": []
+    }
+  ]
+}
diff --git a/iopsys-econet/base-files/lib/preinit/05_mount_tmpfs_overlay b/iopsys-econet/base-files/lib/preinit/05_mount_tmpfs_overlay
index bf4f5cf6d..81e72b4e2 100644
--- a/iopsys-econet/base-files/lib/preinit/05_mount_tmpfs_overlay
+++ b/iopsys-econet/base-files/lib/preinit/05_mount_tmpfs_overlay
@@ -3,8 +3,12 @@ mount_tmpfs_overlay() {
 
 	case "$rootfs_type" in
 	squashfs*)
-		ramoverlay
-		echo "ramoverlay activated"
+		if ! ubiattach -p /dev/mtd8; then
+			echo "ubi attach failed activate ramoverlay"
+			ramoverlay
+		else
+			echo "ubi attached"
+		fi
 		;;
 	esac
 }
diff --git a/iopsys-econet/image/upgrade-bundle/upgrade b/iopsys-econet/image/upgrade-bundle/upgrade
index 0c22cc761..148eb98df 100755
--- a/iopsys-econet/image/upgrade-bundle/upgrade
+++ b/iopsys-econet/image/upgrade-bundle/upgrade
@@ -1,5 +1,7 @@
 #!/bin/sh
 
+. /lib/functions.sh
+
 invocation=$1
 image=$2
 log_tag="$0 $1"
@@ -45,13 +47,14 @@ do_exit_reboot() {
 
 do_squashfs_update() {
 	local image=$1
-	local next_bank_boot_mtd="mtd2"
-	local next_bank_rootfs_mtd="mtd4"
+	local next_bank_boot_mtd=mtd"$( find_mtd_index "boot2" )"
+	local next_bank_rootfs_mtd=mtd"$( find_mtd_index "rootfs2" )"
 	local next_bank_id="2"
+	local ubicontainer_mtd=mtd"$( find_mtd_index "ubi_container" )"
 
 	if [[ "1" != "$(fw_printenv -n active_image)" ]]; then
-		next_bank_boot_mtd="mtd1"
-		next_bank_rootfs_mtd="mtd3"
+		next_bank_boot_mtd=mtd"$( find_mtd_index "boot1" )"
+		next_bank_rootfs_mtd=mtd"$( find_mtd_index "rootfs1" )"
 		next_bank_id="1"
 	fi
 
@@ -82,6 +85,15 @@ do_squashfs_update() {
 	rm /tmp/squashfs.bin
 
 	do_post_upgrade $image
+	log "- Install overlay partition if not present -"
+	if [[ "1" != "$(fw_printenv -n overlay_installed)" ]]; then
+		flash_erase /dev/$ubicontainer_mtd 0 0
+		ubiformat /dev/$ubicontainer_mtd
+		ubiattach -p /dev/$ubicontainer_mtd
+		ubimkvol /dev/ubi0 -n 6 -N overlay1 -s 10MiB
+		ubimkvol /dev/ubi0 -n 7 -N overlay2 -s 10MiB
+		fw_setenv overlay_installed "1"
+	fi
 	log "- Set boot bank $next_bank_id and reboot-"
 	fw_setenv active_image $next_bank_id
 	do_exit_reboot
-- 
GitLab