From patchwork Mon Oct 21 12:32:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Valentin?= X-Patchwork-Id: 1180556 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=marcant.net Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SQGiPcF/"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46xbdt4StYz9sPL for ; Mon, 21 Oct 2019 23:33:22 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=APzWptvvQe7naKgc7HJoydg2pmvB9kvSMUSpYXifSFE=; b=SQGiPcF/cvcbTl 89D9oKCNmVCF62mWni1w2m5ZZK/kVZMhu6hipWrhFY6+fLp6AOS2sQ29Z5TNF9I2ROETGCvIwkdMd 361Su8ZDX/RfQWkyOdP1qzRwEEw4xd4DPZ7XjlpMlS9x9HYC5N8U9cczuj8LUC3Dxa0UcLo+ClsYn ktMyVX51HpKGFTBcLQuJK0FS6MlvZ0GvfwCaRFUfHCt/41EYY+3RCy+RCqCoUKzFzbY427wg339OZ 7FlsZYntMLBJQQvy+QfW2Q/SkMd1GSMJmHhy0jjfQv8T2xvMj7nmUGdXfgFNgdZsBK+4fmP+hF/Zj jLasecxdhJjTbrWEFzPA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iMWs8-0006kb-8b; Mon, 21 Oct 2019 12:33:16 +0000 Received: from mail3.marcant.net ([217.14.160.188]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1iMWrN-0005kv-Rj for openwrt-devel@lists.openwrt.org; Mon, 21 Oct 2019 12:32:33 +0000 Received: from [192.168.180.1] (port=54470 helo=admins.marcant.net) by mail3.marcant.net with esmtp (Exim 4.82_1-5b7a7c0-XX) (envelope-from ) id 1iMWrE-00081H-30 for openwrt-devel@lists.openwrt.org; Mon, 21 Oct 2019 14:32:20 +0200 Received: from varus.marcant.loc (unknown [192.168.3.44]) by admins.marcant.net (Postfix) with ESMTPA id CEB0E2802BF; Mon, 21 Oct 2019 14:32:20 +0200 (CEST) From: =?utf-8?q?Andr=C3=A9_Valentin?= To: openwrt-devel@lists.openwrt.org Date: Mon, 21 Oct 2019 14:32:13 +0200 Message-Id: <20191021123214.2252-5-avalentin@marcant.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191021123214.2252-1-avalentin@marcant.net> References: <20191021123214.2252-1-avalentin@marcant.net> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191021_053230_067976_E4044A41 X-CRM114-Status: GOOD ( 12.29 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [217.14.160.188 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record Subject: [OpenWrt-Devel] [PATCH 4/5] target/generic: add ascii search option to mtd-mac-address helper X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: avalentin@marcant.net Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This improves the function of_get_mac_address_mtd the be able to handle u-boot environement. It is now possible to read a MAC address from the env and set this in the dt. Code has been taken from ar71xx. Signed-off-by: André Valentin --- ...ET-add-of_get_mac_address_mtd_search.patch | 96 +++++++++++++++++++ ...ET-add-of_get_mac_address_mtd_search.patch | 96 +++++++++++++++++++ 2 files changed, 192 insertions(+) create mode 100644 target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch create mode 100644 target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch diff --git a/target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch b/target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch new file mode 100644 index 0000000000..bf8ff608c0 --- /dev/null +++ b/target/linux/generic/hack-4.14/680-NET-add-of_get_mac_address_mtd_search.patch @@ -0,0 +1,96 @@ +--- a/drivers/of/of_net.c ++++ b/drivers/of/of_net.c +@@ -12,6 +12,7 @@ + #include + #include + #include ++#include + + /** + * of_get_phy_mode - Get phy mode for given device_node +@@ -49,6 +50,28 @@ static void *of_get_mac_addr(struct devi + return NULL; + } + ++char *of_get_mac_address_mtd_helper(const char *name, const char *buf, unsigned buf_len) ++{ ++ unsigned len = strlen(name); ++ char *cur, *last; ++ ++ if (buf_len == 0 || len == 0) ++ return NULL; ++ ++ if (buf_len < len) ++ return NULL; ++ ++ if (len == 1) ++ return memchr(buf, (int) *name, buf_len); ++ ++ last = (char *) buf + buf_len - len; ++ for (cur = (char *) buf; cur <= last; cur++) ++ if (cur[0] == name[0] && memcmp(cur, name, len) == 0) ++ return cur + len; ++ ++ return NULL; ++} ++ + static const void *of_get_mac_address_mtd(struct device_node *np) + { + #ifdef CONFIG_MTD +@@ -58,6 +81,7 @@ static const void *of_get_mac_address_mt + int size, ret; + struct mtd_info *mtd; + const char *part; ++ const char *search; + const __be32 *list; + phandle phandle; + u32 mac_inc = 0; +@@ -84,8 +109,46 @@ static const void *of_get_mac_address_mt + if (IS_ERR(mtd)) + return NULL; + +- ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac); +- put_mtd_device(mtd); ++ if (of_property_read_string(np, "mtd-mac-search", &search) == 0) { ++ // try to find mac address in mtd ++ char *buf; ++ char *mac_str; ++ int t; ++ ++ buf = vmalloc(mtd->size); ++ ++ ret = mtd_read(mtd, be32_to_cpup(list), mtd->size-be32_to_cpup(list), &retlen, buf); ++ put_mtd_device(mtd); ++ ++ buf[retlen - 1] = '\0'; ++ mac_str = of_get_mac_address_mtd_helper(search, buf, retlen); ++ ++ if (!mac_str) { ++ vfree(buf); ++ return NULL;; ++ } ++ ++ if (strlen(mac_str) == 19 && mac_str[0] == '"' && mac_str[18] == '"') { ++ mac_str[18] = 0; ++ mac_str++; ++ } ++ ++ t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); ++ ++ if (t != ETH_ALEN) ++ t = sscanf(mac_str, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); ++ ++ if (t != ETH_ALEN) { ++ vfree(buf); ++ return NULL; ++ } ++ vfree(buf); ++ } else { ++ ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac); ++ put_mtd_device(mtd); ++ } + + if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx)) + inc_idx = 5; diff --git a/target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch b/target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch new file mode 100644 index 0000000000..bf8ff608c0 --- /dev/null +++ b/target/linux/generic/hack-4.19/680-NET-add-of_get_mac_address_mtd_search.patch @@ -0,0 +1,96 @@ +--- a/drivers/of/of_net.c ++++ b/drivers/of/of_net.c +@@ -12,6 +12,7 @@ + #include + #include + #include ++#include + + /** + * of_get_phy_mode - Get phy mode for given device_node +@@ -49,6 +50,28 @@ static void *of_get_mac_addr(struct devi + return NULL; + } + ++char *of_get_mac_address_mtd_helper(const char *name, const char *buf, unsigned buf_len) ++{ ++ unsigned len = strlen(name); ++ char *cur, *last; ++ ++ if (buf_len == 0 || len == 0) ++ return NULL; ++ ++ if (buf_len < len) ++ return NULL; ++ ++ if (len == 1) ++ return memchr(buf, (int) *name, buf_len); ++ ++ last = (char *) buf + buf_len - len; ++ for (cur = (char *) buf; cur <= last; cur++) ++ if (cur[0] == name[0] && memcmp(cur, name, len) == 0) ++ return cur + len; ++ ++ return NULL; ++} ++ + static const void *of_get_mac_address_mtd(struct device_node *np) + { + #ifdef CONFIG_MTD +@@ -58,6 +81,7 @@ static const void *of_get_mac_address_mt + int size, ret; + struct mtd_info *mtd; + const char *part; ++ const char *search; + const __be32 *list; + phandle phandle; + u32 mac_inc = 0; +@@ -84,8 +109,46 @@ static const void *of_get_mac_address_mt + if (IS_ERR(mtd)) + return NULL; + +- ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac); +- put_mtd_device(mtd); ++ if (of_property_read_string(np, "mtd-mac-search", &search) == 0) { ++ // try to find mac address in mtd ++ char *buf; ++ char *mac_str; ++ int t; ++ ++ buf = vmalloc(mtd->size); ++ ++ ret = mtd_read(mtd, be32_to_cpup(list), mtd->size-be32_to_cpup(list), &retlen, buf); ++ put_mtd_device(mtd); ++ ++ buf[retlen - 1] = '\0'; ++ mac_str = of_get_mac_address_mtd_helper(search, buf, retlen); ++ ++ if (!mac_str) { ++ vfree(buf); ++ return NULL;; ++ } ++ ++ if (strlen(mac_str) == 19 && mac_str[0] == '"' && mac_str[18] == '"') { ++ mac_str[18] = 0; ++ mac_str++; ++ } ++ ++ t = sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); ++ ++ if (t != ETH_ALEN) ++ t = sscanf(mac_str, "%02hhx-%02hhx-%02hhx-%02hhx-%02hhx-%02hhx", ++ &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]); ++ ++ if (t != ETH_ALEN) { ++ vfree(buf); ++ return NULL; ++ } ++ vfree(buf); ++ } else { ++ ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac); ++ put_mtd_device(mtd); ++ } + + if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx)) + inc_idx = 5;