blob: 3ab49e74bb67db51c9d04a6587ddaa6a262a3122 [file] [log] [blame]
Luka Perkovdff289642012-05-27 11:44:51 +00001/*
Stefan Roesebb5c4282014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
Marek Behúndb5b53e2021-09-24 23:07:13 +02003 * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
4 * Armada 39x
Luka Perkovdff289642012-05-27 11:44:51 +00005 *
6 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
Pali Roháraa98a1e2021-09-24 23:07:14 +02007 * (c) 2021 Pali Rohár <pali@kernel.org>
8 * (c) 2021 Marek Behún <marek.behun@nic.cz>
Luka Perkovdff289642012-05-27 11:44:51 +00009 *
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 Roese04ec0d32016-01-07 14:12:04 +010015#include "kwbimage.h"
16#include "mkimage.h"
Pali Rohár3c703aaf2021-09-24 23:06:42 +020017#include "version.h"
Stefan Roese04ec0d32016-01-07 14:12:04 +010018
Luka Perkovdff289642012-05-27 11:44:51 +000019#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdarg.h>
Stefan Roese04ec0d32016-01-07 14:12:04 +010023#include <image.h>
Luka Perkovdff289642012-05-27 11:44:51 +000024#include <libgen.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <unistd.h>
28#include <stdint.h>
Marek Behún2d9f2452021-09-24 23:06:52 +020029#include <time.h>
Luka Perkovdff289642012-05-27 11:44:51 +000030#include <sys/stat.h>
Pali Rohár3a2e9bc2022-03-02 11:49:21 +010031#include <pthread.h>
Luka Perkovdff289642012-05-27 11:44:51 +000032
Pali Rohárfd935e92021-09-24 23:07:06 +020033#ifdef __linux__
34#include "termios_linux.h"
35#else
36#include <termios.h>
37#endif
38
Luka Perkovdff289642012-05-27 11:44:51 +000039/*
40 * Marvell BootROM UART Sensing
41 */
42
43static unsigned char kwboot_msg_boot[] = {
44 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
45};
46
Stefan Roesebb5c4282014-10-22 12:13:21 +020047static unsigned char kwboot_msg_debug[] = {
48 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
49};
50
51/* Defines known to work on Kirkwood */
Luka Perkovdff289642012-05-27 11:44:51 +000052#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
53
Stefan Roesebb5c4282014-10-22 12:13:21 +020054/* Defines known to work on Armada XP */
Stefan Roesebb5c4282014-10-22 12:13:21 +020055#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
56
Luka Perkovdff289642012-05-27 11:44:51 +000057/*
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 Perkovdff289642012-05-27 11:44:51 +000065
Pali Rohárbed18ef2021-09-24 23:06:48 +020066#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
67
Luka Perkovdff289642012-05-27 11:44:51 +000068struct kwboot_block {
69 uint8_t soh;
70 uint8_t pnum;
71 uint8_t _pnum;
Pali Rohárbed18ef2021-09-24 23:06:48 +020072 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovdff289642012-05-27 11:44:51 +000073 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +020074} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +000075
Pali Rohárdef98382022-01-25 18:13:00 +010076#define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */
Marek Behún2d9f2452021-09-24 23:06:52 +020077#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovdff289642012-05-27 11:44:51 +000078
Pali Rohár6303a232021-10-27 20:57:02 +020079/* ARM code to change baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +020080static unsigned char kwboot_baud_code[] = {
81 /* ; #define UART_BASE 0xd0012000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +020082 /* ; #define DLL 0x00 */
83 /* ; #define DLH 0x04 */
84 /* ; #define LCR 0x0c */
85 /* ; #define DLAB 0x80 */
86 /* ; #define LSR 0x14 */
Pali Rohár2a8b7692021-09-24 23:07:05 +020087 /* ; #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ár2a8b7692021-09-24 23:07:05 +020091 /* ; 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ár9e624c92021-10-27 20:57:00 +0200105 /* ; msleep(5); */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200106 /* ; return 0; */
107 /* ; } */
108
Pali Rohár2a8b7692021-09-24 23:07:05 +0200109 /* ; r0 = UART_BASE */
Pali Rohár15b16e52021-10-27 20:57:01 +0200110 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
111 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200112
Pali Rohár2a8b7692021-09-24 23:07:05 +0200113 /* ; 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ár2d760ed2021-11-01 14:00:02 +0100138 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200139
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ár9e624c92021-10-27 20:57:00 +0200145 /* ; r2 = new_baudrate */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100146 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200147
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ár9e624c92021-10-27 20:57:00 +0200186 /* ; 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ár2a8b7692021-09-24 23:07:05 +0200190 /* .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ár2d760ed2021-11-01 14:00:02 +0100195 /* ; Jump to the end of execution */
196 0x01, 0x00, 0x00, 0xea, /* b end */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200197
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ár6303a232021-10-27 20:57:02 +0200205
206 /* end: */
207};
208
Pali Rohár2d760ed2021-11-01 14:00:02 +0100209/* ARM code from binary header executed by BootROM before changing baudrate */
Pali Rohár6303a232021-10-27 20:57:02 +0200210static unsigned char kwboot_baud_code_binhdr_pre[] = {
Pali Rohár2d760ed2021-11-01 14:00:02 +0100211 /* ; #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ár6303a232021-10-27 20:57:02 +0200228 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100229
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ár6303a232021-10-27 20:57:02 +0200266};
267
Pali Rohár2d760ed2021-11-01 14:00:02 +0100268/* ARM code for returning from binary header back to BootROM */
Pali Rohár6303a232021-10-27 20:57:02 +0200269static 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ár2a8b7692021-09-24 23:07:05 +0200273};
274
Pali Rohár6303a232021-10-27 20:57:02 +0200275/* ARM code for jumping to the original image exec_addr */
276static 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ár2a8b7692021-09-24 23:07:05 +0200282
283static const char kwb_baud_magic[16] = "$baudratechange";
284
Luka Perkovdff289642012-05-27 11:44:51 +0000285static int kwboot_verbose;
286
Stefan Roesebb5c4282014-10-22 12:13:21 +0200287static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +0000288static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200289
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200290static ssize_t
291kwboot_write(int fd, const char *buf, size_t len)
292{
Pali Rohár5350df12022-01-25 18:13:10 +0100293 ssize_t tot = 0;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200294
295 while (tot < len) {
296 ssize_t wr = write(fd, buf + tot, len - tot);
297
Pali Rohár5350df12022-01-25 18:13:10 +0100298 if (wr < 0 && errno == EINTR)
299 continue;
300 else if (wr < 0)
301 return wr;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200302
303 tot += wr;
304 }
305
306 return tot;
307}
308
Luka Perkovdff289642012-05-27 11:44:51 +0000309static void
310kwboot_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
322static 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
339static void
340kwboot_spinner(void)
341{
342 if (kwboot_verbose)
343 __spinner();
344}
345
346static 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árd01b9ce2021-09-24 23:06:46 +0200359 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000360
361 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200362 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000363 fputc(' ', stdout);
364 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200365 nl = "";
366 pos = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000367 }
368
369 fflush(stdout);
370
371}
372
373static void
374kwboot_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árd01b9ce2021-09-24 23:06:46 +0200383
384 if (pct == 100)
385 pct = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000386}
387
388static int
389kwboot_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ár613bd8b2022-01-25 18:13:11 +0100410 if (nfds < 0 && errno == EINTR)
411 continue;
412 else if (nfds < 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000413 goto out;
Pali Rohár613bd8b2022-01-25 18:13:11 +0100414 else if (!nfds) {
Luka Perkovdff289642012-05-27 11:44:51 +0000415 errno = ETIMEDOUT;
416 goto out;
417 }
418
419 n = read(fd, buf, len);
Pali Rohár613bd8b2022-01-25 18:13:11 +0100420 if (n < 0 && errno == EINTR)
421 continue;
422 else if (n <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000423 goto out;
424
425 buf = (char *)buf + n;
426 len -= n;
427 } while (len > 0);
428
429 rc = 0;
430out:
431 return rc;
432}
433
434static int
Pali Rohárf2acbba2021-10-27 20:56:59 +0200435kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovdff289642012-05-27 11:44:51 +0000436{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200437 if (!buf)
438 return 0;
439
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200440 if (kwboot_write(fd, buf, len) < 0)
441 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000442
Pali Rohárf2acbba2021-10-27 20:56:59 +0200443 if (nodrain)
444 return 0;
445
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200446 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000447}
448
449static int
450kwboot_tty_send_char(int fd, unsigned char c)
451{
Pali Rohárf2acbba2021-10-27 20:56:59 +0200452 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovdff289642012-05-27 11:44:51 +0000453}
454
455static speed_t
Pali Rohár2a8b7692021-09-24 23:07:05 +0200456kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000457{
458 switch (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +0200459#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 Perkovdff289642012-05-27 11:44:51 +0000524 case 115200:
525 return B115200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200526#endif
527#ifdef B76800
528 case 76800:
529 return B76800;
530#endif
531#ifdef B57600
Luka Perkovdff289642012-05-27 11:44:51 +0000532 case 57600:
533 return B57600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200534#endif
535#ifdef B38400
Luka Perkovdff289642012-05-27 11:44:51 +0000536 case 38400:
537 return B38400;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200538#endif
539#ifdef B19200
Luka Perkovdff289642012-05-27 11:44:51 +0000540 case 19200:
541 return B19200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200542#endif
543#ifdef B9600
Luka Perkovdff289642012-05-27 11:44:51 +0000544 case 9600:
545 return B9600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200546#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árfd935e92021-09-24 23:07:06 +0200596#ifdef BOTHER
597 return BOTHER;
598#else
Pali Rohár2a8b7692021-09-24 23:07:05 +0200599 return B0;
Pali Rohárfd935e92021-09-24 23:07:06 +0200600#endif
Luka Perkovdff289642012-05-27 11:44:51 +0000601 }
Pali Rohár2a8b7692021-09-24 23:07:05 +0200602}
603
604static int
Marek Behún67835492021-09-24 23:07:07 +0200605_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
611static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200612kwboot_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árfd935e92021-09-24 23:07:06 +0200628#ifdef BOTHER
629 if (speed == BOTHER)
630 tio.c_ospeed = tio.c_ispeed = baudrate;
631#endif
632
Pali Rohár2a8b7692021-09-24 23:07:05 +0200633 rc = cfsetospeed(&tio, speed);
634 if (rc)
635 return rc;
636
637 rc = cfsetispeed(&tio, speed);
638 if (rc)
639 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000640
Pali Rohár2a8b7692021-09-24 23:07:05 +0200641 rc = tcsetattr(fd, TCSANOW, &tio);
642 if (rc)
643 return rc;
644
Marek Behún67835492021-09-24 23:07:07 +0200645 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ár2a8b7692021-09-24 23:07:05 +0200665 return 0;
Marek Behún67835492021-09-24 23:07:07 +0200666
667baud_fail:
668 fprintf(stderr, "Could not set baudrate to requested value\n");
669 errno = EINVAL;
670 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000671}
672
673static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200674kwboot_open_tty(const char *path, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000675{
Pali Rohár2bffe242021-09-24 23:07:10 +0200676 int rc, fd, flags;
Luka Perkovdff289642012-05-27 11:44:51 +0000677 struct termios tio;
678
679 rc = -1;
680
Marek Behún6c598c32021-09-24 23:07:11 +0200681 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovdff289642012-05-27 11:44:51 +0000682 if (fd < 0)
683 goto out;
684
Pali Rohár909a0b92021-09-24 23:07:08 +0200685 rc = tcgetattr(fd, &tio);
686 if (rc)
687 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +0000688
Pali Rohár909a0b92021-09-24 23:07:08 +0200689 cfmakeraw(&tio);
Marek Behún6c598c32021-09-24 23:07:11 +0200690 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár40003a02021-10-25 15:12:53 +0200691 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovdff289642012-05-27 11:44:51 +0000692 tio.c_cc[VMIN] = 1;
Pali Rohár12095b22021-09-24 23:07:09 +0200693 tio.c_cc[VTIME] = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000694
Luka Perkovdff289642012-05-27 11:44:51 +0000695 rc = tcsetattr(fd, TCSANOW, &tio);
696 if (rc)
697 goto out;
698
Pali Rohár2bffe242021-09-24 23:07:10 +0200699 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ár2a8b7692021-09-24 23:07:05 +0200707 rc = kwboot_tty_change_baudrate(fd, baudrate);
708 if (rc)
709 goto out;
710
Luka Perkovdff289642012-05-27 11:44:51 +0000711 rc = fd;
712out:
713 if (rc < 0) {
714 if (fd >= 0)
715 close(fd);
716 }
717
718 return rc;
719}
720
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100721static void *
722kwboot_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
750static int
751kwboot_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
767static int
768kwboot_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 Perkovdff289642012-05-27 11:44:51 +0000787static int
Pali Rohár224110d2022-03-02 11:49:20 +0100788kwboot_bootmsg(int tty)
Luka Perkovdff289642012-05-27 11:44:51 +0000789{
Pali Roháre68c27bd2022-01-25 18:13:08 +0100790 struct kwboot_block block;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100791 pthread_t write_thread;
792 int rc, err;
Luka Perkovdff289642012-05-27 11:44:51 +0000793 char c;
794
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100795 /* flush input and output queue */
796 tcflush(tty, TCIOFLUSH);
Luka Perkovdff289642012-05-27 11:44:51 +0000797
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100798 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 Perkovdff289642012-05-27 11:44:51 +0000803
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100804 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovdff289642012-05-27 11:44:51 +0000805
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100806 err = 0;
807 while (1) {
Luka Perkovdff289642012-05-27 11:44:51 +0000808 kwboot_spinner();
809
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100810 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 Perkovdff289642012-05-27 11:44:51 +0000821
822 kwboot_printv("\n");
823
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100824 rc = kwboot_msg_stop_thread(write_thread);
825 if (rc) {
826 perror("Failed to stop write thread");
Pali Roháre68c27bd2022-01-25 18:13:08 +0100827 return rc;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100828 }
829
830 if (err) {
831 errno = err;
832 perror("Failed to read response for boot message pattern");
833 return -1;
834 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100835
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áre3a18282022-03-02 11:49:18 +0100851 rc = tcflush(tty, TCOFLUSH);
852 if (rc) {
853 perror("Failed to flush output queue");
854 return rc;
855 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100856
857 /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */
858 memset(&block, 0xff, sizeof(block));
Pali Roháre3a18282022-03-02 11:49:18 +0100859 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áre68c27bd2022-01-25 18:13:08 +0100864
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áre3a18282022-03-02 11:49:18 +0100872 rc = tcflush(tty, TCIFLUSH);
873 if (rc) {
874 perror("Failed to flush input queue");
875 return rc;
876 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100877
878 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000879}
880
881static int
Pali Rohár224110d2022-03-02 11:49:20 +0100882kwboot_debugmsg(int tty)
Stefan Roesebb5c4282014-10-22 12:13:21 +0200883{
Pali Rohár2af673b2022-03-02 11:49:22 +0100884 unsigned char buf[8192];
885 pthread_t write_thread;
886 int rc, err, i, pos;
887 size_t off;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200888
Pali Rohár2af673b2022-03-02 11:49:22 +0100889 /* flush input and output queue */
890 tcflush(tty, TCIOFLUSH);
Stefan Roesebb5c4282014-10-22 12:13:21 +0200891
Pali Rohár2af673b2022-03-02 11:49:22 +0100892 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 Roesebb5c4282014-10-22 12:13:21 +0200897
Pali Rohár2af673b2022-03-02 11:49:22 +0100898 kwboot_printv("Sending debug message. Please reboot the target...");
899 kwboot_spinner();
Stefan Roesebb5c4282014-10-22 12:13:21 +0200900
Pali Rohár2af673b2022-03-02 11:49:22 +0100901 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áre3a18282022-03-02 11:49:18 +0100910 break;
Pali Rohár2af673b2022-03-02 11:49:22 +0100911 }
912 off += rc - 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200913
914 kwboot_spinner();
915
Pali Rohár2af673b2022-03-02 11:49:22 +0100916 /*
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 Roesebb5c4282014-10-22 12:13:21 +0200937
938 kwboot_printv("\n");
939
Pali Rohár2af673b2022-03-02 11:49:22 +0100940 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 Roesebb5c4282014-10-22 12:13:21 +02001017}
1018
Pali Rohár58cf04de2021-09-24 23:06:44 +02001019static size_t
Luka Perkovdff289642012-05-27 11:44:51 +00001020kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
1021 size_t size, int pnum)
1022{
Marek Behúnedb63242021-09-24 23:06:45 +02001023 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +00001024
Stefan Roesebb5c4282014-10-22 12:13:21 +02001025 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +00001026 block->pnum = pnum;
1027 block->_pnum = ~block->pnum;
1028
Pali Rohárbed18ef2021-09-24 23:06:48 +02001029 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovdff289642012-05-27 11:44:51 +00001030 memcpy(&block->data[0], data, n);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001031 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovdff289642012-05-27 11:44:51 +00001032
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ún2d9f2452021-09-24 23:06:52 +02001040static 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 Perkovdff289642012-05-27 11:44:51 +00001060static int
Marek Behúneabacb82021-09-24 23:06:49 +02001061_is_xm_reply(char c)
1062{
Pali Rohár6af14292022-01-25 18:13:03 +01001063 return c == ACK || c == NAK;
Marek Behúneabacb82021-09-24 23:06:49 +02001064}
1065
1066static int
Pali Rohár42c61a42021-09-24 23:06:54 +02001067_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ár42c61a42021-09-24 23:06:54 +02001078 default:
1079 errno = EPROTO;
1080 break;
1081 }
1082
1083 return rc;
1084}
1085
1086static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001087kwboot_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
1112static int
Pali Rohár230d4f82022-01-25 18:13:04 +01001113kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Roháre4529bd2022-01-25 18:13:02 +01001114 int ignore_nak_reply,
Pali Rohár959e8502021-10-25 15:13:04 +02001115 int allow_non_xm, int *non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001116 int baudrate, int *baud_changed)
Pali Rohárd06d5202021-09-24 23:06:50 +02001117{
Marek Behún2d9f2452021-09-24 23:06:52 +02001118 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001119 uint64_t recv_until = _now() + timeout;
Pali Rohárd06d5202021-09-24 23:06:50 +02001120 int rc;
1121
1122 while (1) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001123 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohárd06d5202021-09-24 23:06:50 +02001124 if (rc) {
1125 if (errno != ETIMEDOUT)
1126 return rc;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001127 else if (allow_non_xm && *non_xm_print)
Marek Behún2d9f2452021-09-24 23:06:52 +02001128 return -1;
1129 else
1130 *c = NAK;
Pali Rohárd06d5202021-09-24 23:06:50 +02001131 }
1132
1133 /* If received xmodem reply, end. */
Pali Roháre4529bd2022-01-25 18:13:02 +01001134 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árd06d5202021-09-24 23:06:50 +02001140 break;
Pali Roháre4529bd2022-01-25 18:13:02 +01001141 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001142
1143 /*
Pali Rohár2a8b7692021-09-24 23:07:05 +02001144 * 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ún0a3b85a2021-09-24 23:06:53 +02001150 * Otherwise decrease timeout by time elapsed.
Pali Rohárd06d5202021-09-24 23:06:50 +02001151 */
1152 if (allow_non_xm) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001153 recv_until = _now() + timeout;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001154
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ún0a3b85a2021-09-24 23:06:53 +02001168 } else {
Pali Rohár230d4f82022-01-25 18:13:04 +01001169 if (stop_on_non_xm)
Pali Rohár959e8502021-10-25 15:13:04 +02001170 break;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001171 timeout = recv_until - _now();
1172 if (timeout < 0) {
1173 errno = ETIMEDOUT;
1174 return -1;
1175 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001176 }
1177 }
1178
1179 return 0;
1180}
1181
1182static int
1183kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár80241ce2022-01-25 18:13:05 +01001184 int *done_print, int baudrate, int allow_retries)
Luka Perkovdff289642012-05-27 11:44:51 +00001185{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001186 int non_xm_print, baud_changed;
1187 int rc, err, retries;
Luka Perkovdff289642012-05-27 11:44:51 +00001188 char c;
1189
Pali Rohárd06d5202021-09-24 23:06:50 +02001190 *done_print = 0;
Pali Rohár6daa5612021-10-27 20:56:58 +02001191 non_xm_print = 0;
1192 baud_changed = 0;
Pali Rohárd06d5202021-09-24 23:06:50 +02001193
Pali Roháre665c6a2021-10-25 15:13:03 +02001194 retries = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001195 do {
Pali Rohárf2acbba2021-10-27 20:56:59 +02001196 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovdff289642012-05-27 11:44:51 +00001197 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001198 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001199
Pali Rohárd06d5202021-09-24 23:06:50 +02001200 if (allow_non_xm && !*done_print) {
1201 kwboot_progress(100, '.');
1202 kwboot_printv("Done\n");
1203 *done_print = 1;
1204 }
Stefan Roesebb5c4282014-10-22 12:13:21 +02001205
Pali Rohár959e8502021-10-25 15:13:04 +02001206 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001207 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001208 allow_non_xm, &non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001209 baudrate, &baud_changed);
Pali Rohárd06d5202021-09-24 23:06:50 +02001210 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001211 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001212
Pali Rohárb3760f92022-01-25 18:13:06 +01001213 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ár80241ce2022-01-25 18:13:05 +01001219 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovdff289642012-05-27 11:44:51 +00001220
Marek Behúnbcc5e042021-09-24 23:06:51 +02001221 if (non_xm_print)
1222 kwboot_printv("\n");
1223
Pali Rohár2a8b7692021-09-24 23:07:05 +02001224 if (allow_non_xm && baudrate && !baud_changed) {
1225 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohár2a8b7692021-09-24 23:07:05 +02001226 errno = EPROTO;
Pali Rohár6af14292022-01-25 18:13:03 +01001227 return -1;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001228 }
1229
Pali Rohár42c61a42021-09-24 23:06:54 +02001230 return _xm_reply_to_error(c);
Pali Rohár6af14292022-01-25 18:13:03 +01001231err:
Pali Rohár2a8b7692021-09-24 23:07:05 +02001232 err = errno;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001233 kwboot_printv("\n");
1234 errno = err;
1235 return rc;
Pali Rohár42c61a42021-09-24 23:06:54 +02001236}
Luka Perkovdff289642012-05-27 11:44:51 +00001237
Pali Rohár42c61a42021-09-24 23:06:54 +02001238static int
1239kwboot_xm_finish(int fd)
1240{
1241 int rc, retries;
1242 char c;
Luka Perkovdff289642012-05-27 11:44:51 +00001243
Pali Rohár42c61a42021-09-24 23:06:54 +02001244 kwboot_printv("Finishing transfer\n");
1245
Pali Roháre665c6a2021-10-25 15:13:03 +02001246 retries = 0;
Pali Rohár42c61a42021-09-24 23:06:54 +02001247 do {
1248 rc = kwboot_tty_send_char(fd, EOT);
1249 if (rc)
1250 return rc;
1251
Pali Rohár959e8502021-10-25 15:13:04 +02001252 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001253 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001254 0, NULL, 0, NULL);
Pali Rohár42c61a42021-09-24 23:06:54 +02001255 if (rc)
1256 return rc;
Pali Roháre665c6a2021-10-25 15:13:03 +02001257 } while (c == NAK && retries++ < 16);
Pali Rohár42c61a42021-09-24 23:06:54 +02001258
1259 return _xm_reply_to_error(c);
Luka Perkovdff289642012-05-27 11:44:51 +00001260}
1261
1262static int
Pali Rohárbed18ef2021-09-24 23:06:48 +02001263kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001264 size_t size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001265{
Pali Rohárd06d5202021-09-24 23:06:50 +02001266 int done_print = 0;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001267 size_t sent, left;
1268 int rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001269
Pali Rohárbed18ef2021-09-24 23:06:48 +02001270 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1271 header ? "header" : "data", size);
Luka Perkovdff289642012-05-27 11:44:51 +00001272
Pali Rohárbed18ef2021-09-24 23:06:48 +02001273 left = size;
1274 sent = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001275
Pali Rohárbed18ef2021-09-24 23:06:48 +02001276 while (sent < size) {
Luka Perkovdff289642012-05-27 11:44:51 +00001277 struct kwboot_block block;
Pali Rohárd06d5202021-09-24 23:06:50 +02001278 int last_block;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001279 size_t blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001280
Pali Rohárbed18ef2021-09-24 23:06:48 +02001281 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1282 data += blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001283
Pali Rohárd06d5202021-09-24 23:06:50 +02001284 last_block = (left <= blksz);
1285
Pali Rohár80241ce2022-01-25 18:13:05 +01001286 /*
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árd06d5202021-09-24 23:06:50 +02001308 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár80241ce2022-01-25 18:13:05 +01001309 &done_print, baudrate, header);
Luka Perkovdff289642012-05-27 11:44:51 +00001310 if (rc)
1311 goto out;
1312
Pali Rohárbed18ef2021-09-24 23:06:48 +02001313 sent += blksz;
1314 left -= blksz;
1315
Pali Rohárd06d5202021-09-24 23:06:50 +02001316 if (!done_print)
1317 kwboot_progress(sent * 100 / size, '.');
Pali Rohárbed18ef2021-09-24 23:06:48 +02001318 }
Luka Perkovdff289642012-05-27 11:44:51 +00001319
Pali Rohárd06d5202021-09-24 23:06:50 +02001320 if (!done_print)
1321 kwboot_printv("Done\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001322
Pali Rohárbed18ef2021-09-24 23:06:48 +02001323 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001324out:
Pali Rohár5ed896f2021-09-24 23:06:47 +02001325 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001326 return rc;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001327}
1328
1329static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001330kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohárbed18ef2021-09-24 23:06:48 +02001331{
1332 const uint8_t *img = _img;
1333 int rc, pnum;
1334 size_t hdrsz;
1335
Marek Behúnd1b0b032021-09-24 23:07:01 +02001336 hdrsz = kwbheader_size(img);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001337
Pali Rohár400b5cf2021-11-05 23:29:58 +01001338 /*
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árbed18ef2021-09-24 23:06:48 +02001346 pnum = 1;
1347
Pali Rohár2a8b7692021-09-24 23:07:05 +02001348 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001349 if (rc)
1350 return rc;
1351
Pali Rohár400b5cf2021-11-05 23:29:58 +01001352 /*
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ár2a8b7692021-09-24 23:07:05 +02001363
1364 rc = kwboot_xm_finish(tty);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001365 if (rc)
1366 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001367
Pali Rohár2a8b7692021-09-24 23:07:05 +02001368 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001369 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 Perkovdff289642012-05-27 11:44:51 +00001376}
1377
1378static int
Marek Behúnea5b2b32021-09-24 23:06:40 +02001379kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovdff289642012-05-27 11:44:51 +00001380{
Pali Rohár111779f2022-02-03 17:45:20 +01001381 char buf[128];
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001382 ssize_t nin;
Luka Perkovdff289642012-05-27 11:44:51 +00001383
Pali Rohár111779f2022-02-03 17:45:20 +01001384 nin = read(in, buf, sizeof(buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -04001385 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +00001386 return -1;
1387
1388 if (quit) {
1389 int i;
1390
1391 for (i = 0; i < nin; i++) {
Pali Rohár111779f2022-02-03 17:45:20 +01001392 if (buf[i] == quit[*s]) {
Luka Perkovdff289642012-05-27 11:44:51 +00001393 (*s)++;
Pali Rohár111779f2022-02-03 17:45:20 +01001394 if (!quit[*s]) {
Pali Rohár6a5d1312022-02-18 12:24:13 +01001395 nin = (i > *s) ? (i - *s) : 0;
Pali Rohár111779f2022-02-03 17:45:20 +01001396 break;
1397 }
Pali Rohár48615ba2021-07-23 11:14:20 +02001398 } else {
Pali Rohár111779f2022-02-03 17:45:20 +01001399 if (*s > i && kwboot_write(out, quit, *s - i) < 0)
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001400 return -1;
1401 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +02001402 }
Luka Perkovdff289642012-05-27 11:44:51 +00001403 }
Pali Rohár111779f2022-02-03 17:45:20 +01001404
1405 if (i == nin)
Pali Rohár6a5d1312022-02-18 12:24:13 +01001406 nin -= (nin > *s) ? *s : nin;
Luka Perkovdff289642012-05-27 11:44:51 +00001407 }
1408
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001409 if (kwboot_write(out, buf, nin) < 0)
1410 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001411
1412 return 0;
1413}
1414
1415static int
1416kwboot_terminal(int tty)
1417{
1418 int rc, in, s;
Marek Behúnea5b2b32021-09-24 23:06:40 +02001419 const char *quit = "\34c";
Luka Perkovdff289642012-05-27 11:44:51 +00001420 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ún6c598c32021-09-24 23:07:11 +02001438 quit[0] | 0100, quit[1]);
Luka Perkovdff289642012-05-27 11:44:51 +00001439 } 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árc1efca42021-10-25 15:12:52 +02001449 FD_ZERO(&rfds);
Luka Perkovdff289642012-05-27 11:44:51 +00001450 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ún4eb55de2021-09-24 23:06:39 +02001468 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovdff289642012-05-27 11:44:51 +00001469 rc = kwboot_term_pipe(in, tty, quit, &s);
1470 if (rc)
1471 break;
1472 }
1473 } while (quit[s] != 0);
1474
Pali Roháreafc9692021-07-23 11:14:18 +02001475 if (in >= 0)
1476 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +02001477 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001478out:
1479 return rc;
1480}
1481
1482static void *
Pali Rohárd9355bf2021-09-24 23:07:03 +02001483kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovdff289642012-05-27 11:44:51 +00001484{
Pali Rohár4d276012021-09-24 23:06:55 +02001485 int rc, fd;
Luka Perkovdff289642012-05-27 11:44:51 +00001486 struct stat st;
1487 void *img;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001488 off_t tot;
Luka Perkovdff289642012-05-27 11:44:51 +00001489
1490 rc = -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001491 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árd9355bf2021-09-24 23:07:03 +02001501 img = malloc(st.st_size + reserve);
1502 if (!img)
Luka Perkovdff289642012-05-27 11:44:51 +00001503 goto out;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001504
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 Perkovdff289642012-05-27 11:44:51 +00001518 }
1519
1520 rc = 0;
1521 *size = st.st_size;
1522out:
1523 if (rc && img) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001524 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001525 img = NULL;
1526 }
1527 if (fd >= 0)
1528 close(fd);
1529
1530 return img;
1531}
1532
1533static uint8_t
Marek Behúnd1b0b032021-09-24 23:07:01 +02001534kwboot_hdr_csum8(const void *hdr)
Luka Perkovdff289642012-05-27 11:44:51 +00001535{
Marek Behúnd1b0b032021-09-24 23:07:01 +02001536 const uint8_t *data = hdr;
1537 uint8_t csum;
1538 size_t size;
1539
1540 size = kwbheader_size_for_csum(hdr);
Luka Perkovdff289642012-05-27 11:44:51 +00001541
1542 for (csum = 0; size-- > 0; data++)
1543 csum += *data;
1544
1545 return csum;
1546}
1547
Pali Rohár0b659e52021-10-25 15:12:55 +02001548static uint32_t *
1549kwboot_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
1559static uint32_t
1560kwboot_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 Perkovdff289642012-05-27 11:44:51 +00001579static int
Pali Rohár5725e0c2021-09-24 23:06:57 +02001580kwboot_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ár2a8b7692021-09-24 23:07:05 +02001591static void *
Pali Roháraed39f22021-10-25 15:12:56 +02001592kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001593{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001594 struct main_hdr_v1 *hdr = img;
Pali Roháraed39f22021-10-25 15:12:56 +02001595 void *result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001596
Pali Roháraed39f22021-10-25 15:12:56 +02001597 /*
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ár2a8b7692021-09-24 23:07:05 +02001604 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Roháraed39f22021-10-25 15:12:56 +02001605 *size += grow;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001606
Pali Roháraed39f22021-10-25 15:12:56 +02001607 return result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001608}
1609
Pali Rohárd9355bf2021-09-24 23:07:03 +02001610static void
1611kwboot_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ár6fa97092021-10-25 15:13:02 +02001615 struct opt_hdr_v1 *ohdr;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001616 uint8_t *data;
1617
1618 srcaddr = le32_to_cpu(hdr->srcaddr);
1619
Pali Rohár6fa97092021-10-25 15:13:02 +02001620 /* 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árd9355bf2021-09-24 23:07:03 +02001629 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ár6fa97092021-10-25 15:13:02 +02001645 if (hdrsz > kwbheader_size(img)) {
1646 hdr->headersz_msb = hdrsz >> 16;
1647 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1648 }
Pali Rohárd9355bf2021-09-24 23:07:03 +02001649 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001650}
1651
1652static void *
1653kwboot_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ár87cc3c92021-10-21 16:46:06 +02001657 uint32_t num_args;
1658 uint32_t offset;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001659 uint32_t ohdrsz;
Pali Rohár90eb9002021-10-25 15:13:01 +02001660 uint8_t *prev_ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001661
Pali Rohár32eec192022-01-12 18:20:52 +01001662 if (hdr->ext) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001663 for_each_opt_hdr_v1 (ohdr, img)
1664 if (opt_hdr_v1_next(ohdr) == NULL)
1665 break;
1666
Pali Rohár90eb9002021-10-25 15:13:01 +02001667 prev_ext = opt_hdr_v1_ext(ohdr);
1668 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001669 } else {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001670 ohdr = (void *)(hdr + 1);
Pali Rohár90eb9002021-10-25 15:13:01 +02001671 prev_ext = &hdr->ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001672 }
1673
Pali Rohár87cc3c92021-10-21 16:46:06 +02001674 /*
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ár32eec192022-01-12 18:20:52 +01001686 *prev_ext = 1;
Pali Rohár90eb9002021-10-25 15:13:01 +02001687
Pali Rohár2a8b7692021-09-24 23:07:05 +02001688 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ár87cc3c92021-10-21 16:46:06 +02001693 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001694
Pali Rohár87cc3c92021-10-21 16:46:06 +02001695 return &ohdr->data[4 + 4 * num_args];
Pali Rohárd9355bf2021-09-24 23:07:03 +02001696}
1697
Pali Rohár2a8b7692021-09-24 23:07:05 +02001698static void
Pali Rohár6303a232021-10-27 20:57:02 +02001699_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Roháraed39f22021-10-25 15:12:56 +02001700 int old_baud, int new_baud)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001701{
Pali Roháraed39f22021-10-25 15:12:56 +02001702 struct main_hdr_v1 *hdr = img;
Pali Rohár6303a232021-10-27 20:57:02 +02001703 uint32_t orig_datasz;
1704 uint32_t codesz;
Pali Roháraed39f22021-10-25 15:12:56 +02001705 uint8_t *code;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001706
Pali Rohár6303a232021-10-27 20:57:02 +02001707 if (for_data) {
Pali Roháraed39f22021-10-25 15:12:56 +02001708 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1709
Pali Rohár6303a232021-10-27 20:57:02 +02001710 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ár2a8b7692021-09-24 23:07:05 +02001718
Pali Rohár6303a232021-10-27 20:57:02 +02001719 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1720 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1721 code += codesz;
1722 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001723
Pali Rohár6303a232021-10-27 20:57:02 +02001724 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ár2a8b7692021-09-24 23:07:05 +02001731
Pali Rohár6303a232021-10-27 20:57:02 +02001732 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áraed39f22021-10-25 15:12:56 +02001739 } else {
Pali Rohár6303a232021-10-27 20:57:02 +02001740 codesz = sizeof(kwboot_baud_code_binhdr_post);
1741 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1742 code += codesz;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001743 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001744}
1745
Pali Rohár5725e0c2021-09-24 23:06:57 +02001746static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001747kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001748{
Stefan Roesec74165d2015-09-29 09:19:59 +02001749 struct main_hdr_v1 *hdr;
Pali Rohár88255af2021-09-24 23:06:58 +02001750 uint32_t srcaddr;
Luka Perkovdff289642012-05-27 11:44:51 +00001751 uint8_t csum;
Marek Behún811821e2021-09-24 23:07:04 +02001752 size_t hdrsz;
Stefan Roesec74165d2015-09-29 09:19:59 +02001753 int image_ver;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001754 int is_secure;
Luka Perkovdff289642012-05-27 11:44:51 +00001755
Luka Perkovdff289642012-05-27 11:44:51 +00001756 hdr = img;
1757
Marek Behún3e6117e2021-09-24 23:07:12 +02001758 if (*size < sizeof(struct main_hdr_v1))
1759 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001760
Marek Behúnfa9caec2021-09-24 23:07:00 +02001761 image_ver = kwbimage_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +02001762 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +02001763 fprintf(stderr, "Invalid image header version\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001764 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001765 }
1766
Marek Behúnd1b0b032021-09-24 23:07:01 +02001767 hdrsz = kwbheader_size(hdr);
Stefan Roesec74165d2015-09-29 09:19:59 +02001768
Marek Behún3e6117e2021-09-24 23:07:12 +02001769 if (*size < hdrsz)
1770 goto err;
Pali Rohár12281162021-07-23 11:14:21 +02001771
Marek Behúnd1b0b032021-09-24 23:07:01 +02001772 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behún3e6117e2021-09-24 23:07:12 +02001773 if (csum != hdr->checksum)
1774 goto err;
Stefan Roesec74165d2015-09-29 09:19:59 +02001775
Pali Rohár88255af2021-09-24 23:06:58 +02001776 srcaddr = le32_to_cpu(hdr->srcaddr);
1777
1778 switch (hdr->blockid) {
1779 case IBR_HDR_SATA_ID:
Marek Behún3e6117e2021-09-24 23:07:12 +02001780 if (srcaddr < 1)
1781 goto err;
1782
Pali Rohár88255af2021-09-24 23:06:58 +02001783 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ár398d4152021-09-24 23:06:59 +02001794
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ár88255af2021-09-24 23:06:58 +02001802 }
1803
Pali Rohárd9355bf2021-09-24 23:07:03 +02001804 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behún3e6117e2021-09-24 23:07:12 +02001805 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1806 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001807
Pali Rohár0b659e52021-10-25 15:12:55 +02001808 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1809 goto err;
1810
Pali Rohár5725e0c2021-09-24 23:06:57 +02001811 is_secure = kwboot_img_is_secure(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001812
Pali Rohár5725e0c2021-09-24 23:06:57 +02001813 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ún3e6117e2021-09-24 23:07:12 +02001817 goto err;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001818 }
1819
1820 kwboot_printv("Patching image boot signature to UART\n");
1821 hdr->blockid = IBR_HDR_UART_ID;
1822 }
Luka Perkovdff289642012-05-27 11:44:51 +00001823
Pali Rohár3024ebd2021-10-22 12:37:47 +02001824 if (!is_secure) {
Pali Rohár71db3622021-10-25 15:12:58 +02001825 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áre4b8fca2022-01-25 18:13:13 +01001832 hdr->flags |= 0x1;
Pali Rohár71db3622021-10-25 15:12:58 +02001833 hdr->options &= ~0x1F;
1834 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
1835 hdr->options |= 0 << 3;
1836 }
Pali Rohár3024ebd2021-10-22 12:37:47 +02001837 if (image_ver == 0)
1838 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
1839 hdr->nandpagesize = 0;
1840 }
1841
Pali Rohár2a8b7692021-09-24 23:07:05 +02001842 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001843 if (image_ver == 0) {
1844 fprintf(stderr,
1845 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001846 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001847 }
1848
1849 if (is_secure) {
1850 fprintf(stderr,
1851 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001852 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001853 }
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áraed39f22021-10-25 15:12:56 +02001863 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001864
1865 /*
1866 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Roháraed39f22021-10-25 15:12:56 +02001867 * 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ár2a8b7692021-09-24 23:07:05 +02001870 */
1871 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Roháraed39f22021-10-25 15:12:56 +02001872 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001873
Pali Rohárf625a222021-10-25 15:12:57 +02001874 /* Update the 32-bit data checksum */
1875 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
1876
Pali Rohár2a8b7692021-09-24 23:07:05 +02001877 /* recompute header size */
1878 hdrsz = kwbheader_size(hdr);
1879 }
1880
Pali Rohárd9355bf2021-09-24 23:07:03 +02001881 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Rohár085d9ce2021-10-25 15:13:00 +02001882 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001883
1884 if (is_secure) {
1885 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001886 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001887 }
1888
1889 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Rohár085d9ce2021-10-25 15:13:00 +02001890 kwboot_img_grow_hdr(img, size, grow);
Pali Rohárd9355bf2021-09-24 23:07:03 +02001891 }
1892
Marek Behúnd1b0b032021-09-24 23:07:01 +02001893 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +00001894
Pali Rohárd9355bf2021-09-24 23:07:03 +02001895 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behún3e6117e2021-09-24 23:07:12 +02001896 return 0;
1897err:
1898 errno = EINVAL;
1899 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001900}
1901
1902static void
1903kwboot_usage(FILE *stream, char *progname)
1904{
1905 fprintf(stream,
Kevin Smith6c7c1ba2016-02-16 21:28:17 +00001906 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +02001907 progname);
Luka Perkovdff289642012-05-27 11:44:51 +00001908 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02001909 fprintf(stream,
1910 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02001911 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 Roesef7f509d2015-05-29 13:25:04 +02001915 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +00001916 fprintf(stream,
1917 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001918 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
1925int
1926main(int argc, char **argv)
1927{
1928 const char *ttypath, *imgpath;
Pali Rohár4d276012021-09-24 23:06:55 +02001929 int rv, rc, tty, term;
Pali Rohár224110d2022-03-02 11:49:20 +01001930 int bootmsg;
1931 int debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +00001932 void *img;
1933 size_t size;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001934 size_t after_img_rsv;
1935 int baudrate;
Pali Rohár48953362022-01-25 18:13:07 +01001936 int prev_optind;
1937 int c;
Luka Perkovdff289642012-05-27 11:44:51 +00001938
1939 rv = 1;
1940 tty = -1;
Pali Rohár224110d2022-03-02 11:49:20 +01001941 bootmsg = 0;
1942 debugmsg = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001943 imgpath = NULL;
1944 img = NULL;
1945 term = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001946 size = 0;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001947 after_img_rsv = KWBOOT_XM_BLKSZ;
1948 baudrate = 115200;
Luka Perkovdff289642012-05-27 11:44:51 +00001949
Pali Rohár73ae7aa2021-11-05 23:30:42 +01001950 printf("kwboot version %s\n", PLAIN_VERSION);
1951
Luka Perkovdff289642012-05-27 11:44:51 +00001952 kwboot_verbose = isatty(STDOUT_FILENO);
1953
1954 do {
Pali Rohár48953362022-01-25 18:13:07 +01001955 prev_optind = optind;
1956 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +00001957 if (c < 0)
1958 break;
1959
1960 switch (c) {
1961 case 'b':
Pali Rohár48953362022-01-25 18:13:07 +01001962 if (imgpath || bootmsg || debugmsg)
1963 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01001964 bootmsg = 1;
Pali Rohár48953362022-01-25 18:13:07 +01001965 if (prev_optind == optind)
1966 goto usage;
Pali Rohárcf91fa82022-02-07 10:12:24 +01001967 if (optind < argc - 1 && argv[optind] && argv[optind][0] != '-')
Pali Rohár48953362022-01-25 18:13:07 +01001968 imgpath = argv[optind++];
Luka Perkovdff289642012-05-27 11:44:51 +00001969 break;
1970
Stefan Roesebb5c4282014-10-22 12:13:21 +02001971 case 'D':
Pali Rohár48953362022-01-25 18:13:07 +01001972 if (imgpath || bootmsg || debugmsg)
1973 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01001974 bootmsg = 0;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001975 imgpath = optarg;
1976 break;
1977
1978 case 'd':
Pali Rohár48953362022-01-25 18:13:07 +01001979 if (imgpath || bootmsg || debugmsg)
1980 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01001981 debugmsg = 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001982 break;
1983
Luka Perkovdff289642012-05-27 11:44:51 +00001984 case 'p':
Pali Rohár4d276012021-09-24 23:06:55 +02001985 /* nop, for backward compatibility */
Luka Perkovdff289642012-05-27 11:44:51 +00001986 break;
1987
1988 case 't':
1989 term = 1;
1990 break;
1991
Stefan Roesebb5c4282014-10-22 12:13:21 +02001992 case 'a':
Stefan Roesebb5c4282014-10-22 12:13:21 +02001993 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1994 break;
1995
Stefan Roesef7f509d2015-05-29 13:25:04 +02001996 case 'q':
Pali Rohárd51b8342022-03-02 11:49:19 +01001997 /* nop, for backward compatibility */
Stefan Roesef7f509d2015-05-29 13:25:04 +02001998 break;
1999
2000 case 's':
2001 msg_rsp_timeo = atoi(optarg);
2002 break;
2003
Kevin Smith4d31a842016-02-16 21:28:19 +00002004 case 'o':
2005 blk_rsp_timeo = atoi(optarg);
2006 break;
2007
Luka Perkovdff289642012-05-27 11:44:51 +00002008 case 'B':
Pali Rohár2a8b7692021-09-24 23:07:05 +02002009 baudrate = atoi(optarg);
Luka Perkovdff289642012-05-27 11:44:51 +00002010 break;
2011
2012 case 'h':
2013 rv = 0;
2014 default:
2015 goto usage;
2016 }
2017 } while (1);
2018
Pali Rohár577506c2022-01-25 18:13:12 +01002019 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovdff289642012-05-27 11:44:51 +00002020 goto usage;
2021
Luka Perkovdff289642012-05-27 11:44:51 +00002022 ttypath = argv[optind++];
2023
Pali Rohár48953362022-01-25 18:13:07 +01002024 if (optind != argc)
2025 goto usage;
2026
Pali Rohár2a8b7692021-09-24 23:07:05 +02002027 tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002028 if (tty < 0) {
2029 perror(ttypath);
2030 goto out;
2031 }
2032
Pali Rohár2a8b7692021-09-24 23:07:05 +02002033 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ár6303a232021-10-27 20:57:02 +02002038 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árd8774392021-10-25 15:12:54 +02002042 KWBOOT_XM_BLKSZ +
Pali Rohárd8774392021-10-25 15:12:54 +02002043 sizeof(kwboot_baud_code) +
Pali Rohár6303a232021-10-27 20:57:02 +02002044 sizeof(kwboot_baud_code_data_jump) +
Pali Rohárd8774392021-10-25 15:12:54 +02002045 KWBOOT_XM_BLKSZ;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002046
Luka Perkovdff289642012-05-27 11:44:51 +00002047 if (imgpath) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002048 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovdff289642012-05-27 11:44:51 +00002049 if (!img) {
2050 perror(imgpath);
2051 goto out;
2052 }
Luka Perkovdff289642012-05-27 11:44:51 +00002053
Pali Rohár2a8b7692021-09-24 23:07:05 +02002054 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002055 if (rc) {
2056 fprintf(stderr, "%s: Invalid image.\n", imgpath);
2057 goto out;
2058 }
2059 }
2060
Stefan Roesebb5c4282014-10-22 12:13:21 +02002061 if (debugmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002062 rc = kwboot_debugmsg(tty);
Pali Rohár2af673b2022-03-02 11:49:22 +01002063 if (rc)
Stefan Roesebb5c4282014-10-22 12:13:21 +02002064 goto out;
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -04002065 } else if (bootmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002066 rc = kwboot_bootmsg(tty);
Pali Rohár3a2e9bc2022-03-02 11:49:21 +01002067 if (rc)
Luka Perkovdff289642012-05-27 11:44:51 +00002068 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +00002069 }
2070
2071 if (img) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002072 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002073 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;
2088out:
2089 if (tty >= 0)
2090 close(tty);
2091
2092 if (img)
Pali Rohárd9355bf2021-09-24 23:07:03 +02002093 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00002094
2095 return rv;
2096
2097usage:
2098 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
2099 goto out;
2100}