mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: mfe@pengutronix.de, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] console: pbl: correctly handle relocate_to_adr after pbl_set_putc
Date: Thu,  7 Sep 2023 10:21:26 +0200	[thread overview]
Message-ID: <20230907082126.2326381-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230907082126.2326381-1-a.fatoum@pengutronix.de>

pbl_set_putc may be called by a PBL entry point to store the absolute
address of a routine to be used for printing out a character.

If barebox happens to be located outside of the initially known RAM,
it will be relocated into it by means of relocate_to_adr(), but nothing
will take care to update the function pointer stored by pbl_set_putc.

This will usually continue to work until barebox sets up the MMU and
everything not known to be RAM is marked as eXecute Never. After that,
the next PBL console print will trigger an instruction abort.

Fix this by not storing the putc function pointer, but instead an offset
relative to _text.

This is the second part of fixing barebox hanging on i.MX8M when located
at an address greater than 4G.

This change has been tested on both i.MX8M (AArch64) and i.MX6 (Thumb2).

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 pbl/console.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/pbl/console.c b/pbl/console.c
index 1a6e839c1559..d81bf580d52f 100644
--- a/pbl/console.c
+++ b/pbl/console.c
@@ -2,13 +2,14 @@
 
 #include <common.h>
 #include <debug_ll.h>
+#include <asm/sections.h>
 #include <linux/err.h>
 
 /*
  * Put these in the data section so that they survive the clearing of the
  * BSS segment.
  */
-static __attribute__ ((section(".data"))) void (*__putc)(void *ctx, int c);
+static __attribute__ ((section(".data"))) ulong putc_offset;
 static __attribute__ ((section(".data"))) void *putc_ctx;
 
 /**
@@ -21,13 +22,19 @@ static __attribute__ ((section(".data"))) void *putc_ctx;
  */
 void pbl_set_putc(void (*putcf)(void *ctx, int c), void *ctx)
 {
-	__putc = putcf;
+	putc_offset = (ulong)putcf - (ulong)_text;
 	putc_ctx = ctx;
 }
 
+static void __putc(void *ctx, int c)
+{
+	void (*putc)(void *, int) = (void *)_text + putc_offset;
+	putc(ctx, c);
+}
+
 void console_putc(unsigned int ch, char c)
 {
-	if (__putc)
+	if (putc_offset)
 		__putc(putc_ctx, c);
 	else
 		putc_ll(c);
-- 
2.39.2




  parent reply	other threads:[~2023-09-07  8:23 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-07  8:21 [PATCH 0/2] " Ahmad Fatoum
2023-09-07  8:21 ` [PATCH 1/2] ARM: mmu64: mark barebox text section executable during early init Ahmad Fatoum
2023-09-07  8:21 ` Ahmad Fatoum [this message]
2023-09-08  6:58 ` [PATCH 0/2] console: pbl: correctly handle relocate_to_adr after pbl_set_putc Sascha Hauer

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=20230907082126.2326381-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=mfe@pengutronix.de \
    /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