From patchwork Thu Aug 16 20:34:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 144449 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2513968ljj; Thu, 16 Aug 2018 13:34:26 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzuEuKOAWzIYLa9FTG1/VQdA5FONSQ8cwJxdvHQKq9O1F/UDXDeiUW5DX9o8jHBGak4IO+C X-Received: by 2002:a50:f098:: with SMTP id v24-v6mr39055283edl.90.1534451666876; Thu, 16 Aug 2018 13:34:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534451666; cv=none; d=google.com; s=arc-20160816; b=Z4ju3/00Bbjh0vxostwUHnGp6L+1D+PbeaRrlhLA6hFAIePgcyEFQfhKJAMG7D6lO0 fuoa8kn2SeRZSDY8XJQ0d5JkJXgQe5tdnioUc1mWItJb5i/mEDLsAq/rJgnvf4Fdi3Er tMKq5yWj67z9lV2W5plgqp8+iJAr1Ten/sm6Om+rONcVuoT2CatkUCgBL34fj43LG8DZ EgQDxN4hv3BztP9fHF25e6o8u4YCv2CLMdVwHv4ZF5VxQBRgbRN4DH6aZrmyNy8OJnd5 cgq8lMq63x/WuhaYmSyBVAaSPw1Lzox4G43u18vg800I7NmAjUNL36UDMmM+wTTWy2Eu 3QXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:message-id:date:to:from :dkim-signature:arc-authentication-results; bh=FajzuuQN58F8WAGxjNjC9HDaEDxKTAIXHC9+Bk/lfXM=; b=DXx1N97FV6P9x6+B6S7fJ37AXdfD/pa3fsg9VNW++Imo4yPKcwJb9zcVQuFlXQ2pWY VAcXdktxovYtigL/s4Jrax1DQ0ewM78shVlsFvw2FGyiTDTk70DodsNOOc5ccJiUcsWJ cASoOBNPrmjDN3gowIE7f5GiDLZfskima+dX1Uk/YQbEZJGmpurt2YV4uBjtSKTHq90B D4Ox8AVsAAVq5pO14agtyNj5khx4ajYFOLJhJKXv0yWI6n51D0jm8HhMVTTqSh02O6UQ SSrice0kEwsj32JXZ+RCHlCKBXM86FQKn4HVmWN+4jLXxK/VrfKE7AAJtHpL/+LWmI1l 2AiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b="EEr/1VVe"; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id z42-v6si1847874ede.224.2018.08.16.13.34.25; Thu, 16 Aug 2018 13:34:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b="EEr/1VVe"; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id 353CAC21E13; Thu, 16 Aug 2018 20:34:20 +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=RCVD_IN_MSPIKE_H2, 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 EC1ADC21C57; Thu, 16 Aug 2018 20:34:17 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 0F243C21C57; Thu, 16 Aug 2018 20:34:15 +0000 (UTC) Received: from mail-lf1-f68.google.com (mail-lf1-f68.google.com [209.85.167.68]) by lists.denx.de (Postfix) with ESMTPS id 91862C21C38 for ; Thu, 16 Aug 2018 20:34:15 +0000 (UTC) Received: by mail-lf1-f68.google.com with SMTP id j8-v6so4355359lfb.4 for ; Thu, 16 Aug 2018 13:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=I2OUxI1oH0x7w6ssavSCtMWBMW7Ht/ibdBj+xTu5NKA=; b=EEr/1VVeBEIBPof15Ccp36YLi0VBnKraU7Azdbh89xP6PjOWbS6QDIUY8y4p+kYdcj wRkEXqDXOnmJ8/yxMJ/UBXVBsZAnXkvZH/RnxZZ/q80Bmw+nOBd22mxNmCFIEQ79RfNs agVOP0tJjvKbNZcvLsrGVv1nwOStrKqzG3p7s= 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; bh=I2OUxI1oH0x7w6ssavSCtMWBMW7Ht/ibdBj+xTu5NKA=; b=OlxWUBusMHvvmY7pKDalu9dlxHwVWg1qn0LEcEpxtcvVa1VEv5N3TiyBNAOEIYn9/X +e4BvebrmeXiRIQG4ltkHWSkqOPJ2FG9vOphrhnTVkFEBrBQDWiF+3JvM4qRIyUZbmWY R/g/xKzPtyK5Qh5J2dFQaBFYp0EznC1ME6Kkt8gpLLT77hzeAYbdzHDkX5A+GlDmzklM vWMLkl+KS5bkovB1fxya9lFuWZJDugCzf6OoGg4FUgmwDcsPmJeczjR8v3s8tuhY0nPw qmQvhAwE7BdXrZ2cON1+lmisuwBa+WJb7cViwskDe176aEZFuVpUkuLqk5oQO++7UHZt eJEQ== X-Gm-Message-State: AOUpUlEATkbJOlaxXZzPvsSSaZAWdtJeDL90TbH9hQzUhzsOOudBY9fV m/hSAY9UZpL6K0sNf92rGhKfXDdpZ8o= X-Received: by 2002:a19:4c57:: with SMTP id z84-v6mr20878509lfa.67.1534451654604; Thu, 16 Aug 2018 13:34:14 -0700 (PDT) Received: from localhost ([195.238.92.132]) by smtp.gmail.com with ESMTPSA id u84-v6sm49617lff.35.2018.08.16.13.34.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 16 Aug 2018 13:34:13 -0700 (PDT) From: Sam Protsenko To: u-boot@lists.denx.de Date: Thu, 16 Aug 2018 23:34:12 +0300 Message-Id: <20180816203413.1598-1-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.18.0 Cc: Tom Rini , Sandeep Patil , Praneeth Bajjuri , Alistair Strachan Subject: [U-Boot] [PATCH v4 1/2] common: Add support for Android DT image 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: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Android documentation recommends new image format for storing DTB/DTBO files: [1]. To support that format, this patch adds helper functions for Android DTB/DTBO format. In image-android-dt.* files you can find helper functions to work with Android DT image format, such us routines for: - printing the dump of image structure - getting the address and size of desired dtb/dtbo file This patch uses dt_table.h file, that was added in commit 643cefa4d848 ("Import Android's dt_table.h for DT image format") by Alex Deymo. [1] https://source.android.com/devices/architecture/dto/partitions Signed-off-by: Sam Protsenko Reviewed-by: Tom Rini --- Changes in v4: - fix SPDX tags - renamed .reserved[] to .version (just reflect changes in dt_image.h) Changes in v3: - dropped include/dt_image.h (was sent in another patch by Alex Deymo) - rebased on top of last master common/image-android-dt.c | 156 +++++++++++++++++++++++++++++++++++++ include/image-android-dt.h | 20 +++++ 2 files changed, 176 insertions(+) create mode 100644 common/image-android-dt.c create mode 100644 include/image-android-dt.h diff --git a/common/image-android-dt.c b/common/image-android-dt.c new file mode 100644 index 0000000000..c0683ee70f --- /dev/null +++ b/common/image-android-dt.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * (C) Copyright 2018 Linaro Ltd. + * Sam Protsenko + */ + +#include +#include +#include +#include +#include + +/** + * Check if image header is correct. + * + * @param hdr_addr Start address of DT image + * @return true if header is correct or false if header is incorrect + */ +bool android_dt_check_header(ulong hdr_addr) +{ + const struct dt_table_header *hdr; + u32 magic; + + hdr = map_sysmem(hdr_addr, sizeof(*hdr)); + magic = fdt32_to_cpu(hdr->magic); + unmap_sysmem(hdr); + + return magic == DT_TABLE_MAGIC; +} + +/** + * Get the address of FDT (dtb or dtbo) in memory by its index in image. + * + * @param hdr_addr Start address of DT image + * @param index Index of desired FDT in image (starting from 0) + * @param[out] addr If not NULL, will contain address to specified FDT + * @param[out] size If not NULL, will contain size of specified FDT + * + * @return true on success or false on error + */ +bool android_dt_get_fdt_by_index(ulong hdr_addr, u32 index, ulong *addr, + u32 *size) +{ + const struct dt_table_header *hdr; + const struct dt_table_entry *e; + u32 entry_count, entries_offset, entry_size; + ulong e_addr; + u32 dt_offset, dt_size; + + hdr = map_sysmem(hdr_addr, sizeof(*hdr)); + entry_count = fdt32_to_cpu(hdr->dt_entry_count); + entries_offset = fdt32_to_cpu(hdr->dt_entries_offset); + entry_size = fdt32_to_cpu(hdr->dt_entry_size); + unmap_sysmem(hdr); + + if (index > entry_count) { + printf("Error: index > dt_entry_count (%u > %u)\n", index, + entry_count); + return false; + } + + e_addr = hdr_addr + entries_offset + index * entry_size; + e = map_sysmem(e_addr, sizeof(*e)); + dt_offset = fdt32_to_cpu(e->dt_offset); + dt_size = fdt32_to_cpu(e->dt_size); + unmap_sysmem(e); + + if (addr) + *addr = hdr_addr + dt_offset; + if (size) + *size = dt_size; + + return true; +} + +#if !defined(CONFIG_SPL_BUILD) +static void android_dt_print_fdt_info(const struct fdt_header *fdt) +{ + u32 fdt_size; + int root_node_off; + const char *compatible = NULL; + + fdt_size = fdt_totalsize(fdt); + root_node_off = fdt_path_offset(fdt, "/"); + if (root_node_off < 0) { + printf("Error: Root node not found\n"); + } else { + compatible = fdt_getprop(fdt, root_node_off, "compatible", + NULL); + } + + printf(" (FDT)size = %d\n", fdt_size); + printf(" (FDT)compatible = %s\n", + compatible ? compatible : "(unknown)"); +} + +/** + * Print information about DT image structure. + * + * @param hdr_addr Start address of DT image + */ +void android_dt_print_contents(ulong hdr_addr) +{ + const struct dt_table_header *hdr; + u32 entry_count, entries_offset, entry_size; + u32 i; + + hdr = map_sysmem(hdr_addr, sizeof(*hdr)); + entry_count = fdt32_to_cpu(hdr->dt_entry_count); + entries_offset = fdt32_to_cpu(hdr->dt_entries_offset); + entry_size = fdt32_to_cpu(hdr->dt_entry_size); + + /* Print image header info */ + printf("dt_table_header:\n"); + printf(" magic = %08x\n", fdt32_to_cpu(hdr->magic)); + printf(" total_size = %d\n", fdt32_to_cpu(hdr->total_size)); + printf(" header_size = %d\n", fdt32_to_cpu(hdr->header_size)); + printf(" dt_entry_size = %d\n", entry_size); + printf(" dt_entry_count = %d\n", entry_count); + printf(" dt_entries_offset = %d\n", entries_offset); + printf(" page_size = %d\n", fdt32_to_cpu(hdr->page_size)); + printf(" version = %d\n", fdt32_to_cpu(hdr->version)); + + unmap_sysmem(hdr); + + /* Print image entries info */ + for (i = 0; i < entry_count; ++i) { + const ulong e_addr = hdr_addr + entries_offset + i * entry_size; + const struct dt_table_entry *e; + const struct fdt_header *fdt; + u32 dt_offset, dt_size; + u32 j; + + e = map_sysmem(e_addr, sizeof(*e)); + dt_offset = fdt32_to_cpu(e->dt_offset); + dt_size = fdt32_to_cpu(e->dt_size); + + printf("dt_table_entry[%d]:\n", i); + printf(" dt_size = %d\n", dt_size); + printf(" dt_offset = %d\n", dt_offset); + printf(" id = %08x\n", fdt32_to_cpu(e->id)); + printf(" rev = %08x\n", fdt32_to_cpu(e->rev)); + for (j = 0; j < 4; ++j) { + printf(" custom[%d] = %08x\n", j, + fdt32_to_cpu(e->custom[j])); + } + + unmap_sysmem(e); + + /* Print FDT info for this entry */ + fdt = map_sysmem(hdr_addr + dt_offset, sizeof(*fdt)); + android_dt_print_fdt_info(fdt); + unmap_sysmem(fdt); + } +} +#endif diff --git a/include/image-android-dt.h b/include/image-android-dt.h new file mode 100644 index 0000000000..9a3aa8fa30 --- /dev/null +++ b/include/image-android-dt.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * (C) Copyright 2018 Linaro Ltd. + * Sam Protsenko + */ + +#ifndef IMAGE_ANDROID_DT_H +#define IMAGE_ANDROID_DT_H + +#include + +bool android_dt_check_header(ulong hdr_addr); +bool android_dt_get_fdt_by_index(ulong hdr_addr, u32 index, ulong *addr, + u32 *size); + +#if !defined(CONFIG_SPL_BUILD) +void android_dt_print_contents(ulong hdr_addr); +#endif + +#endif /* IMAGE_ANDROID_DT_H */