From 80c42a77f0e8f2f553b969e1ef20436495228f08 Mon Sep 17 00:00:00 2001 From: Craig Hesling Date: Tue, 12 Nov 2019 17:00:23 -0800 Subject: flash_fp_mcu: Add retry logic This CL adds reset and retry logic. This is important because stm32mon sometimes fails to get the stm32 bootloader's attention on startup, so we need to reset the chip and try again. BRANCH=nocturne,hatch BUG=b:143374692,b:144729003 TEST=# Nocturne flash_fp_mcu /opt/google/biod/fw/*.bin TEST=# Ensure PS crrev.com/c/1921705 is applied. # Run http://go/bit/hesling/5791510394044416 Signed-off-by: Craig Hesling Change-Id: I755d9b8cbb8813fe961f359c128c674e4c395ebb Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1913626 Reviewed-by: Tom Hughes --- util/flash_fp_mcu | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/util/flash_fp_mcu b/util/flash_fp_mcu index 8baebbd6a3..086b742e8e 100644 --- a/util/flash_fp_mcu +++ b/util/flash_fp_mcu @@ -12,6 +12,7 @@ DEFINE_boolean 'remove_flash_read_protect' "${FLAGS_TRUE}" \ 'Remove flash read protection while performing command' 'U' DEFINE_boolean 'remove_flash_write_protect' "${FLAGS_TRUE}" \ 'Remove flash write protection while performing command' 'u' +DEFINE_integer 'retries' 4 'Specify number of retries' 'R' FLAGS_HELP="Usage: ${0} [flags] ec.bin" # Process commandline flags @@ -119,7 +120,7 @@ flash_fp_mcu_stm32() { local STM32MON_READ_FLAGS=" -p -s ${spidev} -r" local STM32MON_WRITE_FLAGS="-p -s ${spidev} -e -w" - local stm32mon_flags="" + local stm32mon_flags="--retries 6" if [[ "${FLAGS_remove_flash_write_protect}" -eq "${FLAGS_TRUE}" ]]; then STM32MON_READ_FLAGS=" -u ${STM32MON_READ_FLAGS}" @@ -136,13 +137,13 @@ flash_fp_mcu_stm32() { echo "Output file already exists: ${file}" exit 1 fi - stm32mon_flags="${STM32MON_READ_FLAGS}" + stm32mon_flags+=" ${STM32MON_READ_FLAGS}" else if [[ ! -f "${file}" ]]; then echo "Invalid image file: ${file}" exit 1 fi - stm32mon_flags="${STM32MON_WRITE_FLAGS}" + stm32mon_flags+=" ${STM32MON_WRITE_FLAGS}" fi check_hardware_write_protect_disabled @@ -183,16 +184,33 @@ flash_fp_mcu_stm32() { # poorly behaved chip select line. See b/145023809. sleep 0.5 - # Release reset as the SPI bus is now ready - echo 1 > "/sys/class/gpio/gpio${gpio_nrst}/value" - echo "in" > "/sys/class/gpio/gpio${gpio_nrst}/direction" - - # Print out the actual underlying command we're running and run it + local attempt=0 + local cmd_exit_status=1 local cmd="stm32mon ${stm32mon_flags} ${file}" - echo "${cmd}" - ${cmd} - local cmd_exit_status=$? + for attempt in $(seq ${FLAGS_retries}); do + # Reset sequence to enter bootloader mode + echo "out" > "/sys/class/gpio/gpio${gpio_nrst}/direction" + sleep 0.001 + echo 0 > "/sys/class/gpio/gpio${gpio_nrst}/value" + sleep 0.01 + + # Release reset as the SPI bus is now ready + echo 1 > "/sys/class/gpio/gpio${gpio_nrst}/value" + echo "in" > "/sys/class/gpio/gpio${gpio_nrst}/direction" + + # Print out the actual underlying command we're running and run it + echo "# ${cmd}" + ${cmd} + cmd_exit_status=$? + + if [[ "${cmd_exit_status}" -eq 0 ]]; then + break + fi + echo "# Attempt ${attempt} failed." + echo + sleep 1 + done # unload spidev echo "${spiid}" > /sys/bus/spi/drivers/spidev/unbind -- cgit v1.2.1