|
23 | 23 |
|
24 | 24 | #ifdef HAS_TCP |
25 | 25 | #include <ArduinoIoTCloudTCP.h> |
| 26 | + |
26 | 27 | #ifdef BOARD_HAS_ECCX08 |
27 | 28 | #include "tls/BearSSLTrustAnchors.h" |
28 | 29 | #include "tls/utility/CryptoUtil.h" |
|
34 | 35 | #endif |
35 | 36 |
|
36 | 37 | #ifdef BOARD_HAS_OFFLOADED_ECCX08 |
37 | | -#include <ArduinoECCX08.h> |
38 | | -#include "tls/utility/CryptoUtil.h" |
| 38 | + #include <ArduinoECCX08.h> |
| 39 | + #include "tls/utility/CryptoUtil.h" |
39 | 40 | #endif |
40 | 41 |
|
41 | | -#ifdef BOARD_STM32H7 |
42 | | -# include "tls/utility/SHA256.h" |
43 | | -# include <stm32h7xx_hal_rtc_ex.h> |
44 | | -# include <WiFi.h> |
| 42 | +#if OTA_ENABLED |
| 43 | + #include "utility/ota/OTA.h" |
45 | 44 | #endif |
46 | 45 |
|
47 | | -#include "utility/ota/OTA.h" |
48 | | -#include "utility/ota/FlashSHA256.h" |
49 | 46 | #include <algorithm> |
50 | 47 | #include "cbor/CBOREncoder.h" |
51 | | - |
52 | 48 | #include "utility/watchdog/Watchdog.h" |
53 | 49 |
|
54 | | - |
55 | | -/****************************************************************************** |
56 | | - * EXTERN |
57 | | - ******************************************************************************/ |
58 | | - |
59 | | -#ifdef BOARD_STM32H7 |
60 | | -extern RTC_HandleTypeDef RTCHandle; |
61 | | -#endif |
62 | | - |
63 | 50 | /****************************************************************************** |
64 | 51 | LOCAL MODULE FUNCTIONS |
65 | 52 | ******************************************************************************/ |
@@ -151,61 +138,8 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, |
151 | 138 | #endif /* AVR */ |
152 | 139 |
|
153 | 140 | #if OTA_ENABLED && !defined(__AVR__) |
154 | | -#if defined(BOARD_STM32H7) |
155 | | - /* The length of the application can be retrieved the same way it was |
156 | | - * communicated to the bootloader, that is by writing to the non-volatile |
157 | | - * storage registers of the RTC. |
158 | | - */ |
159 | | - SHA256 sha256; |
160 | | - uint32_t const app_start = 0x8040000; |
161 | | - uint32_t const app_size = HAL_RTCEx_BKUPRead(&RTCHandle, RTC_BKP_DR3); |
162 | | - |
163 | | - sha256.begin(); |
164 | | - uint32_t b = 0; |
165 | | - uint32_t bytes_read = 0; for(uint32_t a = app_start; |
166 | | - bytes_read < app_size; |
167 | | - bytes_read += sizeof(b), a += sizeof(b)) |
168 | | - { |
169 | | - /* Read the next chunk of memory. */ |
170 | | - memcpy(&b, reinterpret_cast<const void *>(a), sizeof(b)); |
171 | | - /* Feed it to SHA256. */ |
172 | | - sha256.update(reinterpret_cast<uint8_t *>(&b), sizeof(b)); |
173 | | - } |
174 | | - /* Retrieve the final hash string. */ |
175 | | - uint8_t sha256_hash[SHA256::HASH_SIZE] = {0}; |
176 | | - sha256.finalize(sha256_hash); |
177 | | - String sha256_str; |
178 | | - std::for_each(sha256_hash, |
179 | | - sha256_hash + SHA256::HASH_SIZE, |
180 | | - [&sha256_str](uint8_t const elem) |
181 | | - { |
182 | | - char buf[4]; |
183 | | - snprintf(buf, 4, "%02X", elem); |
184 | | - sha256_str += buf; |
185 | | - }); |
186 | | - DEBUG_VERBOSE("SHA256: %d bytes (of %d) read", bytes_read, app_size); |
187 | | -#elif defined(ARDUINO_ARCH_SAMD) |
188 | | - /* Calculate the SHA256 checksum over the firmware stored in the flash of the |
189 | | - * MCU. Note: As we don't know the length per-se we read chunks of the flash |
190 | | - * until we detect one containing only 0xFF (= flash erased). This only works |
191 | | - * for firmware updated via OTA and second stage bootloaders (SxU family) |
192 | | - * because only those erase the complete flash before performing an update. |
193 | | - * Since the SHA256 firmware image is only required for the cloud servers to |
194 | | - * perform a version check after the OTA update this is a acceptable trade off. |
195 | | - * The bootloader is excluded from the calculation and occupies flash address |
196 | | - * range 0 to 0x2000, total flash size of 0x40000 bytes (256 kByte). |
197 | | - */ |
198 | | - String const sha256_str = FlashSHA256::calc(0x2000, 0x40000 - 0x2000); |
199 | | -#elif defined(ARDUINO_NANO_RP2040_CONNECT) |
200 | | - /* The maximum size of a RP2040 OTA update image is 1 MByte (that is 1024 * |
201 | | - * 1024 bytes or 0x100'000 bytes). |
202 | | - */ |
203 | | - String const sha256_str = FlashSHA256::calc(XIP_BASE, 0x100000); |
204 | | -#else |
205 | | -# error "No method for SHA256 checksum calculation over application image defined for this architecture." |
206 | | -#endif |
207 | | - DEBUG_VERBOSE("SHA256: HASH(%d) = %s", strlen(sha256_str.c_str()), sha256_str.c_str()); |
208 | | - _ota_img_sha256 = sha256_str; |
| 141 | + _ota_img_sha256 = getOTAImageSHA256(); |
| 142 | + DEBUG_VERBOSE("SHA256: HASH(%d) = %s", strlen(_ota_img_sha256.c_str()), _ota_img_sha256.c_str()); |
209 | 143 | #endif /* OTA_ENABLED */ |
210 | 144 |
|
211 | 145 | #if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) |
@@ -300,6 +234,11 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress, |
300 | 234 | _ota_cap = true; |
301 | 235 | #endif |
302 | 236 |
|
| 237 | +#if defined(ARDUINO_ARCH_ESP32) && OTA_ENABLED |
| 238 | + /* NOTE: here is possible to check if current partition scheme is OTA compatible */ |
| 239 | + _ota_cap = true; |
| 240 | +#endif |
| 241 | + |
303 | 242 | #ifdef BOARD_HAS_OFFLOADED_ECCX08 |
304 | 243 | if (String(WiFi.firmwareVersion()) < String("1.4.4")) { |
305 | 244 | DEBUG_ERROR("ArduinoIoTCloudTCP::%s In order to connect to Arduino IoT Cloud, NINA firmware needs to be >= 1.4.4, current %s", __FUNCTION__, WiFi.firmwareVersion()); |
@@ -840,6 +779,25 @@ void ArduinoIoTCloudTCP::onOTARequest() |
840 | 779 | bool const use_ethernet = _connection->getInterface() == NetworkAdapter::ETHERNET ? true : false; |
841 | 780 | _ota_error = portenta_h7_onOTARequest(_ota_url.c_str(), use_ethernet); |
842 | 781 | #endif |
| 782 | + |
| 783 | +#ifdef ARDUINO_ARCH_ESP32 |
| 784 | + _ota_error = esp32_onOTARequest(_ota_url.c_str()); |
| 785 | +#endif |
| 786 | +} |
| 787 | + |
| 788 | +String ArduinoIoTCloudTCP::getOTAImageSHA256() |
| 789 | +{ |
| 790 | +#if defined (ARDUINO_ARCH_SAMD) |
| 791 | + return samd_getOTAImageSHA256(); |
| 792 | +#elif defined (ARDUINO_NANO_RP2040_CONNECT) |
| 793 | + return rp2040_connect_getOTAImageSHA256(); |
| 794 | +#elif defined (BOARD_STM32H7) |
| 795 | + return portenta_h7_getOTAImageSHA256(); |
| 796 | +#elif defined (ARDUINO_ARCH_ESP32) |
| 797 | + return esp32_getOTAImageSHA256(); |
| 798 | +#else |
| 799 | + # error "No method for SHA256 checksum calculation over application image defined for this architecture." |
| 800 | +#endif |
843 | 801 | } |
844 | 802 | #endif |
845 | 803 |
|
|
0 commit comments