mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eugeniy Paltsev <eugeniy.paltsev@synopsys.com>
To: linux-snps-arc@lists.infradead.org,
	Vineet Gupta <vineet.gupta1@synopsys.com>
Cc: linux-kernel@vger.kernel.org,
	Alexey Brodkin <alexey.brodkin@synopsys.com>,
	Corentin Labbe <clabbe@baylibre.com>,
	khilman@baylibre.com,
	Eugeniy Paltsev <eugeniy.paltsev@synopsys.com>
Subject: [RFC 2/2] ARC: U-boot: check magic number passed from u-boot
Date: Wed,  6 Feb 2019 20:22:28 +0300	[thread overview]
Message-ID: <20190206172228.9261-3-Eugeniy.Paltsev@synopsys.com> (raw)
In-Reply-To: <20190206172228.9261-1-Eugeniy.Paltsev@synopsys.com>

In case of devboards we really often disable bootloader and load
Linux image in memory via JTAG. In case of using kernel with
CONFIG_ARC_UBOOT_SUPPORT enabled we may crash as we will try to
interpret some junk in a registers as a pointers to bootargs/etc
which aren't set by anyone in case of JTAG using.

Try to make it much less possible by check magic number and
'U-boot - kernel' ABI revision number passed from U-boot.
Ignore U-boot arguments if we got wrong magic number or unknown
ABI revision.

Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
---
 arch/arc/kernel/head.S  |  5 ++++-
 arch/arc/kernel/setup.c | 26 ++++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/arch/arc/kernel/head.S b/arch/arc/kernel/head.S
index 7095055bb874..3fb88ec62bc7 100644
--- a/arch/arc/kernel/head.S
+++ b/arch/arc/kernel/head.S
@@ -92,10 +92,13 @@ ENTRY(stext)
 
 #ifdef CONFIG_ARC_UBOOT_SUPPORT
 	; Uboot - kernel ABI
+	;    r1 = bits [31:8] magic number, bits [7:0] uboot-kernel ABI revision
+	; ABI revision 0:
 	;    r0 = [0] No uboot interaction, [1] cmdline in r2, [2] DTB in r2
-	;    r1 = magic number (board identity, unused as of now
 	;    r2 = pointer to uboot provided cmdline or external DTB in mem
 	; These are handled later in handle_uboot_args()
+	st      r1, [@uboot_rev_magic]
+	mov	r1, 0			; errase magic from the register
 	st	r0, [@uboot_tag]
 	st	r2, [@uboot_arg]
 #endif
diff --git a/arch/arc/kernel/setup.c b/arch/arc/kernel/setup.c
index 7edb35c26322..868dda3d4b43 100644
--- a/arch/arc/kernel/setup.c
+++ b/arch/arc/kernel/setup.c
@@ -36,6 +36,7 @@ unsigned int intr_to_DE_cnt;
 
 /* Part of U-boot ABI: see head.S */
 int __initdata uboot_tag;
+int __initdata uboot_rev_magic;
 char __initdata *uboot_arg;
 
 const struct machine_desc *machine_desc;
@@ -467,6 +468,10 @@ static inline bool is_kernel(unsigned long addr)
 	return addr >= (unsigned long)_stext && addr <= (unsigned long)_end;
 }
 
+#define UBOOT_MAGIC_VALUE		0x567890
+#define UBOOT_MAGIC_GET(x)		(((x) & GENMASK(31, 8)) >> 8)
+#define UBOOT_REVISION_GET(x)		((x) & GENMASK(7, 0))
+
 /* uboot_tag values for U-boot - kernel ABI revisions 0+; see head.S */
 #define UBOOT_REV0P_TAG_NONE		0
 #define UBOOT_REV0P_TAG_CMDLINE		1
@@ -478,6 +483,25 @@ void __init handle_uboot_args(void)
 	bool use_embedded_dtb = true;
 
 #ifdef CONFIG_ARC_UBOOT_SUPPORT
+	/* check that we got correct magic */
+	if (UBOOT_MAGIC_GET(uboot_rev_magic) != UBOOT_MAGIC_VALUE) {
+		pr_warn("Invalid magic '%06lx' is passed from uboot, uboot args ingnored\n",
+			UBOOT_MAGIC_GET(uboot_rev_magic));
+
+		goto ignore_uboot_args;
+	}
+
+	/*
+	 * check that we know this U-boot args ABI revision.
+	 * as for today we only have one revision - '0'.
+	 */
+	if (UBOOT_REVISION_GET(uboot_rev_magic) != 0) {
+		pr_warn("Unknown args revision '%02lx' is passed from uboot, uboot args ingnored\n",
+			UBOOT_REVISION_GET(uboot_rev_magic));
+
+		goto ignore_uboot_args;
+	}
+
 	/* check that we know this tag */
 	if (uboot_tag != UBOOT_REV0P_TAG_NONE &&
 	    uboot_tag != UBOOT_REV0P_TAG_CMDLINE &&
@@ -498,6 +522,8 @@ void __init handle_uboot_args(void)
 
 	if (uboot_tag == UBOOT_REV0P_TAG_CMDLINE)
 		append_boot_cmdline = true;
+
+ignore_uboot_args:
 #endif
 
 	if (use_embedded_dtb) {
-- 
2.14.5


  parent reply	other threads:[~2019-02-06 17:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-06 17:22 [RFC 0/2] ARC: rework U-boot arguments handling Eugeniy Paltsev
2019-02-06 17:22 ` [RFC 1/2] ARC: U-boot: check arguments paranoidly Eugeniy Paltsev
2019-02-06 20:25   ` Vineet Gupta
2019-02-06 22:13   ` Vineet Gupta
2019-02-12  0:31     ` Vineet Gupta
2019-02-06 17:22 ` Eugeniy Paltsev [this message]
2019-02-06 21:33   ` [RFC 2/2] ARC: U-boot: check magic number passed from u-boot Vineet Gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190206172228.9261-3-Eugeniy.Paltsev@synopsys.com \
    --to=eugeniy.paltsev@synopsys.com \
    --cc=alexey.brodkin@synopsys.com \
    --cc=clabbe@baylibre.com \
    --cc=khilman@baylibre.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-snps-arc@lists.infradead.org \
    --cc=vineet.gupta1@synopsys.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®