|
| 1 | +#!/bin/sh - |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +REMOTEPROC_DIR="/sys/class/remoteproc/remoteproc0" |
| 6 | +RPMSG_DIR="/dev/ttyRPMSG0" |
| 7 | +ELF_NAME="arduino.ino.elf" |
| 8 | +ELF_INSTALL_PATH="/lib/firmware/$ELF_NAME" |
| 9 | +INSTALL_PATH="/usr/local/arduino/run_arduino.sh" |
| 10 | +# systemd path should be same as ${systemd_unitdir}/system/ in the yocto distro |
| 11 | +SYSTEMD_SERVICE_PATH="/lib/systemd/system/$(basename $INSTALL_PATH .sh).service" |
| 12 | +# Will be defined in autodetect_board() |
| 13 | +BOARD="" |
| 14 | + |
| 15 | +# A pair of prenthesis+percent is used as placeholder. |
| 16 | +### {% BEGINNING OF BINARY PART ### |
| 17 | +ELF_HASH="" |
| 18 | +ELF_BINARY="" |
| 19 | +### END OF BINARY PART %} ### |
| 20 | + |
| 21 | + |
| 22 | +autodetect_board() { |
| 23 | + if [ ! -d /proc/device-tree/ ]; then |
| 24 | + echo "Proc Device tree are not available, Could not detect on which board we are" > /dev/kmsg |
| 25 | + exit 1 |
| 26 | + fi |
| 27 | + |
| 28 | + #search on device tree compatible entry the board type |
| 29 | + if $(grep -q "stm32mp157c-ev" /proc/device-tree/compatible) ; then |
| 30 | + BOARD="STM32MP157_EVAL" |
| 31 | + elif $(grep -q "stm32mp157c-dk" /proc/device-tree/compatible) ; then |
| 32 | + BOARD="STM32MP157_DK" |
| 33 | + elif $(grep -q "stm32mp157a-dk" /proc/device-tree/compatible) ; then |
| 34 | + BOARD="STM32MP157_DK" |
| 35 | + elif $(grep -q "stm32mp157" /proc/device-tree/compatible) ; then |
| 36 | + BOARD="STM32MP157_GENERIC" |
| 37 | + else |
| 38 | + echo "Board is not an STM32MP157 BOARD" > /dev/kmsg |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +firmware_load() { |
| 45 | + if [ -z "$ELF_BINARY" ]; then |
| 46 | + echo "No Arduino binary contained. Run generate command first." |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + |
| 50 | + if ( echo "$ELF_HASH $ELF_INSTALL_PATH" | sha256sum --status -c - 2>/dev/null ); then |
| 51 | + # The same firmware already exists, skip this step |
| 52 | + echo "The same firmware is already installed. Starting..." |
| 53 | + return 0 |
| 54 | + fi |
| 55 | + |
| 56 | + # Decode base64-encoded binary to a temp directory and check hash |
| 57 | + tmp_elf_file="/tmp/$ELF_NAME" |
| 58 | + if which uudecode >/dev/null 2>&1; then |
| 59 | + echo -n "$ELF_BINARY" | uudecode -o /dev/stdout | gzip -d > "$tmp_elf_file" |
| 60 | + else |
| 61 | + echo -n "$ELF_BINARY" | tail -n +2 | base64 -d - 2>/dev/null | gzip -d > "$tmp_elf_file" |
| 62 | + fi |
| 63 | + echo "$ELF_HASH $tmp_elf_file" | sha256sum --status -c - |
| 64 | + |
| 65 | + # Copy elf into /lib/firmware |
| 66 | + mv $tmp_elf_file $ELF_INSTALL_PATH |
| 67 | + echo "Arduino: Executable created: $ELF_INSTALL_PATH" > /dev/kmsg |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +firmware_start() { |
| 72 | + # Change the name of the firmware |
| 73 | + echo -n arduino.ino.elf > $REMOTEPROC_DIR/firmware |
| 74 | + |
| 75 | + # Change path to found firmware |
| 76 | + #echo -n /home/root >/sys/module/firmware_class/parameters/path |
| 77 | + |
| 78 | + # Restart firmware |
| 79 | + echo "Arduino: Starting $ELF_INSTALL_PATH" > /dev/kmsg |
| 80 | + echo start > $REMOTEPROC_DIR/state 2>/dev/null || true |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +firmware_stop() { |
| 85 | + # Stop the firmware |
| 86 | + echo "Arduino: Stopping $ELF_INSTALL_PATH" > /dev/kmsg |
| 87 | + echo stop > $REMOTEPROC_DIR/state 2>/dev/null || true |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +generate_packaged_script() { |
| 92 | + elf_path="$1" |
| 93 | + this_script=$(readlink -f "$0") |
| 94 | + output_script="$2" |
| 95 | + if [ "$this_script" = "$output_script" ]; then |
| 96 | + echo "The output file name must be diffent from this script file" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + # Generate a copy of this script with a self-contained elf binary and its hash |
| 101 | + # The elf binary is gzip'ed, making its size to 1/6, and then Base64-encoded |
| 102 | + # using uuencode. |
| 103 | + head -n $(grep -n "{%" "$this_script" | cut -d: -f1 | head -n 1) $this_script > $output_script |
| 104 | + echo "ELF_HASH='$(sha256sum $elf_path | cut -d' ' -f1)'" >> $output_script |
| 105 | + echo -n "ELF_BINARY='" >> $output_script |
| 106 | + if which uuencode >/dev/null 2>&1; then |
| 107 | + gzip -c "$elf_path" | uuencode -m $ELF_NAME >> $output_script |
| 108 | + else |
| 109 | + echo "begin-base64 644 $ELF_NAME" >> $output_script |
| 110 | + gzip -c "$elf_path" | base64 >> $output_script |
| 111 | + fi |
| 112 | + echo "'" >> $output_script |
| 113 | + tail -n +$(grep -n "%}" "$this_script" | cut -d: -f1 | head -n 1) $this_script >> $output_script |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +systemd_install() { |
| 118 | + mkdir -p $(dirname $INSTALL_PATH) |
| 119 | + cp $0 "$INSTALL_PATH" |
| 120 | + echo "File created: $INSTALL_PATH" |
| 121 | + cat > "$SYSTEMD_SERVICE_PATH" << EOF |
| 122 | +[Unit] |
| 123 | +Description=Run Arduino firmware via remoteproc |
| 124 | +After=systemd-modules-load.service |
| 125 | +
|
| 126 | +[Service] |
| 127 | +Type=oneshot |
| 128 | +RemainAfterExit=yes |
| 129 | +ExecStart=sh $INSTALL_PATH start |
| 130 | +ExecStop=sh $INSTALL_PATH stop |
| 131 | +
|
| 132 | +[Install] |
| 133 | +WantedBy=sysinit.target |
| 134 | +EOF |
| 135 | + echo "File created: $SYSTEMD_SERVICE_PATH" |
| 136 | + echo "Please wait until systemd services are reloaded..." |
| 137 | + systemctl daemon-reload |
| 138 | + systemctl enable $(basename $SYSTEMD_SERVICE_PATH) |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +systemd_uninstall() { |
| 143 | + systemctl stop $(basename $SYSTEMD_SERVICE_PATH) |
| 144 | + systemctl disable $(basename $SYSTEMD_SERVICE_PATH) |
| 145 | + rm "$SYSTEMD_SERVICE_PATH" |
| 146 | + echo "File deleted: $SYSTEMD_SERVICE_PATH" |
| 147 | + rm -r $(dirname $INSTALL_PATH) |
| 148 | + echo "File deleted: $INSTALL_PATH" |
| 149 | +} |
| 150 | + |
| 151 | +try_send() { |
| 152 | + # Wait for /dev/ttyRPMSGx for 5 seconds, because the virtual device can be |
| 153 | + # created later depending on where Serial.begin() is located in the Arduino code. |
| 154 | + count=0 |
| 155 | + while [ ! -c $RPMSG_DIR ]; do |
| 156 | + if [ $count -eq 2 ]; then |
| 157 | + echo "Waiting for virtual serial $RPMSG_DIR is created..." |
| 158 | + elif [ $count -ge 5 ]; then |
| 159 | + echo "No virtual serial $RPMSG_DIR is created." |
| 160 | + echo "If you didn't enable the virtual serial, ignore this message." |
| 161 | + return 0 |
| 162 | + fi |
| 163 | + sleep 1; |
| 164 | + count=$(expr $count + 1) |
| 165 | + done |
| 166 | + # Linux host must send any dummy data first to finish initialization of rpmsg |
| 167 | + # on the coprocessor side. This message should be discarded. |
| 168 | + # See: https://github.com/OpenAMP/open-amp/issues/182 |
| 169 | + echo "DUMMY" >$RPMSG_DIR |
| 170 | + echo "Virtual serial $RPMSG_DIR connection established." |
| 171 | +} |
| 172 | + |
| 173 | +case "$1" in |
| 174 | + start) |
| 175 | + autodetect_board |
| 176 | + firmware_load |
| 177 | + firmware_stop |
| 178 | + firmware_start |
| 179 | + try_send |
| 180 | + echo "Arduino firmware started." |
| 181 | + ;; |
| 182 | + stop) |
| 183 | + autodetect_board |
| 184 | + firmware_stop |
| 185 | + echo "Arduino firmware stopped." |
| 186 | + ;; |
| 187 | + restart) |
| 188 | + autodetect_board |
| 189 | + firmware_stop |
| 190 | + firmware_start |
| 191 | + try_send |
| 192 | + echo "Arduino firmware restarted." |
| 193 | + ;; |
| 194 | + install) |
| 195 | + autodetect_board |
| 196 | + systemd_install |
| 197 | + echo "Auto-start service $(basename $SYSTEMD_SERVICE_PATH) installed." |
| 198 | + ;; |
| 199 | + uninstall) |
| 200 | + autodetect_board |
| 201 | + systemd_uninstall |
| 202 | + echo "Auto-start service $(basename $SYSTEMD_SERVICE_PATH) uninstalled." |
| 203 | + ;; |
| 204 | + monitor) |
| 205 | + autodetect_board |
| 206 | + stty igncr onlcr -echo -F $RPMSG_DIR |
| 207 | + cat $RPMSG_DIR |
| 208 | + ;; |
| 209 | + send-msg) |
| 210 | + autodetect_board |
| 211 | + echo "${@:2}" >$RPMSG_DIR |
| 212 | + ;; |
| 213 | + send-file) |
| 214 | + autodetect_board |
| 215 | + dd if="$2" of=$RPMSG_DIR |
| 216 | + ;; |
| 217 | + minicom) |
| 218 | + autodetect_board |
| 219 | + TERM=xterm minicom -D $RPMSG_DIR |
| 220 | + ;; |
| 221 | + generate) |
| 222 | + generate_packaged_script $2 $3 |
| 223 | + echo "$(readlink -f "$3") generated successfully." |
| 224 | + echo "This file should be uploaded manually by SCP, SFTP, Kermit, or etc." |
| 225 | + echo "Then run \"sh ./$(basename $3) start\" command in the board's console." |
| 226 | + echo "For detailed instructions, please visit:" |
| 227 | + echo " https://github.com/stm32duino/Arduino_Core_STM32/tree/master/variants/STM32MP157_DK/README.md" |
| 228 | + ;; |
| 229 | + *) |
| 230 | + echo "Usage: $0 [start|stop|restart]" |
| 231 | + echo " $0 [install|uninstall]" |
| 232 | + echo " $0 [monitor|send-msg|send-file|minicom]" |
| 233 | + echo " $0 [generate]" |
| 234 | + echo "" |
| 235 | + echo "$0 is a helper script that helps managing an Arduino binary" |
| 236 | + echo "file for the coprocessor using remoteproc framework." |
| 237 | + echo "" |
| 238 | + echo "$0 generate <input ELF file> <output script file>" |
| 239 | + echo " For Arduino IDE internal use only." |
| 240 | + echo " Generate a new shell script file that contains the input ELF binary." |
| 241 | + echo " The contained ELF binary is gzip'ed and Base64-encoded by uuencode." |
| 242 | + echo "" |
| 243 | + echo "$0 start" |
| 244 | + echo " Upload the binary to the coprocessor then start it." |
| 245 | + echo " This command must be executed while the script contains the binary" |
| 246 | + echo " after generate command is run." |
| 247 | + echo "" |
| 248 | + echo "$0 install" |
| 249 | + echo " Run the binary on boot automatically by installing a systemd service." |
| 250 | + echo "" |
| 251 | + echo "$0 uninstall" |
| 252 | + echo " Uninstall the autostart service." |
| 253 | + echo "" |
| 254 | + echo "$0 monitor" |
| 255 | + echo " Monitor data received from the coprocessor via RPMsg serial (VirtIOSerial)." |
| 256 | + echo " This command cannot send any data to the coprocessor." |
| 257 | + echo "" |
| 258 | + echo "$0 send-msg <message...>" |
| 259 | + echo " Send a message to the coprocessor via RPMsg serial (VirtIOSerial)." |
| 260 | + echo "" |
| 261 | + echo "$0 send-file <filename>" |
| 262 | + echo " Send a file content to the coprocessor via RPMsg serial (VirtIOSerial)." |
| 263 | + echo "" |
| 264 | + echo "$0 minicom" |
| 265 | + echo " Launch minicom interactive serial communication program." |
| 266 | + echo "" |
| 267 | + echo "$0 stop" |
| 268 | + echo " Stop the coprocessor." |
| 269 | + echo "" |
| 270 | + echo "$0 restart" |
| 271 | + echo " Restart the coprocessor." |
| 272 | + ;; |
| 273 | +esac |
| 274 | + |
| 275 | +exit 0 |
0 commit comments