From 3bb531266298e7a0964581dfd2aa9f6b1190895c Mon Sep 17 00:00:00 2001 From: Mike Engel Date: Mon, 20 Mar 2017 17:07:09 +0100 Subject: [PATCH] meta-digi-dey: Add mdev support into recovery ramdisk. This commit adds mdev support into the recovery ramdisk to mount/unmount storage devices for the firmware up tool. Signed-off-by: Mike Engel https://jira.digi.com/browse/DEL-3692 --- .../images/dey-image-recovery-initramfs.bb | 3 +- .../recovery/recovery-initramfs.bb | 7 ++ .../recovery-initramfs/automount_block.sh | 63 +++++++++++ .../recovery-initramfs/automount_mtd.sh | 51 +++++++++ .../recovery/recovery-initramfs/mdev.conf | 4 + .../recovery-initramfs-init | 102 ++---------------- 6 files changed, 138 insertions(+), 92 deletions(-) create mode 100644 meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_block.sh create mode 100644 meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_mtd.sh create mode 100644 meta-digi-dey/recipes-core/recovery/recovery-initramfs/mdev.conf diff --git a/meta-digi-dey/recipes-core/images/dey-image-recovery-initramfs.bb b/meta-digi-dey/recipes-core/images/dey-image-recovery-initramfs.bb index 9864a4557..bd484673d 100644 --- a/meta-digi-dey/recipes-core/images/dey-image-recovery-initramfs.bb +++ b/meta-digi-dey/recipes-core/images/dey-image-recovery-initramfs.bb @@ -5,6 +5,7 @@ LICENSE = "MIT" PACKAGE_INSTALL = " \ busybox \ + parted \ psplash \ recovery-initramfs \ swupdate \ @@ -13,7 +14,7 @@ PACKAGE_INSTALL = " \ wipe \ " -PACKAGE_INSTALL_append_ccimx6 = " e2fsprogs-mke2fs parted" +PACKAGE_INSTALL_append_ccimx6 = " e2fsprogs-mke2fs" PACKAGE_INSTALL_append_ccimx6ul = " mtd-utils-ubifs" # Do not pollute the initrd image with rootfs features diff --git a/meta-digi-dey/recipes-core/recovery/recovery-initramfs.bb b/meta-digi-dey/recipes-core/recovery/recovery-initramfs.bb index c6eea48e3..fb88dba95 100644 --- a/meta-digi-dey/recipes-core/recovery/recovery-initramfs.bb +++ b/meta-digi-dey/recipes-core/recovery/recovery-initramfs.bb @@ -13,6 +13,9 @@ DEPENDS += "${@base_conditional('TRUSTFENCE_SIGN', '1', 'virtual/kernel openssl- SRC_URI = " \ file://recovery-initramfs-init \ file://swupdate.cfg \ + file://automount_block.sh \ + file://automount_mtd.sh \ + file://mdev.conf \ " S = "${WORKDIR}" @@ -21,6 +24,10 @@ do_install() { install -d ${D}${sysconfdir} install -m 0755 ${WORKDIR}/recovery-initramfs-init ${D}/init install -m 0644 ${WORKDIR}/swupdate.cfg ${D}${sysconfdir} + install -d ${D}${base_libdir}/mdev + install -m 0755 ${WORKDIR}/automount_block.sh ${D}${base_libdir}/mdev/automount_block.sh + install -m 0755 ${WORKDIR}/automount_mtd.sh ${D}${base_libdir}/mdev/automount_mtd.sh + install -m 0644 ${WORKDIR}/mdev.conf ${D}${sysconfdir} # If Trustfence is enabled, copy the public key that is going to be used into the # initramfs '/etc/ssl/certs' folder in order to verify swupdate packages. diff --git a/meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_block.sh b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_block.sh new file mode 100644 index 000000000..1d3466b4b --- /dev/null +++ b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_block.sh @@ -0,0 +1,63 @@ +#!/bin/sh +# +# Copyright (c) 2017, Digi International Inc. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, you can obtain one at http://mozilla.org/MPL/2.0/. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +MDEV_AUTOMOUNT_ROOT="/run/media" +UPDATE_MOUNTPOINT="/mnt/update" + +DEVICE="$(echo "${MDEV}" | sed -n -e '/^mmc/{s,^\([^p]\+\)p[0-9]\+$,\1,g;T;p}' -e '/^sd/{s,^\([^0-9]\+\)[0-9]\+$,\1,g;T;p}')" +PARTITION="$(echo "${MDEV}" | sed -n -e '/^mmc/{s,^[^p]\+p\([0-9]\+\)$,\1,g;T;p}' -e '/^sd/{s,^[^0-9]\+\([0-9]\+\)$,\1,g;T;p}')" + +# This will detect if the block device has a update partition +is_update_device() { + parted -s "/dev/${DEVICE}" print | grep -qs update +} + +# This will verify that the requested partition is the update partition +is_update_partition() { + parted -s "/dev/${DEVICE}" print | sed -ne "s,^[^0-9]*\([0-9]\+\).*\.*,\1,g;T;p" | grep -qs "${PARTITION}" +} + +if is_update_device; then + if is_update_partition; then + if mkdir -p ${UPDATE_MOUNTPOINT} && ! mountpoint -q ${UPDATE_MOUNTPOINT}; then + FSTYPE="$(blkid /dev/${MDEV} | sed -e 's,.*TYPE="\([^"]\+\)".*,\1,g')" + if ! mount ${FSTYPE:+-t ${FSTYPE}} "/dev/${MDEV}" "${UPDATE_MOUNTPOINT}"; then + rmdir --ignore-fail-on-non-empty ${UPDATE_MOUNTPOINT} + fi + fi + fi + # If it's 'update' device but not partition, just exit + exit 0 +fi + +case "${ACTION}" in +add) + # Create mountpoint and mount the mmc device + if mkdir -p ${MDEV_AUTOMOUNT_ROOT}/${MDEV} && ! mountpoint -q ${MDEV_AUTOMOUNT_ROOT}/${MDEV}; then + FSTYPE="$(blkid /dev/${MDEV} | sed -e 's,.*TYPE="\([^"]\+\)".*,\1,g')" + mount -r ${FSTYPE:+-t ${FSTYPE}} /dev/${MDEV} ${MDEV_AUTOMOUNT_ROOT}/${MDEV} + fi + ;; +remove) + # Umount and then remove mountpoint + if grep -q "/dev/${MDEV}[[:blank:]]" /proc/mounts; then + mdir=$(sed -ne "s,/dev/${MDEV}[[:blank:]]\+\([^[:blank:]]\+\)[[:blank:]].*,\1,g;T;p" /proc/mounts) + umount "${mdir}" + rmdir -- "${mdir}" 2>/dev/null + fi + ;; +esac diff --git a/meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_mtd.sh b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_mtd.sh new file mode 100644 index 000000000..ed1be95ba --- /dev/null +++ b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/automount_mtd.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# +# Copyright (c) 2017, Digi International Inc. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, you can obtain one at http://mozilla.org/MPL/2.0/. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +UPDATE_MOUNTPOINT="/mnt/update" +PARTITION_NAME="update" + +# This will detect if the block device has a update partition +is_update_device() { + grep -qs update /proc/mtd +} + +# This will verify that the requested partition is the update partition +is_update_partition() { + grep -qs "^${MDEV}:.*\.*" /proc/mtd +} + +if is_update_device; then + if is_update_partition; then + # Attach and get UBI device number + dev_number="$(ubiattach -p /dev/${MDEV} 2>/dev/null | sed -ne 's,.*device number \([0-9]\).*,\1,g;T;p' 2>/dev/null)" + # Check if volume exists. + if ubinfo "/dev/ubi${dev_number}" -N "${PARTITION_NAME}" >/dev/null 2>&1; then + if mkdir -p ${UPDATE_MOUNTPOINT} && ! mountpoint -q ${UPDATE_MOUNTPOINT}; then + # Mount the volume. + if ! mount -t ubifs "ubi${dev_number}:${PARTITION_NAME}" "${UPDATE_MOUNTPOINT}"; then + echo "ERROR: Could not mount '${PARTITION_NAME}' partition" + rmdir --ignore-fail-on-non-empty ${UPDATE_MOUNTPOINT} + fi + fi + else + echo "ERROR: Could not mount '${PARTITION_NAME}' partition, volume not found" + rmdir --ignore-fail-on-non-empty ${UPDATE_MOUNTPOINT} + fi + fi + # If it's 'update' device but not partition, just exit + exit 0 +fi diff --git a/meta-digi-dey/recipes-core/recovery/recovery-initramfs/mdev.conf b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/mdev.conf new file mode 100644 index 000000000..20f8a2014 --- /dev/null +++ b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/mdev.conf @@ -0,0 +1,4 @@ +# block devices +sd[a-z][0-9]+ 0:0 660 */lib/mdev/automount_block.sh +mmcblk[0-9]+p[0-9]+ 0:0 660 */lib/mdev/automount_block.sh +mtd[0-9]+ 0:0 660 */lib/mdev/automount_mtd.sh diff --git a/meta-digi-dey/recipes-core/recovery/recovery-initramfs/recovery-initramfs-init b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/recovery-initramfs-init index 64f492dc8..882059b47 100644 --- a/meta-digi-dey/recipes-core/recovery/recovery-initramfs/recovery-initramfs-init +++ b/meta-digi-dey/recipes-core/recovery/recovery-initramfs/recovery-initramfs-init @@ -162,91 +162,6 @@ is_nand() { fi } -#------------------------------------------------------------------------------ -# Function - mount_external_disks -# -# Mounts all available external disks. -#------------------------------------------------------------------------------ -mount_external_disks() { - local devices=$(ls -1 /dev/sd? 2>/dev/null) - for device in ${devices}; do - for i in ${device}?; do - local dev_name=$(basename "${i}") - local mount_dir="${USB_MOUNT_DIR}/${dev_name}" - mkdir -p "${mount_dir}" - FSTYPE="$(blkid /dev/${dev_name} | sed -e 's,.*TYPE="\([^"]\+\)".*,\1,g')" - mount -r ${FSTYPE:+-t ${FSTYPE}} "/dev/${dev_name}" "${mount_dir}" - done - done -} - -#------------------------------------------------------------------------------ -# Function - mount_partition -# -# Mounts the given partition. -# -# @param ${1} - Partition name to mount. -# @param ${2} - Mount point. -#------------------------------------------------------------------------------ -mount_partition() { - if [ "$(is_nand)" = "yes" ]; then - mount_ubi_volume "${1}" "${2}" - else - mount_emmc_block "${1}" "${2}" - fi -} - -#------------------------------------------------------------------------------ -# Function - mount_ubi_volume -# -# Mounts the given UBI volume. -# -# @param ${1} - UBI Volume name to mount. -# @param ${2} - Mount point. -#------------------------------------------------------------------------------ -mount_ubi_volume() { - # Find the MTD partition. - local mtd_num="$(sed -ne "s/mtd\([0-9]\+\):.*\<${1}\>.*/\1/g;T;p" /proc/mtd 2>/dev/null)" - if [ -z "${mtd_num}" ]; then - log_warning "Could not find MTD partition for volume '${1}'" - else - # Attach and get UBI device number - local dev_number="$(ubiattach -p /dev/mtd${mtd_num} 2>/dev/null | sed -ne 's,.*device number \([0-9]\).*,\1,g;T;p' 2>/dev/null)" - # Check if volume exists. - ubinfo "/dev/ubi${dev_number}" -N "${1}" >/dev/null 2>&1 - if [ "$?" = "0" ]; then - # Mount the volume. - mkdir -p "${2}" - mount -t ubifs "ubi${dev_number}:${1}" "${2}" >/dev/null 2>&1 - if [ "$?" != "0" ]; then - log_warning "Could not mount '${1}' partition" - fi - else - log_warning "Could not mount '${1}' partition, volume not found" - fi - fi -} - -#------------------------------------------------------------------------------ -# Function - mount_emmc_block -# -# Mounts the given emmc partition block name. -# -# @param ${1} - Partition name to mount. -# @param ${2} - Mount point. -#------------------------------------------------------------------------------ -mount_emmc_block() { - # Find partition block number. - local partition_block="/dev/mmcblk0p$(parted -s /dev/mmcblk0 print | sed -ne "s,^[^0-9]*\([0-9]\+\).*\<${1}\>.*,\1,g;T;p")" - # Mount the volume. - mkdir -p "${2}" - FSTYPE="$(blkid ${partition_block} | sed -e 's,.*TYPE="\([^"]\+\)".*,\1,g')" - mount -r ${FSTYPE:+-t ${FSTYPE}} "${partition_block}" "${2}" - if [ "$?" != "0" ]; then - log_warning "Could not mount '${1}' partition (${partition_block})" - fi -} - #------------------------------------------------------------------------------ # Function - format_partition # @@ -411,6 +326,17 @@ mount -t sysfs sysfs /sys mount -t devtmpfs devtmpfs /dev mount -t tmpfs tmpfs /tmp +# Register mdev as device manager +if [ -f /proc/sys/kernel/hotplug ]; then + echo > /dev/mdev.seq + echo > /dev/mdev.log + echo "/sbin/mdev" > /proc/sys/kernel/hotplug + mdev -s +fi + +# Give some time for the devices to settle down +sleep 2 + # Setup fw_printenv. mkdir -p /var/lock @@ -502,9 +428,6 @@ if [ -n "${update_package_bool}" ]; then if [ -z "${update_package}" ]; then quit_with_error "Firmware update package not specified" else - # Give some time for the devices to settle down - sleep 5 - # Format the UBI volume before updating. if [ "$(is_nand)" = "yes" ]; then psplash_message "Formatting rootfs partition..." @@ -512,9 +435,6 @@ if [ -n "${update_package_bool}" ]; then psplash_progress "0" fi - mount_external_disks - mount_partition update "${UPDATE_MOUNT_DIR}" - # Check whether the package is local and get the absolute path if echo "${update_package}" | grep -qs '^file://'; then update_package="$(swu_package_path $(basename ${update_package}))"