diff mbox series

[v2,1/3] disk: support MTD partitions

Message ID 20240307130726.1582487-2-avromanov@salutedevices.com
State Superseded
Delegated to: Dario Binacchi
Headers show
Series Introduce mtdblock device | expand

Commit Message

Alexey Romanov March 7, 2024, 1:07 p.m. UTC
Add new MTD partition driver, which can be useful with
mtdblock driver combination.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
---
 disk/part.c           |  5 +++-
 drivers/mtd/Kconfig   |  1 +
 drivers/mtd/mtdpart.c | 69 +++++++++++++++++++++++++++++++++++++++++++
 include/part.h        |  2 ++
 4 files changed, 76 insertions(+), 1 deletion(-)

Comments

Heinrich Schuchardt April 3, 2024, 12:23 p.m. UTC | #1
On 07.03.24 14:07, Alexey Romanov wrote:
> Add new MTD partition driver, which can be useful with
> mtdblock driver combination.
>
> Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
> ---
>   disk/part.c           |  5 +++-
>   drivers/mtd/Kconfig   |  1 +
>   drivers/mtd/mtdpart.c | 69 +++++++++++++++++++++++++++++++++++++++++++
>   include/part.h        |  2 ++
>   4 files changed, 76 insertions(+), 1 deletion(-)
>
> diff --git a/disk/part.c b/disk/part.c
> index 36b88205ec..0fc5cc0419 100644
> --- a/disk/part.c
> +++ b/disk/part.c
> @@ -304,7 +304,8 @@ static void print_part_header(const char *type, struct blk_desc *desc)
>   	CONFIG_IS_ENABLED(DOS_PARTITION) || \
>   	CONFIG_IS_ENABLED(ISO_PARTITION) || \
>   	CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
> -	CONFIG_IS_ENABLED(EFI_PARTITION)
> +	CONFIG_IS_ENABLED(EFI_PARTITION) || \
> +	CONFIG_IS_ENABLED(MTD_PARTITIONS)
>   	puts ("\nPartition Map for ");
>   	switch (desc->uclass_id) {
>   	case UCLASS_IDE:
> @@ -343,6 +344,8 @@ static void print_part_header(const char *type, struct blk_desc *desc)
>   	case UCLASS_BLKMAP:
>   		puts("BLKMAP");
>   		break;
> +	case UCLASS_MTD:
> +		puts("MTD");
>   	default:

The function should simply call uclass_get_name() instead of duplicating it:

[PATCH 1/1] disk: simplify print_part_header()
https://lore.kernel.org/u-boot/20240403114047.84030-1-heinrich.schuchardt@canonical.com/T/#u

>   		printf("UNKNOWN(%d)", desc->uclass_id);
>   		break;
> diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
> index 1902351719..40272f7e50 100644
> --- a/drivers/mtd/Kconfig
> +++ b/drivers/mtd/Kconfig
> @@ -2,6 +2,7 @@ menu "MTD Support"
>
>   config MTD_PARTITIONS
>   	bool
> +	select PARTITIONS
>
>   config MTD
>   	bool "Enable MTD layer"
> diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> index 4886392a1c..67244869f2 100644
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
> @@ -21,6 +21,8 @@
>
>   #include <common.h>
>   #include <malloc.h>
> +#include <memalign.h>
> +#include <part.h>
>   #include <linux/bug.h>
>   #include <linux/errno.h>
>   #include <linux/compat.h>
> @@ -1055,3 +1057,70 @@ uint64_t mtd_get_device_size(const struct mtd_info *mtd)
>   	return mtd->size;
>   }
>   EXPORT_SYMBOL_GPL(mtd_get_device_size);
> +
> +static struct mtd_info *mtd_get_partition_by_index(struct mtd_info *mtd, int index)
> +{
> +	struct mtd_info *part;
> +	int i = 0;
> +
> +	list_for_each_entry(part, &mtd->partitions, node)
> +		if (i++ == index)
> +			return part;
> +
> +	debug("Partition with idx=%d not found on MTD device %s\n", index, mtd->name);
> +	return NULL;
> +}
> +
> +static int part_get_info_mtd(struct blk_desc *dev_desc, int part_idx,
> +			     struct disk_partition *info)
> +{
> +	struct mtd_info *master = blk_desc_to_mtd(dev_desc);
> +	struct mtd_info *part;
> +
> +	if (!master) {
> +		pr_err("MTD device is NULL\n");
> +		return -EINVAL;
> +	}
> +
> +	part = mtd_get_partition_by_index(master, part_idx);
> +	if (!part) {
> +		debug("Failed to find partition with idx=%d\n", part_idx);
> +		return -EINVAL;
> +	}
> +
> +	snprintf(info->name, PART_NAME_LEN, part->name);
> +	info->start = part->offset / dev_desc->blksz;
> +	info->size = part->size / dev_desc->blksz;
> +	info->blksz = dev_desc->blksz;
> +
> +	return 0;
> +}
> +
> +static void part_print_mtd(struct blk_desc *dev_desc)
> +{
> +	struct mtd_info *master = blk_desc_to_mtd(dev_desc);
> +	struct mtd_info *part;
> +
> +	list_for_each_entry(part, &master->partitions, node)
> +		printf("- 0x%012llx-0x%012llx : \"%s\"\n",
> +		       part->offset, part->offset + part->size, part->name);
> +}
> +
> +static int part_test_mtd(struct blk_desc *dev_desc)
> +{
> +	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
> +
> +	if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
> +		return -1;
> +
> +	return 0;
> +}
> +
> +U_BOOT_PART_TYPE(mtd) = {
> +	.name		= "MTD",
> +	.part_type	= PART_TYPE_MTD,
> +	.max_entries	= MTD_ENTRY_NUMBERS,
> +	.get_info	= part_get_info_ptr(part_get_info_mtd),
> +	.print		= part_print_ptr(part_print_mtd),
> +	.test		= part_test_mtd,
> +};
> diff --git a/include/part.h b/include/part.h
> index db34bc6bb7..f7f3773a95 100644
> --- a/include/part.h
> +++ b/include/part.h
> @@ -30,12 +30,14 @@ struct block_drvr {
>   #define PART_TYPE_ISO		0x03
>   #define PART_TYPE_AMIGA		0x04
>   #define PART_TYPE_EFI		0x05
> +#define PART_TYPE_MTD		0x06
>
>   /* maximum number of partition entries supported by search */
>   #define DOS_ENTRY_NUMBERS	8
>   #define ISO_ENTRY_NUMBERS	64
>   #define MAC_ENTRY_NUMBERS	64
>   #define AMIGA_ENTRY_NUMBERS	8
> +#define MTD_ENTRY_NUMBERS	64

Please, add a blank line here.

Best regards

Heinrich

>   /*
>    * Type string for U-Boot bootable partitions
>    */
diff mbox series

Patch

diff --git a/disk/part.c b/disk/part.c
index 36b88205ec..0fc5cc0419 100644
--- a/disk/part.c
+++ b/disk/part.c
@@ -304,7 +304,8 @@  static void print_part_header(const char *type, struct blk_desc *desc)
 	CONFIG_IS_ENABLED(DOS_PARTITION) || \
 	CONFIG_IS_ENABLED(ISO_PARTITION) || \
 	CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
-	CONFIG_IS_ENABLED(EFI_PARTITION)
+	CONFIG_IS_ENABLED(EFI_PARTITION) || \
+	CONFIG_IS_ENABLED(MTD_PARTITIONS)
 	puts ("\nPartition Map for ");
 	switch (desc->uclass_id) {
 	case UCLASS_IDE:
@@ -343,6 +344,8 @@  static void print_part_header(const char *type, struct blk_desc *desc)
 	case UCLASS_BLKMAP:
 		puts("BLKMAP");
 		break;
+	case UCLASS_MTD:
+		puts("MTD");
 	default:
 		printf("UNKNOWN(%d)", desc->uclass_id);
 		break;
diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
index 1902351719..40272f7e50 100644
--- a/drivers/mtd/Kconfig
+++ b/drivers/mtd/Kconfig
@@ -2,6 +2,7 @@  menu "MTD Support"
 
 config MTD_PARTITIONS
 	bool
+	select PARTITIONS
 
 config MTD
 	bool "Enable MTD layer"
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index 4886392a1c..67244869f2 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -21,6 +21,8 @@ 
 
 #include <common.h>
 #include <malloc.h>
+#include <memalign.h>
+#include <part.h>
 #include <linux/bug.h>
 #include <linux/errno.h>
 #include <linux/compat.h>
@@ -1055,3 +1057,70 @@  uint64_t mtd_get_device_size(const struct mtd_info *mtd)
 	return mtd->size;
 }
 EXPORT_SYMBOL_GPL(mtd_get_device_size);
+
+static struct mtd_info *mtd_get_partition_by_index(struct mtd_info *mtd, int index)
+{
+	struct mtd_info *part;
+	int i = 0;
+
+	list_for_each_entry(part, &mtd->partitions, node)
+		if (i++ == index)
+			return part;
+
+	debug("Partition with idx=%d not found on MTD device %s\n", index, mtd->name);
+	return NULL;
+}
+
+static int part_get_info_mtd(struct blk_desc *dev_desc, int part_idx,
+			     struct disk_partition *info)
+{
+	struct mtd_info *master = blk_desc_to_mtd(dev_desc);
+	struct mtd_info *part;
+
+	if (!master) {
+		pr_err("MTD device is NULL\n");
+		return -EINVAL;
+	}
+
+	part = mtd_get_partition_by_index(master, part_idx);
+	if (!part) {
+		debug("Failed to find partition with idx=%d\n", part_idx);
+		return -EINVAL;
+	}
+
+	snprintf(info->name, PART_NAME_LEN, part->name);
+	info->start = part->offset / dev_desc->blksz;
+	info->size = part->size / dev_desc->blksz;
+	info->blksz = dev_desc->blksz;
+
+	return 0;
+}
+
+static void part_print_mtd(struct blk_desc *dev_desc)
+{
+	struct mtd_info *master = blk_desc_to_mtd(dev_desc);
+	struct mtd_info *part;
+
+	list_for_each_entry(part, &master->partitions, node)
+		printf("- 0x%012llx-0x%012llx : \"%s\"\n",
+		       part->offset, part->offset + part->size, part->name);
+}
+
+static int part_test_mtd(struct blk_desc *dev_desc)
+{
+	ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
+
+	if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
+		return -1;
+
+	return 0;
+}
+
+U_BOOT_PART_TYPE(mtd) = {
+	.name		= "MTD",
+	.part_type	= PART_TYPE_MTD,
+	.max_entries	= MTD_ENTRY_NUMBERS,
+	.get_info	= part_get_info_ptr(part_get_info_mtd),
+	.print		= part_print_ptr(part_print_mtd),
+	.test		= part_test_mtd,
+};
diff --git a/include/part.h b/include/part.h
index db34bc6bb7..f7f3773a95 100644
--- a/include/part.h
+++ b/include/part.h
@@ -30,12 +30,14 @@  struct block_drvr {
 #define PART_TYPE_ISO		0x03
 #define PART_TYPE_AMIGA		0x04
 #define PART_TYPE_EFI		0x05
+#define PART_TYPE_MTD		0x06
 
 /* maximum number of partition entries supported by search */
 #define DOS_ENTRY_NUMBERS	8
 #define ISO_ENTRY_NUMBERS	64
 #define MAC_ENTRY_NUMBERS	64
 #define AMIGA_ENTRY_NUMBERS	8
+#define MTD_ENTRY_NUMBERS	64
 /*
  * Type string for U-Boot bootable partitions
  */