From 345a4f60d1feaf05c3a326f12cab7885d6753ebd Mon Sep 17 00:00:00 2001 From: dongheng Date: Thu, 23 May 2019 11:32:00 +0800 Subject: [PATCH] feat(app_update): add OTA init bin --- components/app_update/CMakeLists.txt | 12 ++- components/app_update/Makefile.projbuild | 60 +++++++++++++ components/app_update/dump_otadata.py | 88 +++++++++++++++++++ components/app_update/gen_empty_partition.py | 82 +++++++++++++++++ components/app_update/project_include.cmake | 8 ++ components/esptool_py/flash_project_args.in | 3 +- components/esptool_py/flasher_args.json.in | 13 +-- components/partition_table/Makefile.projbuild | 4 +- 8 files changed, 262 insertions(+), 8 deletions(-) create mode 100755 components/app_update/dump_otadata.py create mode 100755 components/app_update/gen_empty_partition.py create mode 100644 components/app_update/project_include.cmake diff --git a/components/app_update/CMakeLists.txt b/components/app_update/CMakeLists.txt index 9b05695a..ec65a9c1 100644 --- a/components/app_update/CMakeLists.txt +++ b/components/app_update/CMakeLists.txt @@ -1,11 +1,21 @@ set(COMPONENT_SRCDIRS ".") set(COMPONENT_ADD_INCLUDEDIRS "include") -set(COMPONENT_REQUIRES "spi_flash") +set(COMPONENT_REQUIRES "spi_flash" "partition_table") set(COMPONENT_PRIV_REQUIRES "bootloader_support" "util") register_component() +# Add custom target for generating empty otadata partition for flashing +if(${OTADATA_PARTITION_OFFSET}) + add_custom_command(OUTPUT "${PROJECT_BINARY_DIR}/${BLANK_OTADATA_FILE}" + COMMAND ${PYTHON} ${CMAKE_CURRENT_SOURCE_DIR}/gen_empty_partition.py + --size ${OTADATA_PARTITION_SIZE} "${PROJECT_BINARY_DIR}/${BLANK_OTADATA_FILE}") + + add_custom_target(blank_ota_data ALL DEPENDS "${PROJECT_BINARY_DIR}/${BLANK_OTADATA_FILE}") + add_dependencies(flash blank_ota_data) +endif() + if(CONFIG_APP_UPDATE_CHECK_APP_SUM) target_compile_definitions(${COMPONENT_NAME} PUBLIC -D CONFIG_ENABLE_BOOT_CHECK_SUM=1) endif() diff --git a/components/app_update/Makefile.projbuild b/components/app_update/Makefile.projbuild index b47ee4ad..0603e480 100644 --- a/components/app_update/Makefile.projbuild +++ b/components/app_update/Makefile.projbuild @@ -1,3 +1,6 @@ +# Generate partition binary +# +.PHONY: dump_otadata erase_ota blank_ota_data ifdef CONFIG_APP_UPDATE_CHECK_APP_SUM CFLAGS += -DCONFIG_ENABLE_BOOT_CHECK_SUM=1 @@ -6,3 +9,60 @@ endif ifdef CONFIG_APP_UPDATE_CHECK_APP_HASH CFLAGS += -DCONFIG_ENABLE_BOOT_CHECK_SHA256=1 endif + +GEN_EMPTY_PART := $(PYTHON) $(COMPONENT_PATH)/gen_empty_partition.py +BLANK_OTA_DATA_FILE = $(BUILD_DIR_BASE)/ota_data_initial.bin + +PARTITION_TABLE_LEN := 0xC00 +OTADATA_LEN := 0x2000 + +PARTITION_TABLE_ONCHIP_BIN_PATH := $(call dequote,$(abspath $(BUILD_DIR_BASE))) +PARTITION_TABLE_ONCHIP_BIN_NAME := "onchip_partition.bin" +OTADATA_ONCHIP_BIN_NAME := "onchip_otadata.bin" + +PARTITION_TABLE_ONCHIP_BIN := $(PARTITION_TABLE_ONCHIP_BIN_PATH)/$(call dequote,$(PARTITION_TABLE_ONCHIP_BIN_NAME)) +OTADATA_ONCHIP_BIN := $(PARTITION_TABLE_ONCHIP_BIN_PATH)/$(call dequote,$(OTADATA_ONCHIP_BIN_NAME)) + +PARTITION_TABLE_GET_BIN_CMD = $(ESPTOOLPY_SERIAL) read_flash $(PARTITION_TABLE_OFFSET) $(PARTITION_TABLE_LEN) $(PARTITION_TABLE_ONCHIP_BIN) +OTADATA_GET_BIN_CMD = $(ESPTOOLPY_SERIAL) read_flash $(OTADATA_OFFSET) $(OTADATA_LEN) $(OTADATA_ONCHIP_BIN) + +GEN_OTADATA = $(IDF_PATH)/components/app_update/dump_otadata.py +ERASE_OTADATA_CMD = $(ESPTOOLPY_SERIAL) erase_region $(OTADATA_OFFSET) $(OTADATA_LEN) + +# If there is no otadata partition, both OTA_DATA_OFFSET and BLANK_OTA_DATA_FILE +# expand to empty values. +ESPTOOL_ALL_FLASH_ARGS += $(OTA_DATA_OFFSET) $(BLANK_OTA_DATA_FILE) + +$(PARTITION_TABLE_ONCHIP_BIN): + $(PARTITION_TABLE_GET_BIN_CMD) + +onchip_otadata_get_info: $(PARTITION_TABLE_ONCHIP_BIN) + $(eval OTADATA_OFFSET:=$(shell $(GET_PART_INFO) --type data --subtype ota --offset $(PARTITION_TABLE_ONCHIP_BIN))) + @echo $(if $(OTADATA_OFFSET), $(shell export OTADATA_OFFSET), $(shell rm -f $(PARTITION_TABLE_ONCHIP_BIN));$(error "ERROR: ESP32 does not have otadata partition.")) + +$(OTADATA_ONCHIP_BIN): + $(OTADATA_GET_BIN_CMD) + +dump_otadata: onchip_otadata_get_info $(OTADATA_ONCHIP_BIN) $(PARTITION_TABLE_ONCHIP_BIN) + @echo "otadata retrieved. Contents:" + @echo $(SEPARATOR) + $(GEN_OTADATA) $(OTADATA_ONCHIP_BIN) + @echo $(SEPARATOR) + rm -f $(PARTITION_TABLE_ONCHIP_BIN) + rm -f $(OTADATA_ONCHIP_BIN) + +$(BLANK_OTA_DATA_FILE): partition_table_get_info + $(GEN_EMPTY_PART) --size $(OTA_DATA_SIZE) $(BLANK_OTA_DATA_FILE) + $(eval BLANK_OTA_DATA_FILE = $(shell if [ $(OTA_DATA_SIZE) != 0 ]; then echo $(BLANK_OTA_DATA_FILE); else echo " "; fi) ) + +blank_ota_data: $(BLANK_OTA_DATA_FILE) + +erase_ota: partition_table_get_info | check_python_dependencies + @echo $(if $(OTA_DATA_OFFSET), "Erase ota_data [addr=$(OTA_DATA_OFFSET) size=$(OTA_DATA_SIZE)] ...", $(error "ERROR: Partition table does not have ota_data partition.")) + $(ESPTOOLPY_SERIAL) erase_region $(OTA_DATA_OFFSET) $(OTA_DATA_SIZE) + +all: blank_ota_data +flash: blank_ota_data + +clean: + rm -f $(BLANK_OTA_DATA_FILE) diff --git a/components/app_update/dump_otadata.py b/components/app_update/dump_otadata.py new file mode 100755 index 00000000..040bd2b0 --- /dev/null +++ b/components/app_update/dump_otadata.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# +# gen_otadata prints info about the otadata partition. +# +# Copyright 2018 Espressif Systems (Shanghai) PTE LTD +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import print_function, division +import argparse +import os +import re +import struct +import sys +import hashlib +import binascii + +__version__ = '1.0' + +quiet = False + +def status(msg): + """ Print status message to stderr """ + if not quiet: + critical(msg) + +def critical(msg): + """ Print critical message to stderr """ + if not quiet: + sys.stderr.write(msg) + sys.stderr.write('\n') + +def little_endian(buff, offset): + data = buff[offset:offset+4] + data.reverse() + data = ''.join(data) + return data + +def main(): + global quiet + parser = argparse.ArgumentParser(description='Prints otadata partition in human readable form.') + + parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true') + + search_type = parser.add_mutually_exclusive_group() + + parser.add_argument('input', help='Path to binary file containing otadata partition to parse.', + type=argparse.FileType('rb')) + + args = parser.parse_args() + + quiet = args.quiet + + input = args.input.read() + + hex_input_0 = binascii.hexlify(input) + hex_input_0 = map(''.join, zip(*[iter(hex_input_0)]*2)) + hex_input_1 = binascii.hexlify(input[4096:]) + hex_input_1 = map(''.join, zip(*[iter(hex_input_1)]*2)) + + print("\t%11s\t%8s |\t%8s\t%8s" %("OTA_SEQ", "CRC", "OTA_SEQ", "CRC")) + print("Firmware: 0x%s \t 0x%s |\t0x%s \t 0x%s" % (little_endian(hex_input_0, 0), little_endian(hex_input_0, 28), \ + little_endian(hex_input_1, 0), little_endian(hex_input_1, 28))) +class InputError(RuntimeError): + def __init__(self, e): + super(InputError, self).__init__(e) + +class ValidationError(InputError): + def __init__(self, partition, message): + super(ValidationError, self).__init__( + "Partition %s invalid: %s" % (partition.name, message)) + +if __name__ == '__main__': + try: + r = main() + sys.exit(r) + except InputError as e: + print(e, file=sys.stderr) + sys.exit(2) diff --git a/components/app_update/gen_empty_partition.py b/components/app_update/gen_empty_partition.py new file mode 100755 index 00000000..365b2ada --- /dev/null +++ b/components/app_update/gen_empty_partition.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# +# generates an empty binary file +# +# This tool generates an empty binary file of the required size. +# +# Copyright 2018 Espressif Systems (Shanghai) PTE LTD +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http:#www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import print_function, division +from __future__ import unicode_literals +import argparse +import os +import re +import struct +import sys +import hashlib +import binascii + +__version__ = '1.0' + +quiet = False + +def status(msg): + """ Print status message to stderr """ + if not quiet: + critical(msg) + +def critical(msg): + """ Print critical message to stderr """ + if not quiet: + sys.stderr.write(msg) + sys.stderr.write('\n') + +def generate_blanked_file(size, output_path): + output = b"\xFF" * size + try: + stdout_binary = sys.stdout.buffer # Python 3 + except AttributeError: + stdout_binary = sys.stdout + with stdout_binary if output_path == '-' else open(output_path, 'wb') as f: + f.write(output) + +def main(): + global quiet + parser = argparse.ArgumentParser(description='Generates an empty binary file of the required size.') + + parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true') + + parser.add_argument('--size', help='Size of generated the file', type=str, required=True) + + parser.add_argument('output', help='Path for binary file.', nargs='?', default='-') + args = parser.parse_args() + + quiet = args.quiet + + size = int(args.size, 0) + if size > 0 : + generate_blanked_file(size, args.output) + return 0 + +class InputError(RuntimeError): + def __init__(self, e): + super(InputError, self).__init__(e) + +if __name__ == '__main__': + try: + r = main() + sys.exit(r) + except InputError as e: + print(e, file=sys.stderr) + sys.exit(2) diff --git a/components/app_update/project_include.cmake b/components/app_update/project_include.cmake new file mode 100644 index 00000000..70b896f0 --- /dev/null +++ b/components/app_update/project_include.cmake @@ -0,0 +1,8 @@ + +# Set empty otadata partition file for flashing, if OTA data partition in +# partition table +# (NB: because of component dependency, we know partition_table +# project_include.cmake has already been included.) +if(${OTADATA_PARTITION_OFFSET}) + set(BLANK_OTADATA_FILE "ota_data_initial.bin") +endif() diff --git a/components/esptool_py/flash_project_args.in b/components/esptool_py/flash_project_args.in index 52b35690..50d910a2 100644 --- a/components/esptool_py/flash_project_args.in +++ b/components/esptool_py/flash_project_args.in @@ -3,5 +3,6 @@ --flash_freq ${ESPFLASHFREQ} 0x0000 bootloader/bootloader.bin ${PARTITION_TABLE_OFFSET} partition_table/partition-table.bin -${APP_PARTITION_OFFSET} ${PROJECT_NAME}.bin ${PHY_PARTITION_OFFSET} ${PHY_PARTITION_BIN_FILE} +${OTADATA_PARTITION_OFFSET} ${BLANK_OTADATA_FILE} +${APP_PARTITION_OFFSET} ${PROJECT_NAME}.bin diff --git a/components/esptool_py/flasher_args.json.in b/components/esptool_py/flasher_args.json.in index 3236408e..4d63f9f9 100644 --- a/components/esptool_py/flasher_args.json.in +++ b/components/esptool_py/flasher_args.json.in @@ -5,13 +5,16 @@ "flash_files" : { "0x0000" : "bootloader/bootloader.bin", "${PARTITION_TABLE_OFFSET}" : "partition_table/partition-table.bin", - "${APP_PARTITION_OFFSET}" : "${PROJECT_NAME}.bin", - "${PHY_PARTITION_OFFSET}" : "${PHY_PARTITION_BIN_FILE}" + "${PHY_PARTITION_OFFSET}" : "${PHY_PARTITION_BIN_FILE}", + "${OTADATA_PARTITION_OFFSET}" : "${BLANK_OTADATA_FILE}", + "${APP_PARTITION_OFFSET}" : "${PROJECT_NAME}.bin" }, "bootloader" : { "offset" : "0x1000", "file" : "bootloader/bootloader.bin" }, - "app" : { "offset" : "${APP_PARTITION_OFFSET}", - "file" : "${PROJECT_NAME}.bin" }, "partition_table" : { "offset" : "${PARTITION_TABLE_OFFSET}", - "file" : "partition_table/partition-table.bin" } + "file" : "partition_table/partition-table.bin" }, + "otadata" : { "offset" : "${OTADATA_PARTITION_OFFSET}", + "file" : "${BLANK_OTADATA_FILE}" }, + "app" : { "offset" : "${APP_PARTITION_OFFSET}", + "file" : "${PROJECT_NAME}.bin" } } diff --git a/components/partition_table/Makefile.projbuild b/components/partition_table/Makefile.projbuild index f7257261..0473b871 100644 --- a/components/partition_table/Makefile.projbuild +++ b/components/partition_table/Makefile.projbuild @@ -56,6 +56,8 @@ $(PARTITION_TABLE_BIN_UNSIGNED): $(PARTITION_TABLE_CSV_PATH) $(SDKCONFIG_MAKEFIL all_binaries: $(PARTITION_TABLE_BIN) partition_table_get_info partition_table_get_info: $(PARTITION_TABLE_BIN) + $(eval OTA_DATA_SIZE := $(shell $(GET_PART_INFO) --type data --subtype ota --size $(PARTITION_TABLE_BIN) || echo 0)) + $(eval OTA_DATA_OFFSET := $(shell $(GET_PART_INFO) --type data --subtype ota --offset $(PARTITION_TABLE_BIN))) $(eval PHY_DATA_OFFSET:=$(shell $(GET_PART_INFO) --type data --subtype phy --offset $(PARTITION_TABLE_BIN))) $(eval APP_OFFSET:=$(shell $(GET_PART_INFO) --default-boot-partition --offset $(PARTITION_TABLE_BIN))) $(eval APP_SIZE:=$(shell $(GET_PART_INFO) --default-boot-partition --size $(PARTITION_TABLE_BIN))) @@ -63,7 +65,7 @@ partition_table_get_info: $(PARTITION_TABLE_BIN) $(eval APP2_SIZE:=$(shell $(GET_PART_INFO) --type app --subtype ota_1 --size $(PARTITION_TABLE_BIN))) $(eval CFLAGS += -DAPP_OFFSET=$(APP_OFFSET) -DAPP_SIZE=$(APP_SIZE)) -export PHY_DATA_OFFSET APP_OFFSET APP_SIZE APP2_OFFSET APP2_SIZE +export OTA_DATA_SIZE OTA_DATA_OFFSET PHY_DATA_OFFSET APP_OFFSET APP_SIZE APP2_OFFSET APP2_SIZE PARTITION_TABLE_FLASH_CMD = $(ESPTOOLPY_SERIAL) write_flash $(PARTITION_TABLE_OFFSET) $(PARTITION_TABLE_BIN) ESPTOOL_ALL_FLASH_ARGS += $(PARTITION_TABLE_OFFSET) $(PARTITION_TABLE_BIN)