Add swupdate images
authorHugo Villeneuve <hugo@hugovil.com>
Fri, 30 Dec 2022 05:00:01 +0000 (00:00 -0500)
committerHugo Villeneuve <hugo@hugovil.com>
Wed, 1 Mar 2023 17:10:15 +0000 (12:10 -0500)
recipes-core/images/image-hvmpd-dev-update.bb [new file with mode: 0644]
recipes-core/images/image-hvmpd-prod-update.bb [new file with mode: 0644]
recipes-core/images/update-common.inc [new file with mode: 0644]
recipes-core/images/update/sw-description [new file with mode: 0644]
recipes-core/images/update/update.sh [new file with mode: 0644]

diff --git a/recipes-core/images/image-hvmpd-dev-update.bb b/recipes-core/images/image-hvmpd-dev-update.bb
new file mode 100644 (file)
index 0000000..f874b45
--- /dev/null
@@ -0,0 +1,8 @@
+SUMMARY = "HV MPD SWUpdate development image"
+
+# Image(s) to build before building swupdate image
+IMAGE_DEPENDS = "image-hvmpd-dev"
+
+require update-common.inc
+
+SWUPDATE_IMAGES_FSTYPES[image-hvmpd-dev] = ".tar.gz"
diff --git a/recipes-core/images/image-hvmpd-prod-update.bb b/recipes-core/images/image-hvmpd-prod-update.bb
new file mode 100644 (file)
index 0000000..754da4d
--- /dev/null
@@ -0,0 +1,8 @@
+SUMMARY = "HV MPD SWUpdate production image"
+
+# Image(s) to build before building swupdate image
+IMAGE_DEPENDS = "image-hvmpd-prod"
+
+require update-common.inc
+
+SWUPDATE_IMAGES_FSTYPES[image-hvmpd-prod] = ".tar.gz"
diff --git a/recipes-core/images/update-common.inc b/recipes-core/images/update-common.inc
new file mode 100644 (file)
index 0000000..ed80f58
--- /dev/null
@@ -0,0 +1,15 @@
+LICENSE = "CLOSED"
+
+FILESEXTRAPATHS:prepend := "${THISDIR}/update:"
+
+SRC_URI = " \
+    file://sw-description \
+    file://update.sh \
+"
+
+# Image(s) and files that will be included in the .swu image
+SWUPDATE_IMAGES = "${IMAGE_DEPENDS}"
+
+UBOOT_PART_VAR ?= "mmcpart"
+
+inherit swupdate
diff --git a/recipes-core/images/update/sw-description b/recipes-core/images/update/sw-description
new file mode 100644 (file)
index 0000000..78d91e9
--- /dev/null
@@ -0,0 +1,83 @@
+software = {
+       version = "@@DISTRO_VERSION@@";
+       description = "Firmware update description for @@MACHINE@@";
+
+       @@MACHINE@@ = {
+               hardware-compatibility: [ "revA" ];
+               stable: {
+                       bootenv-common: (
+                               {
+                                       name = "bootcount";
+                                       value = "0";
+                               },
+                               {
+                                       name = "upgrade_available";
+                                       value = "1";
+                               },
+                       );
+
+                       rootfsA: {
+                               images: (
+                                       {
+                                               filename = "@@IMAGE_DEPENDS@@-@@MACHINE@@@@SWUPDATE_IMAGES_FSTYPES[@@IMAGE_DEPENDS@@]@@";
+                                               type = "archive";
+                                               compressed = "zlib";
+                                               preserve-attributes = true;
+                                               sha256 = "$swupdate_get_sha256(@@IMAGE_DEPENDS@@-@@MACHINE@@@@SWUPDATE_IMAGES_FSTYPES[@@IMAGE_DEPENDS@@]@@)";
+                                               path = "/"; /* Destination path when decompressing. */
+                                               filesystem = "ext4";
+                                               device = "@@ROOT_PARENT_DEV@@@@ROOT_PART_PREFIX@@@@ROOT_PART_A_ID@@";
+                                       }
+                               );
+                               scripts: (
+                                       {
+                                               filename = "update.sh";
+                                               type = "shellscript";
+                                               data = "@@ROOT_PART_PREFIX@@@@ROOT_PART_A_ID@@"; /* Destination partition ID */
+                                               sha256 = "$swupdate_get_sha256(update.sh)";
+                                        }
+                                );
+                               bootenv: (
+                                       {
+                                               ref = "#./../bootenv-common";
+                                       },
+                                       {
+                                               name = "@@UBOOT_PART_VAR@@";
+                                               value = "@@ROOT_PART_A_ID@@";
+                                       }
+                               );
+                       };
+                       rootfsB: {
+                               images: (
+                                       {
+                                               filename = "@@IMAGE_DEPENDS@@-@@MACHINE@@@@SWUPDATE_IMAGES_FSTYPES[@@IMAGE_DEPENDS@@]@@";
+                                               type = "archive";
+                                               compressed = "zlib";
+                                               preserve-attributes = true;
+                                               sha256 = "$swupdate_get_sha256(@@IMAGE_DEPENDS@@-@@MACHINE@@@@SWUPDATE_IMAGES_FSTYPES[@@IMAGE_DEPENDS@@]@@)";
+                                               path = "/"; /* Destination path when decompressing. */
+                                               filesystem = "ext4";
+                                               device = "@@ROOT_PARENT_DEV@@@@ROOT_PART_PREFIX@@@@ROOT_PART_B_ID@@";
+                                       }
+                               );
+                               scripts: (
+                                       {
+                                               filename = "update.sh";
+                                               type = "shellscript";
+                                               data = "@@ROOT_PART_PREFIX@@@@ROOT_PART_B_ID@@"; /* Destination partition ID */
+                                               sha256 = "$swupdate_get_sha256(update.sh)";
+                                        }
+                                );
+                               bootenv: (
+                                       {
+                                               ref = "#./../bootenv-common";
+                                       },
+                                       {
+                                               name = "@@UBOOT_PART_VAR@@";
+                                               value = "@@ROOT_PART_B_ID@@";
+                                       }
+                               );
+                       };
+               };
+       };
+}
diff --git a/recipes-core/images/update/update.sh b/recipes-core/images/update/update.sh
new file mode 100644 (file)
index 0000000..6efa218
--- /dev/null
@@ -0,0 +1,90 @@
+#!/bin/sh
+set -e
+
+# Shell scripts are called via system command. SWUpdate scans for all scripts
+# and calls them before and after installing the images. SWUpdate passes
+# ‘preinst’ or ‘postinst’ as first argument to the script. If the data attribute
+# is defined, its value is passed as the last argument(s) to the script.
+
+FSTYPE="ext4"
+
+echo "${0}: arguments = \"${*}\""
+
+if [ $# -lt 2 ]; then
+    exit 1;
+fi
+
+# L'environnement U-Boot doit être valide pour utiliser ce script.
+# Après la programmation initiale avec uuu, il faut effectuer la sauvegarde
+# de l'environnement en mémoire non-volatile (ex: eMMC) dans U-boot avec
+# 'saveenv'. Sinon, fw_saveenv va utiliser l'environnement par défaut contenu
+# dans /etc/u-boot-initial-env
+if fw_printenv 2>&1 | grep -q 'Cannot read environment'; then
+    echo "Warning: U-Boot environment cannot be read. Make sure you save the"
+    echo "         default environment to flash using these U-Boot commands:"
+    echo "             $> env default -a"
+    echo "             $> saveenv"
+fi
+
+do_preinst()
+{
+    # Find internal parent kernel device name of rootfs.
+    # For example:
+    # - /dev/sda1 --> /dev/sda
+    # - /dev/mmcblk2p2 --> /dev/mmcblk2
+    disk="/dev/$(lsblk -ndo pkname $(findmnt -n -o SOURCE /))"
+
+    if [ ! -b "${disk}" ]; then
+        echo "Error: disk \"${disk}\" not found"
+        exit 1
+    fi
+
+    update_dev="${disk}${next_part}"
+
+    if [ ! -b "${update_dev}" ]; then
+        echo "Error: destination partition \"${update_dev}\" not found"
+        exit 1
+    fi
+
+    echo "${0}: destination device = ${update_dev}"
+
+    # Create temporary mount point:
+    tmp_file=$(mktemp -q -d /tmp/swupdate-mount.XXXXXX)
+    if [ ${?} -ne 0 ]; then
+        echo "Error: cannot create temporary file"
+        exit 1
+    fi
+
+    # Mount update partition:
+    mount -t ${FSTYPE} ${update_dev} ${tmp_file}
+
+    # Remove old data:
+    echo "${0}: erasing old data..."
+    rm -rf ${tmp_file}/*
+
+    # Finish
+    umount ${tmp_file}
+    rmdir ${tmp_file}
+
+    exit 0
+}
+
+do_postinst()
+{
+    exit 0
+}
+
+next_part=${2}
+
+case "$1" in
+    preinst)
+        do_preinst
+        ;;
+    postinst)
+        do_postinst
+        ;;
+    *)
+        echo "unsupported install mode: \"${1}\""
+        exit 1
+        ;;
+esac