|
| 1 | +/* |
| 2 | + Copyright (c) 2025 Arduino SA |
| 3 | +
|
| 4 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | +*/ |
| 8 | +#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) \ |
| 9 | + || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA) |
| 10 | +#include "H7FlashFormatter.h" |
| 11 | +#include "wiced_resource.h" |
| 12 | +#include "certificates.h" |
| 13 | + |
| 14 | +//QSPIFBlockDevice _root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000); |
| 15 | + |
| 16 | +MBEDH7FlashFormatter::MBEDH7FlashFormatter(): |
| 17 | +_root(QSPI_SO0, QSPI_SO1, QSPI_SO2, QSPI_SO3, QSPI_SCK, QSPI_CS, QSPIF_POLARITY_MODE_1, 40000000), |
| 18 | +_wifi_data(&_root, 1), |
| 19 | +_wifi_data_fs("wlan"), |
| 20 | +_ota_data(&_root, 2), |
| 21 | +_ota_data_fs("fs"), |
| 22 | +_kvstore_data(&_root, 3) |
| 23 | +{ |
| 24 | +} |
| 25 | + |
| 26 | +bool MBEDH7FlashFormatter::checkPartition() |
| 27 | +{ |
| 28 | + if (_root.init() != QSPIF_BD_ERROR_OK) |
| 29 | + { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + if (!checkWifiPartition()) |
| 34 | + { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + if (_ota_data.init() != QSPIF_BD_ERROR_OK || _ota_data_fs.mount(&_ota_data) != 0) |
| 39 | + { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + if (_ota_data.size() < 5 * 1024 * 1024) |
| 44 | + { |
| 45 | + return false; |
| 46 | + } |
| 47 | + _ota_data_fs.unmount(); |
| 48 | + _ota_data.deinit(); |
| 49 | + |
| 50 | + if (_kvstore_data.init() != QSPIF_BD_ERROR_OK) |
| 51 | + { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + _kvstore_data.deinit(); |
| 56 | + _root.deinit(); |
| 57 | + |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +bool MBEDH7FlashFormatter::formatPartition() { |
| 62 | + _root.erase(0x0, _root.get_erase_size()); |
| 63 | + mbed::MBRBlockDevice::partition(&_root, 1, 0x0B, 0, 1024 * 1024); |
| 64 | + mbed::MBRBlockDevice::partition(&_root, 2, 0x0B, 1024 * 1024, 13 * 1024 * 1024); |
| 65 | + mbed::MBRBlockDevice::partition(&_root, 3, 0x0B, 13 * 1024 * 1024, 14 * 1024 * 1024); |
| 66 | + |
| 67 | + if(_ota_data_fs.mount(&_ota_data) != 0) { |
| 68 | + if(_ota_data_fs.reformat(&_ota_data) != 0) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + }else { |
| 72 | + _ota_data_fs.unmount(); |
| 73 | + } |
| 74 | + |
| 75 | + if(!formatWifiPartition()) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + _root.deinit(); |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +long MBEDH7FlashFormatter::getFileSize(FILE *fp) { |
| 83 | + fseek(fp, 0, SEEK_END); |
| 84 | + int size = ftell(fp); |
| 85 | + fseek(fp, 0, SEEK_SET); |
| 86 | + return size; |
| 87 | +} |
| 88 | + |
| 89 | +bool MBEDH7FlashFormatter::checkWifiPartition() { |
| 90 | + int err = _wifi_data_fs.mount(&_wifi_data); |
| 91 | + if (err) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + DIR *dir; |
| 96 | + struct dirent *ent; |
| 97 | + |
| 98 | + |
| 99 | + if ((dir = opendir("/wlan")) == NULL) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + bool found = false; |
| 104 | + while ((ent = readdir (dir)) != NULL) { |
| 105 | + String fullname = "/wlan/" + String(ent->d_name); |
| 106 | + if (fullname == "/wlan/4343WA1.BIN") { |
| 107 | + found = true; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + closedir (dir); |
| 113 | + _wifi_data_fs.unmount(); |
| 114 | + return found; |
| 115 | +} |
| 116 | + |
| 117 | +bool MBEDH7FlashFormatter::formatWifiPartition() { |
| 118 | + _wifi_data_fs.reformat(&_wifi_data); |
| 119 | + extern const unsigned char wifi_firmware_image_data[]; |
| 120 | + extern const resource_hnd_t wifi_firmware_image; |
| 121 | + FILE* fp = fopen("/wlan/4343WA1.BIN", "wb"); |
| 122 | + const int file_size = 421098; |
| 123 | + int chunck_size = 1024; |
| 124 | + int byte_count = 0; |
| 125 | + |
| 126 | + while (byte_count < file_size) { |
| 127 | + if(byte_count + chunck_size > file_size) |
| 128 | + chunck_size = file_size - byte_count; |
| 129 | + int ret = fwrite(&wifi_firmware_image_data[byte_count], chunck_size, 1, fp); |
| 130 | + if (ret != 1) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + byte_count += chunck_size; |
| 134 | + } |
| 135 | + fclose(fp); |
| 136 | + |
| 137 | + chunck_size = 1024; |
| 138 | + byte_count = 0; |
| 139 | + const uint32_t offset = 15 * 1024 * 1024 + 1024 * 512; |
| 140 | + |
| 141 | + while (byte_count < file_size) { |
| 142 | + if(byte_count + chunck_size > file_size) |
| 143 | + chunck_size = file_size - byte_count; |
| 144 | + int ret = _root.program(wifi_firmware_image_data, offset + byte_count, chunck_size); |
| 145 | + if (ret != 0) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + byte_count += chunck_size; |
| 149 | + } |
| 150 | + |
| 151 | + chunck_size = 128; |
| 152 | + byte_count = 0; |
| 153 | + fp = fopen("/wlan/cacert.pem", "wb"); |
| 154 | + |
| 155 | + while (byte_count < cacert_pem_len) { |
| 156 | + if(byte_count + chunck_size > cacert_pem_len) |
| 157 | + chunck_size = cacert_pem_len - byte_count; |
| 158 | + int ret = fwrite(&cacert_pem[byte_count], chunck_size, 1 ,fp); |
| 159 | + if (ret != 1) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + byte_count += chunck_size; |
| 163 | + } |
| 164 | + |
| 165 | + fclose(fp); |
| 166 | + _wifi_data_fs.unmount(); |
| 167 | + return true; |
| 168 | +} |
| 169 | + |
| 170 | +#endif |
0 commit comments