From patchwork Mon Mar 18 23:51:24 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 228841 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 8CFB52C009B for ; Tue, 19 Mar 2013 10:54:32 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 09C644A171; Tue, 19 Mar 2013 00:54:31 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9JP7vsuOStNP; Tue, 19 Mar 2013 00:54:30 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 5D7984A0AB; Tue, 19 Mar 2013 00:53:27 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7F2A44A02F for ; Tue, 19 Mar 2013 00:53:23 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id M-WxNgHo3JE7 for ; Tue, 19 Mar 2013 00:53:21 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-qe0-f74.google.com (mail-qe0-f74.google.com [209.85.128.74]) by theia.denx.de (Postfix) with ESMTPS id 1CDC14A032 for ; Tue, 19 Mar 2013 00:53:19 +0100 (CET) Received: by mail-qe0-f74.google.com with SMTP id 9so640959qea.1 for ; Mon, 18 Mar 2013 16:53:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=b/8N/KjBnxGKYkaXWDuVIJoxK2iYTpFJVCmTtooD6DM=; b=esw4/AkacXXtLQScg7BbhgtV60S34XUd/sCazjRI36WHaEVvJuWEqEzh8psCe9MWXm ZZn8zA998j9ZCogV4X2mx+/IR09tLqsevc0gw2+PquE0DlfBFl9Uw48SbfftCNuZxtYc DU2PGSswg6sOqUxDPODrwWw+HeK5jYTu7+Irsefi3jaeOW/Q2tXoS2/9U5hdJPcJQdmN SoWYZuAQZJPtWxlF1ZLGcVtGjTW1Ve5fL4a+U3c10D+YkahX69QxzsSLw12ducgi91qA N3Og434e4XhnSbTcl3tqDomoexLR0amObRoNe4/OprFypHFZOfNp8E9oo3NTNvorb576 naJg== X-Received: by 10.58.228.66 with SMTP id sg2mr14335333vec.30.1363650798005; Mon, 18 Mar 2013 16:53:18 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id k21si2190078yhh.1.2013.03.18.16.53.17 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Mon, 18 Mar 2013 16:53:17 -0700 (PDT) Received: from kaka.mtv.corp.google.com (kaka.mtv.corp.google.com [172.22.73.79]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id D1F9731C1D7; Mon, 18 Mar 2013 16:53:17 -0700 (PDT) Received: by kaka.mtv.corp.google.com (Postfix, from userid 121222) id 87225160341; Mon, 18 Mar 2013 16:53:17 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Date: Mon, 18 Mar 2013 16:51:24 -0700 Message-Id: <1363650725-30459-5-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.8.1.3 In-Reply-To: <1363650725-30459-1-git-send-email-sjg@chromium.org> References: <1363650725-30459-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQkr1mn70X8ujCXKHp9Z5w0H35kOAKuZQTsH69UT1eRyj8YdCD9yCDEKwdjN9kAvm29YNcM0wUyZnvxBS46WIgVMQxF0EyIoMa3rgMLL5Rg7WLq4BTL/9uJ9nm/IbeVFiojXUz+j+onwRqxH5mAriiWnZpCp99I7Bzx7CjubdD5qAu+NuV9TjFuENm9nPmNrTS18u1aO Cc: Joel A Fernandes , Will Drewry , u-boot-review@google.com, Bill Richardson , Randall Spangler , Tom Rini , Vadim Bendebury , =?UTF-8?q?Andreas=20B=C3=A4ck?= , Kees Cook Subject: [U-Boot] [PATCH v2 04/45] libfdt: Add fdt_next_subnode() to permit easy subnode iteration X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This allows use to replace code like this: for (ndepth = 0, count = 0, noffset = fdt_next_node(fit, images_noffset, &ndepth); (noffset >= 0) && (ndepth > 0); noffset = fdt_next_node(fit, noffset, &ndepth)) { if (ndepth == 1) ... with: for (ndepth = 0, noffset = fdt_next_subnode(fit, image_noffset, &ndepth); noffset >= 0; noffset = fdt_next_subnode(fit, noffset, &ndepth)) { which is slightly better, and doesn't require two levels of indentation for code in the loop. Signed-off-by: Simon Glass --- Changes in v2: None include/libfdt.h | 17 +++++++++++++++++ lib/libfdt/fdt.c | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/libfdt.h b/include/libfdt.h index fc7f75b..50c90d6 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -136,6 +136,23 @@ uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset); int fdt_next_node(const void *fdt, int offset, int *depth); +/** + * fdt_next_subnode() - get offset of next direct child + * + * Set depth to 0, offset to parent, then call this function repeatedly + * to get direct subnodes of a parent node. + * + * @fdt: FDT blob + * @offset: Set this to offset of parent for the first call. For + * subsquent calls, pass in the value returns from the last + * call. + * @depth: Used internally to monitor depth - set this to 0 for the + * first call. + * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more + * children + */ +int fdt_next_subnode(const void *fdt, int offset, int *depth); + /**********************************************************************/ /* General functions */ /**********************************************************************/ diff --git a/lib/libfdt/fdt.c b/lib/libfdt/fdt.c index 387e354..cd86811 100644 --- a/lib/libfdt/fdt.c +++ b/lib/libfdt/fdt.c @@ -202,6 +202,18 @@ int fdt_next_node(const void *fdt, int offset, int *depth) return offset; } +int fdt_next_subnode(const void *fdt, int offset, int *depth) +{ + /* Loop until we find a direct child of the parent (depth == 1) */ + do { + offset = fdt_next_node(fdt, offset, depth); + if (offset < 0 || *depth < 1) + return -FDT_ERR_NOTFOUND; + } while (*depth > 1); + + return offset; +} + const char *_fdt_find_string(const char *strtab, int tabsize, const char *s) { int len = strlen(s) + 1;