,,

Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2018 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include "storage-helper/storage-helper.h"
leothedragon 0:8f0bb79ddd48 20 #include "mbed_trace.h"
leothedragon 0:8f0bb79ddd48 21
leothedragon 0:8f0bb79ddd48 22 #define TRACE_GROUP "SMCS"
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 StorageHelper::StorageHelper(BlockDevice *bd, FileSystem *fs)
leothedragon 0:8f0bb79ddd48 25 : _bd(bd), _fs(fs), fs1(NULL), fs2(NULL), part1(NULL), part2(NULL)
leothedragon 0:8f0bb79ddd48 26 {
leothedragon 0:8f0bb79ddd48 27 }
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 int StorageHelper::init() {
leothedragon 0:8f0bb79ddd48 30 static bool init_done = false;
leothedragon 0:8f0bb79ddd48 31 int status = 0;
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 if(!init_done) {
leothedragon 0:8f0bb79ddd48 34 if (_bd) {
leothedragon 0:8f0bb79ddd48 35 status = _bd->init();
leothedragon 0:8f0bb79ddd48 36
leothedragon 0:8f0bb79ddd48 37 if (status != BD_ERROR_OK) {
leothedragon 0:8f0bb79ddd48 38 tr_warn("bd->init() failed with %d", status);
leothedragon 0:8f0bb79ddd48 39 return -1;
leothedragon 0:8f0bb79ddd48 40 }
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 #if (MCC_PLATFORM_PARTITION_MODE == 1)
leothedragon 0:8f0bb79ddd48 43 // store partition size
leothedragon 0:8f0bb79ddd48 44 mcc_platform_storage_size = _bd->size();
leothedragon 0:8f0bb79ddd48 45 tr_debug("init() - BlockDevice init OK, bd->size() = %llu", mcc_platform_storage_size);
leothedragon 0:8f0bb79ddd48 46 #else
leothedragon 0:8f0bb79ddd48 47 tr_debug("init() - BlockDevice init OK, bd->size() = %llu", _bd->size());
leothedragon 0:8f0bb79ddd48 48 #endif
leothedragon 0:8f0bb79ddd48 49
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51
leothedragon 0:8f0bb79ddd48 52 #if (MCC_PLATFORM_PARTITION_MODE == 1)
leothedragon 0:8f0bb79ddd48 53 #if (NUMBER_OF_PARTITIONS > 0)
leothedragon 0:8f0bb79ddd48 54 status = init_and_mount_partition(&fs1, &part1, PRIMARY_PARTITION_NUMBER, ((const char*) MOUNT_POINT_PRIMARY+1));
leothedragon 0:8f0bb79ddd48 55 if (status != 0) {
leothedragon 0:8f0bb79ddd48 56 #if (MCC_PLATFORM_AUTO_PARTITION == 1)
leothedragon 0:8f0bb79ddd48 57 status = create_partitions();
leothedragon 0:8f0bb79ddd48 58 if (status != 0) {
leothedragon 0:8f0bb79ddd48 59 return status;
leothedragon 0:8f0bb79ddd48 60 }
leothedragon 0:8f0bb79ddd48 61 #else
leothedragon 0:8f0bb79ddd48 62 tr_warn("primary partition init failed");
leothedragon 0:8f0bb79ddd48 63 return status;
leothedragon 0:8f0bb79ddd48 64 #endif
leothedragon 0:8f0bb79ddd48 65 }
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 #if (NUMBER_OF_PARTITIONS == 2)
leothedragon 0:8f0bb79ddd48 68 status = init_and_mount_partition(&fs2, &part2, SECONDARY_PARTITION_NUMBER, ((const char*) MOUNT_POINT_SECONDARY+1));
leothedragon 0:8f0bb79ddd48 69 if (status != 0) {
leothedragon 0:8f0bb79ddd48 70 #if (MCC_PLATFORM_AUTO_PARTITION == 1)
leothedragon 0:8f0bb79ddd48 71 status = create_partitions();
leothedragon 0:8f0bb79ddd48 72 if (status != 0) {
leothedragon 0:8f0bb79ddd48 73 return status;
leothedragon 0:8f0bb79ddd48 74 }
leothedragon 0:8f0bb79ddd48 75 #else
leothedragon 0:8f0bb79ddd48 76 tr_warn("secondary partition init failed");
leothedragon 0:8f0bb79ddd48 77 return status;
leothedragon 0:8f0bb79ddd48 78 #endif
leothedragon 0:8f0bb79ddd48 79 }
leothedragon 0:8f0bb79ddd48 80 #endif // (NUMBER_OF_PARTITIONS == 2)
leothedragon 0:8f0bb79ddd48 81 #if (NUMBER_OF_PARTITIONS > 2)
leothedragon 0:8f0bb79ddd48 82 #error "Invalid number of partitions!!!"
leothedragon 0:8f0bb79ddd48 83 #endif
leothedragon 0:8f0bb79ddd48 84 #endif // (NUMBER_OF_PARTITIONS > 0)
leothedragon 0:8f0bb79ddd48 85 #else // Else for #if (MCC_PLATFORM_PARTITION_MODE == 1)
leothedragon 0:8f0bb79ddd48 86
leothedragon 0:8f0bb79ddd48 87 fs1 = _fs;
leothedragon 0:8f0bb79ddd48 88 part1 = _bd; /* required for mcc_platform_reformat_storage */
leothedragon 0:8f0bb79ddd48 89 status = test_filesystem(fs1, _bd);
leothedragon 0:8f0bb79ddd48 90 if (status != 0) {
leothedragon 0:8f0bb79ddd48 91 tr_info("Formatting...");
leothedragon 0:8f0bb79ddd48 92 status = reformat_partition(fs1, _bd);
leothedragon 0:8f0bb79ddd48 93 if (status != 0) {
leothedragon 0:8f0bb79ddd48 94 tr_warn("Formatting failed with 0x%X", status);
leothedragon 0:8f0bb79ddd48 95 return status;
leothedragon 0:8f0bb79ddd48 96 }
leothedragon 0:8f0bb79ddd48 97 }
leothedragon 0:8f0bb79ddd48 98 #endif // MCC_PLATFORM_PARTITION_MODE
leothedragon 0:8f0bb79ddd48 99 init_done = true;
leothedragon 0:8f0bb79ddd48 100 }
leothedragon 0:8f0bb79ddd48 101 else {
leothedragon 0:8f0bb79ddd48 102 tr_debug("init already done");
leothedragon 0:8f0bb79ddd48 103 }
leothedragon 0:8f0bb79ddd48 104
leothedragon 0:8f0bb79ddd48 105 return status;
leothedragon 0:8f0bb79ddd48 106 }
leothedragon 0:8f0bb79ddd48 107
leothedragon 0:8f0bb79ddd48 108 int StorageHelper::sotp_init(void)
leothedragon 0:8f0bb79ddd48 109 {
leothedragon 0:8f0bb79ddd48 110 int status = FCC_STATUS_SUCCESS;
leothedragon 0:8f0bb79ddd48 111 // Include this only for Developer mode and a device which doesn't have in-built TRNG support.
leothedragon 0:8f0bb79ddd48 112 #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1
leothedragon 0:8f0bb79ddd48 113 #ifdef PAL_USER_DEFINED_CONFIGURATION
leothedragon 0:8f0bb79ddd48 114 #if !PAL_USE_HW_TRNG
leothedragon 0:8f0bb79ddd48 115 status = fcc_entropy_set(MBED_CLOUD_DEV_ENTROPY, FCC_ENTROPY_SIZE);
leothedragon 0:8f0bb79ddd48 116
leothedragon 0:8f0bb79ddd48 117 if (status != FCC_STATUS_SUCCESS && status != FCC_STATUS_ENTROPY_ERROR) {
leothedragon 0:8f0bb79ddd48 118 tr_error("fcc_entropy_set failed with status %d", status);
leothedragon 0:8f0bb79ddd48 119 fcc_finalize();
leothedragon 0:8f0bb79ddd48 120 return status;
leothedragon 0:8f0bb79ddd48 121 }
leothedragon 0:8f0bb79ddd48 122 #endif // PAL_USE_HW_TRNG = 0
leothedragon 0:8f0bb79ddd48 123 /* Include this only for Developer mode. The application will use fixed RoT to simplify user-experience with the application.
leothedragon 0:8f0bb79ddd48 124 * With this change the application be reflashed/SOTP can be erased safely without invalidating the application credentials.
leothedragon 0:8f0bb79ddd48 125 */
leothedragon 0:8f0bb79ddd48 126 status = fcc_rot_set(MBED_CLOUD_DEV_ROT, FCC_ROT_SIZE);
leothedragon 0:8f0bb79ddd48 127
leothedragon 0:8f0bb79ddd48 128 if (status != FCC_STATUS_SUCCESS && status != FCC_STATUS_ROT_ERROR) {
leothedragon 0:8f0bb79ddd48 129 tr_error("fcc_rot_set failed with status %d", status);
leothedragon 0:8f0bb79ddd48 130 fcc_finalize();
leothedragon 0:8f0bb79ddd48 131 } else {
leothedragon 0:8f0bb79ddd48 132 // We can return SUCCESS here as preexisting RoT/Entropy is expected flow.
leothedragon 0:8f0bb79ddd48 133 tr_info("Using hardcoded Root of Trust, not suitable for production use");
leothedragon 0:8f0bb79ddd48 134 status = FCC_STATUS_SUCCESS;
leothedragon 0:8f0bb79ddd48 135 }
leothedragon 0:8f0bb79ddd48 136 #endif // PAL_USER_DEFINED_CONFIGURATION
leothedragon 0:8f0bb79ddd48 137 #endif // #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1
leothedragon 0:8f0bb79ddd48 138 return status;
leothedragon 0:8f0bb79ddd48 139 }
leothedragon 0:8f0bb79ddd48 140
leothedragon 0:8f0bb79ddd48 141
leothedragon 0:8f0bb79ddd48 142 int StorageHelper::reformat_storage(void) {
leothedragon 0:8f0bb79ddd48 143 int status = -1;
leothedragon 0:8f0bb79ddd48 144
leothedragon 0:8f0bb79ddd48 145 if (_bd) {
leothedragon 0:8f0bb79ddd48 146 #if (NUMBER_OF_PARTITIONS > 0)
leothedragon 0:8f0bb79ddd48 147 status = reformat_partition(fs1, part1);
leothedragon 0:8f0bb79ddd48 148 if (status != 0) {
leothedragon 0:8f0bb79ddd48 149 tr_warn("Formatting primary partition failed with 0x%X", status);
leothedragon 0:8f0bb79ddd48 150 return status;
leothedragon 0:8f0bb79ddd48 151 }
leothedragon 0:8f0bb79ddd48 152 #if (NUMBER_OF_PARTITIONS == 2)
leothedragon 0:8f0bb79ddd48 153 status = reformat_partition(fs2, part2);
leothedragon 0:8f0bb79ddd48 154 if (status != 0) {
leothedragon 0:8f0bb79ddd48 155 tr_warn("Formatting secondary partition failed with 0x%X", status);
leothedragon 0:8f0bb79ddd48 156 return status;
leothedragon 0:8f0bb79ddd48 157 }
leothedragon 0:8f0bb79ddd48 158 #endif
leothedragon 0:8f0bb79ddd48 159 #if (NUMBER_OF_PARTITIONS > 2)
leothedragon 0:8f0bb79ddd48 160 #error "Invalid number of partitions!!!"
leothedragon 0:8f0bb79ddd48 161 #endif
leothedragon 0:8f0bb79ddd48 162 #endif
leothedragon 0:8f0bb79ddd48 163
leothedragon 0:8f0bb79ddd48 164 #if NUMBER_OF_PARTITIONS == 0
leothedragon 0:8f0bb79ddd48 165 status = StorageHelper::format(_fs, _bd);
leothedragon 0:8f0bb79ddd48 166 #endif
leothedragon 0:8f0bb79ddd48 167 }
leothedragon 0:8f0bb79ddd48 168
leothedragon 0:8f0bb79ddd48 169 tr_info("Storage reformatted (%d)", status);
leothedragon 0:8f0bb79ddd48 170
leothedragon 0:8f0bb79ddd48 171 return status;
leothedragon 0:8f0bb79ddd48 172 }
leothedragon 0:8f0bb79ddd48 173
leothedragon 0:8f0bb79ddd48 174 int StorageHelper::format(FileSystem *fs, BlockDevice *bd) {
leothedragon 0:8f0bb79ddd48 175 if (!fs || !bd) return -1;
leothedragon 0:8f0bb79ddd48 176
leothedragon 0:8f0bb79ddd48 177 int status;
leothedragon 0:8f0bb79ddd48 178
leothedragon 0:8f0bb79ddd48 179 status = bd->init();
leothedragon 0:8f0bb79ddd48 180 if (status != 0) {
leothedragon 0:8f0bb79ddd48 181 return status;
leothedragon 0:8f0bb79ddd48 182 }
leothedragon 0:8f0bb79ddd48 183
leothedragon 0:8f0bb79ddd48 184 status = fs->mount(bd);
leothedragon 0:8f0bb79ddd48 185 // might fail because already mounted, so ignore
leothedragon 0:8f0bb79ddd48 186
leothedragon 0:8f0bb79ddd48 187 status = fs->reformat(bd);
leothedragon 0:8f0bb79ddd48 188 if (status != 0) {
leothedragon 0:8f0bb79ddd48 189 if (bd->erase(0, bd->size()) == 0) {
leothedragon 0:8f0bb79ddd48 190 if (fs->reformat(bd) == 0) {
leothedragon 0:8f0bb79ddd48 191 status = 0;
leothedragon 0:8f0bb79ddd48 192 printf("The storage reformatted successfully.\n");
leothedragon 0:8f0bb79ddd48 193 }
leothedragon 0:8f0bb79ddd48 194 }
leothedragon 0:8f0bb79ddd48 195 }
leothedragon 0:8f0bb79ddd48 196
leothedragon 0:8f0bb79ddd48 197 return status;
leothedragon 0:8f0bb79ddd48 198 }
leothedragon 0:8f0bb79ddd48 199
leothedragon 0:8f0bb79ddd48 200 #if (MCC_PLATFORM_PARTITION_MODE == 1)
leothedragon 0:8f0bb79ddd48 201 // bd must be initialized before calling this function.
leothedragon 0:8f0bb79ddd48 202 int StorageHelper::init_and_mount_partition(FileSystem **fs, BlockDevice** part, int number_of_partition, const char* mount_point) {
leothedragon 0:8f0bb79ddd48 203 int status;
leothedragon 0:8f0bb79ddd48 204
leothedragon 0:8f0bb79ddd48 205 // Init fs only once.
leothedragon 0:8f0bb79ddd48 206 if (&(**fs) == NULL) {
leothedragon 0:8f0bb79ddd48 207 if (&(**part) == NULL) {
leothedragon 0:8f0bb79ddd48 208 *part = new MBRBlockDevice(_bd, number_of_partition);
leothedragon 0:8f0bb79ddd48 209 }
leothedragon 0:8f0bb79ddd48 210 status = (**part).init();
leothedragon 0:8f0bb79ddd48 211 if (status != 0) {
leothedragon 0:8f0bb79ddd48 212 (**part).deinit();
leothedragon 0:8f0bb79ddd48 213 tr_warn("Init of partition %d fail", number_of_partition);
leothedragon 0:8f0bb79ddd48 214 return status;
leothedragon 0:8f0bb79ddd48 215 }
leothedragon 0:8f0bb79ddd48 216 /* This next change mean that filesystem will be FAT. */
leothedragon 0:8f0bb79ddd48 217 *fs = new FATFileSystem(mount_point, &(**part)); /* this also mount fs. */
leothedragon 0:8f0bb79ddd48 218 }
leothedragon 0:8f0bb79ddd48 219 // re-init and format.
leothedragon 0:8f0bb79ddd48 220 else {
leothedragon 0:8f0bb79ddd48 221 status = (**part).init();
leothedragon 0:8f0bb79ddd48 222 if (status != 0) {
leothedragon 0:8f0bb79ddd48 223 (**part).deinit();
leothedragon 0:8f0bb79ddd48 224 tr_warn("Init of partition %d fail", number_of_partition);
leothedragon 0:8f0bb79ddd48 225 return status;
leothedragon 0:8f0bb79ddd48 226 }
leothedragon 0:8f0bb79ddd48 227
leothedragon 0:8f0bb79ddd48 228 tr_debug("Formatting partition %d ...", number_of_partition);
leothedragon 0:8f0bb79ddd48 229 status = reformat_partition(&(**fs), &(**part));
leothedragon 0:8f0bb79ddd48 230 if (status != 0) {
leothedragon 0:8f0bb79ddd48 231 tr_warn("Formatting partition %d failed with 0x%X", number_of_partition, status);
leothedragon 0:8f0bb79ddd48 232 return status;
leothedragon 0:8f0bb79ddd48 233 }
leothedragon 0:8f0bb79ddd48 234 }
leothedragon 0:8f0bb79ddd48 235
leothedragon 0:8f0bb79ddd48 236 status = test_filesystem(&(**fs), &(**part));
leothedragon 0:8f0bb79ddd48 237 if (status != 0) {
leothedragon 0:8f0bb79ddd48 238 tr_debug("Formatting partition %d ...", number_of_partition);
leothedragon 0:8f0bb79ddd48 239 status = reformat_partition(&(**fs), &(**part));
leothedragon 0:8f0bb79ddd48 240 if (status != 0) {
leothedragon 0:8f0bb79ddd48 241 tr_warn("Formatting partition %d failed with 0x%X", number_of_partition, status);
leothedragon 0:8f0bb79ddd48 242 return status;
leothedragon 0:8f0bb79ddd48 243 }
leothedragon 0:8f0bb79ddd48 244 }
leothedragon 0:8f0bb79ddd48 245
leothedragon 0:8f0bb79ddd48 246 return status;
leothedragon 0:8f0bb79ddd48 247 }
leothedragon 0:8f0bb79ddd48 248 #endif
leothedragon 0:8f0bb79ddd48 249
leothedragon 0:8f0bb79ddd48 250 int StorageHelper::reformat_partition(FileSystem *fs, BlockDevice* part) {
leothedragon 0:8f0bb79ddd48 251 return fs->reformat(part);
leothedragon 0:8f0bb79ddd48 252 }
leothedragon 0:8f0bb79ddd48 253
leothedragon 0:8f0bb79ddd48 254 /* help function for testing filesystem availbility by umount and
leothedragon 0:8f0bb79ddd48 255 * mount filesystem again.
leothedragon 0:8f0bb79ddd48 256 * */
leothedragon 0:8f0bb79ddd48 257 int StorageHelper::test_filesystem(FileSystem *fs, BlockDevice* part) {
leothedragon 0:8f0bb79ddd48 258 // unmount
leothedragon 0:8f0bb79ddd48 259 int status = fs->unmount();
leothedragon 0:8f0bb79ddd48 260 if (status != 0) {
leothedragon 0:8f0bb79ddd48 261 tr_info("test_filesystem() - unmount fail %d", status);
leothedragon 0:8f0bb79ddd48 262 // should be OK, maybe not mounted...
leothedragon 0:8f0bb79ddd48 263 }
leothedragon 0:8f0bb79ddd48 264 // mount again
leothedragon 0:8f0bb79ddd48 265 status = fs->mount(part);
leothedragon 0:8f0bb79ddd48 266 if (status != 0) {
leothedragon 0:8f0bb79ddd48 267 tr_info("test_filesystem() - mount fail %d", status);
leothedragon 0:8f0bb79ddd48 268 return -1;
leothedragon 0:8f0bb79ddd48 269 }
leothedragon 0:8f0bb79ddd48 270 return status;
leothedragon 0:8f0bb79ddd48 271 }
leothedragon 0:8f0bb79ddd48 272
leothedragon 0:8f0bb79ddd48 273 // create partitions, initialize and mount partitions
leothedragon 0:8f0bb79ddd48 274 #if ((MCC_PLATFORM_PARTITION_MODE == 1) && (MCC_PLATFORM_AUTO_PARTITION == 1))
leothedragon 0:8f0bb79ddd48 275 int StorageHelper::create_partitions(void) {
leothedragon 0:8f0bb79ddd48 276 int status;
leothedragon 0:8f0bb79ddd48 277
leothedragon 0:8f0bb79ddd48 278 #if (NUMBER_OF_PARTITIONS > 0)
leothedragon 0:8f0bb79ddd48 279 if (mcc_platform_storage_size < PRIMARY_PARTITION_SIZE) {
leothedragon 0:8f0bb79ddd48 280 tr_error("create_partitions PRIMARY_PARTITION_SIZE too large!!! Storage's size is %" PRIu64 \
leothedragon 0:8f0bb79ddd48 281 " and PRIMARY_PARTITION_SIZE is %" PRIu64,
leothedragon 0:8f0bb79ddd48 282 (uint64_t)mcc_platform_storage_size, (uint64_t)PRIMARY_PARTITION_SIZE);
leothedragon 0:8f0bb79ddd48 283 assert(0);
leothedragon 0:8f0bb79ddd48 284 }
leothedragon 0:8f0bb79ddd48 285
leothedragon 0:8f0bb79ddd48 286 status = MBRBlockDevice::partition(_bd, PRIMARY_PARTITION_NUMBER, 0x83, PRIMARY_PARTITION_START, PRIMARY_PARTITION_START + PRIMARY_PARTITION_SIZE);
leothedragon 0:8f0bb79ddd48 287 tr_debug("Creating primary partition ...");
leothedragon 0:8f0bb79ddd48 288 if (status != 0) {
leothedragon 0:8f0bb79ddd48 289 tr_warn("Creating primary partition failed 0x%X", status);
leothedragon 0:8f0bb79ddd48 290 return status;
leothedragon 0:8f0bb79ddd48 291 }
leothedragon 0:8f0bb79ddd48 292 tr_debug("Created primary partition");
leothedragon 0:8f0bb79ddd48 293
leothedragon 0:8f0bb79ddd48 294 // init and format partition 1
leothedragon 0:8f0bb79ddd48 295 status = init_and_mount_partition(&fs1, &part1, PRIMARY_PARTITION_NUMBER, ((const char*) MOUNT_POINT_PRIMARY+1));
leothedragon 0:8f0bb79ddd48 296 if (status != 0) {
leothedragon 0:8f0bb79ddd48 297 return status;
leothedragon 0:8f0bb79ddd48 298 }
leothedragon 0:8f0bb79ddd48 299 tr_debug("Mounted primary partition");
leothedragon 0:8f0bb79ddd48 300
leothedragon 0:8f0bb79ddd48 301 #if (NUMBER_OF_PARTITIONS == 2)
leothedragon 0:8f0bb79ddd48 302 // use cast (uint64_t) for fixing compile warning.
leothedragon 0:8f0bb79ddd48 303 if (mcc_platform_storage_size < ((uint64_t)PRIMARY_PARTITION_SIZE + (uint64_t)SECONDARY_PARTITION_SIZE)) {
leothedragon 0:8f0bb79ddd48 304 tr_error("create_partitions (PRIMARY_PARTITION_SIZE+SECONDARY_PARTITION_SIZE) too large!!! Storage's size is %" PRIu64 \
leothedragon 0:8f0bb79ddd48 305 " and (PRIMARY_PARTITION_SIZE+SECONDARY_PARTITION_SIZE) %" PRIu64,
leothedragon 0:8f0bb79ddd48 306 (uint64_t)mcc_platform_storage_size, (uint64_t)(PRIMARY_PARTITION_SIZE+SECONDARY_PARTITION_SIZE));
leothedragon 0:8f0bb79ddd48 307 assert(0);
leothedragon 0:8f0bb79ddd48 308 }
leothedragon 0:8f0bb79ddd48 309
leothedragon 0:8f0bb79ddd48 310 // use cast (uint64_t) for fixing compile warning.
leothedragon 0:8f0bb79ddd48 311 status = MBRBlockDevice::partition(_bd, SECONDARY_PARTITION_NUMBER, 0x83, SECONDARY_PARTITION_START, (uint64_t) SECONDARY_PARTITION_START + (uint64_t) SECONDARY_PARTITION_SIZE);
leothedragon 0:8f0bb79ddd48 312 tr_debug("Creating secondary partition ...");
leothedragon 0:8f0bb79ddd48 313 if (status != 0) {
leothedragon 0:8f0bb79ddd48 314 tr_warn("Creating secondary partition failed 0x%X", status);
leothedragon 0:8f0bb79ddd48 315 return status;
leothedragon 0:8f0bb79ddd48 316 }
leothedragon 0:8f0bb79ddd48 317 tr_debug("Created secondary partition");
leothedragon 0:8f0bb79ddd48 318
leothedragon 0:8f0bb79ddd48 319 // init and format partition 2
leothedragon 0:8f0bb79ddd48 320 status = init_and_mount_partition(&fs2, &part2, SECONDARY_PARTITION_NUMBER, ((const char*) MOUNT_POINT_SECONDARY+1));
leothedragon 0:8f0bb79ddd48 321 if (status != 0) {
leothedragon 0:8f0bb79ddd48 322 return status;
leothedragon 0:8f0bb79ddd48 323 }
leothedragon 0:8f0bb79ddd48 324 tr_debug("Mounted secondary partition");
leothedragon 0:8f0bb79ddd48 325 #endif
leothedragon 0:8f0bb79ddd48 326 #if (NUMBER_OF_PARTITIONS > 2)
leothedragon 0:8f0bb79ddd48 327 #error "Invalid number of partitions!!!"
leothedragon 0:8f0bb79ddd48 328 #endif
leothedragon 0:8f0bb79ddd48 329 #endif // (NUMBER_OF_PARTITIONS > 0)
leothedragon 0:8f0bb79ddd48 330 return status;
leothedragon 0:8f0bb79ddd48 331 }
leothedragon 0:8f0bb79ddd48 332 #endif // ((MCC_PLATFORM_PARTITION_MODE == 1) && (MCC_PLATFORM_AUTO_PARTITION == 1))