linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout
@ 2022-10-12  5:04 Rafał Miłecki
  2022-10-12  5:04 ` [PATCH V2 2/2] mtd: parsers: add TP-Link SafeLoader partitions table parser Rafał Miłecki
  2022-10-12 16:21 ` [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rob Herring
  0 siblings, 2 replies; 4+ messages in thread
From: Rafał Miłecki @ 2022-10-12  5:04 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Rob Herring, Krzysztof Kozlowski
  Cc: linux-mtd, devicetree, Florian Fainelli, Hauke Mehrtens,
	bcm-kernel-feedback-list, John Crispin,
	Arınç ÜNAL, Sergio Paracuellos, linux-arm-kernel,
	linux-mips, Rafał Miłecki

From: Rafał Miłecki <rafal@milecki.pl>

Most TP-Link home routers use the same partitioning system based on a
custom ASCII table.

It doesn't seem to have any official name. GPL sources contain tool
named simply "make_flash" and Makefile target "FlashMaker".

This partitions table format was first found in devices with a custom
SafeLoader bootloader so it was called SafeLoader by a community. Later
it was ported to other bootloaders but it seems the name sticked.

Add binding for describing flashes with SafeLoader partitions table. It
allows operating systems to parse it properly and register proper flash
layout.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
V2: Fix typo in commit: s/same/name/
---
 .../tplink,safeloader-partitions.yaml         | 44 +++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml

diff --git a/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
new file mode 100644
index 000000000000..e38aaea5aa11
--- /dev/null
+++ b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mtd/partitions/tplink,safeloader-partitions.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: TP-Link SafeLoader partitions
+
+description: |
+  TP-Link home routers store various data on flash (e.g. bootloader,
+  flash layout, firmware, product info, configuration, calibration
+  data). That requires flash partitioning.
+
+  Flash space layout of TP-Link devices is stored on flash itself using
+  a custom ASCII-based format. That format was first found in TP-Link
+  devices with a custom SafeLoader bootloader. Later it was adapted to
+  CFE and U-Boot bootloaders.
+
+  This binding describes partitioning method and defines offset of ASCII
+  based partitions table. That offset is picked at manufacturing process
+  and doesn't change.
+
+maintainers:
+  - Rafał Miłecki <rafal@milecki.pl>
+
+properties:
+  compatible:
+    const: tplink,safeloader-partitions
+
+  partitions-table-offset:
+    description: Flash offset of partitions table
+    $ref: "/schemas/types.yaml#/definitions/uint32"
+
+required:
+  - partitions-table-offset
+
+additionalProperties: false
+
+examples:
+  - |
+    partitions {
+        compatible = "tplink,safeloader-partitions";
+        partitions-table-offset = <0x100000>;
+    };
-- 
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH V2 2/2] mtd: parsers: add TP-Link SafeLoader partitions table parser
  2022-10-12  5:04 [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rafał Miłecki
@ 2022-10-12  5:04 ` Rafał Miłecki
  2022-10-12 16:21 ` [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rob Herring
  1 sibling, 0 replies; 4+ messages in thread
From: Rafał Miłecki @ 2022-10-12  5:04 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Rob Herring, Krzysztof Kozlowski
  Cc: linux-mtd, devicetree, Florian Fainelli, Hauke Mehrtens,
	bcm-kernel-feedback-list, John Crispin,
	Arınç ÜNAL, Sergio Paracuellos, linux-arm-kernel,
	linux-mips, Rafał Miłecki

From: Rafał Miłecki <rafal@milecki.pl>

This parser deals with most TP-Link home routers. It reads info about
partitions and registers them in the MTD subsystem.

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
V2: Fix types (thanks kernel test robot)
    Fix off-by-one when setting '\0'
---
 drivers/mtd/parsers/Kconfig             |  15 +++
 drivers/mtd/parsers/Makefile            |   1 +
 drivers/mtd/parsers/tplink_safeloader.c | 150 ++++++++++++++++++++++++
 3 files changed, 166 insertions(+)
 create mode 100644 drivers/mtd/parsers/tplink_safeloader.c

diff --git a/drivers/mtd/parsers/Kconfig b/drivers/mtd/parsers/Kconfig
index aaa06050c9bc..c258ba2a3a6f 100644
--- a/drivers/mtd/parsers/Kconfig
+++ b/drivers/mtd/parsers/Kconfig
@@ -123,6 +123,21 @@ config MTD_AFS_PARTS
 	  for your particular device. It won't happen automatically. The
 	  'physmap' map driver (CONFIG_MTD_PHYSMAP) does this, for example.
 
+config MTD_PARSER_TPLINK_SAFELOADER
+	tristate "TP-Link Safeloader partitions parser"
+	depends on MTD && (ARCH_BCM_5301X || ATH79 || SOC_MT7620 || SOC_MT7621 || COMPILE_TEST)
+	help
+	  TP-Link home routers use flash partitions to store various data. Info
+	  about flash space layout is stored in a partitions table using a
+	  custom ASCII-based format.
+
+	  That format was first found in devices with SafeLoader bootloader and
+	  was named after it. Later it was adapted to CFE and U-Boot
+	  bootloaders.
+
+	  This driver reads partitions table, parses it and creates MTD
+	  partitions.
+
 config MTD_PARSER_TRX
 	tristate "Parser for TRX format partitions"
 	depends on MTD && (BCM47XX || ARCH_BCM_5301X || ARCH_MEDIATEK || RALINK || COMPILE_TEST)
diff --git a/drivers/mtd/parsers/Makefile b/drivers/mtd/parsers/Makefile
index 23fa4de4016f..0e70b621a1d8 100644
--- a/drivers/mtd/parsers/Makefile
+++ b/drivers/mtd/parsers/Makefile
@@ -10,6 +10,7 @@ ofpart-$(CONFIG_MTD_OF_PARTS_BCM4908)	+= ofpart_bcm4908.o
 ofpart-$(CONFIG_MTD_OF_PARTS_LINKSYS_NS)+= ofpart_linksys_ns.o
 obj-$(CONFIG_MTD_PARSER_IMAGETAG)	+= parser_imagetag.o
 obj-$(CONFIG_MTD_AFS_PARTS)		+= afs.o
+obj-$(CONFIG_MTD_PARSER_TPLINK_SAFELOADER)	+= tplink_safeloader.o
 obj-$(CONFIG_MTD_PARSER_TRX)		+= parser_trx.o
 obj-$(CONFIG_MTD_SERCOMM_PARTS)		+= scpart.o
 obj-$(CONFIG_MTD_SHARPSL_PARTS)		+= sharpslpart.o
diff --git a/drivers/mtd/parsers/tplink_safeloader.c b/drivers/mtd/parsers/tplink_safeloader.c
new file mode 100644
index 000000000000..23584a477391
--- /dev/null
+++ b/drivers/mtd/parsers/tplink_safeloader.c
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright © 2022 Rafał Miłecki <rafal@milecki.pl>
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/of.h>
+#include <linux/slab.h>
+
+#define TPLINK_SAFELOADER_DATA_OFFSET		4
+#define TPLINK_SAFELOADER_MAX_PARTS		32
+
+struct safeloader_cmn_header {
+	__be32 size;
+	uint32_t unused;
+} __packed;
+
+static void *mtd_parser_tplink_safeloader_read_table(struct mtd_info *mtd)
+{
+	struct safeloader_cmn_header hdr;
+	struct device_node *np;
+	size_t bytes_read;
+	size_t offset;
+	size_t size;
+	char *buf;
+	int err;
+
+	np = mtd_get_of_node(mtd);
+	if (mtd_is_partition(mtd))
+		of_node_get(np);
+	else
+		np = of_get_child_by_name(np, "partitions");
+
+	if (of_property_read_u32(np, "partitions-table-offset", (u32 *)&offset)) {
+		pr_err("Failed to get partitions table offset\n");
+		goto err_put;
+	}
+
+	err = mtd_read(mtd, offset, sizeof(hdr), &bytes_read, (uint8_t *)&hdr);
+	if (err && !mtd_is_bitflip(err)) {
+		pr_err("Failed to read from %s at 0x%zx\n", mtd->name, offset);
+		goto err_put;
+	}
+
+	size = be32_to_cpu(hdr.size);
+
+	buf = kmalloc(size + 1, GFP_KERNEL);
+	if (!buf)
+		goto err_put;
+
+	err = mtd_read(mtd, offset + sizeof(hdr), size, &bytes_read, buf);
+	if (err && !mtd_is_bitflip(err)) {
+		pr_err("Failed to read from %s at 0x%zx\n", mtd->name, offset + sizeof(hdr));
+		goto err_kfree;
+	}
+
+	buf[size] = '\0';
+
+	of_node_put(np);
+
+	return buf;
+
+err_kfree:
+	kfree(buf);
+err_put:
+	of_node_put(np);
+	return NULL;
+}
+
+static int mtd_parser_tplink_safeloader_parse(struct mtd_info *mtd,
+					      const struct mtd_partition **pparts,
+					      struct mtd_part_parser_data *data)
+{
+	struct mtd_partition *parts;
+	char name[65];
+	size_t offset;
+	size_t bytes;
+	char *buf;
+	int idx;
+	int err;
+
+	parts = kcalloc(TPLINK_SAFELOADER_MAX_PARTS, sizeof(*parts), GFP_KERNEL);
+	if (!parts) {
+		err = -ENOMEM;
+		goto err_out;
+	}
+
+	buf = mtd_parser_tplink_safeloader_read_table(mtd);
+	if (!buf) {
+		err = -ENOENT;
+		goto err_out;
+	}
+
+	for (idx = 0, offset = TPLINK_SAFELOADER_DATA_OFFSET;
+	     idx < TPLINK_SAFELOADER_MAX_PARTS &&
+	     sscanf(buf + offset, "partition %64s base 0x%llx size 0x%llx%zn\n",
+		    name, &parts[idx].offset, &parts[idx].size, &bytes) == 3;
+	     idx++, offset += bytes + 1) {
+		parts[idx].name = kstrdup(name, GFP_KERNEL);
+		if (!parts[idx].name) {
+			err = -ENOMEM;
+			goto err_free;
+		}
+	}
+
+	if (idx == TPLINK_SAFELOADER_MAX_PARTS)
+		pr_warn("Reached maximum number of partitions!\n");
+
+	kfree(buf);
+
+	*pparts = parts;
+
+	return idx;
+
+err_free:
+	for (idx -= 1; idx >= 0; idx--)
+		kfree(parts[idx].name);
+err_out:
+	return err;
+};
+
+static void mtd_parser_tplink_safeloader_cleanup(const struct mtd_partition *pparts,
+						 int nr_parts)
+{
+	int i;
+
+	for (i = 0; i < nr_parts; i++)
+		kfree(pparts[i].name);
+
+	kfree(pparts);
+}
+
+static const struct of_device_id mtd_parser_tplink_safeloader_of_match_table[] = {
+	{ .compatible = "tplink,safeloader-partitions" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, mtd_parser_tplink_safeloader_of_match_table);
+
+static struct mtd_part_parser mtd_parser_tplink_safeloader = {
+	.parse_fn = mtd_parser_tplink_safeloader_parse,
+	.cleanup = mtd_parser_tplink_safeloader_cleanup,
+	.name = "tplink-safeloader",
+	.of_match_table = mtd_parser_tplink_safeloader_of_match_table,
+};
+module_mtd_part_parser(mtd_parser_tplink_safeloader);
+
+MODULE_LICENSE("GPL");
-- 
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout
  2022-10-12  5:04 [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rafał Miłecki
  2022-10-12  5:04 ` [PATCH V2 2/2] mtd: parsers: add TP-Link SafeLoader partitions table parser Rafał Miłecki
@ 2022-10-12 16:21 ` Rob Herring
  2022-10-15  8:57   ` Rafał Miłecki
  1 sibling, 1 reply; 4+ messages in thread
From: Rob Herring @ 2022-10-12 16:21 UTC (permalink / raw)
  To: Rafał Miłecki
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Krzysztof Kozlowski, linux-mtd, devicetree, Florian Fainelli,
	Hauke Mehrtens, bcm-kernel-feedback-list, John Crispin,
	Arınç ÜNAL, Sergio Paracuellos, linux-arm-kernel,
	linux-mips, Rafał Miłecki

On Wed, Oct 12, 2022 at 07:04:41AM +0200, Rafał Miłecki wrote:
> From: Rafał Miłecki <rafal@milecki.pl>
> 
> Most TP-Link home routers use the same partitioning system based on a
> custom ASCII table.
> 
> It doesn't seem to have any official name. GPL sources contain tool
> named simply "make_flash" and Makefile target "FlashMaker".
> 
> This partitions table format was first found in devices with a custom
> SafeLoader bootloader so it was called SafeLoader by a community. Later
> it was ported to other bootloaders but it seems the name sticked.
> 
> Add binding for describing flashes with SafeLoader partitions table. It
> allows operating systems to parse it properly and register proper flash
> layout.
> 
> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
> ---
> V2: Fix typo in commit: s/same/name/
> ---
>  .../tplink,safeloader-partitions.yaml         | 44 +++++++++++++++++++
>  1 file changed, 44 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
> 
> diff --git a/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
> new file mode 100644
> index 000000000000..e38aaea5aa11
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
> @@ -0,0 +1,44 @@
> +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/mtd/partitions/tplink,safeloader-partitions.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: TP-Link SafeLoader partitions
> +
> +description: |
> +  TP-Link home routers store various data on flash (e.g. bootloader,
> +  flash layout, firmware, product info, configuration, calibration
> +  data). That requires flash partitioning.
> +
> +  Flash space layout of TP-Link devices is stored on flash itself using
> +  a custom ASCII-based format. That format was first found in TP-Link
> +  devices with a custom SafeLoader bootloader. Later it was adapted to
> +  CFE and U-Boot bootloaders.
> +
> +  This binding describes partitioning method and defines offset of ASCII
> +  based partitions table. That offset is picked at manufacturing process
> +  and doesn't change.
> +
> +maintainers:
> +  - Rafał Miłecki <rafal@milecki.pl>
> +
> +properties:
> +  compatible:
> +    const: tplink,safeloader-partitions
> +
> +  partitions-table-offset:
> +    description: Flash offset of partitions table
> +    $ref: "/schemas/types.yaml#/definitions/uint32"

Don't need quotes.

> +
> +required:
> +  - partitions-table-offset
> +
> +additionalProperties: false
> +
> +examples:
> +  - |
> +    partitions {
> +        compatible = "tplink,safeloader-partitions";
> +        partitions-table-offset = <0x100000>;

What is in the space before this? It is part of the safeloader 
partitions? If the safeloader partitions are not the entire device, then 
perhaps we still need fixed partitions.

Rob

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout
  2022-10-12 16:21 ` [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rob Herring
@ 2022-10-15  8:57   ` Rafał Miłecki
  0 siblings, 0 replies; 4+ messages in thread
From: Rafał Miłecki @ 2022-10-15  8:57 UTC (permalink / raw)
  To: Rob Herring
  Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Krzysztof Kozlowski, linux-mtd, devicetree, Florian Fainelli,
	Hauke Mehrtens, bcm-kernel-feedback-list, John Crispin,
	Arınç ÜNAL, Sergio Paracuellos, linux-arm-kernel,
	linux-mips, Rafał Miłecki

On 12.10.2022 18:21, Rob Herring wrote:
> On Wed, Oct 12, 2022 at 07:04:41AM +0200, Rafał Miłecki wrote:
>> From: Rafał Miłecki <rafal@milecki.pl>
>>
>> Most TP-Link home routers use the same partitioning system based on a
>> custom ASCII table.
>>
>> It doesn't seem to have any official name. GPL sources contain tool
>> named simply "make_flash" and Makefile target "FlashMaker".
>>
>> This partitions table format was first found in devices with a custom
>> SafeLoader bootloader so it was called SafeLoader by a community. Later
>> it was ported to other bootloaders but it seems the name sticked.
>>
>> Add binding for describing flashes with SafeLoader partitions table. It
>> allows operating systems to parse it properly and register proper flash
>> layout.
>>
>> Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
>> ---
>> V2: Fix typo in commit: s/same/name/
>> ---
>>   .../tplink,safeloader-partitions.yaml         | 44 +++++++++++++++++++
>>   1 file changed, 44 insertions(+)
>>   create mode 100644 Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
>>
>> diff --git a/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
>> new file mode 100644
>> index 000000000000..e38aaea5aa11
>> --- /dev/null
>> +++ b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml
>> @@ -0,0 +1,44 @@
>> +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
>> +%YAML 1.2
>> +---
>> +$id: http://devicetree.org/schemas/mtd/partitions/tplink,safeloader-partitions.yaml#
>> +$schema: http://devicetree.org/meta-schemas/core.yaml#
>> +
>> +title: TP-Link SafeLoader partitions
>> +
>> +description: |
>> +  TP-Link home routers store various data on flash (e.g. bootloader,
>> +  flash layout, firmware, product info, configuration, calibration
>> +  data). That requires flash partitioning.
>> +
>> +  Flash space layout of TP-Link devices is stored on flash itself using
>> +  a custom ASCII-based format. That format was first found in TP-Link
>> +  devices with a custom SafeLoader bootloader. Later it was adapted to
>> +  CFE and U-Boot bootloaders.
>> +
>> +  This binding describes partitioning method and defines offset of ASCII
>> +  based partitions table. That offset is picked at manufacturing process
>> +  and doesn't change.
>> +
>> +maintainers:
>> +  - Rafał Miłecki <rafal@milecki.pl>
>> +
>> +properties:
>> +  compatible:
>> +    const: tplink,safeloader-partitions
>> +
>> +  partitions-table-offset:
>> +    description: Flash offset of partitions table
>> +    $ref: "/schemas/types.yaml#/definitions/uint32"
> 
> Don't need quotes.
> 
>> +
>> +required:
>> +  - partitions-table-offset
>> +
>> +additionalProperties: false
>> +
>> +examples:
>> +  - |
>> +    partitions {
>> +        compatible = "tplink,safeloader-partitions";
>> +        partitions-table-offset = <0x100000>;
> 
> What is in the space before this? It is part of the safeloader
> partitions? If the safeloader partitions are not the entire device, then
> perhaps we still need fixed partitions.

Various partitions are placed before and after partitions table.

Usually/always the whole flash space is covered by partitions defined in
the partitions table.

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-10-15  8:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-12  5:04 [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rafał Miłecki
2022-10-12  5:04 ` [PATCH V2 2/2] mtd: parsers: add TP-Link SafeLoader partitions table parser Rafał Miłecki
2022-10-12 16:21 ` [PATCH V2 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Rob Herring
2022-10-15  8:57   ` Rafał Miłecki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).