redpitaya-puzzlefw/sw/buildroot_overlay/opt/puzzlefw/lib/functions.sh

84 lines
2.2 KiB
Bash

#
# Settings and functions for shell scripts.
#
CONFIG_PARTITION=mmcblk0p2
CONFIG_MOUNTPOINT=/mnt/cfg
CONFIG_DIR=/var/lib/puzzlefw/cfg
# Lock the configuration files.
lock_config() {
# Make config directory in RAM filesystem.
mkdir -p $CONFIG_DIR
# Create or open lock file.
# The file remains open until the calling script ends.
exec 3>>${CONFIG_DIR}/lockfile
# Get exclusive lock on the file.
if ! flock -n 3 ; then
echo "ERROR: Configuration locked" >&2
return 1
fi
}
# Read configuration files from the SD card.
# This is only used during boot.
read_config() {
# Mount config filesystem read-only.
mkdir -p $CONFIG_MOUNTPOINT
mount -t ext4 -r -o noatime,data=journal /dev/${CONFIG_PARTITION} $CONFIG_MOUNTPOINT || return 1
# Copy config files to RAM filesystem.
cp -a ${CONFIG_MOUNTPOINT}/*.conf $CONFIG_DIR
umount $CONFIG_MOUNTPOINT
}
# Write changed configuration files to the SD card.
# Parameters: names of config files to synchronize
sync_config() {
# Mount config filesystem.
mkdir -p $CONFIG_MOUNTPOINT
mount -t ext4 -o noatime,data=journal /dev/${CONFIG_PARTITION} $CONFIG_MOUNTPOINT || return 1
for fname in "$@" ; do
# Copy new version of file to SD card.
cp ${CONFIG_DIR}/${fname}.new $CONFIG_MOUNTPOINT
# Atomically replace the old file on the SD card.
mv ${CONFIG_MOUNTPOINT}/${fname}.new ${CONFIG_MOUNTPOINT}/${fname}
# Replace the old file in the RAM filesystem.
mv ${CONFIG_DIR}/${fname}.new ${CONFIG_DIR}/${fname}
done
umount $CONFIG_MOUNTPOINT
sync
}
# Read settings from EEPROM.
# Defines variables $eeprom_ethaddr and $eeprom_hw_rev.
read_eeprom() {
tmpfile=$(mktemp -t eeprom.XXXXXX)
dd if=/sys/bus/i2c/devices/0-0050/eeprom bs=1k skip=6 count=1 status=none | tr '\000' '\n' > $tmpfile
while read -r eeprom_item eeprom_rest ; do
if [ "${eeprom_item}" != "${eeprom_item#ethaddr=}" ]; then
eeprom_ethaddr="${eeprom_item#ethaddr=}"
fi
if [ "${eeprom_item}" != "${eeprom_item#hw_rev=}" ]; then
eeprom_hw_rev="${eeprom_item#hw_rev=}"
fi
done < $tmpfile
rm -f $tmpfile
}