From patchwork Mon Apr 15 09:32:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thierry Reding X-Patchwork-Id: 1085515 X-Patchwork-Delegate: twarren@nvidia.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="CGtugLfc"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 44jNbM1ZjNz9s0W for ; Mon, 15 Apr 2019 19:33:19 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id EC548C21E3B; Mon, 15 Apr 2019 09:33:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 1B86FC21E36; Mon, 15 Apr 2019 09:32:46 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id BFCFBC21DD7; Mon, 15 Apr 2019 09:32:42 +0000 (UTC) Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by lists.denx.de (Postfix) with ESMTPS id 6CF5DC21DB3 for ; Mon, 15 Apr 2019 09:32:42 +0000 (UTC) Received: by mail-wm1-f66.google.com with SMTP id r186so5751323wmf.1 for ; Mon, 15 Apr 2019 02:32:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pv8/5ns3Axnail6bPcQo5MR2HSMRStlKHNISj7ocRV4=; b=CGtugLfcR9PPFiPlU9jPICyyyGmbc4ZkDnBuzuXDaxpHlPFSFO6qphttO5z/nO0sXt JEeuvx4Bn26paY3btUXbQh8qxEaH0/YCtKAiCIdQXo8epn7/HgfXfKAckIMdoIjBEjB8 22pQ4lRB10T4gV/lL5L93cmFEO5KVjNL50UWU+AFB1TzAZZCP9A52uBrmTsx/lMpmSHs VduhWqV2ev1expYKAFKduXCNq4QbPGAKhek66pmwN5M8wHpnr41eXNTbuHf5ayjEPoCe 9ir7bHYBBomUUpRrD+K96Ts1buOuhpRwZj1u4h8vB/+Kr6ajvcnwztHAHGrYLJSv/inZ Vhgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pv8/5ns3Axnail6bPcQo5MR2HSMRStlKHNISj7ocRV4=; b=OS+sbaJEw8YwHHhKy1k+k6rB+CdLP839HMDBldlpExtF3ryV98c9UEa4S/G8cN7RVy h1QEFoX1rgT+/nPYU4Dqpmys5pGVlzdm9avG6vUMhVeRC8ok3rsD7CfXVIAZJPhp+3Fb N6f6JfHqgnc5sbn2jOzSYIonsW98xcFJ8XFfV+4wJcTlS4P6nt+dJbhXO6OWakN7L4c5 +GyChcbbFh+o3WfqW0NDS+Phk2axRHWGvR9onr5DrzaJ47gB34JQqsogROA1KKBd81EI P3YPJG4reR0sTCkyFc7s8fNZ7RRyeClPTFuSozMnoDYcdcR7S/Amg7Ev7RQO0IpndSea 8RwQ== X-Gm-Message-State: APjAAAX9MS0Z8ymjIEy0U0r+/s9U1Tep9WFi3I2agyx/Yx7+cRz6HV6p Cc8Q2V3mn1qN4liw310xmws= X-Google-Smtp-Source: APXvYqwCCICs/FrIhmV1wFA0u76+nS6H1tcj1HvU7PE0IQP/pAyx1T19ejCr1EyEwOIR5yJjZ4rY2A== X-Received: by 2002:a05:600c:2208:: with SMTP id z8mr21042186wml.89.1555320761988; Mon, 15 Apr 2019 02:32:41 -0700 (PDT) Received: from localhost (p2E5BE61D.dip0.t-ipconnect.de. [46.91.230.29]) by smtp.gmail.com with ESMTPSA id s189sm36506053wmf.45.2019.04.15.02.32.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 15 Apr 2019 02:32:41 -0700 (PDT) From: Thierry Reding To: Tom Warren , Simon Glass Date: Mon, 15 Apr 2019 11:32:13 +0200 Message-Id: <20190415093239.27509-2-thierry.reding@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415093239.27509-1-thierry.reding@gmail.com> References: <20190415093239.27509-1-thierry.reding@gmail.com> MIME-Version: 1.0 Cc: u-boot@lists.denx.de, Jon Hunter Subject: [U-Boot] [PATCH v5 01/27] fdtdec: Add fdtdec_set_ethernet_mac_address() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Thierry Reding This function can be used to set the local MAC address for the default Ethernet interface in its device tree node. The default interface is identified by the "ethernet" alias. One case where this is useful is for devices that store their MAC address in a custom location. Once extracted, board code can store the MAC address in U-Boot's control DTB so that it will automatically be used by the Ethernet uclass. Signed-off-by: Thierry Reding --- include/fdtdec.h | 24 ++++++++++++++++++++++++ lib/fdtdec.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/fdtdec.h b/include/fdtdec.h index fa8e34f6f960..e6c22dd5cd5c 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -996,6 +996,30 @@ int fdtdec_setup_memory_banksize_fdt(const void *blob); */ int fdtdec_setup_memory_banksize(void); +/** + * fdtdec_set_ethernet_mac_address() - set MAC address for default interface + * + * Looks up the default interface via the "ethernet" alias (in the /aliases + * node) and stores the given MAC in its "local-mac-address" property. This + * is useful on platforms that store the MAC address in a custom location. + * Board code can call this in the late init stage to make sure that the + * interface device tree node has the right MAC address configured for the + * Ethernet uclass to pick it up. + * + * Typically the FDT passed into this function will be U-Boot's control DTB. + * Given that a lot of code may be holding offsets to various nodes in that + * tree, this code will only set the "local-mac-address" property in-place, + * which means that it needs to exist and have space for the 6-byte address. + * This ensures that the operation is non-destructive and does not invalidate + * offsets that other drivers may be using. + * + * @param fdt FDT blob + * @param mac buffer containing the MAC address to set + * @param size size of MAC address + * @return 0 on success or a negative error code on failure + */ +int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size); + /** * fdtdec_set_phandle() - sets the phandle of a given node * diff --git a/lib/fdtdec.c b/lib/fdtdec.c index d0ba88897335..3ee786b57940 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -1261,6 +1261,35 @@ __weak void *board_fdt_blob_setup(void) } #endif +int fdtdec_set_ethernet_mac_address(void *fdt, const u8 *mac, size_t size) +{ + const char *path; + int offset, err; + + if (!is_valid_ethaddr(mac)) + return -EINVAL; + + path = fdt_get_alias(fdt, "ethernet"); + if (!path) + return 0; + + debug("ethernet alias found: %s\n", path); + + offset = fdt_path_offset(fdt, path); + if (offset < 0) { + debug("ethernet alias points to absent node %s\n", path); + return -ENOENT; + } + + err = fdt_setprop_inplace(fdt, offset, "local-mac-address", mac, size); + if (err < 0) + return err; + + debug("MAC address: %pM\n", mac); + + return 0; +} + static int fdtdec_init_reserved_memory(void *blob) { int na, ns, node, err;