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