blob: ba2fd10ff645183851a37926516022762a0a9f94 [file] [log] [blame]
Luka Perkovd131ad62012-05-27 11:44:51 +00001/*
Stefan Roese84899e22014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
3 * supports Kirkwood, Dove, Armada 370, Armada XP
Luka Perkovd131ad62012-05-27 11:44:51 +00004 *
5 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
6 *
7 * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
8 * Integrated Controller: Functional Specifications" December 2,
9 * 2008. Chapter 24.2 "BootROM Firmware".
10 */
11
Stefan Roesef4db6c92016-01-07 14:12:04 +010012#include "kwbimage.h"
13#include "mkimage.h"
Pali Rohára050a862021-09-24 23:06:42 +020014#include "version.h"
Stefan Roesef4db6c92016-01-07 14:12:04 +010015
Luka Perkovd131ad62012-05-27 11:44:51 +000016#include <stdlib.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdarg.h>
Stefan Roesef4db6c92016-01-07 14:12:04 +010020#include <image.h>
Luka Perkovd131ad62012-05-27 11:44:51 +000021#include <libgen.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <unistd.h>
25#include <stdint.h>
26#include <termios.h>
Marek Behún12df7b72021-09-24 23:06:52 +020027#include <time.h>
Luka Perkovd131ad62012-05-27 11:44:51 +000028#include <sys/stat.h>
29
Luka Perkovd131ad62012-05-27 11:44:51 +000030/*
31 * Marvell BootROM UART Sensing
32 */
33
34static unsigned char kwboot_msg_boot[] = {
35 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
36};
37
Stefan Roese84899e22014-10-22 12:13:21 +020038static unsigned char kwboot_msg_debug[] = {
39 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
40};
41
42/* Defines known to work on Kirkwood */
Luka Perkovd131ad62012-05-27 11:44:51 +000043#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
44#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
45
Stefan Roese84899e22014-10-22 12:13:21 +020046/* Defines known to work on Armada XP */
47#define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
48#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
49
Luka Perkovd131ad62012-05-27 11:44:51 +000050/*
51 * Xmodem Transfers
52 */
53
54#define SOH 1 /* sender start of block header */
55#define EOT 4 /* sender end of block transfer */
56#define ACK 6 /* target block ack */
57#define NAK 21 /* target block negative ack */
58#define CAN 24 /* target/sender transfer cancellation */
59
Pali Rohár2ef87f72021-09-24 23:06:48 +020060#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
61
Luka Perkovd131ad62012-05-27 11:44:51 +000062struct kwboot_block {
63 uint8_t soh;
64 uint8_t pnum;
65 uint8_t _pnum;
Pali Rohár2ef87f72021-09-24 23:06:48 +020066 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovd131ad62012-05-27 11:44:51 +000067 uint8_t csum;
Pali Rohára107c612021-07-23 11:14:14 +020068} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +000069
70#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
Marek Behún12df7b72021-09-24 23:06:52 +020071#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovd131ad62012-05-27 11:44:51 +000072
Pali Rohárca272042021-09-24 23:07:05 +020073/* ARM code making baudrate changing function return to original exec address */
74static unsigned char kwboot_pre_baud_code[] = {
75 /* exec_addr: */
76 0x00, 0x00, 0x00, 0x00, /* .word 0 */
77 0x0c, 0xe0, 0x1f, 0xe5, /* ldr lr, exec_addr */
78};
79
80/* ARM code for binary header injection to change baudrate */
81static unsigned char kwboot_baud_code[] = {
82 /* ; #define UART_BASE 0xd0012000 */
83 /* ; #define THR 0x00 */
84 /* ; #define DLL 0x00 */
85 /* ; #define DLH 0x04 */
86 /* ; #define LCR 0x0c */
87 /* ; #define DLAB 0x80 */
88 /* ; #define LSR 0x14 */
89 /* ; #define THRE 0x20 */
90 /* ; #define TEMT 0x40 */
91 /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
92 /* ; */
93 /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
94 /* ; const u8 *str = "$baudratechange"; */
95 /* ; u8 c; */
96 /* ; do { */
97 /* ; c = *str++; */
98 /* ; writel(UART_BASE + THR, c); */
99 /* ; } while (c); */
100 /* ; while */
101 /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
102 /* ; u32 lcr = readl(UART_BASE + LCR); */
103 /* ; writel(UART_BASE + LCR, lcr | DLAB); */
104 /* ; u8 old_dll = readl(UART_BASE + DLL); */
105 /* ; u8 old_dlh = readl(UART_BASE + DLH); */
106 /* ; u16 old_dl = old_dll | (old_dlh << 8); */
107 /* ; u32 clk = old_b * old_dl; */
108 /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
109 /* ; u8 new_dll = new_dl & 0xff; */
110 /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
111 /* ; writel(UART_BASE + DLL, new_dll); */
112 /* ; writel(UART_BASE + DLH, new_dlh); */
113 /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
114 /* ; msleep(1); */
115 /* ; return 0; */
116 /* ; } */
117
118 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
119
120 /* ; r0 = UART_BASE */
121 0x02, 0x0a, 0xa0, 0xe3, /* mov r0, #0x2000 */
122 0x01, 0x00, 0x4d, 0xe3, /* movt r0, #0xd001 */
123
124 /* ; r2 = address of preamble string */
125 0xd0, 0x20, 0x8f, 0xe2, /* adr r2, preamble */
126
127 /* ; Send preamble string over UART */
128 /* .Lloop_preamble: */
129 /* */
130 /* ; Wait until Transmitter Holding is Empty */
131 /* .Lloop_thre: */
132 /* ; r1 = UART_BASE[LSR] & THRE */
133 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
134 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
135 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
136
137 /* ; Put character into Transmitter FIFO */
138 /* ; r1 = *r2++ */
139 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
140 /* ; UART_BASE[THR] = r1 */
141 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
142
143 /* ; Loop until end of preamble string */
144 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
145 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
146
147 /* ; Wait until Transmitter FIFO is Empty */
148 /* .Lloop_txempty: */
149 /* ; r1 = UART_BASE[LSR] & TEMT */
150 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
151 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
152 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
153
154 /* ; Set Divisor Latch Access Bit */
155 /* ; UART_BASE[LCR] |= DLAB */
156 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
157 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
158 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
159
160 /* ; Read current Divisor Latch */
161 /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
162 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
163 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
164 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
165 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
166 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
167 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
168 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
169
170 /* ; Read old baudrate value */
171 /* ; r2 = old_baudrate */
172 0x8c, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
173
174 /* ; Calculate base clock */
175 /* ; r1 = r2 * r1 */
176 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
177
178 /* ; Read new baudrate value */
179 /* ; r2 = baudrate */
180 0x88, 0x20, 0x9f, 0xe5, /* ldr r2, baudrate */
181
182 /* ; Calculate new Divisor Latch */
183 /* ; r1 = DIV_ROUND(r1, r2) = */
184 /* ; = (r1 + r2/2) / r2 */
185 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
186 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
187 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
188 /* .Lloop_div1: */
189 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
190 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
191 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
192 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
193 /* .Lloop_div2: */
194 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
195 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
196 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
197 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
198 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
199 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
200 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
201
202 /* ; Set new Divisor Latch Low */
203 /* ; UART_BASE[DLL] = r1 & 0xff */
204 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
205 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
206 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
207
208 /* ; Set new Divisor Latch High */
209 /* ; UART_BASE[DLH] = r1>>8 & 0xff */
210 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
211 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
212 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
213
214 /* ; Clear Divisor Latch Access Bit */
215 /* ; UART_BASE[LCR] &= ~DLAB */
216 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
217 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
218 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
219
220 /* ; Sleep 1ms ~~ 600000 cycles at 1200 MHz */
221 /* ; r1 = 600000 */
222 0x9f, 0x1d, 0xa0, 0xe3, /* mov r1, #0x27c0 */
223 0x09, 0x10, 0x40, 0xe3, /* movt r1, #0x0009 */
224 /* .Lloop_sleep: */
225 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
226 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
227 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
228
229 /* ; Return 0 - no error */
230 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
231 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
232
233 /* ; Preamble string */
234 /* preamble: */
235 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
236 0x64, 0x72, 0x61, 0x74,
237 0x65, 0x63, 0x68, 0x61,
238 0x6e, 0x67, 0x65, 0x00,
239
240 /* ; Placeholder for old baudrate value */
241 /* old_baudrate: */
242 0x00, 0x00, 0x00, 0x00, /* .word 0 */
243
244 /* ; Placeholder for new baudrate value */
245 /* new_baudrate: */
246 0x00, 0x00, 0x00, 0x00, /* .word 0 */
247};
248
249#define KWBOOT_BAUDRATE_BIN_HEADER_SZ (sizeof(kwboot_baud_code) + \
250 sizeof(struct opt_hdr_v1) + 8)
251
252static const char kwb_baud_magic[16] = "$baudratechange";
253
Luka Perkovd131ad62012-05-27 11:44:51 +0000254static int kwboot_verbose;
255
Stefan Roese84899e22014-10-22 12:13:21 +0200256static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
257static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +0000258static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +0200259
Marek Behúne453bb42021-09-24 23:06:41 +0200260static ssize_t
261kwboot_write(int fd, const char *buf, size_t len)
262{
263 size_t tot = 0;
264
265 while (tot < len) {
266 ssize_t wr = write(fd, buf + tot, len - tot);
267
268 if (wr < 0)
269 return -1;
270
271 tot += wr;
272 }
273
274 return tot;
275}
276
Luka Perkovd131ad62012-05-27 11:44:51 +0000277static void
278kwboot_printv(const char *fmt, ...)
279{
280 va_list ap;
281
282 if (kwboot_verbose) {
283 va_start(ap, fmt);
284 vprintf(fmt, ap);
285 va_end(ap);
286 fflush(stdout);
287 }
288}
289
290static void
291__spinner(void)
292{
293 const char seq[] = { '-', '\\', '|', '/' };
294 const int div = 8;
295 static int state, bs;
296
297 if (state % div == 0) {
298 fputc(bs, stdout);
299 fputc(seq[state / div % sizeof(seq)], stdout);
300 fflush(stdout);
301 }
302
303 bs = '\b';
304 state++;
305}
306
307static void
308kwboot_spinner(void)
309{
310 if (kwboot_verbose)
311 __spinner();
312}
313
314static void
315__progress(int pct, char c)
316{
317 const int width = 70;
318 static const char *nl = "";
319 static int pos;
320
321 if (pos % width == 0)
322 printf("%s%3d %% [", nl, pct);
323
324 fputc(c, stdout);
325
326 nl = "]\n";
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200327 pos = (pos + 1) % width;
Luka Perkovd131ad62012-05-27 11:44:51 +0000328
329 if (pct == 100) {
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200330 while (pos && pos++ < width)
Luka Perkovd131ad62012-05-27 11:44:51 +0000331 fputc(' ', stdout);
332 fputs(nl, stdout);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200333 nl = "";
334 pos = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000335 }
336
337 fflush(stdout);
338
339}
340
341static void
342kwboot_progress(int _pct, char c)
343{
344 static int pct;
345
346 if (_pct != -1)
347 pct = _pct;
348
349 if (kwboot_verbose)
350 __progress(pct, c);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200351
352 if (pct == 100)
353 pct = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000354}
355
356static int
357kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
358{
359 int rc, nfds;
360 fd_set rfds;
361 struct timeval tv;
362 ssize_t n;
363
364 rc = -1;
365
366 FD_ZERO(&rfds);
367 FD_SET(fd, &rfds);
368
369 tv.tv_sec = 0;
370 tv.tv_usec = timeo * 1000;
371 if (tv.tv_usec > 1000000) {
372 tv.tv_sec += tv.tv_usec / 1000000;
373 tv.tv_usec %= 1000000;
374 }
375
376 do {
377 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
378 if (nfds < 0)
379 goto out;
380 if (!nfds) {
381 errno = ETIMEDOUT;
382 goto out;
383 }
384
385 n = read(fd, buf, len);
Willy Tarreau4469bd72018-07-03 12:10:31 -0400386 if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000387 goto out;
388
389 buf = (char *)buf + n;
390 len -= n;
391 } while (len > 0);
392
393 rc = 0;
394out:
395 return rc;
396}
397
398static int
399kwboot_tty_send(int fd, const void *buf, size_t len)
400{
Stefan Roese84899e22014-10-22 12:13:21 +0200401 if (!buf)
402 return 0;
403
Marek Behúne453bb42021-09-24 23:06:41 +0200404 if (kwboot_write(fd, buf, len) < 0)
405 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000406
Marek Behúne453bb42021-09-24 23:06:41 +0200407 return tcdrain(fd);
Luka Perkovd131ad62012-05-27 11:44:51 +0000408}
409
410static int
411kwboot_tty_send_char(int fd, unsigned char c)
412{
413 return kwboot_tty_send(fd, &c, 1);
414}
415
416static speed_t
Pali Rohárca272042021-09-24 23:07:05 +0200417kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000418{
419 switch (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +0200420#ifdef B4000000
421 case 4000000:
422 return B4000000;
423#endif
424#ifdef B3500000
425 case 3500000:
426 return B3500000;
427#endif
428#ifdef B3000000
429 case 3000000:
430 return B3000000;
431#endif
432#ifdef B2500000
433 case 2500000:
434 return B2500000;
435#endif
436#ifdef B2000000
437 case 2000000:
438 return B2000000;
439#endif
440#ifdef B1500000
441 case 1500000:
442 return B1500000;
443#endif
444#ifdef B1152000
445 case 1152000:
446 return B1152000;
447#endif
448#ifdef B1000000
449 case 1000000:
450 return B1000000;
451#endif
452#ifdef B921600
453 case 921600:
454 return B921600;
455#endif
456#ifdef B614400
457 case 614400:
458 return B614400;
459#endif
460#ifdef B576000
461 case 576000:
462 return B576000;
463#endif
464#ifdef B500000
465 case 500000:
466 return B500000;
467#endif
468#ifdef B460800
469 case 460800:
470 return B460800;
471#endif
472#ifdef B307200
473 case 307200:
474 return B307200;
475#endif
476#ifdef B230400
477 case 230400:
478 return B230400;
479#endif
480#ifdef B153600
481 case 153600:
482 return B153600;
483#endif
484#ifdef B115200
Luka Perkovd131ad62012-05-27 11:44:51 +0000485 case 115200:
486 return B115200;
Pali Rohárca272042021-09-24 23:07:05 +0200487#endif
488#ifdef B76800
489 case 76800:
490 return B76800;
491#endif
492#ifdef B57600
Luka Perkovd131ad62012-05-27 11:44:51 +0000493 case 57600:
494 return B57600;
Pali Rohárca272042021-09-24 23:07:05 +0200495#endif
496#ifdef B38400
Luka Perkovd131ad62012-05-27 11:44:51 +0000497 case 38400:
498 return B38400;
Pali Rohárca272042021-09-24 23:07:05 +0200499#endif
500#ifdef B19200
Luka Perkovd131ad62012-05-27 11:44:51 +0000501 case 19200:
502 return B19200;
Pali Rohárca272042021-09-24 23:07:05 +0200503#endif
504#ifdef B9600
Luka Perkovd131ad62012-05-27 11:44:51 +0000505 case 9600:
506 return B9600;
Pali Rohárca272042021-09-24 23:07:05 +0200507#endif
508#ifdef B4800
509 case 4800:
510 return B4800;
511#endif
512#ifdef B2400
513 case 2400:
514 return B2400;
515#endif
516#ifdef B1800
517 case 1800:
518 return B1800;
519#endif
520#ifdef B1200
521 case 1200:
522 return B1200;
523#endif
524#ifdef B600
525 case 600:
526 return B600;
527#endif
528#ifdef B300
529 case 300:
530 return B300;
531#endif
532#ifdef B200
533 case 200:
534 return B200;
535#endif
536#ifdef B150
537 case 150:
538 return B150;
539#endif
540#ifdef B134
541 case 134:
542 return B134;
543#endif
544#ifdef B110
545 case 110:
546 return B110;
547#endif
548#ifdef B75
549 case 75:
550 return B75;
551#endif
552#ifdef B50
553 case 50:
554 return B50;
555#endif
556 default:
557 return B0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000558 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000559}
560
561static int
Pali Rohárca272042021-09-24 23:07:05 +0200562kwboot_tty_change_baudrate(int fd, int baudrate)
563{
564 struct termios tio;
565 speed_t speed;
566 int rc;
567
568 rc = tcgetattr(fd, &tio);
569 if (rc)
570 return rc;
571
572 speed = kwboot_tty_baudrate_to_speed(baudrate);
573 if (speed == B0) {
574 errno = EINVAL;
575 return -1;
576 }
577
578 rc = cfsetospeed(&tio, speed);
579 if (rc)
580 return rc;
581
582 rc = cfsetispeed(&tio, speed);
583 if (rc)
584 return rc;
585
586 rc = tcsetattr(fd, TCSANOW, &tio);
587 if (rc)
588 return rc;
589
590 return 0;
591}
592
593static int
594kwboot_open_tty(const char *path, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000595{
596 int rc, fd;
597 struct termios tio;
598
599 rc = -1;
600
601 fd = open(path, O_RDWR|O_NOCTTY|O_NDELAY);
602 if (fd < 0)
603 goto out;
604
605 memset(&tio, 0, sizeof(tio));
606
607 tio.c_iflag = 0;
608 tio.c_cflag = CREAD|CLOCAL|CS8;
609
610 tio.c_cc[VMIN] = 1;
611 tio.c_cc[VTIME] = 10;
612
Luka Perkovd131ad62012-05-27 11:44:51 +0000613 rc = tcsetattr(fd, TCSANOW, &tio);
614 if (rc)
615 goto out;
616
Pali Rohárca272042021-09-24 23:07:05 +0200617 rc = kwboot_tty_change_baudrate(fd, baudrate);
618 if (rc)
619 goto out;
620
Luka Perkovd131ad62012-05-27 11:44:51 +0000621 rc = fd;
622out:
623 if (rc < 0) {
624 if (fd >= 0)
625 close(fd);
626 }
627
628 return rc;
629}
630
631static int
632kwboot_bootmsg(int tty, void *msg)
633{
634 int rc;
635 char c;
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300636 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000637
Stefan Roese84899e22014-10-22 12:13:21 +0200638 if (msg == NULL)
639 kwboot_printv("Please reboot the target into UART boot mode...");
640 else
641 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000642
643 do {
644 rc = tcflush(tty, TCIOFLUSH);
645 if (rc)
646 break;
647
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300648 for (count = 0; count < 128; count++) {
649 rc = kwboot_tty_send(tty, msg, 8);
650 if (rc) {
651 usleep(msg_req_delay * 1000);
652 continue;
653 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000654 }
655
Stefan Roese84899e22014-10-22 12:13:21 +0200656 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000657
658 kwboot_spinner();
659
660 } while (rc || c != NAK);
661
662 kwboot_printv("\n");
663
664 return rc;
665}
666
667static int
Stefan Roese84899e22014-10-22 12:13:21 +0200668kwboot_debugmsg(int tty, void *msg)
669{
670 int rc;
671
672 kwboot_printv("Sending debug message. Please reboot the target...");
673
674 do {
675 char buf[16];
676
677 rc = tcflush(tty, TCIOFLUSH);
678 if (rc)
679 break;
680
681 rc = kwboot_tty_send(tty, msg, 8);
682 if (rc) {
683 usleep(msg_req_delay * 1000);
684 continue;
685 }
686
687 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
688
689 kwboot_spinner();
690
691 } while (rc);
692
693 kwboot_printv("\n");
694
695 return rc;
696}
697
Pali Rohárc5d666a2021-09-24 23:06:44 +0200698static size_t
Luka Perkovd131ad62012-05-27 11:44:51 +0000699kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
700 size_t size, int pnum)
701{
Marek Behúnd8cc8512021-09-24 23:06:45 +0200702 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +0000703
Stefan Roese84899e22014-10-22 12:13:21 +0200704 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000705 block->pnum = pnum;
706 block->_pnum = ~block->pnum;
707
Pali Rohár2ef87f72021-09-24 23:06:48 +0200708 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovd131ad62012-05-27 11:44:51 +0000709 memcpy(&block->data[0], data, n);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200710 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovd131ad62012-05-27 11:44:51 +0000711
712 block->csum = 0;
713 for (i = 0; i < n; i++)
714 block->csum += block->data[i];
715
716 return n;
717}
718
Marek Behún12df7b72021-09-24 23:06:52 +0200719static uint64_t
720_now(void)
721{
722 struct timespec ts;
723
724 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
725 static int err_print;
726
727 if (!err_print) {
728 perror("clock_gettime() does not work");
729 err_print = 1;
730 }
731
732 /* this will just make the timeout not work */
733 return -1ULL;
734 }
735
736 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
737}
738
Luka Perkovd131ad62012-05-27 11:44:51 +0000739static int
Marek Behún408ea612021-09-24 23:06:49 +0200740_is_xm_reply(char c)
741{
742 return c == ACK || c == NAK || c == CAN;
743}
744
745static int
Pali Rohár9cdc2642021-09-24 23:06:54 +0200746_xm_reply_to_error(int c)
747{
748 int rc = -1;
749
750 switch (c) {
751 case ACK:
752 rc = 0;
753 break;
754 case NAK:
755 errno = EBADMSG;
756 break;
757 case CAN:
758 errno = ECANCELED;
759 break;
760 default:
761 errno = EPROTO;
762 break;
763 }
764
765 return rc;
766}
767
768static int
Pali Rohárca272042021-09-24 23:07:05 +0200769kwboot_baud_magic_handle(int fd, char c, int baudrate)
770{
771 static size_t rcv_len;
772
773 if (rcv_len < sizeof(kwb_baud_magic)) {
774 /* try to recognize whole magic word */
775 if (c == kwb_baud_magic[rcv_len]) {
776 rcv_len++;
777 } else {
778 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
779 fflush(stdout);
780 rcv_len = 0;
781 }
782 }
783
784 if (rcv_len == sizeof(kwb_baud_magic)) {
785 /* magic word received */
786 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
787
788 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
789 } else {
790 return 0;
791 }
792}
793
794static int
795kwboot_xm_recv_reply(int fd, char *c, int allow_non_xm, int *non_xm_print,
796 int baudrate, int *baud_changed)
Pali Rohár48b3ea62021-09-24 23:06:50 +0200797{
Marek Behún12df7b72021-09-24 23:06:52 +0200798 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún819cd322021-09-24 23:06:53 +0200799 uint64_t recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200800 int rc;
801
Marek Behún819cd322021-09-24 23:06:53 +0200802 if (non_xm_print)
803 *non_xm_print = 0;
Pali Rohárca272042021-09-24 23:07:05 +0200804 if (baud_changed)
805 *baud_changed = 0;
Marek Behún2e81b3a2021-09-24 23:06:51 +0200806
Pali Rohár48b3ea62021-09-24 23:06:50 +0200807 while (1) {
Marek Behún12df7b72021-09-24 23:06:52 +0200808 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200809 if (rc) {
810 if (errno != ETIMEDOUT)
811 return rc;
Marek Behún819cd322021-09-24 23:06:53 +0200812 else if (allow_non_xm && *non_xm_print)
Marek Behún12df7b72021-09-24 23:06:52 +0200813 return -1;
814 else
815 *c = NAK;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200816 }
817
818 /* If received xmodem reply, end. */
819 if (_is_xm_reply(*c))
820 break;
821
822 /*
Pali Rohárca272042021-09-24 23:07:05 +0200823 * If receiving/printing non-xmodem text output is allowed and
824 * such a byte was received, we want to increase receiving time
825 * and either:
826 * - print the byte, if it is not part of baudrate change magic
827 * sequence while baudrate change was requested (-B option)
828 * - change baudrate
Marek Behún819cd322021-09-24 23:06:53 +0200829 * Otherwise decrease timeout by time elapsed.
Pali Rohár48b3ea62021-09-24 23:06:50 +0200830 */
831 if (allow_non_xm) {
Marek Behún12df7b72021-09-24 23:06:52 +0200832 recv_until = _now() + timeout;
Pali Rohárca272042021-09-24 23:07:05 +0200833
834 if (baudrate && !*baud_changed) {
835 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
836 if (rc == 1)
837 *baud_changed = 1;
838 else if (!rc)
839 *non_xm_print = 1;
840 else
841 return rc;
842 } else if (!baudrate || !*baud_changed) {
843 putchar(*c);
844 fflush(stdout);
845 *non_xm_print = 1;
846 }
Marek Behún819cd322021-09-24 23:06:53 +0200847 } else {
848 timeout = recv_until - _now();
849 if (timeout < 0) {
850 errno = ETIMEDOUT;
851 return -1;
852 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200853 }
854 }
855
856 return 0;
857}
858
859static int
860kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohárca272042021-09-24 23:07:05 +0200861 int *done_print, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000862{
Pali Rohárca272042021-09-24 23:07:05 +0200863 int non_xm_print, baud_changed;
864 int rc, err, retries;
Luka Perkovd131ad62012-05-27 11:44:51 +0000865 char c;
866
Pali Rohár48b3ea62021-09-24 23:06:50 +0200867 *done_print = 0;
868
Luka Perkovd131ad62012-05-27 11:44:51 +0000869 retries = 16;
870 do {
871 rc = kwboot_tty_send(fd, block, sizeof(*block));
872 if (rc)
Pali Rohár00a1dee2021-09-24 23:06:43 +0200873 return rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000874
Pali Rohár48b3ea62021-09-24 23:06:50 +0200875 if (allow_non_xm && !*done_print) {
876 kwboot_progress(100, '.');
877 kwboot_printv("Done\n");
878 *done_print = 1;
879 }
Stefan Roese84899e22014-10-22 12:13:21 +0200880
Pali Rohárca272042021-09-24 23:07:05 +0200881 rc = kwboot_xm_recv_reply(fd, &c, allow_non_xm, &non_xm_print,
882 baudrate, &baud_changed);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200883 if (rc)
Pali Rohárca272042021-09-24 23:07:05 +0200884 goto can;
Stefan Roese84899e22014-10-22 12:13:21 +0200885
Pali Rohár48b3ea62021-09-24 23:06:50 +0200886 if (!allow_non_xm && c != ACK)
Luka Perkovd131ad62012-05-27 11:44:51 +0000887 kwboot_progress(-1, '+');
Luka Perkovd131ad62012-05-27 11:44:51 +0000888 } while (c == NAK && retries-- > 0);
889
Marek Behún2e81b3a2021-09-24 23:06:51 +0200890 if (non_xm_print)
891 kwboot_printv("\n");
892
Pali Rohárca272042021-09-24 23:07:05 +0200893 if (allow_non_xm && baudrate && !baud_changed) {
894 fprintf(stderr, "Baudrate was not changed\n");
895 rc = -1;
896 errno = EPROTO;
897 goto can;
898 }
899
Pali Rohár9cdc2642021-09-24 23:06:54 +0200900 return _xm_reply_to_error(c);
Pali Rohárca272042021-09-24 23:07:05 +0200901can:
902 err = errno;
903 kwboot_tty_send_char(fd, CAN);
904 kwboot_printv("\n");
905 errno = err;
906 return rc;
Pali Rohár9cdc2642021-09-24 23:06:54 +0200907}
Luka Perkovd131ad62012-05-27 11:44:51 +0000908
Pali Rohár9cdc2642021-09-24 23:06:54 +0200909static int
910kwboot_xm_finish(int fd)
911{
912 int rc, retries;
913 char c;
Luka Perkovd131ad62012-05-27 11:44:51 +0000914
Pali Rohár9cdc2642021-09-24 23:06:54 +0200915 kwboot_printv("Finishing transfer\n");
916
917 retries = 16;
918 do {
919 rc = kwboot_tty_send_char(fd, EOT);
920 if (rc)
921 return rc;
922
Pali Rohárca272042021-09-24 23:07:05 +0200923 rc = kwboot_xm_recv_reply(fd, &c, 0, NULL, 0, NULL);
Pali Rohár9cdc2642021-09-24 23:06:54 +0200924 if (rc)
925 return rc;
926 } while (c == NAK && retries-- > 0);
927
928 return _xm_reply_to_error(c);
Luka Perkovd131ad62012-05-27 11:44:51 +0000929}
930
931static int
Pali Rohár2ef87f72021-09-24 23:06:48 +0200932kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohárca272042021-09-24 23:07:05 +0200933 size_t size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000934{
Pali Rohár48b3ea62021-09-24 23:06:50 +0200935 int done_print = 0;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200936 size_t sent, left;
937 int rc;
Luka Perkovd131ad62012-05-27 11:44:51 +0000938
Pali Rohár2ef87f72021-09-24 23:06:48 +0200939 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
940 header ? "header" : "data", size);
Luka Perkovd131ad62012-05-27 11:44:51 +0000941
Pali Rohár2ef87f72021-09-24 23:06:48 +0200942 left = size;
943 sent = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000944
Pali Rohár2ef87f72021-09-24 23:06:48 +0200945 while (sent < size) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000946 struct kwboot_block block;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200947 int last_block;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200948 size_t blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000949
Pali Rohár2ef87f72021-09-24 23:06:48 +0200950 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
951 data += blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000952
Pali Rohár48b3ea62021-09-24 23:06:50 +0200953 last_block = (left <= blksz);
954
955 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohárca272042021-09-24 23:07:05 +0200956 &done_print, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +0000957 if (rc)
958 goto out;
959
Pali Rohár2ef87f72021-09-24 23:06:48 +0200960 sent += blksz;
961 left -= blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +0000962
Pali Rohár48b3ea62021-09-24 23:06:50 +0200963 if (!done_print)
964 kwboot_progress(sent * 100 / size, '.');
Pali Rohár2ef87f72021-09-24 23:06:48 +0200965 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000966
Pali Rohár48b3ea62021-09-24 23:06:50 +0200967 if (!done_print)
968 kwboot_printv("Done\n");
Pali Rohár2ef87f72021-09-24 23:06:48 +0200969
970 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000971out:
Pali Rohárd5ba8db2021-09-24 23:06:47 +0200972 kwboot_printv("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +0000973 return rc;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200974}
Luka Perkovd131ad62012-05-27 11:44:51 +0000975
Pali Rohár2ef87f72021-09-24 23:06:48 +0200976static int
Pali Rohárca272042021-09-24 23:07:05 +0200977kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohár2ef87f72021-09-24 23:06:48 +0200978{
979 const uint8_t *img = _img;
980 int rc, pnum;
981 size_t hdrsz;
982
Marek Behúnfe2fd732021-09-24 23:07:01 +0200983 hdrsz = kwbheader_size(img);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200984
985 kwboot_printv("Waiting 2s and flushing tty\n");
986 sleep(2); /* flush isn't effective without it */
987 tcflush(tty, TCIOFLUSH);
988
989 pnum = 1;
990
Pali Rohárca272042021-09-24 23:07:05 +0200991 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200992 if (rc)
993 return rc;
994
995 img += hdrsz;
996 size -= hdrsz;
997
Pali Rohárca272042021-09-24 23:07:05 +0200998 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200999 if (rc)
1000 return rc;
1001
Pali Rohárca272042021-09-24 23:07:05 +02001002 rc = kwboot_xm_finish(tty);
1003 if (rc)
1004 return rc;
1005
1006 if (baudrate) {
1007 char buf[sizeof(kwb_baud_magic)];
1008
1009 /* Wait 1s for baudrate change magic */
1010 rc = kwboot_tty_recv(tty, buf, sizeof(buf), 1000);
1011 if (rc)
1012 return rc;
1013
1014 if (memcmp(buf, kwb_baud_magic, sizeof(buf))) {
1015 errno = EPROTO;
1016 return -1;
1017 }
1018
1019 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1020 rc = kwboot_tty_change_baudrate(tty, 115200);
1021 if (rc)
1022 return rc;
1023 }
1024
1025 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001026}
1027
1028static int
Marek Behún46237e62021-09-24 23:06:40 +02001029kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +00001030{
Marek Behúne453bb42021-09-24 23:06:41 +02001031 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +00001032 char _buf[128], *buf = _buf;
1033
Pali Rohár43fef8d2021-07-23 11:14:17 +02001034 nin = read(in, buf, sizeof(_buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -04001035 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +00001036 return -1;
1037
1038 if (quit) {
1039 int i;
1040
1041 for (i = 0; i < nin; i++) {
1042 if (*buf == quit[*s]) {
1043 (*s)++;
1044 if (!quit[*s])
1045 return 0;
1046 buf++;
1047 nin--;
Pali Rohárb943eee2021-07-23 11:14:20 +02001048 } else {
Marek Behúne453bb42021-09-24 23:06:41 +02001049 if (kwboot_write(out, quit, *s) < 0)
1050 return -1;
1051 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +02001052 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001053 }
1054 }
1055
Marek Behúne453bb42021-09-24 23:06:41 +02001056 if (kwboot_write(out, buf, nin) < 0)
1057 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001058
1059 return 0;
1060}
1061
1062static int
1063kwboot_terminal(int tty)
1064{
1065 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +02001066 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +00001067 struct termios otio, tio;
1068
1069 rc = -1;
1070
1071 in = STDIN_FILENO;
1072 if (isatty(in)) {
1073 rc = tcgetattr(in, &otio);
1074 if (!rc) {
1075 tio = otio;
1076 cfmakeraw(&tio);
1077 rc = tcsetattr(in, TCSANOW, &tio);
1078 }
1079 if (rc) {
1080 perror("tcsetattr");
1081 goto out;
1082 }
1083
1084 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
1085 quit[0]|0100, quit[1]);
1086 } else
1087 in = -1;
1088
1089 rc = 0;
1090 s = 0;
1091
1092 do {
1093 fd_set rfds;
1094 int nfds = 0;
1095
1096 FD_SET(tty, &rfds);
1097 nfds = nfds < tty ? tty : nfds;
1098
1099 if (in >= 0) {
1100 FD_SET(in, &rfds);
1101 nfds = nfds < in ? in : nfds;
1102 }
1103
1104 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1105 if (nfds < 0)
1106 break;
1107
1108 if (FD_ISSET(tty, &rfds)) {
1109 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
1110 if (rc)
1111 break;
1112 }
1113
Marek Behúnf30cb0d2021-09-24 23:06:39 +02001114 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001115 rc = kwboot_term_pipe(in, tty, quit, &s);
1116 if (rc)
1117 break;
1118 }
1119 } while (quit[s] != 0);
1120
Pali Rohárec0fe5b2021-07-23 11:14:18 +02001121 if (in >= 0)
1122 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +02001123 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001124out:
1125 return rc;
1126}
1127
1128static void *
Pali Rohár04ced022021-09-24 23:07:03 +02001129kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovd131ad62012-05-27 11:44:51 +00001130{
Pali Rohárddc04fa2021-09-24 23:06:55 +02001131 int rc, fd;
Luka Perkovd131ad62012-05-27 11:44:51 +00001132 struct stat st;
1133 void *img;
Pali Rohár04ced022021-09-24 23:07:03 +02001134 off_t tot;
Luka Perkovd131ad62012-05-27 11:44:51 +00001135
1136 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001137 img = NULL;
1138
1139 fd = open(path, O_RDONLY);
1140 if (fd < 0)
1141 goto out;
1142
1143 rc = fstat(fd, &st);
1144 if (rc)
1145 goto out;
1146
Pali Rohár04ced022021-09-24 23:07:03 +02001147 img = malloc(st.st_size + reserve);
1148 if (!img)
Luka Perkovd131ad62012-05-27 11:44:51 +00001149 goto out;
Pali Rohár04ced022021-09-24 23:07:03 +02001150
1151 tot = 0;
1152 while (tot < st.st_size) {
1153 ssize_t rd = read(fd, img + tot, st.st_size - tot);
1154
1155 if (rd < 0)
1156 goto out;
1157
1158 tot += rd;
1159
1160 if (!rd && tot < st.st_size) {
1161 errno = EIO;
1162 goto out;
1163 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001164 }
1165
1166 rc = 0;
1167 *size = st.st_size;
1168out:
1169 if (rc && img) {
Pali Rohár04ced022021-09-24 23:07:03 +02001170 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001171 img = NULL;
1172 }
1173 if (fd >= 0)
1174 close(fd);
1175
1176 return img;
1177}
1178
1179static uint8_t
Marek Behúnfe2fd732021-09-24 23:07:01 +02001180kwboot_hdr_csum8(const void *hdr)
Luka Perkovd131ad62012-05-27 11:44:51 +00001181{
Marek Behúnfe2fd732021-09-24 23:07:01 +02001182 const uint8_t *data = hdr;
1183 uint8_t csum;
1184 size_t size;
1185
1186 size = kwbheader_size_for_csum(hdr);
Luka Perkovd131ad62012-05-27 11:44:51 +00001187
1188 for (csum = 0; size-- > 0; data++)
1189 csum += *data;
1190
1191 return csum;
1192}
1193
1194static int
Pali Rohár550c9302021-09-24 23:06:57 +02001195kwboot_img_is_secure(void *img)
1196{
1197 struct opt_hdr_v1 *ohdr;
1198
1199 for_each_opt_hdr_v1 (ohdr, img)
1200 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1201 return 1;
1202
1203 return 0;
1204}
1205
Pali Rohárca272042021-09-24 23:07:05 +02001206static void *
1207kwboot_img_grow_data_left(void *img, size_t *size, size_t grow)
1208{
1209 uint32_t hdrsz, datasz, srcaddr;
1210 struct main_hdr_v1 *hdr = img;
1211 uint8_t *data;
1212
1213 srcaddr = le32_to_cpu(hdr->srcaddr);
1214
1215 hdrsz = kwbheader_size(hdr);
1216 data = (uint8_t *)img + srcaddr;
1217 datasz = *size - srcaddr;
1218
1219 /* only move data if there is not enough space */
1220 if (hdrsz + grow > srcaddr) {
1221 size_t need = hdrsz + grow - srcaddr;
1222
1223 /* move data by enough bytes */
1224 memmove(data + need, data, datasz);
1225 *size += need;
1226 srcaddr += need;
1227 }
1228
1229 srcaddr -= grow;
1230 hdr->srcaddr = cpu_to_le32(srcaddr);
1231 hdr->destaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) - grow);
1232 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
1233
1234 return (uint8_t *)img + srcaddr;
1235}
1236
Pali Rohár04ced022021-09-24 23:07:03 +02001237static void
1238kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1239{
1240 uint32_t hdrsz, datasz, srcaddr;
1241 struct main_hdr_v1 *hdr = img;
1242 uint8_t *data;
1243
1244 srcaddr = le32_to_cpu(hdr->srcaddr);
1245
1246 hdrsz = kwbheader_size(img);
1247 data = (uint8_t *)img + srcaddr;
1248 datasz = *size - srcaddr;
1249
1250 /* only move data if there is not enough space */
1251 if (hdrsz + grow > srcaddr) {
1252 size_t need = hdrsz + grow - srcaddr;
1253
1254 /* move data by enough bytes */
1255 memmove(data + need, data, datasz);
1256
1257 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1258 *size += need;
1259 }
1260
1261 if (kwbimage_version(img) == 1) {
1262 hdrsz += grow;
1263 hdr->headersz_msb = hdrsz >> 16;
1264 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1265 }
1266}
1267
Pali Rohárca272042021-09-24 23:07:05 +02001268static void *
1269kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1270{
1271 struct main_hdr_v1 *hdr = img;
1272 struct opt_hdr_v1 *ohdr;
1273 uint32_t ohdrsz;
1274
1275 ohdrsz = binsz + 8 + sizeof(*ohdr);
1276 kwboot_img_grow_hdr(img, size, ohdrsz);
1277
1278 if (hdr->ext & 0x1) {
1279 for_each_opt_hdr_v1 (ohdr, img)
1280 if (opt_hdr_v1_next(ohdr) == NULL)
1281 break;
1282
1283 *opt_hdr_v1_ext(ohdr) |= 1;
1284 ohdr = opt_hdr_v1_next(ohdr);
1285 } else {
1286 hdr->ext |= 1;
1287 ohdr = (void *)(hdr + 1);
1288 }
1289
1290 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1291 ohdr->headersz_msb = ohdrsz >> 16;
1292 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1293
1294 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
1295
1296 return &ohdr->data[4];
1297}
1298
1299static void
1300_copy_baudrate_change_code(struct main_hdr_v1 *hdr, void *dst, int pre,
1301 int old_baud, int new_baud)
1302{
1303 size_t codesz = sizeof(kwboot_baud_code);
1304 uint8_t *code = dst;
1305
1306 if (pre) {
1307 size_t presz = sizeof(kwboot_pre_baud_code);
1308
1309 /*
1310 * We need to prepend code that loads lr register with original
1311 * value of hdr->execaddr. We do this by putting the original
1312 * exec address before the code that loads it relatively from
1313 * it's beginning.
1314 * Afterwards we change the exec address to this code (which is
1315 * at offset 4, because the first 4 bytes contain the original
1316 * exec address).
1317 */
1318 memcpy(code, kwboot_pre_baud_code, presz);
1319 *(uint32_t *)code = hdr->execaddr;
1320
1321 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + 4);
1322
1323 code += presz;
1324 }
1325
1326 memcpy(code, kwboot_baud_code, codesz - 8);
1327 *(uint32_t *)(code + codesz - 8) = cpu_to_le32(old_baud);
1328 *(uint32_t *)(code + codesz - 4) = cpu_to_le32(new_baud);
1329}
1330
Pali Rohár550c9302021-09-24 23:06:57 +02001331static int
Pali Rohárca272042021-09-24 23:07:05 +02001332kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001333{
1334 int rc;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001335 struct main_hdr_v1 *hdr;
Pali Rohár792e4232021-09-24 23:06:58 +02001336 uint32_t srcaddr;
Luka Perkovd131ad62012-05-27 11:44:51 +00001337 uint8_t csum;
Marek Behún5c8f8122021-09-24 23:07:04 +02001338 size_t hdrsz;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001339 int image_ver;
Pali Rohár550c9302021-09-24 23:06:57 +02001340 int is_secure;
Luka Perkovd131ad62012-05-27 11:44:51 +00001341
1342 rc = -1;
1343 hdr = img;
1344
Marek Behún5c8f8122021-09-24 23:07:04 +02001345 if (*size < sizeof(struct main_hdr_v1)) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001346 errno = EINVAL;
1347 goto out;
1348 }
1349
Marek Behúnacb0b382021-09-24 23:07:00 +02001350 image_ver = kwbimage_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +02001351 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +02001352 fprintf(stderr, "Invalid image header version\n");
1353 errno = EINVAL;
1354 goto out;
1355 }
1356
Marek Behúnfe2fd732021-09-24 23:07:01 +02001357 hdrsz = kwbheader_size(hdr);
Stefan Roesee29f1db2015-09-29 09:19:59 +02001358
Pali Rohár04ced022021-09-24 23:07:03 +02001359 if (*size < hdrsz) {
Pali Rohár825a2ca2021-07-23 11:14:21 +02001360 errno = EINVAL;
1361 goto out;
1362 }
1363
Marek Behúnfe2fd732021-09-24 23:07:01 +02001364 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001365 if (csum != hdr->checksum) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001366 errno = EINVAL;
1367 goto out;
1368 }
1369
Pali Rohár792e4232021-09-24 23:06:58 +02001370 if (image_ver == 0) {
1371 struct main_hdr_v0 *hdr_v0 = img;
1372
1373 hdr_v0->nandeccmode = IBR_HDR_ECC_DISABLED;
1374 hdr_v0->nandpagesize = 0;
1375 }
1376
1377 srcaddr = le32_to_cpu(hdr->srcaddr);
1378
1379 switch (hdr->blockid) {
1380 case IBR_HDR_SATA_ID:
1381 if (srcaddr < 1) {
1382 errno = EINVAL;
1383 goto out;
1384 }
1385 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
1386 break;
1387
1388 case IBR_HDR_SDIO_ID:
1389 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
1390 break;
1391
1392 case IBR_HDR_PEX_ID:
1393 if (srcaddr == 0xFFFFFFFF)
1394 hdr->srcaddr = cpu_to_le32(hdrsz);
1395 break;
Pali Rohárf2c644e2021-09-24 23:06:59 +02001396
1397 case IBR_HDR_SPI_ID:
1398 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1399 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1400 hdr->destaddr = cpu_to_le32(0x00800000);
1401 hdr->execaddr = cpu_to_le32(0x00800000);
1402 }
1403 break;
Pali Rohár792e4232021-09-24 23:06:58 +02001404 }
1405
Pali Rohár04ced022021-09-24 23:07:03 +02001406 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
1407 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) {
1408 errno = EINVAL;
1409 goto out;
1410 }
1411
Pali Rohár550c9302021-09-24 23:06:57 +02001412 is_secure = kwboot_img_is_secure(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001413
Pali Rohár550c9302021-09-24 23:06:57 +02001414 if (hdr->blockid != IBR_HDR_UART_ID) {
1415 if (is_secure) {
1416 fprintf(stderr,
1417 "Image has secure header with signature for non-UART booting\n");
1418 errno = EINVAL;
1419 goto out;
1420 }
1421
1422 kwboot_printv("Patching image boot signature to UART\n");
1423 hdr->blockid = IBR_HDR_UART_ID;
1424 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001425
Pali Rohárca272042021-09-24 23:07:05 +02001426 if (baudrate) {
1427 uint32_t codesz = sizeof(kwboot_baud_code);
1428 void *code;
1429
1430 if (image_ver == 0) {
1431 fprintf(stderr,
1432 "Cannot inject code for changing baudrate into v0 image header\n");
1433 errno = EINVAL;
1434 goto out;
1435 }
1436
1437 if (is_secure) {
1438 fprintf(stderr,
1439 "Cannot inject code for changing baudrate into image with secure header\n");
1440 errno = EINVAL;
1441 goto out;
1442 }
1443
1444 /*
1445 * First inject code that changes the baudrate from the default
1446 * value of 115200 Bd to requested value. This code is inserted
1447 * as a new opt hdr, so it is executed by BootROM after the
1448 * header part is received.
1449 */
1450 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
1451 baudrate);
1452
1453 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
1454 _copy_baudrate_change_code(hdr, code, 0, 115200, baudrate);
1455
1456 /*
1457 * Now inject code that changes the baudrate back to 115200 Bd.
1458 * This code is prepended to the data part of the image, so it
1459 * is executed before U-Boot proper.
1460 */
1461 kwboot_printv("Injecting code for changing baudrate back\n");
1462
1463 codesz += sizeof(kwboot_pre_baud_code);
1464 code = kwboot_img_grow_data_left(img, size, codesz);
1465 _copy_baudrate_change_code(hdr, code, 1, baudrate, 115200);
1466
1467 /* recompute header size */
1468 hdrsz = kwbheader_size(hdr);
1469 }
1470
Pali Rohár04ced022021-09-24 23:07:03 +02001471 if (hdrsz % KWBOOT_XM_BLKSZ) {
1472 size_t offset = (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) %
1473 KWBOOT_XM_BLKSZ;
1474
1475 if (is_secure) {
1476 fprintf(stderr, "Cannot align image with secure header\n");
1477 errno = EINVAL;
1478 goto out;
1479 }
1480
1481 kwboot_printv("Aligning image header to Xmodem block size\n");
1482 kwboot_img_grow_hdr(img, size, offset);
1483 }
1484
Marek Behúnfe2fd732021-09-24 23:07:01 +02001485 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +00001486
Pali Rohár04ced022021-09-24 23:07:03 +02001487 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Luka Perkovd131ad62012-05-27 11:44:51 +00001488 rc = 0;
1489out:
1490 return rc;
1491}
1492
1493static void
1494kwboot_usage(FILE *stream, char *progname)
1495{
Pali Rohára050a862021-09-24 23:06:42 +02001496 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovd131ad62012-05-27 11:44:51 +00001497 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +00001498 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +02001499 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +00001500 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +02001501 fprintf(stream,
1502 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roese84899e22014-10-22 12:13:21 +02001503 fprintf(stream,
1504 " -D <image>: boot <image> without preamble (Dove)\n");
1505 fprintf(stream, " -d: enter debug mode\n");
1506 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001507 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
1508 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +00001509 fprintf(stream,
1510 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001511 fprintf(stream, "\n");
1512 fprintf(stream, " -t: mini terminal\n");
1513 fprintf(stream, "\n");
1514 fprintf(stream, " -B <baud>: set baud rate\n");
1515 fprintf(stream, "\n");
1516}
1517
1518int
1519main(int argc, char **argv)
1520{
1521 const char *ttypath, *imgpath;
Pali Rohárddc04fa2021-09-24 23:06:55 +02001522 int rv, rc, tty, term;
Luka Perkovd131ad62012-05-27 11:44:51 +00001523 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +02001524 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +00001525 void *img;
1526 size_t size;
Pali Rohárca272042021-09-24 23:07:05 +02001527 size_t after_img_rsv;
1528 int baudrate;
Luka Perkovd131ad62012-05-27 11:44:51 +00001529
1530 rv = 1;
1531 tty = -1;
1532 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +02001533 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +00001534 imgpath = NULL;
1535 img = NULL;
1536 term = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001537 size = 0;
Pali Rohárca272042021-09-24 23:07:05 +02001538 after_img_rsv = KWBOOT_XM_BLKSZ;
1539 baudrate = 115200;
Luka Perkovd131ad62012-05-27 11:44:51 +00001540
1541 kwboot_verbose = isatty(STDOUT_FILENO);
1542
1543 do {
Kevin Smith7497a6a2016-02-16 21:28:19 +00001544 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +00001545 if (c < 0)
1546 break;
1547
1548 switch (c) {
1549 case 'b':
1550 bootmsg = kwboot_msg_boot;
1551 imgpath = optarg;
1552 break;
1553
Stefan Roese84899e22014-10-22 12:13:21 +02001554 case 'D':
1555 bootmsg = NULL;
1556 imgpath = optarg;
1557 break;
1558
1559 case 'd':
1560 debugmsg = kwboot_msg_debug;
1561 break;
1562
Luka Perkovd131ad62012-05-27 11:44:51 +00001563 case 'p':
Pali Rohárddc04fa2021-09-24 23:06:55 +02001564 /* nop, for backward compatibility */
Luka Perkovd131ad62012-05-27 11:44:51 +00001565 break;
1566
1567 case 't':
1568 term = 1;
1569 break;
1570
Stefan Roese84899e22014-10-22 12:13:21 +02001571 case 'a':
1572 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
1573 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1574 break;
1575
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001576 case 'q':
1577 msg_req_delay = atoi(optarg);
1578 break;
1579
1580 case 's':
1581 msg_rsp_timeo = atoi(optarg);
1582 break;
1583
Kevin Smith7497a6a2016-02-16 21:28:19 +00001584 case 'o':
1585 blk_rsp_timeo = atoi(optarg);
1586 break;
1587
Luka Perkovd131ad62012-05-27 11:44:51 +00001588 case 'B':
Pali Rohárca272042021-09-24 23:07:05 +02001589 baudrate = atoi(optarg);
Luka Perkovd131ad62012-05-27 11:44:51 +00001590 break;
1591
1592 case 'h':
1593 rv = 0;
1594 default:
1595 goto usage;
1596 }
1597 } while (1);
1598
Stefan Roese84899e22014-10-22 12:13:21 +02001599 if (!bootmsg && !term && !debugmsg)
Luka Perkovd131ad62012-05-27 11:44:51 +00001600 goto usage;
1601
Luka Perkovd131ad62012-05-27 11:44:51 +00001602 if (argc - optind < 1)
1603 goto usage;
1604
1605 ttypath = argv[optind++];
1606
Pali Rohárca272042021-09-24 23:07:05 +02001607 tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001608 if (tty < 0) {
1609 perror(ttypath);
1610 goto out;
1611 }
1612
Pali Rohárca272042021-09-24 23:07:05 +02001613 if (baudrate == 115200)
1614 /* do not change baudrate during Xmodem to the same value */
1615 baudrate = 0;
1616 else
1617 /* ensure we have enough space for baudrate change code */
1618 after_img_rsv += KWBOOT_BAUDRATE_BIN_HEADER_SZ +
1619 sizeof(kwboot_pre_baud_code) +
1620 sizeof(kwboot_baud_code);
1621
Luka Perkovd131ad62012-05-27 11:44:51 +00001622 if (imgpath) {
Pali Rohárca272042021-09-24 23:07:05 +02001623 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovd131ad62012-05-27 11:44:51 +00001624 if (!img) {
1625 perror(imgpath);
1626 goto out;
1627 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001628
Pali Rohárca272042021-09-24 23:07:05 +02001629 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001630 if (rc) {
1631 fprintf(stderr, "%s: Invalid image.\n", imgpath);
1632 goto out;
1633 }
1634 }
1635
Stefan Roese84899e22014-10-22 12:13:21 +02001636 if (debugmsg) {
1637 rc = kwboot_debugmsg(tty, debugmsg);
1638 if (rc) {
1639 perror("debugmsg");
1640 goto out;
1641 }
Willy Tarreau3475a712018-07-03 12:10:30 -04001642 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001643 rc = kwboot_bootmsg(tty, bootmsg);
1644 if (rc) {
1645 perror("bootmsg");
1646 goto out;
1647 }
1648 }
1649
1650 if (img) {
Pali Rohárca272042021-09-24 23:07:05 +02001651 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001652 if (rc) {
1653 perror("xmodem");
1654 goto out;
1655 }
1656 }
1657
1658 if (term) {
1659 rc = kwboot_terminal(tty);
1660 if (rc && !(errno == EINTR)) {
1661 perror("terminal");
1662 goto out;
1663 }
1664 }
1665
1666 rv = 0;
1667out:
1668 if (tty >= 0)
1669 close(tty);
1670
1671 if (img)
Pali Rohár04ced022021-09-24 23:07:03 +02001672 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001673
1674 return rv;
1675
1676usage:
1677 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
1678 goto out;
1679}