2013-11-01 14:39:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mtd/mtd_config.c
|
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* 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
|
2013-11-01 14:39:20 +01:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-11-01 14:39:20 +01:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* 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.
|
2013-11-01 14:39:20 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* dev_config provides an interface for saving and retrieving
|
|
|
|
* application configuration data to a standard MTD partition.
|
|
|
|
* It accepts an MTD pointer (presumable a partition) and
|
|
|
|
* publishes a /dev/config device file for accessing via
|
|
|
|
* defined ioctl calls to set and get config data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2013-11-01 14:39:20 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2016-07-21 21:34:54 +02:00
|
|
|
#include <nuttx/mtd/configdata.h>
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/****************************************************************************
|
2013-11-01 19:22:55 +01:00
|
|
|
* Pre-processor Definitions
|
2018-12-20 21:14:40 +01:00
|
|
|
****************************************************************************/
|
2013-11-01 19:22:55 +01:00
|
|
|
|
|
|
|
/* Define the current format version */
|
|
|
|
|
2022-03-21 21:10:09 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
# define CONFIGDATA_FORMAT_VERSION 1
|
2022-09-07 07:33:40 +02:00
|
|
|
# define MTD_ERASED_ID(dev) ((dev)->erasestate)
|
2022-03-21 21:10:09 +01:00
|
|
|
#else
|
|
|
|
# define CONFIGDATA_FORMAT_VERSION 2
|
2022-09-07 07:33:40 +02:00
|
|
|
# define MTD_ERASED_ID(dev) (((dev)->erasestate << 8) | (dev)->erasestate)
|
2013-11-01 19:22:55 +01:00
|
|
|
#endif
|
2022-03-21 21:10:09 +01:00
|
|
|
#define CONFIGDATA_BLOCK_HDR_SIZE 3
|
2022-09-07 07:33:40 +02:00
|
|
|
#define MTD_ERASED_FLAGS(dev) ((dev)->erasestate)
|
2013-11-04 15:52:16 +01:00
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct mtdconfig_struct_s
|
|
|
|
{
|
2014-06-30 15:52:57 +02:00
|
|
|
FAR struct mtd_dev_s *mtd; /* Contained MTD interface */
|
|
|
|
sem_t exclsem; /* Supports mutual exclusion */
|
2022-02-08 14:41:28 +01:00
|
|
|
uint32_t blocksize; /* Size of blocks in contained MTD */
|
|
|
|
uint32_t erasesize; /* Size of erase block in contained MTD */
|
2014-06-30 15:52:57 +02:00
|
|
|
size_t nblocks; /* Number of blocks available */
|
|
|
|
size_t neraseblocks; /* Number of erase blocks available */
|
2022-09-07 07:33:40 +02:00
|
|
|
uint8_t erasestate; /* Erased value */
|
2014-06-30 15:52:57 +02:00
|
|
|
off_t readoff; /* Read offset (for hexdump) */
|
|
|
|
FAR uint8_t *buffer; /* Temp block read buffer */
|
2013-11-01 14:39:20 +01:00
|
|
|
};
|
|
|
|
|
2017-01-09 12:17:49 +01:00
|
|
|
begin_packed_struct struct mtdconfig_header_s
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2014-06-30 15:52:57 +02:00
|
|
|
uint8_t flags; /* Entry control flags */
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
char name[CONFIG_MTD_CONFIG_NAME_LEN];
|
|
|
|
#else
|
2014-06-30 15:52:57 +02:00
|
|
|
uint8_t instance; /* Instance of the item */
|
|
|
|
uint16_t id; /* ID of the config data item */
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2014-06-30 15:52:57 +02:00
|
|
|
uint16_t len; /* Length of the data block */
|
2017-01-09 12:17:49 +01:00
|
|
|
} end_packed_struct;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_open(FAR struct file *filep);
|
|
|
|
static int mtdconfig_close(FAR struct file *filep);
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t mtdconfig_read(FAR struct file *filep, FAR char *buffer,
|
2022-03-21 21:10:09 +01:00
|
|
|
size_t buflen);
|
2015-11-29 21:42:56 +01:00
|
|
|
static int mtdconfig_ioctl(FAR struct file *filep, int cmd,
|
2022-03-21 21:10:09 +01:00
|
|
|
unsigned long arg);
|
2013-11-01 14:39:20 +01:00
|
|
|
static int mtdconfig_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-03-21 21:10:09 +01:00
|
|
|
bool setup);
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations mtdconfig_fops =
|
|
|
|
{
|
|
|
|
mtdconfig_open, /* open */
|
|
|
|
mtdconfig_close, /* close */
|
|
|
|
mtdconfig_read, /* read */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
mtdconfig_ioctl, /* ioctl */
|
|
|
|
mtdconfig_poll /* poll */
|
2022-01-10 15:51:17 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
, NULL /* unlink */
|
|
|
|
#endif
|
2013-11-01 14:39:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_readbytes
|
|
|
|
*
|
2017-05-11 15:19:24 +02:00
|
|
|
* Reads bytes from the contained MTD device. This will either use
|
2013-11-01 14:39:20 +01:00
|
|
|
* the read function or if that is not available, the bread with a copy.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
static int mtdconfig_readbytes(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
int offset, FAR uint8_t *pdata,
|
|
|
|
int readlen)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
off_t bytestoread = readlen;
|
2019-01-02 16:52:55 +01:00
|
|
|
off_t bytesthisblock;
|
|
|
|
off_t firstbyte;
|
|
|
|
off_t block;
|
|
|
|
off_t index;
|
2013-11-01 14:39:20 +01:00
|
|
|
int ret = OK;
|
|
|
|
size_t bytes;
|
|
|
|
|
2017-05-11 15:19:24 +02:00
|
|
|
/* Test if read interface supported. If it is, use it directly. */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2017-05-11 15:19:24 +02:00
|
|
|
if ((dev->mtd->read != NULL) && (readlen < dev->blocksize))
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* Read interface available. Read directly to buffer */
|
|
|
|
|
|
|
|
bytes = MTD_READ(dev->mtd, offset, readlen, pdata);
|
|
|
|
if (bytes != readlen)
|
|
|
|
{
|
|
|
|
/* Error reading data! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Read interface not available, do a block read into our buffer */
|
|
|
|
|
|
|
|
block = offset / dev->blocksize;
|
|
|
|
firstbyte = offset - (block * dev->blocksize);
|
|
|
|
bytesthisblock = dev->blocksize - firstbyte;
|
|
|
|
if (bytesthisblock > readlen)
|
|
|
|
{
|
|
|
|
bytesthisblock = readlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
while (bytestoread > 0)
|
|
|
|
{
|
2020-06-06 19:21:44 +02:00
|
|
|
if (bytesthisblock < dev->blocksize ||
|
|
|
|
bytestoread < dev->blocksize)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* Copy to temp buffer first...don't need the whole block */
|
|
|
|
|
|
|
|
bytes = MTD_BREAD(dev->mtd, block, 1, dev->buffer);
|
|
|
|
if (bytes != 1)
|
|
|
|
{
|
|
|
|
/* Error reading data! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy data to the output buffer */
|
|
|
|
|
|
|
|
memcpy(&pdata[index], &dev->buffer[firstbyte], bytesthisblock);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are reading a whole block. Read directly to buffer */
|
|
|
|
|
|
|
|
bytes = MTD_BREAD(dev->mtd, block, 1, &pdata[index]);
|
|
|
|
if (bytes != 1)
|
|
|
|
{
|
|
|
|
/* Error reading data! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update values for next block read */
|
|
|
|
|
|
|
|
bytestoread -= bytesthisblock;
|
|
|
|
index += bytesthisblock;
|
2013-11-04 15:52:16 +01:00
|
|
|
bytesthisblock = dev->blocksize;
|
|
|
|
if (bytesthisblock > bytestoread)
|
|
|
|
{
|
|
|
|
bytesthisblock = bytestoread;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
firstbyte = 0;
|
|
|
|
block++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_writebytes
|
|
|
|
*
|
2017-05-10 16:25:39 +02:00
|
|
|
* Writes bytes to the contained MTD device. This will either use
|
2013-11-01 14:39:20 +01:00
|
|
|
* the byte write function or if that is not available, the bwrite.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
static int mtdconfig_writebytes(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
int offset, FAR const uint8_t *pdata,
|
|
|
|
int writelen)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
|
|
|
|
|
|
|
/* Test if this MTD device supports byte write */
|
|
|
|
|
|
|
|
if (dev->mtd->write != NULL)
|
|
|
|
{
|
|
|
|
ret = MTD_WRITE(dev->mtd, offset, writelen, pdata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
/* Perform the write using the block write method of the MTD */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
uint16_t block;
|
|
|
|
uint16_t index;
|
|
|
|
off_t bytes_this_block;
|
|
|
|
off_t bytes_written = 0;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2022-03-21 21:10:09 +01:00
|
|
|
while (writelen > 0)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2017-05-11 21:35:56 +02:00
|
|
|
/* Read existing data from the block into the buffer */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
block = offset / dev->blocksize;
|
|
|
|
ret = MTD_BREAD(dev->mtd, block, 1, dev->buffer);
|
|
|
|
if (ret != 1)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
index = offset - block * dev->blocksize;
|
|
|
|
bytes_this_block = dev->blocksize - index;
|
2013-11-01 14:39:20 +01:00
|
|
|
if (bytes_this_block > writelen)
|
|
|
|
{
|
|
|
|
bytes_this_block = writelen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now write data to the block */
|
|
|
|
|
|
|
|
memcpy(&dev->buffer[index], pdata, bytes_this_block);
|
|
|
|
ret = MTD_BWRITE(dev->mtd, block, 1, dev->buffer);
|
|
|
|
if (ret != 1)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update writelen, etc. */
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
writelen -= bytes_this_block;
|
|
|
|
pdata += bytes_this_block;
|
|
|
|
offset += bytes_this_block;
|
2013-11-04 15:52:16 +01:00
|
|
|
bytes_written += bytes_this_block;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
/* Return the number of bytes written */
|
|
|
|
|
|
|
|
ret = bytes_written;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_findfirstentry
|
|
|
|
*
|
|
|
|
* Locates the first config entry, even if it is empty.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-01 14:39:20 +01:00
|
|
|
* offset to the start of the entry.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_findfirstentry(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
FAR struct mtdconfig_header_s *phdr)
|
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
off_t offset = CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
uint8_t sig[CONFIGDATA_BLOCK_HDR_SIZE];
|
|
|
|
bool found = false;
|
|
|
|
int ret;
|
|
|
|
uint16_t block;
|
|
|
|
off_t bytes_left_in_block;
|
|
|
|
uint16_t endblock;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2017-06-01 14:22:27 +02:00
|
|
|
/* Read the signature bytes */
|
|
|
|
|
|
|
|
ret = mtdconfig_readbytes(dev, 0, sig, sizeof(sig));
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-01 19:22:55 +01:00
|
|
|
if (sig[0] != 'C' || sig[1] != 'D' || sig[2] != CONFIGDATA_FORMAT_VERSION)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* Config Data partition not formatted. */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_RAM_CONSOLIDATE
|
|
|
|
endblock = dev->neraseblocks;
|
|
|
|
#else
|
|
|
|
if (dev->neraseblocks == 1)
|
|
|
|
{
|
|
|
|
endblock = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
endblock = dev->neraseblocks - 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
/* Config is formatted. Now loop until we find the first entry */
|
|
|
|
|
|
|
|
while (!found)
|
|
|
|
{
|
|
|
|
/* Read headers until we find one that hasn't been released.
|
|
|
|
*/
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
ret = mtdconfig_readbytes(dev, offset, (FAR uint8_t *)phdr,
|
2013-11-01 14:39:20 +01:00
|
|
|
sizeof(struct mtdconfig_header_s));
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if this header has been released */
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->flags != MTD_ERASED_FLAGS(dev))
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* This entry has been released. Advance to next entry */
|
|
|
|
|
|
|
|
offset += sizeof(struct mtdconfig_header_s) + phdr->len;
|
2013-11-04 15:52:16 +01:00
|
|
|
block = offset / dev->erasesize;
|
|
|
|
bytes_left_in_block = (block + 1) * dev->erasesize - offset;
|
|
|
|
if (bytes_left_in_block <= sizeof(*phdr))
|
|
|
|
{
|
2018-12-20 21:14:40 +01:00
|
|
|
offset = (block + 1) * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-04 15:52:16 +01:00
|
|
|
if (block + 1 >= endblock)
|
2018-12-20 21:14:40 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_left_in_block == dev->erasesize)
|
|
|
|
{
|
2018-12-20 21:14:40 +01:00
|
|
|
/* Skip block header */
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
offset += CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We found the first entry! */
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the offset of the first entry */
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_findnextentry
|
|
|
|
*
|
|
|
|
* Locates the next config entry starting at offset, even if it is empty.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-11-01 14:39:20 +01:00
|
|
|
* offset to the start of the next entry.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_findnextentry(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
off_t offset,
|
2013-11-04 15:52:16 +01:00
|
|
|
FAR struct mtdconfig_header_s *phdr,
|
|
|
|
uint16_t size)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
bool found = false;
|
|
|
|
int ret;
|
2019-01-02 16:52:55 +01:00
|
|
|
uint16_t block;
|
|
|
|
uint16_t bytes_left_in_block;
|
2013-11-04 15:52:16 +01:00
|
|
|
uint16_t endblock;
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_RAM_CONSOLIDATE
|
|
|
|
endblock = dev->neraseblocks;
|
|
|
|
#else
|
|
|
|
if (dev->neraseblocks == 1)
|
|
|
|
{
|
|
|
|
endblock = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
endblock = dev->neraseblocks - 1;
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Loop until next entry found */
|
|
|
|
|
|
|
|
while (!found)
|
|
|
|
{
|
|
|
|
/* Calculate offset of the next entry */
|
|
|
|
|
|
|
|
offset += sizeof(struct mtdconfig_header_s) + phdr->len;
|
2013-11-04 15:52:16 +01:00
|
|
|
if (offset % dev->erasesize == 0)
|
|
|
|
{
|
|
|
|
offset += CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset >= endblock * dev->erasesize)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if too close to the end of the erase block */
|
|
|
|
|
|
|
|
block = offset / dev->erasesize;
|
|
|
|
bytes_left_in_block = (block + 1) * dev->erasesize - offset;
|
|
|
|
if (bytes_left_in_block <= sizeof(*phdr))
|
|
|
|
{
|
|
|
|
offset = (block + 1) * dev->erasesize + CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
if (block + 1 >= endblock)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Read next header */
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
read_next:
|
2019-01-02 16:52:55 +01:00
|
|
|
ret = mtdconfig_readbytes(dev, offset, (FAR uint8_t *)phdr,
|
|
|
|
sizeof(*phdr));
|
2013-11-01 14:39:20 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if this header has is still active */
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->flags == MTD_ERASED_FLAGS(dev))
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->name[0] == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->id == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* If we are searching for free space, then check
|
|
|
|
* if this space is large enough. If it is, then
|
|
|
|
* we are done.
|
|
|
|
*/
|
|
|
|
|
|
|
|
block = offset / dev->erasesize;
|
|
|
|
bytes_left_in_block = (block + 1) * dev->erasesize - offset;
|
|
|
|
if (size > 0 && bytes_left_in_block >= size + sizeof(*phdr))
|
|
|
|
{
|
|
|
|
/* Free entry of large enough size found */
|
|
|
|
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance to next erase block and continue search */
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
offset = (block + 1) * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
if (block + 1 >= endblock)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't return to top of loop since we manually adjusted
|
|
|
|
* the offset, and don't want to go to the next entry.
|
|
|
|
*/
|
|
|
|
|
|
|
|
goto read_next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If we are searching for existing, active entries
|
|
|
|
* only, then we are done ... we found one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_ramconsolidate
|
|
|
|
*
|
|
|
|
* Attempts to perform a RAM based consolidation of released
|
|
|
|
* items. This requires a large enough free RAM block. This
|
|
|
|
* method of consolidation is used when only a single erase
|
|
|
|
* block is available in the partition.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2018-02-19 22:03:47 +01:00
|
|
|
* Offset to the next available entry (after consolidation).
|
2013-11-01 14:39:20 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-02-19 22:03:47 +01:00
|
|
|
static off_t mtdconfig_ramconsolidate(FAR struct mtdconfig_struct_s *dev)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2019-01-02 16:52:55 +01:00
|
|
|
FAR uint8_t *pbuf;
|
2013-11-04 15:52:16 +01:00
|
|
|
FAR struct mtdconfig_header_s *phdr;
|
|
|
|
struct mtdconfig_header_s hdr;
|
2019-01-02 16:52:55 +01:00
|
|
|
uint16_t src_block = 0;
|
|
|
|
uint16_t dst_block = 0;
|
|
|
|
uint16_t blkper;
|
2013-11-04 15:52:16 +01:00
|
|
|
off_t dst_offset = CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
off_t src_offset = CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
off_t bytes_left_in_block;
|
|
|
|
uint8_t sig[CONFIGDATA_BLOCK_HDR_SIZE];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate a consolidation buffer */
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
pbuf = (FAR uint8_t *)kmm_malloc(dev->erasesize);
|
|
|
|
if (pbuf == NULL)
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* Unable to allocate buffer, can't consolidate! */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop for all blocks and consolidate them */
|
|
|
|
|
|
|
|
blkper = dev->erasesize / dev->blocksize;
|
|
|
|
while (src_block < dev->neraseblocks)
|
|
|
|
{
|
2019-01-02 16:52:55 +01:00
|
|
|
/* Point to beginning of pbuf and read the next erase block */
|
2013-11-04 15:52:16 +01:00
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
ret = MTD_BREAD(dev->mtd, src_block * blkper, blkper, pbuf);
|
2013-11-04 15:52:16 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Error doing block read */
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now erase the block */
|
|
|
|
|
|
|
|
ret = MTD_ERASE(dev->mtd, src_block, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Error erasing the block */
|
|
|
|
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is block zero, then write a format signature */
|
|
|
|
|
|
|
|
if (src_block == 0)
|
|
|
|
{
|
|
|
|
sig[0] = 'C';
|
|
|
|
sig[1] = 'D';
|
|
|
|
sig[2] = CONFIGDATA_FORMAT_VERSION;
|
2018-02-19 22:03:47 +01:00
|
|
|
|
|
|
|
ret = mtdconfig_writebytes(dev, 0, sig, sizeof(sig));
|
|
|
|
if (ret != sizeof(sig))
|
|
|
|
{
|
|
|
|
/* Cannot write even the signature. */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
|
|
|
|
2018-02-19 22:03:47 +01:00
|
|
|
/* Copy active items back to the MTD device. */
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
while (src_offset < dev->erasesize)
|
|
|
|
{
|
2019-01-02 16:52:55 +01:00
|
|
|
phdr = (FAR struct mtdconfig_header_s *) &pbuf[src_offset];
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->name[0] == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->id == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
2018-02-19 22:03:47 +01:00
|
|
|
/* No more data in this erase block. */
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
src_offset = dev->erasesize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->flags == MTD_ERASED_FLAGS(dev))
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* This is an active entry. Copy it. Check if it
|
|
|
|
* fits in the current destination block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bytes_left_in_block = (dst_block + 1) * dev->erasesize -
|
|
|
|
dst_offset;
|
|
|
|
if (bytes_left_in_block < sizeof(*phdr) + phdr->len)
|
|
|
|
{
|
|
|
|
/* Item won't fit in the destination block. Move to
|
2018-02-19 22:03:47 +01:00
|
|
|
* the next block.
|
2013-11-04 15:52:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
dst_block++;
|
2018-12-20 21:14:40 +01:00
|
|
|
dst_offset = dst_block * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
/* Test for program bug. We shouldn't ever overflow
|
|
|
|
* even if no entries were inactive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(dst_block != dev->neraseblocks);
|
|
|
|
}
|
|
|
|
|
2018-02-19 22:03:47 +01:00
|
|
|
/* Now write the item to the current dst_offset location. */
|
2013-11-04 15:52:16 +01:00
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
ret = mtdconfig_writebytes(dev, dst_offset,
|
|
|
|
(FAR uint8_t *)phdr, sizeof(hdr));
|
2018-02-19 22:03:47 +01:00
|
|
|
if (ret != sizeof(hdr))
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
2018-02-19 22:03:47 +01:00
|
|
|
/* I/O Error! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
2013-11-04 15:52:16 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_offset += sizeof(hdr);
|
2018-02-19 22:03:47 +01:00
|
|
|
ret = mtdconfig_writebytes(dev, dst_offset,
|
2019-01-02 16:52:55 +01:00
|
|
|
&pbuf[src_offset + sizeof(hdr)],
|
2018-12-20 21:14:40 +01:00
|
|
|
phdr->len);
|
2018-02-19 22:03:47 +01:00
|
|
|
if (ret != phdr->len)
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
dst_offset += phdr->len;
|
|
|
|
|
|
|
|
/* Test if enough space in dst block for another header */
|
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
if ((dst_offset + sizeof(hdr) >= (dst_block + 1) *
|
|
|
|
dev->erasesize) || (dst_offset == (dst_block + 1) *
|
|
|
|
dev->erasesize))
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
dst_block++;
|
2018-12-20 21:14:40 +01:00
|
|
|
dst_offset = dst_block * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment past the current source item */
|
|
|
|
|
|
|
|
src_offset += sizeof(hdr) + phdr->len;
|
|
|
|
if (src_offset + sizeof(hdr) > dev->erasesize)
|
|
|
|
{
|
|
|
|
src_offset = dev->erasesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(src_offset <= dev->erasesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment to next source block */
|
|
|
|
|
|
|
|
src_block++;
|
|
|
|
src_offset = CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
kmm_free(pbuf);
|
2013-11-04 15:52:16 +01:00
|
|
|
return dst_offset;
|
2018-02-19 22:03:47 +01:00
|
|
|
|
|
|
|
errout:
|
2019-01-02 16:52:55 +01:00
|
|
|
kmm_free(pbuf);
|
2018-02-19 22:03:47 +01:00
|
|
|
ferr("ERROR: fail ram consolidate: %d\n", ret);
|
|
|
|
return 0;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_consolidate
|
|
|
|
*
|
|
|
|
* Performs consolidation by writing erase block zero to the
|
|
|
|
* reserved block at the end, erasing block zero, then
|
|
|
|
* walking through each item and copying them to the newly
|
|
|
|
* erased block. It erases all blocks to the end of the
|
|
|
|
* partition as it goes.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2018-02-19 22:03:47 +01:00
|
|
|
* Offset to the next available entry (after consolidation).
|
2013-11-01 14:39:20 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
#ifndef CONFIG_MTD_CONFIG_RAM_CONSOLIDATE
|
2013-11-01 14:39:20 +01:00
|
|
|
static off_t mtdconfig_consolidate(FAR struct mtdconfig_struct_s *dev)
|
|
|
|
{
|
2019-01-02 16:52:55 +01:00
|
|
|
off_t src_block;
|
|
|
|
off_t dst_block;
|
|
|
|
off_t src_offset;
|
|
|
|
off_t dst_offset;
|
|
|
|
uint16_t blkper;
|
|
|
|
uint16_t x;
|
|
|
|
uint16_t bytes;
|
|
|
|
uint16_t bytes_left_in_block;
|
2013-11-01 14:39:20 +01:00
|
|
|
struct mtdconfig_header_s hdr;
|
|
|
|
int ret;
|
2013-11-04 15:52:16 +01:00
|
|
|
uint8_t sig[CONFIGDATA_BLOCK_HDR_SIZE];
|
2019-01-02 16:52:55 +01:00
|
|
|
FAR uint8_t *pbuf;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Prepare to copy block 0 to the last block (erase blocks) */
|
|
|
|
|
|
|
|
src_block = 0;
|
|
|
|
dst_block = dev->neraseblocks - 1;
|
|
|
|
|
|
|
|
/* Ensure the last block is erased */
|
|
|
|
|
|
|
|
MTD_ERASE(dev->mtd, dst_block, 1);
|
|
|
|
blkper = dev->erasesize / dev->blocksize;
|
|
|
|
dst_block *= blkper; /* Convert to read/write blocks */
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Allocate a small buffer for moving data */
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
pbuf = (FAR uint8_t *)kmm_malloc(dev->blocksize);
|
|
|
|
if (pbuf == NULL)
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
/* Now copy block zero to last block */
|
|
|
|
|
|
|
|
for (x = 0; x < blkper; x++)
|
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
ret = MTD_BREAD(dev->mtd, src_block++, 1, dev->buffer);
|
2013-11-01 14:39:20 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
goto errout;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
ret = MTD_BWRITE(dev->mtd, dst_block++, 1, dev->buffer);
|
2013-11-01 14:39:20 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
goto errout;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Erase block zero and write a format signature. */
|
|
|
|
|
|
|
|
MTD_ERASE(dev->mtd, 0, 1);
|
|
|
|
sig[0] = 'C';
|
|
|
|
sig[1] = 'D';
|
2013-11-01 19:22:55 +01:00
|
|
|
sig[2] = CONFIGDATA_FORMAT_VERSION;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2018-02-19 22:03:47 +01:00
|
|
|
ret = mtdconfig_writebytes(dev, 0, sig, sizeof(sig));
|
|
|
|
if (ret != sizeof(sig))
|
|
|
|
{
|
|
|
|
/* Cannot write even the signature. */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now consolidate entries. */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
src_block = 1;
|
|
|
|
dst_block = 0;
|
2013-11-04 15:52:16 +01:00
|
|
|
src_offset = src_block * dev->erasesize + CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
dst_offset = CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
while (src_block < dev->neraseblocks)
|
|
|
|
{
|
|
|
|
/* Scan all headers and move them to the src_offset */
|
|
|
|
|
|
|
|
retry_relocate:
|
2020-06-06 19:21:44 +02:00
|
|
|
bytes = MTD_READ(dev->mtd, src_offset,
|
|
|
|
sizeof(hdr), (FAR uint8_t *)&hdr);
|
2017-06-01 14:22:27 +02:00
|
|
|
if (bytes != sizeof(hdr))
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
2018-02-19 22:03:47 +01:00
|
|
|
ret = -EIO;
|
2017-06-01 14:22:27 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
if (hdr.flags == MTD_ERASED_FLAGS(dev))
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Test if the source entry is active or if we are at the end
|
|
|
|
* of data for this erase block.
|
|
|
|
*/
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2022-09-07 07:33:40 +02:00
|
|
|
if (hdr.name[0] == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (hdr.id == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* No more data in this erase block. Advance to the
|
|
|
|
* next one.
|
|
|
|
*/
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
src_offset = (src_block + 1) * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
|
|
|
else
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2020-06-06 19:21:44 +02:00
|
|
|
/* Test if this entry will fit in the current destination
|
|
|
|
* block.
|
|
|
|
*/
|
2013-11-04 15:52:16 +01:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
bytes_left_in_block = (dst_block + 1) * dev->erasesize -
|
|
|
|
dst_offset;
|
2013-11-04 15:52:16 +01:00
|
|
|
if (hdr.len + sizeof(hdr) > bytes_left_in_block)
|
|
|
|
{
|
2020-06-06 19:21:44 +02:00
|
|
|
/* Item doesn't fit in the block. Advance to the next
|
|
|
|
* one.
|
|
|
|
*/
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Update control variables */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
dst_block++;
|
2018-12-20 21:14:40 +01:00
|
|
|
dst_offset = dst_block * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
DEBUGASSERT(dst_block != src_block);
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Retry the relocate */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
goto retry_relocate;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Copy this entry to the destination */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
ret = mtdconfig_writebytes(dev, dst_offset,
|
|
|
|
(FAR uint8_t *)&hdr, sizeof(hdr));
|
2018-02-19 22:03:47 +01:00
|
|
|
if (ret != sizeof(hdr))
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
src_offset += sizeof(hdr);
|
|
|
|
dst_offset += sizeof(hdr);
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Now copy the data */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
while (hdr.len)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
bytes = hdr.len;
|
|
|
|
if (bytes > dev->blocksize)
|
|
|
|
{
|
|
|
|
bytes = dev->blocksize;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Move the data. */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
ret = mtdconfig_readbytes(dev, src_offset, pbuf, bytes);
|
2018-02-19 22:03:47 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
ret = mtdconfig_writebytes(dev, dst_offset, pbuf, bytes);
|
2018-02-19 22:03:47 +01:00
|
|
|
if (ret != bytes)
|
|
|
|
{
|
|
|
|
/* I/O Error! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Update control variables */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
hdr.len -= bytes;
|
|
|
|
src_offset += bytes;
|
|
|
|
dst_offset += bytes;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This item has been released. Skip it! */
|
|
|
|
|
|
|
|
src_offset += sizeof(hdr) + hdr.len;
|
2013-11-04 15:52:16 +01:00
|
|
|
if (src_offset + sizeof(hdr) >= (src_block + 1) * dev->erasesize ||
|
2015-10-10 18:41:00 +02:00
|
|
|
src_offset == (src_block + 1) * dev->erasesize)
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* No room left at end of source block */
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
src_offset = (src_block + 1) * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if we are out of space in the src block */
|
|
|
|
|
|
|
|
if (src_offset + sizeof(hdr) >= (src_block + 1) * dev->erasesize)
|
|
|
|
{
|
|
|
|
/* No room at end of src block for another header. Go to next
|
|
|
|
* source block.
|
|
|
|
*/
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
src_offset = (src_block + 1) * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if we advanced to the next block. If we did, then erase the
|
|
|
|
* old block.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (src_block != src_offset / dev->erasesize)
|
|
|
|
{
|
|
|
|
/* Erase the block ... we have emptied it */
|
|
|
|
|
|
|
|
MTD_ERASE(dev->mtd, src_block, 1);
|
|
|
|
src_block++;
|
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
/* Test if we are out of space in the dst block */
|
|
|
|
|
|
|
|
if (dst_offset + sizeof(hdr) >= (dst_block + 1) * dev->erasesize)
|
|
|
|
{
|
2020-06-06 19:21:44 +02:00
|
|
|
/* No room at end of dst block for another header.
|
|
|
|
* Go to next block.
|
|
|
|
*/
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
dst_block++;
|
2020-06-06 19:21:44 +02:00
|
|
|
dst_offset = dst_block * dev->erasesize +
|
|
|
|
CONFIGDATA_BLOCK_HDR_SIZE;
|
2013-11-04 15:52:16 +01:00
|
|
|
DEBUGASSERT(dst_block != src_block);
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
kmm_free(pbuf);
|
2018-02-19 22:03:47 +01:00
|
|
|
return dst_offset;
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
errout:
|
2019-01-02 16:52:55 +01:00
|
|
|
kmm_free(pbuf);
|
2018-02-19 22:03:47 +01:00
|
|
|
ferr("ERROR: fail consolidate: %d\n", ret);
|
2013-11-01 14:39:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-19 22:03:47 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
#endif /* CONFIG_MTD_CONFIG_RAM_CONSOLIDATE */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct mtdconfig_struct_s *dev = inode->i_private;
|
2014-02-11 01:08:49 +01:00
|
|
|
int ret;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the device */
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&dev->exclsem);
|
2013-11-01 14:39:20 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
2013-11-01 14:39:20 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->readoff = 0;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct mtdconfig_struct_s *dev = inode->i_private;
|
|
|
|
|
|
|
|
/* Release exclusive access to the device */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&dev->exclsem);
|
2013-11-01 14:39:20 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t mtdconfig_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct mtdconfig_struct_s *dev = inode->i_private;
|
2019-01-02 16:52:55 +01:00
|
|
|
size_t bytes;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
if (dev->readoff >= dev->neraseblocks * dev->erasesize)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read data from the file */
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
bytes = MTD_READ(dev->mtd, dev->readoff, len, (FAR uint8_t *)buffer);
|
2017-06-01 14:22:27 +02:00
|
|
|
if (bytes != len)
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
dev->readoff += bytes;
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_findentry
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_findentry(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
off_t offset,
|
|
|
|
FAR struct config_data_s *pdata,
|
|
|
|
FAR struct mtdconfig_header_s *phdr)
|
|
|
|
{
|
|
|
|
uint16_t endblock;
|
2017-06-01 14:22:27 +02:00
|
|
|
int ret;
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_RAM_CONSOLIDATE
|
|
|
|
endblock = dev->neraseblocks;
|
|
|
|
#else
|
|
|
|
if (dev->neraseblocks == 1)
|
|
|
|
{
|
|
|
|
endblock = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
endblock = dev->neraseblocks - 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
while (offset > 0 && strcmp(pdata->name, phdr->name) != 0)
|
|
|
|
#else
|
2017-10-04 23:22:27 +02:00
|
|
|
while (offset > 0 && (pdata->id != phdr->id ||
|
|
|
|
pdata->instance != phdr->instance))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->name[0] == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->id == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* Advance to the next block and continue the search */
|
|
|
|
|
|
|
|
offset = (offset + dev->erasesize) / dev->erasesize;
|
|
|
|
offset = offset * dev->erasesize + CONFIGDATA_BLOCK_HDR_SIZE;
|
|
|
|
if (offset >= endblock * dev->erasesize)
|
|
|
|
{
|
|
|
|
/* Entry doesn't exist on the device */
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the 1st header from the next block */
|
|
|
|
|
2019-01-02 16:52:55 +01:00
|
|
|
ret = mtdconfig_readbytes(dev, offset, (FAR uint8_t *)phdr,
|
2018-12-20 21:14:40 +01:00
|
|
|
sizeof(*phdr));
|
2017-06-01 14:22:27 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* Error reading the data */
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
if (phdr->flags == MTD_ERASED_FLAGS(dev))
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nope, not the last header. Get the next one */
|
|
|
|
|
|
|
|
offset = mtdconfig_findnextentry(dev, offset, phdr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_setconfig
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_setconfig(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
FAR struct config_data_s *pdata)
|
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
uint8_t sig[CONFIGDATA_BLOCK_HDR_SIZE]; /* Format signature bytes ("CD") */
|
2013-11-01 14:39:20 +01:00
|
|
|
char retrycount = 0;
|
|
|
|
int ret = -ENOSYS;
|
2019-01-02 16:52:55 +01:00
|
|
|
off_t offset;
|
|
|
|
off_t bytes_left_in_block;
|
|
|
|
off_t bytes;
|
2013-11-01 14:39:20 +01:00
|
|
|
uint16_t block;
|
|
|
|
struct mtdconfig_header_s hdr;
|
2013-11-04 15:52:16 +01:00
|
|
|
uint8_t ram_consolidate;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Allocate a temp block buffer */
|
|
|
|
|
2022-03-21 21:10:09 +01:00
|
|
|
dev->buffer = (FAR uint8_t *)kmm_malloc(dev->blocksize);
|
2017-05-10 16:25:39 +02:00
|
|
|
if (dev->buffer == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2017-05-10 16:25:39 +02:00
|
|
|
/* Read and validate the signature bytes */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
retry:
|
|
|
|
offset = mtdconfig_findfirstentry(dev, &hdr);
|
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
/* Config Data partition not formatted. */
|
|
|
|
|
|
|
|
if (retrycount)
|
|
|
|
{
|
|
|
|
ret = -ENOSYS;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to format the config partition */
|
|
|
|
|
|
|
|
ret = MTD_IOCTL(dev->mtd, MTDIOC_BULKERASE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a format signature */
|
|
|
|
|
|
|
|
sig[0] = 'C';
|
|
|
|
sig[1] = 'D';
|
2013-11-01 19:22:55 +01:00
|
|
|
sig[2] = CONFIGDATA_FORMAT_VERSION;
|
2018-02-19 22:03:47 +01:00
|
|
|
|
|
|
|
ret = mtdconfig_writebytes(dev, 0, sig, sizeof(sig));
|
|
|
|
if (ret != sizeof(sig))
|
|
|
|
{
|
|
|
|
/* Cannot write even the signature. */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Now go try to read the signature again (as verification) */
|
|
|
|
|
|
|
|
retrycount++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Okay, the Config Data partition is formatted. Check if the
|
|
|
|
* config item being written is already in the database. If it
|
|
|
|
* is, we must mark it as obsolete before creating a new entry.
|
|
|
|
*/
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
offset = mtdconfig_findentry(dev, offset, pdata, &hdr);
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Test if the header was found. */
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
if (offset > 0 && strcmp(pdata->name, hdr.name) == 0)
|
|
|
|
#else
|
2013-11-04 15:52:16 +01:00
|
|
|
if (offset > 0 && pdata->id == hdr.id && pdata->instance ==
|
|
|
|
hdr.instance)
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* Mark this entry as released */
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
hdr.flags = (uint8_t)~MTD_ERASED_FLAGS(dev);
|
2013-11-01 14:39:20 +01:00
|
|
|
mtdconfig_writebytes(dev, offset, &hdr.flags, sizeof(hdr.flags));
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Test if the new length is zero. If it is, then we are
|
|
|
|
* deleting the entry.
|
|
|
|
*/
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
if (pdata->len == 0)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
ret = OK;
|
|
|
|
goto errout;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Now find a new entry for this config data */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
retrycount = 0;
|
2018-02-19 22:03:47 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
retry_find:
|
|
|
|
offset = mtdconfig_findfirstentry(dev, &hdr);
|
2018-12-20 21:14:40 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2022-09-07 07:33:40 +02:00
|
|
|
if (offset > 0 && hdr.name[0] == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (offset > 0 && hdr.id == MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
block = offset / dev->erasesize;
|
2013-11-04 15:52:16 +01:00
|
|
|
bytes_left_in_block = (block + 1) * dev->erasesize - offset;
|
|
|
|
if (bytes_left_in_block < sizeof(hdr) + pdata->len)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Simulate an active block to search for the next one
|
|
|
|
* in the code below.
|
2013-11-01 14:39:20 +01:00
|
|
|
*/
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
hdr.name[0] = 1;
|
|
|
|
#else
|
2013-11-04 15:52:16 +01:00
|
|
|
hdr.id = 1;
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2022-09-07 07:33:40 +02:00
|
|
|
if (hdr.name[0] != MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (hdr.id != MTD_ERASED_ID(dev))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
{
|
|
|
|
/* Read the next entry */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
offset = mtdconfig_findnextentry(dev, offset, &hdr, pdata->len);
|
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
/* No free entries left on device! */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_RAM_CONSOLIDATE
|
|
|
|
ram_consolidate = 1;
|
|
|
|
#else
|
|
|
|
ram_consolidate = dev->neraseblocks == 1;
|
|
|
|
#endif
|
|
|
|
if (ram_consolidate)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* If we only have 1 erase block, then we must do a RAM
|
|
|
|
* assisted consolidation of released entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (retrycount)
|
|
|
|
{
|
|
|
|
/* Out of space! */
|
|
|
|
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
mtdconfig_ramconsolidate(dev);
|
2013-11-01 14:39:20 +01:00
|
|
|
retrycount++;
|
2013-11-04 15:52:16 +01:00
|
|
|
goto retry_find;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
#ifndef CONFIG_MTD_CONFIG_RAM_CONSOLIDATE
|
|
|
|
else
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
if (retrycount)
|
|
|
|
{
|
|
|
|
/* Out of space! */
|
|
|
|
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
mtdconfig_consolidate(dev);
|
2013-11-01 14:39:20 +01:00
|
|
|
retrycount++;
|
2013-11-04 15:52:16 +01:00
|
|
|
goto retry_find;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
#endif
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
}
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Test if a new entry was found */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
if (offset > 0)
|
|
|
|
{
|
|
|
|
/* Save the data at this entry */
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
strcpy(hdr.name, pdata->name);
|
|
|
|
#else
|
2013-11-04 15:52:16 +01:00
|
|
|
hdr.id = pdata->id;
|
|
|
|
hdr.instance = pdata->instance;
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-04 15:52:16 +01:00
|
|
|
hdr.len = pdata->len;
|
2022-09-07 07:33:40 +02:00
|
|
|
hdr.flags = MTD_ERASED_FLAGS(dev);
|
2015-10-10 18:41:00 +02:00
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
ret = mtdconfig_writebytes(dev, offset,
|
|
|
|
(FAR uint8_t *)&hdr, sizeof(hdr));
|
2018-02-19 22:03:47 +01:00
|
|
|
if (ret != sizeof(hdr))
|
2017-05-10 16:25:39 +02:00
|
|
|
{
|
|
|
|
/* Cannot write even header! */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
bytes = mtdconfig_writebytes(dev, offset + sizeof(hdr),
|
|
|
|
pdata->configdata, pdata->len);
|
2013-11-04 15:52:16 +01:00
|
|
|
if (bytes != pdata->len)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
2013-11-04 15:52:16 +01:00
|
|
|
/* Error writing data! */
|
2013-11-01 14:39:20 +01:00
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
hdr.flags = MTD_ERASED_FLAGS(dev);
|
2019-01-02 16:52:55 +01:00
|
|
|
mtdconfig_writebytes(dev, offset, (FAR uint8_t *)&hdr,
|
2018-12-20 21:14:40 +01:00
|
|
|
sizeof(hdr.flags));
|
2013-11-04 15:52:16 +01:00
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
2013-11-04 15:52:16 +01:00
|
|
|
|
|
|
|
ret = OK;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
|
|
|
|
/* Free the buffer */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev->buffer);
|
2013-11-01 14:39:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_getconfig
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_getconfig(FAR struct mtdconfig_struct_s *dev,
|
2019-01-02 16:52:55 +01:00
|
|
|
FAR struct config_data_s *pdata)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
int ret = -ENOSYS;
|
2019-01-02 16:52:55 +01:00
|
|
|
off_t offset;
|
|
|
|
off_t bytes_to_read;
|
2013-11-01 14:39:20 +01:00
|
|
|
struct mtdconfig_header_s hdr;
|
|
|
|
|
|
|
|
/* Allocate a temp block buffer */
|
|
|
|
|
2014-09-01 01:26:36 +02:00
|
|
|
dev->buffer = (FAR uint8_t *)kmm_malloc(dev->blocksize);
|
2013-11-01 14:39:20 +01:00
|
|
|
if (dev->buffer == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the offset of the first entry. This will also check
|
|
|
|
* the format signature bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = mtdconfig_findfirstentry(dev, &hdr);
|
2013-11-04 15:52:16 +01:00
|
|
|
offset = mtdconfig_findentry(dev, offset, pdata, &hdr);
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
/* Test if the header was found. */
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
if (offset > 0 && strcmp(pdata->name, hdr.name) == 0)
|
|
|
|
#else
|
2013-11-04 15:52:16 +01:00
|
|
|
if (offset > 0 && (pdata->id == hdr.id && pdata->instance == hdr.instance))
|
2018-12-20 21:14:40 +01:00
|
|
|
#endif
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* Entry found. Read the data */
|
|
|
|
|
|
|
|
bytes_to_read = hdr.len;
|
|
|
|
if (bytes_to_read > pdata->len)
|
|
|
|
{
|
|
|
|
bytes_to_read = pdata->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the read */
|
|
|
|
|
|
|
|
ret = mtdconfig_readbytes(dev, offset + sizeof(hdr), pdata->configdata,
|
|
|
|
bytes_to_read);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* Error reading the data */
|
|
|
|
|
|
|
|
ret = -EIO;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/* Set return data length to match the config item length */
|
|
|
|
|
|
|
|
pdata->len = hdr.len;
|
2013-11-01 14:39:20 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
2020-06-06 19:21:44 +02:00
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
/* Free the buffer */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev->buffer);
|
2013-11-01 14:39:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_deleteconfig
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_deleteconfig(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
FAR struct config_data_s *pdata)
|
|
|
|
{
|
|
|
|
int ret = -ENOENT;
|
|
|
|
off_t offset;
|
|
|
|
struct mtdconfig_header_s hdr;
|
|
|
|
|
|
|
|
/* Allocate a temp block buffer */
|
|
|
|
|
|
|
|
dev->buffer = (FAR uint8_t *)kmm_malloc(dev->blocksize);
|
|
|
|
if (dev->buffer == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the offset of the first entry. This will also check
|
|
|
|
* the format signature bytes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = mtdconfig_findfirstentry(dev, &hdr);
|
|
|
|
offset = mtdconfig_findentry(dev, offset, pdata, &hdr);
|
|
|
|
|
|
|
|
/* Test if the header was found. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
if (offset > 0 && strcmp(pdata->name, hdr.name) == 0)
|
|
|
|
#else
|
|
|
|
if (offset > 0 && (pdata->id == hdr.id && pdata->instance == hdr.instance))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Entry found. Mark this entry as released */
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
hdr.flags = (uint8_t)~MTD_ERASED_FLAGS(dev);
|
2018-12-20 21:14:40 +01:00
|
|
|
mtdconfig_writebytes(dev, offset, &hdr.flags, sizeof(hdr.flags));
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the buffer */
|
|
|
|
|
|
|
|
kmm_free(dev->buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-04-27 06:23:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_firstconfig
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_firstconfig(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
FAR struct config_data_s *pdata)
|
|
|
|
{
|
|
|
|
int ret = -ENOENT;
|
|
|
|
off_t bytes_to_read;
|
|
|
|
struct mtdconfig_header_s hdr;
|
|
|
|
|
|
|
|
/* Allocate a temp block buffer */
|
|
|
|
|
|
|
|
dev->buffer = (FAR uint8_t *)kmm_malloc(dev->blocksize);
|
|
|
|
if (dev->buffer == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->readoff = mtdconfig_findfirstentry(dev, &hdr);
|
|
|
|
|
|
|
|
/* Test if the config item is valid */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
if (dev->readoff != 0 &&
|
2022-09-07 07:33:40 +02:00
|
|
|
hdr.name[0] != MTD_ERASED_ID(dev))
|
2022-04-27 06:23:14 +02:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (dev->readoff != 0 && hdr.id != MTD_ERASED_ID(dev))
|
2022-04-27 06:23:14 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Perform the read */
|
|
|
|
|
|
|
|
bytes_to_read = hdr.len;
|
|
|
|
if (bytes_to_read > pdata->len)
|
|
|
|
{
|
|
|
|
bytes_to_read = pdata->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mtdconfig_readbytes(dev, dev->readoff + sizeof(hdr),
|
|
|
|
pdata->configdata, bytes_to_read);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set other return data items */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
strcpy(pdata->name, hdr.name);
|
|
|
|
#else
|
|
|
|
pdata->id = hdr.id;
|
|
|
|
pdata->instance = hdr.instance;
|
|
|
|
#endif
|
|
|
|
pdata->len = bytes_to_read;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
|
|
|
|
/* Free the buffer */
|
|
|
|
|
|
|
|
kmm_free(dev->buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_nextconfig
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_nextconfig(FAR struct mtdconfig_struct_s *dev,
|
|
|
|
FAR struct config_data_s *pdata)
|
|
|
|
{
|
|
|
|
int ret = -ENOENT;
|
|
|
|
off_t bytes_to_read;
|
|
|
|
struct mtdconfig_header_s hdr;
|
|
|
|
|
|
|
|
/* Allocate a temp block buffer */
|
|
|
|
|
|
|
|
dev->buffer = (FAR uint8_t *)kmm_malloc(dev->blocksize);
|
|
|
|
if (dev->buffer == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mtdconfig_readbytes(dev, dev->readoff, (FAR uint8_t *)&hdr,
|
|
|
|
sizeof(hdr));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->readoff = mtdconfig_findnextentry(dev, dev->readoff, &hdr, 0);
|
|
|
|
|
|
|
|
/* Test if the config item is valid */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
if (dev->readoff != 0 &&
|
2022-09-07 07:33:40 +02:00
|
|
|
hdr.name[0] != MTD_ERASED_ID(dev))
|
2022-04-27 06:23:14 +02:00
|
|
|
#else
|
2022-09-07 07:33:40 +02:00
|
|
|
if (dev->readoff != 0 && hdr.id != MTD_ERASED_ID(dev))
|
2022-04-27 06:23:14 +02:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Test if this is an empty slot */
|
|
|
|
|
|
|
|
bytes_to_read = hdr.len;
|
|
|
|
if (bytes_to_read > pdata->len)
|
|
|
|
{
|
|
|
|
bytes_to_read = pdata->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the config item data */
|
|
|
|
|
|
|
|
ret = mtdconfig_readbytes(dev, dev->readoff + sizeof(hdr),
|
|
|
|
pdata->configdata, bytes_to_read);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
strcpy(pdata->name, hdr.name);
|
|
|
|
#else
|
|
|
|
pdata->id = hdr.id;
|
|
|
|
pdata->instance = hdr.instance;
|
|
|
|
#endif
|
|
|
|
pdata->len = bytes_to_read;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
|
|
|
|
/* Free the buffer */
|
|
|
|
|
|
|
|
kmm_free(dev->buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 15:08:11 +01:00
|
|
|
static int mtdconfig_ioctl(FAR struct file *filep, int cmd,
|
|
|
|
unsigned long arg)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct mtdconfig_struct_s *dev = inode->i_private;
|
|
|
|
FAR struct config_data_s *pdata;
|
2020-06-06 19:21:44 +02:00
|
|
|
int ret = -ENOTTY;
|
2013-11-01 14:39:20 +01:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case CFGDIOC_SETCONFIG:
|
|
|
|
|
|
|
|
/* Set the config item */
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
pdata = (FAR struct config_data_s *)arg;
|
2013-11-01 14:39:20 +01:00
|
|
|
ret = mtdconfig_setconfig(dev, pdata);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CFGDIOC_GETCONFIG:
|
|
|
|
|
|
|
|
/* Get the config item */
|
|
|
|
|
2013-11-04 15:52:16 +01:00
|
|
|
pdata = (FAR struct config_data_s *)arg;
|
2013-11-01 14:39:20 +01:00
|
|
|
ret = mtdconfig_getconfig(dev, pdata);
|
|
|
|
break;
|
2018-12-20 21:14:40 +01:00
|
|
|
|
|
|
|
case CFGDIOC_DELCONFIG:
|
2020-06-06 19:21:44 +02:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/* Set the config item */
|
|
|
|
|
|
|
|
pdata = (FAR struct config_data_s *)arg;
|
|
|
|
ret = mtdconfig_deleteconfig(dev, pdata);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CFGDIOC_FIRSTCONFIG:
|
2020-06-06 19:21:44 +02:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/* Get the the first config item */
|
|
|
|
|
|
|
|
pdata = (FAR struct config_data_s *)arg;
|
2022-04-27 06:23:14 +02:00
|
|
|
ret = mtdconfig_firstconfig(dev, pdata);
|
2018-12-20 21:14:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CFGDIOC_NEXTCONFIG:
|
2020-06-06 19:21:44 +02:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/* Get the next config item */
|
|
|
|
|
|
|
|
pdata = (FAR struct config_data_s *)arg;
|
2022-04-27 06:23:14 +02:00
|
|
|
ret = mtdconfig_nextconfig(dev, pdata);
|
2018-12-20 21:14:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_BULKERASE:
|
2020-06-06 19:21:44 +02:00
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
/* Call the MTD's ioctl for this */
|
|
|
|
|
2022-03-21 21:10:09 +01:00
|
|
|
ret = MTD_IOCTL(dev->mtd, cmd, arg);
|
2018-12-20 21:14:40 +01:00
|
|
|
|
|
|
|
break;
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_poll
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int mtdconfig_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, POLLIN | POLLOUT);
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register a /dev/config device backed by an MTD
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mtdconfig_register(FAR struct mtd_dev_s *mtd)
|
|
|
|
{
|
2018-05-08 14:48:00 +02:00
|
|
|
int ret = -ENOMEM;
|
2013-11-01 14:39:20 +01:00
|
|
|
struct mtdconfig_struct_s *dev;
|
|
|
|
struct mtd_geometry_s geo; /* Device geometry */
|
|
|
|
|
2018-12-20 21:14:40 +01:00
|
|
|
dev = (struct mtdconfig_struct_s *)
|
|
|
|
kmm_malloc(sizeof(struct mtdconfig_struct_s));
|
|
|
|
if (dev != NULL)
|
2013-11-01 14:39:20 +01:00
|
|
|
{
|
|
|
|
/* Initialize the mtdconfig device structure */
|
|
|
|
|
|
|
|
dev->mtd = mtd;
|
|
|
|
|
|
|
|
/* Get the device geometry. (casting to uintptr_t first eliminates
|
|
|
|
* complaints on some architectures where the sizeof long is different
|
|
|
|
* from the size of a pointer).
|
|
|
|
*/
|
|
|
|
|
2020-06-06 19:21:44 +02:00
|
|
|
ret = MTD_IOCTL(mtd, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)((uintptr_t)&geo));
|
2013-11-01 14:39:20 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: MTD ioctl(MTDIOC_GEOMETRY) failed: %d\n", ret);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2013-11-01 14:39:20 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->blocksize = geo.blocksize;
|
|
|
|
dev->neraseblocks = geo.neraseblocks;
|
|
|
|
dev->erasesize = geo.erasesize;
|
|
|
|
dev->nblocks = geo.neraseblocks * geo.erasesize / geo.blocksize;
|
|
|
|
|
2022-09-07 07:33:40 +02:00
|
|
|
/* And query the erase state */
|
|
|
|
|
|
|
|
ret = MTD_IOCTL(mtd, MTDIOC_ERASESTATE,
|
|
|
|
(unsigned long)((uintptr_t)&dev->erasestate));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: MTD ioctl(MTDIOC_ERASESTATE) failed: %d\n", ret);
|
|
|
|
kmm_free(dev);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_init(&dev->exclsem, 0, 1);
|
2020-01-02 17:49:34 +01:00
|
|
|
register_driver("/dev/config", &mtdconfig_fops, 0666, dev);
|
2013-11-01 14:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-10 16:11:29 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtdconfig_unregister
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Unregister a /dev/config device backed by a MTD.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mtdconfig_unregister(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct file file;
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct mtdconfig_struct_s *dev;
|
|
|
|
|
|
|
|
ret = file_open(&file, "/dev/config", 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: open /dev/config failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inode = file.f_inode;
|
|
|
|
dev = (FAR struct mtdconfig_struct_s *)inode->i_private;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_destroy(&dev->exclsem);
|
2022-08-10 16:11:29 +02:00
|
|
|
kmm_free(dev);
|
|
|
|
|
|
|
|
file_close(&file);
|
|
|
|
|
|
|
|
unregister_driver("/dev/config");
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2013-11-01 14:39:20 +01:00
|
|
|
#endif /* CONFIG_MTD_CONFIG */
|