initramfs: add support for new root info file format

This commit is contained in:
Calin Crisan 2019-02-03 15:21:01 +02:00
parent d0bfab9555
commit 6fe87fad81
3 changed files with 37 additions and 33 deletions

View File

@ -1,18 +1,20 @@
#!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
DISK_TIMEOUT="10"
DISK_TIMEOUT=10
msg() {
echo " * $1"
}
sh
msg "Mounting pseudo filesystems"
mount -t devtmpfs devtmpfs /dev
mount -t proc proc /proc
ROOT_DEV=$(cat /proc/cmdline | grep -oE 'root=[/a-z0-9]+' | cut -d '=' -f 2)
if echo ${ROOT_DEV: -2} | grep -E 'p[0-9]' &>/dev/null; then # e.g. /dev/mmcblk0p2
if echo ${ROOT_DEV:-2} | grep -E 'p[0-9]' &>/dev/null; then # e.g. /dev/mmcblk0p2
DISK_DEV=${ROOT_DEV:0:$((${#ROOT_DEV}-2))}
BOOT_DEV=${DISK_DEV}p1
DATA_DEV=${DISK_DEV}p3
@ -28,10 +30,10 @@ while true; do
if [ ${count} -ge ${DISK_TIMEOUT} ]; then
break
fi
if [ -b ${ROOT_DEV} ]; then
if [[ -b ${ROOT_DEV} ]]; then
break
fi
count=$((${count} + 1))
count=$((count + 1))
sleep 1
done
@ -41,15 +43,12 @@ msg "Root device is ${ROOT_DEV}"
msg "Data device is ${DATA_DEV}"
FW_DIR=/data/.fwupdate
FW_FILE=firmware.img.gz
FW_FILE_EXTR=firmware.img
FW_FILE=${FW_DIR}/firmware.img.gz
FW_FILE_EXTR=${FW_DIR}/firmware.img
LEGACY_FW_DIR=/data/.firmware_update
LEGACY_FW_FILE_EXTR=firmware
ROOT_INFO_FILE=${FW_DIR}/root_info
ROOT_INFO_FILE=root_info
on_exit() {
cleanup_on_exit() {
msg "Switching to normal boot"
/remove_initramfs
@ -66,7 +65,7 @@ on_exit() {
echo 'b' > /proc/sysrq-trigger
}
trap on_exit EXIT
trap cleanup_on_exit EXIT
if [ -x /prepare_initramfs ]; then
msg "Preparing initramfs"
@ -79,35 +78,39 @@ mount ${BOOT_DEV} /boot
msg "Mounting data partition"
mount ${DATA_DEV} /data
if ! [ -r ${FW_DIR}/${FW_FILE_EXTR} ]; then
if [ -r ${LEGACY_FW_DIR}/${LEGACY_FW_FILE_EXTR} ]; then
msg "Detected legacy firmware path"
mkdir -p ${FW_DIR}
mv ${LEGACY_FW_DIR}/${LEGACY_FW_FILE_EXTR} ${FW_DIR}/${FW_FILE_EXTR}
msg "Computing root partition info"
root_start=$(fdisk -l ${FW_DIR}/${FW_FILE_EXTR} | grep ${FW_FILE_EXTR}2 | tr -s ' ' | cut -d ' ' -f 4)
root_start=$((${root_start} / 2048))
root_size=$(fdisk -l ${FW_DIR}/${FW_FILE_EXTR} | grep ${FW_FILE_EXTR}2 | tr -s ' ' | cut -d ' ' -f 6)
root_size=$((${root_size} / 2048))
echo ${root_start} ${root_size} > ${FW_DIR}/${ROOT_INFO_FILE}
else
msg "No firmware found, aborting"
exit 1
fi
if ! [[ -r ${FW_FILE_EXTR} ]]; then
msg "No firmware found, aborting"
exit 1
fi
if ! [ -r ${FW_DIR}/${ROOT_INFO_FILE} ]; then
if ! [[ -r ${ROOT_INFO_FILE} ]]; then
msg "No root partition info, aborting"
exit 1
fi
msg "Copying root image"
root_start=$(cat ${FW_DIR}/${ROOT_INFO_FILE} | cut -d ' ' -f 1)
root_size=$(cat ${FW_DIR}/${ROOT_INFO_FILE} | cut -d ' ' -f 2)
root_info=$(cat ${ROOT_INFO_FILE})
count=0
dd if=${FW_DIR}/${FW_FILE_EXTR} skip=${root_start} of=${ROOT_DEV} bs=1048576 count=${root_size} || exit 1
for i in ${root_info}; do count=$((count + 1)); done
if [[ ${count} == 3 ]]; then
root_start=$(echo ${root_info} | cut -d ' ' -f 1)
root_size=$(echo ${root_info} | cut -d ' ' -f 3)
root_start=$((root_start / 2048))
root_size=$((root_size / 2048))
elif [[ ${count} == 2 ]]; then
# compatibility with old info file format
root_start=$(echo ${root_info} | cut -d ' ' -f 1)
root_size=$(echo ${root_info} | cut -d ' ' -f 2)
else
msg "Unrecognized root partition info file format"
exit 1
fi
dd if=${FW_FILE_EXTR} skip=${root_start} of=${ROOT_DEV} bs=1048576 count=${root_size} || exit 1
msg "Cleaning up"
rm -rf ${FW_DIR}

View File

@ -1,4 +1,5 @@
#!/bin/bash
cp ${IMG_DIR}/rootfs.cpio.gz ${BOARD_DIR}/initrd.gz
cp ${IMG_DIR}/rootfs.cpio.gz ${BOARD_DIR}/fwupdater.gz # for compatibility

Binary file not shown.