parttool_example.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env bash
  2. #
  3. # Demonstrates command-line interface of Partition Tool, parttool.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/parttool.bin
  8. PORT=$1
  9. PARTTOOL_PY="python $IDF_PATH/components/partition_table/parttool.py -q"
  10. if [[ "$PORT" != "" ]]; then
  11. PARTTOOL_PY="$PARTTOOL_PY --port $PORT"
  12. fi
  13. GEN_EMPTY_PARTITION_PY="python $IDF_PATH/components/partition_table/gen_empty_partition.py"
  14. BINARY=$2
  15. if [[ "$BINARY" == "" ]]; then
  16. BINARY=build/parttool.bin
  17. fi
  18. function assert_file_same()
  19. {
  20. sz_a=$(stat -c %s $1)
  21. sz_b=$(stat -c %s $2)
  22. sz=$((sz_a < sz_b ? sz_a : sz_b))
  23. res=$(cmp -s -n $sz $1 $2) ||
  24. (echo "!!!!!!!!!!!!!!!!!!!"
  25. echo "FAILURE: $3"
  26. echo "!!!!!!!!!!!!!!!!!!!")
  27. }
  28. # Read app partition and save the contents to a file. The app partition is identified
  29. # using type-subtype combination
  30. echo "Checking if device app binary matches built binary"
  31. $PARTTOOL_PY read_partition --partition-type=app --partition-subtype=factory --output=app.bin
  32. assert_file_same app.bin $BINARY "Device app binary does not match built binary"
  33. # Retrieve info on data storage partition, this time identifying it by name.
  34. offset=$($PARTTOOL_PY get_partition_info --partition-name=storage --info offset)
  35. size=$($PARTTOOL_PY get_partition_info --partition-name=storage --info size)
  36. echo "Found data partition at offset $offset with size $size"
  37. # Create a file whose contents will be written to the storage partition
  38. head -c $(($size)) /dev/urandom > write.bin
  39. # Write the contents of the created file to storage partition
  40. echo "Writing to data partition"
  41. $PARTTOOL_PY write_partition --partition-name=storage --input write.bin
  42. # Read back the contents of the storage partition
  43. echo "Reading data partition"
  44. $PARTTOOL_PY read_partition --partition-name=storage --output read.bin
  45. assert_file_same write.bin read.bin "Read contents of storage partition does not match source file contents"
  46. # Erase contents of the storage partition
  47. echo "Erasing data partition"
  48. $PARTTOOL_PY erase_partition --partition-name=storage
  49. # Read back the erased data partition
  50. echo "Reading data partition"
  51. $PARTTOOL_PY read_partition --partition-name=storage --output read.bin
  52. # Generate a file of all 0xFF
  53. $GEN_EMPTY_PARTITION_PY $(($size)) blank.bin
  54. assert_file_same read.bin blank.bin "Contents of storage partition not fully erased"
  55. # Example end and cleanup
  56. printf "\nPartition tool operations performed successfully\n"
  57. rm -rf app.bin read.bin blank.bin write.bin