From patchwork Tue Apr 5 16:46:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fu Wei Fu X-Patchwork-Id: 65108 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp575748lbc; Tue, 5 Apr 2016 09:47:17 -0700 (PDT) X-Received: by 10.140.33.229 with SMTP id j92mr20709623qgj.20.1459874836885; Tue, 05 Apr 2016 09:47:16 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id w9si15003785qhc.10.2016.04.05.09.47.16; Tue, 05 Apr 2016 09:47:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linaro-uefi-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linaro-uefi-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=linaro-uefi-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 70BD9618D2; Tue, 5 Apr 2016 16:47:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 8FA34617B2; Tue, 5 Apr 2016 16:47:11 +0000 (UTC) X-Original-To: linaro-uefi@lists.linaro.org Delivered-To: linaro-uefi@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 1C712618CE; Tue, 5 Apr 2016 16:47:08 +0000 (UTC) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by lists.linaro.org (Postfix) with ESMTPS id D194061831 for ; Tue, 5 Apr 2016 16:47:05 +0000 (UTC) Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BAE207F6A7; Tue, 5 Apr 2016 16:47:04 +0000 (UTC) Received: from magi-f22.redhat.com (vpn1-4-12.pek2.redhat.com [10.72.4.12]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u35Gl0N3021744; Tue, 5 Apr 2016 12:47:01 -0400 From: fu.wei@linaro.org To: xen-devel@lists.xensource.com, julien.grall@arm.com, sstabellini@kernel.org, dgdegra@tycho.nsa.gov, konrad.wilk@oracle.com Date: Wed, 6 Apr 2016 00:46:36 +0800 Message-Id: <1459874796-27052-1-git-send-email-fu.wei@linaro.org> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Cc: jcm@redhat.com, linaro-uefi@lists.linaro.org Subject: [Linaro-uefi] [PATCH v5] xen/arm64: check XSM Magic from the second unknown module. X-BeenThere: linaro-uefi@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: linaro-uefi-bounces@lists.linaro.org Sender: "Linaro-uefi" From: Fu Wei This patch adds a has_xsm_magic helper function for detecting XSM from the second unknown module. If Xen can't get the kind of module from compatible, we guess the kind of these unknowns respectively: (1) The first unknown must be kernel. (2) Detect the XSM Magic from the 2nd unknown: a. If it's XSM, set the kind as XSM, and that also means we won't load ramdisk; b. if it's not XSM, set the kind as ramdisk. So if user want to load ramdisk, it must be the 2nd unknown. We also detect the XSM Magic for the following unknowns, then set its kind according to the return value of has_xsm_magic. By this way, arm64 behavior can be compatible to x86 and can simplify multi-arch bootloader such as GRUB. Signed-off-by: Fu Wei Acked-by: Julien Grall --- Changelog: v5: Wrap "#include " up by #ifdef CONFIG_HAS_DEVICE_TREE Improve has_xsm_magic code. v4: http://lists.xen.org/archives/html/xen-devel/2016-04/msg00553.html Fix some code style and comments problems. Only check the Magic number. Re-order the code in process_multiboot_node to get the base address first, then the XSM Magic checking function only need to check if the Magic number is valid. Factor the XSM Magic checking code into an helper and re-use it in xsm_dt_policy_init. v3: http://lists.xen.org/archives/html/xen-devel/2016-03/msg03564.html Using memcmp instead of strncmp. Using "return 0;" instead of panic(); Improve some comments. v2: http://lists.xen.org/archives/html/xen-devel/2016-03/msg03543.html Using XEN_MAGIC macro instead of 0xf97cff8c : uint32_t selinux_magic = 0xf97cff8c; --> uint32_t xen_magic = XEN_MAGIC; Comment out the code(return 0 directly), if CONFIG_FLASK is not set. v1: http://lists.xen.org/archives/html/xen-devel/2016-03/msg02430.html The first upstream patch to xen-devel mailing lists. xen/arch/arm/bootfdt.c | 37 +++++++++++++++++++++++++------------ xen/include/xsm/xsm.h | 8 +++++++- xen/xsm/xsm_core.c | 27 +++++++++++++++++++++++++++ xen/xsm/xsm_policy.c | 8 ++------ 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/xen/arch/arm/bootfdt.c b/xen/arch/arm/bootfdt.c index 8a14015..d130633 100644 --- a/xen/arch/arm/bootfdt.c +++ b/xen/arch/arm/bootfdt.c @@ -14,6 +14,7 @@ #include #include #include +#include #include static bool_t __init device_tree_node_matches(const void *fdt, int node, @@ -175,6 +176,17 @@ static void __init process_multiboot_node(const void *fdt, int node, const char *cmdline; int len; + prop = fdt_get_property(fdt, node, "reg", &len); + if ( !prop ) + panic("node %s missing `reg' property\n", name); + + if ( len < dt_cells_to_size(address_cells + size_cells) ) + panic("fdt: node `%s': `reg` property length is too short\n", + name); + + cell = (const __be32 *)prop->data; + device_tree_get_reg(&cell, address_cells, size_cells, &start, &size); + if ( fdt_node_check_compatible(fdt, node, "xen,linux-zimage") == 0 || fdt_node_check_compatible(fdt, node, "multiboot,kernel") == 0 ) kind = BOOTMOD_KERNEL; @@ -186,7 +198,17 @@ static void __init process_multiboot_node(const void *fdt, int node, else kind = BOOTMOD_UNKNOWN; - /* Guess that first two unknown are kernel and ramdisk respectively. */ + /** + * Guess the kind of these first two unknowns respectively: + * (1) The first unknown must be kernel. + * (2) Detect the XSM Magic from the 2nd unknown: + * a. If it's XSM, set the kind as XSM, and that also means we + * won't load ramdisk; + * b. if it's not XSM, set the kind as ramdisk. + * So if user want to load ramdisk, it must be the 2nd unknown. + * We also detect the XSM Magic for the following unknowns, + * then set its kind according to the return value of has_xsm_magic. + */ if ( kind == BOOTMOD_UNKNOWN ) { switch ( kind_guess++ ) @@ -195,19 +217,10 @@ static void __init process_multiboot_node(const void *fdt, int node, case 1: kind = BOOTMOD_RAMDISK; break; default: break; } + if ( kind_guess > 1 && has_xsm_magic(start) ) + kind = BOOTMOD_XSM; } - prop = fdt_get_property(fdt, node, "reg", &len); - if ( !prop ) - panic("node %s missing `reg' property\n", name); - - if ( len < dt_cells_to_size(address_cells + size_cells) ) - panic("fdt: node `%s': `reg` property length is too short\n", - name); - - cell = (const __be32 *)prop->data; - device_tree_get_reg(&cell, address_cells, size_cells, &start, &size); - prop = fdt_get_property(fdt, node, "bootargs", &len); if ( prop ) { diff --git a/xen/include/xsm/xsm.h b/xen/include/xsm/xsm.h index 3afed70..803c7ea 100644 --- a/xen/include/xsm/xsm.h +++ b/xen/include/xsm/xsm.h @@ -745,6 +745,7 @@ extern int xsm_multiboot_policy_init(unsigned long *module_map, #ifdef CONFIG_HAS_DEVICE_TREE extern int xsm_dt_init(void); extern int xsm_dt_policy_init(void); +extern bool has_xsm_magic(paddr_t); #endif extern int register_xsm(struct xsm_operations *ops); @@ -771,7 +772,12 @@ static inline int xsm_dt_init(void) { return 0; } -#endif + +static inline bool has_xsm_magic(paddr_t start) +{ + return false; +} +#endif /* CONFIG_HAS_DEVICE_TREE */ #endif /* CONFIG_XSM */ diff --git a/xen/xsm/xsm_core.c b/xen/xsm/xsm_core.c index 5e432de..634ec98 100644 --- a/xen/xsm/xsm_core.c +++ b/xen/xsm/xsm_core.c @@ -19,6 +19,10 @@ #ifdef CONFIG_XSM +#ifdef CONFIG_HAS_DEVICE_TREE +#include +#endif + #define XSM_FRAMEWORK_VERSION "1.0.0" struct xsm_operations *xsm_ops; @@ -109,6 +113,29 @@ int __init xsm_dt_init(void) return ret; } + +/** + * has_xsm_magic - Check XSM Magic of the module header by phy address + * A XSM module has a special header + * ------------------------------------------------ + * uint magic | uint target_len | uchar target[8] | + * 0xf97cff8c | 8 | "XenFlask" | + * ------------------------------------------------ + * 0xf97cff8c is policy magic number (XSM_MAGIC). + * Here we only check the "magic" of the module. + */ +bool __init has_xsm_magic(paddr_t start) +{ + xsm_magic_t magic; + + if ( XSM_MAGIC ) + { + copy_from_paddr(&magic, start, sizeof(magic) ); + return ( magic == XSM_MAGIC ); + } + + return false; +} #endif int register_xsm(struct xsm_operations *ops) diff --git a/xen/xsm/xsm_policy.c b/xen/xsm/xsm_policy.c index b60d822..bde8015 100644 --- a/xen/xsm/xsm_policy.c +++ b/xen/xsm/xsm_policy.c @@ -79,7 +79,6 @@ int __init xsm_dt_policy_init(void) { struct bootmodule *mod = boot_module_find_by_kind(BOOTMOD_XSM); paddr_t paddr, len; - xsm_magic_t magic; if ( !mod || !mod->size ) return 0; @@ -87,12 +86,9 @@ int __init xsm_dt_policy_init(void) paddr = mod->start; len = mod->size; - copy_from_paddr(&magic, paddr, sizeof(magic)); - - if ( magic != XSM_MAGIC ) + if ( !has_xsm_magic(paddr) ) { - printk(XENLOG_ERR "xsm: Invalid magic for XSM blob got 0x%x " - "expected 0x%x\n", magic, XSM_MAGIC); + printk(XENLOG_ERR "xsm: Invalid magic for XSM blob\n"); return -EINVAL; }