- Switch to Sytemd - Add development tools: adroid tool, usbgadget support - Rework on recovery Image
145 lines
3.2 KiB
Bash
Executable File
145 lines
3.2 KiB
Bash
Executable File
#! /bin/sh
|
|
|
|
. /etc/profile
|
|
PATH=/sbin:/bin:/usr/sbin:/usr/bin
|
|
|
|
if [ -z "$DISK" ]; then
|
|
DISK="mmcblk0"
|
|
fi
|
|
|
|
ROOTFS_DEV="/dev/${DISK}p2"
|
|
SUPPORTED_COMMAND="rootfs kernel initramfs"
|
|
TMP_MOUNT="/tmp/rootfs"
|
|
|
|
rootfs()
|
|
{
|
|
path=$1
|
|
if [ -z "$path" ]; then
|
|
echo "Please specify rootfs path"
|
|
return 1
|
|
fi
|
|
backup_file="/tmp/rootfs-backup.img"
|
|
echo "Checking rootfs at: $path"
|
|
if [ ! -e "$path" ]; then
|
|
echo "Error: rootfs image not found $path"
|
|
return 1
|
|
fi
|
|
# backup the rootfs file
|
|
echo "Backing up the current rootfs to $backup_file"
|
|
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
|
|
mkdir -p $TMP_MOUNT
|
|
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"
|
|
if ! tar -xpvf "$path" -C $TMP_MOUNT; then
|
|
echo "Error: unable to install new rootfs. Restore and quit"
|
|
umount $TMP_MOUNT
|
|
dd if=$backup_file of=$ROOTFS_DEV
|
|
return 1
|
|
fi
|
|
echo "Patch /etc/fstab"
|
|
cat << EOF >> $TMP_MOUNT/etc/fstab
|
|
|
|
/dev/${DISK}p1 /boot vfat defaults 0 0
|
|
/dev/${DISK}p3 /var/etc ext4 defaults 0 0
|
|
/dev/${DISK}p4 /home ext4 defaults 0 0
|
|
|
|
EOF
|
|
sync
|
|
echo "Unmount the rootfs partition"
|
|
umount $TMP_MOUNT
|
|
echo "Done"
|
|
return 0
|
|
}
|
|
|
|
kernel()
|
|
{
|
|
path=$1
|
|
if [ -z "$path" ]; then
|
|
echo "Please specify kernel path"
|
|
fi
|
|
echo "Checking kernel at: $path"
|
|
if [ ! -e "$path" ]; then
|
|
echo "Error: kernel file not found"
|
|
return 1
|
|
fi
|
|
echo "Update kernel"
|
|
mv /boot/Image.gz /boot/Image.gz.old
|
|
cp -v "$path" /boot/Image.gz
|
|
cd /boot
|
|
sync
|
|
echo "Done"
|
|
return 0
|
|
}
|
|
|
|
initramfs()
|
|
{
|
|
path=$1
|
|
if [ -z "$path" ]; then
|
|
echo "Please specify path to initramfs"
|
|
fi
|
|
echo "Checking initramfs at: $path"
|
|
if [ ! -e "$path" ]; then
|
|
echo "Error: initramfs file not found"
|
|
return 1
|
|
fi
|
|
echo "Update recovery intramfs"
|
|
mv /boot/recovery-${MACHINE}.cpio.lz4.u-boot /boot/recovery-${MACHINE}.cpio.lz4.u-boot.old
|
|
cp -v "$path" /boot/recovery-${MACHINE}.cpio.lz4.u-boot
|
|
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 $file
|
|
;;
|
|
kernel)
|
|
kernel $file
|
|
;;
|
|
initramfs)
|
|
initramfs $file
|
|
;;
|
|
*)
|
|
cat << EOF
|
|
Usage: $name <command> [file]
|
|
commands:
|
|
- rootfs: update rootfs
|
|
- kernel: update kernel image
|
|
- initramfs: update recovery initramfs
|
|
EOF
|
|
exit 1
|
|
;;
|
|
esac
|