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,12 +1,14 @@
#!/bin/sh #!/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin PATH=/bin:/sbin:/usr/bin:/usr/sbin
DISK_TIMEOUT="10" DISK_TIMEOUT=10
msg() { msg() {
echo " * $1" echo " * $1"
} }
sh
msg "Mounting pseudo filesystems" msg "Mounting pseudo filesystems"
mount -t devtmpfs devtmpfs /dev mount -t devtmpfs devtmpfs /dev
mount -t proc proc /proc mount -t proc proc /proc
@ -28,10 +30,10 @@ while true; do
if [ ${count} -ge ${DISK_TIMEOUT} ]; then if [ ${count} -ge ${DISK_TIMEOUT} ]; then
break break
fi fi
if [ -b ${ROOT_DEV} ]; then if [[ -b ${ROOT_DEV} ]]; then
break break
fi fi
count=$((${count} + 1)) count=$((count + 1))
sleep 1 sleep 1
done done
@ -41,15 +43,12 @@ msg "Root device is ${ROOT_DEV}"
msg "Data device is ${DATA_DEV}" msg "Data device is ${DATA_DEV}"
FW_DIR=/data/.fwupdate FW_DIR=/data/.fwupdate
FW_FILE=firmware.img.gz FW_FILE=${FW_DIR}/firmware.img.gz
FW_FILE_EXTR=firmware.img FW_FILE_EXTR=${FW_DIR}/firmware.img
LEGACY_FW_DIR=/data/.firmware_update ROOT_INFO_FILE=${FW_DIR}/root_info
LEGACY_FW_FILE_EXTR=firmware
ROOT_INFO_FILE=root_info cleanup_on_exit() {
on_exit() {
msg "Switching to normal boot" msg "Switching to normal boot"
/remove_initramfs /remove_initramfs
@ -66,7 +65,7 @@ on_exit() {
echo 'b' > /proc/sysrq-trigger echo 'b' > /proc/sysrq-trigger
} }
trap on_exit EXIT trap cleanup_on_exit EXIT
if [ -x /prepare_initramfs ]; then if [ -x /prepare_initramfs ]; then
msg "Preparing initramfs" msg "Preparing initramfs"
@ -79,35 +78,39 @@ mount ${BOOT_DEV} /boot
msg "Mounting data partition" msg "Mounting data partition"
mount ${DATA_DEV} /data mount ${DATA_DEV} /data
if ! [ -r ${FW_DIR}/${FW_FILE_EXTR} ]; then if ! [[ -r ${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" msg "No firmware found, aborting"
exit 1 exit 1
fi fi
fi
if ! [ -r ${FW_DIR}/${ROOT_INFO_FILE} ]; then if ! [[ -r ${ROOT_INFO_FILE} ]]; then
msg "No root partition info, aborting" msg "No root partition info, aborting"
exit 1 exit 1
fi fi
msg "Copying root image" msg "Copying root image"
root_start=$(cat ${FW_DIR}/${ROOT_INFO_FILE} | cut -d ' ' -f 1) root_info=$(cat ${ROOT_INFO_FILE})
root_size=$(cat ${FW_DIR}/${ROOT_INFO_FILE} | cut -d ' ' -f 2) 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" msg "Cleaning up"
rm -rf ${FW_DIR} rm -rf ${FW_DIR}

View File

@ -1,4 +1,5 @@
#!/bin/bash #!/bin/bash
cp ${IMG_DIR}/rootfs.cpio.gz ${BOARD_DIR}/initrd.gz 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.