Last active
May 16, 2020 18:20
-
-
Save foundObjects/0645ba25bd3f97ae9ee7f41ce6a4fde1 to your computer and use it in GitHub Desktop.
This is outdated, there's an improved version located here: https://github.com/foundObjects/sbc-flasher friendly/easy sd/usb/emmc image writer with verification and support for compressed images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# flasher.sh -- SBC image writer script with media verification | |
# | |
# source: https://gist.github.com/foundObjects/0645ba25bd3f97ae9ee7f41ce6a4fde1 | |
# contact: Arglebargle @ forum.pine64.org or https://github.com/foundObjects/ | |
# license: http://www.wtfpl.net/txt/copying/ | |
# | |
# usage: flasher.sh (--verify-only) image target | |
__main() { | |
verify_only='' | |
PARAMS=() | |
while (("$#")); do | |
[[ $1 == --*=* ]] || [[ $1 == -?=* ]] && | |
set -- "${1%%=*}" "${1#*=}" "${@:2}" | |
case "$1" in | |
-V | --verify | --verify-only) | |
verify_only='true' | |
shift | |
;; | |
--help) | |
_usage | |
exit | |
;; | |
--) # end argument parsing | |
shift | |
break | |
;; | |
-*) # unsupported flags | |
echo "Error: Unsupported flag $1" >&2 | |
_usage | |
exit 1 | |
;; | |
*) # preserve positional arguments | |
PARAMS+=("$1") | |
shift | |
;; | |
esac | |
done | |
set -- "${PARAMS[@]}" | |
# standard flow: write image, then verify | |
# if --verify-only flag passed then just verify | |
if [[ -r "$1" ]] && [[ -b "$2" ]]; then | |
# $1 is a readable file, $2 is a block device, flags parsed | |
[[ $verify_only ]] || if _write "$1" "$2"; then | |
echo "Write successful" | |
else | |
echo "Write failed" | |
exit 1 | |
fi | |
if _verify "$1" "$2"; then | |
echo "Image verified successfully" | |
else | |
echo "Verify failed" | |
exit 1 | |
fi | |
else | |
_usage | |
exit 1 | |
fi | |
exit 0 | |
} | |
_usage() { echo "Usage: $0 (--verify-only) image target"; } | |
_verify() ( | |
set -euo pipefail &>/dev/null | |
case "$(file -b "$1")" in | |
"DOS/MBR boot sector;"*) | |
echo "Verifying raw image" | |
_size="$(stat -c '%s' "$1")" | |
pv -s "$_size" "$1" | cmp -s -n "$_size" "$2" | |
;; | |
"XZ compressed data") | |
echo "Verifying xz image" | |
_size="$(xz --robot --list "$1" | grep totals | awk '{ print $5 }')" | |
pv -s "$_size" <(xz -T0 -dkqc "$1") | cmp -s -n "$_size" "$2" | |
;; | |
*) | |
echo "Unknown image type" | |
return 1 | |
;; | |
esac | |
) | |
_write() ( | |
set -euo pipefail &>/dev/null | |
case $(file -b "$1") in | |
"DOS/MBR boot sector;"*) | |
echo "writing raw image to $2" | |
dd if="$1" of="$2" bs=4M conv=fsync status=progress | |
;; | |
"XZ compressed data") | |
echo "writing xz compressed image to $2" | |
xz -T0 -dkqc "$1" | dd of="$2" bs=4M conv=fsync status=progress | |
;; | |
*) | |
echo "Unknown image type" | |
return 1 | |
;; | |
esac | |
) | |
__main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment