|
| 1 | +#!/bin/sh - |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +REMOTEPROC_DIR="/sys/class/remoteproc/remoteproc0" |
| 6 | +ELF_NAME="arduino.ino.elf" |
| 7 | +ELF_INSTALL_PATH="/lib/firmware/$ELF_NAME" |
| 8 | +INSTALL_PATH="/usr/local/arduino/run_arduino.sh" |
| 9 | +# systemd path should be same as ${systemd_unitdir}/system/ in the yoctdo distro |
| 10 | +SYSTEMD_SERVICE_PATH="/lib/systemd/system/$(basename $INSTALL_PATH .sh).service" |
| 11 | +# Will be defined in autodetect_board() |
| 12 | +BOARD="" |
| 13 | + |
| 14 | +# A pair of prenthesis+percent is used as placeholder. |
| 15 | +### {% BEGINNING OF BINARY PART ### |
| 16 | +ELF_HASH="" |
| 17 | +ELF_BINARY="" |
| 18 | +### END OF BINARY PART %} ### |
| 19 | + |
| 20 | + |
| 21 | +autodetect_board() { |
| 22 | + if [ ! -d /proc/device-tree/ ]; then |
| 23 | + echo "Proc Device tree are not available, Could not detect on which board we are" > /dev/kmsg |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | + |
| 27 | + #search on device tree compatible entry the board type |
| 28 | + if $(grep -q "stm32mp157c-ev" /proc/device-tree/compatible) ; then |
| 29 | + BOARD="STM32MP15_M4_EVAL" |
| 30 | + elif $(grep -q "stm32mp157c-dk" /proc/device-tree/compatible) ; then |
| 31 | + BOARD="STM32MP15_M4_DISCO" |
| 32 | + else |
| 33 | + echo "Board is not an EVAL or a DISCO BOARD" > /dev/kmsg |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +firmware_load() { |
| 40 | + if [ -z "$ELF_BINARY" ]; then |
| 41 | + echo "No Arduino binary contained. Run generate command first." |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | + |
| 45 | + if ( echo "$ELF_HASH $ELF_INSTALL_PATH" | sha256sum --status -c - 2>/dev/null ); then |
| 46 | + # The same firmware already exists, skip this step |
| 47 | + echo "The same firmware is already installed. Starting..." |
| 48 | + return 0 |
| 49 | + fi |
| 50 | + |
| 51 | + # Decode base64-encoded binary to a temp directory and check hash |
| 52 | + tmp_elf_file="/tmp/$ELF_NAME" |
| 53 | + if which uudecode >/dev/null 2>&1; then |
| 54 | + echo -n "$ELF_BINARY" | uudecode -o /dev/stdout | gzip -d > "$tmp_elf_file" |
| 55 | + else |
| 56 | + echo -n "$ELF_BINARY" | tail -n +2 | base64 -d - 2>/dev/null | gzip -d > "$tmp_elf_file" |
| 57 | + fi |
| 58 | + echo "$ELF_HASH $tmp_elf_file" | sha256sum --status -c - |
| 59 | + |
| 60 | + # Copy elf into /lib/firmware |
| 61 | + mv $tmp_elf_file $ELF_INSTALL_PATH |
| 62 | + echo "Arduino: Excutable created: $ELF_INSTALL_PATH" > /dev/kmsg |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +firmware_start() { |
| 67 | + # Change the name of the firmware |
| 68 | + echo -n arduino.ino.elf > $REMOTEPROC_DIR/firmware |
| 69 | + |
| 70 | + # Change path to found firmware |
| 71 | + #echo -n /home/root >/sys/module/firmware_class/parameters/path |
| 72 | + |
| 73 | + # Restart firmware |
| 74 | + echo "Arduino: Starting $ELF_INSTALL_PATH" > /dev/kmsg |
| 75 | + echo start > $REMOTEPROC_DIR/state 2>/dev/null || true |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +firmware_stop() { |
| 80 | + # Stop the firmware |
| 81 | + echo "Arduino: Stoping $ELF_INSTALL_PATH" > /dev/kmsg |
| 82 | + echo stop > $REMOTEPROC_DIR/state 2>/dev/null || true |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +generate_packaged_script() { |
| 87 | + elf_path="$1" |
| 88 | + this_script=$(readlink -f "$0") |
| 89 | + output_script="$2" |
| 90 | + if [ "$this_script" = "$output_script" ]; then |
| 91 | + echo "The output file name must be diffent from this script file" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + |
| 95 | + # Generate a copy of this script with a self-contained elf binary and its hash |
| 96 | + # The elf binary is gzip'ed, making its size to 1/6, and then Base64-encoded |
| 97 | + # using uuencode. |
| 98 | + head -n $(grep -n "{%" "$this_script" | cut -d: -f1 | head -n 1) $this_script > $output_script |
| 99 | + echo "ELF_HASH='$(sha256sum $elf_path | cut -d' ' -f1)'" >> $output_script |
| 100 | + echo -n "ELF_BINARY='" >> $output_script |
| 101 | + if which uuencode >/dev/null 2>&1; then |
| 102 | + gzip -c "$elf_path" | uuencode -m $ELF_NAME >> $output_script |
| 103 | + else |
| 104 | + echo "begin-base64 644 $ELF_NAME" >> $output_script |
| 105 | + gzip -c "$elf_path" | base64 >> $output_script |
| 106 | + fi |
| 107 | + echo "'" >> $output_script |
| 108 | + tail -n +$(grep -n "%}" "$this_script" | cut -d: -f1 | head -n 1) $this_script >> $output_script |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +systemd_install() { |
| 113 | + mkdir -p $(dirname $INSTALL_PATH) |
| 114 | + cp $0 "$INSTALL_PATH" |
| 115 | + echo "File created: $INSTALL_PATH" |
| 116 | + cat > "$SYSTEMD_SERVICE_PATH" << EOF |
| 117 | +[Unit] |
| 118 | +Description=Run Arduino firmware via remoteproc |
| 119 | +After=systemd-modules-load.service |
| 120 | +
|
| 121 | +[Service] |
| 122 | +Type=oneshot |
| 123 | +RemainAfterExit=yes |
| 124 | +ExecStart=sh $INSTALL_PATH start |
| 125 | +ExecStop=sh $INSTALL_PATH stop |
| 126 | +
|
| 127 | +[Install] |
| 128 | +WantedBy=sysinit.target |
| 129 | +EOF |
| 130 | + echo "File created: $SYSTEMD_SERVICE_PATH" |
| 131 | + echo "Please wait until systemd services are reloaded..." |
| 132 | + systemctl daemon-reload |
| 133 | + systemctl enable $(basename $SYSTEMD_SERVICE_PATH) |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +systemd_uninstall() { |
| 138 | + systemctl stop $(basename $SYSTEMD_SERVICE_PATH) |
| 139 | + systemctl disable $(basename $SYSTEMD_SERVICE_PATH) |
| 140 | + rm "$SYSTEMD_SERVICE_PATH" |
| 141 | + echo "File deleted: $SYSTEMD_SERVICE_PATH" |
| 142 | + rm -r $(dirname $INSTALL_PATH) |
| 143 | + echo "File deleted: $INSTALL_PATH" |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +case "$1" in |
| 148 | + start) |
| 149 | + autodetect_board |
| 150 | + firmware_load |
| 151 | + firmware_stop |
| 152 | + firmware_start |
| 153 | + ;; |
| 154 | + stop) |
| 155 | + autodetect_board |
| 156 | + firmware_stop |
| 157 | + ;; |
| 158 | + restart) |
| 159 | + autodetect_board |
| 160 | + firmware_stop |
| 161 | + firmware_start |
| 162 | + ;; |
| 163 | + install) |
| 164 | + autodetect_board |
| 165 | + systemd_install |
| 166 | + echo "Auto-start service $(basename $SYSTEMD_SERVICE_PATH) installed." |
| 167 | + ;; |
| 168 | + uninstall) |
| 169 | + autodetect_board |
| 170 | + systemd_uninstall |
| 171 | + echo "Auto-start service $(basename $SYSTEMD_SERVICE_PATH) uninstalled." |
| 172 | + ;; |
| 173 | + generate) |
| 174 | + generate_packaged_script $2 $3 |
| 175 | + echo "$(readlink -f "$3") generated successfully." |
| 176 | + echo "This file should be uploaded manually by SCP, SFTP, Kermit, or etc." |
| 177 | + echo "Then run \"sh ./$(basename $3) start\" command in the board's console." |
| 178 | + echo "For detailed instructions, please visit:" |
| 179 | + echo " https://github.com/stm32duino/Arduino_Core_STM32/tree/master/variants/STM32MP157_DK/README.md" |
| 180 | + ;; |
| 181 | + *) |
| 182 | + echo "Usage: $0 [start|stop|restart|generate|install|uninstall]" |
| 183 | + echo "" |
| 184 | + echo "run_arduino.sh is a helper script that helps managing an Arduino binary" |
| 185 | + echo "file for the coprocessor using remoteproc framework." |
| 186 | + echo "" |
| 187 | + echo "$0 generate <input ELF file> <output script file>" |
| 188 | + echo " Generate a new shell script file that contains the input ELF binary." |
| 189 | + echo " The contained ELF binary is gzip'ed and Base64-encoded by uuencode." |
| 190 | + echo "" |
| 191 | + echo "$0 start" |
| 192 | + echo " Upload the binary to the coprocessor then start it." |
| 193 | + echo " This command must be excuted while the script contains the binary" |
| 194 | + echo " after generate command is run." |
| 195 | + echo "" |
| 196 | + echo "$0 install" |
| 197 | + echo " Run the binary on boot automatically by installing a systemd service." |
| 198 | + echo "" |
| 199 | + echo "$0 uninstall" |
| 200 | + echo " Uninstall the autostart service." |
| 201 | + echo "" |
| 202 | + echo "$0 stop" |
| 203 | + echo " Stop the coprocessor." |
| 204 | + echo "" |
| 205 | + echo "$0 restart" |
| 206 | + echo " Restart the coprocessor." |
| 207 | + ;; |
| 208 | +esac |
| 209 | + |
| 210 | +exit 0 |
0 commit comments