From patchwork Wed Jun 6 14:06:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Protsenko X-Patchwork-Id: 137831 Delivered-To: patch@linaro.org Received: by 2002:a2e:970d:0:0:0:0:0 with SMTP id r13-v6csp847787lji; Wed, 6 Jun 2018 07:07:21 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLxLhTqnHD+Vbio70ogYBvu5QTIFW22W5OoAMfkRHw7zuAYzMuYx2rqyWhDi9VGMpH+6MUb X-Received: by 2002:a50:aca4:: with SMTP id x33-v6mr3907179edc.270.1528294041559; Wed, 06 Jun 2018 07:07:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528294041; cv=none; d=google.com; s=arc-20160816; b=WToPsbBHswDPrbHoCSNN1sAlRjhN5DZ/JzU8zSA4tCHA5+K0/4bq2i+OF6ByclVuy0 bPg88ASJhpzFWcqXwCceukOhPr9ng3IUYrrbdk235+rWeGkCjmuIDd/aPkHMB2dgnny5 wjTnZp+iAdrX6m7wbAeW+tnYeS4+OO0+S9efIg0fbXKSZPXLf7xoQfQiNooxcSnoDXV/ gzKyO1raAizIw1aTvocrc3Hq63X6hHSWh+viQDkFS32Jb8ZWpDK2RBYMRMR3irA4tEC/ bp8mGGoYgPf9yzV55spw2+caPE/UjAKlurP03one+QrM0MelAKrSwajJw5DiTBAEGOvU gT9g== 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=UpoDDqfoJyYIDSkSt7oV1tkvJkoqmIeTK+jYdms9V1Y=; b=D7izQ6DLhU3+Q0AwKe+TSBH23GWQf4gbvvee/3lq0K89cTed8D98084h4qRhQVTlwp 01rdLXSJWPNVbpscz5FzElYXDULtjYJNmgzPJOIBbRi2IgmQy5gf/uUuSFzut/Ksttnm x6ql3fNTvXTAu6b2gZR2yBvr/KIiyHS0bk9gzWxDqkJDC/0D0Qu9WHn+4wraEZiUQDE4 g0kCTGzzoRWq1/sjhuVK/pvCPDfPWZKbA6ggeko3SwBhs/yoJNpCRXNJ1v+KE9JuTrCM W6WzBR8eVxS0LLJ8zih2kB/3/YksCg5H2b+xYe0A1dm9XgZwq2veDRVSkRDRFO3vv4Uq 2teQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=kmnjkEfl; 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 a13-v6si3705026edm.339.2018.06.06.07.07.19; Wed, 06 Jun 2018 07:07:21 -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=kmnjkEfl; 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 26888C2202D; Wed, 6 Jun 2018 14:07:16 +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_H3, RCVD_IN_MSPIKE_WL, 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 6B7BBC21FDA; Wed, 6 Jun 2018 14:07:14 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 4DBC5C22026; Wed, 6 Jun 2018 14:06:57 +0000 (UTC) Received: from mail-lf0-f67.google.com (mail-lf0-f67.google.com [209.85.215.67]) by lists.denx.de (Postfix) with ESMTPS id 74022C22007 for ; Wed, 6 Jun 2018 14:06:57 +0000 (UTC) Received: by mail-lf0-f67.google.com with SMTP id j13-v6so9308739lfb.13 for ; Wed, 06 Jun 2018 07:06:57 -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=p1cO/UWA/qkL07AffdDCVAZpDCKr5LeW6rPU6xLP2gU=; b=kmnjkEfllzeu7oImr8K5IHT4KA2xZCeW4uIIywarFKK1N7adojAD05v69tggk4zsMq tOFa0QGDLg6M8x5483sbMJKpKHBWsP23bMn45fTndc7PZKpTdvPS8tTcSDgVpeT1Ctbc 7RZkX119kZ9Yn87KEhtdZonUQ5ZyAv57qnLEc= 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=p1cO/UWA/qkL07AffdDCVAZpDCKr5LeW6rPU6xLP2gU=; b=DiYo5JSx0toQBHkReWzbyaOkPpqpcgv8Iyw0YViC0LIZN2pA2Vk6C4sk0h0GJUIFG9 GxpXvzVAvYeil2C3Kpe2BRLOSJrgEF1pK/GJjZgK07eezD7/vXGwGqHWbC0Sfq2SOkRs Sn9O5xlFqJxmh0MRRzT7KkFw5h+9wIOfyTYgfO1tbO0L1FoTY8fRkHbqHN7WHH9trQPj tRE131vT8gb6Bb1nTslX0FRvLyu0IqDbRWlejr9/3hnXDeUeJ7gFpvu/okwWrApXRNh1 p8FAbIyKLOJM4geE3nrKQatGrKoXUHGsnO11Gm9QbNOiuqm11pmRWMJCzggrt6a7y5eH xWDQ== X-Gm-Message-State: APt69E0G2Nj2lDEPP46yHFJTawZOQYO4YojCP92Hvyoh8y+ETWTPFD9Z 45B1IyCMd22dI5XwHkEq6eVw0d/SwgI= X-Received: by 2002:a2e:3c0e:: with SMTP id j14-v6mr2383274lja.149.1528294016500; Wed, 06 Jun 2018 07:06:56 -0700 (PDT) Received: from localhost ([195.238.92.132]) by smtp.gmail.com with ESMTPSA id a18-v6sm5140275ljf.17.2018.06.06.07.06.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 06 Jun 2018 07:06:55 -0700 (PDT) From: Sam Protsenko To: u-boot@lists.denx.de Date: Wed, 6 Jun 2018 17:06:53 +0300 Message-Id: <20180606140654.17994-1-semen.protsenko@linaro.org> X-Mailer: git-send-email 2.17.0 Cc: Praneeth Bajjuri , Tom Rini Subject: [U-Boot] [PATCH v3 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 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 --- 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 | 157 +++++++++++++++++++++++++++++++++++++ include/image-android-dt.h | 21 +++++ 2 files changed, 178 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..9b7683faab --- /dev/null +++ b/common/image-android-dt.c @@ -0,0 +1,157 @@ +/* + * (C) Copyright 2018 Linaro Ltd. + * Sam Protsenko + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#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(" reserved[0] = %08x\n", fdt32_to_cpu(hdr->reserved[0])); + + 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..08b810d461 --- /dev/null +++ b/include/image-android-dt.h @@ -0,0 +1,21 @@ +/* + * (C) Copyright 2018 Linaro Ltd. + * Sam Protsenko + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#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 */