From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97274606A32F for ; Fri, 4 Dec 2026 09:46:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=SWuQA1S7; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=r+QQkPDD 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:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=WazmBcoqRrF8SC9XNzc5plZ2+rlKSuAyoB+ExyMp9Fk=; b=SWuQA1S7TTSynm213GMYRGMb1A JAFCXEGiqSbZrlNIoPaTxjbUs2cNaWZ9p6Kk5vo126b9QONkJCqsBdbLo2EaueSWkbeFkuakDheUD FpE3MMaWx3H0x0Fdx6HgeVWNqyJDFLy7W4hdqutD4aUkK9LwIqH1JGcQIiOrNjUsTeyTu/S4K3Qwm ZMcD0Aa1Kgmc/nvptSgh3XfbkuvEZyeRHl/Vkxf7ji7gjUBvbziACSJgGrd/xuowGhLq1d5J1EKuw XyfP/37diUBPoII/34C6m9IDa0mSv6Rn0DiuT+jts0eRbukz+GlOWc8ZytYMUxUDfKtvwEbrjxtgy omx9sMDw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZPru-0000000HPbt-1tjy; Fri, 04 Dec 2026 09:46:18 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZPro-0000000HPYQ-1s26 for linux-rockchip@lists.infradead.org; Fri, 04 Dec 2026 09:46:14 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-4bb9d8e8e90so6047405e9.3 for ; Fri, 04 Dec 2026 01:45:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796377554; x=1796982354; darn=lists.infradead.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=uvMCy8wGMtNKdz1YFf2E+Hkxaq1SGVEQlHKSK/0T/W4=; b=r+QQkPDDUaA56NWLzcBqHkafmI4IkFXszM/RdYDR8lpUbZfOk9xUWnToWFor9QRxIL TFWUmAkFu6WJNSfloP5wQ1rdOVOO7p3qe9P5jMvXPlBaGgjl4FeIhakNB8e9jtU5WAqf iuRT6+/K79G9Yv5R3oIFTzahhpnUJmgggVHdW40QOe2T4CzSnvAxe1Mor28yYteBmzX0 1Cz/ZBX4R6pluT9PL6Y7tmjo/8ymggZqancwUD0EqI2zMLzs97qbL7jxN5gDOtz+rK5I qTYZOg50EnoKdZFPAJGrcWro9ggbIOFumA9lhrOz8LBe1jtmNQ6+vMvIFGeqdztxFTSz kEEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796377554; x=1796982354; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=uvMCy8wGMt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JwMJ/WECz/zA2hM kzyY1uF51/H4+kT5nbuXBUEe1Zy3uHECH0t+BBi4wHvDeXu2wakX4WWH3TtjFP0bfN9e435wOvv Wzp52OE7oANx7nEzmQLpXjz/ML3+aRwwbA9wthm5WuW1UxM3KLpDMoWdL9gFpAvOaUJ3VqhVZ4O bdq/mim0rs6EhFqCRYQ/ItoY5k7LcEVbhX0cmQedVuBLvCWW1Bv/givQPm0beJAw X-Received: by 2002:a05:600c:354f:b0:4a1:f377:5c68 with SMTP id 5b1f17b1804b1-4bbeb8d0d7fmr24380395e9.34.1796377553701; Fri, 04 Dec 2026 01:45:53 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbea25ce51sm42637355e9.5.2026.12.04.01.45.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 01:45:53 -0800 (PST) Date: Fri, 4 Dec 2026 09:45:52 +0000 From: David Laight To: Alexey Charkov Cc: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Heiko Stuebner , =?UTF-8?B?UmFm?= =?UTF-8?B?YcWCIE1pxYJlY2tp?= , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Andrew Lunn , Heiner Kallweit , Russell King , Willem de Bruijn , Simon Horman , Ryan Leung , Ricardo Pardini , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v5 3/5] net: of_net: Derive MAC addresses from a unique device ID Message-ID: <20261204094552.62ffd53b@pumpkin> In-Reply-To: <20261125-rk3576-otp-cpuid-mac-v5-3-b3125c9a5fc8@flipper.net> References: <20261125-rk3576-otp-cpuid-mac-v5-0-b3125c9a5fc8@flipper.net> <20261125-rk3576-otp-cpuid-mac-v5-3-b3125c9a5fc8@flipper.net> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) X-Archived-By: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_014612_516710_1E4C9171 X-CRM114-Status: GOOD ( 43.08 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-rockchip@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Upstream kernel work for Rockchip platforms List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "Linux-rockchip" X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_2 autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Wed, 25 Nov 2026 23:36:40 +0400 Alexey Charkov wrote: > Bootloaders on several platforms derive a stable MAC address from an > identifier unique to the chip, such as a SoC serial number in OTP > memory, so that boards without dedicated storage for an address still > get a persistent one. They patch the result into the device tree they > pass on, which leaves kernels started any other way, e.g. straight from > the SPL in Falcon mode, with a random address changing on every boot. > > Derive the address in of_get_mac_address() instead, as the last resort > after any stored one, from a "unique-id" NVMEM cell. Doing it here > rather than in individual drivers lets any controller with a device tree > node use it, including PCIe ones. Pick the derivation by the compatible > of the referenced cell, as the algorithms differ between vendors, and > index the addresses by the device's "ethernetN" alias, which is how the > bootloaders assign them to interfaces. > > Start with Rockchip, whose bootloaders hash the CPU ID as a lowercase > hex string with SHA-256 and XOR the index into the last octet. Follow > U-Boot's rockchip_setup_macaddr() exactly, so that both yield the same > addresses. > > Suggested-by: Andrew Lunn > Signed-off-by: Alexey Charkov > --- > net/core/of_net.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- > 1 file changed, 103 insertions(+), 2 deletions(-) > > diff --git a/net/core/of_net.c b/net/core/of_net.c > index 93ea425b9248..1f9858d2d444 100644 > --- a/net/core/of_net.c > +++ b/net/core/of_net.c > @@ -4,7 +4,9 @@ > * > * Initially copied out of arch/powerpc/kernel/prom_parse.c > */ > +#include > #include > +#include > #include > #include > #include > @@ -97,6 +99,99 @@ int of_get_mac_address_nvmem(struct device_node *np, u8 *addr) > } > EXPORT_SYMBOL(of_get_mac_address_nvmem); > > +#define ROCKCHIP_CPUID_LEN 16 > + > +/* > + * Rockchip bootloaders hash the CPU ID as a lowercase hex string with SHA-256 > + * and XOR the index into the last octet, see rockchip_setup_macaddr() in > + * U-Boot's arch/arm/mach-rockchip/board.c. > + */ > +static int of_mac_from_rockchip_cpuid(const u8 *id, size_t len, int index, > + u8 *addr) > +{ > + char hex[ROCKCHIP_CPUID_LEN * 2]; > + u8 digest[SHA256_DIGEST_SIZE]; > + > + if (len != ROCKCHIP_CPUID_LEN || index > U8_MAX) > + return -EINVAL; > + > + bin2hex(hex, id, ROCKCHIP_CPUID_LEN); > + sha256(hex, sizeof(hex), digest); If you are going to use sha256() (or pretty much and other hash function for that matter) you might just as well hash the ascii string. sha256() does seem a little excessive, jhash() would do even though it only returns 32 bits. > + > + memcpy(addr, digest, ETH_ALEN); > + addr[0] &= 0xfe; /* clear the multicast bit */ > + addr[0] |= 0x02; /* set the locally administered bit */ > + addr[5] ^= index; That doesn't look that good to me. Setting the 'locally administered' bit doesn't really help with uniqueness. There certainly used to be plenty of physical ethernet cards that had 'locally administered' MAC addresses burnt into their EEPROMs. It would be better to use the same OUI (first 24 bits) that Linux uses for 'random' MAC addresses - just with a random number that is fixed for the specific hardware. IIRC 'locally administered' addresses mostly exist for DecNet where the MAC address was set by the local network administrator and was used to allocate a 32bit value that identified the machine (cf an IP address). David > + > + return 0; > +} > + > +struct of_mac_unique_id { > + const char *compatible; > + int (*derive)(const u8 *id, size_t len, int index, u8 *addr); > +}; > + > +/* Derivations by the compatible of the unique ID cell */ > +static const struct of_mac_unique_id of_mac_unique_ids[] = { > + { "rockchip,cpuid", of_mac_from_rockchip_cpuid }, > +}; > + > +/* > + * Derive a MAC address from a "unique-id" NVMEM cell the way the cell's > + * binding defines it, indexed by the "ethernetN" alias of the device, as that > + * is how bootloaders assign the derived addresses to interfaces. > + */ > +static int of_get_mac_address_unique_id(struct device_node *np, u8 *addr) > +{ > + const struct of_mac_unique_id *entry = NULL; > + struct of_phandle_args cell_spec; > + struct nvmem_cell *cell; > + int index, alias, ret, i; > + size_t len; > + u8 *id; > + > + index = of_property_match_string(np, "nvmem-cell-names", "unique-id"); > + if (index < 0) > + return -ENOENT; > + > + ret = of_parse_phandle_with_optional_args(np, "nvmem-cells", > + "#nvmem-cell-cells", > + index, &cell_spec); > + if (ret) > + return ret; > + > + for (i = 0; i < ARRAY_SIZE(of_mac_unique_ids); i++) { > + if (of_device_is_compatible(cell_spec.np, > + of_mac_unique_ids[i].compatible)) { > + entry = &of_mac_unique_ids[i]; > + break; > + } > + } > + of_node_put(cell_spec.np); > + if (!entry) > + return -EOPNOTSUPP; > + > + alias = of_alias_get_id(np, "ethernet"); > + if (alias < 0) > + return alias; > + > + cell = of_nvmem_cell_get(np, "unique-id"); > + if (IS_ERR(cell)) > + return PTR_ERR(cell); > + > + id = nvmem_cell_read(cell, &len); > + nvmem_cell_put(cell); > + if (IS_ERR(id)) > + return PTR_ERR(id); > + > + ret = entry->derive(id, len, alias, addr); > + kfree(id); > + if (ret) > + return ret; > + > + return is_valid_ether_addr(addr) ? 0 : -EINVAL; > +} > + > /** > * of_get_mac_address() > * @np: Caller's Device Node > @@ -108,7 +203,9 @@ EXPORT_SYMBOL(of_get_mac_address_nvmem); > * because that is the default address. If that isn't set, then the obsolete > * 'address' is checked, just in case we're using an old device tree. If any > * of the above isn't set, then try to get MAC address from nvmem cell named > - * 'mac-address'. > + * 'mac-address'. As the last resort, derive one from an identifier unique to > + * the device in an nvmem cell named 'unique-id', if the way to do so is known > + * for that cell. > * > * Note that the 'address' property is supposed to contain a virtual address of > * the register set, but some DTS files have redefined that property to be the > @@ -142,7 +239,11 @@ int of_get_mac_address(struct device_node *np, u8 *addr) > if (!ret) > return 0; > > - return of_get_mac_address_nvmem(np, addr); > + ret = of_get_mac_address_nvmem(np, addr); > + if (!ret || ret == -EPROBE_DEFER) > + return ret; > + > + return of_get_mac_address_unique_id(np, addr); > } > EXPORT_SYMBOL(of_get_mac_address); > > _______________________________________________ Linux-rockchip mailing list Linux-rockchip@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-rockchip