mk_qemu_img.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env bash
  2. ############################################################################
  3. # tools/esp32/mk_qemu_img.sh
  4. #
  5. # Licensed to the Apache Software Foundation (ASF) under one or more
  6. # contributor license agreements. See the NOTICE file distributed with
  7. # this work for additional information regarding copyright ownership. The
  8. # ASF licenses this file to you under the Apache License, Version 2.0 (the
  9. # "License"); you may not use this file except in compliance with the
  10. # License. You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. # License for the specific language governing permissions and limitations
  18. # under the License.
  19. #
  20. ############################################################################
  21. SCRIPT_NAME=$(basename "${0}")
  22. BOOTLOADER_IMG=""
  23. PARTITION_IMG=""
  24. BOOTLOADER_OFFSET=0x1000
  25. PARTITION_OFFSET=0x8000
  26. NUTTX_OFFSET=0x10000
  27. NUTTX_IMG="nuttx.bin"
  28. FLASH_IMG="esp32_qemu_img.bin"
  29. usage() {
  30. echo ""
  31. echo "USAGE: ${SCRIPT_NAME} [-h] -b <bootloader> -p <partition_table> [-n <nuttx>] [-i <image_name>]"
  32. echo ""
  33. echo "Where:"
  34. echo " -b <bootloader> path to the bootloader image"
  35. echo " -p <partition_table> path to the partition table image"
  36. echo " -n <nuttx> path to the nuttx image (default nuttx.bin)"
  37. echo " -i <image_name> name of the resulting image (default esp32_qemu_img.bin)"
  38. echo " -h will show this help and terminate"
  39. echo ""
  40. }
  41. imgappend() {
  42. dd of="${1}" if="${2}" bs=1 seek="$(printf '%d' ${3})" conv=notrunc status=none
  43. }
  44. while [ -n "${1}" ]; do
  45. case "${1}" in
  46. -b )
  47. shift
  48. BOOTLOADER_IMG=${1}
  49. ;;
  50. -p )
  51. shift
  52. PARTITION_IMG=${1}
  53. ;;
  54. -n )
  55. shift
  56. NUTTX_IMG=${1}
  57. ;;
  58. -i )
  59. shift
  60. FLASH_IMG=${1}
  61. ;;
  62. -h )
  63. usage
  64. exit 0
  65. ;;
  66. *)
  67. usage
  68. exit 1
  69. ;;
  70. esac
  71. shift
  72. done
  73. # Make sure we have the required argument(s)
  74. if [ -z "${BOOTLOADER_IMG}" ] || [ -z "${PARTITION_IMG}" ] ; then
  75. echo ""
  76. echo "${SCRIPT_NAME}: Missing bootloader and partition table binary images."
  77. usage
  78. exit 1
  79. fi
  80. printf "Generating %s...\n" "${FLASH_IMG}"
  81. printf "\tBootloader: %s\n" "${BOOTLOADER_IMG}"
  82. printf "\tPartition Table: %s\n" "${PARTITION_IMG}"
  83. dd if=/dev/zero bs=1024 count=4096 of="${FLASH_IMG}" status=none
  84. imgappend ${FLASH_IMG} ${BOOTLOADER_IMG} ${BOOTLOADER_OFFSET}
  85. imgappend ${FLASH_IMG} ${PARTITION_IMG} ${PARTITION_OFFSET}
  86. imgappend ${FLASH_IMG} ${NUTTX_IMG} ${NUTTX_OFFSET}
  87. if [ ${?} -ne 0 ]; then
  88. printf "Failed to generate %s!\n" "${FLASH_IMG}"
  89. exit 1
  90. fi
  91. printf "Generated %s successfully!\n" "${FLASH_IMG}"
  92. printf "You can run it with QEMU using:\n"
  93. printf "\tqemu-system-xtensa -nographic -machine esp32 -drive file=%s,if=mtd,format=raw\n" "${FLASH_IMG}"
  94. echo "${FLASH_IMG}" >> nuttx.manifest