From patchwork Wed Nov 12 16:59:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Grant Likely X-Patchwork-Id: 5291751 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 79DCB9F2F1 for ; Wed, 12 Nov 2014 21:23:14 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9110B201CE for ; Wed, 12 Nov 2014 21:23:13 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id AE76D201C0 for ; Wed, 12 Nov 2014 21:23:12 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XofLh-0004yY-Ba; Wed, 12 Nov 2014 21:21:09 +0000 Received: from mail-wi0-f173.google.com ([209.85.212.173]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XofLf-0004vf-3r for linux-arm-kernel@lists.infradead.org; Wed, 12 Nov 2014 21:21:08 +0000 Received: by mail-wi0-f173.google.com with SMTP id n3so6314584wiv.0 for ; Wed, 12 Nov 2014 13:20:45 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:subject:to:cc:in-reply-to:references :date:message-id; bh=Z40ktM1lDhGvhugqz4suZ9QhzD8pG88f63S+dUftYQ8=; b=URXG5DLZIrHgGpEQGijp50PQB3Omz2tXxY/hBGpoiJP9spMCV59oHeH8F5AGGMGwWm G3C5AIc27yPHNW7irESu8ywSVFecQGyNu2eboG9SwTJ5N4ITqYAO5sIvMrXCHCgwfvZX Ayk5011Fnfmo8prQq6PU4ceA4xHNbTWlF6iw95zgZ8hMkHR9QbpdR4pwJPz/Wdhy28y4 uy8GxUDqTzo1NraqnsZpxErEzffTBQ9vW7grXSpZlkOYjxdH3bEHE/XGCVHn+o7Y7OPj FYW9KfK9HN+w3cZt1EnNA7UrVSh4EiQbibQPyON4hCdqBijDxdJWrRSXJ98XGvDAb2KR Zr+Q== X-Gm-Message-State: ALoCoQk3v/7xcoXeBF5uqgKPSENTTf/RVVmveyj0WGWAgRvT50/yXAtUL0z1EBSRh6+XUfDfiaw3 X-Received: by 10.180.107.35 with SMTP id gz3mr54346544wib.38.1415827245193; Wed, 12 Nov 2014 13:20:45 -0800 (PST) Received: from trevor.secretlab.ca (host86-166-83-112.range86-166.btcentralplus.com. [86.166.83.112]) by mx.google.com with ESMTPSA id ft8sm9991551wjb.17.2014.11.12.13.20.43 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 Nov 2014 13:20:44 -0800 (PST) Received: by trevor.secretlab.ca (Postfix, from userid 1000) id 689CCC41BAB; Wed, 12 Nov 2014 16:59:35 +0000 (GMT) From: Grant Likely Subject: Re: [PATCH v4] of/fdt: export fdt blob as /sys/firmware/fdt To: Ard Biesheuvel , 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 In-Reply-To: <1415795308-14639-1-git-send-email-ard.biesheuvel@linaro.org> References: <1415795308-14639-1-git-send-email-ard.biesheuvel@linaro.org> Date: Wed, 12 Nov 2014 16:59:35 +0000 Message-Id: <20141112165935.689CCC41BAB@trevor.secretlab.ca> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141112_132107_301318_7F60FD2F X-CRM114-Status: GOOD ( 21.35 ) X-Spam-Score: 0.4 (/) Cc: Ard Biesheuvel X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00, DATE_IN_PAST_03_06, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Wed, 12 Nov 2014 13:28:28 +0100 , Ard Biesheuvel wrote: > 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 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: g. diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index be16ce2ffd69..fbe8f8d418f7 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 83a8e1154602..c7997e87b4b5 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,10 +23,20 @@ #include #include #include +#include #include /* for COMMAND_LINE_SIZE */ #include +static u32 of_fdt_crc32; + +static u32 of_fdt_get_crc32(void *fdt) +{ + static u32 const OF_FDT_CRC32_SEED = 0x04c11db7; + + return crc32_be(OF_FDT_CRC32_SEED, fdt, fdt_totalsize(fdt)); +} + /* * of_fdt_limit_memory - limit the number of regions in the /memory node * @limit: maximum entries @@ -1003,6 +1014,7 @@ bool __init early_init_dt_verify(void *params) /* Setup flat device-tree pointer */ initial_boot_params = params; + of_fdt_crc32 = of_fdt_get_crc32(initial_boot_params); return true; } @@ -1080,27 +1092,31 @@ 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 || fdt_check_header(initial_boot_params)) + return 0; - return 0; + if (of_fdt_crc32 != of_fdt_get_crc32(initial_boot_params)) { + pr_warn("fdt: not creating '/sys/firmware/fdt': CRC check failed"); + 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 */