Skip to content

Instantly share code, notes, and snippets.

@evadot
Last active March 27, 2020 03:24
Show Gist options
  • Save evadot/1bcad7c4c1e5cc7f9a692f4d19ab5421 to your computer and use it in GitHub Desktop.
Save evadot/1bcad7c4c1e5cc7f9a692f4d19ab5421 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
#
# Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Defaults
LOCALDIR=/usr/local/
BOOTSCR=0
SLICE=1
usage() {
cat <<EOF
Usage: `basename $0` [options] boardname device,directory
Options:
-b -- Also install u-boot script boot.scr
-p slice -- Slice number for the FAT partition
(default: 1)
-d localdir -- Use a different local dir
(default: /usr/local)
EOF
exit 1
}
check_var() {
if [ -z "${1}" ]; then
echo "${2}"
exit 1
fi
}
install_raw() {
dd if=$1 of=$2 bs=$3 seek=$4 conv=sync,notrunc > /dev/null 2>&1
}
install_file() {
for i in $2; do
cp $1/${i} $3
done
}
install_file_dev() {
echo "${TARGET} is a device, attempting to mount the first partition as a FAT filesystem"
scheme=$(gpart show "${TARGET}" | head -n 1 | awk '{print $5}')
if [ "${scheme}" == "MBR" ]; then
suffix="s${SLICE}"
elif [ "${scheme}" == "GPT" ]; then
suffix="p${SLICE}"
else
echo "Cannot guess partition scheme for ${TARGET}"
exit 1
fi
dir=$(mktemp -d)
mount_msdosfs ${TARGET}${suffix} $dir
if [ $? -ne 0 ]; then
echo "Mounting as FAT failed, aborting"
rmdir $dir
exit 1
fi
install_file $1 "$2" ${dir}
umount $dir
rmdir $dir
}
while [ $# -gt 2 ]; do
case $1 in
-d)
shift
LOCALDIR=$1
shift
;;
-b)
BOOTSCR=1
shift
;;
-p)
shift
SLICE=$1
shift
;;
esac
done
if [ $# -lt 2 ]; then
usage
fi
BOARD_NAME=$1
TARGET=$2
if [ ! -d ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME} ]; then
echo "No u-boot ports for ${BOARD_NAME}, try pkg install u-boot-${BOARD_NAME}"
exit 1
fi
if [ ! -f ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME}/metadata ]; then
echo "Package ${BOARD_NAME} doesn't provide any metadata file"
exit 1
fi
. ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME}/metadata
check_var "${METHOD}" "METHOD is mandatory in metadata"
check_var "${FILES}" "FILES is mandatory in metadata"
user_id=$(id -u)
if [ -c "${TARGET}" ]; then
TARGET_TYPE="device"
elif [ -f "${TARGET}" ]; then
TARGET_TYPE="file"
elif [ -d "${TARGET}" ]; then
TARGET_TYPE="dir"
else
echo "Cannot guess the target type, aborting"
exit 1
fi
# First install the u-boot proper
if [ ${METHOD} = "uboot-raw" ]; then
if [ ${TARGET_TYPE} != "file" ] && [ "${TARGET_TYPE}" != "device" ]; then
echo "raw method need a file or device"
exit 1
fi
if [ "${TARGET_TYPE}" == "device" ] && [ "${user_id}" -ne 0 ]; then
echo "raw method on a device needs root permission"
exit 1
fi
check_var "${OFFSET}" "OFFSET is mandatory for raw method"
check_var "${BS}" "BS is mandatory for raw method"
install_raw ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME}/$FILES $TARGET $BS $OFFSET
elif [ ${METHOD} = "uboot-file" ]; then
if [ "${TARGET_TYPE}" == "device" ]; then
install_file_dev ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME} "${FILES}"
elif [ "${TARGET_TYPE}" == "dir" ]; then
install_file ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME} "${FILES}" ${TARGET}
else
echo "file method need a directory or a device"
exit 1
fi
else
echo "Unsupported method ${METHOD}"
exit 1
fi
# Then if we need too, install the u-boot script
if [ "${BOOTSCR}" == 1 ]; then
if [ "${TARGET_TYPE}" == "device" ]; then
install_file_dev ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME} boot.scr
elif [ "${TARGET_TYPE}" == "dir" ]; then
install_file ${LOCALDIR}/share/u-boot/u-boot-${BOARD_NAME} boot.scr ${TARGET}
else
echo "Don't know how to install boot.scr on ${TARGET}"
exit 1
fi
fi
@bsdimp
Copy link

bsdimp commented Jul 23, 2018

So there's a couple of minor issues with this.
(1) for the 'files' case, the 'target' device should be the whole disk, not the FAT partition to match the 'raw' case.
(2) for files, your error message should say you need a device or a directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment