Example
Dependencies: FXAS21002 FXOS8700Q
simple-mbed-cloud-client/storage-helper/storage-helper.h@0:11cc2b7889af, 2019-11-19 (annotated)
- Committer:
- maygup01
- Date:
- Tue Nov 19 09:49:38 2019 +0000
- Revision:
- 0:11cc2b7889af
Example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maygup01 | 0:11cc2b7889af | 1 | // ---------------------------------------------------------------------------- |
maygup01 | 0:11cc2b7889af | 2 | // Copyright 2016-2018 ARM Ltd. |
maygup01 | 0:11cc2b7889af | 3 | // |
maygup01 | 0:11cc2b7889af | 4 | // SPDX-License-Identifier: Apache-2.0 |
maygup01 | 0:11cc2b7889af | 5 | // |
maygup01 | 0:11cc2b7889af | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
maygup01 | 0:11cc2b7889af | 7 | // you may not use this file except in compliance with the License. |
maygup01 | 0:11cc2b7889af | 8 | // You may obtain a copy of the License at |
maygup01 | 0:11cc2b7889af | 9 | // |
maygup01 | 0:11cc2b7889af | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
maygup01 | 0:11cc2b7889af | 11 | // |
maygup01 | 0:11cc2b7889af | 12 | // Unless required by applicable law or agreed to in writing, software |
maygup01 | 0:11cc2b7889af | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
maygup01 | 0:11cc2b7889af | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maygup01 | 0:11cc2b7889af | 15 | // See the License for the specific language governing permissions and |
maygup01 | 0:11cc2b7889af | 16 | // limitations under the License. |
maygup01 | 0:11cc2b7889af | 17 | // ---------------------------------------------------------------------------- |
maygup01 | 0:11cc2b7889af | 18 | |
maygup01 | 0:11cc2b7889af | 19 | #ifndef SIMPLEMBEDCLOUDCLIENT_STORAGEHELPER_H_ |
maygup01 | 0:11cc2b7889af | 20 | #define SIMPLEMBEDCLOUDCLIENT_STORAGEHELPER_H_ |
maygup01 | 0:11cc2b7889af | 21 | |
maygup01 | 0:11cc2b7889af | 22 | #include "mbed.h" |
maygup01 | 0:11cc2b7889af | 23 | #include "BlockDevice.h" |
maygup01 | 0:11cc2b7889af | 24 | #include "FileSystem.h" |
maygup01 | 0:11cc2b7889af | 25 | #include "factory_configurator_client.h" |
maygup01 | 0:11cc2b7889af | 26 | |
maygup01 | 0:11cc2b7889af | 27 | // This is for single or dual partition mode. This is supposed to be used with storage for data e.g. SD card. |
maygup01 | 0:11cc2b7889af | 28 | // Enable by 1/disable by 0. |
maygup01 | 0:11cc2b7889af | 29 | #ifndef MCC_PLATFORM_PARTITION_MODE |
maygup01 | 0:11cc2b7889af | 30 | #define MCC_PLATFORM_PARTITION_MODE 0 |
maygup01 | 0:11cc2b7889af | 31 | #endif |
maygup01 | 0:11cc2b7889af | 32 | |
maygup01 | 0:11cc2b7889af | 33 | #include "pal.h" |
maygup01 | 0:11cc2b7889af | 34 | #if (MCC_PLATFORM_PARTITION_MODE == 1) |
maygup01 | 0:11cc2b7889af | 35 | #include "MBRBlockDevice.h" |
maygup01 | 0:11cc2b7889af | 36 | #include "FATFileSystem.h" |
maygup01 | 0:11cc2b7889af | 37 | |
maygup01 | 0:11cc2b7889af | 38 | // Set to 1 for enabling automatic partitioning storage if required. This is effective only if MCC_PLATFORM_PARTITION_MODE is defined to 1. |
maygup01 | 0:11cc2b7889af | 39 | // Partioning will be triggered only if initialization of available partitions fail. |
maygup01 | 0:11cc2b7889af | 40 | #ifndef MCC_PLATFORM_AUTO_PARTITION |
maygup01 | 0:11cc2b7889af | 41 | #define MCC_PLATFORM_AUTO_PARTITION 0 |
maygup01 | 0:11cc2b7889af | 42 | #endif |
maygup01 | 0:11cc2b7889af | 43 | |
maygup01 | 0:11cc2b7889af | 44 | #ifndef PRIMARY_PARTITION_NUMBER |
maygup01 | 0:11cc2b7889af | 45 | #define PRIMARY_PARTITION_NUMBER 1 |
maygup01 | 0:11cc2b7889af | 46 | #endif |
maygup01 | 0:11cc2b7889af | 47 | |
maygup01 | 0:11cc2b7889af | 48 | #ifndef PRIMARY_PARTITION_START |
maygup01 | 0:11cc2b7889af | 49 | #define PRIMARY_PARTITION_START 0 |
maygup01 | 0:11cc2b7889af | 50 | #endif |
maygup01 | 0:11cc2b7889af | 51 | |
maygup01 | 0:11cc2b7889af | 52 | #ifndef PRIMARY_PARTITION_SIZE |
maygup01 | 0:11cc2b7889af | 53 | #define PRIMARY_PARTITION_SIZE 1024*1024*1024 // default partition size 1GB |
maygup01 | 0:11cc2b7889af | 54 | #endif |
maygup01 | 0:11cc2b7889af | 55 | |
maygup01 | 0:11cc2b7889af | 56 | #ifndef SECONDARY_PARTITION_NUMBER |
maygup01 | 0:11cc2b7889af | 57 | #define SECONDARY_PARTITION_NUMBER 2 |
maygup01 | 0:11cc2b7889af | 58 | #endif |
maygup01 | 0:11cc2b7889af | 59 | |
maygup01 | 0:11cc2b7889af | 60 | #ifndef SECONDARY_PARTITION_START |
maygup01 | 0:11cc2b7889af | 61 | #define SECONDARY_PARTITION_START PRIMARY_PARTITION_SIZE |
maygup01 | 0:11cc2b7889af | 62 | #endif |
maygup01 | 0:11cc2b7889af | 63 | |
maygup01 | 0:11cc2b7889af | 64 | #ifndef SECONDARY_PARTITION_SIZE |
maygup01 | 0:11cc2b7889af | 65 | #define SECONDARY_PARTITION_SIZE 1024*1024*1024 // default partition size 1GB |
maygup01 | 0:11cc2b7889af | 66 | #endif |
maygup01 | 0:11cc2b7889af | 67 | |
maygup01 | 0:11cc2b7889af | 68 | #ifndef NUMBER_OF_PARTITIONS |
maygup01 | 0:11cc2b7889af | 69 | #define NUMBER_OF_PARTITIONS PAL_NUMBER_OF_PARTITIONS |
maygup01 | 0:11cc2b7889af | 70 | #endif |
maygup01 | 0:11cc2b7889af | 71 | |
maygup01 | 0:11cc2b7889af | 72 | #ifndef MOUNT_POINT_PRIMARY |
maygup01 | 0:11cc2b7889af | 73 | #define MOUNT_POINT_PRIMARY PAL_FS_MOUNT_POINT_PRIMARY |
maygup01 | 0:11cc2b7889af | 74 | #endif |
maygup01 | 0:11cc2b7889af | 75 | |
maygup01 | 0:11cc2b7889af | 76 | #ifndef MOUNT_POINT_SECONDARY |
maygup01 | 0:11cc2b7889af | 77 | #define MOUNT_POINT_SECONDARY PAL_FS_MOUNT_POINT_SECONDARY |
maygup01 | 0:11cc2b7889af | 78 | #endif |
maygup01 | 0:11cc2b7889af | 79 | |
maygup01 | 0:11cc2b7889af | 80 | #endif // MCC_PLATFORM_PARTITION_MODE |
maygup01 | 0:11cc2b7889af | 81 | |
maygup01 | 0:11cc2b7889af | 82 | // Include this only for Developer mode and device which doesn't have in-built TRNG support |
maygup01 | 0:11cc2b7889af | 83 | #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1 |
maygup01 | 0:11cc2b7889af | 84 | #ifdef PAL_USER_DEFINED_CONFIGURATION |
maygup01 | 0:11cc2b7889af | 85 | #define FCC_ROT_SIZE 16 |
maygup01 | 0:11cc2b7889af | 86 | const uint8_t MBED_CLOUD_DEV_ROT[FCC_ROT_SIZE] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; |
maygup01 | 0:11cc2b7889af | 87 | #if !PAL_USE_HW_TRNG |
maygup01 | 0:11cc2b7889af | 88 | #define FCC_ENTROPY_SIZE 48 |
maygup01 | 0:11cc2b7889af | 89 | const uint8_t MBED_CLOUD_DEV_ENTROPY[FCC_ENTROPY_SIZE] = { 0xf6, 0xd6, 0xc0, 0x09, 0x9e, 0x6e, 0xf2, 0x37, 0xdc, 0x29, 0x88, 0xf1, 0x57, 0x32, 0x7d, 0xde, 0xac, 0xb3, 0x99, 0x8c, 0xb9, 0x11, 0x35, 0x18, 0xeb, 0x48, 0x29, 0x03, 0x6a, 0x94, 0x6d, 0xe8, 0x40, 0xc0, 0x28, 0xcc, 0xe4, 0x04, 0xc3, 0x1f, 0x4b, 0xc2, 0xe0, 0x68, 0xa0, 0x93, 0xe6, 0x3a }; |
maygup01 | 0:11cc2b7889af | 90 | #endif // PAL_USE_HW_TRNG = 0 |
maygup01 | 0:11cc2b7889af | 91 | #endif // PAL_USER_DEFINED_CONFIGURATION |
maygup01 | 0:11cc2b7889af | 92 | #endif // #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1 |
maygup01 | 0:11cc2b7889af | 93 | |
maygup01 | 0:11cc2b7889af | 94 | class StorageHelper { |
maygup01 | 0:11cc2b7889af | 95 | public: |
maygup01 | 0:11cc2b7889af | 96 | /** |
maygup01 | 0:11cc2b7889af | 97 | * Initializes a new StorageHelper. |
maygup01 | 0:11cc2b7889af | 98 | * StorageHelper manages storage across multiple partitions, |
maygup01 | 0:11cc2b7889af | 99 | * initializes SOTP, and can format the storage. |
maygup01 | 0:11cc2b7889af | 100 | * |
maygup01 | 0:11cc2b7889af | 101 | * @param bd An un-initialized block device to back the file system |
maygup01 | 0:11cc2b7889af | 102 | * @param fs An un-mounted file system |
maygup01 | 0:11cc2b7889af | 103 | */ |
maygup01 | 0:11cc2b7889af | 104 | StorageHelper(BlockDevice *bd, FileSystem *fs); |
maygup01 | 0:11cc2b7889af | 105 | |
maygup01 | 0:11cc2b7889af | 106 | /** |
maygup01 | 0:11cc2b7889af | 107 | * Initialize the storage helper, this initializes and mounts |
maygup01 | 0:11cc2b7889af | 108 | * both the block device and file system |
maygup01 | 0:11cc2b7889af | 109 | * |
maygup01 | 0:11cc2b7889af | 110 | * @returns 0 if successful, non-0 when not successful |
maygup01 | 0:11cc2b7889af | 111 | */ |
maygup01 | 0:11cc2b7889af | 112 | int init(); |
maygup01 | 0:11cc2b7889af | 113 | |
maygup01 | 0:11cc2b7889af | 114 | /** |
maygup01 | 0:11cc2b7889af | 115 | * Initialize the factory configurator client, sets entropy, |
maygup01 | 0:11cc2b7889af | 116 | * and reads root of trust. |
maygup01 | 0:11cc2b7889af | 117 | * |
maygup01 | 0:11cc2b7889af | 118 | * @returns 0 if successful, non-0 when not successful |
maygup01 | 0:11cc2b7889af | 119 | */ |
maygup01 | 0:11cc2b7889af | 120 | int sotp_init(); |
maygup01 | 0:11cc2b7889af | 121 | |
maygup01 | 0:11cc2b7889af | 122 | /** |
maygup01 | 0:11cc2b7889af | 123 | * Format the block device, and remount the filesystem |
maygup01 | 0:11cc2b7889af | 124 | * |
maygup01 | 0:11cc2b7889af | 125 | * @returns 0 if successful, non-0 when not successful |
maygup01 | 0:11cc2b7889af | 126 | */ |
maygup01 | 0:11cc2b7889af | 127 | int reformat_storage(void); |
maygup01 | 0:11cc2b7889af | 128 | |
maygup01 | 0:11cc2b7889af | 129 | /** |
maygup01 | 0:11cc2b7889af | 130 | * Initialize and format a blockdevice and file system |
maygup01 | 0:11cc2b7889af | 131 | */ |
maygup01 | 0:11cc2b7889af | 132 | static int format(FileSystem *fs, BlockDevice *bd); |
maygup01 | 0:11cc2b7889af | 133 | |
maygup01 | 0:11cc2b7889af | 134 | private: |
maygup01 | 0:11cc2b7889af | 135 | #if (MCC_PLATFORM_PARTITION_MODE == 1) |
maygup01 | 0:11cc2b7889af | 136 | // for checking that PRIMARY_PARTITION_SIZE and SECONDARY_PARTITION_SIZE do not overflow. |
maygup01 | 0:11cc2b7889af | 137 | bd_size_t mcc_platform_storage_size; |
maygup01 | 0:11cc2b7889af | 138 | |
maygup01 | 0:11cc2b7889af | 139 | /** |
maygup01 | 0:11cc2b7889af | 140 | * Initialize and mount the partition on the file system |
maygup01 | 0:11cc2b7889af | 141 | * The block device must be initialized before calling this function. |
maygup01 | 0:11cc2b7889af | 142 | * |
maygup01 | 0:11cc2b7889af | 143 | * @param fs Pointer to an array of file systems, one per partition |
maygup01 | 0:11cc2b7889af | 144 | * @param part Pointer to an array of block devices, one per partition. |
maygup01 | 0:11cc2b7889af | 145 | * All these need to be initialized. |
maygup01 | 0:11cc2b7889af | 146 | * @param number_of_partitions Total number of partitions |
maygup01 | 0:11cc2b7889af | 147 | * @param mount_point Mount point |
maygup01 | 0:11cc2b7889af | 148 | * |
maygup01 | 0:11cc2b7889af | 149 | * @returns 0 if successful, non-0 when not successful |
maygup01 | 0:11cc2b7889af | 150 | */ |
maygup01 | 0:11cc2b7889af | 151 | int init_and_mount_partition(FileSystem **fs, BlockDevice** part, int number_of_partition, const char* mount_point); |
maygup01 | 0:11cc2b7889af | 152 | #endif |
maygup01 | 0:11cc2b7889af | 153 | |
maygup01 | 0:11cc2b7889af | 154 | #if ((MCC_PLATFORM_PARTITION_MODE == 1) && (MCC_PLATFORM_AUTO_PARTITION == 1)) |
maygup01 | 0:11cc2b7889af | 155 | int create_partitions(void); |
maygup01 | 0:11cc2b7889af | 156 | #endif |
maygup01 | 0:11cc2b7889af | 157 | /** |
maygup01 | 0:11cc2b7889af | 158 | * Reformat a single partition |
maygup01 | 0:11cc2b7889af | 159 | * |
maygup01 | 0:11cc2b7889af | 160 | * @param fs A file system |
maygup01 | 0:11cc2b7889af | 161 | * @param part A block device |
maygup01 | 0:11cc2b7889af | 162 | * |
maygup01 | 0:11cc2b7889af | 163 | * @returns 0 if successful, non-0 when not successful |
maygup01 | 0:11cc2b7889af | 164 | */ |
maygup01 | 0:11cc2b7889af | 165 | int reformat_partition(FileSystem *fs, BlockDevice* part); |
maygup01 | 0:11cc2b7889af | 166 | |
maygup01 | 0:11cc2b7889af | 167 | /** |
maygup01 | 0:11cc2b7889af | 168 | * Test whether the file system is functional. |
maygup01 | 0:11cc2b7889af | 169 | * This unmounts, then mounts the file system against the block device |
maygup01 | 0:11cc2b7889af | 170 | * |
maygup01 | 0:11cc2b7889af | 171 | * If the file system cannot be unmounted, this will be ignored. |
maygup01 | 0:11cc2b7889af | 172 | * |
maygup01 | 0:11cc2b7889af | 173 | * @param fs A file system |
maygup01 | 0:11cc2b7889af | 174 | * @param part A block device |
maygup01 | 0:11cc2b7889af | 175 | * |
maygup01 | 0:11cc2b7889af | 176 | * @returns 0 if successful, non-0 when not successful |
maygup01 | 0:11cc2b7889af | 177 | */ |
maygup01 | 0:11cc2b7889af | 178 | int test_filesystem(FileSystem *fs, BlockDevice* part); |
maygup01 | 0:11cc2b7889af | 179 | |
maygup01 | 0:11cc2b7889af | 180 | BlockDevice *_bd; |
maygup01 | 0:11cc2b7889af | 181 | FileSystem *_fs; |
maygup01 | 0:11cc2b7889af | 182 | |
maygup01 | 0:11cc2b7889af | 183 | FileSystem *fs1; |
maygup01 | 0:11cc2b7889af | 184 | FileSystem *fs2; |
maygup01 | 0:11cc2b7889af | 185 | BlockDevice *part1; |
maygup01 | 0:11cc2b7889af | 186 | BlockDevice *part2; |
maygup01 | 0:11cc2b7889af | 187 | }; |
maygup01 | 0:11cc2b7889af | 188 | |
maygup01 | 0:11cc2b7889af | 189 | #endif // SIMPLEMBEDCLOUDCLIENT_STORAGEHELPER_H_ |