+From 69357074558daf6ff24c9f58714935e9e095a865 Mon Sep 17 00:00:00 2001
+From: OpenWrt community <openwrt-devel@lists.openwrt.org>
+Date: Wed, 13 Jul 2022 13:37:33 +0200
+Subject: [PATCH] kernel: add block fit partition parser
+
+---
+ block/blk.h | 2 ++
+ block/partitions/Kconfig | 7 +++++++
+ block/partitions/Makefile | 1 +
+ block/partitions/check.h | 3 +++
+ block/partitions/core.c | 17 +++++++++++++++++
+ block/partitions/efi.c | 8 ++++++++
+ block/partitions/efi.h | 3 +++
+ block/partitions/msdos.c | 10 ++++++++++
+ drivers/mtd/mtd_blkdevs.c | 2 ++
+ drivers/mtd/ubi/block.c | 3 +++
+ include/linux/msdos_partition.h | 1 +
+ 11 files changed, 57 insertions(+)
+
--- a/block/blk.h
+++ b/block/blk.h
-@@ -361,6 +361,8 @@ char *disk_name(struct gendisk *hd, int
+@@ -354,6 +354,8 @@ void blk_free_ext_minor(unsigned int min
#define ADDPART_FLAG_NONE 0
#define ADDPART_FLAG_RAID 1
#define ADDPART_FLAG_WHOLEDISK 2
+int parse_fit_partitions(struct parsed_partitions *state, u64 start_sector, u64 nr_sectors, int *slot, int add_remain);
--- a/block/partitions/core.c
+++ b/block/partitions/core.c
-@@ -10,6 +10,10 @@
+@@ -12,6 +12,10 @@
#include <linux/vmalloc.h>
#include <linux/blktrace_api.h>
#include <linux/raid/detect.h>
#include "check.h"
static int (*check_part[])(struct parsed_partitions *) = {
-@@ -46,6 +50,9 @@ static int (*check_part[])(struct parsed
+@@ -48,6 +52,9 @@ static int (*check_part[])(struct parsed
#ifdef CONFIG_EFI_PARTITION
efi_partition, /* this must come before msdos */
#endif
#ifdef CONFIG_SGI_PARTITION
sgi_partition,
#endif
-@@ -694,6 +701,14 @@ static bool blk_add_partition(struct gen
+@@ -408,6 +415,11 @@ static struct block_device *add_partitio
+ goto out_del;
+ }
+
++#ifdef CONFIG_FIT_PARTITION
++ if (flags & ADDPART_FLAG_READONLY)
++ bdev->bd_read_only = true;
++#endif
++
+ /* everything is up and running, commence */
+ err = xa_insert(&disk->part_tbl, partno, bdev, GFP_KERNEL);
+ if (err)
+@@ -598,6 +610,11 @@ static bool blk_add_partition(struct gen
(state->parts[p].flags & ADDPART_FLAG_RAID))
- md_autodetect_dev(part_to_dev(part)->devt);
+ md_autodetect_dev(part->bd_dev);
+#ifdef CONFIG_FIT_PARTITION
+ if ((state->parts[p].flags & ADDPART_FLAG_ROOTDEV) && ROOT_DEV == 0)
-+ ROOT_DEV = part_to_dev(part)->devt;
-+
-+ if (state->parts[p].flags & ADDPART_FLAG_READONLY)
-+ part->policy = true;
++ ROOT_DEV = part->bd_dev;
+#endif
+
return true;
}
---- a/drivers/mtd/ubi/block.c
-+++ b/drivers/mtd/ubi/block.c
-@@ -396,7 +396,11 @@ int ubiblock_create(struct ubi_volume_in
-
- gd->fops = &ubiblock_ops;
- gd->major = ubiblock_major;
-+#ifdef CONFIG_FIT_PARTITION
-+ gd->minors = 0;
-+#else
- gd->minors = 1;
-+#endif
- gd->first_minor = idr_alloc(&ubiblock_minor_idr, dev, 0, 0, GFP_KERNEL);
- if (gd->first_minor < 0) {
- dev_err(disk_to_dev(gd),
-@@ -413,6 +417,9 @@ int ubiblock_create(struct ubi_volume_in
- goto out_put_disk;
- }
- gd->private_data = dev;
-+#ifdef CONFIG_FIT_PARTITION
-+ gd->flags |= GENHD_FL_EXT_DEVT;
-+#endif
- sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
- set_capacity(gd, disk_capacity);
- dev->gd = gd;
--- a/block/partitions/efi.c
+++ b/block/partitions/efi.c
-@@ -706,6 +706,9 @@ int efi_partition(struct parsed_partitio
+@@ -716,6 +716,9 @@ int efi_partition(struct parsed_partitio
gpt_entry *ptes = NULL;
u32 i;
unsigned ssz = queue_logical_block_size(state->disk->queue) / 512;
if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
kfree(gpt);
-@@ -739,6 +742,11 @@ int efi_partition(struct parsed_partitio
+@@ -749,6 +752,11 @@ int efi_partition(struct parsed_partitio
ARRAY_SIZE(ptes[i].partition_name));
utf16_le_to_7bit(ptes[i].partition_name, label_max, info->volname);
state->parts[i + 1].has_info = true;
typedef struct _gpt_header {
__le64 signature;
---- a/drivers/mtd/mtdblock.c
-+++ b/drivers/mtd/mtdblock.c
-@@ -334,7 +334,11 @@ static void mtdblock_remove_dev(struct m
- static struct mtd_blktrans_ops mtdblock_tr = {
- .name = "mtdblock",
- .major = MTD_BLOCK_MAJOR,
-+#ifdef CONFIG_FIT_PARTITION
-+ .part_bits = 2,
-+#else
- .part_bits = 0,
-+#endif
- .blksize = 512,
- .open = mtdblock_open,
- .flush = mtdblock_flush,
---- a/drivers/mtd/mtd_blkdevs.c
-+++ b/drivers/mtd/mtd_blkdevs.c
-@@ -407,18 +407,8 @@ int add_mtd_blktrans_dev(struct mtd_blkt
- gd->first_minor = (new->devnum) << tr->part_bits;
- gd->fops = &mtd_block_ops;
-
-- if (tr->part_bits)
-- if (new->devnum < 26)
-- snprintf(gd->disk_name, sizeof(gd->disk_name),
-- "%s%c", tr->name, 'a' + new->devnum);
-- else
-- snprintf(gd->disk_name, sizeof(gd->disk_name),
-- "%s%c%c", tr->name,
-- 'a' - 1 + new->devnum / 26,
-- 'a' + new->devnum % 26);
-- else
-- snprintf(gd->disk_name, sizeof(gd->disk_name),
-- "%s%d", tr->name, new->devnum);
-+ snprintf(gd->disk_name, sizeof(gd->disk_name),
-+ "%s%d", tr->name, new->devnum);
-
- set_capacity(gd, ((u64)new->size * tr->blksize) >> 9);
-
--- a/block/partitions/msdos.c
+++ b/block/partitions/msdos.c
-@@ -563,6 +563,15 @@ static void parse_minix(struct parsed_pa
+@@ -564,6 +564,15 @@ static void parse_minix(struct parsed_pa
#endif /* CONFIG_MINIX_SUBPARTITION */
}
static struct {
unsigned char id;
void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
-@@ -574,6 +583,7 @@ static struct {
+@@ -575,6 +584,7 @@ static struct {
{UNIXWARE_PARTITION, parse_unixware},
{SOLARIS_X86_PARTITION, parse_solaris_x86},
{NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
{0, NULL},
};
+--- a/drivers/mtd/mtd_blkdevs.c
++++ b/drivers/mtd/mtd_blkdevs.c
+@@ -345,6 +345,8 @@ int add_mtd_blktrans_dev(struct mtd_blkt
+ gd->first_minor = (new->devnum) << tr->part_bits;
+ gd->minors = 1 << tr->part_bits;
+ gd->fops = &mtd_block_ops;
++ if (IS_ENABLED(CONFIG_FIT_PARTITION) && !mtd_type_is_nand(new->mtd))
++ gd->flags |= GENHD_FL_EXT_DEVT;
+
+ if (tr->part_bits)
+ if (new->devnum < 26)
+--- a/drivers/mtd/ubi/block.c
++++ b/drivers/mtd/ubi/block.c
+@@ -428,6 +428,9 @@ int ubiblock_create(struct ubi_volume_in
+ goto out_cleanup_disk;
+ }
+ gd->private_data = dev;
++#ifdef CONFIG_FIT_PARTITION
++ gd->flags |= GENHD_FL_EXT_DEVT;
++#endif
+ sprintf(gd->disk_name, "ubiblock%d_%d", dev->ubi_num, dev->vol_id);
+ set_capacity(gd, disk_capacity);
+ dev->gd = gd;
--- a/include/linux/msdos_partition.h
+++ b/include/linux/msdos_partition.h
@@ -31,6 +31,7 @@ enum msdos_sys_ind {