Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2 | * Boot a Marvell SoC, with Xmodem over UART0. |
Marek Behún | b843aed | 2021-09-24 23:07:13 +0200 | [diff] [blame] | 3 | * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and |
| 4 | * Armada 39x |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 5 | * |
| 6 | * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com> |
Pali Rohár | cf8c932 | 2021-09-24 23:07:14 +0200 | [diff] [blame] | 7 | * (c) 2021 Pali Rohár <pali@kernel.org> |
| 8 | * (c) 2021 Marek Behún <marek.behun@nic.cz> |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 9 | * |
| 10 | * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281 |
| 11 | * Integrated Controller: Functional Specifications" December 2, |
| 12 | * 2008. Chapter 24.2 "BootROM Firmware". |
| 13 | */ |
| 14 | |
Stefan Roese | f4db6c9 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 15 | #include "kwbimage.h" |
| 16 | #include "mkimage.h" |
Pali Rohár | a050a86 | 2021-09-24 23:06:42 +0200 | [diff] [blame] | 17 | #include "version.h" |
Stefan Roese | f4db6c9 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 18 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <stdarg.h> |
Stefan Roese | f4db6c9 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 23 | #include <image.h> |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 24 | #include <libgen.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <errno.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdint.h> |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 29 | #include <time.h> |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | |
Pali Rohár | 93b5563 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 32 | #ifdef __linux__ |
| 33 | #include "termios_linux.h" |
| 34 | #else |
| 35 | #include <termios.h> |
| 36 | #endif |
| 37 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 38 | /* |
| 39 | * Marvell BootROM UART Sensing |
| 40 | */ |
| 41 | |
| 42 | static unsigned char kwboot_msg_boot[] = { |
| 43 | 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 |
| 44 | }; |
| 45 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 46 | static unsigned char kwboot_msg_debug[] = { |
| 47 | 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 |
| 48 | }; |
| 49 | |
| 50 | /* Defines known to work on Kirkwood */ |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 51 | #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */ |
| 52 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 53 | /* Defines known to work on Armada XP */ |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 54 | #define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */ |
| 55 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 56 | /* |
| 57 | * Xmodem Transfers |
| 58 | */ |
| 59 | |
| 60 | #define SOH 1 /* sender start of block header */ |
| 61 | #define EOT 4 /* sender end of block transfer */ |
| 62 | #define ACK 6 /* target block ack */ |
| 63 | #define NAK 21 /* target block negative ack */ |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 64 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 65 | #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */ |
| 66 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 67 | struct kwboot_block { |
| 68 | uint8_t soh; |
| 69 | uint8_t pnum; |
| 70 | uint8_t _pnum; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 71 | uint8_t data[KWBOOT_XM_BLKSZ]; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 72 | uint8_t csum; |
Pali Rohár | a107c61 | 2021-07-23 11:14:14 +0200 | [diff] [blame] | 73 | } __packed; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 74 | |
Pali Rohár | ef95143 | 2022-01-25 18:13:00 +0100 | [diff] [blame] | 75 | #define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */ |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 76 | #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */ |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 77 | |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 78 | /* ARM code to change baudrate */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 79 | static unsigned char kwboot_baud_code[] = { |
| 80 | /* ; #define UART_BASE 0xd0012000 */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 81 | /* ; #define DLL 0x00 */ |
| 82 | /* ; #define DLH 0x04 */ |
| 83 | /* ; #define LCR 0x0c */ |
| 84 | /* ; #define DLAB 0x80 */ |
| 85 | /* ; #define LSR 0x14 */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 86 | /* ; #define TEMT 0x40 */ |
| 87 | /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */ |
| 88 | /* ; */ |
| 89 | /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 90 | /* ; while */ |
| 91 | /* ; (!(readl(UART_BASE + LSR) & TEMT)); */ |
| 92 | /* ; u32 lcr = readl(UART_BASE + LCR); */ |
| 93 | /* ; writel(UART_BASE + LCR, lcr | DLAB); */ |
| 94 | /* ; u8 old_dll = readl(UART_BASE + DLL); */ |
| 95 | /* ; u8 old_dlh = readl(UART_BASE + DLH); */ |
| 96 | /* ; u16 old_dl = old_dll | (old_dlh << 8); */ |
| 97 | /* ; u32 clk = old_b * old_dl; */ |
| 98 | /* ; u16 new_dl = DIV_ROUND(clk, new_b); */ |
| 99 | /* ; u8 new_dll = new_dl & 0xff; */ |
| 100 | /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */ |
| 101 | /* ; writel(UART_BASE + DLL, new_dll); */ |
| 102 | /* ; writel(UART_BASE + DLH, new_dlh); */ |
| 103 | /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */ |
Pali Rohár | 5645229 | 2021-10-27 20:57:00 +0200 | [diff] [blame] | 104 | /* ; msleep(5); */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 105 | /* ; return 0; */ |
| 106 | /* ; } */ |
| 107 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 108 | /* ; r0 = UART_BASE */ |
Pali Rohár | 558176d | 2021-10-27 20:57:01 +0200 | [diff] [blame] | 109 | 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */ |
| 110 | 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 111 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 112 | /* ; Wait until Transmitter FIFO is Empty */ |
| 113 | /* .Lloop_txempty: */ |
| 114 | /* ; r1 = UART_BASE[LSR] & TEMT */ |
| 115 | 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */ |
| 116 | 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */ |
| 117 | 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */ |
| 118 | |
| 119 | /* ; Set Divisor Latch Access Bit */ |
| 120 | /* ; UART_BASE[LCR] |= DLAB */ |
| 121 | 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */ |
| 122 | 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */ |
| 123 | 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */ |
| 124 | |
| 125 | /* ; Read current Divisor Latch */ |
| 126 | /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */ |
| 127 | 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */ |
| 128 | 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */ |
| 129 | 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */ |
| 130 | 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */ |
| 131 | 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */ |
| 132 | 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */ |
| 133 | 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */ |
| 134 | |
| 135 | /* ; Read old baudrate value */ |
| 136 | /* ; r2 = old_baudrate */ |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 137 | 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 138 | |
| 139 | /* ; Calculate base clock */ |
| 140 | /* ; r1 = r2 * r1 */ |
| 141 | 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */ |
| 142 | |
| 143 | /* ; Read new baudrate value */ |
Pali Rohár | 5645229 | 2021-10-27 20:57:00 +0200 | [diff] [blame] | 144 | /* ; r2 = new_baudrate */ |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 145 | 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 146 | |
| 147 | /* ; Calculate new Divisor Latch */ |
| 148 | /* ; r1 = DIV_ROUND(r1, r2) = */ |
| 149 | /* ; = (r1 + r2/2) / r2 */ |
| 150 | 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */ |
| 151 | 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */ |
| 152 | 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */ |
| 153 | /* .Lloop_div1: */ |
| 154 | 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */ |
| 155 | 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */ |
| 156 | 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */ |
| 157 | 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */ |
| 158 | /* .Lloop_div2: */ |
| 159 | 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */ |
| 160 | 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */ |
| 161 | 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */ |
| 162 | 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */ |
| 163 | 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */ |
| 164 | 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */ |
| 165 | 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */ |
| 166 | |
| 167 | /* ; Set new Divisor Latch Low */ |
| 168 | /* ; UART_BASE[DLL] = r1 & 0xff */ |
| 169 | 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */ |
| 170 | 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */ |
| 171 | 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */ |
| 172 | |
| 173 | /* ; Set new Divisor Latch High */ |
| 174 | /* ; UART_BASE[DLH] = r1>>8 & 0xff */ |
| 175 | 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */ |
| 176 | 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */ |
| 177 | 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */ |
| 178 | |
| 179 | /* ; Clear Divisor Latch Access Bit */ |
| 180 | /* ; UART_BASE[LCR] &= ~DLAB */ |
| 181 | 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */ |
| 182 | 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */ |
| 183 | 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */ |
| 184 | |
Pali Rohár | 5645229 | 2021-10-27 20:57:00 +0200 | [diff] [blame] | 185 | /* ; Loop 0x2dc000 (2998272) cycles */ |
| 186 | /* ; which is about 5ms on 1200 MHz CPU */ |
| 187 | /* ; r1 = 0x2dc000 */ |
| 188 | 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 189 | /* .Lloop_sleep: */ |
| 190 | 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */ |
| 191 | 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */ |
| 192 | 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */ |
| 193 | |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 194 | /* ; Jump to the end of execution */ |
| 195 | 0x01, 0x00, 0x00, 0xea, /* b end */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 196 | |
| 197 | /* ; Placeholder for old baudrate value */ |
| 198 | /* old_baudrate: */ |
| 199 | 0x00, 0x00, 0x00, 0x00, /* .word 0 */ |
| 200 | |
| 201 | /* ; Placeholder for new baudrate value */ |
| 202 | /* new_baudrate: */ |
| 203 | 0x00, 0x00, 0x00, 0x00, /* .word 0 */ |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 204 | |
| 205 | /* end: */ |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 208 | /* ARM code from binary header executed by BootROM before changing baudrate */ |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 209 | static unsigned char kwboot_baud_code_binhdr_pre[] = { |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 210 | /* ; #define UART_BASE 0xd0012000 */ |
| 211 | /* ; #define THR 0x00 */ |
| 212 | /* ; #define LSR 0x14 */ |
| 213 | /* ; #define THRE 0x20 */ |
| 214 | /* ; */ |
| 215 | /* ; void send_preamble(void) { */ |
| 216 | /* ; const u8 *str = "$baudratechange"; */ |
| 217 | /* ; u8 c; */ |
| 218 | /* ; do { */ |
| 219 | /* ; while */ |
| 220 | /* ; ((readl(UART_BASE + LSR) & THRE)); */ |
| 221 | /* ; c = *str++; */ |
| 222 | /* ; writel(UART_BASE + THR, c); */ |
| 223 | /* ; } while (c); */ |
| 224 | /* ; } */ |
| 225 | |
| 226 | /* ; Preserve registers for BootROM */ |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 227 | 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */ |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 228 | |
| 229 | /* ; r0 = UART_BASE */ |
| 230 | 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */ |
| 231 | 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */ |
| 232 | |
| 233 | /* ; r2 = address of preamble string */ |
| 234 | 0x00, 0x20, 0x8f, 0xe2, /* adr r2, .Lstr_preamble */ |
| 235 | |
| 236 | /* ; Skip preamble data section */ |
| 237 | 0x03, 0x00, 0x00, 0xea, /* b .Lloop_preamble */ |
| 238 | |
| 239 | /* ; Preamble string */ |
| 240 | /* .Lstr_preamble: */ |
| 241 | 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */ |
| 242 | 0x64, 0x72, 0x61, 0x74, |
| 243 | 0x65, 0x63, 0x68, 0x61, |
| 244 | 0x6e, 0x67, 0x65, 0x00, |
| 245 | |
| 246 | /* ; Send preamble string over UART */ |
| 247 | /* .Lloop_preamble: */ |
| 248 | /* */ |
| 249 | /* ; Wait until Transmitter Holding is Empty */ |
| 250 | /* .Lloop_thre: */ |
| 251 | /* ; r1 = UART_BASE[LSR] & THRE */ |
| 252 | 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */ |
| 253 | 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */ |
| 254 | 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */ |
| 255 | |
| 256 | /* ; Put character into Transmitter FIFO */ |
| 257 | /* ; r1 = *r2++ */ |
| 258 | 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */ |
| 259 | /* ; UART_BASE[THR] = r1 */ |
| 260 | 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */ |
| 261 | |
| 262 | /* ; Loop until end of preamble string */ |
| 263 | 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */ |
| 264 | 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */ |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 265 | }; |
| 266 | |
Pali Rohár | 62a98f4 | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 267 | /* ARM code for returning from binary header back to BootROM */ |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 268 | static unsigned char kwboot_baud_code_binhdr_post[] = { |
| 269 | /* ; Return 0 - no error */ |
| 270 | 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */ |
| 271 | 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */ |
| 272 | }; |
| 273 | |
| 274 | /* ARM code for jumping to the original image exec_addr */ |
| 275 | static unsigned char kwboot_baud_code_data_jump[] = { |
| 276 | 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */ |
| 277 | /* ; Placeholder for exec_addr */ |
| 278 | /* exec_addr: */ |
| 279 | 0x00, 0x00, 0x00, 0x00, /* .word 0 */ |
| 280 | }; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 281 | |
| 282 | static const char kwb_baud_magic[16] = "$baudratechange"; |
| 283 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 284 | static int kwboot_verbose; |
| 285 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 286 | static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO; |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 287 | static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 288 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 289 | static ssize_t |
| 290 | kwboot_write(int fd, const char *buf, size_t len) |
| 291 | { |
Pali Rohár | 6ba7d63 | 2022-01-25 18:13:10 +0100 | [diff] [blame] | 292 | ssize_t tot = 0; |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 293 | |
| 294 | while (tot < len) { |
| 295 | ssize_t wr = write(fd, buf + tot, len - tot); |
| 296 | |
Pali Rohár | 6ba7d63 | 2022-01-25 18:13:10 +0100 | [diff] [blame] | 297 | if (wr < 0 && errno == EINTR) |
| 298 | continue; |
| 299 | else if (wr < 0) |
| 300 | return wr; |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 301 | |
| 302 | tot += wr; |
| 303 | } |
| 304 | |
| 305 | return tot; |
| 306 | } |
| 307 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 308 | static void |
| 309 | kwboot_printv(const char *fmt, ...) |
| 310 | { |
| 311 | va_list ap; |
| 312 | |
| 313 | if (kwboot_verbose) { |
| 314 | va_start(ap, fmt); |
| 315 | vprintf(fmt, ap); |
| 316 | va_end(ap); |
| 317 | fflush(stdout); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | __spinner(void) |
| 323 | { |
| 324 | const char seq[] = { '-', '\\', '|', '/' }; |
| 325 | const int div = 8; |
| 326 | static int state, bs; |
| 327 | |
| 328 | if (state % div == 0) { |
| 329 | fputc(bs, stdout); |
| 330 | fputc(seq[state / div % sizeof(seq)], stdout); |
| 331 | fflush(stdout); |
| 332 | } |
| 333 | |
| 334 | bs = '\b'; |
| 335 | state++; |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | kwboot_spinner(void) |
| 340 | { |
| 341 | if (kwboot_verbose) |
| 342 | __spinner(); |
| 343 | } |
| 344 | |
| 345 | static void |
| 346 | __progress(int pct, char c) |
| 347 | { |
| 348 | const int width = 70; |
| 349 | static const char *nl = ""; |
| 350 | static int pos; |
| 351 | |
| 352 | if (pos % width == 0) |
| 353 | printf("%s%3d %% [", nl, pct); |
| 354 | |
| 355 | fputc(c, stdout); |
| 356 | |
| 357 | nl = "]\n"; |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 358 | pos = (pos + 1) % width; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 359 | |
| 360 | if (pct == 100) { |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 361 | while (pos && pos++ < width) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 362 | fputc(' ', stdout); |
| 363 | fputs(nl, stdout); |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 364 | nl = ""; |
| 365 | pos = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | fflush(stdout); |
| 369 | |
| 370 | } |
| 371 | |
| 372 | static void |
| 373 | kwboot_progress(int _pct, char c) |
| 374 | { |
| 375 | static int pct; |
| 376 | |
| 377 | if (_pct != -1) |
| 378 | pct = _pct; |
| 379 | |
| 380 | if (kwboot_verbose) |
| 381 | __progress(pct, c); |
Pali Rohár | 5a1f8cb | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 382 | |
| 383 | if (pct == 100) |
| 384 | pct = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int |
| 388 | kwboot_tty_recv(int fd, void *buf, size_t len, int timeo) |
| 389 | { |
| 390 | int rc, nfds; |
| 391 | fd_set rfds; |
| 392 | struct timeval tv; |
| 393 | ssize_t n; |
| 394 | |
| 395 | rc = -1; |
| 396 | |
| 397 | FD_ZERO(&rfds); |
| 398 | FD_SET(fd, &rfds); |
| 399 | |
| 400 | tv.tv_sec = 0; |
| 401 | tv.tv_usec = timeo * 1000; |
| 402 | if (tv.tv_usec > 1000000) { |
| 403 | tv.tv_sec += tv.tv_usec / 1000000; |
| 404 | tv.tv_usec %= 1000000; |
| 405 | } |
| 406 | |
| 407 | do { |
| 408 | nfds = select(fd + 1, &rfds, NULL, NULL, &tv); |
Pali Rohár | 91fb095 | 2022-01-25 18:13:11 +0100 | [diff] [blame] | 409 | if (nfds < 0 && errno == EINTR) |
| 410 | continue; |
| 411 | else if (nfds < 0) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 412 | goto out; |
Pali Rohár | 91fb095 | 2022-01-25 18:13:11 +0100 | [diff] [blame] | 413 | else if (!nfds) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 414 | errno = ETIMEDOUT; |
| 415 | goto out; |
| 416 | } |
| 417 | |
| 418 | n = read(fd, buf, len); |
Pali Rohár | 91fb095 | 2022-01-25 18:13:11 +0100 | [diff] [blame] | 419 | if (n < 0 && errno == EINTR) |
| 420 | continue; |
| 421 | else if (n <= 0) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 422 | goto out; |
| 423 | |
| 424 | buf = (char *)buf + n; |
| 425 | len -= n; |
| 426 | } while (len > 0); |
| 427 | |
| 428 | rc = 0; |
| 429 | out: |
| 430 | return rc; |
| 431 | } |
| 432 | |
| 433 | static int |
Pali Rohár | cab817d | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 434 | kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 435 | { |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 436 | if (!buf) |
| 437 | return 0; |
| 438 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 439 | if (kwboot_write(fd, buf, len) < 0) |
| 440 | return -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 441 | |
Pali Rohár | cab817d | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 442 | if (nodrain) |
| 443 | return 0; |
| 444 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 445 | return tcdrain(fd); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static int |
| 449 | kwboot_tty_send_char(int fd, unsigned char c) |
| 450 | { |
Pali Rohár | cab817d | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 451 | return kwboot_tty_send(fd, &c, 1, 0); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static speed_t |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 455 | kwboot_tty_baudrate_to_speed(int baudrate) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 456 | { |
| 457 | switch (baudrate) { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 458 | #ifdef B4000000 |
| 459 | case 4000000: |
| 460 | return B4000000; |
| 461 | #endif |
| 462 | #ifdef B3500000 |
| 463 | case 3500000: |
| 464 | return B3500000; |
| 465 | #endif |
| 466 | #ifdef B3000000 |
| 467 | case 3000000: |
| 468 | return B3000000; |
| 469 | #endif |
| 470 | #ifdef B2500000 |
| 471 | case 2500000: |
| 472 | return B2500000; |
| 473 | #endif |
| 474 | #ifdef B2000000 |
| 475 | case 2000000: |
| 476 | return B2000000; |
| 477 | #endif |
| 478 | #ifdef B1500000 |
| 479 | case 1500000: |
| 480 | return B1500000; |
| 481 | #endif |
| 482 | #ifdef B1152000 |
| 483 | case 1152000: |
| 484 | return B1152000; |
| 485 | #endif |
| 486 | #ifdef B1000000 |
| 487 | case 1000000: |
| 488 | return B1000000; |
| 489 | #endif |
| 490 | #ifdef B921600 |
| 491 | case 921600: |
| 492 | return B921600; |
| 493 | #endif |
| 494 | #ifdef B614400 |
| 495 | case 614400: |
| 496 | return B614400; |
| 497 | #endif |
| 498 | #ifdef B576000 |
| 499 | case 576000: |
| 500 | return B576000; |
| 501 | #endif |
| 502 | #ifdef B500000 |
| 503 | case 500000: |
| 504 | return B500000; |
| 505 | #endif |
| 506 | #ifdef B460800 |
| 507 | case 460800: |
| 508 | return B460800; |
| 509 | #endif |
| 510 | #ifdef B307200 |
| 511 | case 307200: |
| 512 | return B307200; |
| 513 | #endif |
| 514 | #ifdef B230400 |
| 515 | case 230400: |
| 516 | return B230400; |
| 517 | #endif |
| 518 | #ifdef B153600 |
| 519 | case 153600: |
| 520 | return B153600; |
| 521 | #endif |
| 522 | #ifdef B115200 |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 523 | case 115200: |
| 524 | return B115200; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 525 | #endif |
| 526 | #ifdef B76800 |
| 527 | case 76800: |
| 528 | return B76800; |
| 529 | #endif |
| 530 | #ifdef B57600 |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 531 | case 57600: |
| 532 | return B57600; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 533 | #endif |
| 534 | #ifdef B38400 |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 535 | case 38400: |
| 536 | return B38400; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 537 | #endif |
| 538 | #ifdef B19200 |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 539 | case 19200: |
| 540 | return B19200; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 541 | #endif |
| 542 | #ifdef B9600 |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 543 | case 9600: |
| 544 | return B9600; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 545 | #endif |
| 546 | #ifdef B4800 |
| 547 | case 4800: |
| 548 | return B4800; |
| 549 | #endif |
| 550 | #ifdef B2400 |
| 551 | case 2400: |
| 552 | return B2400; |
| 553 | #endif |
| 554 | #ifdef B1800 |
| 555 | case 1800: |
| 556 | return B1800; |
| 557 | #endif |
| 558 | #ifdef B1200 |
| 559 | case 1200: |
| 560 | return B1200; |
| 561 | #endif |
| 562 | #ifdef B600 |
| 563 | case 600: |
| 564 | return B600; |
| 565 | #endif |
| 566 | #ifdef B300 |
| 567 | case 300: |
| 568 | return B300; |
| 569 | #endif |
| 570 | #ifdef B200 |
| 571 | case 200: |
| 572 | return B200; |
| 573 | #endif |
| 574 | #ifdef B150 |
| 575 | case 150: |
| 576 | return B150; |
| 577 | #endif |
| 578 | #ifdef B134 |
| 579 | case 134: |
| 580 | return B134; |
| 581 | #endif |
| 582 | #ifdef B110 |
| 583 | case 110: |
| 584 | return B110; |
| 585 | #endif |
| 586 | #ifdef B75 |
| 587 | case 75: |
| 588 | return B75; |
| 589 | #endif |
| 590 | #ifdef B50 |
| 591 | case 50: |
| 592 | return B50; |
| 593 | #endif |
| 594 | default: |
Pali Rohár | 93b5563 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 595 | #ifdef BOTHER |
| 596 | return BOTHER; |
| 597 | #else |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 598 | return B0; |
Pali Rohár | 93b5563 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 599 | #endif |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 600 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int |
Marek Behún | 99a3d023 | 2021-09-24 23:07:07 +0200 | [diff] [blame] | 604 | _is_within_tolerance(int value, int reference, int tolerance) |
| 605 | { |
| 606 | return 100 * value >= reference * (100 - tolerance) && |
| 607 | 100 * value <= reference * (100 + tolerance); |
| 608 | } |
| 609 | |
| 610 | static int |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 611 | kwboot_tty_change_baudrate(int fd, int baudrate) |
| 612 | { |
| 613 | struct termios tio; |
| 614 | speed_t speed; |
| 615 | int rc; |
| 616 | |
| 617 | rc = tcgetattr(fd, &tio); |
| 618 | if (rc) |
| 619 | return rc; |
| 620 | |
| 621 | speed = kwboot_tty_baudrate_to_speed(baudrate); |
| 622 | if (speed == B0) { |
| 623 | errno = EINVAL; |
| 624 | return -1; |
| 625 | } |
| 626 | |
Pali Rohár | 93b5563 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 627 | #ifdef BOTHER |
| 628 | if (speed == BOTHER) |
| 629 | tio.c_ospeed = tio.c_ispeed = baudrate; |
| 630 | #endif |
| 631 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 632 | rc = cfsetospeed(&tio, speed); |
| 633 | if (rc) |
| 634 | return rc; |
| 635 | |
| 636 | rc = cfsetispeed(&tio, speed); |
| 637 | if (rc) |
| 638 | return rc; |
| 639 | |
| 640 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 641 | if (rc) |
| 642 | return rc; |
| 643 | |
Marek Behún | 99a3d023 | 2021-09-24 23:07:07 +0200 | [diff] [blame] | 644 | rc = tcgetattr(fd, &tio); |
| 645 | if (rc) |
| 646 | return rc; |
| 647 | |
| 648 | if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed) |
| 649 | goto baud_fail; |
| 650 | |
| 651 | #ifdef BOTHER |
| 652 | /* |
| 653 | * Check whether set baudrate is within 3% tolerance. |
| 654 | * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed |
| 655 | * with real values. |
| 656 | */ |
| 657 | if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3)) |
| 658 | goto baud_fail; |
| 659 | |
| 660 | if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3)) |
| 661 | goto baud_fail; |
| 662 | #endif |
| 663 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 664 | return 0; |
Marek Behún | 99a3d023 | 2021-09-24 23:07:07 +0200 | [diff] [blame] | 665 | |
| 666 | baud_fail: |
| 667 | fprintf(stderr, "Could not set baudrate to requested value\n"); |
| 668 | errno = EINVAL; |
| 669 | return -1; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static int |
| 673 | kwboot_open_tty(const char *path, int baudrate) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 674 | { |
Pali Rohár | 911515b | 2021-09-24 23:07:10 +0200 | [diff] [blame] | 675 | int rc, fd, flags; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 676 | struct termios tio; |
| 677 | |
| 678 | rc = -1; |
| 679 | |
Marek Behún | 5fa04f4 | 2021-09-24 23:07:11 +0200 | [diff] [blame] | 680 | fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 681 | if (fd < 0) |
| 682 | goto out; |
| 683 | |
Pali Rohár | c704e0e | 2021-09-24 23:07:08 +0200 | [diff] [blame] | 684 | rc = tcgetattr(fd, &tio); |
| 685 | if (rc) |
| 686 | goto out; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 687 | |
Pali Rohár | c704e0e | 2021-09-24 23:07:08 +0200 | [diff] [blame] | 688 | cfmakeraw(&tio); |
Marek Behún | 5fa04f4 | 2021-09-24 23:07:11 +0200 | [diff] [blame] | 689 | tio.c_cflag |= CREAD | CLOCAL; |
Pali Rohár | 2ecca3d | 2021-10-25 15:12:53 +0200 | [diff] [blame] | 690 | tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 691 | tio.c_cc[VMIN] = 1; |
Pali Rohár | 24a471b | 2021-09-24 23:07:09 +0200 | [diff] [blame] | 692 | tio.c_cc[VTIME] = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 693 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 694 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 695 | if (rc) |
| 696 | goto out; |
| 697 | |
Pali Rohár | 911515b | 2021-09-24 23:07:10 +0200 | [diff] [blame] | 698 | flags = fcntl(fd, F_GETFL); |
| 699 | if (flags < 0) |
| 700 | goto out; |
| 701 | |
| 702 | rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY); |
| 703 | if (rc) |
| 704 | goto out; |
| 705 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 706 | rc = kwboot_tty_change_baudrate(fd, baudrate); |
| 707 | if (rc) |
| 708 | goto out; |
| 709 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 710 | rc = fd; |
| 711 | out: |
| 712 | if (rc < 0) { |
| 713 | if (fd >= 0) |
| 714 | close(fd); |
| 715 | } |
| 716 | |
| 717 | return rc; |
| 718 | } |
| 719 | |
| 720 | static int |
| 721 | kwboot_bootmsg(int tty, void *msg) |
| 722 | { |
Pali Rohár | 2bcd5b1 | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 723 | struct kwboot_block block; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 724 | int rc; |
| 725 | char c; |
Jon Nettleton | 9ca6fae | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 726 | int count; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 727 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 728 | if (msg == NULL) |
| 729 | kwboot_printv("Please reboot the target into UART boot mode..."); |
| 730 | else |
| 731 | kwboot_printv("Sending boot message. Please reboot the target..."); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 732 | |
| 733 | do { |
| 734 | rc = tcflush(tty, TCIOFLUSH); |
| 735 | if (rc) |
| 736 | break; |
| 737 | |
Jon Nettleton | 9ca6fae | 2018-08-13 18:24:38 +0300 | [diff] [blame] | 738 | for (count = 0; count < 128; count++) { |
Pali Rohár | cab817d | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 739 | rc = kwboot_tty_send(tty, msg, 8, 0); |
Pali Rohár | d8865f8 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 740 | if (rc) |
| 741 | break; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 744 | rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 745 | |
| 746 | kwboot_spinner(); |
| 747 | |
| 748 | } while (rc || c != NAK); |
| 749 | |
| 750 | kwboot_printv("\n"); |
| 751 | |
Pali Rohár | 2bcd5b1 | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 752 | if (rc) |
| 753 | return rc; |
| 754 | |
| 755 | /* |
| 756 | * At this stage we have sent more boot message patterns and BootROM |
| 757 | * (at least on Armada XP and 385) started interpreting sent bytes as |
| 758 | * part of xmodem packets. If BootROM is expecting SOH byte as start of |
| 759 | * a xmodem packet and it receives byte 0xff, then it throws it away and |
| 760 | * sends a NAK reply to host. If BootROM does not receive any byte for |
| 761 | * 2s when expecting some continuation of the xmodem packet, it throws |
| 762 | * away the partially received xmodem data and sends NAK reply to host. |
| 763 | * |
| 764 | * Therefore for starting xmodem transfer we have two options: Either |
| 765 | * wait 2s or send 132 0xff bytes (which is the size of xmodem packet) |
| 766 | * to ensure that BootROM throws away any partially received data. |
| 767 | */ |
| 768 | |
| 769 | /* flush output queue with remaining boot message patterns */ |
Pali Rohár | d8865f8 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 770 | rc = tcflush(tty, TCOFLUSH); |
| 771 | if (rc) { |
| 772 | perror("Failed to flush output queue"); |
| 773 | return rc; |
| 774 | } |
Pali Rohár | 2bcd5b1 | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 775 | |
| 776 | /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */ |
| 777 | memset(&block, 0xff, sizeof(block)); |
Pali Rohár | d8865f8 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 778 | rc = kwboot_tty_send(tty, &block, sizeof(block), 0); |
| 779 | if (rc) { |
| 780 | perror("Failed to send sync sequence"); |
| 781 | return rc; |
| 782 | } |
Pali Rohár | 2bcd5b1 | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 783 | |
| 784 | /* |
| 785 | * Sending 132 bytes via 115200B/8-N-1 takes 11.45 ms, reading 132 bytes |
| 786 | * takes 11.45 ms, so waiting for 30 ms should be enough. |
| 787 | */ |
| 788 | usleep(30 * 1000); |
| 789 | |
| 790 | /* flush remaining NAK replies from input queue */ |
Pali Rohár | d8865f8 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 791 | rc = tcflush(tty, TCIFLUSH); |
| 792 | if (rc) { |
| 793 | perror("Failed to flush input queue"); |
| 794 | return rc; |
| 795 | } |
Pali Rohár | 2bcd5b1 | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 796 | |
| 797 | return 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static int |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 801 | kwboot_debugmsg(int tty, void *msg) |
| 802 | { |
| 803 | int rc; |
| 804 | |
| 805 | kwboot_printv("Sending debug message. Please reboot the target..."); |
| 806 | |
| 807 | do { |
| 808 | char buf[16]; |
| 809 | |
| 810 | rc = tcflush(tty, TCIOFLUSH); |
| 811 | if (rc) |
| 812 | break; |
| 813 | |
Pali Rohár | cab817d | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 814 | rc = kwboot_tty_send(tty, msg, 8, 0); |
Pali Rohár | d8865f8 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 815 | if (rc) |
| 816 | break; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 817 | |
| 818 | rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo); |
| 819 | |
| 820 | kwboot_spinner(); |
| 821 | |
| 822 | } while (rc); |
| 823 | |
| 824 | kwboot_printv("\n"); |
| 825 | |
| 826 | return rc; |
| 827 | } |
| 828 | |
Pali Rohár | c5d666a | 2021-09-24 23:06:44 +0200 | [diff] [blame] | 829 | static size_t |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 830 | kwboot_xm_makeblock(struct kwboot_block *block, const void *data, |
| 831 | size_t size, int pnum) |
| 832 | { |
Marek Behún | d8cc851 | 2021-09-24 23:06:45 +0200 | [diff] [blame] | 833 | size_t i, n; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 834 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 835 | block->soh = SOH; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 836 | block->pnum = pnum; |
| 837 | block->_pnum = ~block->pnum; |
| 838 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 839 | n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 840 | memcpy(&block->data[0], data, n); |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 841 | memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 842 | |
| 843 | block->csum = 0; |
| 844 | for (i = 0; i < n; i++) |
| 845 | block->csum += block->data[i]; |
| 846 | |
| 847 | return n; |
| 848 | } |
| 849 | |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 850 | static uint64_t |
| 851 | _now(void) |
| 852 | { |
| 853 | struct timespec ts; |
| 854 | |
| 855 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) { |
| 856 | static int err_print; |
| 857 | |
| 858 | if (!err_print) { |
| 859 | perror("clock_gettime() does not work"); |
| 860 | err_print = 1; |
| 861 | } |
| 862 | |
| 863 | /* this will just make the timeout not work */ |
| 864 | return -1ULL; |
| 865 | } |
| 866 | |
| 867 | return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000; |
| 868 | } |
| 869 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 870 | static int |
Marek Behún | 408ea61 | 2021-09-24 23:06:49 +0200 | [diff] [blame] | 871 | _is_xm_reply(char c) |
| 872 | { |
Pali Rohár | 94c906a | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 873 | return c == ACK || c == NAK; |
Marek Behún | 408ea61 | 2021-09-24 23:06:49 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static int |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 877 | _xm_reply_to_error(int c) |
| 878 | { |
| 879 | int rc = -1; |
| 880 | |
| 881 | switch (c) { |
| 882 | case ACK: |
| 883 | rc = 0; |
| 884 | break; |
| 885 | case NAK: |
| 886 | errno = EBADMSG; |
| 887 | break; |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 888 | default: |
| 889 | errno = EPROTO; |
| 890 | break; |
| 891 | } |
| 892 | |
| 893 | return rc; |
| 894 | } |
| 895 | |
| 896 | static int |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 897 | kwboot_baud_magic_handle(int fd, char c, int baudrate) |
| 898 | { |
| 899 | static size_t rcv_len; |
| 900 | |
| 901 | if (rcv_len < sizeof(kwb_baud_magic)) { |
| 902 | /* try to recognize whole magic word */ |
| 903 | if (c == kwb_baud_magic[rcv_len]) { |
| 904 | rcv_len++; |
| 905 | } else { |
| 906 | printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c); |
| 907 | fflush(stdout); |
| 908 | rcv_len = 0; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if (rcv_len == sizeof(kwb_baud_magic)) { |
| 913 | /* magic word received */ |
| 914 | kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate); |
| 915 | |
| 916 | return kwboot_tty_change_baudrate(fd, baudrate) ? : 1; |
| 917 | } else { |
| 918 | return 0; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | static int |
Pali Rohár | 950ed24 | 2022-01-25 18:13:04 +0100 | [diff] [blame] | 923 | kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm, |
Pali Rohár | 82a9e13 | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 924 | int ignore_nak_reply, |
Pali Rohár | a6fcac2 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 925 | int allow_non_xm, int *non_xm_print, |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 926 | int baudrate, int *baud_changed) |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 927 | { |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 928 | int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo; |
Marek Behún | 819cd32 | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 929 | uint64_t recv_until = _now() + timeout; |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 930 | int rc; |
| 931 | |
| 932 | while (1) { |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 933 | rc = kwboot_tty_recv(fd, c, 1, timeout); |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 934 | if (rc) { |
| 935 | if (errno != ETIMEDOUT) |
| 936 | return rc; |
Marek Behún | 819cd32 | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 937 | else if (allow_non_xm && *non_xm_print) |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 938 | return -1; |
| 939 | else |
| 940 | *c = NAK; |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* If received xmodem reply, end. */ |
Pali Rohár | 82a9e13 | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 944 | if (_is_xm_reply(*c)) { |
| 945 | if (*c == NAK && ignore_nak_reply) { |
| 946 | timeout = recv_until - _now(); |
| 947 | if (timeout >= 0) |
| 948 | continue; |
| 949 | } |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 950 | break; |
Pali Rohár | 82a9e13 | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 951 | } |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 952 | |
| 953 | /* |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 954 | * If receiving/printing non-xmodem text output is allowed and |
| 955 | * such a byte was received, we want to increase receiving time |
| 956 | * and either: |
| 957 | * - print the byte, if it is not part of baudrate change magic |
| 958 | * sequence while baudrate change was requested (-B option) |
| 959 | * - change baudrate |
Marek Behún | 819cd32 | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 960 | * Otherwise decrease timeout by time elapsed. |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 961 | */ |
| 962 | if (allow_non_xm) { |
Marek Behún | 12df7b7 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 963 | recv_until = _now() + timeout; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 964 | |
| 965 | if (baudrate && !*baud_changed) { |
| 966 | rc = kwboot_baud_magic_handle(fd, *c, baudrate); |
| 967 | if (rc == 1) |
| 968 | *baud_changed = 1; |
| 969 | else if (!rc) |
| 970 | *non_xm_print = 1; |
| 971 | else |
| 972 | return rc; |
| 973 | } else if (!baudrate || !*baud_changed) { |
| 974 | putchar(*c); |
| 975 | fflush(stdout); |
| 976 | *non_xm_print = 1; |
| 977 | } |
Marek Behún | 819cd32 | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 978 | } else { |
Pali Rohár | 950ed24 | 2022-01-25 18:13:04 +0100 | [diff] [blame] | 979 | if (stop_on_non_xm) |
Pali Rohár | a6fcac2 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 980 | break; |
Marek Behún | 819cd32 | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 981 | timeout = recv_until - _now(); |
| 982 | if (timeout < 0) { |
| 983 | errno = ETIMEDOUT; |
| 984 | return -1; |
| 985 | } |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int |
| 993 | kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm, |
Pali Rohár | 5875ad4 | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 994 | int *done_print, int baudrate, int allow_retries) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 995 | { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 996 | int non_xm_print, baud_changed; |
| 997 | int rc, err, retries; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 998 | char c; |
| 999 | |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1000 | *done_print = 0; |
Pali Rohár | 455c0d2 | 2021-10-27 20:56:58 +0200 | [diff] [blame] | 1001 | non_xm_print = 0; |
| 1002 | baud_changed = 0; |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1003 | |
Pali Rohár | d14a342 | 2021-10-25 15:13:03 +0200 | [diff] [blame] | 1004 | retries = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1005 | do { |
Pali Rohár | cab817d | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 1006 | rc = kwboot_tty_send(fd, block, sizeof(*block), 1); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1007 | if (rc) |
Pali Rohár | 94c906a | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1008 | goto err; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1009 | |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1010 | if (allow_non_xm && !*done_print) { |
| 1011 | kwboot_progress(100, '.'); |
| 1012 | kwboot_printv("Done\n"); |
| 1013 | *done_print = 1; |
| 1014 | } |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1015 | |
Pali Rohár | a6fcac2 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1016 | rc = kwboot_xm_recv_reply(fd, &c, retries < 3, |
Pali Rohár | 82a9e13 | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1017 | retries > 8, |
Pali Rohár | a6fcac2 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1018 | allow_non_xm, &non_xm_print, |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1019 | baudrate, &baud_changed); |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1020 | if (rc) |
Pali Rohár | 94c906a | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1021 | goto err; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1022 | |
Pali Rohár | 5d8aa4c | 2022-01-25 18:13:06 +0100 | [diff] [blame] | 1023 | if (!allow_non_xm && c != ACK) { |
| 1024 | if (c == NAK && allow_retries && retries + 1 < 16) |
| 1025 | kwboot_progress(-1, '+'); |
| 1026 | else |
| 1027 | kwboot_progress(-1, 'E'); |
| 1028 | } |
Pali Rohár | 5875ad4 | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1029 | } while (c == NAK && allow_retries && retries++ < 16); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1030 | |
Marek Behún | 2e81b3a | 2021-09-24 23:06:51 +0200 | [diff] [blame] | 1031 | if (non_xm_print) |
| 1032 | kwboot_printv("\n"); |
| 1033 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1034 | if (allow_non_xm && baudrate && !baud_changed) { |
| 1035 | fprintf(stderr, "Baudrate was not changed\n"); |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1036 | errno = EPROTO; |
Pali Rohár | 94c906a | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1037 | return -1; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1040 | return _xm_reply_to_error(c); |
Pali Rohár | 94c906a | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1041 | err: |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1042 | err = errno; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1043 | kwboot_printv("\n"); |
| 1044 | errno = err; |
| 1045 | return rc; |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1046 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1047 | |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1048 | static int |
| 1049 | kwboot_xm_finish(int fd) |
| 1050 | { |
| 1051 | int rc, retries; |
| 1052 | char c; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1053 | |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1054 | kwboot_printv("Finishing transfer\n"); |
| 1055 | |
Pali Rohár | d14a342 | 2021-10-25 15:13:03 +0200 | [diff] [blame] | 1056 | retries = 0; |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1057 | do { |
| 1058 | rc = kwboot_tty_send_char(fd, EOT); |
| 1059 | if (rc) |
| 1060 | return rc; |
| 1061 | |
Pali Rohár | a6fcac2 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1062 | rc = kwboot_xm_recv_reply(fd, &c, retries < 3, |
Pali Rohár | 82a9e13 | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1063 | retries > 8, |
Pali Rohár | a6fcac2 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1064 | 0, NULL, 0, NULL); |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1065 | if (rc) |
| 1066 | return rc; |
Pali Rohár | d14a342 | 2021-10-25 15:13:03 +0200 | [diff] [blame] | 1067 | } while (c == NAK && retries++ < 16); |
Pali Rohár | 9cdc264 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1068 | |
| 1069 | return _xm_reply_to_error(c); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | static int |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1073 | kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data, |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1074 | size_t size, int baudrate) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1075 | { |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1076 | int done_print = 0; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1077 | size_t sent, left; |
| 1078 | int rc; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1079 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1080 | kwboot_printv("Sending boot image %s (%zu bytes)...\n", |
| 1081 | header ? "header" : "data", size); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1082 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1083 | left = size; |
| 1084 | sent = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1085 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1086 | while (sent < size) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1087 | struct kwboot_block block; |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1088 | int last_block; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1089 | size_t blksz; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1090 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1091 | blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++); |
| 1092 | data += blksz; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1093 | |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1094 | last_block = (left <= blksz); |
| 1095 | |
Pali Rohár | 5875ad4 | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1096 | /* |
| 1097 | * Handling of repeated xmodem packets is completely broken in |
| 1098 | * Armada 385 BootROM - it completely ignores xmodem packet |
| 1099 | * numbers, they are only used for checksum verification. |
| 1100 | * BootROM can handle a retry of the xmodem packet only during |
| 1101 | * the transmission of kwbimage header and only if BootROM |
| 1102 | * itself sent NAK response to previous attempt (it does it on |
| 1103 | * checksum failure). During the transmission of kwbimage data |
| 1104 | * part, BootROM always expects next xmodem packet, even if it |
| 1105 | * sent NAK to previous attempt - there is absolutely no way to |
| 1106 | * repair incorrectly transmitted xmodem packet during kwbimage |
| 1107 | * data part upload. Also, if kwboot receives non-ACK/NAK |
| 1108 | * response (meaning that original BootROM response was damaged |
| 1109 | * on UART) there is no way to detect if BootROM accepted xmodem |
| 1110 | * packet or not and no way to check if kwboot could repeat the |
| 1111 | * packet or not. |
| 1112 | * |
| 1113 | * Stop transfer and return failure if kwboot receives unknown |
| 1114 | * reply if non-xmodem reply is not allowed (for all xmodem |
| 1115 | * packets except the last header packet) or when non-ACK reply |
| 1116 | * is received during data part transfer. |
| 1117 | */ |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1118 | rc = kwboot_xm_sendblock(tty, &block, header && last_block, |
Pali Rohár | 5875ad4 | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1119 | &done_print, baudrate, header); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1120 | if (rc) |
| 1121 | goto out; |
| 1122 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1123 | sent += blksz; |
| 1124 | left -= blksz; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1125 | |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1126 | if (!done_print) |
| 1127 | kwboot_progress(sent * 100 / size, '.'); |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1128 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1129 | |
Pali Rohár | 48b3ea6 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1130 | if (!done_print) |
| 1131 | kwboot_printv("Done\n"); |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1132 | |
| 1133 | return 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1134 | out: |
Pali Rohár | d5ba8db | 2021-09-24 23:06:47 +0200 | [diff] [blame] | 1135 | kwboot_printv("\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1136 | return rc; |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1137 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1138 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1139 | static int |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1140 | kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate) |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1141 | { |
| 1142 | const uint8_t *img = _img; |
| 1143 | int rc, pnum; |
| 1144 | size_t hdrsz; |
| 1145 | |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1146 | hdrsz = kwbheader_size(img); |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1147 | |
Pali Rohár | f8017c3 | 2021-11-05 23:29:58 +0100 | [diff] [blame] | 1148 | /* |
| 1149 | * If header size is not aligned to xmodem block size (which applies |
| 1150 | * for all images in kwbimage v0 format) then we have to ensure that |
| 1151 | * the last xmodem block of header contains beginning of the data |
| 1152 | * followed by the header. So align header size to xmodem block size. |
| 1153 | */ |
| 1154 | hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ; |
| 1155 | |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1156 | pnum = 1; |
| 1157 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1158 | rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate); |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1159 | if (rc) |
| 1160 | return rc; |
| 1161 | |
Pali Rohár | f8017c3 | 2021-11-05 23:29:58 +0100 | [diff] [blame] | 1162 | /* |
| 1163 | * If we have already sent image data as a part of the last |
| 1164 | * xmodem header block then we have nothing more to send. |
| 1165 | */ |
| 1166 | if (hdrsz < size) { |
| 1167 | img += hdrsz; |
| 1168 | size -= hdrsz; |
| 1169 | rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0); |
| 1170 | if (rc) |
| 1171 | return rc; |
| 1172 | } |
Pali Rohár | 2ef87f7 | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1173 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1174 | rc = kwboot_xm_finish(tty); |
| 1175 | if (rc) |
| 1176 | return rc; |
| 1177 | |
| 1178 | if (baudrate) { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1179 | kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n"); |
| 1180 | rc = kwboot_tty_change_baudrate(tty, 115200); |
| 1181 | if (rc) |
| 1182 | return rc; |
| 1183 | } |
| 1184 | |
| 1185 | return 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | static int |
Marek Behún | 46237e6 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 1189 | kwboot_term_pipe(int in, int out, const char *quit, int *s) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1190 | { |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1191 | char buf[128]; |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 1192 | ssize_t nin; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1193 | |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1194 | nin = read(in, buf, sizeof(buf)); |
Willy Tarreau | 4469bd7 | 2018-07-03 12:10:31 -0400 | [diff] [blame] | 1195 | if (nin <= 0) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1196 | return -1; |
| 1197 | |
| 1198 | if (quit) { |
| 1199 | int i; |
| 1200 | |
| 1201 | for (i = 0; i < nin; i++) { |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1202 | if (buf[i] == quit[*s]) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1203 | (*s)++; |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1204 | if (!quit[*s]) { |
Pali Rohár | 7938b3b | 2022-02-18 12:24:13 +0100 | [diff] [blame] | 1205 | nin = (i > *s) ? (i - *s) : 0; |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1206 | break; |
| 1207 | } |
Pali Rohár | b943eee | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 1208 | } else { |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1209 | if (*s > i && kwboot_write(out, quit, *s - i) < 0) |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 1210 | return -1; |
| 1211 | *s = 0; |
Pali Rohár | b943eee | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 1212 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1213 | } |
Pali Rohár | de75140 | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1214 | |
| 1215 | if (i == nin) |
Pali Rohár | 7938b3b | 2022-02-18 12:24:13 +0100 | [diff] [blame] | 1216 | nin -= (nin > *s) ? *s : nin; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Marek Behún | e453bb4 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 1219 | if (kwboot_write(out, buf, nin) < 0) |
| 1220 | return -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1221 | |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | static int |
| 1226 | kwboot_terminal(int tty) |
| 1227 | { |
| 1228 | int rc, in, s; |
Marek Behún | 46237e6 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 1229 | const char *quit = "\34c"; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1230 | struct termios otio, tio; |
| 1231 | |
| 1232 | rc = -1; |
| 1233 | |
| 1234 | in = STDIN_FILENO; |
| 1235 | if (isatty(in)) { |
| 1236 | rc = tcgetattr(in, &otio); |
| 1237 | if (!rc) { |
| 1238 | tio = otio; |
| 1239 | cfmakeraw(&tio); |
| 1240 | rc = tcsetattr(in, TCSANOW, &tio); |
| 1241 | } |
| 1242 | if (rc) { |
| 1243 | perror("tcsetattr"); |
| 1244 | goto out; |
| 1245 | } |
| 1246 | |
| 1247 | kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", |
Marek Behún | 5fa04f4 | 2021-09-24 23:07:11 +0200 | [diff] [blame] | 1248 | quit[0] | 0100, quit[1]); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1249 | } else |
| 1250 | in = -1; |
| 1251 | |
| 1252 | rc = 0; |
| 1253 | s = 0; |
| 1254 | |
| 1255 | do { |
| 1256 | fd_set rfds; |
| 1257 | int nfds = 0; |
| 1258 | |
Pali Rohár | 0a14341 | 2021-10-25 15:12:52 +0200 | [diff] [blame] | 1259 | FD_ZERO(&rfds); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1260 | FD_SET(tty, &rfds); |
| 1261 | nfds = nfds < tty ? tty : nfds; |
| 1262 | |
| 1263 | if (in >= 0) { |
| 1264 | FD_SET(in, &rfds); |
| 1265 | nfds = nfds < in ? in : nfds; |
| 1266 | } |
| 1267 | |
| 1268 | nfds = select(nfds + 1, &rfds, NULL, NULL, NULL); |
| 1269 | if (nfds < 0) |
| 1270 | break; |
| 1271 | |
| 1272 | if (FD_ISSET(tty, &rfds)) { |
| 1273 | rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL); |
| 1274 | if (rc) |
| 1275 | break; |
| 1276 | } |
| 1277 | |
Marek Behún | f30cb0d | 2021-09-24 23:06:39 +0200 | [diff] [blame] | 1278 | if (in >= 0 && FD_ISSET(in, &rfds)) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1279 | rc = kwboot_term_pipe(in, tty, quit, &s); |
| 1280 | if (rc) |
| 1281 | break; |
| 1282 | } |
| 1283 | } while (quit[s] != 0); |
| 1284 | |
Pali Rohár | ec0fe5b | 2021-07-23 11:14:18 +0200 | [diff] [blame] | 1285 | if (in >= 0) |
| 1286 | tcsetattr(in, TCSANOW, &otio); |
Pali Rohár | 49a0a3b | 2021-07-23 11:14:19 +0200 | [diff] [blame] | 1287 | printf("\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1288 | out: |
| 1289 | return rc; |
| 1290 | } |
| 1291 | |
| 1292 | static void * |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1293 | kwboot_read_image(const char *path, size_t *size, size_t reserve) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1294 | { |
Pali Rohár | ddc04fa | 2021-09-24 23:06:55 +0200 | [diff] [blame] | 1295 | int rc, fd; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1296 | struct stat st; |
| 1297 | void *img; |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1298 | off_t tot; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1299 | |
| 1300 | rc = -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1301 | img = NULL; |
| 1302 | |
| 1303 | fd = open(path, O_RDONLY); |
| 1304 | if (fd < 0) |
| 1305 | goto out; |
| 1306 | |
| 1307 | rc = fstat(fd, &st); |
| 1308 | if (rc) |
| 1309 | goto out; |
| 1310 | |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1311 | img = malloc(st.st_size + reserve); |
| 1312 | if (!img) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1313 | goto out; |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1314 | |
| 1315 | tot = 0; |
| 1316 | while (tot < st.st_size) { |
| 1317 | ssize_t rd = read(fd, img + tot, st.st_size - tot); |
| 1318 | |
| 1319 | if (rd < 0) |
| 1320 | goto out; |
| 1321 | |
| 1322 | tot += rd; |
| 1323 | |
| 1324 | if (!rd && tot < st.st_size) { |
| 1325 | errno = EIO; |
| 1326 | goto out; |
| 1327 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | rc = 0; |
| 1331 | *size = st.st_size; |
| 1332 | out: |
| 1333 | if (rc && img) { |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1334 | free(img); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1335 | img = NULL; |
| 1336 | } |
| 1337 | if (fd >= 0) |
| 1338 | close(fd); |
| 1339 | |
| 1340 | return img; |
| 1341 | } |
| 1342 | |
| 1343 | static uint8_t |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1344 | kwboot_hdr_csum8(const void *hdr) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1345 | { |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1346 | const uint8_t *data = hdr; |
| 1347 | uint8_t csum; |
| 1348 | size_t size; |
| 1349 | |
| 1350 | size = kwbheader_size_for_csum(hdr); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1351 | |
| 1352 | for (csum = 0; size-- > 0; data++) |
| 1353 | csum += *data; |
| 1354 | |
| 1355 | return csum; |
| 1356 | } |
| 1357 | |
Pali Rohár | ad9a3ac | 2021-10-25 15:12:55 +0200 | [diff] [blame] | 1358 | static uint32_t * |
| 1359 | kwboot_img_csum32_ptr(void *img) |
| 1360 | { |
| 1361 | struct main_hdr_v1 *hdr = img; |
| 1362 | uint32_t datasz; |
| 1363 | |
| 1364 | datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t); |
| 1365 | |
| 1366 | return img + le32_to_cpu(hdr->srcaddr) + datasz; |
| 1367 | } |
| 1368 | |
| 1369 | static uint32_t |
| 1370 | kwboot_img_csum32(const void *img) |
| 1371 | { |
| 1372 | const struct main_hdr_v1 *hdr = img; |
| 1373 | uint32_t datasz, csum = 0; |
| 1374 | const uint32_t *data; |
| 1375 | |
| 1376 | datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum); |
| 1377 | if (datasz % sizeof(uint32_t)) |
| 1378 | return 0; |
| 1379 | |
| 1380 | data = img + le32_to_cpu(hdr->srcaddr); |
| 1381 | while (datasz > 0) { |
| 1382 | csum += le32_to_cpu(*data++); |
| 1383 | datasz -= 4; |
| 1384 | } |
| 1385 | |
| 1386 | return cpu_to_le32(csum); |
| 1387 | } |
| 1388 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1389 | static int |
Pali Rohár | 550c930 | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1390 | kwboot_img_is_secure(void *img) |
| 1391 | { |
| 1392 | struct opt_hdr_v1 *ohdr; |
| 1393 | |
| 1394 | for_each_opt_hdr_v1 (ohdr, img) |
| 1395 | if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE) |
| 1396 | return 1; |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1401 | static void * |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1402 | kwboot_img_grow_data_right(void *img, size_t *size, size_t grow) |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1403 | { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1404 | struct main_hdr_v1 *hdr = img; |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1405 | void *result; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1406 | |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1407 | /* |
| 1408 | * 32-bit checksum comes after end of image code, so we will be putting |
| 1409 | * new code there. So we get this pointer and then increase data size |
| 1410 | * (since increasing data size changes kwboot_img_csum32_ptr() return |
| 1411 | * value). |
| 1412 | */ |
| 1413 | result = kwboot_img_csum32_ptr(img); |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1414 | hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow); |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1415 | *size += grow; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1416 | |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1417 | return result; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1420 | static void |
| 1421 | kwboot_img_grow_hdr(void *img, size_t *size, size_t grow) |
| 1422 | { |
| 1423 | uint32_t hdrsz, datasz, srcaddr; |
| 1424 | struct main_hdr_v1 *hdr = img; |
Pali Rohár | d656f5a | 2021-10-25 15:13:02 +0200 | [diff] [blame] | 1425 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1426 | uint8_t *data; |
| 1427 | |
| 1428 | srcaddr = le32_to_cpu(hdr->srcaddr); |
| 1429 | |
Pali Rohár | d656f5a | 2021-10-25 15:13:02 +0200 | [diff] [blame] | 1430 | /* calculate real used space in kwbimage header */ |
| 1431 | if (kwbimage_version(img) == 0) { |
| 1432 | hdrsz = kwbheader_size(img); |
| 1433 | } else { |
| 1434 | hdrsz = sizeof(*hdr); |
| 1435 | for_each_opt_hdr_v1 (ohdr, hdr) |
| 1436 | hdrsz += opt_hdr_v1_size(ohdr); |
| 1437 | } |
| 1438 | |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1439 | data = (uint8_t *)img + srcaddr; |
| 1440 | datasz = *size - srcaddr; |
| 1441 | |
| 1442 | /* only move data if there is not enough space */ |
| 1443 | if (hdrsz + grow > srcaddr) { |
| 1444 | size_t need = hdrsz + grow - srcaddr; |
| 1445 | |
| 1446 | /* move data by enough bytes */ |
| 1447 | memmove(data + need, data, datasz); |
| 1448 | |
| 1449 | hdr->srcaddr = cpu_to_le32(srcaddr + need); |
| 1450 | *size += need; |
| 1451 | } |
| 1452 | |
| 1453 | if (kwbimage_version(img) == 1) { |
| 1454 | hdrsz += grow; |
Pali Rohár | d656f5a | 2021-10-25 15:13:02 +0200 | [diff] [blame] | 1455 | if (hdrsz > kwbheader_size(img)) { |
| 1456 | hdr->headersz_msb = hdrsz >> 16; |
| 1457 | hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff); |
| 1458 | } |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1462 | static void * |
| 1463 | kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz) |
| 1464 | { |
| 1465 | struct main_hdr_v1 *hdr = img; |
| 1466 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | a85a71d | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1467 | uint32_t num_args; |
| 1468 | uint32_t offset; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1469 | uint32_t ohdrsz; |
Pali Rohár | e511cc3 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1470 | uint8_t *prev_ext; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1471 | |
Pali Rohár | 4469103 | 2022-01-12 18:20:52 +0100 | [diff] [blame] | 1472 | if (hdr->ext) { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1473 | for_each_opt_hdr_v1 (ohdr, img) |
| 1474 | if (opt_hdr_v1_next(ohdr) == NULL) |
| 1475 | break; |
| 1476 | |
Pali Rohár | e511cc3 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1477 | prev_ext = opt_hdr_v1_ext(ohdr); |
| 1478 | ohdr = _opt_hdr_v1_next(ohdr); |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1479 | } else { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1480 | ohdr = (void *)(hdr + 1); |
Pali Rohár | e511cc3 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1481 | prev_ext = &hdr->ext; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1482 | } |
| 1483 | |
Pali Rohár | a85a71d | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1484 | /* |
| 1485 | * ARM executable code inside the BIN header on some mvebu platforms |
| 1486 | * (e.g. A370, AXP) must always be aligned with the 128-bit boundary. |
| 1487 | * This requirement can be met by inserting dummy arguments into |
| 1488 | * BIN header, if needed. |
| 1489 | */ |
| 1490 | offset = &ohdr->data[4] - (char *)img; |
| 1491 | num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t); |
| 1492 | |
| 1493 | ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4; |
| 1494 | kwboot_img_grow_hdr(hdr, size, ohdrsz); |
| 1495 | |
Pali Rohár | 4469103 | 2022-01-12 18:20:52 +0100 | [diff] [blame] | 1496 | *prev_ext = 1; |
Pali Rohár | e511cc3 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1497 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1498 | ohdr->headertype = OPT_HDR_V1_BINARY_TYPE; |
| 1499 | ohdr->headersz_msb = ohdrsz >> 16; |
| 1500 | ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff); |
| 1501 | |
| 1502 | memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr)); |
Pali Rohár | a85a71d | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1503 | *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args); |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1504 | |
Pali Rohár | a85a71d | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1505 | return &ohdr->data[4 + 4 * num_args]; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static void |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1509 | _inject_baudrate_change_code(void *img, size_t *size, int for_data, |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1510 | int old_baud, int new_baud) |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1511 | { |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1512 | struct main_hdr_v1 *hdr = img; |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1513 | uint32_t orig_datasz; |
| 1514 | uint32_t codesz; |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1515 | uint8_t *code; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1516 | |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1517 | if (for_data) { |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1518 | orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t); |
| 1519 | |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1520 | codesz = sizeof(kwboot_baud_code) + |
| 1521 | sizeof(kwboot_baud_code_data_jump); |
| 1522 | code = kwboot_img_grow_data_right(img, size, codesz); |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1523 | } else { |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1524 | codesz = sizeof(kwboot_baud_code_binhdr_pre) + |
| 1525 | sizeof(kwboot_baud_code) + |
| 1526 | sizeof(kwboot_baud_code_binhdr_post); |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1527 | code = kwboot_add_bin_ohdr_v1(img, size, codesz); |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1528 | |
| 1529 | codesz = sizeof(kwboot_baud_code_binhdr_pre); |
| 1530 | memcpy(code, kwboot_baud_code_binhdr_pre, codesz); |
| 1531 | code += codesz; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1534 | codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t); |
| 1535 | memcpy(code, kwboot_baud_code, codesz); |
| 1536 | code += codesz; |
| 1537 | *(uint32_t *)code = cpu_to_le32(old_baud); |
| 1538 | code += sizeof(uint32_t); |
| 1539 | *(uint32_t *)code = cpu_to_le32(new_baud); |
| 1540 | code += sizeof(uint32_t); |
| 1541 | |
| 1542 | if (for_data) { |
| 1543 | codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t); |
| 1544 | memcpy(code, kwboot_baud_code_data_jump, codesz); |
| 1545 | code += codesz; |
| 1546 | *(uint32_t *)code = hdr->execaddr; |
| 1547 | code += sizeof(uint32_t); |
| 1548 | hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz); |
| 1549 | } else { |
| 1550 | codesz = sizeof(kwboot_baud_code_binhdr_post); |
| 1551 | memcpy(code, kwboot_baud_code_binhdr_post, codesz); |
| 1552 | code += codesz; |
| 1553 | } |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
Pali Rohár | 550c930 | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1556 | static int |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1557 | kwboot_img_patch(void *img, size_t *size, int baudrate) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1558 | { |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 1559 | struct main_hdr_v1 *hdr; |
Pali Rohár | 792e423 | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 1560 | uint32_t srcaddr; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1561 | uint8_t csum; |
Marek Behún | 5c8f812 | 2021-09-24 23:07:04 +0200 | [diff] [blame] | 1562 | size_t hdrsz; |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 1563 | int image_ver; |
Pali Rohár | 550c930 | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1564 | int is_secure; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1565 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1566 | hdr = img; |
| 1567 | |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1568 | if (*size < sizeof(struct main_hdr_v1)) |
| 1569 | goto err; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1570 | |
Marek Behún | acb0b38 | 2021-09-24 23:07:00 +0200 | [diff] [blame] | 1571 | image_ver = kwbimage_version(img); |
Pali Rohár | 5029d7b | 2021-07-23 11:14:22 +0200 | [diff] [blame] | 1572 | if (image_ver != 0 && image_ver != 1) { |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 1573 | fprintf(stderr, "Invalid image header version\n"); |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1574 | goto err; |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1577 | hdrsz = kwbheader_size(hdr); |
Stefan Roese | e29f1db | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 1578 | |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1579 | if (*size < hdrsz) |
| 1580 | goto err; |
Pali Rohár | 825a2ca | 2021-07-23 11:14:21 +0200 | [diff] [blame] | 1581 | |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1582 | csum = kwboot_hdr_csum8(hdr) - hdr->checksum; |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1583 | if (csum != hdr->checksum) |
| 1584 | goto err; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1585 | |
Pali Rohár | 792e423 | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 1586 | srcaddr = le32_to_cpu(hdr->srcaddr); |
| 1587 | |
| 1588 | switch (hdr->blockid) { |
| 1589 | case IBR_HDR_SATA_ID: |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1590 | if (srcaddr < 1) |
| 1591 | goto err; |
| 1592 | |
Pali Rohár | 792e423 | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 1593 | hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512); |
| 1594 | break; |
| 1595 | |
| 1596 | case IBR_HDR_SDIO_ID: |
| 1597 | hdr->srcaddr = cpu_to_le32(srcaddr * 512); |
| 1598 | break; |
| 1599 | |
| 1600 | case IBR_HDR_PEX_ID: |
| 1601 | if (srcaddr == 0xFFFFFFFF) |
| 1602 | hdr->srcaddr = cpu_to_le32(hdrsz); |
| 1603 | break; |
Pali Rohár | f2c644e | 2021-09-24 23:06:59 +0200 | [diff] [blame] | 1604 | |
| 1605 | case IBR_HDR_SPI_ID: |
| 1606 | if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) { |
| 1607 | kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n"); |
| 1608 | hdr->destaddr = cpu_to_le32(0x00800000); |
| 1609 | hdr->execaddr = cpu_to_le32(0x00800000); |
| 1610 | } |
| 1611 | break; |
Pali Rohár | 792e423 | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 1612 | } |
| 1613 | |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1614 | if (hdrsz > le32_to_cpu(hdr->srcaddr) || |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1615 | *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) |
| 1616 | goto err; |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1617 | |
Pali Rohár | ad9a3ac | 2021-10-25 15:12:55 +0200 | [diff] [blame] | 1618 | if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img)) |
| 1619 | goto err; |
| 1620 | |
Pali Rohár | 550c930 | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1621 | is_secure = kwboot_img_is_secure(img); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1622 | |
Pali Rohár | 550c930 | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1623 | if (hdr->blockid != IBR_HDR_UART_ID) { |
| 1624 | if (is_secure) { |
| 1625 | fprintf(stderr, |
| 1626 | "Image has secure header with signature for non-UART booting\n"); |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1627 | goto err; |
Pali Rohár | 550c930 | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | kwboot_printv("Patching image boot signature to UART\n"); |
| 1631 | hdr->blockid = IBR_HDR_UART_ID; |
| 1632 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1633 | |
Pali Rohár | 0089f61 | 2021-10-22 12:37:47 +0200 | [diff] [blame] | 1634 | if (!is_secure) { |
Pali Rohár | 4bebab6 | 2021-10-25 15:12:58 +0200 | [diff] [blame] | 1635 | if (image_ver == 1) { |
| 1636 | /* |
| 1637 | * Tell BootROM to send BootROM messages to UART port |
| 1638 | * number 0 (used also for UART booting) with default |
| 1639 | * baudrate (which should be 115200) and do not touch |
| 1640 | * UART MPP configuration. |
| 1641 | */ |
Pali Rohár | ffccee2 | 2022-01-25 18:13:13 +0100 | [diff] [blame] | 1642 | hdr->flags |= 0x1; |
Pali Rohár | 4bebab6 | 2021-10-25 15:12:58 +0200 | [diff] [blame] | 1643 | hdr->options &= ~0x1F; |
| 1644 | hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT; |
| 1645 | hdr->options |= 0 << 3; |
| 1646 | } |
Pali Rohár | 0089f61 | 2021-10-22 12:37:47 +0200 | [diff] [blame] | 1647 | if (image_ver == 0) |
| 1648 | ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED; |
| 1649 | hdr->nandpagesize = 0; |
| 1650 | } |
| 1651 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1652 | if (baudrate) { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1653 | if (image_ver == 0) { |
| 1654 | fprintf(stderr, |
| 1655 | "Cannot inject code for changing baudrate into v0 image header\n"); |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1656 | goto err; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | if (is_secure) { |
| 1660 | fprintf(stderr, |
| 1661 | "Cannot inject code for changing baudrate into image with secure header\n"); |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1662 | goto err; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * First inject code that changes the baudrate from the default |
| 1667 | * value of 115200 Bd to requested value. This code is inserted |
| 1668 | * as a new opt hdr, so it is executed by BootROM after the |
| 1669 | * header part is received. |
| 1670 | */ |
| 1671 | kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n", |
| 1672 | baudrate); |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1673 | _inject_baudrate_change_code(img, size, 0, 115200, baudrate); |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1674 | |
| 1675 | /* |
| 1676 | * Now inject code that changes the baudrate back to 115200 Bd. |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1677 | * This code is appended after the data part of the image, and |
| 1678 | * execaddr is changed so that it is executed before U-Boot |
| 1679 | * proper. |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1680 | */ |
| 1681 | kwboot_printv("Injecting code for changing baudrate back\n"); |
Pali Rohár | 063cb35 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1682 | _inject_baudrate_change_code(img, size, 1, baudrate, 115200); |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1683 | |
Pali Rohár | 82c5a0a | 2021-10-25 15:12:57 +0200 | [diff] [blame] | 1684 | /* Update the 32-bit data checksum */ |
| 1685 | *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img); |
| 1686 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1687 | /* recompute header size */ |
| 1688 | hdrsz = kwbheader_size(hdr); |
| 1689 | } |
| 1690 | |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1691 | if (hdrsz % KWBOOT_XM_BLKSZ) { |
Pali Rohár | ed792c2 | 2021-10-25 15:13:00 +0200 | [diff] [blame] | 1692 | size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ; |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1693 | |
| 1694 | if (is_secure) { |
| 1695 | fprintf(stderr, "Cannot align image with secure header\n"); |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1696 | goto err; |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | kwboot_printv("Aligning image header to Xmodem block size\n"); |
Pali Rohár | ed792c2 | 2021-10-25 15:13:00 +0200 | [diff] [blame] | 1700 | kwboot_img_grow_hdr(img, size, grow); |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1701 | } |
| 1702 | |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1703 | hdr->checksum = kwboot_hdr_csum8(hdr) - csum; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1704 | |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1705 | *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize); |
Marek Behún | b4eea8f | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 1706 | return 0; |
| 1707 | err: |
| 1708 | errno = EINVAL; |
| 1709 | return -1; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static void |
| 1713 | kwboot_usage(FILE *stream, char *progname) |
| 1714 | { |
| 1715 | fprintf(stream, |
Kevin Smith | 8669dac | 2016-02-16 21:28:17 +0000 | [diff] [blame] | 1716 | "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n", |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1717 | progname); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1718 | fprintf(stream, "\n"); |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1719 | fprintf(stream, |
| 1720 | " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n"); |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1721 | fprintf(stream, |
| 1722 | " -D <image>: boot <image> without preamble (Dove)\n"); |
| 1723 | fprintf(stream, " -d: enter debug mode\n"); |
| 1724 | fprintf(stream, " -a: use timings for Armada XP\n"); |
Stefan Roese | 1c0df9e | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 1725 | fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n"); |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 1726 | fprintf(stream, |
| 1727 | " -o <block-timeo>: use specific xmodem block timeout\n"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1728 | fprintf(stream, "\n"); |
| 1729 | fprintf(stream, " -t: mini terminal\n"); |
| 1730 | fprintf(stream, "\n"); |
| 1731 | fprintf(stream, " -B <baud>: set baud rate\n"); |
| 1732 | fprintf(stream, "\n"); |
| 1733 | } |
| 1734 | |
| 1735 | int |
| 1736 | main(int argc, char **argv) |
| 1737 | { |
| 1738 | const char *ttypath, *imgpath; |
Pali Rohár | ddc04fa | 2021-09-24 23:06:55 +0200 | [diff] [blame] | 1739 | int rv, rc, tty, term; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1740 | void *bootmsg; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1741 | void *debugmsg; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1742 | void *img; |
| 1743 | size_t size; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1744 | size_t after_img_rsv; |
| 1745 | int baudrate; |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1746 | int prev_optind; |
| 1747 | int c; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1748 | |
| 1749 | rv = 1; |
| 1750 | tty = -1; |
| 1751 | bootmsg = NULL; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1752 | debugmsg = NULL; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1753 | imgpath = NULL; |
| 1754 | img = NULL; |
| 1755 | term = 0; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1756 | size = 0; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1757 | after_img_rsv = KWBOOT_XM_BLKSZ; |
| 1758 | baudrate = 115200; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1759 | |
Pali Rohár | 75176dc | 2021-11-05 23:30:42 +0100 | [diff] [blame] | 1760 | printf("kwboot version %s\n", PLAIN_VERSION); |
| 1761 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1762 | kwboot_verbose = isatty(STDOUT_FILENO); |
| 1763 | |
| 1764 | do { |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1765 | prev_optind = optind; |
| 1766 | c = getopt(argc, argv, "hbptaB:dD:q:s:o:"); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1767 | if (c < 0) |
| 1768 | break; |
| 1769 | |
| 1770 | switch (c) { |
| 1771 | case 'b': |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1772 | if (imgpath || bootmsg || debugmsg) |
| 1773 | goto usage; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1774 | bootmsg = kwboot_msg_boot; |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1775 | if (prev_optind == optind) |
| 1776 | goto usage; |
Pali Rohár | 9e6d71d | 2022-02-07 10:12:24 +0100 | [diff] [blame] | 1777 | if (optind < argc - 1 && argv[optind] && argv[optind][0] != '-') |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1778 | imgpath = argv[optind++]; |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1779 | break; |
| 1780 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1781 | case 'D': |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1782 | if (imgpath || bootmsg || debugmsg) |
| 1783 | goto usage; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1784 | bootmsg = NULL; |
| 1785 | imgpath = optarg; |
| 1786 | break; |
| 1787 | |
| 1788 | case 'd': |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1789 | if (imgpath || bootmsg || debugmsg) |
| 1790 | goto usage; |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1791 | debugmsg = kwboot_msg_debug; |
| 1792 | break; |
| 1793 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1794 | case 'p': |
Pali Rohár | ddc04fa | 2021-09-24 23:06:55 +0200 | [diff] [blame] | 1795 | /* nop, for backward compatibility */ |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | |
| 1798 | case 't': |
| 1799 | term = 1; |
| 1800 | break; |
| 1801 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1802 | case 'a': |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1803 | msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP; |
| 1804 | break; |
| 1805 | |
Stefan Roese | 1c0df9e | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 1806 | case 'q': |
Pali Rohár | 132016e | 2022-03-02 11:49:19 +0100 | [diff] [blame^] | 1807 | /* nop, for backward compatibility */ |
Stefan Roese | 1c0df9e | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 1808 | break; |
| 1809 | |
| 1810 | case 's': |
| 1811 | msg_rsp_timeo = atoi(optarg); |
| 1812 | break; |
| 1813 | |
Kevin Smith | 7497a6a | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 1814 | case 'o': |
| 1815 | blk_rsp_timeo = atoi(optarg); |
| 1816 | break; |
| 1817 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1818 | case 'B': |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1819 | baudrate = atoi(optarg); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1820 | break; |
| 1821 | |
| 1822 | case 'h': |
| 1823 | rv = 0; |
| 1824 | default: |
| 1825 | goto usage; |
| 1826 | } |
| 1827 | } while (1); |
| 1828 | |
Pali Rohár | a3c6496 | 2022-01-25 18:13:12 +0100 | [diff] [blame] | 1829 | if (!bootmsg && !term && !debugmsg && !imgpath) |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1830 | goto usage; |
| 1831 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1832 | ttypath = argv[optind++]; |
| 1833 | |
Pali Rohár | c513fe4 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 1834 | if (optind != argc) |
| 1835 | goto usage; |
| 1836 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1837 | tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1838 | if (tty < 0) { |
| 1839 | perror(ttypath); |
| 1840 | goto out; |
| 1841 | } |
| 1842 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1843 | if (baudrate == 115200) |
| 1844 | /* do not change baudrate during Xmodem to the same value */ |
| 1845 | baudrate = 0; |
| 1846 | else |
| 1847 | /* ensure we have enough space for baudrate change code */ |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1848 | after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 + |
| 1849 | sizeof(kwboot_baud_code_binhdr_pre) + |
Pali Rohár | 5923ef6 | 2021-10-25 15:12:54 +0200 | [diff] [blame] | 1850 | sizeof(kwboot_baud_code) + |
Pali Rohár | 8dbe027 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1851 | sizeof(kwboot_baud_code_binhdr_post) + |
| 1852 | KWBOOT_XM_BLKSZ + |
| 1853 | sizeof(kwboot_baud_code) + |
| 1854 | sizeof(kwboot_baud_code_data_jump) + |
Pali Rohár | 5923ef6 | 2021-10-25 15:12:54 +0200 | [diff] [blame] | 1855 | KWBOOT_XM_BLKSZ; |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1856 | |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1857 | if (imgpath) { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1858 | img = kwboot_read_image(imgpath, &size, after_img_rsv); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1859 | if (!img) { |
| 1860 | perror(imgpath); |
| 1861 | goto out; |
| 1862 | } |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1863 | |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1864 | rc = kwboot_img_patch(img, &size, baudrate); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1865 | if (rc) { |
| 1866 | fprintf(stderr, "%s: Invalid image.\n", imgpath); |
| 1867 | goto out; |
| 1868 | } |
| 1869 | } |
| 1870 | |
Stefan Roese | 84899e2 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1871 | if (debugmsg) { |
| 1872 | rc = kwboot_debugmsg(tty, debugmsg); |
| 1873 | if (rc) { |
| 1874 | perror("debugmsg"); |
| 1875 | goto out; |
| 1876 | } |
Willy Tarreau | 3475a71 | 2018-07-03 12:10:30 -0400 | [diff] [blame] | 1877 | } else if (bootmsg) { |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1878 | rc = kwboot_bootmsg(tty, bootmsg); |
| 1879 | if (rc) { |
| 1880 | perror("bootmsg"); |
| 1881 | goto out; |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | if (img) { |
Pali Rohár | ca27204 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1886 | rc = kwboot_xmodem(tty, img, size, baudrate); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1887 | if (rc) { |
| 1888 | perror("xmodem"); |
| 1889 | goto out; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | if (term) { |
| 1894 | rc = kwboot_terminal(tty); |
| 1895 | if (rc && !(errno == EINTR)) { |
| 1896 | perror("terminal"); |
| 1897 | goto out; |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | rv = 0; |
| 1902 | out: |
| 1903 | if (tty >= 0) |
| 1904 | close(tty); |
| 1905 | |
| 1906 | if (img) |
Pali Rohár | 04ced02 | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1907 | free(img); |
Luka Perkov | d131ad6 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1908 | |
| 1909 | return rv; |
| 1910 | |
| 1911 | usage: |
| 1912 | kwboot_usage(rv ? stderr : stdout, basename(argv[0])); |
| 1913 | goto out; |
| 1914 | } |