XinStellaris commented on code in PR #6829:
URL: https://github.com/apache/incubator-nuttx/pull/6829#discussion_r954958805


##########
drivers/mtd/mtd_config_fs.c:
##########
@@ -0,0 +1,2654 @@
+/****************************************************************************
+ * drivers/mtd/mtd_config_fs.c
+ *
+ * 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.
+ *
+ * NVS: non volatile storage in flash
+ *
+ * Copyright (c) 2018 Laczen
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <nuttx/crc8.h>
+#include <debug.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/poll.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/mtd/mtd.h>
+#include <nuttx/mtd/configdata.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_MTD_NVS_WRITE_BLOCK_SIZE 4
+#define NVS_WRITE_BLOCK_SIZE            CONFIG_MTD_NVS_WRITE_BLOCK_SIZE
+#define MIN(a, b)                       ((a) > (b) ? (b) : (a))
+
+/* MASKS AND SHIFT FOR ADDRESSES
+ * an address in nvs is an uint32_t where:
+ *   high 2 bytes represent the sector number
+ *   low 2 bytes represent the offset in a sector
+ */
+#define ADDR_SECT_MASK                  0xFFFF0000
+#define ADDR_SECT_SHIFT                 16
+#define ADDR_OFFS_MASK                  0x0000FFFF
+
+/* Status return values */
+
+#define NVS_STATUS_NOSPACE              1
+
+#define NVS_BLOCK_SIZE                  32
+
+#define NVS_LOOKUP_CACHE_NO_ADDR        0xFFFFFFFF
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/**
+ * @brief Non-volatile Storage File system structure
+ *
+ * @param ate_wra: Allocation table entry write address. Addresses are stored
+ * as uint32_t: high 2 bytes are sector, low 2 bytes are offset in sector,
+ * @param data_wra: Data write address.
+ * @param sector_size File system is divided into sectors each sector
+ * should be multiple of pagesize
+ * @param sector_count Amount of sectors in the file systems
+ * @param write_block_size Alignment size
+ * @param nvs_lock Mutex
+ * @param flash_device Flash Device
+ */
+
+struct nvs_fs
+{
+  FAR struct mtd_dev_s *mtd;          /* mtd device */
+  uint32_t             ate_wra;       /* next alloc table entry
+                                       * write address
+                                       */
+  uint32_t             data_wra;      /* next data write address */
+  uint16_t             sector_size;   /* filesystem is divided into
+                                       * sectors, sector size should be
+                                       * multiple of pagesize
+                                       */
+  uint16_t             page_size;     /* page size */
+  uint16_t             sector_count;  /* amount of sectors */
+  bool                 ready;         /* is the filesystem initialized */
+
+  uint32_t             step_addr;     /* for traverse */
+  mutex_t              nvs_lock;
+
+#ifdef CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE
+  uint32_t lookup_cache[CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE];
+#endif
+};
+
+/* Allocation Table Entry */
+
+begin_packed_struct struct nvs_ate
+{
+  uint32_t id;         /* data id */
+  uint16_t offset;     /* data offset within sector */
+  uint16_t len;        /* data len within sector */
+  uint16_t key_len;    /* key string len */
+  uint8_t  part;       /* part of a multipart data - future extension */
+  uint8_t  crc8;       /* crc8 check of the ate entry */
+  uint8_t  expired;    /* 0xFF-newest entry, others-old entry */
+  uint8_t  reserved;   /* for future extension */
+  uint16_t reserved2;  /* for future extension */
+} end_packed_struct;
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/* MTD NVS opeation api */
+
+static int     mtdnvs_open(FAR struct file *filep);
+static int     mtdnvs_close(FAR struct file *filep);
+static ssize_t mtdnvs_read(FAR struct file *filep, FAR char *buffer,
+                           size_t buflen);
+static int     mtdnvs_ioctl(FAR struct file *filep, int cmd,
+                            unsigned long arg);
+static int     mtdnvs_poll(FAR struct file *filep, FAR struct pollfd *fds,
+                           bool setup);
+
+/* Basic flash operation api */
+
+static ssize_t flash_read(FAR struct mtd_dev_s *mtd, off_t offset,
+                          FAR void *data, size_t len);
+static ssize_t flash_write(FAR struct mtd_dev_s *mtd, off_t offset,
+                           FAR const uint8_t *data, size_t len);
+static int flash_erase(FAR struct mtd_dev_s *mtd, off_t offset, size_t len);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const struct file_operations mtdnvs_fops =
+{
+  mtdnvs_open,  /* open */
+  mtdnvs_close, /* close */
+  mtdnvs_read,  /* read */
+  NULL,         /* write */
+  NULL,         /* seek */
+  mtdnvs_ioctl, /* ioctl */
+  mtdnvs_poll   /* poll */
+#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
+  , NULL        /* unlink */
+#endif
+};
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fnv_hash_32
+ ****************************************************************************/
+
+static uint32_t fnv_hash_32(FAR const uint8_t *input, uint32_t len)
+{
+  uint32_t i = 0;
+  uint32_t hval = 2166136261;
+
+  /* FNV-1 hash each octet in the buffer */
+
+  while (i++ < len)
+    {
+      /* multiply by the 32 bit FNV magic prime mod 2^32 */
+
+      hval *= 0x01000193;
+
+      /* xor the bottom with the current octet */
+
+      hval ^= (uint32_t)*input++;
+    }
+
+  return hval;
+}
+
+#ifdef CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE
+
+/****************************************************************************
+ * Name: nvs_lookup_cache_pos
+ ****************************************************************************/
+
+static inline size_t nvs_lookup_cache_pos(uint32_t id)
+{
+  return id % CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE;
+}
+
+/****************************************************************************
+ * Name: nvs_lookup_cache_rebuild
+ ****************************************************************************/
+
+static int nvs_lookup_cache_rebuild(FAR struct nvs_fs *fs)
+{
+  int rc;
+  uint32_t addr;
+  uint32_t ate_addr;
+  FAR uint32_t *cache_entry;
+  struct nvs_ate ate;
+
+  memset(fs->lookup_cache, CONFIG_MTD_CONFIG_ERASEDVALUE,
+    sizeof(fs->lookup_cache));
+  addr = fs->ate_wra;
+
+  while (true)
+    {
+      /* Make a copy of 'addr' as it will be advanced by nvs_pref_ate() */
+
+      ate_addr = addr;
+      rc = nvs_prev_ate(fs, &addr, &ate);
+
+      if (rc)
+        {
+          return rc;
+        }
+
+      cache_entry = &fs->lookup_cache[nvs_lookup_cache_pos(ate.id)];
+
+      if (ate.id != 0xffffffff && *cache_entry == NVS_LOOKUP_CACHE_NO_ADDR &&
+          nvs_ate_valid(fs, &ate))
+        {
+          *cache_entry = ate_addr;
+        }
+
+      if (addr == fs->ate_wra)
+        {
+          break;
+        }
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: nvs_lookup_cache_invalidate
+ ****************************************************************************/
+
+static void nvs_lookup_cache_invalidate(FAR struct nvs_fs *fs,
+                                                uint32_t sector)
+{
+  FAR uint32_t *cache_entry = fs->lookup_cache;
+  FAR uint32_t *const cache_end =
+    &fs->lookup_cache[CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE_SIZE];
+
+  for (; cache_entry < cache_end; ++cache_entry)
+    {
+      if ((*cache_entry >> ADDR_SECT_SHIFT) == sector)
+        {
+          *cache_entry = NVS_LOOKUP_CACHE_NO_ADDR;
+        }
+    }
+}
+
+#endif /* CONFIG_MTD_NVS_LOOKUP_CACHE */
+
+/****************************************************************************
+ * Name: flash_read
+ ****************************************************************************/
+
+static ssize_t flash_read(FAR struct mtd_dev_s *mtd, off_t offset,
+                                 FAR void *data, size_t len)
+{
+  int ret;
+  ret = MTD_READ(mtd, offset, len, data);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  return OK;
+}
+
+/****************************************************************************
+ * Name: flash_write
+ ****************************************************************************/
+
+static ssize_t flash_write(FAR struct mtd_dev_s *mtd, off_t offset,
+                           FAR const uint8_t *data, size_t len)
+{
+  int ret = -ENOTSUP;
+
+#ifdef CONFIG_MTD_BYTE_WRITE
+  ret = MTD_WRITE(mtd, offset, len, data);
+  if (ret < 0)
+    {
+      return ret;
+    }
+  else
+    {
+      ret = OK;
+    }
+#endif
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: flash_erase
+ ****************************************************************************/
+
+static int flash_erase(FAR struct mtd_dev_s *mtd, off_t startblock,
+                    size_t nblocks)
+{
+  return MTD_ERASE(mtd, startblock, nblocks);
+}
+
+/****************************************************************************
+ * Name: nvs_al_size
+ *
+ * Description:
+ *   nvs_al_size() returns size aligned to NVS_WRITE_BLOCK_SIZE.
+ *
+ * Parameter:
+ *   fs  - offset from 0 of internal flash
+ *   len - avaiable size for NVM
+ *
+ ****************************************************************************/
+
+static inline size_t nvs_al_size(FAR struct nvs_fs *fs, size_t len)
+{
+  uint8_t write_block_size = NVS_WRITE_BLOCK_SIZE;
+
+  if (write_block_size <= 1U)
+    {
+      return len;
+    }
+
+  return (len + (write_block_size - 1U)) & ~(write_block_size - 1U);
+}
+
+/****************************************************************************
+ * Name: nvs_flash_al_wrt
+ *
+ * Description:
+ *   flash routines, basic aligned flash write to nvs address.
+ *
+ ****************************************************************************/
+
+static int nvs_flash_al_wrt(FAR struct nvs_fs *fs, uint32_t addr,
+                             FAR const void *data, size_t len)
+{
+  FAR const uint8_t *data8 = (FAR const uint8_t *)data;
+  int rc = 0;
+  off_t offset;
+  size_t blen;
+  uint8_t buf[NVS_BLOCK_SIZE];
+
+  if (!len)
+    {
+      /* Nothing to write, avoid changing the flash protection */
+
+      return 0;
+    }
+
+  offset = fs->sector_size * (addr >> ADDR_SECT_SHIFT);
+  offset += addr & ADDR_OFFS_MASK;
+
+  blen = len & ~(NVS_WRITE_BLOCK_SIZE - 1U);
+  if (blen > 0)
+    {
+      rc = flash_write(fs->mtd, offset, data8, blen);
+      if (rc)
+        {
+          /* flash write error */
+
+          goto end;
+        }
+
+      len -= blen;
+      offset += blen;
+      data8 += blen;
+    }
+
+  if (len)
+    {
+      memcpy(buf, data8, len);
+      (void)memset(buf + len, CONFIG_MTD_CONFIG_ERASEDVALUE,
+                   NVS_WRITE_BLOCK_SIZE - len);
+
+      rc = flash_write(fs->mtd, offset, buf,
+                       NVS_WRITE_BLOCK_SIZE);
+      if (rc)
+        {
+          /* flash write error */
+
+          goto end;
+        }
+    }
+
+end:
+  return rc;
+}
+
+/****************************************************************************
+ * Name: nvs_flash_rd
+ *
+ * Description:
+ *   basic flash read from nvs address.
+ *
+ ****************************************************************************/
+
+static int nvs_flash_rd(FAR struct nvs_fs *fs, uint32_t addr,
+                        FAR void *data, size_t len)
+{
+  int rc;
+  off_t offset;
+
+  offset = fs->sector_size * (addr >> ADDR_SECT_SHIFT);
+  offset += addr & ADDR_OFFS_MASK;
+
+  rc = flash_read(fs->mtd, offset, data, len);
+  return rc;
+}
+
+/****************************************************************************
+ * Name: nvs_flash_ate_wrt
+ *
+ * Description:
+ *   allocation entry write.
+ *
+ ****************************************************************************/
+
+static int nvs_flash_ate_wrt(FAR struct nvs_fs *fs,
+                                FAR const struct nvs_ate *entry)
+{
+  int rc;
+
+  rc = nvs_flash_al_wrt(fs, fs->ate_wra, entry, sizeof (struct nvs_ate));
+
+#ifdef CONFIG_MTD_CONFIG_FAIL_SAFE_LOOKUP_CACHE
+  /* 0xffffffff is a special-purpose identifier. Exclude it from the cache */
+
+  if (entry->id != 0xffffffff)
+    {
+      fs->lookup_cache[nvs_lookup_cache_pos(entry->id)] = fs->ate_wra;
+    }
+#endif
+
+  fs->ate_wra -= nvs_al_size(fs, sizeof(struct nvs_ate));
+
+  return rc;
+}
+
+/****************************************************************************
+ * Name: nvs_flash_data_wrt
+ ****************************************************************************/
+
+static int nvs_flash_data_wrt(FAR struct nvs_fs *fs,
+                              FAR const void *data, size_t len)
+{
+  int rc;
+
+  rc = nvs_flash_al_wrt(fs, fs->data_wra, data, len);
+  fs->data_wra += nvs_al_size(fs, len);
+  finfo("write data done, data_wra=0x%lx\n",
+    fs->data_wra);
+
+  return rc;
+}
+
+/****************************************************************************
+ * Name: nvs_flash_ate_rd
+ ****************************************************************************/
+
+static int nvs_flash_ate_rd(FAR struct nvs_fs *fs, uint32_t addr,
+                            FAR struct nvs_ate *entry)
+{
+  return nvs_flash_rd(fs, addr, entry, sizeof(struct nvs_ate));
+}
+
+/****************************************************************************
+ * Name: nvs_flash_block_cmp
+ *
+ * Description:
+ *   nvs_flash_block_cmp compares the data in flash at addr to data
+ *   in blocks of size NVS_BLOCK_SIZE aligned to fs->write_block_size
+ *   returns 0 if equal, 1 if not equal, errcode if error
+ *
+ ****************************************************************************/
+
+static int nvs_flash_block_cmp(FAR struct nvs_fs *fs, uint32_t addr,
+                               FAR const void *data, size_t len)
+{
+  FAR const uint8_t *data8 = (FAR const uint8_t *)data;
+  int rc;
+  size_t bytes_to_cmp;
+  size_t block_size;
+  uint8_t buf[NVS_BLOCK_SIZE];
+
+  block_size = NVS_BLOCK_SIZE & ~(NVS_WRITE_BLOCK_SIZE - 1U);
+
+  while (len)
+    {
+      bytes_to_cmp = MIN(block_size, len);
+      rc = nvs_flash_rd(fs, addr, buf, bytes_to_cmp);
+      if (rc)
+        {
+          return rc;
+        }
+
+      rc = memcmp(data8, buf, bytes_to_cmp);
+      if (rc)
+        {
+          return 1;
+        }
+
+      len -= bytes_to_cmp;
+      addr += bytes_to_cmp;
+      data8 += bytes_to_cmp;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: nvs_flash_direct_cmp
+ *
+ * Description:
+ *   nvs_flash_direct_cmp compares the data in flash at addr1 and addr2
+ *   of len in blocks of size NVS_BLOCK_SIZE aligned to fs->write_block_size
+ *   returns 0 if equal, 1 if not equal, errcode if error.
+ *
+ ****************************************************************************/
+
+static int nvs_flash_direct_cmp(FAR struct nvs_fs *fs, uint32_t addr1,
+                               uint32_t addr2, size_t len)
+{
+  int rc;
+  size_t bytes_to_cmp;
+  size_t block_size;
+  uint8_t buf1[NVS_BLOCK_SIZE];
+  uint8_t buf2[NVS_BLOCK_SIZE];
+
+  block_size = NVS_BLOCK_SIZE & ~(NVS_WRITE_BLOCK_SIZE - 1U);
+
+  while (len)
+    {
+      bytes_to_cmp = MIN(block_size, len);
+      rc = nvs_flash_rd(fs, addr1, buf1, bytes_to_cmp);
+      if (rc)
+        {
+          return rc;
+        }
+
+      rc = nvs_flash_rd(fs, addr2, buf2, bytes_to_cmp);
+      if (rc)
+        {
+          return rc;
+        }
+
+      rc = memcmp(buf1, buf2, bytes_to_cmp);
+      if (rc)
+        {
+          return 1;
+        }
+
+      len -= bytes_to_cmp;
+      addr1 += bytes_to_cmp;
+      addr2 += bytes_to_cmp;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: nvs_flash_cmp_const
+ *
+ * Description:
+ *   nvs_flash_cmp_const compares the data in flash at addr to a constant
+ *   value. returns 0 if all data in flash is equal to value, 1 if not equal,
+ *   errcode if error.
+ *
+ ****************************************************************************/
+
+static int nvs_flash_cmp_const(FAR struct nvs_fs *fs, uint32_t addr,
+                               uint8_t value, size_t len)
+{
+  int rc;
+  size_t bytes_to_cmp;
+  size_t block_size;
+  uint8_t cmp[NVS_BLOCK_SIZE];
+
+  block_size = NVS_BLOCK_SIZE & ~(NVS_WRITE_BLOCK_SIZE - 1U);
+
+  (void)memset(cmp, value, block_size);

Review Comment:
   remove it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to