From nobody Wed Jun 10 17:29:18 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 337BAC433F5 for ; Tue, 10 May 2022 16:23:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347318AbiEJQ13 (ORCPT ); Tue, 10 May 2022 12:27:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237625AbiEJQ1X (ORCPT ); Tue, 10 May 2022 12:27:23 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E2C2A0BFC; Tue, 10 May 2022 09:23:23 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id j4so30243869lfh.8; Tue, 10 May 2022 09:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lE9M1LT3yKC1XMSzyFOtlSmz+JiIYT6pQw3EYMoZJec=; b=UHgfWbsTrNXXX6mF1ThS2P4I3clamg5/10H18jPoQOT8ICcwCcR2ohxK4nQBIv1lmN PX2uPGYkEabwoJ4clEomc62ZzxHhv+n7Q1sB/pl1yJqzpgQoNbAGwxfsB+G1aGLtsHHC hi3MNnhRMcIHD7h0eRxAWUbwd1VH91Nfq2zf86XZ8IHKAjc/YPsiL/yPV+im9nYg9wQ7 U4FuD5PnDqnnNTtWWm5XPGM9bpndelYfMlcOqXHaLC4vjTHbwLhRT6b6Pb8vjQ7Y90o+ nAMP5zRsVS0zsAVI9uKgzQEpuZegbm3RT2qIg9SkHWUlFX9tM+ZD6XjXH+ZB9a72V7Fh /kHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lE9M1LT3yKC1XMSzyFOtlSmz+JiIYT6pQw3EYMoZJec=; b=IrtudqCezAcRTDC9NeeXXP5Z0KCupJuKm0Z+woKS8+6Hr7bh+ZsV6LVB+Ab/zy2U2J JC6beqc1tVf8mlbxQRjPFkshk4bdEaXE8spDwH6H/DFM2P9FwvWEwtOrgezLpEHyIQ0h nAzKTAOMixqBvm6aPdcFc5pfvImjVSWFCusYz49bwUFlRI+omx+kf20uc9UO0QRDYqeP D6vRq3+ohfgM1imgrN8ROOmR91SVsMlC8Aw88PdJoZAp1giSmq/5ox0cB5Dxb5maAfWg wPeOSlfFlXOFA5WyF8g5UyFEc6Oed3DH7/9YMbt0Z4Ot8sKByoi2j4KDMLQCWYLvM0af ELgg== X-Gm-Message-State: AOAM530F8noeFhryBAL2bgSKWOcE2tQRtVJ4uyEe6JchM3LCwONh4bh7 A8e+LGl3bbynr8aOEy7DcPc= X-Google-Smtp-Source: ABdhPJwfQ0dQeA8YbZQyqC6JBjPo56fJ0/mD8aAtBAuEVrVNRrN152j/YvTuQrWeK3ZE6QkDtxHmmw== X-Received: by 2002:a19:f706:0:b0:473:9e36:79de with SMTP id z6-20020a19f706000000b004739e3679demr17272138lfe.35.1652199801284; Tue, 10 May 2022 09:23:21 -0700 (PDT) Received: from vmu1804.lan ([2a06:a003:501a:a7ce::887]) by smtp.googlemail.com with ESMTPSA id k19-20020a2eb753000000b0024f501aa770sm2217089ljo.134.2022.05.10.09.23.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 09:23:20 -0700 (PDT) From: Mikhail Zhilkin To: Rob Herring , Krzysztof Kozlowski Cc: Miquel Raynal , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Karim , M , Mikhail Zhilkin Subject: [PATCH v4 1/3] dt-bindings: Add Sercomm (Suzhou) Corporation vendor prefix Date: Tue, 10 May 2022 16:23:14 +0000 Message-Id: <20220510162314.20810-1-csharper2005@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220510161641.20655-1-csharper2005@gmail.com> References: <20220510161641.20655-1-csharper2005@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Add "sercomm" vendor prefix for "Sercomm (Suzhou) Corporation". Company website: Link: https://www.sercomm.com/ Signed-off-by: Mikhail Zhilkin Acked-by: Krzysztof Kozlowski --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Docum= entation/devicetree/bindings/vendor-prefixes.yaml index 01430973ecec..65ff22364fb3 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -1082,6 +1082,8 @@ patternProperties: description: Sensirion AG "^sensortek,.*": description: Sensortek Technology Corporation + "^sercomm,.*": + description: Sercomm (Suzhou) Corporation "^sff,.*": description: Small Form Factor Committee "^sgd,.*": --=20 2.25.1 From nobody Wed Jun 10 17:29:18 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D17EC433F5 for ; Tue, 10 May 2022 16:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347323AbiEJQ2U (ORCPT ); Tue, 10 May 2022 12:28:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345070AbiEJQ2P (ORCPT ); Tue, 10 May 2022 12:28:15 -0400 Received: from mail-lj1-x22b.google.com (mail-lj1-x22b.google.com [IPv6:2a00:1450:4864:20::22b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23F9F132A03; Tue, 10 May 2022 09:24:17 -0700 (PDT) Received: by mail-lj1-x22b.google.com with SMTP id s27so21491950ljd.2; Tue, 10 May 2022 09:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Z+YkSbV6bjoznxMCuUfht3UUoaizx6Wbg5BycQIS6zs=; b=A05ZxnXVAWmmDDGxBx9E47mbX7S0Lb5kx3BXIz0LE4ucreSY1X5yTdw7uft1fPn42/ aSTUlBrV3mgtTEgdylYx+nll563rrNB3yLv3YfpwcocKTTB2nJlHQuPPZGDOFo9FkApv /nt7jSKjA7VUx7/6XV8T/FD/6BPbfx5kbcMXuaFE9qU1KY5ooZyFiGQsQQjAxe8SV26L n4CchBQ/O/UQcRId+lmCaDgNha4pS/0skcAe5ggQpZbwwC2dvFNIifmWQZeGdp+Mn/9X 3IfuPe7QNMlHO9mJiWu7xumyVo9xH3sYL1B7a3UvEOfhiosc9MogVya8LuVtSls1mevH u79A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Z+YkSbV6bjoznxMCuUfht3UUoaizx6Wbg5BycQIS6zs=; b=Htqzv5IVAxLwP1XzYTQ6YO2Lsz6FFFGZ6B5mDH7uj/4KEoBSh6TLd1ic4zdA67r1aW fjeNXvS1y0G4SW8YvUqJZWny+4IKVa2rggdPsA766U8/d7eYUBbb4O/ttdU4tGp8wBgI Aic0r1e1yGoy3gOupr0bfMG25iZ5vcLf3nDqR7z4+FofFDcn5S0vbHJ2e8uonGR1fYdR 6DQibOtNdpb9FtTPFVO/6c4RDFb7m7PVb4MHNnGkWGIVn6d5YIw7xGKm5wZbq89S4h+r 7v1jPo839tvsf6RCeBcutfDjKubUN8EAHufbuLjxZ5e+oRdrASgznW32enJLTZD93Mv+ cY6A== X-Gm-Message-State: AOAM531mrVl8ApK1hwInPPCtECZeZoz+7w8unnyh0vjHEuJJ4v6+pnh/ v7V6JtNJDo1a8jdK+yPNWw4= X-Google-Smtp-Source: ABdhPJyJMk2lXCJXghy02HIfIaweFY01KpQ6Pmd5e5ocPKNv1uom8v6aOVxqjQNPks87LU5Q4KIkog== X-Received: by 2002:a2e:a30a:0:b0:24f:cf6:11ab with SMTP id l10-20020a2ea30a000000b0024f0cf611abmr14027237lje.461.1652199855528; Tue, 10 May 2022 09:24:15 -0700 (PDT) Received: from vmu1804.lan ([2a06:a003:501a:a7ce::887]) by smtp.googlemail.com with ESMTPSA id r12-20020a19ac4c000000b0047255d2111fsm2338420lfc.78.2022.05.10.09.24.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 09:24:15 -0700 (PDT) From: Mikhail Zhilkin To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring Cc: Krzysztof Kozlowski , =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Karim , M , Mikhail Zhilkin , Krzysztof Kozlowski Subject: [PATCH v4 2/3] dt-bindings: mtd: partitions: Extend fixed-partitions binding Date: Tue, 10 May 2022 16:24:03 +0000 Message-Id: <20220510162403.20861-1-csharper2005@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220510161641.20655-1-csharper2005@gmail.com> References: <20220510161641.20655-1-csharper2005@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extend fixed-partitions binding for support of Sercomm partition parser (add "sercomm,sc-partitions" compatible). Signed-off-by: Mikhail Zhilkin Reviewed-by: Krzysztof Kozlowski --- .../mtd/partitions/fixed-partitions.yaml | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/mtd/partitions/fixed-partiti= ons.yaml b/Documentation/devicetree/bindings/mtd/partitions/fixed-partition= s.yaml index ea4cace6a955..ad3ccd250802 100644 --- a/Documentation/devicetree/bindings/mtd/partitions/fixed-partitions.yaml +++ b/Documentation/devicetree/bindings/mtd/partitions/fixed-partitions.yaml @@ -19,7 +19,11 @@ maintainers: =20 properties: compatible: - const: fixed-partitions + oneOf: + - const: fixed-partitions + - items: + - const: sercomm,sc-partitions + - const: fixed-partitions =20 "#address-cells": true =20 @@ -27,7 +31,24 @@ properties: =20 patternProperties: "@[0-9a-f]+$": - $ref: "partition.yaml#" + allOf: + - $ref: "partition.yaml#" + - if: + properties: + compatible: + contains: + const: sercomm,sc-partitions + then: + properties: + sercomm,scpart-id: + description: Partition id in Sercomm partition map. Mtd + parser uses this id to find a record in the partition map + containing offset and size of the current partition. The + values from partition map overrides partition offset and + size defined in reg property of the dts. Frequently these + values are the same, but may differ if device has bad + eraseblocks on a flash. + $ref: /schemas/types.yaml#/definitions/uint32 =20 required: - "#address-cells" @@ -52,6 +73,7 @@ examples: reg =3D <0x0100000 0x200000>; }; }; + - | partitions { compatible =3D "fixed-partitions"; @@ -64,6 +86,7 @@ examples: reg =3D <0x00000000 0x1 0x00000000>; }; }; + - | partitions { compatible =3D "fixed-partitions"; @@ -82,6 +105,7 @@ examples: reg =3D <0x2 0x00000000 0x1 0x00000000>; }; }; + - | partitions { compatible =3D "fixed-partitions"; @@ -119,3 +143,30 @@ examples: }; }; }; + + - | + partitions { + compatible =3D "sercomm,sc-partitions", "fixed-partitions"; + #address-cells =3D <1>; + #size-cells =3D <1>; + + partition@0 { + label =3D "u-boot"; + reg =3D <0x0 0x100000>; + sercomm,scpart-id =3D <0>; + read-only; + }; + + partition@100000 { + label =3D "dynamic partition map"; + reg =3D <0x100000 0x100000>; + sercomm,scpart-id =3D <1>; + }; + + partition@200000 { + label =3D "Factory"; + reg =3D <0x200000 0x100000>; + sercomm,scpart-id =3D <2>; + read-only; + }; + }; --=20 2.25.1 From nobody Wed Jun 10 17:29:18 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6690C433EF for ; Tue, 10 May 2022 16:30:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347376AbiEJQeI (ORCPT ); Tue, 10 May 2022 12:34:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347413AbiEJQdV (ORCPT ); Tue, 10 May 2022 12:33:21 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11612201F0B for ; Tue, 10 May 2022 09:29:04 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id b19so24557726wrh.11 for ; Tue, 10 May 2022 09:29:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3XVNWTw//x2P33682v6NyS3MKzAVxlenoKtyup9T5mU=; b=KQEFBD6h/2K08A1SA37c7PyrjJuSV5KTGE+BASQ4M1USvyhFBBeiWIXOI3DzZEE27J AUT1f0wRh+QfeqGh2zY24jEFJv4eSsqtBqfk+xvZ7GxJ3j0GSAjSVLhA2T2ZwPz32yz4 B0/0h/t7vZiyPtXQgLyB0TcgwycLD3mMVmwG/jdGQb34I8X2deFwq+JoGDp4PX1lNyRR g2ZQqNVxQTj0tRPRD/0amR48tjGdWRI1Y4pPs+le93k7k6RgsV0/u7JyMvWV0m6GlDFc Zz7nvaAcQ3gvim2XAzceHhsI3EyueAMXwvo6uklK3bZiHMtDpga1sXqY0ML8ccHtQTBU aIFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3XVNWTw//x2P33682v6NyS3MKzAVxlenoKtyup9T5mU=; b=UWTG6vN1Lt2mxv8DcSZNTSe4WFVzoS4AZf01dbkYBPLki+HUC47E61GUipOn7M4H9A gYXiIlBh6oRRSS61lZSGWCji1FboAJlwI+Z0W+We+17p3KQ0a1pnuxQvto8deecrqFSD pZTvMokQ9KrDKzLSJ5KhVG+lMeeFm6Oht9kDIf8TCyHIH/qESImtaoHD08oymgOejrOc p1ftMMr/Opz0FseW7d52oTOhlf3SukYu57t22RE16LMNff6KYNmZlTFysFchgwqBXV7f ga4LdX8tv+lvCRurb4e2xil/aRxDtL3VZVriOtbQ9i0WtJ4i/FGRLs3Q/gQWkngGQ2ff SXnA== X-Gm-Message-State: AOAM531HJOioiu+kcKuiYrGu5cZe24D3xV5CuDar66xhqfZFqT1o7fVU yd3UcELWDlWoLuZTgKlxIqM= X-Google-Smtp-Source: ABdhPJyI0E2SLxIB75cDEQnsj2weBY7mp03iqZlhsa1AFk7p/HK9fphoC0dBbGnI9kTWrE+wHNlGOg== X-Received: by 2002:adf:d1ea:0:b0:20c:505e:babc with SMTP id g10-20020adfd1ea000000b0020c505ebabcmr19224148wrd.562.1652200142595; Tue, 10 May 2022 09:29:02 -0700 (PDT) Received: from vmu1804.lan ([104.28.230.247]) by smtp.googlemail.com with ESMTPSA id e22-20020a05600c4e5600b003942a244f2csm3205947wmq.5.2022.05.10.09.28.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 09:29:02 -0700 (PDT) From: Mikhail Zhilkin To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra Cc: Mikhail Zhilkin , Hauke Mehrtens , NOGUCHI Hiroshi , INAGAKI Hiroshi , linux-kernel@vger.kernel.org, linux-mtd@lists.infradead.org, Karim , M Subject: [PATCH v4 3/3] mtd: parsers: add support for Sercomm partitions Date: Tue, 10 May 2022 16:26:55 +0000 Message-Id: <20220510162655.21011-1-csharper2005@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220510161641.20655-1-csharper2005@gmail.com> References: <20220510161641.20655-1-csharper2005@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds an MTD partition parser for the Sercomm partition table that is used in some Beeline, Netgear and Sercomm routers. The Sercomm partition map table contains real partition offsets, which may differ from device to device depending on the number and location of bad blocks on NAND. This is essentially the same code as proposed by NOGUCHI Hiroshi: Link: https://github.com/openwrt/openwrt/pull/1318#issuecomment-420607394 Signed-off-by: NOGUCHI Hiroshi Signed-off-by: Mikhail Zhilkin --- drivers/mtd/parsers/Kconfig | 9 ++ drivers/mtd/parsers/Makefile | 1 + drivers/mtd/parsers/scpart.c | 240 +++++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+) create mode 100644 drivers/mtd/parsers/scpart.c diff --git a/drivers/mtd/parsers/Kconfig b/drivers/mtd/parsers/Kconfig index 23763d16e4f9..851f4670d89b 100644 --- a/drivers/mtd/parsers/Kconfig +++ b/drivers/mtd/parsers/Kconfig @@ -186,3 +186,12 @@ config MTD_QCOMSMEM_PARTS help This provides support for parsing partitions from Shared Memory (SMEM) for NAND and SPI flash on Qualcomm platforms. + +config MTD_SERCOMM_PARTS + tristate "Sercomm partition table parser" + depends on MTD + help + This provides partitions table parser for devices with Sercomm + partition map. This partition table contains real partition + offsets, which may differ from device to device depending on the + number and location of bad blocks on NAND. diff --git a/drivers/mtd/parsers/Makefile b/drivers/mtd/parsers/Makefile index 2e98aa048278..2fcf0ab9e7da 100644 --- a/drivers/mtd/parsers/Makefile +++ b/drivers/mtd/parsers/Makefile @@ -10,6 +10,7 @@ ofpart-$(CONFIG_MTD_OF_PARTS_LINKSYS_NS)+=3D ofpart_links= ys_ns.o obj-$(CONFIG_MTD_PARSER_IMAGETAG) +=3D parser_imagetag.o obj-$(CONFIG_MTD_AFS_PARTS) +=3D afs.o obj-$(CONFIG_MTD_PARSER_TRX) +=3D parser_trx.o +obj-$(CONFIG_MTD_SERCOMM_PARTS) +=3D scpart.o obj-$(CONFIG_MTD_SHARPSL_PARTS) +=3D sharpslpart.o obj-$(CONFIG_MTD_REDBOOT_PARTS) +=3D redboot.o obj-$(CONFIG_MTD_QCOMSMEM_PARTS) +=3D qcomsmempart.o diff --git a/drivers/mtd/parsers/scpart.c b/drivers/mtd/parsers/scpart.c new file mode 100644 index 000000000000..d7bb736c9f2a --- /dev/null +++ b/drivers/mtd/parsers/scpart.c @@ -0,0 +1,240 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * drivers/mtd/scpart.c: Sercomm Partition Parser + * + * Copyright (C) 2018 NOGUCHI Hiroshi + * Copyright (C) 2022 Mikhail Zhilkin + */ + +#include +#include +#include +#include +#include + +#define MOD_NAME "scpart" + +#ifdef pr_fmt +#undef pr_fmt +#endif + +#define pr_fmt(fmt) MOD_NAME ": " fmt + +static const char sc_part_magic[] =3D { + 'S', 'C', 'F', 'L', 'M', 'A', 'P', 'O', 'K', '\0', +}; +#define PART_MAGIC_LEN sizeof(sc_part_magic) + +/* assumes that all fields are set by CPU native endian */ +struct sc_part_desc { + uint32_t part_id; + uint32_t part_offs; + uint32_t part_bytes; +}; +#define ID_ALREADY_FOUND 0xFFFFFFFFUL + +#define MAP_OFFS_IN_BLK 0x800 + +#define MAP_MIRROR_NUM 2 + +static int scpart_desc_is_valid(struct sc_part_desc *pdesc) +{ + return ((pdesc->part_id !=3D 0xFFFFFFFFUL) && + (pdesc->part_offs !=3D 0xFFFFFFFFUL) && + (pdesc->part_bytes !=3D 0xFFFFFFFFUL)); +} + +static int scpart_scan_partmap(struct mtd_info *master, loff_t partmap_off= s, + struct sc_part_desc **ppdesc) +{ + uint8_t *buf; + loff_t offs; + size_t retlen; + struct sc_part_desc *pdesc =3D NULL; + struct sc_part_desc *tmpdesc; + int cnt =3D 0; + int res2; + int res =3D 0; + + buf =3D kzalloc(master->erasesize, GFP_KERNEL); + if (!buf) { + res =3D -ENOMEM; + goto out; + } + + res2 =3D mtd_read(master, partmap_offs, master->erasesize, &retlen, buf); + if (res2 || retlen !=3D master->erasesize) { + res =3D -EIO; + goto free; + } + + offs =3D MAP_OFFS_IN_BLK; + while (offs < (master->erasesize - sizeof(*tmpdesc))) { + tmpdesc =3D (struct sc_part_desc *)&(buf[offs]); + if (!scpart_desc_is_valid(tmpdesc)) + break; + cnt++; + offs +=3D sizeof(*tmpdesc); + } + + if (cnt > 0) { + int bytes =3D cnt * sizeof(*pdesc); + + pdesc =3D kcalloc(cnt, sizeof(*pdesc), GFP_KERNEL); + if (!pdesc) { + res =3D -ENOMEM; + goto free; + } + memcpy(pdesc, &(buf[MAP_OFFS_IN_BLK]), bytes); + + *ppdesc =3D pdesc; + res =3D cnt; + } + +free: + kfree(buf); + +out: + return res; +} + +static int scpart_find_partmap(struct mtd_info *master, + struct sc_part_desc **ppdesc) +{ + loff_t offs; + uint8_t rdbuf[PART_MAGIC_LEN]; + size_t retlen; + int magic_found =3D 0; + int res2; + int res =3D 0; + + offs =3D 0; + while ((magic_found < MAP_MIRROR_NUM) && + (offs < master->size) && !mtd_block_isbad(master, offs)) { + res2 =3D mtd_read(master, offs, PART_MAGIC_LEN, &retlen, rdbuf); + if (res2 || (retlen !=3D PART_MAGIC_LEN)) { + res =3D -EIO; + goto out; + } + if (!memcmp(rdbuf, sc_part_magic, PART_MAGIC_LEN)) { + pr_debug("Signature found at 0x%llx\n", offs); + magic_found++; + res =3D scpart_scan_partmap(master, offs, ppdesc); + if (res > 0) + goto out; + } + offs +=3D master->erasesize; + } + +out: + if (res > 0) + pr_info("Valid 'SC PART MAP' (%d partitions) found at 0x%llx\n", res, of= fs); + else + pr_info("No valid 'SC PART MAP' was found\n"); + + return res; +} + +static int scpart_parse(struct mtd_info *master, + const struct mtd_partition **pparts, + struct mtd_part_parser_data *data) +{ + struct sc_part_desc *scpart_map =3D NULL; + struct mtd_partition *parts =3D NULL; + struct device_node *mtd_node; + struct device_node *ofpart_node; + struct device_node *pp; + const char *partname; + int nr_scparts; + int nr_parts =3D 0; + int n; + int res =3D 0; + + mtd_node =3D mtd_get_of_node(master); + if (!mtd_node) + goto out; + + ofpart_node =3D of_get_child_by_name(mtd_node, "partitions"); + if (!ofpart_node) + goto out; + + nr_scparts =3D scpart_find_partmap(master, &scpart_map); + if (nr_scparts <=3D 0) { + res =3D nr_scparts; + goto free; + } + + parts =3D kcalloc(of_get_child_count(ofpart_node), sizeof(*parts), + GFP_KERNEL); + if (!parts) { + res =3D -ENOMEM; + goto out; + } + + for_each_child_of_node(ofpart_node, pp) { + u32 scpart_id; + + if (of_property_read_u32(pp, "sercomm,scpart-id", &scpart_id)) + continue; + + for (n =3D 0 ; n < nr_scparts ; n++) + if ((scpart_map[n].part_id !=3D ID_ALREADY_FOUND) && + (scpart_id =3D=3D scpart_map[n].part_id)) + break; + if (n >=3D nr_scparts) + /* not match */ + continue; + + /* add the partition found in OF into MTD partition array */ + parts[nr_parts].offset =3D scpart_map[n].part_offs; + parts[nr_parts].size =3D scpart_map[n].part_bytes; + parts[nr_parts].of_node =3D pp; + + if (!of_property_read_string(pp, "label", &partname)) + parts[nr_parts].name =3D partname; + if (of_property_read_bool(pp, "read-only")) + parts[nr_parts].mask_flags |=3D MTD_WRITEABLE; + if (of_property_read_bool(pp, "lock")) + parts[nr_parts].mask_flags |=3D MTD_POWERUP_LOCK; + + /* mark as 'done' */ + scpart_map[n].part_id =3D ID_ALREADY_FOUND; + + nr_parts++; + } + + if (nr_parts > 0) { + *pparts =3D parts; + res =3D nr_parts; + } else + pr_info("No partition in OF matches partition ID with 'SC PART MAP'.\n"); + + of_node_put(pp); + +free: + kfree(scpart_map); + if (res <=3D 0) + kfree(parts); + +out: + return res; +} + +static const struct of_device_id scpart_parser_of_match_table[] =3D { + { .compatible =3D "sercomm,sc-partitions" }, + {}, +}; +MODULE_DEVICE_TABLE(of, scpart_parser_of_match_table); + +static struct mtd_part_parser scpart_parser =3D { + .parse_fn =3D scpart_parse, + .name =3D "scpart", + .of_match_table =3D scpart_parser_of_match_table, +}; +module_mtd_part_parser(scpart_parser); + +/* mtd parsers will request the module by parser name */ +MODULE_ALIAS("scpart"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("NOGUCHI Hiroshi "); +MODULE_DESCRIPTION("Sercomm partition parser"); --=20 2.25.1