persistent-vars-storage-ubootenv: adds a uboot-env compatible persistent storage utility

The Morse Micro persistent-vars-storage package is used to load and store certain device
parameters or configuration from non-volatile storage on a device. This is useful for a
more persistent configuration than what is offered by UCI. Typically used for dpp keys.

The uboot-env version targets any device which can allocate a uboot-env style partition
in flash dedicated for this purpose. It is recommended to not use the actual uboot-env
partition, but rather create a new partition using the uboot-env format.
This commit is contained in:
Morse Micro 2024-09-17 20:28:41 +10:00 committed by Arien Judge
parent 7eb52b2b31
commit 3d1c17f3da
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,36 @@
#
# Copyright 2024 Morse Micro
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=persistent-vars-storage-ubootenv
PKG_RELEASE=1
PKG_MAINTAINER:=Morse Micro
include $(INCLUDE_DIR)/package.mk
define Package/persistent-vars-storage-ubootenv
SECTION:=Utilities
CATEGORY:=Utilities
PROVIDES:=persistent-vars-storage
TITLE:=Reads or writes key-value vars from/into the persistent storage (u-boot-env)
DEPENDS:= uboot-envtools
endef
define Build/Compile
endef
define Package/persistent-vars-storage-ubootenv/install
$(INSTALL_DIR) $(1)/sbin
$(INSTALL_BIN) ./files/sbin/persistent_vars_storage.sh $(1)/sbin/persistent_vars_storage.sh
$(INSTALL_DIR) $(1)/etc
endef
$(eval $(call BuildPackage,persistent-vars-storage-ubootenv))

View File

@ -0,0 +1,74 @@
#!/bin/sh
#
# Copyright (C) 2023 MorseMicro
#
set -eu
trap exit_handler EXIT
exit_handler()
{
if [ "$?" -ne 0 ]; then
echo "Usage: $0 OPERATION(READ|WRITE|ERASE) KEY [VALUE]" 1>&2
exit 1
fi
}
# We don't use OpenWrt's find_mtd_part here because we want to write a script
# that exploits 'set -eu'.
find_mtd_partition()
{
local name="$1"
for partition in /sys/class/mtd/mtd*; do
if [ "$(cat "$partition/name" 2> /dev/null)" = "$name" ]; then
echo "/dev/$(basename "$partition")"
return
fi
done
1>&2 echo "Internal error: can't find $name partition"
exit 1
}
show_raw_partition_data()
{
local partition="$1" skip="$2" count="$3"
# Running this through shell/echo strips the nulls and puts a newline in (consistent with fw_printenv)
echo "$(dd if="$(find_mtd_partition "$partition")" bs=1 skip=$(($skip)) count="$count" 2> /dev/null)"
}
# Use secondary uboot env in preference
# (this one is less likely to be the 'real' uboot env).
# The disadvantage of using the real uboot env is that if it's empty/fails the CRC
# uboot-envtools decides to write the 'default' environment that it understands.
# Unfortunately, this is very unlikely to be consistent with the default environment
# compiled into the uboot, so it causes issues.
if [ -e /etc/fw_sys.config ]; then
conffile=/etc/fw_sys.config
else
conffile=/etc/fw_env.config
fi
operation="$1"
key="$2"
case "$operation" in
READ)
fw_printenv -n -c "$conffile" "$key" 2> /dev/null
;;
WRITE)
value="$3"
fw_setenv -c "$conffile" "$key" "$value"
;;
ERASE)
fw_setenv -c "$conffile" "$key"
;;
*)
exit 1
;;
esac