|
| 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) || defined(ARDUINO_PORTENTA_C33) |
| 10 | +#include "FlashFormatterQSPI.h" |
| 11 | + |
| 12 | +FlashFormatterQSPI::FlashFormatterQSPI(): |
| 13 | +_root(BlockDevice::get_default_instance()), |
| 14 | +_wifiData(_root, 1), |
| 15 | +_wifiFS("wlan"), |
| 16 | +_otaData(_root, 2), |
| 17 | +_otaFS("fs"), |
| 18 | +_kvstoreData(_root, 3), |
| 19 | +_runtimeData(_root, 4), |
| 20 | +_runtimeFS("opt"), |
| 21 | +_runtimeFormat(false) |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +bool FlashFormatterQSPI::checkPartition() |
| 26 | +{ |
| 27 | + if (_root->init() != BD_ERROR_OK) { |
| 28 | + _runtimeFormat = true; |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + /* check PLC runtime partition. This should be performed as first check to |
| 33 | + * correctly set the _runtimeFormat flag. |
| 34 | + */ |
| 35 | + _runtimeFormat = false; |
| 36 | + if (_runtimeData.init() != BD_ERROR_OK) { |
| 37 | + _runtimeFormat = true; |
| 38 | + return false; |
| 39 | + } |
| 40 | + _runtimeData.deinit(); |
| 41 | + |
| 42 | + /* check WiFi partition */ |
| 43 | + if (_wifiData.init() != BD_ERROR_OK || _wifiFS.mount(&_wifiData) != 0) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + if (!checkWiFiData()) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + _wifiFS.unmount(); |
| 52 | + _wifiData.deinit(); |
| 53 | + |
| 54 | + /* check OTA partition */ |
| 55 | + if (_otaData.init() != BD_ERROR_OK || _otaFS.mount(&_otaData) != 0) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + if (_otaData.size() < 5 * 1024 * 1024) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + _otaFS.unmount(); |
| 64 | + _otaData.deinit(); |
| 65 | + |
| 66 | + /* check KVStore partition */ |
| 67 | + if (_kvstoreData.init() != BD_ERROR_OK) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + if (_kvstoreData.size() < 1 * 1024 * 1024) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + _kvstoreData.deinit(); |
| 75 | + |
| 76 | + _root->deinit(); |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +bool FlashFormatterQSPI::formatPartition() { |
| 81 | + |
| 82 | + if (_root->init() != BD_ERROR_OK) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + if (_root->erase(0x0, _root->get_erase_size()) != BD_ERROR_OK) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + MBRBlockDevice::partition(_root, 1, 0x0B, 0, 1 * 1024 * 1024); |
| 91 | + if (_wifiFS.reformat(&_wifiData) != 0) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + if (!restoreWifiData()) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + _wifiFS.unmount(); |
| 99 | + |
| 100 | + MBRBlockDevice::partition(_root, 2, 0x0B, 1 * 1024 * 1024, 6 * 1024 * 1024); |
| 101 | + if (_otaFS.reformat(&_otaData) != 0) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + _otaFS.unmount(); |
| 105 | + |
| 106 | + MBRBlockDevice::partition(_root, 3, 0x0B, 6 * 1024 * 1024, 7 * 1024 * 1024); |
| 107 | + |
| 108 | + if (_runtimeFormat) { |
| 109 | + MBRBlockDevice::partition(_root, 4, 0x0B, 7 * 1024 * 1024, 14 * 1024 * 1024); |
| 110 | + if (_runtimeFS.reformat(&_runtimeData) != 0) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + _runtimeFS.unmount(); |
| 114 | + } |
| 115 | + |
| 116 | + _root->deinit(); |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +bool FlashFormatterQSPI::checkFile(const char* partition, const char* filename) |
| 122 | +{ |
| 123 | + DIR *dir; |
| 124 | + struct dirent *ent; |
| 125 | + |
| 126 | + if ((dir = opendir(partition)) == NULL) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + bool foundFile = false; |
| 131 | + while ((ent = readdir (dir)) != NULL) { |
| 132 | + String fullname = String(partition) + "/" + String(ent->d_name); |
| 133 | + if (fullname == String(partition) + "/" + String(filename)) { |
| 134 | + foundFile = true; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + closedir (dir); |
| 139 | + return foundFile; |
| 140 | +} |
| 141 | + |
| 142 | +bool FlashFormatterQSPI::writeFile(const char* partition, const char* filename, const uint8_t* data, size_t size, size_t maxChunkSize) |
| 143 | +{ |
| 144 | + String fullname = String(partition) + "/" + String(filename); |
| 145 | + FILE* fp = fopen(fullname.c_str(), "wb"); |
| 146 | + if (!fp) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + size_t chunkSize = maxChunkSize; |
| 151 | + size_t byteCount = 0; |
| 152 | + while (byteCount < size) { |
| 153 | + if (byteCount + chunkSize > size) |
| 154 | + chunkSize = size - byteCount; |
| 155 | + int ret = fwrite(&data[byteCount], chunkSize, 1, fp); |
| 156 | + if (ret != 1) { |
| 157 | + fclose(fp); |
| 158 | + return false; |
| 159 | + } |
| 160 | + byteCount += chunkSize; |
| 161 | + } |
| 162 | + size_t written = ftell(fp); |
| 163 | + fclose(fp); |
| 164 | + |
| 165 | + return written == size; |
| 166 | +} |
| 167 | + |
| 168 | +bool FlashFormatterQSPI::writeFlash(const uint8_t* data, size_t size, size_t offset, size_t maxChunkSize) |
| 169 | +{ |
| 170 | + size_t chunkSize = maxChunkSize; |
| 171 | + size_t byteCount = 0; |
| 172 | + while (byteCount < size) { |
| 173 | + if(byteCount + chunkSize > size) |
| 174 | + chunkSize = size - byteCount; |
| 175 | + int ret = _root->program(data, offset + byteCount, chunkSize); |
| 176 | + if (ret != 0) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + byteCount += chunkSize; |
| 180 | + } |
| 181 | + return true; |
| 182 | +} |
| 183 | + |
| 184 | +#endif |
0 commit comments