2013-04-30 20:37:34 +02:00
|
|
|
/****************************************************************************
|
2013-11-18 16:43:44 +01:00
|
|
|
* drivers/mtd/mtd_partition.c
|
2013-04-30 20:37:34 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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-04-30 20:37:34 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-04-30 20:37:34 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01: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-04-30 20:37:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2021-07-21 09:27:36 +02:00
|
|
|
#include <inttypes.h>
|
2013-04-30 20:37:34 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2013-12-12 16:21:55 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/stat.h>
|
2013-04-30 20:37:34 +02:00
|
|
|
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2013-05-02 16:07:42 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifdef CONFIG_FS_PROCFS
|
|
|
|
#include <nuttx/fs/procfs.h>
|
|
|
|
#endif
|
2013-04-30 20:37:34 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-09 15:24:17 +01:00
|
|
|
#define PART_NAME_MAX 15
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This type represents the state of the MTD device. The struct mtd_dev_s
|
|
|
|
* must appear at the beginning of the definition so that you can freely
|
|
|
|
* cast between pointers to struct mtd_dev_s and struct mtd_partition_s.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct mtd_partition_s
|
|
|
|
{
|
|
|
|
/* This structure must reside at the beginning so that we can simply cast
|
|
|
|
* from struct mtd_dev_s * to struct mtd_partition_s *
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct mtd_dev_s child; /* The "child" MTD vtable that manages the
|
|
|
|
* sub-region */
|
2020-11-11 08:09:08 +01:00
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/* Other implementation specific data may follow here */
|
|
|
|
|
|
|
|
FAR struct mtd_dev_s *parent; /* The "parent" MTD driver that manages the
|
|
|
|
* entire FLASH */
|
2013-05-01 01:02:34 +02:00
|
|
|
off_t firstblock; /* Offset to the first block of the managed
|
2013-04-30 20:37:34 +02:00
|
|
|
* sub-region */
|
|
|
|
uint16_t blkpererase; /* Number of R/W blocks in one erase block */
|
2023-10-09 12:28:27 +02:00
|
|
|
struct mtd_geometry_s geo; /* The geometry for the partition */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
|
|
|
|
struct mtd_partition_s *pnext; /* Pointer to next partition struct */
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
2018-11-09 15:24:17 +01:00
|
|
|
char name[PART_NAME_MAX + 1]; /* Name of the partition */
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This structure describes one open "file" */
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
|
|
|
|
struct part_procfs_file_s
|
|
|
|
{
|
|
|
|
struct procfs_file_s base; /* Base open file structure */
|
|
|
|
struct mtd_partition_s *nextpart;
|
2013-04-30 20:37:34 +02:00
|
|
|
};
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
2013-04-30 20:37:34 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* MTD driver methods */
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
static int part_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks);
|
|
|
|
static ssize_t part_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buf);
|
|
|
|
static ssize_t part_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buf);
|
|
|
|
static ssize_t part_read(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR uint8_t *buffer);
|
2013-05-02 16:07:42 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2013-12-12 16:21:55 +01:00
|
|
|
static ssize_t part_write(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR const uint8_t *buffer);
|
|
|
|
#endif
|
|
|
|
static int part_ioctl(FAR struct mtd_dev_s *dev, int cmd,
|
|
|
|
unsigned long arg);
|
2023-03-29 12:10:56 +02:00
|
|
|
static int part_isbad(FAR struct mtd_dev_s *dev, off_t block);
|
|
|
|
static int part_markbad(FAR struct mtd_dev_s *dev, off_t block);
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* File system methods */
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
|
|
|
|
static int part_procfs_open(FAR struct file *filep,
|
|
|
|
FAR const char *relpath, int oflags, mode_t mode);
|
|
|
|
static int part_procfs_close(FAR struct file *filep);
|
|
|
|
static ssize_t part_procfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
|
|
|
|
static int part_procfs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp);
|
|
|
|
|
|
|
|
#if 0 /* Not implemented */
|
|
|
|
static int part_procfs_opendir(const char *relpath,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int part_procfs_closedir(FAR struct fs_dirent_s *dir);
|
2022-07-26 08:56:41 +02:00
|
|
|
static int part_procfs_readdir(FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2013-12-12 16:21:55 +01:00
|
|
|
static int part_procfs_rewinddir(FAR struct fs_dirent_s *dir);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int part_procfs_stat(FAR const char *relpath,
|
|
|
|
FAR struct stat *buf);
|
2013-05-02 16:07:42 +02:00
|
|
|
#endif
|
2013-04-30 20:37:34 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
|
2017-06-01 14:22:27 +02:00
|
|
|
static struct mtd_partition_s *g_pfirstpartition = NULL;
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2023-04-17 20:27:33 +02:00
|
|
|
const struct procfs_operations g_part_operations =
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
part_procfs_open, /* open */
|
|
|
|
part_procfs_close, /* close */
|
|
|
|
part_procfs_read, /* read */
|
|
|
|
NULL, /* write */
|
2023-11-25 13:11:03 +01:00
|
|
|
NULL, /* poll */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
part_procfs_dup, /* dup */
|
|
|
|
|
|
|
|
NULL, /* opendir */
|
|
|
|
NULL, /* closedir */
|
|
|
|
NULL, /* readdir */
|
|
|
|
NULL, /* rewinddir */
|
|
|
|
|
|
|
|
part_procfs_stat /* stat */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_blockcheck
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the provided block offset lies within the partition
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool part_blockcheck(FAR struct mtd_partition_s *priv, off_t block)
|
|
|
|
{
|
|
|
|
off_t partsize;
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
partsize = priv->geo.neraseblocks * priv->blkpererase;
|
2013-05-02 16:07:42 +02:00
|
|
|
return block < partsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_bytecheck
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check if the provided byte offset lies within the partition
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static bool part_bytecheck(FAR struct mtd_partition_s *priv, off_t byoff)
|
|
|
|
{
|
|
|
|
off_t readend;
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
readend = (byoff + priv->geo.erasesize - 1) / priv->geo.erasesize;
|
|
|
|
return readend <= priv->geo.neraseblocks;
|
2013-05-02 16:07:42 +02:00
|
|
|
}
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_erase
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Erase several blocks, each of the size previously reported.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_erase(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
2013-05-02 16:07:42 +02:00
|
|
|
off_t eoffset;
|
2013-04-30 20:37:34 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Make sure that erase would not extend past the end of the partition */
|
|
|
|
|
2013-10-31 17:40:10 +01:00
|
|
|
if (!part_blockcheck(priv, (startblock + nblocks - 1) * priv->blkpererase))
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Erase beyond the end of the partition\n");
|
2013-04-30 20:37:34 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
2013-05-01 01:02:34 +02:00
|
|
|
/* Just add the partition offset to the requested block and let the
|
2013-04-30 20:37:34 +02:00
|
|
|
* underlying MTD driver perform the erase.
|
2013-05-02 16:07:42 +02:00
|
|
|
*
|
|
|
|
* NOTE: the offset here is in units of erase blocks.
|
2013-04-30 20:37:34 +02:00
|
|
|
*/
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
eoffset = priv->firstblock / priv->blkpererase;
|
|
|
|
DEBUGASSERT(eoffset * priv->blkpererase == priv->firstblock);
|
|
|
|
|
|
|
|
return priv->parent->erase(priv->parent, startblock + eoffset, nblocks);
|
2013-04-30 20:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_bread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the specified number of blocks into the user provided buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t part_bread(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR uint8_t *buf)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && (buf || nblocks == 0));
|
|
|
|
|
|
|
|
/* Make sure that read would not extend past the end of the partition */
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
if (!part_blockcheck(priv, startblock + nblocks - 1))
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Read beyond the end of the partition\n");
|
2013-04-30 20:37:34 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
2013-05-01 01:02:34 +02:00
|
|
|
/* Just add the partition offset to the requested block and let the
|
2013-04-30 20:37:34 +02:00
|
|
|
* underlying MTD driver perform the read.
|
|
|
|
*/
|
|
|
|
|
2013-05-01 01:02:34 +02:00
|
|
|
return priv->parent->bread(priv->parent, startblock + priv->firstblock,
|
2013-04-30 20:37:34 +02:00
|
|
|
nblocks, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_bwrite
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write the specified number of blocks from the user provided buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t part_bwrite(FAR struct mtd_dev_s *dev, off_t startblock,
|
|
|
|
size_t nblocks, FAR const uint8_t *buf)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && (buf || nblocks == 0));
|
|
|
|
|
|
|
|
/* Make sure that write would not extend past the end of the partition */
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
if (!part_blockcheck(priv, startblock + nblocks - 1))
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Write beyond the end of the partition\n");
|
2013-04-30 20:37:34 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
2013-05-01 01:02:34 +02:00
|
|
|
/* Just add the partition offset to the requested block and let the
|
2013-04-30 20:37:34 +02:00
|
|
|
* underlying MTD driver perform the write.
|
|
|
|
*/
|
|
|
|
|
2013-05-01 01:02:34 +02:00
|
|
|
return priv->parent->bwrite(priv->parent, startblock + priv->firstblock,
|
2013-04-30 20:37:34 +02:00
|
|
|
nblocks, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the specified number of bytes to the user provided buffer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-11 08:09:08 +01:00
|
|
|
static ssize_t part_read(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR uint8_t *buffer)
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
off_t newoffset;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && (buffer || nbytes == 0));
|
|
|
|
|
|
|
|
/* Does the underlying MTD device support the read method? */
|
|
|
|
|
|
|
|
if (priv->parent->read)
|
|
|
|
{
|
|
|
|
/* Make sure that read would not extend past the end of the partition */
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
if (!part_bytecheck(priv, offset + nbytes - 1))
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Read beyond the end of the partition\n");
|
2013-04-30 20:37:34 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
2013-05-01 01:02:34 +02:00
|
|
|
/* Just add the partition offset to the requested block and let the
|
2013-04-30 20:37:34 +02:00
|
|
|
* underlying MTD driver perform the read.
|
|
|
|
*/
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
newoffset = offset + priv->firstblock * priv->geo.blocksize;
|
2013-04-30 20:37:34 +02:00
|
|
|
return priv->parent->read(priv->parent, newoffset, nbytes, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The underlying MTD driver does not support the read() method */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2020-11-11 08:09:08 +01:00
|
|
|
static ssize_t part_write(FAR struct mtd_dev_s *dev, off_t offset,
|
|
|
|
size_t nbytes, FAR const uint8_t *buffer)
|
2013-05-02 16:07:42 +02:00
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
off_t newoffset;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && (buffer || nbytes == 0));
|
|
|
|
|
|
|
|
/* Does the underlying MTD device support the write method? */
|
|
|
|
|
|
|
|
if (priv->parent->write)
|
|
|
|
{
|
2020-11-11 08:09:08 +01:00
|
|
|
/* Make sure that write would not extend past the end of the
|
|
|
|
* partition
|
|
|
|
*/
|
2013-05-02 16:07:42 +02:00
|
|
|
|
|
|
|
if (!part_bytecheck(priv, offset + nbytes - 1))
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Write beyond the end of the partition\n");
|
2013-05-02 16:07:42 +02:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just add the partition offset to the requested block and let the
|
|
|
|
* underlying MTD driver perform the write.
|
|
|
|
*/
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
newoffset = offset + priv->firstblock * priv->geo.blocksize;
|
2013-05-02 16:07:42 +02:00
|
|
|
return priv->parent->write(priv->parent, newoffset, nbytes, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The underlying MTD driver does not support the write() method */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_ioctl(FAR struct mtd_dev_s *dev, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
int ret = -EINVAL; /* Assume good command with bad parameters */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case MTDIOC_GEOMETRY:
|
|
|
|
{
|
|
|
|
FAR struct mtd_geometry_s *geo = (FAR struct mtd_geometry_s *)arg;
|
2023-10-09 12:28:27 +02:00
|
|
|
if (geo != NULL)
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
2023-10-09 12:28:27 +02:00
|
|
|
memcpy(geo, &priv->geo, sizeof(*geo));
|
2013-04-30 20:37:34 +02:00
|
|
|
ret = OK;
|
2023-10-09 12:28:27 +02:00
|
|
|
}
|
2013-04-30 20:37:34 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-11 08:10:16 +02:00
|
|
|
case BIOC_PARTINFO:
|
2021-07-30 15:40:18 +02:00
|
|
|
{
|
|
|
|
FAR struct partition_info_s *info =
|
|
|
|
(FAR struct partition_info_s *)arg;
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
2023-10-09 12:28:27 +02:00
|
|
|
info->numsectors = priv->geo.neraseblocks * priv->blkpererase;
|
|
|
|
info->sectorsize = priv->geo.blocksize;
|
2021-07-30 15:40:18 +02:00
|
|
|
info->startsector = priv->firstblock;
|
|
|
|
|
2022-08-24 04:23:20 +02:00
|
|
|
strlcpy(info->parent, priv->parent->name,
|
|
|
|
sizeof(info->parent));
|
2021-07-30 15:40:18 +02:00
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-08-11 09:46:01 +02:00
|
|
|
case BIOC_XIPBASE:
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
|
|
|
FAR void **ppv = (FAR void**)arg;
|
|
|
|
unsigned long base;
|
|
|
|
|
|
|
|
if (ppv)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Get the XIP base of the entire FLASH */
|
2013-04-30 20:37:34 +02:00
|
|
|
|
2021-08-11 09:46:01 +02:00
|
|
|
ret = priv->parent->ioctl(priv->parent, BIOC_XIPBASE,
|
2013-04-30 20:37:34 +02:00
|
|
|
(unsigned long)((uintptr_t)&base));
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Add the offset of this partition to the XIP base and
|
2013-04-30 20:37:34 +02:00
|
|
|
* return the sum to the caller.
|
|
|
|
*/
|
|
|
|
|
2021-07-21 09:27:36 +02:00
|
|
|
*ppv = (FAR void *)(uintptr_t)
|
2023-10-09 12:28:27 +02:00
|
|
|
(base + priv->firstblock * priv->geo.blocksize);
|
2013-04-30 20:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTDIOC_BULKERASE:
|
|
|
|
{
|
|
|
|
/* Erase the entire partition */
|
|
|
|
|
2013-10-31 17:40:10 +01:00
|
|
|
ret = priv->parent->erase(priv->parent,
|
|
|
|
priv->firstblock / priv->blkpererase,
|
2023-10-09 12:28:27 +02:00
|
|
|
priv->geo.neraseblocks);
|
2013-04-30 20:37:34 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-24 12:04:57 +02:00
|
|
|
case MTDIOC_ERASESECTORS:
|
|
|
|
{
|
|
|
|
/* Erase sectors as defined in mtd_erase_s structure */
|
|
|
|
|
|
|
|
FAR struct mtd_erase_s *erase = (FAR struct mtd_erase_s *)arg;
|
|
|
|
|
|
|
|
ret = priv->parent->erase(priv->parent,
|
|
|
|
priv->firstblock / priv->blkpererase +
|
|
|
|
erase->startblock,
|
|
|
|
erase->nblocks);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
default:
|
2013-05-01 01:02:34 +02:00
|
|
|
{
|
|
|
|
/* Pass any unhandled ioctl() calls to the underlying driver */
|
|
|
|
|
|
|
|
ret = priv->parent->ioctl(priv->parent, cmd, arg);
|
|
|
|
}
|
2013-04-30 20:37:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:10:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_isbad
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Check bad block for the specified block number.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_isbad(FAR struct mtd_dev_s *dev, off_t block)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Does the underlying MTD device support the isbad method? */
|
|
|
|
|
|
|
|
if (priv->parent->isbad)
|
|
|
|
{
|
|
|
|
return priv->parent->isbad(priv->parent, block +
|
|
|
|
priv->firstblock / priv->blkpererase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The underlying MTD driver does not support the isbad() method */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_markbad
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Mark bad block for the specified block number.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_markbad(FAR struct mtd_dev_s *dev, off_t block)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv);
|
|
|
|
|
|
|
|
/* Does the underlying MTD device support the markbad method? */
|
|
|
|
|
|
|
|
if (priv->parent->markbad)
|
|
|
|
{
|
|
|
|
return priv->parent->markbad(priv->parent, block +
|
|
|
|
priv->firstblock / priv->blkpererase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The underlying MTD driver does not support the markbad() method */
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_procfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_procfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct part_procfs_file_s *attr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* PROCFS is read-only. Any attempt to open with any kind of write
|
|
|
|
* access is not permitted.
|
|
|
|
*
|
|
|
|
* REVISIT: Write-able proc files could be quite useful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
2013-12-12 16:21:55 +01:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a container to hold the task and attribute selection */
|
|
|
|
|
2020-11-11 08:09:08 +01:00
|
|
|
attr = (FAR struct part_procfs_file_s *)
|
|
|
|
kmm_zalloc(sizeof(struct part_procfs_file_s));
|
2013-12-12 16:21:55 +01:00
|
|
|
if (!attr)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2013-12-12 16:21:55 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the file attributes */
|
|
|
|
|
|
|
|
attr->nextpart = g_pfirstpartition;
|
|
|
|
|
|
|
|
/* Save the index as the open-specific state in filep->f_priv */
|
|
|
|
|
|
|
|
filep->f_priv = (FAR void *)attr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_procfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_procfs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct part_procfs_file_s *attr;
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
attr = (FAR struct part_procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(attr);
|
|
|
|
|
|
|
|
/* Release the file attributes structure */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(attr);
|
2013-12-12 16:21:55 +01:00
|
|
|
filep->f_priv = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_procfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t part_procfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct part_procfs_file_s *attr;
|
|
|
|
FAR struct mtd_geometry_s geo;
|
2013-12-12 18:08:20 +01:00
|
|
|
ssize_t total = 0;
|
|
|
|
ssize_t blkpererase;
|
|
|
|
ssize_t ret;
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
2018-11-09 15:24:17 +01:00
|
|
|
char partname[PART_NAME_MAX + 1];
|
2013-12-12 16:21:55 +01:00
|
|
|
FAR const char *ptr;
|
|
|
|
uint8_t x;
|
|
|
|
#endif
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
attr = (FAR struct part_procfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(attr);
|
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
/* If we are at the end of the list, then return 0 signifying the
|
|
|
|
* end-of-file. This also handles the special case when there are
|
|
|
|
* no registered MTD partitions.
|
|
|
|
*/
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
if (attr->nextpart)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
2013-12-12 18:08:20 +01:00
|
|
|
/* Output a header before the first entry */
|
|
|
|
|
|
|
|
if (attr->nextpart == g_pfirstpartition)
|
|
|
|
{
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
2020-11-11 08:09:08 +01:00
|
|
|
total = snprintf(buffer, buflen, "%-*s Start Size MTD\n",
|
|
|
|
PART_NAME_MAX, "Name");
|
2013-12-12 16:21:55 +01:00
|
|
|
#else
|
2018-11-08 16:46:11 +01:00
|
|
|
total = snprintf(buffer, buflen, " Start Size MTD\n");
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
2013-12-12 18:08:20 +01:00
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
/* Provide the requested data */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Get the geometry of the FLASH device */
|
|
|
|
|
|
|
|
ret = attr->nextpart->parent->ioctl(attr->nextpart->parent,
|
|
|
|
MTDIOC_GEOMETRY, (unsigned long)((uintptr_t)&geo));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-08-09 22:48:00 +02:00
|
|
|
ferr("ERROR: mtd->ioctl failed: %zd\n", ret);
|
2013-12-12 18:08:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2020-11-11 08:09:08 +01:00
|
|
|
/* Get the number of blocks per erase. There must be an even
|
|
|
|
* number of blocks in one erase blocks.
|
2013-12-12 18:08:20 +01:00
|
|
|
*/
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
blkpererase = geo.erasesize / geo.blocksize;
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
/* Copy data from the next known partition */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
2018-08-28 15:06:37 +02:00
|
|
|
ptr = attr->nextpart->name;
|
|
|
|
for (x = 0; x < sizeof(partname) - 1; x++)
|
2013-12-12 18:08:20 +01:00
|
|
|
{
|
2018-08-28 15:06:37 +02:00
|
|
|
/* Test for end of partition name */
|
2013-12-12 18:08:20 +01:00
|
|
|
|
2018-08-28 15:06:37 +02:00
|
|
|
if (*ptr == ',' || *ptr == '\0')
|
|
|
|
{
|
|
|
|
/* Perform space fill for alignment */
|
2013-12-12 18:08:20 +01:00
|
|
|
|
2018-08-28 15:06:37 +02:00
|
|
|
partname[x] = ' ';
|
2013-12-12 18:08:20 +01:00
|
|
|
}
|
2018-08-28 15:06:37 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Copy next byte of partition name */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2018-08-28 15:06:37 +02:00
|
|
|
partname[x] = *ptr++;
|
|
|
|
}
|
2013-12-12 18:08:20 +01:00
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2018-08-28 15:06:37 +02:00
|
|
|
partname[x] = '\0';
|
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
/* Terminate the partition name and add to output buffer */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
2021-08-27 18:37:37 +02:00
|
|
|
ret = snprintf(&buffer[total], buflen - total,
|
2021-08-27 19:30:30 +02:00
|
|
|
"%s%7ju %7ju %s\n",
|
|
|
|
partname,
|
|
|
|
(uintmax_t)attr->nextpart->firstblock / blkpererase,
|
2023-10-09 12:28:27 +02:00
|
|
|
(uintmax_t)attr->nextpart->geo.neraseblocks,
|
2021-08-27 19:30:30 +02:00
|
|
|
attr->nextpart->parent->name);
|
2013-12-12 16:21:55 +01:00
|
|
|
#else
|
2020-11-11 08:11:39 +01:00
|
|
|
ret = snprintf(&buffer[total], buflen - total, "%7ju %7ju %s\n",
|
2021-08-27 19:30:30 +02:00
|
|
|
(uintmax_t)attr->nextpart->firstblock / blkpererase,
|
2023-10-09 12:28:27 +02:00
|
|
|
(uintmax_t)attr->nextpart->geo.neraseblocks,
|
2021-08-27 19:30:30 +02:00
|
|
|
attr->nextpart->parent->name);
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
|
|
|
|
2013-12-12 18:08:20 +01:00
|
|
|
if (ret + total < buflen)
|
|
|
|
{
|
|
|
|
/* It fit in the buffer totally. Advance total and move to
|
|
|
|
* next partition.
|
|
|
|
*/
|
|
|
|
|
|
|
|
total += ret;
|
|
|
|
attr->nextpart = attr->nextpart->pnext;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This one didn't fit completely. Truncate the partial
|
|
|
|
* entry and break the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
buffer[total] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (attr->nextpart);
|
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* Update the file offset */
|
|
|
|
|
|
|
|
if (total > 0)
|
|
|
|
{
|
|
|
|
filep->f_pos += total;
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_procfs_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-11 08:09:08 +01:00
|
|
|
static int part_procfs_dup(FAR const struct file *oldp,
|
|
|
|
FAR struct file *newp)
|
2013-12-12 16:21:55 +01:00
|
|
|
{
|
|
|
|
FAR struct part_procfs_file_s *oldattr;
|
|
|
|
FAR struct part_procfs_file_s *newattr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* Recover our private data from the old struct file instance */
|
|
|
|
|
|
|
|
oldattr = (FAR struct part_procfs_file_s *)oldp->f_priv;
|
|
|
|
DEBUGASSERT(oldattr);
|
|
|
|
|
|
|
|
/* Allocate a new container to hold the task and attribute selection */
|
|
|
|
|
2020-11-11 08:09:08 +01:00
|
|
|
newattr = (FAR struct part_procfs_file_s *)
|
|
|
|
kmm_zalloc(sizeof(struct part_procfs_file_s));
|
2013-12-12 16:21:55 +01:00
|
|
|
if (!newattr)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to allocate file attributes\n");
|
2013-12-12 16:21:55 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* The copy the file attribute from the old attributes to the new */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
memcpy(newattr, oldattr, sizeof(struct part_procfs_file_s));
|
|
|
|
|
|
|
|
/* Save the new attributes in the new file structure */
|
|
|
|
|
|
|
|
newp->f_priv = (FAR void *)newattr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: part_procfs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int part_procfs_stat(const char *relpath, struct stat *buf)
|
|
|
|
{
|
|
|
|
/* File/directory size, access block size */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
|
2013-12-12 16:21:55 +01:00
|
|
|
buf->st_size = 0;
|
|
|
|
buf->st_blksize = 0;
|
|
|
|
buf->st_blocks = 0;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtd_partition
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Give an instance of an MTD driver, create a flash partition, ie.,
|
|
|
|
* another MTD driver instance that only operates with a sub-region of
|
2014-04-23 21:02:00 +02:00
|
|
|
* FLASH media. That sub-region is defined by a sector offset and a
|
2013-04-30 20:37:34 +02:00
|
|
|
* sector count (where the size of a sector is provided the by parent MTD
|
|
|
|
* driver).
|
|
|
|
*
|
2013-05-03 20:52:33 +02:00
|
|
|
* NOTE: Since there may be a number of MTD partition drivers operating on
|
|
|
|
* the same, underlying FLASH driver, that FLASH driver must be capable
|
|
|
|
* of enforcing mutually exclusive access to the FLASH device. Without
|
|
|
|
* partitions, that mutual exclusion would be provided by the file system
|
|
|
|
* above the FLASH driver.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-04-23 21:02:00 +02:00
|
|
|
* mtd - The MTD device to be partitioned
|
|
|
|
* firstblock - The offset in bytes to the first block
|
|
|
|
* nblocks - The number of blocks in the partition
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, another MTD device representing the partition is returned.
|
|
|
|
* A NULL value is returned on a failure.
|
|
|
|
*
|
2013-04-30 20:37:34 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-11 08:09:08 +01:00
|
|
|
FAR struct mtd_dev_s *mtd_partition(FAR struct mtd_dev_s *mtd,
|
|
|
|
off_t firstblock,
|
2013-04-30 20:37:34 +02:00
|
|
|
off_t nblocks)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *part;
|
|
|
|
off_t erasestart;
|
|
|
|
off_t eraseend;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(mtd);
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
/* Allocate a partition device structure */
|
|
|
|
|
|
|
|
part = (FAR struct mtd_partition_s *)
|
|
|
|
kmm_zalloc(sizeof(struct mtd_partition_s));
|
|
|
|
if (!part)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate memory for the partition device\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/* Get the geometry of the FLASH device */
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
ret = mtd->ioctl(mtd, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)((uintptr_t)&part->geo));
|
2013-04-30 20:37:34 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: mtd->ioctl failed: %d\n", ret);
|
2023-10-09 12:28:27 +02:00
|
|
|
kmm_free(part);
|
2013-04-30 20:37:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the number of blocks per erase. There must be an even number of
|
|
|
|
* blocks in one erase blocks.
|
|
|
|
*/
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
part->blkpererase = part->geo.erasesize / part->geo.blocksize;
|
|
|
|
DEBUGASSERT(part->blkpererase * part->geo.blocksize ==
|
|
|
|
part->geo.erasesize);
|
2013-04-30 20:37:34 +02:00
|
|
|
|
|
|
|
/* Adjust the offset and size if necessary so that they are multiples of
|
|
|
|
* the erase block size (making sure that we do not go outside of the
|
2014-04-23 21:02:00 +02:00
|
|
|
* requested sub-region). NOTE that 'eraseend' is the first erase block
|
2013-04-30 20:37:34 +02:00
|
|
|
* beyond the sub-region.
|
|
|
|
*/
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
erasestart = (firstblock + part->blkpererase - 1) / part->blkpererase;
|
|
|
|
eraseend = (firstblock + nblocks) / part->blkpererase;
|
2013-04-30 20:37:34 +02:00
|
|
|
|
|
|
|
if (erasestart >= eraseend)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: sub-region too small\n");
|
2023-10-09 12:28:27 +02:00
|
|
|
kmm_free(part);
|
2013-04-30 20:37:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-31 17:40:10 +01:00
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/* Verify that the sub-region is valid for this geometry */
|
|
|
|
|
2023-10-09 12:28:27 +02:00
|
|
|
if (eraseend > part->geo.neraseblocks)
|
2013-04-30 20:37:34 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: sub-region too big\n");
|
2023-10-09 12:28:27 +02:00
|
|
|
kmm_free(part);
|
2013-10-31 17:40:10 +01:00
|
|
|
return NULL;
|
2013-04-30 20:37:34 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 18:59:57 +02:00
|
|
|
/* Initialize the partition device structure. (unsupported methods were
|
2014-09-01 01:34:44 +02:00
|
|
|
* nullified by kmm_zalloc).
|
2013-05-01 18:59:57 +02:00
|
|
|
*/
|
2013-04-30 20:37:34 +02:00
|
|
|
|
2023-03-29 12:10:56 +02:00
|
|
|
part->child.erase = part_erase;
|
|
|
|
part->child.bread = part_bread;
|
|
|
|
part->child.bwrite = part_bwrite;
|
|
|
|
part->child.read = mtd->read ? part_read : NULL;
|
|
|
|
part->child.ioctl = part_ioctl;
|
|
|
|
part->child.isbad = part_isbad;
|
|
|
|
part->child.markbad = part_markbad;
|
2013-05-03 20:52:33 +02:00
|
|
|
#ifdef CONFIG_MTD_BYTE_WRITE
|
2023-03-29 12:10:56 +02:00
|
|
|
part->child.write = mtd->write ? part_write : NULL;
|
2013-05-03 20:52:33 +02:00
|
|
|
#endif
|
2023-09-12 05:54:40 +02:00
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
|
|
|
part->child.name = part->name;
|
|
|
|
#else
|
2023-03-29 12:10:56 +02:00
|
|
|
part->child.name = "part";
|
2023-09-12 05:54:40 +02:00
|
|
|
#endif
|
2023-03-29 12:10:56 +02:00
|
|
|
part->parent = mtd;
|
2023-10-09 12:28:27 +02:00
|
|
|
part->firstblock = erasestart * part->blkpererase;
|
|
|
|
part->geo.neraseblocks = eraseend - erasestart;
|
2013-04-30 20:37:34 +02:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
2023-02-08 04:29:09 +01:00
|
|
|
strlcpy(part->name, "(noname)", sizeof(part->name));
|
2013-12-12 16:21:55 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_PROCFS_EXCLUDE_PARTITIONS)
|
2014-04-23 21:02:00 +02:00
|
|
|
/* Add this partition to the list of known partitions */
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
if (g_pfirstpartition == NULL)
|
|
|
|
{
|
|
|
|
g_pfirstpartition = part;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct mtd_partition_s *plast;
|
|
|
|
|
|
|
|
/* Add the partition to the end of the list */
|
2020-11-11 08:09:08 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
part->pnext = NULL;
|
|
|
|
|
|
|
|
plast = g_pfirstpartition;
|
|
|
|
while (plast->pnext != NULL)
|
|
|
|
{
|
|
|
|
/* Get pointer to next partition */
|
2020-11-11 08:09:08 +01:00
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
plast = plast->pnext;
|
|
|
|
}
|
|
|
|
|
|
|
|
plast->pnext = part;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-30 20:37:34 +02:00
|
|
|
/* Return the implementation-specific state structure as the MTD device */
|
|
|
|
|
|
|
|
return &part->child;
|
|
|
|
}
|
|
|
|
|
2013-12-12 16:21:55 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mtd_setpartitionname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Sets the name of the specified partition.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-11-29 22:03:51 +01:00
|
|
|
#ifdef CONFIG_MTD_PARTITION_NAMES
|
2013-12-12 16:21:55 +01:00
|
|
|
int mtd_setpartitionname(FAR struct mtd_dev_s *mtd, FAR const char *name)
|
|
|
|
{
|
|
|
|
FAR struct mtd_partition_s *priv = (FAR struct mtd_partition_s *)mtd;
|
|
|
|
|
2018-08-28 15:06:37 +02:00
|
|
|
if (priv == NULL || name == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-12-12 16:21:55 +01:00
|
|
|
|
|
|
|
/* Allocate space for the name */
|
2018-08-28 15:06:37 +02:00
|
|
|
|
2022-03-18 15:55:50 +01:00
|
|
|
strlcpy(priv->name, name, sizeof(priv->name));
|
2013-12-12 16:21:55 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|