Skip to contentSkip to conversation

kwboot fix for AXP + some others

PATCH In review Marek Behún 2 participants 12 messages Oct 27, 2021 – Nov 3, 2021

lore
Share & tools
lore
#0 lore raw

cover kwboot fix for AXP + some others

From: Marek Behún <[email protected]>

Hi Stefan,

here are some more Pali's changes for kwboot, reviewed by me. The last one should fix boot on AXP.

Marek

Pali Rohár (5):
tools: kwboot: Fix sending retry of last header packet
tools: kwboot: Do not call tcdrain() after each sent packet
tools: kwboot: Increase delay after changing baudrate in ARM code
tools: kwboot: Replace ARM mov + movt instruction pair by mov + orr
tools: kwboot: Do not use stack when setting baudrate back to default
value

 tools/kwboot.c | 148 +++++++++++++++++++++++++++----------------------
 1 file changed, 83 insertions(+), 65 deletions(-)
Signature · 1 line
2.32.0
#1 lore raw

PATCH 1/5 tools: kwboot: Fix sending retry of last header packet

From: Pali Rohár <[email protected]>

After the trasfer of last header packet, it is possible that baudrate change pattern is received, and also that NAK byte is received so that the packet should be sent again.

Thus we should not clear the baudrate change state when sending retry of that packet.

Move code for initializing state variables from kwboot_xm_recv_reply() to kwboot_xm_sendblock().

---
 tools/kwboot.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
