From patchwork Sat Oct 15 09:29:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 13007632 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 976DCC4332F for ; Sat, 15 Oct 2022 09:31:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=I60bqrXW12d7X1UWFZtPq0R+pY81uUWftU70rAHOihw=; b=28Rl5bc3nac9tM +X47xP3QdP1I8TLNlMdcgC1GW1rH2ajOHkK6f6fgoYoAFD1qACjGEcZn/DnONgPQe1tzdNOFTtAEv lYtjQvjZek0UkD4LC6eNRZ4DepL0FD9FEmDMMSNRhp64OYfJKG+5h13tbMdOGYVWoGHGMAnTSUfGw k6GIYyIAoVCMqIHJxjM2KIdCWxoIP6UaGYAJE9P7wR+XVwAI9jsN3qHV8GvEEO78m3wZ4EC93M+5n 1IqO+rZo9dQf4TNjt9cY8r33F7h/nEm8xD8sMl393vuqZyBH8l1nabmCr8VZ6+A6o93SRWxuviM/0 bYN0yIpp6ICP5uff0aHQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ojdUY-00GhcV-09; Sat, 15 Oct 2022 09:30:02 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ojdUT-00GhbV-27; Sat, 15 Oct 2022 09:29:58 +0000 Received: by mail-ed1-x52b.google.com with SMTP id l22so9769565edj.5; Sat, 15 Oct 2022 02:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=YdzYzQPczOob4IO3pFuC56rrjAxpW/32RTAW6DZMbmw=; b=ScPcjlScG/12Dzm1gMutsJyhlbXrRHcQe5LoFTVzEPNAlIgbKfw9jTLOOJjsoXYvxt cPdraLACd90IvK/7cIXd1ehw6DDI9lqLttV27FM/coh5tUDubXuvdft3QygshnghsPJk NVmk56mAzQr5l4+f58oxFwggXQ0xeRI0Bo72u9UZqNjJ4J+ClZWT6t1SW89xKXm8wiLm IatFqAJvlBq2UiVl0pBZwvdJ9h7m0Xlgs7YYIlbKPlrv36WyK1uHFO06fRt0pl94zZ8Y AxtdlX0heYM5nbTOQvRzd0Z8PcDYY+LZCwtuPz7wGwiwJUw3xmc4tWQM8tPLCaXApzkQ nEAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YdzYzQPczOob4IO3pFuC56rrjAxpW/32RTAW6DZMbmw=; b=Mn1epLEDpogz2bUgrFvTENb4wgByhekxb5h4b4HALa1k7TY4nnzgDhAI3lt5+u9GPn nDB8a5GNEb2mhj5xFL+F/FJKq1adxRhQAlM7II5UBJR37Y+3Uw2VNKrVrdN9T0jaCRp0 AZY+UDUvxVAtZzyHZP4kLHhDM4Ywy1mugtPTIicGZruktWg8iJy35kwXj9+3lkc6NXhX aiMcqze2xsv7O/LTZpmX2KY/ALe24r0kwPSqdXlvFRU4RWi/6MnpL/gKBoyxNKqoa34T k30gzknAp6grp/jenc6rg0+hiDznkW2gbqc5sHpWCsGk2frOS0kty15vIKFe33rB7Xr+ A1AA== X-Gm-Message-State: ACrzQf2lNK4lS2Ez8dpZuyZcjLhqJPcavOWhX2KckhqUu7+AORbtMx7I B8IT3bsOS2t3pUk0WDaiVOQ= X-Google-Smtp-Source: AMsMyM72A/OQ5F+l11gRXWkiD/asNwXE5n0HCwpwa1VFlMvtIR/rNH0GO5v33NsnX6lqLOgY95iJVQ== X-Received: by 2002:aa7:c40b:0:b0:45d:4492:a8cb with SMTP id j11-20020aa7c40b000000b0045d4492a8cbmr1575750edq.217.1665826195174; Sat, 15 Oct 2022 02:29:55 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id v5-20020a1709060b4500b0073d6093ac93sm2919033ejg.16.2022.10.15.02.29.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Oct 2022 02:29:54 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski Cc: linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, Florian Fainelli , Hauke Mehrtens , bcm-kernel-feedback-list@broadcom.com, John Crispin , =?utf-8?b?QXLEsW7DpyDDnE5BTA==?= , Sergio Paracuellos , linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V3 1/2] dt-bindings: mtd: partitions: add TP-Link SafeLoader layout Date: Sat, 15 Oct 2022 11:29:49 +0200 Message-Id: <20221015092950.27467-1-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221015_022957_145407_7C1B93EE X-CRM114-Status: GOOD ( 15.07 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Rafał Miłecki 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 Reviewed-by: Rob Herring --- V2: Fix typo in commit: s/same/name/ V3: Drop quotes from $ref Describe flash space coverage & partitions types in the binding See commit body of PATCH 2/2 for real life example --- .../tplink,safeloader-partitions.yaml | 49 +++++++++++++++++++ 1 file changed, 49 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..63e596aa0ca3 --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/partitions/tplink,safeloader-partitions.yaml @@ -0,0 +1,49 @@ +# 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. + + Partitions specified in partitions table cover whole flash space. Some + contain static data that shouldn't get modified (device's MAC or WiFi + calibration data). Others are semi-static (like kernel). Finally some + partitions contain fully changeable content (like rootfs). + + 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 + +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>; + }; From patchwork Sat Oct 15 09:29:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 13007633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C448FC433FE for ; Sat, 15 Oct 2022 09:31:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PrFby3NWP4TE/aN+BW1zyl1HWTqsrg9eBy/Y2aKy9MY=; b=zfuII9+lusTvvf 7RDHaO53Xhh0E8soKbAuRFUU/NvAbTPAWjH1kjF7+GQuqtdADyX8n6yBhNg6XMQmAm8kD/9CnlKdx 80vcBSpc6cgcywOpvpkNlNVu/Lha7Vubaiywdx5sPRW9/WY3YvFRjDR3WyAXn1r3i8z+S+ZmSPKDV gw+ULDWchyZoJERAM707VfCDTsf/yoAbZleRvh6yNC3ZP4SOGEDRMT7R7SiGdfuGLyiXUvdpTHIxJ kvAAGbsMZwQSD1F4Z7MPggbLdzs1Ww8Brcg7z4ggsERHFiFVnxmxqfPauJYNDkH/2tB0R7idv10lR UZlaajxQS0dfcRBbMWnA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ojdUj-00GheE-AN; Sat, 15 Oct 2022 09:30:13 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ojdUV-00Ghbr-MN; Sat, 15 Oct 2022 09:30:01 +0000 Received: by mail-ed1-x52f.google.com with SMTP id m16so9784223edc.4; Sat, 15 Oct 2022 02:29:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=n1leUPz9eRmtkKAuoQy7jtZ9sa/gPKNy6QQPI2xntTE=; b=gi+ghUDbcJJsvBA7TpOUOxcrM6ymKzlJ8bdphuNJQ81M09hMEBZzwtBpJbqulmqxwu ikn2qY7g6K1Y6IL9hdR7TVr+51Z2Wk2KgQYmbqIDTJ2G6CaFt7QJZlOn380zhZ4Rx5sQ Dzfub5Ukn19j9IJ159NUFrK6MLpGqxNSP7DOi7g0eUMwORd4621Z+XLQH7FVJxv7vEag KIrO1f3ayILTNpUrnGieKWOd06htwOj+HT+DLWfTNHnoWixK5cKLS7P8bZN60W7QAYT3 XNzsIYvVP1HdXW+pQNrpgITTM9oqnjClDEiSveuwVG1Zq2u3dU9hURyw9rOXOn1nv4Eb qvDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n1leUPz9eRmtkKAuoQy7jtZ9sa/gPKNy6QQPI2xntTE=; b=irqo0wBqHvMDqgkP6BbL5B218xwTFM8Vtp6csOEefH2FtKBJobI+Pfo20jriMB6+XL ro2Gp4klEELmjmQA/bmMo6hmNvPey57b19S9dMFUZSr0AF+TK7WLJaMWHNWwevoSQLnO jvnDF5f7kYL1BgKDYvhATD/hXnIsWvwhTBpaUGK+llWpFaTrsQOc3sHCsmVfPI+gLIqy XDVbQrztDG6zBgwy8viMupsyKl/oNpFwQ9IG9UzpcwqH7oSA3kDBwz1fMWFrr+rQ/HNd xx9Nmq6OQwehsaIfNAbrzYlOIQ55OBu24LWrite0n6LhmwtXTHkSpi4paq4B1YqHPEa2 hj1g== X-Gm-Message-State: ACrzQf24Z0WkXacwAi8KF/o10oZVT3ZDH9uWSkNH+QR38Zo9NSlTVUif DF+omr92pzy0w574r0xHQIQ= X-Google-Smtp-Source: AMsMyM5wQ+gLYaZPD+V12nQs8b7vvOVrCp1dn3Psuunk1hNHOf2ImHlHQVtPaGON8fzPTm1wVbgkhw== X-Received: by 2002:a05:6402:270b:b0:45d:61cd:73cc with SMTP id y11-20020a056402270b00b0045d61cd73ccmr297129edd.136.1665826197766; Sat, 15 Oct 2022 02:29:57 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id v5-20020a1709060b4500b0073d6093ac93sm2919033ejg.16.2022.10.15.02.29.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Oct 2022 02:29:57 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski Cc: linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, Florian Fainelli , Hauke Mehrtens , bcm-kernel-feedback-list@broadcom.com, John Crispin , =?utf-8?b?QXLEsW7DpyDDnE5BTA==?= , Sergio Paracuellos , linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V3 2/2] mtd: parsers: add TP-Link SafeLoader partitions table parser Date: Sat, 15 Oct 2022 11:29:50 +0200 Message-Id: <20221015092950.27467-2-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221015092950.27467-1-zajec5@gmail.com> References: <20221015092950.27467-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221015_022959_772553_9FE0D5D0 X-CRM114-Status: GOOD ( 23.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Rafał Miłecki This parser deals with most TP-Link home routers. It reads info about partitions and registers them in the MTD subsystem. Example from TP-Link Archer C5 V2: spi-nor spi0.0: s25fl128s1 (16384 Kbytes) 15 tplink-safeloader partitions found on MTD device spi0.0 Creating 15 MTD partitions on "spi0.0": 0x000000000000-0x000000040000 : "fs-uboot" 0x000000040000-0x000000440000 : "os-image" 0x000000440000-0x000000e40000 : "rootfs" 0x000000e40000-0x000000e40200 : "default-mac" 0x000000e40200-0x000000e40400 : "pin" 0x000000e40400-0x000000e40600 : "product-info" 0x000000e50000-0x000000e60000 : "partition-table" 0x000000e60000-0x000000e60200 : "soft-version" 0x000000e61000-0x000000e70000 : "support-list" 0x000000e70000-0x000000e80000 : "profile" 0x000000e80000-0x000000e90000 : "default-config" 0x000000e90000-0x000000ee0000 : "user-config" 0x000000ee0000-0x000000fe0000 : "log" 0x000000fe0000-0x000000ff0000 : "radio_bk" 0x000000ff0000-0x000001000000 : "radio" Signed-off-by: Rafał Miłecki --- V2: Fix types (thanks kernel test robot) Fix off-by-one when setting '\0' V3: Include parsing example in commit body --- 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 + */ + +#include +#include +#include +#include +#include +#include + +#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");