--- zzzz-none-000/linux-5.4.213/drivers/mtd/mtdpart.c 2022-09-15 10:04:56.000000000 +0000 +++ alder-5690pro-762/linux-5.4.213/drivers/mtd/mtdpart.c 2024-08-14 09:02:07.000000000 +0000 @@ -15,10 +15,14 @@ #include #include #include +#include #include #include #include "mtdcore.h" +#include "mtdsplit/mtdsplit.h" + +#define MTD_ERASE_PARTIAL 0x8000 /* partition only covers parts of an erase block */ /* Our partition linked list */ static LIST_HEAD(mtd_partitions); @@ -38,6 +42,12 @@ struct list_head list; }; +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part); +static int parse_mtd_partitions_by_type(struct mtd_info *master, + enum mtd_parser_type type, + const struct mtd_partition **pparts, + struct mtd_part_parser_data *data); + /* * Given a pointer to the MTD object in the mtd_part structure, we can retrieve * the pointer to that structure. @@ -198,6 +208,53 @@ { struct mtd_part *part = mtd_to_part(mtd); int ret; + size_t wrlen = 0; + u8 *erase_buf = NULL; + u32 erase_buf_ofs = 0; + bool partial_start = false; + + if (mtd->flags & MTD_ERASE_PARTIAL) { + size_t readlen = 0; + u64 mtd_ofs; + + erase_buf = kmalloc(part->parent->erasesize, GFP_ATOMIC); + if (!erase_buf) + return -ENOMEM; + + mtd_ofs = part->offset + instr->addr; + erase_buf_ofs = do_div(mtd_ofs, part->parent->erasesize); + + if (erase_buf_ofs > 0) { + instr->addr -= erase_buf_ofs; + ret = mtd_read(part->parent, + instr->addr + part->offset, + part->parent->erasesize, + &readlen, erase_buf); + + instr->len += erase_buf_ofs; + partial_start = true; + } else { + mtd_ofs = part->offset + part->mtd.size; + erase_buf_ofs = part->parent->erasesize - + do_div(mtd_ofs, part->parent->erasesize); + + if (erase_buf_ofs > 0) { + instr->len += erase_buf_ofs; + ret = mtd_read(part->parent, + part->offset + instr->addr + + instr->len - part->parent->erasesize, + part->parent->erasesize, &readlen, + erase_buf); + } else { + ret = 0; + } + } + if (ret < 0) { + kfree(erase_buf); + return ret; + } + + } instr->addr += part->offset; ret = part->parent->_erase(part->parent, instr); @@ -205,6 +262,24 @@ instr->fail_addr -= part->offset; instr->addr -= part->offset; + if (mtd->flags & MTD_ERASE_PARTIAL) { + if (partial_start) { + part->parent->_write(part->parent, + instr->addr, erase_buf_ofs, + &wrlen, erase_buf); + instr->addr += erase_buf_ofs; + } else { + instr->len -= erase_buf_ofs; + part->parent->_write(part->parent, + instr->addr + instr->len, + erase_buf_ofs, &wrlen, + erase_buf + + part->parent->erasesize - + erase_buf_ofs); + } + kfree(erase_buf); + } + return ret; } @@ -217,7 +292,16 @@ static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) { struct mtd_part *part = mtd_to_part(mtd); - return part->parent->_unlock(part->parent, ofs + part->offset, len); + + ofs += part->offset; + + if (mtd->flags & MTD_ERASE_PARTIAL) { + /* round up len to next erasesize and round down offset to prev block */ + len = (mtd_div_by_eb(len, part->parent) + 1) * part->parent->erasesize; + ofs &= ~(part->parent->erasesize - 1); + } + + return part->parent->_unlock(part->parent, ofs, len); } static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) @@ -517,19 +601,22 @@ remainder = do_div(tmp, wr_alignment); if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { /* Doesn't start on a boundary of major erase size */ - /* FIXME: Let it be writable if it is on a boundary of - * _minor_ erase size though */ - slave->mtd.flags &= ~MTD_WRITEABLE; - printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", - part->name); + slave->mtd.flags |= MTD_ERASE_PARTIAL; + if (((u32)slave->mtd.size) > parent->erasesize) + slave->mtd.flags &= ~MTD_WRITEABLE; + else + slave->mtd.erasesize = slave->mtd.size; } - tmp = part_absolute_offset(parent) + slave->mtd.size; + tmp = part_absolute_offset(parent) + slave->offset + slave->mtd.size; remainder = do_div(tmp, wr_alignment); if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { - slave->mtd.flags &= ~MTD_WRITEABLE; - printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", - part->name); + slave->mtd.flags |= MTD_ERASE_PARTIAL; + + if ((u32)slave->mtd.size > parent->erasesize) + slave->mtd.flags &= ~MTD_WRITEABLE; + else + slave->mtd.erasesize = slave->mtd.size; } mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops); @@ -612,6 +699,7 @@ if (ret) goto err_remove_part; + mtd_partition_split(parent, new); mtd_add_partition_attrs(new); return 0; @@ -698,6 +786,66 @@ } EXPORT_SYMBOL_GPL(mtd_del_partition); +static int +run_parsers_by_type(struct mtd_part *slave, enum mtd_parser_type type) +{ + struct mtd_partition *parts; + int nr_parts; + int i; + + nr_parts = parse_mtd_partitions_by_type(&slave->mtd, type, (const struct mtd_partition **)&parts, + NULL); + if (nr_parts <= 0) + return nr_parts; + + if (WARN_ON(!parts)) + return 0; + + for (i = 0; i < nr_parts; i++) { + /* adjust partition offsets */ + parts[i].offset += slave->offset; + + mtd_add_partition(slave->parent, + parts[i].name, + parts[i].offset, + parts[i].size); + } + + kfree(parts); + + return nr_parts; +} + +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME +#else +#define SPLIT_FIRMWARE_NAME "unused" +#endif + +static void split_firmware(struct mtd_info *master, struct mtd_part *part) +{ + run_parsers_by_type(part, MTD_PARSER_TYPE_FIRMWARE); +} + +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part) +{ + static int rootfs_found = 0; + + if (rootfs_found) + return; + + if (!strcmp(part->mtd.name, "rootfs")) { + run_parsers_by_type(part, MTD_PARSER_TYPE_ROOTFS); + + rootfs_found = 1; + } + + if (IS_ENABLED(CONFIG_MTD_SPLIT_FIRMWARE) && + !strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) && + !of_find_property(mtd_get_of_node(&part->mtd), "compatible", NULL)) + split_firmware(master, part); +} + /* * This function, given a master MTD object and a partition table, creates * and registers slave MTD objects which are bound to the master according to @@ -715,7 +863,7 @@ uint64_t cur_offset = 0; int i, ret; - printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); + pr_notice("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); for (i = 0; i < nbparts; i++) { slave = allocate_partition(master, parts + i, i, cur_offset); @@ -724,9 +872,9 @@ goto err_del_partitions; } - mutex_lock(&mtd_partitions_mutex); - list_add(&slave->list, &mtd_partitions); - mutex_unlock(&mtd_partitions_mutex); + mutex_lock(&mtd_partitions_mutex); + list_add(&slave->list, &mtd_partitions); + mutex_unlock(&mtd_partitions_mutex); ret = add_mtd_device(&slave->mtd); if (ret) { @@ -738,12 +886,13 @@ goto err_del_partitions; } + mtd_partition_split(master, slave); mtd_add_partition_attrs(slave); /* Look for subpartitions */ parse_mtd_partitions(&slave->mtd, parts[i].types, NULL); - cur_offset = slave->offset + slave->mtd.size; - } + cur_offset = slave->offset + slave->mtd.size; + } return 0; @@ -817,6 +966,9 @@ */ static const char * const default_mtd_part_types[] = { "cmdlinepart", +#ifdef CONFIG_AVM_MTD_PARSER + "avmpart_of_nor_spi", +#endif "ofpart", NULL }; @@ -1023,6 +1175,61 @@ } } +static struct mtd_part_parser * +get_partition_parser_by_type(enum mtd_parser_type type, + struct mtd_part_parser *start) +{ + struct mtd_part_parser *p, *ret = NULL; + + spin_lock(&part_parser_lock); + + p = list_prepare_entry(start, &part_parsers, list); + if (start) + mtd_part_parser_put(start); + + list_for_each_entry_continue(p, &part_parsers, list) { + if (p->type == type && try_module_get(p->owner)) { + ret = p; + break; + } + } + + spin_unlock(&part_parser_lock); + + return ret; +} + +static int parse_mtd_partitions_by_type(struct mtd_info *master, + enum mtd_parser_type type, + const struct mtd_partition **pparts, + struct mtd_part_parser_data *data) +{ + struct mtd_part_parser *prev = NULL; + int ret = 0; + + while (1) { + struct mtd_part_parser *parser; + + parser = get_partition_parser_by_type(type, prev); + if (!parser) + break; + + ret = (*parser->parse_fn)(master, pparts, data); + + if (ret > 0) { + mtd_part_parser_put(parser); + printk(KERN_NOTICE + "%d %s partitions found on MTD device %s\n", + ret, parser->name, master->name); + break; + } + + prev = parser; + } + + return ret; +} + int mtd_is_partition(const struct mtd_info *mtd) { struct mtd_part *part; @@ -1040,6 +1247,24 @@ } EXPORT_SYMBOL_GPL(mtd_is_partition); +struct mtd_info *mtd_get_master(const struct mtd_info *mtd) +{ + if (!mtd_is_partition(mtd)) + return (struct mtd_info *)mtd; + + return mtd_to_part(mtd)->parent; +} +EXPORT_SYMBOL_GPL(mtd_get_master); + +uint64_t mtdpart_get_offset(const struct mtd_info *mtd) +{ + if (!mtd_is_partition(mtd)) + return 0; + + return mtd_to_part(mtd)->offset; +} +EXPORT_SYMBOL_GPL(mtdpart_get_offset); + /* Returns the size of the entire flash chip */ uint64_t mtd_get_device_size(const struct mtd_info *mtd) {