From patchwork Fri Nov 14 17:05:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 40838 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 840C7244FC for ; Fri, 14 Nov 2014 17:06:17 +0000 (UTC) Received: by mail-wg0-f72.google.com with SMTP id a1sf878010wgh.11 for ; Fri, 14 Nov 2014 09:06:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=4jub6z4R0BJKGvLWWYUcROPKbhjujP5Goxb4cxT1UU0=; b=Mq/RdIRZMQdNfT7EZEh05hGFAgbLXZgHF3eoF7/5fRVhUn7WQojodFm7Su0a2CgQ5x K/5/BQ9dSjT4ljLr586yaoGZ36gbzYyH7oEoItNWkf79A9fjd6re4LKWZjMybVY62fL8 FAvYlkzhCClMxKxmbU7bjA0V30USWp37ldHI8QZEkbWyZX55JVnGYLOFUB4GBncMj6ne x/ca+vK6m4sOE1nO6o9+scOFn1bn6XjQxYd5cCpBi55PO+1ItrRJITfH2IgpHE5B08XQ vKUwTZD09usgT1V5Ozmyl50Qta8UWdkm4mKLAbIqL3EIFs4uhVKIpwxwFRS47aeTUHML bDJQ== X-Gm-Message-State: ALoCoQlcPn6cD2T1YJ/yfMGrz8HtpY6mItn9hBURRDnsOLgaqBM1xgeKqarkv3JCd6F+T9yHDVsw X-Received: by 10.152.8.67 with SMTP id p3mr1159273laa.4.1415984776821; Fri, 14 Nov 2014 09:06:16 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.197.100 with SMTP id it4ls984058lac.7.gmail; Fri, 14 Nov 2014 09:06:16 -0800 (PST) X-Received: by 10.112.63.70 with SMTP id e6mr2836631lbs.93.1415984776540; Fri, 14 Nov 2014 09:06:16 -0800 (PST) Received: from mail-la0-f52.google.com (mail-la0-f52.google.com. [209.85.215.52]) by mx.google.com with ESMTPS id wt3si42323389lbb.44.2014.11.14.09.06.16 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 14 Nov 2014 09:06:16 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) client-ip=209.85.215.52; Received: by mail-la0-f52.google.com with SMTP id pv20so15103021lab.39 for ; Fri, 14 Nov 2014 09:06:16 -0800 (PST) X-Received: by 10.152.87.100 with SMTP id w4mr9143326laz.27.1415984776174; Fri, 14 Nov 2014 09:06:16 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.184.201 with SMTP id ew9csp797717lbc; Fri, 14 Nov 2014 09:06:14 -0800 (PST) X-Received: by 10.68.255.195 with SMTP id as3mr11673533pbd.38.1415984773885; Fri, 14 Nov 2014 09:06:13 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id yl8si14651779pab.171.2014.11.14.09.06.13 for ; Fri, 14 Nov 2014 09:06:13 -0800 (PST) Received-SPF: none (google.com: devicetree-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161327AbaKNRGL (ORCPT + 4 others); Fri, 14 Nov 2014 12:06:11 -0500 Received: from mail-wi0-f180.google.com ([209.85.212.180]:52149 "EHLO mail-wi0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935233AbaKNRGJ (ORCPT ); Fri, 14 Nov 2014 12:06:09 -0500 Received: by mail-wi0-f180.google.com with SMTP id hi2so3321809wib.7 for ; Fri, 14 Nov 2014 09:06:07 -0800 (PST) X-Received: by 10.180.86.198 with SMTP id r6mr9357452wiz.29.1415984767762; Fri, 14 Nov 2014 09:06:07 -0800 (PST) Received: from ards-macbook-pro.local (cag06-7-83-153-85-71.fbx.proxad.net. [83.153.85.71]) by mx.google.com with ESMTPSA id d14sm40335963wjz.26.2014.11.14.09.06.05 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 14 Nov 2014 09:06:06 -0800 (PST) From: Ard Biesheuvel To: grant.likely@linaro.org, leif.lindholm@linaro.org, geoff.levand@linaro.org, mark.rutland@arm.com, rob.herring@linaro.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel Subject: [PATCH v5.5] of/fdt: export fdt blob as /sys/firmware/fdt Date: Fri, 14 Nov 2014 18:05:35 +0100 Message-Id: <1415984735-32388-1-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 Sender: devicetree-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: devicetree@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Create a new /sys entry '/sys/firmware/fdt' to export the FDT blob that was passed to the kernel by the bootloader. This allows userland applications such as kexec to access the raw binary. The fact that this node does not reside under /sys/firmware/device-tree is deliberate: FDT is also used on arm64 UEFI/ACPI systems to communicate just the UEFI and ACPI entry points, but the FDT is never unflattened and used to configure the system. A CRC32 checksum is calculated over the entire FDT blob, and verified at late_initcall time. The sysfs entry is instantiated only if the checksum is valid, i.e., if the FDT blob has not been modified in the mean time. Otherwise, a warning is printed. Signed-off-by: Ard Biesheuvel --- v5.5: based on Grant's changes """ Actually, I made a couple of changes when I merged it. I removed the older debugfs interface since it overlaps, and I added tests for initial_boot_params to make sure it doesn't try to run on an invalid FDT """ but I removed the second fdt_check_header() again in the late init code, as it would result in a corrupted FDT to be silently ignored. Note that initial_boot_params can only be set if fdt_check_header() succeeded the first time, so a failure occurring the second time should produce a warning, and the CRC check will catch it anyway. The CRC check itself is fixed to use the API as it is supposed to. I also moved the CRC variable definition inside the #ifdef OF_EARLY_FLATTREE region. v4: use pr_warn() instead of WARN() v3: keep checksum instead of copying the entire blob, and WARN on mismatch drivers/of/Kconfig | 1 + drivers/of/fdt.c | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index 1a13f5b722c5..0348c208343c 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig @@ -23,6 +23,7 @@ config OF_FLATTREE bool select DTC select LIBFDT + select CRC32 config OF_EARLY_FLATTREE bool diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index d1ffca8b34ea..2bbda0775f57 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -9,6 +9,7 @@ * version 2 as published by the Free Software Foundation. */ +#include #include #include #include @@ -22,6 +23,7 @@ #include #include #include +#include #include /* for COMMAND_LINE_SIZE */ #include @@ -425,6 +427,8 @@ void *initial_boot_params; #ifdef CONFIG_OF_EARLY_FLATTREE +static u32 of_fdt_crc32; + /** * res_mem_reserve_reg() - reserve all memory described in 'reg' property */ @@ -996,6 +1000,8 @@ bool __init early_init_dt_verify(void *params) /* Setup flat device-tree pointer */ initial_boot_params = params; + of_fdt_crc32 = crc32_be(~0, initial_boot_params, + fdt_totalsize(initial_boot_params)); /* check device tree validity */ if (fdt_check_header(params)) { @@ -1080,27 +1086,32 @@ void __init unflatten_and_copy_device_tree(void) unflatten_device_tree(); } -#if defined(CONFIG_DEBUG_FS) && defined(DEBUG) -static struct debugfs_blob_wrapper flat_dt_blob; - -static int __init of_flat_dt_debugfs_export_fdt(void) +#ifdef CONFIG_SYSFS +static ssize_t of_fdt_raw_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buf, loff_t off, size_t count) { - struct dentry *d = debugfs_create_dir("device-tree", NULL); - - if (!d) - return -ENOENT; + memcpy(buf, initial_boot_params + off, count); + return count; +} - flat_dt_blob.data = initial_boot_params; - flat_dt_blob.size = fdt_totalsize(initial_boot_params); +static int __init of_fdt_raw_init(void) +{ + static struct bin_attribute of_fdt_raw_attr = + __BIN_ATTR(fdt, S_IRUSR, of_fdt_raw_read, NULL, 0); - d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR, - d, &flat_dt_blob); - if (!d) - return -ENOENT; + if (!initial_boot_params) + return 0; - return 0; + if (of_fdt_crc32 != crc32_be(~0, initial_boot_params, + fdt_totalsize(initial_boot_params))) { + pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed\n"); + return 0; + } + of_fdt_raw_attr.size = fdt_totalsize(initial_boot_params); + return sysfs_create_bin_file(firmware_kobj, &of_fdt_raw_attr); } -module_init(of_flat_dt_debugfs_export_fdt); +late_initcall(of_fdt_raw_init); #endif #endif /* CONFIG_OF_EARLY_FLATTREE */