app_image_format.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. App Image Format
  2. ================
  3. :link_to_translation:`zh_CN:[中文]`
  4. .. _app-image-structures:
  5. Application Image Structures
  6. ----------------------------
  7. An application image consists of the following:
  8. 1. The :cpp:type:`esp_image_header_t` structure describes the mode of SPI flash and the count of memory segments.
  9. 2. The :cpp:type:`esp_image_segment_header_t` structure describes each segment, its length, and its location in {IDF_TARGET_NAME}'s memory, followed by the data with a length of ``data_len``. The data offset for each segment in the image is calculated in the following way:
  10. * offset for 0 Segment = sizeof(:cpp:type:`esp_image_header_t`) + sizeof(:cpp:type:`esp_image_segment_header_t`)
  11. * offset for 1 Segment = offset for 0 Segment + length of 0 Segment + sizeof(:cpp:type:`esp_image_segment_header_t`)
  12. * offset for 2 Segment = offset for 1 Segment + length of 1 Segment + sizeof(:cpp:type:`esp_image_segment_header_t`)
  13. * ...
  14. The count of each segment is defined in the ``segment_count`` field that is stored in :cpp:type:`esp_image_header_t`. The count cannot be more than :c:macro:`ESP_IMAGE_MAX_SEGMENTS`.
  15. To get the list of your image segments, please run the following command:
  16. .. code-block::
  17. esptool.py --chip {IDF_TARGET_PATH_NAME} image_info build/app.bin
  18. .. code-block::
  19. esptool.py v2.3.1
  20. Image version: 1
  21. Entry point: 40080ea4
  22. 13 segments
  23. Segment 1: len 0x13ce0 load 0x3f400020 file_offs 0x00000018 SOC_DROM
  24. Segment 2: len 0x00000 load 0x3ff80000 file_offs 0x00013d00 SOC_RTC_DRAM
  25. Segment 3: len 0x00000 load 0x3ff80000 file_offs 0x00013d08 SOC_RTC_DRAM
  26. Segment 4: len 0x028e0 load 0x3ffb0000 file_offs 0x00013d10 DRAM
  27. Segment 5: len 0x00000 load 0x3ffb28e0 file_offs 0x000165f8 DRAM
  28. Segment 6: len 0x00400 load 0x40080000 file_offs 0x00016600 SOC_IRAM
  29. Segment 7: len 0x09600 load 0x40080400 file_offs 0x00016a08 SOC_IRAM
  30. Segment 8: len 0x62e4c load 0x400d0018 file_offs 0x00020010 SOC_IROM
  31. Segment 9: len 0x06cec load 0x40089a00 file_offs 0x00082e64 SOC_IROM
  32. Segment 10: len 0x00000 load 0x400c0000 file_offs 0x00089b58 SOC_RTC_IRAM
  33. Segment 11: len 0x00004 load 0x50000000 file_offs 0x00089b60 SOC_RTC_DATA
  34. Segment 12: len 0x00000 load 0x50000004 file_offs 0x00089b6c SOC_RTC_DATA
  35. Segment 13: len 0x00000 load 0x50000004 file_offs 0x00089b74 SOC_RTC_DATA
  36. Checksum: e8 (valid)
  37. Validation Hash: 407089ca0eae2bbf83b4120979d3354b1c938a49cb7a0c997f240474ef2ec76b (valid)
  38. You can also see the information on segments in the ESP-IDF logs while your application is booting:
  39. .. code-block::
  40. I (443) esp_image: segment 0: paddr=0x00020020 vaddr=0x3f400020 size=0x13ce0 ( 81120) map
  41. I (489) esp_image: segment 1: paddr=0x00033d08 vaddr=0x3ff80000 size=0x00000 ( 0) load
  42. I (530) esp_image: segment 2: paddr=0x00033d10 vaddr=0x3ff80000 size=0x00000 ( 0) load
  43. I (571) esp_image: segment 3: paddr=0x00033d18 vaddr=0x3ffb0000 size=0x028e0 ( 10464) load
  44. I (612) esp_image: segment 4: paddr=0x00036600 vaddr=0x3ffb28e0 size=0x00000 ( 0) load
  45. I (654) esp_image: segment 5: paddr=0x00036608 vaddr=0x40080000 size=0x00400 ( 1024) load
  46. I (695) esp_image: segment 6: paddr=0x00036a10 vaddr=0x40080400 size=0x09600 ( 38400) load
  47. I (737) esp_image: segment 7: paddr=0x00040018 vaddr=0x400d0018 size=0x62e4c (405068) map
  48. I (847) esp_image: segment 8: paddr=0x000a2e6c vaddr=0x40089a00 size=0x06cec ( 27884) load
  49. I (888) esp_image: segment 9: paddr=0x000a9b60 vaddr=0x400c0000 size=0x00000 ( 0) load
  50. I (929) esp_image: segment 10: paddr=0x000a9b68 vaddr=0x50000000 size=0x00004 ( 4) load
  51. I (971) esp_image: segment 11: paddr=0x000a9b74 vaddr=0x50000004 size=0x00000 ( 0) load
  52. I (1012) esp_image: segment 12: paddr=0x000a9b7c vaddr=0x50000004 size=0x00000 ( 0) load
  53. .. only:: esp32
  54. For more details on the type of memory segments and their address ranges, see **{IDF_TARGET_NAME} Technical Reference Manual** > **System and Memory** > **Embedded Memory** [`PDF <{IDF_TARGET_TRM_EN_URL}#sysmem>`__].
  55. .. only:: not esp32
  56. For more details on the type of memory segments and their address ranges, see **{IDF_TARGET_NAME} Technical Reference Manual** > **System and Memory** > **Internal Memory** [`PDF <{IDF_TARGET_TRM_EN_URL}#sysmem>`__].
  57. 3. The image has a single checksum byte after the last segment. This byte is written on a sixteen byte padded boundary, so the application image might need padding.
  58. 4. If the ``hash_appended`` field from :cpp:type:`esp_image_header_t` is set then a SHA256 checksum will be appended. The value of the SHA256 hash is calculated on the range from the first byte and up to this field. The length of this field is 32 bytes.
  59. 5. If the option :ref:`CONFIG_SECURE_SIGNED_APPS_SCHEME` is set to ECDSA then the application image will have an additional 68 bytes for an ECDSA signature, which includes:
  60. * version word (4 bytes)
  61. * signature data (64 bytes)
  62. 6. If the option :ref:`CONFIG_SECURE_SIGNED_APPS_SCHEME` is set to RSA or ECDSA (V2) then the application image will have an additional signature sector of 4 KB in size. For more details on the format of this signature sector, please refer to :ref:`signature-block-format`.
  63. .. _app-image-format-application-description:
  64. Application Description
  65. -----------------------
  66. The ``DROM`` segment of the application binary starts with the :cpp:type:`esp_app_desc_t` structure which carries specific fields describing the application:
  67. * ``magic_word``: the magic word for the :cpp:type:`esp_app_desc_t` structure
  68. * ``secure_version``: see :doc:`Anti-rollback </api-reference/system/ota>`
  69. * ``version``: see :doc:`App version </api-reference/system/misc_system_api>` [#f1]_
  70. * ``project_name``: filled from ``PROJECT_NAME`` [#f1]_
  71. * ``time`` and ``date``: compile time and date
  72. * ``idf_ver``: version of ESP-IDF [#f1]_
  73. * ``app_elf_sha256``: contains SHA256 hash for the application ELF file
  74. .. [#f1] The maximum length is 32 characters, including null-termination character. For example, if the length of ``PROJECT_NAME`` exceeds 31 characters, the excess characters will be disregarded.
  75. This structure is useful for identification of images uploaded via Over-the-Air (OTA) updates because it has a fixed offset = sizeof(:cpp:type:`esp_image_header_t`) + sizeof(:cpp:type:`esp_image_segment_header_t`). As soon as a device receives the first fragment containing this structure, it has all the information to determine whether the update should be continued with or not.
  76. To obtain the :cpp:type:`esp_app_desc_t` structure for the currently running application, use :cpp:func:`esp_app_get_description`.
  77. To obtain the :cpp:type:`esp_app_desc_t` structure for another OTA partition, use :cpp:func:`esp_ota_get_partition_description`.
  78. Adding a Custom Structure to an Application
  79. -------------------------------------------
  80. Users also have the opportunity to have similar structure with a fixed offset relative to the beginning of the image.
  81. The following pattern can be used to add a custom structure to your image:
  82. .. code-block:: c
  83. const __attribute__((section(".rodata_custom_desc"))) esp_custom_app_desc_t custom_app_desc = { ... }
  84. Offset for custom structure is sizeof(:cpp:type:`esp_image_header_t`) + sizeof(:cpp:type:`esp_image_segment_header_t`) + sizeof(:cpp:type:`esp_app_desc_t`).
  85. To guarantee that the custom structure is located in the image even if it is not used, you need to add ``target_link_libraries(${COMPONENT_TARGET} "-u custom_app_desc")`` into ``CMakeLists.txt``.
  86. API Reference
  87. -------------
  88. .. include-build-file:: inc/esp_app_format.inc