mcu: simplify and fix 'mcu.sh' and 'mcu.hotplug' scripts

This reduces amount of helper functions and fixes also global variables
handle inside 'mcu.sh' and 'mcu.hotplug' shell scripts. While at it,
provide additional debug information when fetching images list and
system information.

Signed-off-by: Piotr Dymacz <pepe2k@gmail.com>
This commit is contained in:
Piotr Dymacz
2023-03-10 09:38:46 +01:00
committed by John Crispin
parent 725899248a
commit b9d20e083c
3 changed files with 102 additions and 214 deletions

View File

@@ -8,7 +8,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=mcu
PKG_VERSION:=2023-03-09
PKG_VERSION:=2023-03-10
PKG_RELEASE=1
PKG_LICENSE:=GPL-2.0

View File

@@ -15,6 +15,9 @@ mcu_hotplug_setup() {
SECT="$1"
MCU_SYSINFO_OUTPUT=""
MCU_IMGLIST_OUTPUT=""
[ "$MCU_CFG_FOUND" = "1" ] && return 0
config_get sn "$SECT" sn
@@ -58,9 +61,6 @@ mcu_hotplug_setup() {
mcu_logi "found compatible MCU with S/N '$sn' at USB bus '$dev'"
MCU_SYSINFO_OUTPUT=""
MCU_IMGLIST_OUTPUT=""
# We expect just a single ttyACM interface
usb_path="/sys/bus/usb/devices/${dev}*/tty/*"
for tty in $usb_path; do

View File

@@ -43,7 +43,7 @@ mcu_logw() {
mcu_log "warn" "$1"
}
_mcu_get_fwlist() {
mcu_fetch_fwlist() {
local uart="$1"
local baud="$2"
local flow="$3"
@@ -56,38 +56,16 @@ _mcu_get_fwlist() {
[ -n "$baud" ] || baud="115200"
[ -n "$MCU_IMGLIST_OUTPUT" ] || {
MCU_IMGLIST_OUTPUT="$(umcumgr -s -d "$uart" -b "$baud$flow" list)"
[ $? -eq 0 ] || {
mcu_loge "request 'list' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
[ -n "$MCU_IMGLIST_OUTPUT" ] && return 0
MCU_IMGLIST_OUTPUT="$(umcumgr -s -d "$uart" -b "$baud$flow" list)"
[ $? -eq 0 ] || {
mcu_loge "request 'list' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
}
_mcu_get_fwmetadata() {
local slot="$1"
local uart="$2"
local baud="$3"
local flow="$4"
local value
[ -n "$MCU_IMGLIST_OUTPUT" ] || {
_mcu_get_fwlist "$uart" "$baud" "$flow" || return 1
}
if [ "$slot" = "0" ]; then
slot="slot0_metadata"
else
slot="slot1_metadata"
fi
value="$(echo "$MCU_IMGLIST_OUTPUT" | grep "$slot=" | cut -d '=' -f 2)"
echo "$value"
}
_mcu_get_sysinfo() {
mcu_fetch_sysinfo() {
local uart="$1"
local baud="$2"
local flow="$3"
@@ -100,154 +78,83 @@ _mcu_get_sysinfo() {
[ -n "$baud" ] || baud="115200"
[ -n "$MCU_SYSINFO_OUTPUT" ] && return 0
MCU_SYSINFO_OUTPUT="$(umcumgr -s -d "$uart" -b "$baud$flow" sysinfo)"
[ $? -eq 0 ] || return 1
}
_mcu_get() {
local field="$1"
local uart="$2"
local baud="$3"
local flow="$4"
local value
[ -n "$MCU_SYSINFO_OUTPUT" ] || {
_mcu_get_sysinfo "$uart" "$baud" "$flow" || {
mcu_loge "request 'sysinfo' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
[ $? -eq 0 ] || {
mcu_loge "request 'sysinfo' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
value="$(echo "$MCU_SYSINFO_OUTPUT" | grep "$field=" | cut -d '=' -f 2)"
echo "$value"
}
mcu_get_sn() {
local uart="$1"
local baud="$2"
local flow="$3"
local value
value="$(_mcu_get "serial_num" "$uart" "$baud" "$flow")"
[ $? -eq 0 ] || return 1
[ -n "$value" ] && mcu_logd "MCU S/N: '$value'"
echo "$value"
}
mcu_get_board() {
local uart="$1"
local baud="$2"
local flow="$3"
local value
value="$(_mcu_get "board" "$uart" "$baud" "$flow")"
[ $? -eq 0 ] || return 1
[ -n "$value" ] && mcu_logd "MCU board: '$value'"
echo "$value"
}
mcu_get_slotsnum() {
local uart="$1"
local baud="$2"
local flow="$3"
local value
value="$(_mcu_get "single_slot" "$uart" "$baud" "$flow")"
[ $? -eq 0 ] || return 1
[ -n "$value" ] || value="1"
[ "$value" != "1" ] && value="2"
[ -n "$value" ] && mcu_logd "number of firmware slots: '$value'"
echo "$value"
}
mcu_get_softver() {
local uart="$1"
local baud="$2"
local flow="$3"
local value
value="$(_mcu_get "soft_ver" "$uart" "$baud" "$flow")"
[ $? -eq 0 ] || return 1
[ -n "$value" ] && mcu_logd "MCUboot version: '$value'"
echo "$value"
}
mcu_get_activeslot() {
local uart="$1"
local baud="$2"
local flow="$3"
local value
value="$(_mcu_get "active_slot" "$uart" "$baud" "$flow")"
[ $? -eq 0 ] || return 1
[ -n "$value" ] && mcu_logd "active firmware slot: '$value'"
echo "$value"
}
mcu_get_fwname() {
local slot="$1"
local uart="$2"
local baud="$3"
local flow="$4"
mcu_get() {
local param="$1"
local slot="$2"
local value
local metadata
local sysinfo_field
metadata="$(_mcu_get_fwmetadata "$slot" "$uart" "$baud" "$flow")"
[ $? -eq 0 ] || return 1
case "$param" in
"board"|\
"soft_ver"|\
"serial_num"|\
"active_slot")
sysinfo_field="$param"
;;
"slots_num")
sysinfo_field="single_slot"
;;
"fwname")
[ -n "$slot" ] || return 1
param="image list: slot${slot} fw_name"
[ -n "$metadata" ] && {
json_load "$metadata"
json_get_var value fw_name
metadata="$(echo "$MCU_IMGLIST_OUTPUT" | grep "slot${slot}_metadata=" | cut -d '=' -f 2)"
[ -n "$metadata" ] && {
json_load "$metadata"
json_get_var value fw_name
}
;;
"fwsha")
[ -n "$slot" ] || return 1
param="image list: slot${slot}_hash"
value="$(echo "$MCU_IMGLIST_OUTPUT" | grep "slot${slot}_hash=" | cut -d '=' -f 2)"
;;
*)
return 1
;;
esac
[ -n "$sysinfo_field" ] && {
value="$(echo "$MCU_SYSINFO_OUTPUT" | grep "${sysinfo_field}=" | cut -d '=' -f 2)"
[ "$sysinfo_field" = "single_slot" ] && {
[ -n "$value" ] || value="1"
[ "$value" != "1" ] && value="2"
}
param="sysinfo: $param"
}
if [ -n "$value" ]; then
mcu_logi "firmware installed in slot '$slot': '$value'"
else
mcu_logw "no firmware installed in slot '$slot'"
fi
[ -n "$value" ] && mcu_logd "$param: '$value'"
echo "$value"
}
mcu_get_fwsha() {
local slot="$1"
mcu_req() {
local cmd="$1"
local uart="$2"
local baud="$3"
local flow="$4"
local value
[ -n "$MCU_IMGLIST_OUTPUT" ] || {
_mcu_get_fwlist "$uart" "$baud" "$flow" || return 1
}
if [ "$slot" = "0" ]; then
slot="slot0_hash"
else
slot="slot1_hash"
fi
value="$(echo "$MCU_IMGLIST_OUTPUT" | grep "$slot=" | cut -d '=' -f 2)"
echo "$value"
}
mcu_req_boot() {
local uart="$1"
local baud="$2"
local flow="$3"
case "$cmd" in
"boot"|\
"reset")
;;
*)
return 1
;;
esac
if [ "$flow" = "1" ]; then
flow=" -f"
@@ -257,34 +164,12 @@ mcu_req_boot() {
[ -n "$baud" ] || baud="115200"
umcumgr -s -d "$uart" -b "$baud$flow" boot || {
mcu_loge "request 'boot' failed"
umcumgr -s -d "$uart" -b "$baud$flow" "$cmd" || {
mcu_loge "request '$cmd' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
mcu_logi "MCU requested to boot the firmware"
}
mcu_req_reset() {
local uart="$1"
local baud="$2"
local flow="$3"
if [ "$flow" = "1" ]; then
flow=" -f"
else
flow=""
fi
[ -n "$baud" ] || baud="115200"
# Request warm reset of the MCU
umcumgr -s -d "$uart" -b "$baud$flow" reset || {
mcu_loge "request 'reset' failed"
return 1
}
mcu_logi "MCU requested to reset"
mcu_logi "MCU requested '$cmd'"
}
mcu_sel_slot() {
@@ -303,7 +188,7 @@ mcu_sel_slot() {
# Request firmware active slot change
umcumgr -s -d "$uart" -b "$baud$flow" select "$slot" || {
mcu_loge "request 'select slot' failed"
mcu_loge "request 'select slot' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
@@ -352,7 +237,7 @@ mcu_fw_upload() {
# Upload fw to selected slot (TODO: slots numbering Zephyr vs. MCUboot)
[ "$slot" = "1" ] && slot="2"
umcumgr -q -n "$slot" -d "$uart" -b "$baud$flow" upload "$fw_path" || {
mcu_loge "request 'upload' failed"
mcu_loge "request 'upload' failed (uart='$uart', baud='$baud', flow='$flow')"
return 1
}
@@ -375,15 +260,10 @@ mcu_sn_check_and_update() {
local sn_dev
if [ "$flow" = "1" ]; then
flow=" -f"
else
flow=""
fi
# Fetch sysinfo
mcu_fetch_sysinfo "$uart" "$baud" "$flow" || return 1
[ -n "$baud" ] || baud="115200"
sn_dev="$(mcu_get_sn "$uart" "$baud" "$flow")"
sn_dev="$(mcu_get "serial_num")"
[ -n "$sn_dev" ] || return 1
[ -n "$sn_dev" ] && {
@@ -421,21 +301,29 @@ mcu_fw_check_and_update() {
local fw0_sha
local fw1_sha
local board
local soft_ver
config_get firmware "$SECT" firmware
[ -n "$firmware" ] || mcu_logw "option 'firmware' unset"
# MCU board name
board="$(mcu_get_board "$uart" "$baud" "$flow")"
# Fetch sysinfo and firmware images list
mcu_fetch_sysinfo "$uart" "$baud" "$flow" || return 1
mcu_fetch_fwlist "$uart" "$baud" "$flow" || return 1
# MCU board name and software version
board="$(mcu_get "board")"
[ $? -eq 0 ] || return 1
soft_ver="$(mcu_get "soft_ver")"
[ $? -eq 0 ] || return 1
# Number of firmware slots and active slot
fw_slots="$(mcu_get_slotsnum "$uart" "$baud" "$flow")"
fw_slots="$(mcu_get "slots_num")"
[ $? -eq 0 ] || return 1
[ -n "$fw_slots" ] || fw_slots="1"
[ "$fw_slots" = "2" ] && {
active_slot="$(mcu_get_activeslot "$uart" "$baud" "$flow")"
active_slot="$(mcu_get "active_slot")"
[ $? -eq 0 ] || return 1
}
[ -n "$active_slot" ] || active_slot="0"
@@ -460,20 +348,20 @@ mcu_fw_check_and_update() {
fw0_sha="$(mcu_fwfile_sha "${MCU_FW_DIR}/${board}/${firmware}/slot0.bin")"
}
slot0_fw="$(mcu_get_fwname "0" "$uart" "$baud" "$flow")"
slot0_fw="$(mcu_get "fwname" "0")"
[ $? -eq 0 ] || return 1
[ -n "$slot0_fw" ] && {
slot0_sha="$(mcu_get_fwsha "0" "$uart" "$baud" "$flow")"
slot0_sha="$(mcu_get "fwsha" "0")"
[ $? -eq 0 ] || return 1
}
[ "$fw_slots" = "2" ] && {
slot1_fw="$(mcu_get_fwname "1" "$uart" "$baud" "$flow")"
slot1_fw="$(mcu_get "fwname" "1")"
[ $? -eq 0 ] || return 1
[ -n "$slot1_fw" ] && {
slot1_sha="$(mcu_get_fwsha "1" "$uart" "$baud" "$flow")"
slot1_sha="$(mcu_get "fwsha" "1")"
[ $? -eq 0 ] || return 1
}
}
@@ -492,7 +380,7 @@ mcu_fw_check_and_update() {
uci -q set mcu.${SECT}.firmware="$firmware"
uci -q commit mcu
mcu_req_boot "$uart" "$baud" "$flow"
mcu_req "boot" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
}
@@ -508,10 +396,10 @@ mcu_fw_check_and_update() {
[ $? -ne 0 ] && return 1
# Changing active slots requires MCU reset at the moment
mcu_req_reset "$uart" "$baud" "$flow"
mcu_req "reset" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
else
mcu_req_boot "$uart" "$baud" "$flow"
mcu_req "boot" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
fi
@@ -525,7 +413,7 @@ mcu_fw_check_and_update() {
mcu_fw_upload "$board" "0" "$firmware" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
mcu_req_boot "$uart" "$baud" "$flow"
mcu_req "boot" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
return 0
@@ -540,10 +428,10 @@ mcu_fw_check_and_update() {
[ $? -ne 0 ] && return 1
# Changing active slots requires MCU reset at the moment
mcu_req_reset "$uart" "$baud" "$flow"
mcu_req "reset" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
else
mcu_req_boot "$uart" "$baud" "$flow"
mcu_req "boot" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
fi
@@ -567,7 +455,7 @@ mcu_fw_check_and_update() {
[ $? -ne 0 ] && return 1
# Changing active slots requires MCU reset at the moment
mcu_req_reset "$uart" "$baud" "$flow"
mcu_req "reset" "$uart" "$baud" "$flow"
[ $? -ne 0 ] && return 1
return 0