tools/kwboot.c +2 -5
index bb7cae9f05..b2c48812c3 100644
@@ -859,11 +859,6 @@ kwboot_xm_recv_reply(int fd, char *c, int nak_on_non_xm,
 	uint64_t recv_until = _now() + timeout;
 	int rc;
 
-	if (non_xm_print)
-		*non_xm_print = 0;
-	if (baud_changed)
-		*baud_changed = 0;
-
 	while (1) {
 		rc = kwboot_tty_recv(fd, c, 1, timeout);
 		if (rc) {
@@ -929,6 +924,8 @@ kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
 	char c;
 
 	*done_print = 0;
+	non_xm_print = 0;
+	baud_changed = 0;
 
 	retries = 0;
 	do {
Signature · 1 line
2.32.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH u-boot-marvell 1/5] tools: kwboot: Fix sending retry of last header packet

Quoted 6 lines · Marek Behún

From: Pali Rohár <[email protected]>

After the trasfer of last header packet, it is possible that baudrate change pattern is received, and also that NAK byte is received so that the packet should be sent again.

Quoted 8 lines

Thus we should not clear the baudrate change state when sending retry of that packet.

Move code for initializing state variables from kwboot_xm_recv_reply() to kwboot_xm_sendblock().

Thanks,
Stefan

Quoted 22 lines
---
  tools/kwboot.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
tools/kwboot.c +0 -5
index bb7cae9f05..b2c48812c3 100644
@@ -859,11 +859,6 @@ kwboot_xm_recv_reply(int fd, char *c, int nak_on_non_xm,
  	uint64_t recv_until = _now() + timeout;
  	int rc;
 
-	if (non_xm_print)
-		*non_xm_print = 0;
-	if (baud_changed)
-		*baud_changed = 0;
-
  	while (1) {
  		rc = kwboot_tty_recv(fd, c, 1, timeout);
  		if (rc) {

tools/kwboot.c kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm, · line 931

Quoted 8 lines
tools/kwboot.c +2 -0
@@ -929,6 +924,8 @@ kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
  	char c;
 
  	*done_print = 0;
+	non_xm_print = 0;
+	baud_changed = 0;
 
  	retries = 0;
  	do {

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#3 lore raw

PATCH 2/5 tools: kwboot: Do not call tcdrain() after each sent packet

In reply to Marek Behún

From: Pali Rohár <[email protected]>

Kwboot puts each xmodem packet to kernel queue, then waits until all bytes of that packet are transmitted over UART and then waits for xmodem reply until it is received into kernel queue.

If some reply is received during the time we are waiting until all bytes are transmitted, then kernel puts them into the queue and returns it to kwboot in next read() call.

So there is no need to wait (with tcdrain() function) until all bytes from xmodem packet are transmitted over UART, since any reply received either during that time or after is returned to kwboot with the next read().

Therefore do not call tcdrain() after each xmodem packet sent. Instead directly wait for any reply after putting xmodem packet into write kernel queue.

This change could speed up xmodem transfer in case tcdrain() function waits for a longer time.

---
 tools/kwboot.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
tools/kwboot.c +8 -5
index b2c48812c3..a6bfd3d4ce 100644
@@ -404,7 +404,7 @@ out:
 }
 
 static int
-kwboot_tty_send(int fd, const void *buf, size_t len)
+kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
 {
 	if (!buf)
 		return 0;
@@ -412,13 +412,16 @@ kwboot_tty_send(int fd, const void *buf, size_t len)
 	if (kwboot_write(fd, buf, len) < 0)
 		return -1;
 
+	if (nodrain)
+		return 0;
+
 	return tcdrain(fd);
 }
 
 static int
 kwboot_tty_send_char(int fd, unsigned char c)
 {
-	return kwboot_tty_send(fd, &c, 1);
+	return kwboot_tty_send(fd, &c, 1, 0);
 }
 
 static speed_t
@@ -705,7 +708,7 @@ kwboot_bootmsg(int tty, void *msg)
 			break;
 
 		for (count = 0; count < 128; count++) {
-			rc = kwboot_tty_send(tty, msg, 8);
+			rc = kwboot_tty_send(tty, msg, 8, 0);
 			if (rc) {
 				usleep(msg_req_delay * 1000);
 				continue;
@@ -737,7 +740,7 @@ kwboot_debugmsg(int tty, void *msg)
 		if (rc)
 			break;
 
-		rc = kwboot_tty_send(tty, msg, 8);
+		rc = kwboot_tty_send(tty, msg, 8, 0);
 		if (rc) {
 			usleep(msg_req_delay * 1000);
 			continue;
@@ -929,7 +932,7 @@ kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
 
 	retries = 0;
 	do {
-		rc = kwboot_tty_send(fd, block, sizeof(*block));
+		rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
 		if (rc)
 			return rc;
 
Signature · 1 line
2.32.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH u-boot-marvell 2/5] tools: kwboot: Do not call tcdrain() after each sent packet

Quoted 18 lines · Marek Behún

From: Pali Rohár <[email protected]>

Kwboot puts each xmodem packet to kernel queue, then waits until all bytes of that packet are transmitted over UART and then waits for xmodem reply until it is received into kernel queue.

If some reply is received during the time we are waiting until all bytes are transmitted, then kernel puts them into the queue and returns it to kwboot in next read() call.

So there is no need to wait (with tcdrain() function) until all bytes from xmodem packet are transmitted over UART, since any reply received either during that time or after is returned to kwboot with the next read().

Therefore do not call tcdrain() after each xmodem packet sent. Instead directly wait for any reply after putting xmodem packet into write kernel queue.

Quoted 5 lines

This change could speed up xmodem transfer in case tcdrain() function waits for a longer time.

Thanks,
Stefan

Quoted 55 lines
---
  tools/kwboot.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
tools/kwboot.c +7 -4
index b2c48812c3..a6bfd3d4ce 100644
@@ -404,7 +404,7 @@ out:
  }
 
  static int
-kwboot_tty_send(int fd, const void *buf, size_t len)
+kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
  {
  	if (!buf)
  		return 0;
@@ -412,13 +412,16 @@ kwboot_tty_send(int fd, const void *buf, size_t len)
  	if (kwboot_write(fd, buf, len) < 0)
  		return -1;
 
+	if (nodrain)
+		return 0;
+
  	return tcdrain(fd);
  }
 
  static int
  kwboot_tty_send_char(int fd, unsigned char c)
  {
-	return kwboot_tty_send(fd, &c, 1);
+	return kwboot_tty_send(fd, &c, 1, 0);
  }
 
  static speed_t
@@ -705,7 +708,7 @@ kwboot_bootmsg(int tty, void *msg)
  			break;
 
  		for (count = 0; count < 128; count++) {
-			rc = kwboot_tty_send(tty, msg, 8);
+			rc = kwboot_tty_send(tty, msg, 8, 0);
  			if (rc) {
  				usleep(msg_req_delay * 1000);
  				continue;
@@ -737,7 +740,7 @@ kwboot_debugmsg(int tty, void *msg)
  		if (rc)
  			break;
 
-		rc = kwboot_tty_send(tty, msg, 8);
+		rc = kwboot_tty_send(tty, msg, 8, 0);
  		if (rc) {
  			usleep(msg_req_delay * 1000);
  			continue;

tools/kwboot.c kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm, · line 938

Quoted 8 lines
tools/kwboot.c +1 -1
@@ -929,7 +932,7 @@ kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
 
  	retries = 0;
  	do {
-		rc = kwboot_tty_send(fd, block, sizeof(*block));
+		rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
  		if (rc)
  			return rc;
 

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#5 lore raw

PATCH 3/5 tools: kwboot: Increase delay after changing baudrate in ARM code

In reply to Marek Behún

From: Pali Rohár <[email protected]>

Increase loop cycles from 600000 to 2998272, which should increase delay from 1ms to about 5ms on 1200 MHz CPU.

The Number 2998272 was chosen as the nearest value around 3000000 which can be encoded into one ARM mov instruction. It avoids usage of movt instruction which is not supported by ARMv5te cores.

---
 tools/kwboot.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
tools/kwboot.c +9 -9
index a6bfd3d4ce..84294cadfe 100644
@@ -119,7 +119,7 @@ static unsigned char kwboot_baud_code[] = {
 				/* ;   writel(UART_BASE + DLL, new_dll);      */
 				/* ;   writel(UART_BASE + DLH, new_dlh);      */
 				/* ;   writel(UART_BASE + LCR, lcr & ~DLAB);  */
-				/* ;   msleep(1);                             */
+				/* ;   msleep(5);                             */
 				/* ;   return 0;                              */
 				/* ; }                                        */
 
@@ -130,7 +130,7 @@ static unsigned char kwboot_baud_code[] = {
 	0x01, 0x00, 0x4d, 0xe3, /* movt  r0, #0xd001                          */
 
 				/*  ; r2 = address of preamble string         */
-	0xd0, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
+	0xcc, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
 
 				/*  ; Send preamble string over UART          */
 				/* .Lloop_preamble:                           */
@@ -177,15 +177,15 @@ static unsigned char kwboot_baud_code[] = {
 
 				/*  ; Read old baudrate value                 */
 				/*  ; r2 = old_baudrate                       */
-	0x8c, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
+	0x88, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
 
 				/*  ; Calculate base clock                    */
 				/*  ; r1 = r2 * r1                            */
 	0x92, 0x01, 0x01, 0xe0, /* mul   r1, r2, r1                           */
 
 				/*  ; Read new baudrate value                 */
-				/*  ; r2 = baudrate                           */
-	0x88, 0x20, 0x9f, 0xe5, /* ldr   r2, baudrate                         */
+				/*  ; r2 = new_baudrate                       */
+	0x84, 0x20, 0x9f, 0xe5, /* ldr   r2, new_baudrate                     */
 
 				/*  ; Calculate new Divisor Latch             */
 				/*  ; r1 = DIV_ROUND(r1, r2) =                */
@@ -225,10 +225,10 @@ static unsigned char kwboot_baud_code[] = {
 	0x80, 0x10, 0xc1, 0xe3, /* bic   r1, r1, #0x80                        */
 	0x0c, 0x10, 0x80, 0xe5, /* str   r1, [r0, #0x0c]                      */
 
-				/*  ; Sleep 1ms ~~ 600000 cycles at 1200 MHz  */
-				/*  ; r1 = 600000                             */
-	0x9f, 0x1d, 0xa0, 0xe3, /* mov   r1, #0x27c0                          */
-	0x09, 0x10, 0x40, 0xe3, /* movt  r1, #0x0009                          */
+				/*  ; Loop 0x2dc000 (2998272) cycles          */
+				/*  ; which is about 5ms on 1200 MHz CPU      */
+				/*  ; r1 = 0x2dc000                           */
+	0xb7, 0x19, 0xa0, 0xe3, /* mov   r1, #0x2dc000                        */
 				/* .Lloop_sleep:                              */
 	0x01, 0x10, 0x41, 0xe2, /* sub   r1, r1, #1                           */
 	0x00, 0x00, 0x51, 0xe3, /* cmp   r1, #0                               */
Signature · 1 line
2.32.0
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH u-boot-marvell 3/5] tools: kwboot: Increase delay after changing baudrate in ARM code

Quoted 11 lines · Marek Behún

From: Pali Rohár <[email protected]>

Increase loop cycles from 600000 to 2998272, which should increase delay from 1ms to about 5ms on 1200 MHz CPU.

The Number 2998272 was chosen as the nearest value around 3000000 which can be encoded into one ARM mov instruction. It avoids usage of movt instruction which is not supported by ARMv5te cores.

Thanks,
Stefan

Quoted 51 lines
---
  tools/kwboot.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
tools/kwboot.c +5 -6
index a6bfd3d4ce..84294cadfe 100644
@@ -119,7 +119,7 @@ static unsigned char kwboot_baud_code[] = {
  				/* ;   writel(UART_BASE + DLL, new_dll);      */
  				/* ;   writel(UART_BASE + DLH, new_dlh);      */
  				/* ;   writel(UART_BASE + LCR, lcr & ~DLAB);  */
-				/* ;   msleep(1);                             */
+				/* ;   msleep(5);                             */
  				/* ;   return 0;                              */
  				/* ; }                                        */
 
@@ -130,7 +130,7 @@ static unsigned char kwboot_baud_code[] = {
  	0x01, 0x00, 0x4d, 0xe3, /* movt  r0, #0xd001                          */
 
  				/*  ; r2 = address of preamble string         */
-	0xd0, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
+	0xcc, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
 
  				/*  ; Send preamble string over UART          */
  				/* .Lloop_preamble:                           */
@@ -177,15 +177,15 @@ static unsigned char kwboot_baud_code[] = {
 
  				/*  ; Read old baudrate value                 */
  				/*  ; r2 = old_baudrate                       */
-	0x8c, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
+	0x88, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
 
  				/*  ; Calculate base clock                    */
  				/*  ; r1 = r2 * r1                            */
  	0x92, 0x01, 0x01, 0xe0, /* mul   r1, r2, r1                           */
 
  				/*  ; Read new baudrate value                 */
-				/*  ; r2 = baudrate                           */
-	0x88, 0x20, 0x9f, 0xe5, /* ldr   r2, baudrate                         */
+				/*  ; r2 = new_baudrate                       */
+	0x84, 0x20, 0x9f, 0xe5, /* ldr   r2, new_baudrate                     */
 
  				/*  ; Calculate new Divisor Latch             */
  				/*  ; r1 = DIV_ROUND(r1, r2) =                */
@@ -225,10 +225,10 @@ static unsigned char kwboot_baud_code[] = {
  	0x80, 0x10, 0xc1, 0xe3, /* bic   r1, r1, #0x80                        */
  	0x0c, 0x10, 0x80, 0xe5, /* str   r1, [r0, #0x0c]                      */
 
-				/*  ; Sleep 1ms ~~ 600000 cycles at 1200 MHz  */

tools/kwboot.c static unsigned char kwboot_baud_code[] = { · line 234

Quoted 10 lines
tools/kwboot.c +4 -3
-				/*  ; r1 = 600000                             */
-	0x9f, 0x1d, 0xa0, 0xe3, /* mov   r1, #0x27c0                          */
-	0x09, 0x10, 0x40, 0xe3, /* movt  r1, #0x0009                          */
+				/*  ; Loop 0x2dc000 (2998272) cycles          */
+				/*  ; which is about 5ms on 1200 MHz CPU      */
+				/*  ; r1 = 0x2dc000                           */
+	0xb7, 0x19, 0xa0, 0xe3, /* mov   r1, #0x2dc000                        */
  				/* .Lloop_sleep:                              */
  	0x01, 0x10, 0x41, 0xe2, /* sub   r1, r1, #1                           */
  	0x00, 0x00, 0x51, 0xe3, /* cmp   r1, #0                               */

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#7 lore raw

PATCH 4/5 tools: kwboot: Replace ARM mov + movt instruction pair by mov + orr

In reply to Marek Behún

From: Pali Rohár <[email protected]>

Older Armada SoCs have custom ARMv5te compatible core which does not support movt instruction. So replace mov + movt instruction pair used for immediate move construction by mov + orr instructions which are supported also by ARMv5te.

After this change kwboot ARM code should be compatible with any 32-bit ARM core compatible by ARMv2 or new. At least GNU AS does not throw any error or warning.

---
 tools/kwboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/kwboot.c +2 -2
index 84294cadfe..62c218ef64 100644
@@ -126,8 +126,8 @@ static unsigned char kwboot_baud_code[] = {
 	0xfe, 0x5f, 0x2d, 0xe9, /* push  { r1 - r12, lr }                     */
 
 				/*  ; r0 = UART_BASE                          */
-	0x02, 0x0a, 0xa0, 0xe3, /* mov   r0, #0x2000                          */
-	0x01, 0x00, 0x4d, 0xe3, /* movt  r0, #0xd001                          */
+	0x0d, 0x02, 0xa0, 0xe3, /* mov   r0, #0xd0000000                      */
+	0x12, 0x0a, 0x80, 0xe3, /* orr   r0, r0, #0x12000                     */
 
 				/*  ; r2 = address of preamble string         */
 	0xcc, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
Signature · 1 line
2.32.0
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH u-boot-marvell 4/5] tools: kwboot: Replace ARM mov + movt instruction pair by mov + orr

Quoted 7 lines · Marek Behún

From: Pali Rohár <[email protected]>

Older Armada SoCs have custom ARMv5te compatible core which does not support movt instruction. So replace mov + movt instruction pair used for immediate move construction by mov + orr instructions which are supported also by ARMv5te.

Quoted 6 lines

After this change kwboot ARM code should be compatible with any 32-bit ARM core compatible by ARMv2 or new. At least GNU AS does not throw any error or warning.

Thanks,
Stefan

Quoted 10 lines
---
  tools/kwboot.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tools/kwboot.c +0 -0
index 84294cadfe..62c218ef64 100644

tools/kwboot.c static unsigned char kwboot_baud_code[] = { · line 133

Quoted 10 lines
tools/kwboot.c +2 -2
@@ -126,8 +126,8 @@ static unsigned char kwboot_baud_code[] = {
  	0xfe, 0x5f, 0x2d, 0xe9, /* push  { r1 - r12, lr }                     */
 
  				/*  ; r0 = UART_BASE                          */
-	0x02, 0x0a, 0xa0, 0xe3, /* mov   r0, #0x2000                          */
-	0x01, 0x00, 0x4d, 0xe3, /* movt  r0, #0xd001                          */
+	0x0d, 0x02, 0xa0, 0xe3, /* mov   r0, #0xd0000000                      */
+	0x12, 0x0a, 0x80, 0xe3, /* orr   r0, r0, #0x12000                     */
 
  				/*  ; r2 = address of preamble string         */
  	0xcc, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#9 lore raw

PATCH 5/5 tools: kwboot: Do not use stack when setting baudrate back to default value

In reply to Marek Behún

From: Pali Rohár <[email protected]>

The ARM code we inject into the image to change baudrate back to the default value of 115200 Baud, which is run after successful UART transfer of the whole image, cannot use stack as at this stage stack pointer is not initialized yet.

Stack can only be used when BootROM is executing binary header, to preserve state of registers, since BootROM expects that.

Change the ARM baudrate code to not use stack at all and put binary header specific pre + post code (which stores and restores registers) into separate arrays.

The baudrate change code now jumps at it's end and expects that there is either code which returns to the BootROM or jumps to the original exec address.

---
 tools/kwboot.c | 112 ++++++++++++++++++++++++++++---------------------
 1 file changed, 65 insertions(+), 47 deletions(-)
tools/kwboot.c +65 -47
index 62c218ef64..359b43c0d8 100644
@@ -78,14 +78,7 @@ struct kwboot_block {
 #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
 #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
 
-/* ARM code making baudrate changing function return to original exec address */
-static unsigned char kwboot_pre_baud_code[] = {
-				/* exec_addr:                                 */
-	0x00, 0x00, 0x00, 0x00, /* .word 0                                    */
-	0x0c, 0xe0, 0x1f, 0xe5, /* ldr lr, exec_addr                          */
-};
-
-/* ARM code for binary header injection to change baudrate */
+/* ARM code to change baudrate */
 static unsigned char kwboot_baud_code[] = {
 				/* ; #define UART_BASE 0xd0012000             */
 				/* ; #define THR       0x00                   */
@@ -123,14 +116,12 @@ static unsigned char kwboot_baud_code[] = {
 				/* ;   return 0;                              */
 				/* ; }                                        */
 
-	0xfe, 0x5f, 0x2d, 0xe9, /* push  { r1 - r12, lr }                     */
-
 				/*  ; r0 = UART_BASE                          */
 	0x0d, 0x02, 0xa0, 0xe3, /* mov   r0, #0xd0000000                      */
 	0x12, 0x0a, 0x80, 0xe3, /* orr   r0, r0, #0x12000                     */
 
 				/*  ; r2 = address of preamble string         */
-	0xcc, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
+	0xc8, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
 
 				/*  ; Send preamble string over UART          */
 				/* .Lloop_preamble:                           */
@@ -177,7 +168,7 @@ static unsigned char kwboot_baud_code[] = {
 
 				/*  ; Read old baudrate value                 */
 				/*  ; r2 = old_baudrate                       */
-	0x88, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
+	0x84, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
 
 				/*  ; Calculate base clock                    */
 				/*  ; r1 = r2 * r1                            */
@@ -185,7 +176,7 @@ static unsigned char kwboot_baud_code[] = {
 
 				/*  ; Read new baudrate value                 */
 				/*  ; r2 = new_baudrate                       */
-	0x84, 0x20, 0x9f, 0xe5, /* ldr   r2, new_baudrate                     */
+	0x80, 0x20, 0x9f, 0xe5, /* ldr   r2, new_baudrate                     */
 
 				/*  ; Calculate new Divisor Latch             */
 				/*  ; r1 = DIV_ROUND(r1, r2) =                */
@@ -234,9 +225,7 @@ static unsigned char kwboot_baud_code[] = {
 	0x00, 0x00, 0x51, 0xe3, /* cmp   r1, #0                               */
 	0xfc, 0xff, 0xff, 0x1a, /* bne   .Lloop_sleep                         */
 
-				/*  ; Return 0 - no error                     */
-	0x00, 0x00, 0xa0, 0xe3, /* mov   r0, #0                               */
-	0xfe, 0x9f, 0xbd, 0xe8, /* pop   { r1 - r12, pc }                     */
+	0x05, 0x00, 0x00, 0xea, /* b     end                                  */
 
 				/*  ; Preamble string                         */
 				/* preamble:                                  */
@@ -252,10 +241,29 @@ static unsigned char kwboot_baud_code[] = {
 				/*  ; Placeholder for new baudrate value      */
 				/* new_baudrate:                              */
 	0x00, 0x00, 0x00, 0x00, /* .word 0                                    */
+
+				/* end:                                       */
+};
+
+/* ARM code for storing registers for future returning back to the bootrom */
+static unsigned char kwboot_baud_code_binhdr_pre[] = {
+	0xfe, 0x5f, 0x2d, 0xe9, /* push  { r1 - r12, lr }                     */
 };
 
-#define KWBOOT_BAUDRATE_BIN_HEADER_SZ (sizeof(kwboot_baud_code) + \
-				       sizeof(struct opt_hdr_v1) + 8 + 16)
+/* ARM code for returning back to the bootrom */
+static unsigned char kwboot_baud_code_binhdr_post[] = {
+				/*  ; Return 0 - no error                     */
+	0x00, 0x00, 0xa0, 0xe3, /* mov   r0, #0                               */
+	0xfe, 0x9f, 0xbd, 0xe8, /* pop   { r1 - r12, pc }                     */
+};
+
+/* ARM code for jumping to the original image exec_addr */
+static unsigned char kwboot_baud_code_data_jump[] = {
+	0x04, 0xf0, 0x1f, 0xe5, /* ldr   pc, exec_addr                        */
+				/*  ; Placeholder for exec_addr               */
+				/* exec_addr:                                 */
+	0x00, 0x00, 0x00, 0x00, /* .word 0                                    */
+};
 
 static const char kwb_baud_magic[16] = "$baudratechange";
 
@@ -1409,44 +1417,51 @@ kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
 }
 
 static void
-_inject_baudrate_change_code(void *img, size_t *size, int pre,
+_inject_baudrate_change_code(void *img, size_t *size, int for_data,
 			     int old_baud, int new_baud)
 {
-	uint32_t codesz = sizeof(kwboot_baud_code);
 	struct main_hdr_v1 *hdr = img;
+	uint32_t orig_datasz;
+	uint32_t codesz;
 	uint8_t *code;
 
-	if (pre) {
-		uint32_t presz = sizeof(kwboot_pre_baud_code);
-		uint32_t orig_datasz;
-
+	if (for_data) {
 		orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
 
-		code = kwboot_img_grow_data_right(img, size, presz + codesz);
-
-		/*
-		 * We need to prepend code that loads lr register with original
-		 * value of hdr->execaddr. We do this by putting the original
-		 * exec address before the code that loads it relatively from
-		 * it's beginning.
-		 * Afterwards we change the exec address to this code (which is
-		 * at offset 4, because the first 4 bytes contain the original
-		 * exec address).
-		 */
-		memcpy(code, kwboot_pre_baud_code, presz);
-		*(uint32_t *)code = hdr->execaddr;
-
-		hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) +
-					    orig_datasz + 4);
-
-		code += presz;
+		codesz = sizeof(kwboot_baud_code) +
+			 sizeof(kwboot_baud_code_data_jump);
+		code = kwboot_img_grow_data_right(img, size, codesz);
 	} else {
+		codesz = sizeof(kwboot_baud_code_binhdr_pre) +
+			 sizeof(kwboot_baud_code) +
+			 sizeof(kwboot_baud_code_binhdr_post);
 		code = kwboot_add_bin_ohdr_v1(img, size, codesz);
+
+		codesz = sizeof(kwboot_baud_code_binhdr_pre);
+		memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
+		code += codesz;
 	}
 
-	memcpy(code, kwboot_baud_code, codesz - 8);
-	*(uint32_t *)(code + codesz - 8) = cpu_to_le32(old_baud);
-	*(uint32_t *)(code + codesz - 4) = cpu_to_le32(new_baud);
+	codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
+	memcpy(code, kwboot_baud_code, codesz);
+	code += codesz;
+	*(uint32_t *)code = cpu_to_le32(old_baud);
+	code += sizeof(uint32_t);
+	*(uint32_t *)code = cpu_to_le32(new_baud);
+	code += sizeof(uint32_t);
+
+	if (for_data) {
+		codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
+		memcpy(code, kwboot_baud_code_data_jump, codesz);
+		code += codesz;
+		*(uint32_t *)code = hdr->execaddr;
+		code += sizeof(uint32_t);
+		hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
+	} else {
+		codesz = sizeof(kwboot_baud_code_binhdr_post);
+		memcpy(code, kwboot_baud_code_binhdr_post, codesz);
+		code += codesz;
+	}
 }
 
 static int
@@ -1729,10 +1744,13 @@ main(int argc, char **argv)
 		baudrate = 0;
 	else
 		/* ensure we have enough space for baudrate change code */
-		after_img_rsv += KWBOOT_BAUDRATE_BIN_HEADER_SZ +
+		after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
+				 sizeof(kwboot_baud_code_binhdr_pre) +
+				 sizeof(kwboot_baud_code) +
+				 sizeof(kwboot_baud_code_binhdr_post) +
 				 KWBOOT_XM_BLKSZ +
-				 sizeof(kwboot_pre_baud_code) +
 				 sizeof(kwboot_baud_code) +
+				 sizeof(kwboot_baud_code_data_jump) +
 				 KWBOOT_XM_BLKSZ;
 
 	if (imgpath) {
Signature · 1 line
2.32.0
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH u-boot-marvell 5/5] tools: kwboot: Do not use stack when setting baudrate back to default value

Quoted 14 lines · Marek Behún

From: Pali Rohár <[email protected]>

The ARM code we inject into the image to change baudrate back to the default value of 115200 Baud, which is run after successful UART transfer of the whole image, cannot use stack as at this stage stack pointer is not initialized yet.

Stack can only be used when BootROM is executing binary header, to preserve state of registers, since BootROM expects that.

Change the ARM baudrate code to not use stack at all and put binary header specific pre + post code (which stores and restores registers) into separate arrays.

Quoted 6 lines

The baudrate change code now jumps at it's end and expects that there is either code which returns to the BootROM or jumps to the original exec address.

Thanks,
Stefan

Quoted 187 lines
---
  tools/kwboot.c | 112 ++++++++++++++++++++++++++++---------------------
  1 file changed, 65 insertions(+), 47 deletions(-)
tools/kwboot.c +61 -46
index 62c218ef64..359b43c0d8 100644
@@ -78,14 +78,7 @@ struct kwboot_block {
  #define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
  #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
 
-/* ARM code making baudrate changing function return to original exec address */
-static unsigned char kwboot_pre_baud_code[] = {
-				/* exec_addr:                                 */
-	0x00, 0x00, 0x00, 0x00, /* .word 0                                    */
-	0x0c, 0xe0, 0x1f, 0xe5, /* ldr lr, exec_addr                          */
-};
-
-/* ARM code for binary header injection to change baudrate */
+/* ARM code to change baudrate */
  static unsigned char kwboot_baud_code[] = {
  				/* ; #define UART_BASE 0xd0012000             */
  				/* ; #define THR       0x00                   */
@@ -123,14 +116,12 @@ static unsigned char kwboot_baud_code[] = {
  				/* ;   return 0;                              */
  				/* ; }                                        */
 
-	0xfe, 0x5f, 0x2d, 0xe9, /* push  { r1 - r12, lr }                     */
-
  				/*  ; r0 = UART_BASE                          */
  	0x0d, 0x02, 0xa0, 0xe3, /* mov   r0, #0xd0000000                      */
  	0x12, 0x0a, 0x80, 0xe3, /* orr   r0, r0, #0x12000                     */
 
  				/*  ; r2 = address of preamble string         */
-	0xcc, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
+	0xc8, 0x20, 0x8f, 0xe2, /* adr   r2, preamble                         */
 
  				/*  ; Send preamble string over UART          */
  				/* .Lloop_preamble:                           */
@@ -177,7 +168,7 @@ static unsigned char kwboot_baud_code[] = {
 
  				/*  ; Read old baudrate value                 */
  				/*  ; r2 = old_baudrate                       */
-	0x88, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
+	0x84, 0x20, 0x9f, 0xe5, /* ldr   r2, old_baudrate                     */
 
  				/*  ; Calculate base clock                    */
  				/*  ; r1 = r2 * r1                            */
@@ -185,7 +176,7 @@ static unsigned char kwboot_baud_code[] = {
 
  				/*  ; Read new baudrate value                 */
  				/*  ; r2 = new_baudrate                       */
-	0x84, 0x20, 0x9f, 0xe5, /* ldr   r2, new_baudrate                     */
+	0x80, 0x20, 0x9f, 0xe5, /* ldr   r2, new_baudrate                     */
 
  				/*  ; Calculate new Divisor Latch             */
  				/*  ; r1 = DIV_ROUND(r1, r2) =                */
@@ -234,9 +225,7 @@ static unsigned char kwboot_baud_code[] = {
  	0x00, 0x00, 0x51, 0xe3, /* cmp   r1, #0                               */
  	0xfc, 0xff, 0xff, 0x1a, /* bne   .Lloop_sleep                         */
 
-				/*  ; Return 0 - no error                     */
-	0x00, 0x00, 0xa0, 0xe3, /* mov   r0, #0                               */
-	0xfe, 0x9f, 0xbd, 0xe8, /* pop   { r1 - r12, pc }                     */
+	0x05, 0x00, 0x00, 0xea, /* b     end                                  */
 
  				/*  ; Preamble string                         */
  				/* preamble:                                  */
@@ -252,10 +241,29 @@ static unsigned char kwboot_baud_code[] = {
  				/*  ; Placeholder for new baudrate value      */
  				/* new_baudrate:                              */
  	0x00, 0x00, 0x00, 0x00, /* .word 0                                    */
+
+				/* end:                                       */
+};
+
+/* ARM code for storing registers for future returning back to the bootrom */
+static unsigned char kwboot_baud_code_binhdr_pre[] = {
+	0xfe, 0x5f, 0x2d, 0xe9, /* push  { r1 - r12, lr }                     */
  };
 
-#define KWBOOT_BAUDRATE_BIN_HEADER_SZ (sizeof(kwboot_baud_code) + \
-				       sizeof(struct opt_hdr_v1) + 8 + 16)
+/* ARM code for returning back to the bootrom */
+static unsigned char kwboot_baud_code_binhdr_post[] = {
+				/*  ; Return 0 - no error                     */
+	0x00, 0x00, 0xa0, 0xe3, /* mov   r0, #0                               */
+	0xfe, 0x9f, 0xbd, 0xe8, /* pop   { r1 - r12, pc }                     */
+};
+
+/* ARM code for jumping to the original image exec_addr */
+static unsigned char kwboot_baud_code_data_jump[] = {
+	0x04, 0xf0, 0x1f, 0xe5, /* ldr   pc, exec_addr                        */
+				/*  ; Placeholder for exec_addr               */
+				/* exec_addr:                                 */
+	0x00, 0x00, 0x00, 0x00, /* .word 0                                    */
+};
 
  static const char kwb_baud_magic[16] = "$baudratechange";
 
@@ -1409,44 +1417,51 @@ kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
  }
 
  static void
-_inject_baudrate_change_code(void *img, size_t *size, int pre,
+_inject_baudrate_change_code(void *img, size_t *size, int for_data,
  			     int old_baud, int new_baud)
  {
-	uint32_t codesz = sizeof(kwboot_baud_code);
  	struct main_hdr_v1 *hdr = img;
+	uint32_t orig_datasz;
+	uint32_t codesz;
  	uint8_t *code;
 
-	if (pre) {
-		uint32_t presz = sizeof(kwboot_pre_baud_code);
-		uint32_t orig_datasz;
-
+	if (for_data) {
  		orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
 
-		code = kwboot_img_grow_data_right(img, size, presz + codesz);
-
-		/*
-		 * We need to prepend code that loads lr register with original
-		 * value of hdr->execaddr. We do this by putting the original
-		 * exec address before the code that loads it relatively from
-		 * it's beginning.
-		 * Afterwards we change the exec address to this code (which is
-		 * at offset 4, because the first 4 bytes contain the original
-		 * exec address).
-		 */
-		memcpy(code, kwboot_pre_baud_code, presz);
-		*(uint32_t *)code = hdr->execaddr;
-
-		hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) +
-					    orig_datasz + 4);
-
-		code += presz;
+		codesz = sizeof(kwboot_baud_code) +
+			 sizeof(kwboot_baud_code_data_jump);
+		code = kwboot_img_grow_data_right(img, size, codesz);
  	} else {
+		codesz = sizeof(kwboot_baud_code_binhdr_pre) +
+			 sizeof(kwboot_baud_code) +
+			 sizeof(kwboot_baud_code_binhdr_post);
  		code = kwboot_add_bin_ohdr_v1(img, size, codesz);
+
+		codesz = sizeof(kwboot_baud_code_binhdr_pre);
+		memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
+		code += codesz;
  	}
 
-	memcpy(code, kwboot_baud_code, codesz - 8);
-	*(uint32_t *)(code + codesz - 8) = cpu_to_le32(old_baud);
-	*(uint32_t *)(code + codesz - 4) = cpu_to_le32(new_baud);
+	codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
+	memcpy(code, kwboot_baud_code, codesz);
+	code += codesz;
+	*(uint32_t *)code = cpu_to_le32(old_baud);
+	code += sizeof(uint32_t);
+	*(uint32_t *)code = cpu_to_le32(new_baud);
+	code += sizeof(uint32_t);
+
+	if (for_data) {
+		codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
+		memcpy(code, kwboot_baud_code_data_jump, codesz);
+		code += codesz;
+		*(uint32_t *)code = hdr->execaddr;
+		code += sizeof(uint32_t);
+		hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
+	} else {
+		codesz = sizeof(kwboot_baud_code_binhdr_post);
+		memcpy(code, kwboot_baud_code_binhdr_post, codesz);
+		code += codesz;
+	}
  }
 
  static int
@@ -1729,10 +1744,13 @@ main(int argc, char **argv)
  		baudrate = 0;
  	else
  		/* ensure we have enough space for baudrate change code */
-		after_img_rsv += KWBOOT_BAUDRATE_BIN_HEADER_SZ +
+		after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +

tools/kwboot.c main(int argc, char **argv) · line 1756

Quoted 10 lines
tools/kwboot.c +4 -1
+				 sizeof(kwboot_baud_code_binhdr_pre) +
+				 sizeof(kwboot_baud_code) +
+				 sizeof(kwboot_baud_code_binhdr_post) +
  				 KWBOOT_XM_BLKSZ +
-				 sizeof(kwboot_pre_baud_code) +
  				 sizeof(kwboot_baud_code) +
+				 sizeof(kwboot_baud_code_data_jump) +
  				 KWBOOT_XM_BLKSZ;
 
  	if (imgpath) {

Viele Grüße,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#11 lore raw
In reply to Marek Behún
Quoted 17 lines · Marek Behún

From: Marek Behún <[email protected]>

Hi Stefan,

here are some more Pali's changes for kwboot, reviewed by me. The last one should fix boot on AXP.

Marek

Pali Rohár (5):
tools: kwboot: Fix sending retry of last header packet
tools: kwboot: Do not call tcdrain() after each sent packet
tools: kwboot: Increase delay after changing baudrate in ARM code
tools: kwboot: Replace ARM mov + movt instruction pair by mov + orr
tools: kwboot: Do not use stack when setting baudrate back to default
value

Quoted 3 lines
  tools/kwboot.c | 148 +++++++++++++++++++++++++++----------------------
  1 file changed, 83 insertions(+), 65 deletions(-)

Applied to u-boot-marvell/master

Thanks,
Stefan