1
0
mirror of https://github.com/lxsang/meta-rpi-diya.git synced 2024-12-29 03:48:22 +01:00
meta-rpi-diya/recipes-diya/initramfs/files/diya-update

137 lines
3.0 KiB
Plaintext
Raw Normal View History

2023-08-02 17:51:11 +02:00
#! /bin/sh
. /etc/profile
DEFAULT_ROOTFS_NAME="rootfs-$MACHINE.tar.bz2"
DEFAULT_KERNEL_NAME="Image-$MACHINE.bin"
DEFAULT_INITRAMFS_NAME="recovery-$MACHINE.cpio.gz"
UPDATE_SRC_PATH="/home/diya/.update"
ROOTFS_DEV="/dev/mmcblk0p2"
SUPPORTED_COMMAND="rootfs kernel initramfs"
TMP_MOUNT="/tmp/rootfs"
rootfs()
{
filename=$1
if [ -z "$filename" ]; then
filename="$DEFAULT_ROOTFS_NAME"
fi
path="$UPDATE_SRC_PATH/$filename"
backup_file="$UPDATE_SRC_PATH/rootfs-backup.img"
echo "Checking rootfs at: $path"
if [ ! -e "$path" ]; then
echo "Error: rootfs file not found"
return 1
fi
# backup the rootfs file
echo "Backing up the current rootfs"
if ! dd if=$ROOTFS_DEV of=$backup_file; then
echo "Error: unable to backup current rootfs"
return 1
fi
echo "Format rootfs partition"
if ! mkfs.ext4 -F $ROOTFS_DEV; then
echo "Error: Unable to format rootfs partition. Restore and quit"
dd if=$backup_file of=$ROOTFS_DEV
return 1
fi
2023-08-02 18:07:55 +02:00
mkdir -p $TMP_MOUNT
2023-08-02 17:51:11 +02:00
echo "Mount rootfs partition to $TMP_MOUNT"
if ! mount $ROOTFS_DEV $TMP_MOUNT; then
echo "Error: Unable to mount rootfs partition. Restore and quit"
dd if=$backup_file of=$ROOTFS_DEV
return 1
fi
echo "Installing new rootfs"
2023-08-02 18:19:25 +02:00
if ! tar --same-owner -xpvf "$path" -C $TMP_MOUNT; then
2023-08-02 17:51:11 +02:00
echo "Error: unable to install new rootfs. Restore and quit"
umount $TMP_MOUNT
dd if=$backup_file of=$ROOTFS_DEV
return 1
fi
sync
echo "Unmount the rootfs partition"
umount $TMP_MOUNT
echo "Done"
return 0
}
kernel()
{
filename=$1
if [ -z "$filename" ]; then
filename="$DEFAULT_KERNEL_NAME"
fi
path="$UPDATE_SRC_PATH/$filename"
echo "Checking kernel at: $path"
if [ ! -e "$path" ]; then
echo "Error: kernel file not found"
return 1
fi
echo "Update kernel"
2023-08-02 18:28:34 +02:00
cp -v "$path" /boot/kernel8.img
cd /boot
sync
2023-08-02 17:51:11 +02:00
echo "Done"
return 0
}
initramfs()
{
filename=$1
if [ -z "$filename" ]; then
filename="$DEFAULT_INITRAMFS_NAME"
fi
path="$UPDATE_SRC_PATH/$filename"
echo "Checking initramfs at: $path"
if [ ! -e "$path" ]; then
echo "Error: initramfs file not found"
return 1
fi
echo "Update recovery intramfs"
2023-08-02 18:28:34 +02:00
cp -v "$path" /boot/$DEFAULT_INITRAMFS_NAME
2023-08-02 17:51:11 +02:00
cd /boot
sync
echo "Done"
return 0
}
command_valid() {
VALUE=$1
echo $SUPPORTED_COMMAND | tr " " '\n' | grep -F -q -x "$VALUE"
}
name=$(basename $0)
cmd=${name#diya-update-}
file="$1"
if ! command_valid "$cmd"; then
cmd="$1"
file="$2"
fi
case "$cmd" in
rootfs)
rootfs $1
;;
kernel)
kernel $1
;;
initramfs)
initramfs $1
;;
*)
cat << EOF
Usage: $name <command> [file]
commands:
- rootfs: update rootfs
- kernel: update kernel image
- initramfs: update recovery initramfs
EOF
exit 1
;;
esac