Simple interface for Mbed Cloud Client

Dependents:  

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileSystemInit.cpp Source File

FileSystemInit.cpp

00001 /*******************************************************************************
00002 * Copyright 2016, 2017 ARM Ltd.
00003 *
00004 * Licensed under the Apache License, Version 2.0 (the "License");
00005 * you may not use this file except in compliance with the License.
00006 * You may obtain a copy of the License at
00007 *
00008 *     http://www.apache.org/licenses/LICENSE-2.0
00009 *
00010 * Unless required by applicable law or agreed to in writing, software
00011 * distributed under the License is distributed on an "AS IS" BASIS,
00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 * See the License for the specific language governing permissions and
00014 * limitations under the License.
00015 *******************************************************************************/
00016 //uncomment this to use littleFS instead of fatFS
00017 //#define PAL_EXAMPLE_USE_LITTLE_FS
00018 
00019 #include "pal.h"
00020 #include "mbed.h"
00021 #include "BlockDevice.h"
00022 #include "MBRBlockDevice.h"
00023 #include "storage-selector/storage-selector.h"
00024 
00025 bool FileSystemInit = false;
00026 
00027 #ifndef PRIMARY_PARTITION_NUMBER
00028 #define PRIMARY_PARTITION_NUMBER 1
00029 #endif
00030 
00031 #ifndef PRIMARY_PARTITION_START
00032 #define PRIMARY_PARTITION_START 0
00033 #endif
00034 
00035 #ifndef PRIMARY_PARTITION_SIZE
00036 #define PRIMARY_PARTITION_SIZE 512*1024
00037 #endif
00038 
00039 #ifndef SECONDARY_PARTITION_NUMBER
00040 #define SECONDARY_PARTITION_NUMBER 2
00041 #endif
00042 
00043 #ifndef SECONDARY_PARTITION_START
00044 #define SECONDARY_PARTITION_START PRIMARY_PARTITION_SIZE
00045 #endif
00046 
00047 #ifndef SECONDARY_PARTITION_SIZE
00048 #define SECONDARY_PARTITION_SIZE PRIMARY_PARTITION_SIZE
00049 #endif
00050 
00051 //Uncomment this to create the partitions
00052 #define PAL_EXAMPLE_GENERATE_PARTITION
00053 
00054 //Uncomment this to format partitions if fs->mount() fails
00055 #define PAL_EXAMPLE_FORMAT_PARTITION
00056 
00057 #define PAL_PARTITION_TYPE 0x83
00058 //
00059 // See the mbed_lib.json in the sd-driver library for the definitions.
00060 // See the sd-driver library README.md for details with CI-shield etc.
00061 // Add also new boards/exceptions there rather than in code directly
00062 // OR
00063 // alternatively overload via your mbed_app.json (MBED_CONF_APP...)
00064 //
00065 
00066 static BlockDevice *bd = storage_selector();
00067 
00068 #if (MBED_CONF_STORAGE_SELECTOR_FILESYSTEM_INSTANCES > 0 )
00069 static MBRBlockDevice part1(bd, 1);
00070 static FileSystem  *fs1;
00071 #if ((MBED_CONF_STORAGE_SELECTOR_FILESYSTEM_INSTANCES == 2) && (PAL_NUMBER_OF_PARTITIONS == 2))
00072 static MBRBlockDevice part2(bd, 2);
00073 static FileSystem  *fs2;
00074 #endif
00075 #endif
00076 
00077 
00078 static int ReFormatPartition(BlockDevice* part, FileSystem* filesystem)
00079 {
00080     int err = 0;
00081 #ifdef PAL_EXAMPLE_FORMAT_PARTITION
00082     printf("re-format partition\r\n");
00083     err = filesystem->reformat(part);
00084     if (!err) {
00085         err = filesystem->mount(part);
00086         if (err != 0) {
00087             printf("failed to mount %d\r\n", err);
00088         }
00089     }
00090     else {
00091         printf("failed to re format partition cause %d\r\n", err);
00092     }
00093 #endif
00094     return err;
00095 }
00096 
00097 static int initFileSystem(BlockDevice* part, FileSystem* filesystem)
00098 {
00099     int err = 0;
00100     err = filesystem->unmount(); // filesystem_selector func do mount but doesnt return value , for checking if mount function return error we need first to unmount and then try to mount again.
00101     if (err < 0) {
00102         printf("failed to unmount %d\r\n", err);
00103     }
00104     err = filesystem->mount(part);
00105     if (err < 0) {
00106         printf("failed to mount %d\r\n", err);
00107         err = ReFormatPartition(part, filesystem);
00108     }
00109     if (err == 0) {
00110         err = filesystem->mkdir("bsp_test", 0600); // FATFS miss magic field. mkdir to check FS correctness.
00111         if ((err != 0) && (err != (int)-EEXIST)) {
00112             err = ReFormatPartition(part, filesystem);
00113         }
00114         filesystem->remove("bsp_test"); // delete in any case even after format
00115     }
00116     return err;
00117 }
00118 
00119 int initSDcardAndFileSystem(void)
00120 {
00121 
00122     int err = 0;
00123     err = bd->init();
00124     if (err < 0) {
00125         printf("Failed to initialize block device\r\n");
00126     }
00127     else {
00128         printf("Initializing the file system\r\n");
00129 #if (MBED_CONF_STORAGE_SELECTOR_FILESYSTEM_INSTANCES > 0)
00130         err = part1.init();
00131         if (err < 0)
00132         {
00133             printf("failed to init primary partition cause %d\r\n", err);
00134             err = MBRBlockDevice::partition(bd, PRIMARY_PARTITION_NUMBER, PAL_PARTITION_TYPE, PRIMARY_PARTITION_START, PRIMARY_PARTITION_START + PRIMARY_PARTITION_SIZE);
00135             if (err < 0) {
00136                 printf("Failed to initialize primary partition\r\n");
00137             }
00138         }
00139         if (!err)
00140         {
00141             fs1 = filesystem_selector(((char*)PAL_FS_MOUNT_POINT_PRIMARY + 1), &part1, 1);
00142             err = initFileSystem(&part1, fs1);
00143         }
00144 #if (MBED_CONF_STORAGE_SELECTOR_FILESYSTEM_INSTANCES == 2)
00145 #if (PAL_NUMBER_OF_PARTITIONS == 2)
00146         if (!err) {
00147             err = part2.init();
00148             if (err < 0) {
00149                 printf("failed to init secondary partition cause %d\r\n", err);
00150                 err = MBRBlockDevice::partition(bd, SECONDARY_PARTITION_NUMBER, PAL_PARTITION_TYPE, SECONDARY_PARTITION_START, SECONDARY_PARTITION_START + SECONDARY_PARTITION_SIZE);
00151                 if (err < 0) {
00152                     printf("Failed to initialize secondary partition\r\n");
00153                 }
00154             }
00155             if (!err) {
00156                 fs2 = filesystem_selector(((char*)PAL_FS_MOUNT_POINT_SECONDARY + 1), &part2, 2);
00157                 err = initFileSystem(&part2, fs2);
00158             }
00159         }
00160 #endif
00161 #endif
00162 #endif
00163     }
00164     if (!err)
00165     {
00166         printf("Succeed to initialize the file system\r\n");
00167         FileSystemInit = true;
00168     }
00169 
00170     return err;
00171 }