From patchwork Mon Aug 8 22:19:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 9269885 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8003560754 for ; Mon, 8 Aug 2016 22:22:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 70F3120748 for ; Mon, 8 Aug 2016 22:22:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 649D028161; Mon, 8 Aug 2016 22:22:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3594E28159 for ; Mon, 8 Aug 2016 22:22:05 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bWstr-0004P7-02; Mon, 08 Aug 2016 22:19:59 +0000 Received: from merlin.infradead.org ([2001:4978:20e::2]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bWstV-0004KS-Pb; Mon, 08 Aug 2016 22:19:37 +0000 Received: from geoff by merlin.infradead.org with local (Exim 4.85_2 #1 (Red Hat Linux)) id 1bWstR-0001PF-98; Mon, 08 Aug 2016 22:19:33 +0000 Message-Id: In-Reply-To: References: From: Geoff Levand Patch-Date: Mon, 8 Aug 2016 14:52:54 -0700 Subject: [PATCH v3 1/3] kexec: Add common device tree routines To: Simon Horman Date: Mon, 08 Aug 2016 22:19:33 +0000 X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pratyush Anand , AKASHI Takahiro , Mark Rutland , kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Common device tree routines that can be shared between all arches that have device tree support. Signed-off-by: Geoff Levand --- kexec/Makefile | 4 ++ kexec/dt-ops.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ kexec/dt-ops.h | 13 ++++++ 3 files changed, 162 insertions(+) create mode 100644 kexec/dt-ops.c create mode 100644 kexec/dt-ops.h diff --git a/kexec/Makefile b/kexec/Makefile index e2aee84..cc3f08b 100644 --- a/kexec/Makefile +++ b/kexec/Makefile @@ -73,6 +73,10 @@ dist += kexec/mem_regions.c kexec/mem_regions.h $(ARCH)_MEM_REGIONS = KEXEC_SRCS += $($(ARCH)_MEM_REGIONS) +dist += kexec/dt-ops.c kexec/dt-ops.h +$(ARCH)_DT_OPS = +KEXEC_SRCS += $($(ARCH)_DT_OPS) + include $(srcdir)/kexec/arch/alpha/Makefile include $(srcdir)/kexec/arch/arm/Makefile include $(srcdir)/kexec/arch/i386/Makefile diff --git a/kexec/dt-ops.c b/kexec/dt-ops.c new file mode 100644 index 0000000..915dbf5 --- /dev/null +++ b/kexec/dt-ops.c @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include + +#include "kexec.h" +#include "dt-ops.h" + +static const char n_chosen[] = "/chosen"; + +static const char p_bootargs[] = "bootargs"; +static const char p_initrd_start[] = "linux,initrd-start"; +static const char p_initrd_end[] = "linux,initrd-end"; + +int dtb_set_initrd(char **dtb, off_t *dtb_size, off_t start, off_t end) +{ + int result; + uint64_t value; + + dbgprintf("%s: start %jd, end %jd, size %jd (%jd KiB)\n", + __func__, (intmax_t)start, (intmax_t)end, + (intmax_t)(end - start), + (intmax_t)(end - start) / 1024); + + value = cpu_to_fdt64(start); + + result = dtb_set_property(dtb, dtb_size, n_chosen, p_initrd_start, + &value, sizeof(value)); + + if (result) + return result; + + value = cpu_to_fdt64(end); + + result = dtb_set_property(dtb, dtb_size, n_chosen, p_initrd_end, + &value, sizeof(value)); + + if (result) { + dtb_delete_property(*dtb, n_chosen, p_initrd_start); + return result; + } + + return 0; +} + +int dtb_set_bootargs(char **dtb, off_t *dtb_size, const char *command_line) +{ + return dtb_set_property(dtb, dtb_size, n_chosen, p_bootargs, + command_line, strlen(command_line) + 1); +} + +int dtb_set_property(char **dtb, off_t *dtb_size, const char *node, + const char *prop, const void *value, int value_len) +{ + int result; + int nodeoffset; + void *new_dtb; + int new_size; + + value_len = FDT_TAGALIGN(value_len); + + new_size = FDT_TAGALIGN(*dtb_size + fdt_node_len(node) + + fdt_prop_len(prop, value_len)); + + new_dtb = malloc(new_size); + + if (!new_dtb) { + dbgprintf("%s: malloc failed\n", __func__); + return -ENOMEM; + } + + result = fdt_open_into(*dtb, new_dtb, new_size); + + if (result) { + dbgprintf("%s: fdt_open_into failed: %s\n", __func__, + fdt_strerror(result)); + goto on_error; + } + + nodeoffset = fdt_path_offset(new_dtb, node); + + if (nodeoffset == -FDT_ERR_NOTFOUND) { + result = fdt_add_subnode(new_dtb, nodeoffset, node); + + if (result) { + dbgprintf("%s: fdt_add_subnode failed: %s\n", __func__, + fdt_strerror(result)); + goto on_error; + } + } else if (nodeoffset < 0) { + dbgprintf("%s: fdt_path_offset failed: %s\n", __func__, + fdt_strerror(nodeoffset)); + goto on_error; + } + + result = fdt_setprop(new_dtb, nodeoffset, prop, value, value_len); + + if (result) { + dbgprintf("%s: fdt_setprop failed: %s\n", __func__, + fdt_strerror(result)); + goto on_error; + } + + /* + * Can't call free on dtb since dtb may have been mmaped by + * slurp_file(). + */ + + result = fdt_pack(new_dtb); + + if (result) + dbgprintf("%s: Unable to pack device tree: %s\n", __func__, + fdt_strerror(result)); + + *dtb = new_dtb; + *dtb_size = fdt_totalsize(*dtb); + + return 0; + +on_error: + free(new_dtb); + return result; +} + +int dtb_delete_property(char *dtb, const char *node, const char *prop) +{ + int result; + int nodeoffset = fdt_path_offset(dtb, node); + + if (nodeoffset < 0) { + dbgprintf("%s: fdt_path_offset failed: %s\n", __func__, + fdt_strerror(nodeoffset)); + return nodeoffset; + } + + result = fdt_delprop(dtb, nodeoffset, prop); + + if (result) + dbgprintf("%s: fdt_delprop failed: %s\n", __func__, + fdt_strerror(nodeoffset)); + + return result; +} diff --git a/kexec/dt-ops.h b/kexec/dt-ops.h new file mode 100644 index 0000000..e70d15d --- /dev/null +++ b/kexec/dt-ops.h @@ -0,0 +1,13 @@ +#if !defined(KEXEC_DT_OPS_H) +#define KEXEC_DT_OPS_H + +#include + +int dtb_set_initrd(char **dtb, off_t *dtb_size, off_t start, off_t end); +int dtb_set_bootargs(char **dtb, off_t *dtb_size, const char *command_line); +int dtb_set_property(char **dtb, off_t *dtb_size, const char *node, + const char *prop, const void *value, int value_len); + +int dtb_delete_property(char *dtb, const char *node, const char *prop); + +#endif