-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.sh
executable file
·93 lines (83 loc) · 3.11 KB
/
upload.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
#
# Upload new firmware to a device running ssh_subsystem_fwup
#
# Usage:
# upload.sh [destination IP] [Path to .fw file]
#
# If unspecified, the destination is nerves.local and the .fw file is naively
# guessed
#
# You may want to add the following to your `~/.ssh/config` to avoid recording
# the IP addresses of the target:
#
# Host nerves.local
# UserKnownHostsFile /dev/null
# StrictHostKeyChecking no
#
# The firmware update protocol is:
#
# 1. Connect to the ssh_subsystem_fwup service running on port 22
# 2. Send the firmware file
# 3. The response from the device is a progress bar from fwup that can either
# be ignored or shown to the user.
# 4. The ssh connection is closed with an exit code to indicate success or
# failure
#
# Feel free to copy this script wherever is convenient. The template is at
# https://github.com/nerves-project/ssh_subsystem_fwup/blob/main/priv/templates/script.upload.eex
#
set -e
DESTINATION=$1
FILENAME="$2"
help() {
echo
echo "upload.sh [destination IP] [Path to .fw file]"
echo
echo "Default destination IP is 'nerves.local'"
echo "Default firmware bundle is the first .fw file in '_build/\${MIX_TARGET}_\${MIX_ENV}/nerves/images'"
echo
echo "MIX_TARGET=$MIX_TARGET"
echo "MIX_ENV=$MIX_ENV"
exit 1
}
[ -n "$DESTINATION" ] || DESTINATION=nerves.local
if [ -z "$FILENAME" ]; then
[ -n "$MIX_TARGET" ] || MIX_TARGET=rpi0
[ -n "$MIX_ENV" ] || MIX_ENV=dev
FIRMWARE_PATH="./_build/${MIX_TARGET}_${MIX_ENV}/nerves/images"
if [ ! -d "$FIRMWARE_PATH" ]; then
echo "Can't find the build products."
echo
echo "Nerves environment"
echo "MIX_TARGET: ${MIX_TARGET}"
echo "MIX_ENV: ${MIX_ENV}"
echo
echo "Make sure your Nerves environment is correct."
echo
echo "If the Nerves environment is correct make sure you have built the firmware"
echo "using 'mix firmware'."
echo
echo "If you are uploading a .fw file from a custom path you can specify the"
echo "path like so:"
echo
echo " $0 <device hostname or IP address> </path/to/my/firmware.fw>"
echo
exit 1
fi
FILENAME=$(ls "$FIRMWARE_PATH/"*.fw 2> /dev/null | head -n 1)
[ -n "$FILENAME" ] || (echo "Error: error determining firmware bundle."; help)
fi
[ -f "$FILENAME" ] || (echo "Error: can't find '$FILENAME'"; help)
FIRMWARE_METADATA=$(fwup -m -i "$FILENAME" || echo "meta-product=Error reading metadata!")
FIRMWARE_PRODUCT=$(echo "$FIRMWARE_METADATA" | grep -E "^meta-product=" -m 1 2>/dev/null | cut -d '=' -f 2- | tr -d '"')
FIRMWARE_VERSION=$(echo "$FIRMWARE_METADATA" | grep -E "^meta-version=" -m 1 2>/dev/null | cut -d '=' -f 2- | tr -d '"')
FIRMWARE_PLATFORM=$(echo "$FIRMWARE_METADATA" | grep -E "^meta-platform=" -m 1 2>/dev/null | cut -d '=' -f 2- | tr -d '"')
FIRMWARE_UUID=$(echo "$FIRMWARE_METADATA" | grep -E "^meta-uuid=" -m 1 2>/dev/null | cut -d '=' -f 2- | tr -d '"')
echo "Path: $FILENAME"
echo "Product: $FIRMWARE_PRODUCT $FIRMWARE_VERSION"
echo "UUID: $FIRMWARE_UUID"
echo "Platform: $FIRMWARE_PLATFORM"
echo
echo "Uploading to $DESTINATION..."
cat "$FILENAME" | ssh -s $SSH_OPTIONS $DESTINATION fwup