leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers storage-helper.h Source File

storage-helper.h

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2018 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #ifndef SIMPLEMBEDCLOUDCLIENT_STORAGEHELPER_H_
00020 #define SIMPLEMBEDCLOUDCLIENT_STORAGEHELPER_H_
00021 
00022 #include "mbed.h"
00023 #include "BlockDevice.h"
00024 #include "FileSystem.h"
00025 #include "factory_configurator_client.h"
00026 
00027 // This is for single or dual partition mode. This is supposed to be used with storage for data e.g. SD card.
00028 // Enable by 1/disable by 0.
00029 #ifndef MCC_PLATFORM_PARTITION_MODE
00030 #define MCC_PLATFORM_PARTITION_MODE 0
00031 #endif
00032 
00033 #include "pal.h"
00034 #if (MCC_PLATFORM_PARTITION_MODE == 1)
00035 #include "MBRBlockDevice.h"
00036 #include "FATFileSystem.h"
00037 
00038 // Set to 1 for enabling automatic partitioning storage if required. This is effective only if MCC_PLATFORM_PARTITION_MODE is defined to 1.
00039 // Partioning will be triggered only if initialization of available partitions fail.
00040 #ifndef MCC_PLATFORM_AUTO_PARTITION
00041 #define MCC_PLATFORM_AUTO_PARTITION 0
00042 #endif
00043 
00044 #ifndef PRIMARY_PARTITION_NUMBER
00045 #define PRIMARY_PARTITION_NUMBER 1
00046 #endif
00047 
00048 #ifndef PRIMARY_PARTITION_START
00049 #define PRIMARY_PARTITION_START 0
00050 #endif
00051 
00052 #ifndef PRIMARY_PARTITION_SIZE
00053 #define PRIMARY_PARTITION_SIZE 1024*1024*1024 // default partition size 1GB
00054 #endif
00055 
00056 #ifndef SECONDARY_PARTITION_NUMBER
00057 #define SECONDARY_PARTITION_NUMBER 2
00058 #endif
00059 
00060 #ifndef SECONDARY_PARTITION_START
00061 #define SECONDARY_PARTITION_START PRIMARY_PARTITION_SIZE
00062 #endif
00063 
00064 #ifndef SECONDARY_PARTITION_SIZE
00065 #define SECONDARY_PARTITION_SIZE 1024*1024*1024 // default partition size 1GB
00066 #endif
00067 
00068 #ifndef NUMBER_OF_PARTITIONS
00069 #define NUMBER_OF_PARTITIONS PAL_NUMBER_OF_PARTITIONS
00070 #endif
00071 
00072 #ifndef MOUNT_POINT_PRIMARY
00073 #define MOUNT_POINT_PRIMARY PAL_FS_MOUNT_POINT_PRIMARY
00074 #endif
00075 
00076 #ifndef MOUNT_POINT_SECONDARY
00077 #define MOUNT_POINT_SECONDARY PAL_FS_MOUNT_POINT_SECONDARY
00078 #endif
00079 
00080 #endif // MCC_PLATFORM_PARTITION_MODE
00081 
00082 // Include this only for Developer mode and device which doesn't have in-built TRNG support
00083 #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1
00084 #ifdef PAL_USER_DEFINED_CONFIGURATION
00085 #define FCC_ROT_SIZE                       16
00086 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 };
00087 #if !PAL_USE_HW_TRNG
00088 #define FCC_ENTROPY_SIZE                   48
00089 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 };
00090 #endif // PAL_USE_HW_TRNG = 0
00091 #endif // PAL_USER_DEFINED_CONFIGURATION
00092 #endif // #if MBED_CONF_DEVICE_MANAGEMENT_DEVELOPER_MODE == 1
00093 
00094 class StorageHelper {
00095 public:
00096     /**
00097      * Initializes a new StorageHelper.
00098      * StorageHelper manages storage across multiple partitions,
00099      * initializes SOTP, and can format the storage.
00100      *
00101      * @param bd An un-initialized block device to back the file system
00102      * @param fs An un-mounted file system
00103      */
00104     StorageHelper(BlockDevice *bd, FileSystem *fs);
00105 
00106     /**
00107      * Initialize the storage helper, this initializes and mounts
00108      * both the block device and file system
00109      *
00110      * @returns 0 if successful, non-0 when not successful
00111      */
00112     int init();
00113 
00114     /**
00115      * Initialize the factory configurator client, sets entropy,
00116      * and reads root of trust.
00117      *
00118      * @returns 0 if successful, non-0 when not successful
00119      */
00120     int sotp_init();
00121 
00122     /**
00123      * Format the block device, and remount the filesystem
00124      *
00125      * @returns 0 if successful, non-0 when not successful
00126      */
00127     int reformat_storage(void);
00128 
00129     /**
00130      * Initialize and format a blockdevice and file system
00131      */
00132     static int format(FileSystem *fs, BlockDevice *bd);
00133 
00134 private:
00135 #if (MCC_PLATFORM_PARTITION_MODE == 1)
00136     // for checking that PRIMARY_PARTITION_SIZE and SECONDARY_PARTITION_SIZE do not overflow.
00137     bd_size_t mcc_platform_storage_size;
00138 
00139     /**
00140      * Initialize and mount the partition on the file system
00141      * The block device must be initialized before calling this function.
00142      *
00143      * @param fs Pointer to an array of file systems, one per partition
00144      * @param part Pointer to an array of block devices, one per partition.
00145      *              All these need to be initialized.
00146      * @param number_of_partitions Total number of partitions
00147      * @param mount_point Mount point
00148      *
00149      * @returns 0 if successful, non-0 when not successful
00150      */
00151     int init_and_mount_partition(FileSystem **fs, BlockDevice** part, int number_of_partition, const char* mount_point);
00152 #endif
00153 
00154 #if ((MCC_PLATFORM_PARTITION_MODE == 1) && (MCC_PLATFORM_AUTO_PARTITION == 1))
00155     int create_partitions(void);
00156 #endif
00157     /**
00158      * Reformat a single partition
00159      *
00160      * @param fs A file system
00161      * @param part A block device
00162      *
00163      * @returns 0 if successful, non-0 when not successful
00164      */
00165     int reformat_partition(FileSystem *fs, BlockDevice* part);
00166 
00167     /**
00168      * Test whether the file system is functional.
00169      * This unmounts, then mounts the file system against the block device
00170      *
00171      * If the file system cannot be unmounted, this will be ignored.
00172      *
00173      * @param fs A file system
00174      * @param part A block device
00175      *
00176      * @returns 0 if successful, non-0 when not successful
00177      */
00178     int test_filesystem(FileSystem *fs, BlockDevice* part);
00179 
00180     BlockDevice *_bd;
00181     FileSystem *_fs;
00182 
00183     FileSystem *fs1;
00184     FileSystem *fs2;
00185     BlockDevice *part1;
00186     BlockDevice *part2;
00187 };
00188 
00189 #endif // SIMPLEMBEDCLOUDCLIENT_STORAGEHELPER_H_