otatool_example.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env bash
  2. #
  3. # Demonstrates command-line interface of OTA Partitions Tool, otatool.py
  4. #
  5. #
  6. # $1 - serial port where target device to operate on is connnected to, by default the first found valid serial port
  7. # $2 - path to this example's built binary file (parttool.bin), by default $PWD/build/otatool.bin
  8. PORT=$1
  9. OTATOOL_PY="python $IDF_PATH/components/app_update/otatool.py -q"
  10. if [[ "$PORT" != "" ]]; then
  11. OTATOOL_PY="$OTATOOL_PY --port $PORT"
  12. fi
  13. BINARY=$2
  14. if [[ "$BINARY" == "" ]]; then
  15. BINARY=build/otatool.bin
  16. fi
  17. function assert_file_same()
  18. {
  19. sz_a=$(stat -c %s $1)
  20. sz_b=$(stat -c %s $2)
  21. sz=$((sz_a < sz_b ? sz_a : sz_b))
  22. res=$(cmp -s -n $sz $1 $2) ||
  23. (echo "!!!!!!!!!!!!!!!!!!!"
  24. echo "FAILURE: $3"
  25. echo "!!!!!!!!!!!!!!!!!!!")
  26. }
  27. function assert_running_partition()
  28. {
  29. running=$(python get_running_partition.py)
  30. if [[ "$running" != "$1" ]]; then
  31. echo "!!!!!!!!!!!!!!!!!!!"
  32. echo "FAILURE: Running partition '$running' does not match expected '$1'"
  33. echo "!!!!!!!!!!!!!!!!!!!"
  34. exit 1
  35. fi
  36. }
  37. # Flash the example firmware to OTA partitions. The first write uses slot number to identify OTA
  38. # partition, the second one uses the name.
  39. echo "Writing factory firmware to ota_0"
  40. $OTATOOL_PY write_ota_partition --slot 0 --input $BINARY
  41. echo "Writing factory firmware to ota_1"
  42. $OTATOOL_PY write_ota_partition --name ota_1 --input $BINARY
  43. # Read back the written firmware
  44. $OTATOOL_PY read_ota_partition --name ota_0 --output app0.bin
  45. $OTATOOL_PY read_ota_partition --slot 1 --output app1.bin
  46. assert_file_same $BINARY app0.bin "Slot 0 app does not match factory app"
  47. assert_file_same $BINARY app1.bin "Slot 1 app does not match factory app"
  48. # Switch to factory app
  49. echo "Switching to factory app"
  50. $OTATOOL_PY erase_otadata
  51. assert_running_partition factory
  52. # Switch to slot 0
  53. echo "Switching to OTA slot 0"
  54. $OTATOOL_PY switch_ota_partition --slot 0
  55. assert_running_partition ota_0
  56. # Switch to slot 1 twice in a row
  57. echo "Switching to OTA slot 1 (twice in a row)"
  58. $OTATOOL_PY switch_ota_partition --slot 1
  59. assert_running_partition ota_1
  60. $OTATOOL_PY switch_ota_partition --name ota_1
  61. assert_running_partition ota_1
  62. # Switch to slot 0 twice in a row
  63. echo "Switching to OTA slot 0 (twice in a row)"
  64. $OTATOOL_PY switch_ota_partition --slot 0
  65. assert_running_partition ota_0
  66. $OTATOOL_PY switch_ota_partition --name ota_0
  67. assert_running_partition ota_0
  68. # Switch to factory app
  69. echo "Switching to factory app"
  70. $OTATOOL_PY erase_otadata
  71. assert_running_partition factory
  72. # Switch to slot 1
  73. echo "Switching to OTA slot 1"
  74. $OTATOOL_PY switch_ota_partition --slot 1
  75. assert_running_partition ota_1
  76. # Example end and cleanup
  77. printf "\nPartition tool operations performed successfully\n"
  78. rm -rf app0.bin app1.bin