[PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID

Ryan Leung untilscour at protonmail.com
Wed Nov 25 05:49:26 PST 2026


Hi Alexey,

On Tuesday, November 24th, 2026 at 8:04 PM, Alexey Charkov <alchark at flipper.net> wrote:

> Rockchip bootloaders derive stable Ethernet MAC addresses from the CPU
> ID in the OTP memory and patch them into the device tree they pass on.
> Kernels started any other way, e.g. straight from the SPL in Falcon
> mode, end up with random addresses which change on every boot, even
> though the CPU ID is just as readable from Linux.
> 
> Implement the derivation for cells marked as "rockchip,cpuid", handing
> the result to consumers which ask for the cell as "mac-address", such as
> of_get_mac_address(). Leave the CPU ID unchanged for any other reader,
> so that the same cell keeps serving the ID itself.
> 
> Follow U-Boot's rockchip_setup_macaddr() exactly, so that both yield the
> same addresses: hash the CPU ID's lowercase hex string with SHA-256,
> take the first six octets as a unicast, locally administered address,
> and XOR the cell argument into the last octet, which is how U-Boot gets
> eth1addr from ethaddr.
> 
> Put the address in the first six octets and keep the cell at the size
> of the CPU ID, as NVMEM cells have a fixed size and MAC address
> consumers accept a longer cell.
> 
> Hooking the derivation into the OTP provider rather than a separate
> layout means it works with any fixed layout of the OTP cells, and needs
> no per-SoC knowledge, as the cell already gives the CPU ID's location.
> 
> Signed-off-by: Alexey Charkov <alchark at flipper.net>
> ---
>  drivers/nvmem/Kconfig        |  4 +++-
>  drivers/nvmem/rockchip-otp.c | 48 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 51 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
> index d2f5bb236663..5776d4e4b391 100644
> --- a/drivers/nvmem/Kconfig
> +++ b/drivers/nvmem/Kconfig
> @@ -448,9 +448,11 @@ config NVMEM_ROCKCHIP_OTP
>  	tristate "Rockchip OTP controller support"
>  	depends on ARCH_ROCKCHIP || COMPILE_TEST
>  	depends on HAS_IOMEM
> +	select CRYPTO_LIB_SHA256
>  	help
>  	  This is a simple driver to dump specified values of Rockchip SoC
> -	  from OTP, such as cpu-leakage.
> +	  from OTP, such as cpu-leakage. It also derives stable Ethernet MAC
> +	  addresses from the CPU ID, as Rockchip bootloaders do.
> 
>  	  This driver can also be built as a module. If so, the module
>  	  will be called nvmem_rockchip_otp.
> diff --git a/drivers/nvmem/rockchip-otp.c b/drivers/nvmem/rockchip-otp.c
> index 7eb834516ab4..eb91e7a34051 100644
> --- a/drivers/nvmem/rockchip-otp.c
> +++ b/drivers/nvmem/rockchip-otp.c
> @@ -6,9 +6,12 @@
>   * Author: Finley Xiao <finley.xiao at rock-chips.com>
>   */
> 
> +#include <crypto/sha2.h>
>  #include <linux/clk.h>
>  #include <linux/delay.h>
>  #include <linux/device.h>
> +#include <linux/etherdevice.h>
> +#include <linux/hex.h>
>  #include <linux/io.h>
>  #include <linux/iopoll.h>
>  #include <linux/module.h>
> @@ -67,6 +70,8 @@
>  #define RK3588_AUTO_EN			BIT(0)
>  #define RK3588_RD_DONE			BIT(1)
> 
> +#define ROCKCHIP_CPUID_LEN		16
> +
>  struct rockchip_data {
>  	int size;
>  	int read_offset;
> @@ -319,6 +324,48 @@ static int rockchip_otp_read(void *context, unsigned int offset,
>  	return ret;
>  }
> 
> +/*
> + * Derive a MAC address from the CPU ID the same way Rockchip bootloaders do
> + * (see rockchip_setup_macaddr() in U-Boot's arch/arm/mach-rockchip/board.c),
> + * for consumers asking for one. Everyone else gets the CPU ID unchanged.
> + *
> + * The address takes the first six octets of the cell, which keeps the size of
> + * the CPU ID. Consumers only look at those, so the rest is left as it is.
> + *
> + * The cell argument is XORed into the last octet rather than added to the
> + * address, matching how U-Boot derives eth1addr from ethaddr.
> + */
> +static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
> +				 unsigned int offset, void *buf, size_t bytes)
> +{
> +	char cpuid[ROCKCHIP_CPUID_LEN * 2];
> +	u8 digest[SHA256_DIGEST_SIZE];
> +	u8 *mac = buf;
> +
> +	if (!id || strcmp(id, "mac-address"))
> +		return 0;
> +
> +	if (bytes != ROCKCHIP_CPUID_LEN || index < 0 || index > U8_MAX)
> +		return -EINVAL;
> +
> +	bin2hex(cpuid, buf, ROCKCHIP_CPUID_LEN);
> +	sha256(cpuid, sizeof(cpuid), digest);
> +
> +	memcpy(mac, digest, ETH_ALEN);
> +	mac[0] &= 0xfe;		/* clear the multicast bit */
> +	mac[0] |= 0x02;		/* set the locally administered bit */
> +	mac[5] ^= index;
> +
> +	return 0;
> +}
> +
> +static void rockchip_otp_fixup_dt_cell_info(struct nvmem_device *nvmem,
> +					    struct nvmem_cell_info *cell)
> +{
> +	if (of_device_is_compatible(cell->np, "rockchip,cpuid"))
> +		cell->read_post_process = rockchip_otp_cpuid_pp;
> +}
> +
>  static struct nvmem_config otp_config = {
>  	.name = "rockchip-otp",
>  	.owner = THIS_MODULE,
> @@ -328,6 +375,7 @@ static struct nvmem_config otp_config = {
>  	.stride = 1,
>  	.word_size = sizeof(u8),
>  	.reg_read = rockchip_otp_read,
> +	.fixup_dt_cell_info = rockchip_otp_fixup_dt_cell_info,
>  };
> 
>  static const char * const px30_otp_clocks[] = {
> 
> --
> 2.56.0
> 
> 

Perhaps "rockchip,cpuid" could include a mention of "mac" somewhere?

Tested-by: Ryan Leung <untilscour at protonmail.com> # RK3528 NanoPi R28S

I tested on FriendlyELEC NanoPi R28S (RK3528) with index of 100 and the MAC address was exactly as expected.

Looking forward to using this.

Best regards,
Ryan



More information about the Linux-rockchip mailing list