]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
disk: support MTD partitions
authorAlexey Romanov <avromanov@salutedevices.com>
Thu, 18 Jul 2024 05:46:04 +0000 (08:46 +0300)
committerMichael Trimarchi <michael@amarulasolutions.com>
Thu, 8 Aug 2024 07:27:45 +0000 (09:27 +0200)
Add new MTD partition driver, which can be useful with
mtdblock driver combination.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
disk/part.c
drivers/mtd/Kconfig
drivers/mtd/mtdpart.c
include/part.h

index bc932526f906af54e348c1ef31a80b33ca5db105..86f669926e1516d8c7ef3fe009cf0990ba26beec 100644 (file)
@@ -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)
        printf("\nPartition Map for %s device %d  --   Partition Type: %s\n\n",
               uclass_get_name(desc->uclass_id), desc->devnum, type);
 #endif /* any CONFIG_..._PARTITION */
index 4fdc9645d084ac529b99522f23bb80ed64cea08f..e4e29e0a3c39c6a734e5331bd025c26a1430e75a 100644 (file)
@@ -2,6 +2,7 @@ menu "MTD Support"
 
 config MTD_PARTITIONS
        bool
+       select PARTITIONS
 
 config MTD
        bool "Enable MTD layer"
index be1d19b4ffa5982efc2da1631eddb493b7fe5efe..88094b81e7a2e27ce18a3d185ba63b708c2deb45 100644 (file)
@@ -20,6 +20,8 @@
 #endif
 
 #include <malloc.h>
+#include <memalign.h>
+#include <part.h>
 #include <linux/bug.h>
 #include <linux/errno.h>
 #include <linux/compat.h>
@@ -1054,3 +1056,77 @@ 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 __maybe_unused 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) {
+               debug("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 __maybe_unused part_print_mtd(struct blk_desc *dev_desc)
+{
+       struct mtd_info *master = blk_desc_to_mtd(dev_desc);
+       struct mtd_info *part;
+
+       if (!master)
+               return;
+
+       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)
+{
+       struct mtd_info *master = blk_desc_to_mtd(dev_desc);
+       ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
+
+       if (!master)
+               return -1;
+
+       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,
+};
index b187ec4b4bd504f6b4f51595fde154284504685b..a5994f2cace2c3bc95ecbbebbe60051162007e04 100644 (file)
@@ -30,12 +30,15 @@ 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
  */