http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/src/fs_cli.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_cli.c b/fs/fs/src/fs_cli.c index 4498d87..d481c6e 100644 --- a/fs/fs/src/fs_cli.c +++ b/fs/fs/src/fs_cli.c @@ -17,7 +17,9 @@ * under the License. */ -#ifdef SHELL_PRESENT +#include "syscfg/syscfg.h" + +#if MYNEWT_VAL(FS_CLI) #include <inttypes.h> #include <string.h> @@ -227,4 +229,4 @@ fs_cli_init(void) shell_cmd_register(&fs_mv_struct); shell_cmd_register(&fs_cat_struct); } -#endif /* SHELL_PRESENT */ +#endif /* MYNEWT_VAL(FS_CLI) */
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/src/fs_mount.c ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_mount.c b/fs/fs/src/fs_mount.c index 9653505..374fa1b 100644 --- a/fs/fs/src/fs_mount.c +++ b/fs/fs/src/fs_mount.c @@ -16,8 +16,10 @@ * specific language governing permissions and limitations * under the License. */ -#include <fs/fs.h> -#include <fs/fs_if.h> + +#include "syscfg/syscfg.h" +#include "fs/fs.h" +#include "fs/fs_if.h" #include "fs_priv.h" const struct fs_ops *fs_root_ops = NULL; @@ -30,7 +32,7 @@ fs_register(const struct fs_ops *fops) } fs_root_ops = fops; -#ifdef SHELL_PRESENT +#if MYNEWT_VAL(FS_CLI) fs_cli_init(); #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/src/fs_priv.h ---------------------------------------------------------------------- diff --git a/fs/fs/src/fs_priv.h b/fs/fs/src/fs_priv.h index af08e2f..f8f092d 100644 --- a/fs/fs/src/fs_priv.h +++ b/fs/fs/src/fs_priv.h @@ -19,11 +19,13 @@ #ifndef __FS_PRIV_H__ #define __FS_PRIV_H__ +#include "syscfg/syscfg.h" + struct fs_ops; extern const struct fs_ops *fs_root_ops; -#ifdef SHELL_PRESENT +#if MYNEWT_VAL(FS_CLI) void fs_cli_init(void); -#endif /* SHELL_PRESENT */ +#endif #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/include/nffs/nffs.h ---------------------------------------------------------------------- diff --git a/fs/nffs/include/nffs/nffs.h b/fs/nffs/include/nffs/nffs.h index 73a0ec1..4a608cb 100644 --- a/fs/nffs/include/nffs/nffs.h +++ b/fs/nffs/include/nffs/nffs.h @@ -22,6 +22,7 @@ #include <stddef.h> #include <inttypes.h> +#include "fs/fs.h" #define NFFS_FILENAME_MAX_LEN 256 /* Does not require null terminator. */ #define NFFS_MAX_AREAS 256 @@ -58,4 +59,6 @@ int nffs_init(void); int nffs_detect(const struct nffs_area_desc *area_descs); int nffs_format(const struct nffs_area_desc *area_descs); +int nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad); + #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/pkg.yml ---------------------------------------------------------------------- diff --git a/fs/nffs/pkg.yml b/fs/nffs/pkg.yml index 61e07e0..1a3f11d 100644 --- a/fs/nffs/pkg.yml +++ b/fs/nffs/pkg.yml @@ -26,10 +26,18 @@ pkg.keywords: - filesystem - ffs -pkg.features: NFFS pkg.deps: - fs/fs - hw/hal - libs/os - libs/testutil - sys/log + - sys/stats + +pkg.init_function: nffs_pkg_init +pkg.init_stage: 2 + +pkg.syscfg_defs: + NFFS_DETECT_FAIL: + description: 'TBD' + value: 'NFFS_DETECT_FAIL_FORMAT' http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs.c ---------------------------------------------------------------------- diff --git a/fs/nffs/src/nffs.c b/fs/nffs/src/nffs.c index 28fdeea..5ee63aa 100644 --- a/fs/nffs/src/nffs.c +++ b/fs/nffs/src/nffs.c @@ -21,7 +21,11 @@ #include <string.h> #include <stdlib.h> #include <assert.h> + +#include "sysinit/sysinit.h" +#include "bsp/bsp.h" #include "hal/hal_flash.h" +#include "hal/flash_map.h" #include "os/os_mempool.h" #include "os/os_mutex.h" #include "os/os_malloc.h" @@ -686,7 +690,6 @@ nffs_init(void) return FS_ENOMEM; } - log_init(); log_console_handler_init(&nffs_log_console_handler); log_register("nffs", &nffs_log, &nffs_log_console_handler); @@ -698,3 +701,52 @@ nffs_init(void) fs_register(&nffs_ops); return 0; } + +void +nffs_pkg_init(void) +{ + struct nffs_area_desc descs[NFFS_AREA_MAX + 1]; + int cnt; + int rc; + + /* Initialize nffs's internal state. */ + rc = nffs_init(); + SYSINIT_PANIC_ASSERT(rc == 0); + + /* Convert the set of flash blocks we intend to use for nffs into an array + * of nffs area descriptors. + */ + cnt = NFFS_AREA_MAX; + rc = nffs_misc_desc_from_flash_area(FLASH_AREA_NFFS, &cnt, descs); + SYSINIT_PANIC_ASSERT(rc == 0); + + /* Attempt to restore an existing nffs file system from flash. */ + rc = nffs_detect(descs); + switch (rc) { + case 0: + break; + + case FS_ECORRUPT: + /* No valid nffs instance detected; act based on configued detection + * failure policy. + */ + switch (MYNEWT_VAL(NFFS_DETECT_FAIL)) { + case NFFS_DETECT_FAIL_IGNORE: + break; + + case NFFS_DETECT_FAIL_FORMAT: + rc = nffs_format(descs); + SYSINIT_PANIC_ASSERT(rc == 0); + break; + + default: + SYSINIT_PANIC(); + break; + } + break; + + default: + SYSINIT_PANIC(); + break; + } +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs_misc.c ---------------------------------------------------------------------- diff --git a/fs/nffs/src/nffs_misc.c b/fs/nffs/src/nffs_misc.c index cbf0e45..76591ca 100644 --- a/fs/nffs/src/nffs_misc.c +++ b/fs/nffs/src/nffs_misc.c @@ -18,6 +18,9 @@ */ #include <assert.h> +#include "hal/flash_map.h" +#include "hal/hal_bsp.h" +#include "hal/hal_flash_int.h" #include "os/os_malloc.h" #include "nffs/nffs.h" #include "nffs_priv.h" @@ -437,3 +440,70 @@ nffs_misc_ready(void) { return nffs_root_dir != NULL; } + + +/* + * Turn flash region into a set of areas for NFFS use. + * + * Limit the number of regions we return to be less than *cnt. + * If sector count within region exceeds that, collect multiple sectors + * to a region. + */ +int +nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad) +{ + int i, j; + const struct hal_flash *hf; + const struct flash_area *fa; + int max_cnt, move_on; + int first_idx, last_idx; + uint32_t start, size; + uint32_t min_size; + + if (!flash_map || idx >= flash_map_entries) { + return -1; + } + first_idx = last_idx = -1; + max_cnt = *cnt; + *cnt = 0; + + fa = &flash_map[idx]; + + hf = bsp_flash_dev(fa->fa_flash_id); + for (i = 0; i < hf->hf_sector_cnt; i++) { + hf->hf_itf->hff_sector_info(i, &start, &size); + if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) { + if (first_idx == -1) { + first_idx = i; + } + last_idx = i; + *cnt = *cnt + 1; + } + } + if (*cnt > max_cnt) { + min_size = fa->fa_size / max_cnt; + } else { + min_size = 0; + } + *cnt = 0; + + move_on = 1; + for (i = first_idx, j = 0; i < last_idx + 1; i++) { + hf->hf_itf->hff_sector_info(i, &start, &size); + if (move_on) { + nad[j].nad_flash_id = fa->fa_flash_id; + nad[j].nad_offset = start; + nad[j].nad_length = size; + *cnt = *cnt + 1; + move_on = 0; + } else { + nad[j].nad_length += size; + } + if (nad[j].nad_length >= min_size) { + j++; + move_on = 1; + } + } + nad[*cnt].nad_length = 0; + return 0; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs_priv.h ---------------------------------------------------------------------- diff --git a/fs/nffs/src/nffs_priv.h b/fs/nffs/src/nffs_priv.h index 47c4e9a..c8da07e 100644 --- a/fs/nffs/src/nffs_priv.h +++ b/fs/nffs/src/nffs_priv.h @@ -58,6 +58,9 @@ #define NFFS_BLOCK_MAX_DATA_SZ_MAX 2048 +#define NFFS_DETECT_FAIL_IGNORE 1 +#define NFFS_DETECT_FAIL_FORMAT 2 + /** On-disk representation of an area header. */ struct nffs_disk_area { uint32_t nda_magic[4]; /* NFFS_AREA_MAGIC{0,1,2,3} */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs_restore.c ---------------------------------------------------------------------- diff --git a/fs/nffs/src/nffs_restore.c b/fs/nffs/src/nffs_restore.c index fd425fc..b0ed578 100644 --- a/fs/nffs/src/nffs_restore.c +++ b/fs/nffs/src/nffs_restore.c @@ -1198,7 +1198,7 @@ nffs_restore_corrupt_scratch(void) static void nffs_log_contents(void) { -#if LOG_LEVEL > LOG_LEVEL_DEBUG +#if MYNEWT_VAL(LOG_LEVEL) > LOG_LEVEL_DEBUG return; #endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/test/arch/cortex_m4/nffs_test.c ---------------------------------------------------------------------- diff --git a/fs/nffs/src/test/arch/cortex_m4/nffs_test.c b/fs/nffs/src/test/arch/cortex_m4/nffs_test.c deleted file mode 100644 index 654089b..0000000 --- a/fs/nffs/src/test/arch/cortex_m4/nffs_test.c +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - - -#include "nffs/nffs_test.h" - -int -nffs_test_all(void) -{ - return 0; -}