|
| 1 | +/* |
| 2 | + * Copyright (C) 2019-2021 OpenBikeSensor Contributors |
| 3 | + * Contact: https://openbikesensor.org |
| 4 | + * |
| 5 | + * This file is part of the OpenBikeSensor firmware. |
| 6 | + * |
| 7 | + * The OpenBikeSensor firmware is free software: you can |
| 8 | + * redistribute it and/or modify it under the terms of the GNU |
| 9 | + * Lesser General Public License as published by the Free Software |
| 10 | + * Foundation, either version 3 of the License, or (at your option) |
| 11 | + * any later version. |
| 12 | + * |
| 13 | + * OpenBikeSensor firmware is distributed in the hope that |
| 14 | + * it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 15 | + * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE. See the GNU Lesser General Public License for more |
| 17 | + * details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public |
| 20 | + * License along with the OpenBikeSensor firmware. If not, |
| 21 | + * see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +#include "Arduino.h" |
| 24 | +#include "Firmware.h" |
| 25 | +#include <WiFiClientSecure.h> |
| 26 | +#include <HTTPClient.h> |
| 27 | +#include <SD.h> |
| 28 | +#include <Update.h> |
| 29 | +#include <esp_ota_ops.h> |
| 30 | + |
| 31 | +/* TODO: Introduce cert management. */ |
| 32 | +const char * const GITHUB_ROOT_CA = |
| 33 | + "-----BEGIN CERTIFICATE-----\n" |
| 34 | + "MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\n" |
| 35 | + "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n" |
| 36 | + "d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\n" |
| 37 | + "ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\n" |
| 38 | + "MAkGA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+ "Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\n" |
| 53 | + "vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\n" |
| 54 | + "+OkuE6N36B9K\n" |
| 55 | + "-----END CERTIFICATE-----"; |
| 56 | + |
| 57 | +static const String FLASH_APP_FILENAME("/sdflash/app.bin"); |
| 58 | +static const String OPEN_BIKE_SENSOR_FLASH_APP_PROJECT_NAME("OpenBikeSensorFlash"); |
| 59 | +static const size_t APP_PARTITION_SIZE = 0x380000; // read from part? |
| 60 | +static const int SHA256_HASH_LEN = 32; |
| 61 | + |
| 62 | +// todo: error handling |
| 63 | +void Firmware::downloadToSd(String url, String filename) { |
| 64 | + WiFiClientSecure client; |
| 65 | + client.setCACert(GITHUB_ROOT_CA); |
| 66 | + HTTPClient http; |
| 67 | + http.setUserAgent(mUserAgent); |
| 68 | + http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); |
| 69 | + |
| 70 | + if (http.begin(client, url)) { |
| 71 | + int status = http.GET(); |
| 72 | + log_i("Opening %s got %d.", url.c_str(), status); |
| 73 | + log_i("http size: %d. ", http.getSize()); |
| 74 | + log_i("Will access Stream:, free heap %dkb", ESP.getFreeHeap() / 1024); |
| 75 | + if (status == 200) { |
| 76 | + File newFile = SD.open(filename, FILE_WRITE); |
| 77 | + int written = http.writeToStream(&newFile); |
| 78 | + newFile.close(); |
| 79 | + log_i("Got %d bytes", written); |
| 80 | + } |
| 81 | + } |
| 82 | + http.end(); |
| 83 | +} |
| 84 | + |
| 85 | +bool Firmware::downloadToFlash(String url, |
| 86 | + std::function<void(uint32_t pos, uint32_t size)> progress) { |
| 87 | + bool success = false; |
| 88 | + WiFiClientSecure client; |
| 89 | + client.setCACert(GITHUB_ROOT_CA); |
| 90 | + HTTPClient http; |
| 91 | + http.setUserAgent(mUserAgent); |
| 92 | + http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); |
| 93 | + if (http.begin(client, url)) { |
| 94 | + progress(0, 999); |
| 95 | + int status = http.GET(); |
| 96 | + log_i("Opening %s got %d.", url.c_str(), status); |
| 97 | + size_t size = http.getSize(); |
| 98 | + log_i("http size: %d. ", size); |
| 99 | + log_i("Will access Stream:, free heap %dkb", ESP.getFreeHeap() / 1024); |
| 100 | + if (status == 200) { |
| 101 | + Update.begin(); |
| 102 | + Update.onProgress([progress, size](size_t pos, size_t all) { |
| 103 | + progress(pos, size); |
| 104 | + }); |
| 105 | + |
| 106 | + Stream& stream = http.getStream(); |
| 107 | + byte buffer[256]; |
| 108 | + size_t read; |
| 109 | + size_t written = 0; |
| 110 | + while ((read = stream.readBytes(&buffer[0], sizeof(buffer))) > 0) { |
| 111 | + Update.write(buffer, read); |
| 112 | + written += read; |
| 113 | + } |
| 114 | + log_i("Got %d bytes", written); |
| 115 | + } |
| 116 | + } |
| 117 | + http.end(); |
| 118 | + if (Update.end(true)) { //true to set the size to the current progress |
| 119 | + mLastMessage = "Success"; |
| 120 | + |
| 121 | + // Suppress the ota Firmware switch! |
| 122 | + const esp_partition_t *running = esp_ota_get_running_partition(); |
| 123 | + esp_ota_set_boot_partition(running); |
| 124 | + success = true; |
| 125 | + } else { |
| 126 | + mLastMessage = Update.errorString(); |
| 127 | + log_e("Update: %s", mLastMessage.c_str()); |
| 128 | + } |
| 129 | + return success; |
| 130 | +} |
| 131 | + |
| 132 | +String Firmware::getLastMessage() { |
| 133 | + return mLastMessage; |
| 134 | +}; |
| 135 | + |
| 136 | +const esp_partition_t* Firmware::findEspFlashAppPartition() { |
| 137 | + const esp_partition_t *part |
| 138 | + = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_0, nullptr); |
| 139 | + |
| 140 | + esp_app_desc_t app_desc; |
| 141 | + esp_err_t ret = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_get_partition_description(part, &app_desc)); |
| 142 | + |
| 143 | + if (ret != ESP_OK || OPEN_BIKE_SENSOR_FLASH_APP_PROJECT_NAME != app_desc.project_name) { |
| 144 | + part |
| 145 | + = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_OTA_1, |
| 146 | + nullptr); |
| 147 | + ret = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_get_partition_description(part, &app_desc)); |
| 148 | + } |
| 149 | + |
| 150 | + if (ret == ESP_OK && OPEN_BIKE_SENSOR_FLASH_APP_PROJECT_NAME == app_desc.project_name) { |
| 151 | + return part; |
| 152 | + } else { |
| 153 | + return nullptr; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +String Firmware::getFlashAppVersion() { |
| 158 | + const esp_partition_t *part = findEspFlashAppPartition(); |
| 159 | + esp_app_desc_t app_desc; |
| 160 | + esp_err_t ret = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_get_partition_description(part, &app_desc)); |
| 161 | + String version; |
| 162 | + if (ret == ESP_OK) { |
| 163 | + version = app_desc.version; |
| 164 | + } |
| 165 | + return version; |
| 166 | +} |
| 167 | + |
| 168 | +static void calculateSha256(File &f, uint8_t *shaResult, int32_t bytes) { |
| 169 | + mbedtls_md_context_t ctx; |
| 170 | + mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256; |
| 171 | + mbedtls_md_init(&ctx); |
| 172 | + mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0); |
| 173 | + mbedtls_md_starts(&ctx); |
| 174 | + |
| 175 | + const uint16_t bufferSize = 8192; |
| 176 | + uint8_t *buffer = static_cast<uint8_t *>(malloc(bufferSize)); |
| 177 | + uint32_t toRead = bufferSize; |
| 178 | + uint32_t read; |
| 179 | + uint32_t readFromFile = 0; |
| 180 | + while ((read = f.read(buffer, toRead)) > 0) { |
| 181 | + mbedtls_md_update(&ctx, buffer, read); |
| 182 | + readFromFile += read; |
| 183 | + if (readFromFile + toRead > bytes) { |
| 184 | + toRead = bytes - readFromFile; |
| 185 | + } |
| 186 | + } |
| 187 | + free(buffer); |
| 188 | + mbedtls_md_finish(&ctx, shaResult); |
| 189 | + mbedtls_md_free(&ctx); |
| 190 | +} |
| 191 | + |
| 192 | +String Firmware::checkSdFirmware() { |
| 193 | + String error; |
| 194 | + File f = SD.open(FLASH_APP_FILENAME, FILE_READ); |
| 195 | + if (!f) { |
| 196 | + error = "Failed to find '" + FLASH_APP_FILENAME + "' on sd card."; |
| 197 | + return error; |
| 198 | + } |
| 199 | + const int32_t fileSize = f.size(); |
| 200 | + if (fileSize > APP_PARTITION_SIZE) { |
| 201 | + error = "Firmware to flash is to large. "; |
| 202 | + } else { |
| 203 | + uint8_t shaResult[SHA256_HASH_LEN]; |
| 204 | + calculateSha256(f, shaResult, fileSize - SHA256_HASH_LEN); |
| 205 | + |
| 206 | + for (int i = 0; i < SHA256_HASH_LEN; i++) { |
| 207 | + int c = f.read(); |
| 208 | + if (shaResult[i] != c) { |
| 209 | + error = "Checksum mismatch."; |
| 210 | + break; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + f.close(); |
| 215 | + return error; |
| 216 | +} |
| 217 | + |
| 218 | +bool Firmware::switchToFlashApp() { |
| 219 | + bool success = false; |
| 220 | + const esp_partition_t *part = findEspFlashAppPartition(); |
| 221 | + if (part) { |
| 222 | + success = (ESP_ERROR_CHECK_WITHOUT_ABORT(esp_ota_set_boot_partition(part)) == ESP_OK); |
| 223 | + } |
| 224 | + return success; |
| 225 | +} |
0 commit comments