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