blob: 4dfb1038b4ffd078b8c6d5a569f6a462a7beae78 [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.
Marek Behúnb843aed2021-09-24 23:07:13 +02003 * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
4 * Armada 39x
Luka Perkovd131ad62012-05-27 11:44:51 +00005 *
6 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
Pali Rohárcf8c9322021-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 Perkovd131ad62012-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 Roesef4db6c92016-01-07 14:12:04 +010015#include "kwbimage.h"
16#include "mkimage.h"
Pali Rohára050a862021-09-24 23:06:42 +020017#include "version.h"
Stefan Roesef4db6c92016-01-07 14:12:04 +010018
Luka Perkovd131ad62012-05-27 11:44:51 +000019#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdarg.h>
Stefan Roesef4db6c92016-01-07 14:12:04 +010023#include <image.h>
Luka Perkovd131ad62012-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ún12df7b72021-09-24 23:06:52 +020029#include <time.h>
Luka Perkovd131ad62012-05-27 11:44:51 +000030#include <sys/stat.h>
31
Pali Rohár93b55632021-09-24 23:07:06 +020032#ifdef __linux__
33#include "termios_linux.h"
34#else
35#include <termios.h>
36#endif
37
Luka Perkovd131ad62012-05-27 11:44:51 +000038/*
39 * Marvell BootROM UART Sensing
40 */
41
42static unsigned char kwboot_msg_boot[] = {
43 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
44};
45
Stefan Roese84899e22014-10-22 12:13:21 +020046static unsigned char kwboot_msg_debug[] = {
47 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
48};
49
50/* Defines known to work on Kirkwood */
Luka Perkovd131ad62012-05-27 11:44:51 +000051#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
52
Stefan Roese84899e22014-10-22 12:13:21 +020053/* Defines known to work on Armada XP */
Stefan Roese84899e22014-10-22 12:13:21 +020054#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
55
Luka Perkovd131ad62012-05-27 11:44:51 +000056/*
57 * Xmodem Transfers
58 */
59
60#define SOH 1 /* sender start of block header */
61#define EOT 4 /* sender end of block transfer */
62#define ACK 6 /* target block ack */
63#define NAK 21 /* target block negative ack */
Luka Perkovd131ad62012-05-27 11:44:51 +000064
Pali Rohár2ef87f72021-09-24 23:06:48 +020065#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
66
Luka Perkovd131ad62012-05-27 11:44:51 +000067struct kwboot_block {
68 uint8_t soh;
69 uint8_t pnum;
70 uint8_t _pnum;
Pali Rohár2ef87f72021-09-24 23:06:48 +020071 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovd131ad62012-05-27 11:44:51 +000072 uint8_t csum;
Pali Rohára107c612021-07-23 11:14:14 +020073} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +000074
Pali Roháref951432022-01-25 18:13:00 +010075#define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */
Marek Behún12df7b72021-09-24 23:06:52 +020076#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovd131ad62012-05-27 11:44:51 +000077
Pali Rohár8dbe0272021-10-27 20:57:02 +020078/* ARM code to change baudrate */
Pali Rohárca272042021-09-24 23:07:05 +020079static unsigned char kwboot_baud_code[] = {
80 /* ; #define UART_BASE 0xd0012000 */
Pali Rohárca272042021-09-24 23:07:05 +020081 /* ; #define DLL 0x00 */
82 /* ; #define DLH 0x04 */
83 /* ; #define LCR 0x0c */
84 /* ; #define DLAB 0x80 */
85 /* ; #define LSR 0x14 */
Pali Rohárca272042021-09-24 23:07:05 +020086 /* ; #define TEMT 0x40 */
87 /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
88 /* ; */
89 /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
Pali Rohárca272042021-09-24 23:07:05 +020090 /* ; while */
91 /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
92 /* ; u32 lcr = readl(UART_BASE + LCR); */
93 /* ; writel(UART_BASE + LCR, lcr | DLAB); */
94 /* ; u8 old_dll = readl(UART_BASE + DLL); */
95 /* ; u8 old_dlh = readl(UART_BASE + DLH); */
96 /* ; u16 old_dl = old_dll | (old_dlh << 8); */
97 /* ; u32 clk = old_b * old_dl; */
98 /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
99 /* ; u8 new_dll = new_dl & 0xff; */
100 /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
101 /* ; writel(UART_BASE + DLL, new_dll); */
102 /* ; writel(UART_BASE + DLH, new_dlh); */
103 /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
Pali Rohár56452292021-10-27 20:57:00 +0200104 /* ; msleep(5); */
Pali Rohárca272042021-09-24 23:07:05 +0200105 /* ; return 0; */
106 /* ; } */
107
Pali Rohárca272042021-09-24 23:07:05 +0200108 /* ; r0 = UART_BASE */
Pali Rohár558176d2021-10-27 20:57:01 +0200109 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
110 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohárca272042021-09-24 23:07:05 +0200111
Pali Rohárca272042021-09-24 23:07:05 +0200112 /* ; Wait until Transmitter FIFO is Empty */
113 /* .Lloop_txempty: */
114 /* ; r1 = UART_BASE[LSR] & TEMT */
115 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
116 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
117 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
118
119 /* ; Set Divisor Latch Access Bit */
120 /* ; UART_BASE[LCR] |= DLAB */
121 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
122 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
123 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
124
125 /* ; Read current Divisor Latch */
126 /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
127 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
128 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
129 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
130 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
131 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
132 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
133 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
134
135 /* ; Read old baudrate value */
136 /* ; r2 = old_baudrate */
Pali Rohár62a98f42021-11-01 14:00:02 +0100137 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohárca272042021-09-24 23:07:05 +0200138
139 /* ; Calculate base clock */
140 /* ; r1 = r2 * r1 */
141 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
142
143 /* ; Read new baudrate value */
Pali Rohár56452292021-10-27 20:57:00 +0200144 /* ; r2 = new_baudrate */
Pali Rohár62a98f42021-11-01 14:00:02 +0100145 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohárca272042021-09-24 23:07:05 +0200146
147 /* ; Calculate new Divisor Latch */
148 /* ; r1 = DIV_ROUND(r1, r2) = */
149 /* ; = (r1 + r2/2) / r2 */
150 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
151 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
152 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
153 /* .Lloop_div1: */
154 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
155 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
156 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
157 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
158 /* .Lloop_div2: */
159 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
160 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
161 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
162 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
163 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
164 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
165 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
166
167 /* ; Set new Divisor Latch Low */
168 /* ; UART_BASE[DLL] = r1 & 0xff */
169 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
170 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
171 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
172
173 /* ; Set new Divisor Latch High */
174 /* ; UART_BASE[DLH] = r1>>8 & 0xff */
175 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
176 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
177 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
178
179 /* ; Clear Divisor Latch Access Bit */
180 /* ; UART_BASE[LCR] &= ~DLAB */
181 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
182 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
183 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
184
Pali Rohár56452292021-10-27 20:57:00 +0200185 /* ; Loop 0x2dc000 (2998272) cycles */
186 /* ; which is about 5ms on 1200 MHz CPU */
187 /* ; r1 = 0x2dc000 */
188 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */
Pali Rohárca272042021-09-24 23:07:05 +0200189 /* .Lloop_sleep: */
190 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
191 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
192 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
193
Pali Rohár62a98f42021-11-01 14:00:02 +0100194 /* ; Jump to the end of execution */
195 0x01, 0x00, 0x00, 0xea, /* b end */
Pali Rohárca272042021-09-24 23:07:05 +0200196
197 /* ; Placeholder for old baudrate value */
198 /* old_baudrate: */
199 0x00, 0x00, 0x00, 0x00, /* .word 0 */
200
201 /* ; Placeholder for new baudrate value */
202 /* new_baudrate: */
203 0x00, 0x00, 0x00, 0x00, /* .word 0 */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200204
205 /* end: */
Pali Rohárca272042021-09-24 23:07:05 +0200206};
207
Pali Rohár62a98f42021-11-01 14:00:02 +0100208/* ARM code from binary header executed by BootROM before changing baudrate */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200209static unsigned char kwboot_baud_code_binhdr_pre[] = {
Pali Rohár62a98f42021-11-01 14:00:02 +0100210 /* ; #define UART_BASE 0xd0012000 */
211 /* ; #define THR 0x00 */
212 /* ; #define LSR 0x14 */
213 /* ; #define THRE 0x20 */
214 /* ; */
215 /* ; void send_preamble(void) { */
216 /* ; const u8 *str = "$baudratechange"; */
217 /* ; u8 c; */
218 /* ; do { */
219 /* ; while */
220 /* ; ((readl(UART_BASE + LSR) & THRE)); */
221 /* ; c = *str++; */
222 /* ; writel(UART_BASE + THR, c); */
223 /* ; } while (c); */
224 /* ; } */
225
226 /* ; Preserve registers for BootROM */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200227 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
Pali Rohár62a98f42021-11-01 14:00:02 +0100228
229 /* ; r0 = UART_BASE */
230 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
231 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
232
233 /* ; r2 = address of preamble string */
234 0x00, 0x20, 0x8f, 0xe2, /* adr r2, .Lstr_preamble */
235
236 /* ; Skip preamble data section */
237 0x03, 0x00, 0x00, 0xea, /* b .Lloop_preamble */
238
239 /* ; Preamble string */
240 /* .Lstr_preamble: */
241 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
242 0x64, 0x72, 0x61, 0x74,
243 0x65, 0x63, 0x68, 0x61,
244 0x6e, 0x67, 0x65, 0x00,
245
246 /* ; Send preamble string over UART */
247 /* .Lloop_preamble: */
248 /* */
249 /* ; Wait until Transmitter Holding is Empty */
250 /* .Lloop_thre: */
251 /* ; r1 = UART_BASE[LSR] & THRE */
252 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
253 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
254 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
255
256 /* ; Put character into Transmitter FIFO */
257 /* ; r1 = *r2++ */
258 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
259 /* ; UART_BASE[THR] = r1 */
260 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
261
262 /* ; Loop until end of preamble string */
263 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
264 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200265};
266
Pali Rohár62a98f42021-11-01 14:00:02 +0100267/* ARM code for returning from binary header back to BootROM */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200268static unsigned char kwboot_baud_code_binhdr_post[] = {
269 /* ; Return 0 - no error */
270 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
271 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
272};
273
274/* ARM code for jumping to the original image exec_addr */
275static unsigned char kwboot_baud_code_data_jump[] = {
276 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */
277 /* ; Placeholder for exec_addr */
278 /* exec_addr: */
279 0x00, 0x00, 0x00, 0x00, /* .word 0 */
280};
Pali Rohárca272042021-09-24 23:07:05 +0200281
282static const char kwb_baud_magic[16] = "$baudratechange";
283
Luka Perkovd131ad62012-05-27 11:44:51 +0000284static int kwboot_verbose;
285
Stefan Roese84899e22014-10-22 12:13:21 +0200286static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +0000287static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +0200288
Marek Behúne453bb42021-09-24 23:06:41 +0200289static ssize_t
290kwboot_write(int fd, const char *buf, size_t len)
291{
Pali Rohár6ba7d632022-01-25 18:13:10 +0100292 ssize_t tot = 0;
Marek Behúne453bb42021-09-24 23:06:41 +0200293
294 while (tot < len) {
295 ssize_t wr = write(fd, buf + tot, len - tot);
296
Pali Rohár6ba7d632022-01-25 18:13:10 +0100297 if (wr < 0 && errno == EINTR)
298 continue;
299 else if (wr < 0)
300 return wr;
Marek Behúne453bb42021-09-24 23:06:41 +0200301
302 tot += wr;
303 }
304
305 return tot;
306}
307
Luka Perkovd131ad62012-05-27 11:44:51 +0000308static void
309kwboot_printv(const char *fmt, ...)
310{
311 va_list ap;
312
313 if (kwboot_verbose) {
314 va_start(ap, fmt);
315 vprintf(fmt, ap);
316 va_end(ap);
317 fflush(stdout);
318 }
319}
320
321static void
322__spinner(void)
323{
324 const char seq[] = { '-', '\\', '|', '/' };
325 const int div = 8;
326 static int state, bs;
327
328 if (state % div == 0) {
329 fputc(bs, stdout);
330 fputc(seq[state / div % sizeof(seq)], stdout);
331 fflush(stdout);
332 }
333
334 bs = '\b';
335 state++;
336}
337
338static void
339kwboot_spinner(void)
340{
341 if (kwboot_verbose)
342 __spinner();
343}
344
345static void
346__progress(int pct, char c)
347{
348 const int width = 70;
349 static const char *nl = "";
350 static int pos;
351
352 if (pos % width == 0)
353 printf("%s%3d %% [", nl, pct);
354
355 fputc(c, stdout);
356
357 nl = "]\n";
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200358 pos = (pos + 1) % width;
Luka Perkovd131ad62012-05-27 11:44:51 +0000359
360 if (pct == 100) {
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200361 while (pos && pos++ < width)
Luka Perkovd131ad62012-05-27 11:44:51 +0000362 fputc(' ', stdout);
363 fputs(nl, stdout);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200364 nl = "";
365 pos = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000366 }
367
368 fflush(stdout);
369
370}
371
372static void
373kwboot_progress(int _pct, char c)
374{
375 static int pct;
376
377 if (_pct != -1)
378 pct = _pct;
379
380 if (kwboot_verbose)
381 __progress(pct, c);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200382
383 if (pct == 100)
384 pct = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000385}
386
387static int
388kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
389{
390 int rc, nfds;
391 fd_set rfds;
392 struct timeval tv;
393 ssize_t n;
394
395 rc = -1;
396
397 FD_ZERO(&rfds);
398 FD_SET(fd, &rfds);
399
400 tv.tv_sec = 0;
401 tv.tv_usec = timeo * 1000;
402 if (tv.tv_usec > 1000000) {
403 tv.tv_sec += tv.tv_usec / 1000000;
404 tv.tv_usec %= 1000000;
405 }
406
407 do {
408 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
Pali Rohár91fb0952022-01-25 18:13:11 +0100409 if (nfds < 0 && errno == EINTR)
410 continue;
411 else if (nfds < 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000412 goto out;
Pali Rohár91fb0952022-01-25 18:13:11 +0100413 else if (!nfds) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000414 errno = ETIMEDOUT;
415 goto out;
416 }
417
418 n = read(fd, buf, len);
Pali Rohár91fb0952022-01-25 18:13:11 +0100419 if (n < 0 && errno == EINTR)
420 continue;
421 else if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000422 goto out;
423
424 buf = (char *)buf + n;
425 len -= n;
426 } while (len > 0);
427
428 rc = 0;
429out:
430 return rc;
431}
432
433static int
Pali Rohárcab817d2021-10-27 20:56:59 +0200434kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovd131ad62012-05-27 11:44:51 +0000435{
Stefan Roese84899e22014-10-22 12:13:21 +0200436 if (!buf)
437 return 0;
438
Marek Behúne453bb42021-09-24 23:06:41 +0200439 if (kwboot_write(fd, buf, len) < 0)
440 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000441
Pali Rohárcab817d2021-10-27 20:56:59 +0200442 if (nodrain)
443 return 0;
444
Marek Behúne453bb42021-09-24 23:06:41 +0200445 return tcdrain(fd);
Luka Perkovd131ad62012-05-27 11:44:51 +0000446}
447
448static int
449kwboot_tty_send_char(int fd, unsigned char c)
450{
Pali Rohárcab817d2021-10-27 20:56:59 +0200451 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovd131ad62012-05-27 11:44:51 +0000452}
453
454static speed_t
Pali Rohárca272042021-09-24 23:07:05 +0200455kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000456{
457 switch (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +0200458#ifdef B4000000
459 case 4000000:
460 return B4000000;
461#endif
462#ifdef B3500000
463 case 3500000:
464 return B3500000;
465#endif
466#ifdef B3000000
467 case 3000000:
468 return B3000000;
469#endif
470#ifdef B2500000
471 case 2500000:
472 return B2500000;
473#endif
474#ifdef B2000000
475 case 2000000:
476 return B2000000;
477#endif
478#ifdef B1500000
479 case 1500000:
480 return B1500000;
481#endif
482#ifdef B1152000
483 case 1152000:
484 return B1152000;
485#endif
486#ifdef B1000000
487 case 1000000:
488 return B1000000;
489#endif
490#ifdef B921600
491 case 921600:
492 return B921600;
493#endif
494#ifdef B614400
495 case 614400:
496 return B614400;
497#endif
498#ifdef B576000
499 case 576000:
500 return B576000;
501#endif
502#ifdef B500000
503 case 500000:
504 return B500000;
505#endif
506#ifdef B460800
507 case 460800:
508 return B460800;
509#endif
510#ifdef B307200
511 case 307200:
512 return B307200;
513#endif
514#ifdef B230400
515 case 230400:
516 return B230400;
517#endif
518#ifdef B153600
519 case 153600:
520 return B153600;
521#endif
522#ifdef B115200
Luka Perkovd131ad62012-05-27 11:44:51 +0000523 case 115200:
524 return B115200;
Pali Rohárca272042021-09-24 23:07:05 +0200525#endif
526#ifdef B76800
527 case 76800:
528 return B76800;
529#endif
530#ifdef B57600
Luka Perkovd131ad62012-05-27 11:44:51 +0000531 case 57600:
532 return B57600;
Pali Rohárca272042021-09-24 23:07:05 +0200533#endif
534#ifdef B38400
Luka Perkovd131ad62012-05-27 11:44:51 +0000535 case 38400:
536 return B38400;
Pali Rohárca272042021-09-24 23:07:05 +0200537#endif
538#ifdef B19200
Luka Perkovd131ad62012-05-27 11:44:51 +0000539 case 19200:
540 return B19200;
Pali Rohárca272042021-09-24 23:07:05 +0200541#endif
542#ifdef B9600
Luka Perkovd131ad62012-05-27 11:44:51 +0000543 case 9600:
544 return B9600;
Pali Rohárca272042021-09-24 23:07:05 +0200545#endif
546#ifdef B4800
547 case 4800:
548 return B4800;
549#endif
550#ifdef B2400
551 case 2400:
552 return B2400;
553#endif
554#ifdef B1800
555 case 1800:
556 return B1800;
557#endif
558#ifdef B1200
559 case 1200:
560 return B1200;
561#endif
562#ifdef B600
563 case 600:
564 return B600;
565#endif
566#ifdef B300
567 case 300:
568 return B300;
569#endif
570#ifdef B200
571 case 200:
572 return B200;
573#endif
574#ifdef B150
575 case 150:
576 return B150;
577#endif
578#ifdef B134
579 case 134:
580 return B134;
581#endif
582#ifdef B110
583 case 110:
584 return B110;
585#endif
586#ifdef B75
587 case 75:
588 return B75;
589#endif
590#ifdef B50
591 case 50:
592 return B50;
593#endif
594 default:
Pali Rohár93b55632021-09-24 23:07:06 +0200595#ifdef BOTHER
596 return BOTHER;
597#else
Pali Rohárca272042021-09-24 23:07:05 +0200598 return B0;
Pali Rohár93b55632021-09-24 23:07:06 +0200599#endif
Luka Perkovd131ad62012-05-27 11:44:51 +0000600 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000601}
602
603static int
Marek Behún99a3d0232021-09-24 23:07:07 +0200604_is_within_tolerance(int value, int reference, int tolerance)
605{
606 return 100 * value >= reference * (100 - tolerance) &&
607 100 * value <= reference * (100 + tolerance);
608}
609
610static int
Pali Rohárca272042021-09-24 23:07:05 +0200611kwboot_tty_change_baudrate(int fd, int baudrate)
612{
613 struct termios tio;
614 speed_t speed;
615 int rc;
616
617 rc = tcgetattr(fd, &tio);
618 if (rc)
619 return rc;
620
621 speed = kwboot_tty_baudrate_to_speed(baudrate);
622 if (speed == B0) {
623 errno = EINVAL;
624 return -1;
625 }
626
Pali Rohár93b55632021-09-24 23:07:06 +0200627#ifdef BOTHER
628 if (speed == BOTHER)
629 tio.c_ospeed = tio.c_ispeed = baudrate;
630#endif
631
Pali Rohárca272042021-09-24 23:07:05 +0200632 rc = cfsetospeed(&tio, speed);
633 if (rc)
634 return rc;
635
636 rc = cfsetispeed(&tio, speed);
637 if (rc)
638 return rc;
639
640 rc = tcsetattr(fd, TCSANOW, &tio);
641 if (rc)
642 return rc;
643
Marek Behún99a3d0232021-09-24 23:07:07 +0200644 rc = tcgetattr(fd, &tio);
645 if (rc)
646 return rc;
647
648 if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed)
649 goto baud_fail;
650
651#ifdef BOTHER
652 /*
653 * Check whether set baudrate is within 3% tolerance.
654 * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed
655 * with real values.
656 */
657 if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3))
658 goto baud_fail;
659
660 if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3))
661 goto baud_fail;
662#endif
663
Pali Rohárca272042021-09-24 23:07:05 +0200664 return 0;
Marek Behún99a3d0232021-09-24 23:07:07 +0200665
666baud_fail:
667 fprintf(stderr, "Could not set baudrate to requested value\n");
668 errno = EINVAL;
669 return -1;
Pali Rohárca272042021-09-24 23:07:05 +0200670}
671
672static int
673kwboot_open_tty(const char *path, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000674{
Pali Rohár911515b2021-09-24 23:07:10 +0200675 int rc, fd, flags;
Luka Perkovd131ad62012-05-27 11:44:51 +0000676 struct termios tio;
677
678 rc = -1;
679
Marek Behún5fa04f42021-09-24 23:07:11 +0200680 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovd131ad62012-05-27 11:44:51 +0000681 if (fd < 0)
682 goto out;
683
Pali Rohárc704e0e2021-09-24 23:07:08 +0200684 rc = tcgetattr(fd, &tio);
685 if (rc)
686 goto out;
Luka Perkovd131ad62012-05-27 11:44:51 +0000687
Pali Rohárc704e0e2021-09-24 23:07:08 +0200688 cfmakeraw(&tio);
Marek Behún5fa04f42021-09-24 23:07:11 +0200689 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár2ecca3d2021-10-25 15:12:53 +0200690 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovd131ad62012-05-27 11:44:51 +0000691 tio.c_cc[VMIN] = 1;
Pali Rohár24a471b2021-09-24 23:07:09 +0200692 tio.c_cc[VTIME] = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000693
Luka Perkovd131ad62012-05-27 11:44:51 +0000694 rc = tcsetattr(fd, TCSANOW, &tio);
695 if (rc)
696 goto out;
697
Pali Rohár911515b2021-09-24 23:07:10 +0200698 flags = fcntl(fd, F_GETFL);
699 if (flags < 0)
700 goto out;
701
702 rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY);
703 if (rc)
704 goto out;
705
Pali Rohárca272042021-09-24 23:07:05 +0200706 rc = kwboot_tty_change_baudrate(fd, baudrate);
707 if (rc)
708 goto out;
709
Luka Perkovd131ad62012-05-27 11:44:51 +0000710 rc = fd;
711out:
712 if (rc < 0) {
713 if (fd >= 0)
714 close(fd);
715 }
716
717 return rc;
718}
719
720static int
721kwboot_bootmsg(int tty, void *msg)
722{
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100723 struct kwboot_block block;
Luka Perkovd131ad62012-05-27 11:44:51 +0000724 int rc;
725 char c;
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300726 int count;
Luka Perkovd131ad62012-05-27 11:44:51 +0000727
Stefan Roese84899e22014-10-22 12:13:21 +0200728 if (msg == NULL)
729 kwboot_printv("Please reboot the target into UART boot mode...");
730 else
731 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000732
733 do {
734 rc = tcflush(tty, TCIOFLUSH);
735 if (rc)
736 break;
737
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300738 for (count = 0; count < 128; count++) {
Pali Rohárcab817d2021-10-27 20:56:59 +0200739 rc = kwboot_tty_send(tty, msg, 8, 0);
Pali Rohárd8865f82022-03-02 11:49:18 +0100740 if (rc)
741 break;
Luka Perkovd131ad62012-05-27 11:44:51 +0000742 }
743
Stefan Roese84899e22014-10-22 12:13:21 +0200744 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000745
746 kwboot_spinner();
747
748 } while (rc || c != NAK);
749
750 kwboot_printv("\n");
751
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100752 if (rc)
753 return rc;
754
755 /*
756 * At this stage we have sent more boot message patterns and BootROM
757 * (at least on Armada XP and 385) started interpreting sent bytes as
758 * part of xmodem packets. If BootROM is expecting SOH byte as start of
759 * a xmodem packet and it receives byte 0xff, then it throws it away and
760 * sends a NAK reply to host. If BootROM does not receive any byte for
761 * 2s when expecting some continuation of the xmodem packet, it throws
762 * away the partially received xmodem data and sends NAK reply to host.
763 *
764 * Therefore for starting xmodem transfer we have two options: Either
765 * wait 2s or send 132 0xff bytes (which is the size of xmodem packet)
766 * to ensure that BootROM throws away any partially received data.
767 */
768
769 /* flush output queue with remaining boot message patterns */
Pali Rohárd8865f82022-03-02 11:49:18 +0100770 rc = tcflush(tty, TCOFLUSH);
771 if (rc) {
772 perror("Failed to flush output queue");
773 return rc;
774 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100775
776 /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */
777 memset(&block, 0xff, sizeof(block));
Pali Rohárd8865f82022-03-02 11:49:18 +0100778 rc = kwboot_tty_send(tty, &block, sizeof(block), 0);
779 if (rc) {
780 perror("Failed to send sync sequence");
781 return rc;
782 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100783
784 /*
785 * Sending 132 bytes via 115200B/8-N-1 takes 11.45 ms, reading 132 bytes
786 * takes 11.45 ms, so waiting for 30 ms should be enough.
787 */
788 usleep(30 * 1000);
789
790 /* flush remaining NAK replies from input queue */
Pali Rohárd8865f82022-03-02 11:49:18 +0100791 rc = tcflush(tty, TCIFLUSH);
792 if (rc) {
793 perror("Failed to flush input queue");
794 return rc;
795 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100796
797 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000798}
799
800static int
Stefan Roese84899e22014-10-22 12:13:21 +0200801kwboot_debugmsg(int tty, void *msg)
802{
803 int rc;
804
805 kwboot_printv("Sending debug message. Please reboot the target...");
806
807 do {
808 char buf[16];
809
810 rc = tcflush(tty, TCIOFLUSH);
811 if (rc)
812 break;
813
Pali Rohárcab817d2021-10-27 20:56:59 +0200814 rc = kwboot_tty_send(tty, msg, 8, 0);
Pali Rohárd8865f82022-03-02 11:49:18 +0100815 if (rc)
816 break;
Stefan Roese84899e22014-10-22 12:13:21 +0200817
818 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
819
820 kwboot_spinner();
821
822 } while (rc);
823
824 kwboot_printv("\n");
825
826 return rc;
827}
828
Pali Rohárc5d666a2021-09-24 23:06:44 +0200829static size_t
Luka Perkovd131ad62012-05-27 11:44:51 +0000830kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
831 size_t size, int pnum)
832{
Marek Behúnd8cc8512021-09-24 23:06:45 +0200833 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +0000834
Stefan Roese84899e22014-10-22 12:13:21 +0200835 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000836 block->pnum = pnum;
837 block->_pnum = ~block->pnum;
838
Pali Rohár2ef87f72021-09-24 23:06:48 +0200839 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovd131ad62012-05-27 11:44:51 +0000840 memcpy(&block->data[0], data, n);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200841 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovd131ad62012-05-27 11:44:51 +0000842
843 block->csum = 0;
844 for (i = 0; i < n; i++)
845 block->csum += block->data[i];
846
847 return n;
848}
849
Marek Behún12df7b72021-09-24 23:06:52 +0200850static uint64_t
851_now(void)
852{
853 struct timespec ts;
854
855 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
856 static int err_print;
857
858 if (!err_print) {
859 perror("clock_gettime() does not work");
860 err_print = 1;
861 }
862
863 /* this will just make the timeout not work */
864 return -1ULL;
865 }
866
867 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
868}
869
Luka Perkovd131ad62012-05-27 11:44:51 +0000870static int
Marek Behún408ea612021-09-24 23:06:49 +0200871_is_xm_reply(char c)
872{
Pali Rohár94c906a2022-01-25 18:13:03 +0100873 return c == ACK || c == NAK;
Marek Behún408ea612021-09-24 23:06:49 +0200874}
875
876static int
Pali Rohár9cdc2642021-09-24 23:06:54 +0200877_xm_reply_to_error(int c)
878{
879 int rc = -1;
880
881 switch (c) {
882 case ACK:
883 rc = 0;
884 break;
885 case NAK:
886 errno = EBADMSG;
887 break;
Pali Rohár9cdc2642021-09-24 23:06:54 +0200888 default:
889 errno = EPROTO;
890 break;
891 }
892
893 return rc;
894}
895
896static int
Pali Rohárca272042021-09-24 23:07:05 +0200897kwboot_baud_magic_handle(int fd, char c, int baudrate)
898{
899 static size_t rcv_len;
900
901 if (rcv_len < sizeof(kwb_baud_magic)) {
902 /* try to recognize whole magic word */
903 if (c == kwb_baud_magic[rcv_len]) {
904 rcv_len++;
905 } else {
906 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
907 fflush(stdout);
908 rcv_len = 0;
909 }
910 }
911
912 if (rcv_len == sizeof(kwb_baud_magic)) {
913 /* magic word received */
914 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
915
916 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
917 } else {
918 return 0;
919 }
920}
921
922static int
Pali Rohár950ed242022-01-25 18:13:04 +0100923kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Rohár82a9e132022-01-25 18:13:02 +0100924 int ignore_nak_reply,
Pali Rohára6fcac22021-10-25 15:13:04 +0200925 int allow_non_xm, int *non_xm_print,
Pali Rohárca272042021-09-24 23:07:05 +0200926 int baudrate, int *baud_changed)
Pali Rohár48b3ea62021-09-24 23:06:50 +0200927{
Marek Behún12df7b72021-09-24 23:06:52 +0200928 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún819cd322021-09-24 23:06:53 +0200929 uint64_t recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200930 int rc;
931
932 while (1) {
Marek Behún12df7b72021-09-24 23:06:52 +0200933 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200934 if (rc) {
935 if (errno != ETIMEDOUT)
936 return rc;
Marek Behún819cd322021-09-24 23:06:53 +0200937 else if (allow_non_xm && *non_xm_print)
Marek Behún12df7b72021-09-24 23:06:52 +0200938 return -1;
939 else
940 *c = NAK;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200941 }
942
943 /* If received xmodem reply, end. */
Pali Rohár82a9e132022-01-25 18:13:02 +0100944 if (_is_xm_reply(*c)) {
945 if (*c == NAK && ignore_nak_reply) {
946 timeout = recv_until - _now();
947 if (timeout >= 0)
948 continue;
949 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200950 break;
Pali Rohár82a9e132022-01-25 18:13:02 +0100951 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200952
953 /*
Pali Rohárca272042021-09-24 23:07:05 +0200954 * If receiving/printing non-xmodem text output is allowed and
955 * such a byte was received, we want to increase receiving time
956 * and either:
957 * - print the byte, if it is not part of baudrate change magic
958 * sequence while baudrate change was requested (-B option)
959 * - change baudrate
Marek Behún819cd322021-09-24 23:06:53 +0200960 * Otherwise decrease timeout by time elapsed.
Pali Rohár48b3ea62021-09-24 23:06:50 +0200961 */
962 if (allow_non_xm) {
Marek Behún12df7b72021-09-24 23:06:52 +0200963 recv_until = _now() + timeout;
Pali Rohárca272042021-09-24 23:07:05 +0200964
965 if (baudrate && !*baud_changed) {
966 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
967 if (rc == 1)
968 *baud_changed = 1;
969 else if (!rc)
970 *non_xm_print = 1;
971 else
972 return rc;
973 } else if (!baudrate || !*baud_changed) {
974 putchar(*c);
975 fflush(stdout);
976 *non_xm_print = 1;
977 }
Marek Behún819cd322021-09-24 23:06:53 +0200978 } else {
Pali Rohár950ed242022-01-25 18:13:04 +0100979 if (stop_on_non_xm)
Pali Rohára6fcac22021-10-25 15:13:04 +0200980 break;
Marek Behún819cd322021-09-24 23:06:53 +0200981 timeout = recv_until - _now();
982 if (timeout < 0) {
983 errno = ETIMEDOUT;
984 return -1;
985 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200986 }
987 }
988
989 return 0;
990}
991
992static int
993kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár5875ad42022-01-25 18:13:05 +0100994 int *done_print, int baudrate, int allow_retries)
Luka Perkovd131ad62012-05-27 11:44:51 +0000995{
Pali Rohárca272042021-09-24 23:07:05 +0200996 int non_xm_print, baud_changed;
997 int rc, err, retries;
Luka Perkovd131ad62012-05-27 11:44:51 +0000998 char c;
999
Pali Rohár48b3ea62021-09-24 23:06:50 +02001000 *done_print = 0;
Pali Rohár455c0d22021-10-27 20:56:58 +02001001 non_xm_print = 0;
1002 baud_changed = 0;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001003
Pali Rohárd14a3422021-10-25 15:13:03 +02001004 retries = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001005 do {
Pali Rohárcab817d2021-10-27 20:56:59 +02001006 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovd131ad62012-05-27 11:44:51 +00001007 if (rc)
Pali Rohár94c906a2022-01-25 18:13:03 +01001008 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001009
Pali Rohár48b3ea62021-09-24 23:06:50 +02001010 if (allow_non_xm && !*done_print) {
1011 kwboot_progress(100, '.');
1012 kwboot_printv("Done\n");
1013 *done_print = 1;
1014 }
Stefan Roese84899e22014-10-22 12:13:21 +02001015
Pali Rohára6fcac22021-10-25 15:13:04 +02001016 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Rohár82a9e132022-01-25 18:13:02 +01001017 retries > 8,
Pali Rohára6fcac22021-10-25 15:13:04 +02001018 allow_non_xm, &non_xm_print,
Pali Rohárca272042021-09-24 23:07:05 +02001019 baudrate, &baud_changed);
Pali Rohár48b3ea62021-09-24 23:06:50 +02001020 if (rc)
Pali Rohár94c906a2022-01-25 18:13:03 +01001021 goto err;
Stefan Roese84899e22014-10-22 12:13:21 +02001022
Pali Rohár5d8aa4c2022-01-25 18:13:06 +01001023 if (!allow_non_xm && c != ACK) {
1024 if (c == NAK && allow_retries && retries + 1 < 16)
1025 kwboot_progress(-1, '+');
1026 else
1027 kwboot_progress(-1, 'E');
1028 }
Pali Rohár5875ad42022-01-25 18:13:05 +01001029 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovd131ad62012-05-27 11:44:51 +00001030
Marek Behún2e81b3a2021-09-24 23:06:51 +02001031 if (non_xm_print)
1032 kwboot_printv("\n");
1033
Pali Rohárca272042021-09-24 23:07:05 +02001034 if (allow_non_xm && baudrate && !baud_changed) {
1035 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohárca272042021-09-24 23:07:05 +02001036 errno = EPROTO;
Pali Rohár94c906a2022-01-25 18:13:03 +01001037 return -1;
Pali Rohárca272042021-09-24 23:07:05 +02001038 }
1039
Pali Rohár9cdc2642021-09-24 23:06:54 +02001040 return _xm_reply_to_error(c);
Pali Rohár94c906a2022-01-25 18:13:03 +01001041err:
Pali Rohárca272042021-09-24 23:07:05 +02001042 err = errno;
Pali Rohárca272042021-09-24 23:07:05 +02001043 kwboot_printv("\n");
1044 errno = err;
1045 return rc;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001046}
Luka Perkovd131ad62012-05-27 11:44:51 +00001047
Pali Rohár9cdc2642021-09-24 23:06:54 +02001048static int
1049kwboot_xm_finish(int fd)
1050{
1051 int rc, retries;
1052 char c;
Luka Perkovd131ad62012-05-27 11:44:51 +00001053
Pali Rohár9cdc2642021-09-24 23:06:54 +02001054 kwboot_printv("Finishing transfer\n");
1055
Pali Rohárd14a3422021-10-25 15:13:03 +02001056 retries = 0;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001057 do {
1058 rc = kwboot_tty_send_char(fd, EOT);
1059 if (rc)
1060 return rc;
1061
Pali Rohára6fcac22021-10-25 15:13:04 +02001062 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Rohár82a9e132022-01-25 18:13:02 +01001063 retries > 8,
Pali Rohára6fcac22021-10-25 15:13:04 +02001064 0, NULL, 0, NULL);
Pali Rohár9cdc2642021-09-24 23:06:54 +02001065 if (rc)
1066 return rc;
Pali Rohárd14a3422021-10-25 15:13:03 +02001067 } while (c == NAK && retries++ < 16);
Pali Rohár9cdc2642021-09-24 23:06:54 +02001068
1069 return _xm_reply_to_error(c);
Luka Perkovd131ad62012-05-27 11:44:51 +00001070}
1071
1072static int
Pali Rohár2ef87f72021-09-24 23:06:48 +02001073kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohárca272042021-09-24 23:07:05 +02001074 size_t size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001075{
Pali Rohár48b3ea62021-09-24 23:06:50 +02001076 int done_print = 0;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001077 size_t sent, left;
1078 int rc;
Luka Perkovd131ad62012-05-27 11:44:51 +00001079
Pali Rohár2ef87f72021-09-24 23:06:48 +02001080 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1081 header ? "header" : "data", size);
Luka Perkovd131ad62012-05-27 11:44:51 +00001082
Pali Rohár2ef87f72021-09-24 23:06:48 +02001083 left = size;
1084 sent = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001085
Pali Rohár2ef87f72021-09-24 23:06:48 +02001086 while (sent < size) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001087 struct kwboot_block block;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001088 int last_block;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001089 size_t blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001090
Pali Rohár2ef87f72021-09-24 23:06:48 +02001091 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1092 data += blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001093
Pali Rohár48b3ea62021-09-24 23:06:50 +02001094 last_block = (left <= blksz);
1095
Pali Rohár5875ad42022-01-25 18:13:05 +01001096 /*
1097 * Handling of repeated xmodem packets is completely broken in
1098 * Armada 385 BootROM - it completely ignores xmodem packet
1099 * numbers, they are only used for checksum verification.
1100 * BootROM can handle a retry of the xmodem packet only during
1101 * the transmission of kwbimage header and only if BootROM
1102 * itself sent NAK response to previous attempt (it does it on
1103 * checksum failure). During the transmission of kwbimage data
1104 * part, BootROM always expects next xmodem packet, even if it
1105 * sent NAK to previous attempt - there is absolutely no way to
1106 * repair incorrectly transmitted xmodem packet during kwbimage
1107 * data part upload. Also, if kwboot receives non-ACK/NAK
1108 * response (meaning that original BootROM response was damaged
1109 * on UART) there is no way to detect if BootROM accepted xmodem
1110 * packet or not and no way to check if kwboot could repeat the
1111 * packet or not.
1112 *
1113 * Stop transfer and return failure if kwboot receives unknown
1114 * reply if non-xmodem reply is not allowed (for all xmodem
1115 * packets except the last header packet) or when non-ACK reply
1116 * is received during data part transfer.
1117 */
Pali Rohár48b3ea62021-09-24 23:06:50 +02001118 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár5875ad42022-01-25 18:13:05 +01001119 &done_print, baudrate, header);
Luka Perkovd131ad62012-05-27 11:44:51 +00001120 if (rc)
1121 goto out;
1122
Pali Rohár2ef87f72021-09-24 23:06:48 +02001123 sent += blksz;
1124 left -= blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001125
Pali Rohár48b3ea62021-09-24 23:06:50 +02001126 if (!done_print)
1127 kwboot_progress(sent * 100 / size, '.');
Pali Rohár2ef87f72021-09-24 23:06:48 +02001128 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001129
Pali Rohár48b3ea62021-09-24 23:06:50 +02001130 if (!done_print)
1131 kwboot_printv("Done\n");
Pali Rohár2ef87f72021-09-24 23:06:48 +02001132
1133 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001134out:
Pali Rohárd5ba8db2021-09-24 23:06:47 +02001135 kwboot_printv("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001136 return rc;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001137}
Luka Perkovd131ad62012-05-27 11:44:51 +00001138
Pali Rohár2ef87f72021-09-24 23:06:48 +02001139static int
Pali Rohárca272042021-09-24 23:07:05 +02001140kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohár2ef87f72021-09-24 23:06:48 +02001141{
1142 const uint8_t *img = _img;
1143 int rc, pnum;
1144 size_t hdrsz;
1145
Marek Behúnfe2fd732021-09-24 23:07:01 +02001146 hdrsz = kwbheader_size(img);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001147
Pali Rohárf8017c32021-11-05 23:29:58 +01001148 /*
1149 * If header size is not aligned to xmodem block size (which applies
1150 * for all images in kwbimage v0 format) then we have to ensure that
1151 * the last xmodem block of header contains beginning of the data
1152 * followed by the header. So align header size to xmodem block size.
1153 */
1154 hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
1155
Pali Rohár2ef87f72021-09-24 23:06:48 +02001156 pnum = 1;
1157
Pali Rohárca272042021-09-24 23:07:05 +02001158 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001159 if (rc)
1160 return rc;
1161
Pali Rohárf8017c32021-11-05 23:29:58 +01001162 /*
1163 * If we have already sent image data as a part of the last
1164 * xmodem header block then we have nothing more to send.
1165 */
1166 if (hdrsz < size) {
1167 img += hdrsz;
1168 size -= hdrsz;
1169 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
1170 if (rc)
1171 return rc;
1172 }
Pali Rohár2ef87f72021-09-24 23:06:48 +02001173
Pali Rohárca272042021-09-24 23:07:05 +02001174 rc = kwboot_xm_finish(tty);
1175 if (rc)
1176 return rc;
1177
1178 if (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +02001179 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1180 rc = kwboot_tty_change_baudrate(tty, 115200);
1181 if (rc)
1182 return rc;
1183 }
1184
1185 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001186}
1187
1188static int
Marek Behún46237e62021-09-24 23:06:40 +02001189kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +00001190{
Pali Rohárde751402022-02-03 17:45:20 +01001191 char buf[128];
Marek Behúne453bb42021-09-24 23:06:41 +02001192 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +00001193
Pali Rohárde751402022-02-03 17:45:20 +01001194 nin = read(in, buf, sizeof(buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -04001195 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +00001196 return -1;
1197
1198 if (quit) {
1199 int i;
1200
1201 for (i = 0; i < nin; i++) {
Pali Rohárde751402022-02-03 17:45:20 +01001202 if (buf[i] == quit[*s]) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001203 (*s)++;
Pali Rohárde751402022-02-03 17:45:20 +01001204 if (!quit[*s]) {
Pali Rohár7938b3b2022-02-18 12:24:13 +01001205 nin = (i > *s) ? (i - *s) : 0;
Pali Rohárde751402022-02-03 17:45:20 +01001206 break;
1207 }
Pali Rohárb943eee2021-07-23 11:14:20 +02001208 } else {
Pali Rohárde751402022-02-03 17:45:20 +01001209 if (*s > i && kwboot_write(out, quit, *s - i) < 0)
Marek Behúne453bb42021-09-24 23:06:41 +02001210 return -1;
1211 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +02001212 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001213 }
Pali Rohárde751402022-02-03 17:45:20 +01001214
1215 if (i == nin)
Pali Rohár7938b3b2022-02-18 12:24:13 +01001216 nin -= (nin > *s) ? *s : nin;
Luka Perkovd131ad62012-05-27 11:44:51 +00001217 }
1218
Marek Behúne453bb42021-09-24 23:06:41 +02001219 if (kwboot_write(out, buf, nin) < 0)
1220 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001221
1222 return 0;
1223}
1224
1225static int
1226kwboot_terminal(int tty)
1227{
1228 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +02001229 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +00001230 struct termios otio, tio;
1231
1232 rc = -1;
1233
1234 in = STDIN_FILENO;
1235 if (isatty(in)) {
1236 rc = tcgetattr(in, &otio);
1237 if (!rc) {
1238 tio = otio;
1239 cfmakeraw(&tio);
1240 rc = tcsetattr(in, TCSANOW, &tio);
1241 }
1242 if (rc) {
1243 perror("tcsetattr");
1244 goto out;
1245 }
1246
1247 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún5fa04f42021-09-24 23:07:11 +02001248 quit[0] | 0100, quit[1]);
Luka Perkovd131ad62012-05-27 11:44:51 +00001249 } else
1250 in = -1;
1251
1252 rc = 0;
1253 s = 0;
1254
1255 do {
1256 fd_set rfds;
1257 int nfds = 0;
1258
Pali Rohár0a143412021-10-25 15:12:52 +02001259 FD_ZERO(&rfds);
Luka Perkovd131ad62012-05-27 11:44:51 +00001260 FD_SET(tty, &rfds);
1261 nfds = nfds < tty ? tty : nfds;
1262
1263 if (in >= 0) {
1264 FD_SET(in, &rfds);
1265 nfds = nfds < in ? in : nfds;
1266 }
1267
1268 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1269 if (nfds < 0)
1270 break;
1271
1272 if (FD_ISSET(tty, &rfds)) {
1273 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
1274 if (rc)
1275 break;
1276 }
1277
Marek Behúnf30cb0d2021-09-24 23:06:39 +02001278 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001279 rc = kwboot_term_pipe(in, tty, quit, &s);
1280 if (rc)
1281 break;
1282 }
1283 } while (quit[s] != 0);
1284
Pali Rohárec0fe5b2021-07-23 11:14:18 +02001285 if (in >= 0)
1286 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +02001287 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001288out:
1289 return rc;
1290}
1291
1292static void *
Pali Rohár04ced022021-09-24 23:07:03 +02001293kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovd131ad62012-05-27 11:44:51 +00001294{
Pali Rohárddc04fa2021-09-24 23:06:55 +02001295 int rc, fd;
Luka Perkovd131ad62012-05-27 11:44:51 +00001296 struct stat st;
1297 void *img;
Pali Rohár04ced022021-09-24 23:07:03 +02001298 off_t tot;
Luka Perkovd131ad62012-05-27 11:44:51 +00001299
1300 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001301 img = NULL;
1302
1303 fd = open(path, O_RDONLY);
1304 if (fd < 0)
1305 goto out;
1306
1307 rc = fstat(fd, &st);
1308 if (rc)
1309 goto out;
1310
Pali Rohár04ced022021-09-24 23:07:03 +02001311 img = malloc(st.st_size + reserve);
1312 if (!img)
Luka Perkovd131ad62012-05-27 11:44:51 +00001313 goto out;
Pali Rohár04ced022021-09-24 23:07:03 +02001314
1315 tot = 0;
1316 while (tot < st.st_size) {
1317 ssize_t rd = read(fd, img + tot, st.st_size - tot);
1318
1319 if (rd < 0)
1320 goto out;
1321
1322 tot += rd;
1323
1324 if (!rd && tot < st.st_size) {
1325 errno = EIO;
1326 goto out;
1327 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001328 }
1329
1330 rc = 0;
1331 *size = st.st_size;
1332out:
1333 if (rc && img) {
Pali Rohár04ced022021-09-24 23:07:03 +02001334 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001335 img = NULL;
1336 }
1337 if (fd >= 0)
1338 close(fd);
1339
1340 return img;
1341}
1342
1343static uint8_t
Marek Behúnfe2fd732021-09-24 23:07:01 +02001344kwboot_hdr_csum8(const void *hdr)
Luka Perkovd131ad62012-05-27 11:44:51 +00001345{
Marek Behúnfe2fd732021-09-24 23:07:01 +02001346 const uint8_t *data = hdr;
1347 uint8_t csum;
1348 size_t size;
1349
1350 size = kwbheader_size_for_csum(hdr);
Luka Perkovd131ad62012-05-27 11:44:51 +00001351
1352 for (csum = 0; size-- > 0; data++)
1353 csum += *data;
1354
1355 return csum;
1356}
1357
Pali Rohárad9a3ac2021-10-25 15:12:55 +02001358static uint32_t *
1359kwboot_img_csum32_ptr(void *img)
1360{
1361 struct main_hdr_v1 *hdr = img;
1362 uint32_t datasz;
1363
1364 datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1365
1366 return img + le32_to_cpu(hdr->srcaddr) + datasz;
1367}
1368
1369static uint32_t
1370kwboot_img_csum32(const void *img)
1371{
1372 const struct main_hdr_v1 *hdr = img;
1373 uint32_t datasz, csum = 0;
1374 const uint32_t *data;
1375
1376 datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
1377 if (datasz % sizeof(uint32_t))
1378 return 0;
1379
1380 data = img + le32_to_cpu(hdr->srcaddr);
1381 while (datasz > 0) {
1382 csum += le32_to_cpu(*data++);
1383 datasz -= 4;
1384 }
1385
1386 return cpu_to_le32(csum);
1387}
1388
Luka Perkovd131ad62012-05-27 11:44:51 +00001389static int
Pali Rohár550c9302021-09-24 23:06:57 +02001390kwboot_img_is_secure(void *img)
1391{
1392 struct opt_hdr_v1 *ohdr;
1393
1394 for_each_opt_hdr_v1 (ohdr, img)
1395 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1396 return 1;
1397
1398 return 0;
1399}
1400
Pali Rohárca272042021-09-24 23:07:05 +02001401static void *
Pali Rohár063cb352021-10-25 15:12:56 +02001402kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohárca272042021-09-24 23:07:05 +02001403{
Pali Rohárca272042021-09-24 23:07:05 +02001404 struct main_hdr_v1 *hdr = img;
Pali Rohár063cb352021-10-25 15:12:56 +02001405 void *result;
Pali Rohárca272042021-09-24 23:07:05 +02001406
Pali Rohár063cb352021-10-25 15:12:56 +02001407 /*
1408 * 32-bit checksum comes after end of image code, so we will be putting
1409 * new code there. So we get this pointer and then increase data size
1410 * (since increasing data size changes kwboot_img_csum32_ptr() return
1411 * value).
1412 */
1413 result = kwboot_img_csum32_ptr(img);
Pali Rohárca272042021-09-24 23:07:05 +02001414 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Rohár063cb352021-10-25 15:12:56 +02001415 *size += grow;
Pali Rohárca272042021-09-24 23:07:05 +02001416
Pali Rohár063cb352021-10-25 15:12:56 +02001417 return result;
Pali Rohárca272042021-09-24 23:07:05 +02001418}
1419
Pali Rohár04ced022021-09-24 23:07:03 +02001420static void
1421kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1422{
1423 uint32_t hdrsz, datasz, srcaddr;
1424 struct main_hdr_v1 *hdr = img;
Pali Rohárd656f5a2021-10-25 15:13:02 +02001425 struct opt_hdr_v1 *ohdr;
Pali Rohár04ced022021-09-24 23:07:03 +02001426 uint8_t *data;
1427
1428 srcaddr = le32_to_cpu(hdr->srcaddr);
1429
Pali Rohárd656f5a2021-10-25 15:13:02 +02001430 /* calculate real used space in kwbimage header */
1431 if (kwbimage_version(img) == 0) {
1432 hdrsz = kwbheader_size(img);
1433 } else {
1434 hdrsz = sizeof(*hdr);
1435 for_each_opt_hdr_v1 (ohdr, hdr)
1436 hdrsz += opt_hdr_v1_size(ohdr);
1437 }
1438
Pali Rohár04ced022021-09-24 23:07:03 +02001439 data = (uint8_t *)img + srcaddr;
1440 datasz = *size - srcaddr;
1441
1442 /* only move data if there is not enough space */
1443 if (hdrsz + grow > srcaddr) {
1444 size_t need = hdrsz + grow - srcaddr;
1445
1446 /* move data by enough bytes */
1447 memmove(data + need, data, datasz);
1448
1449 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1450 *size += need;
1451 }
1452
1453 if (kwbimage_version(img) == 1) {
1454 hdrsz += grow;
Pali Rohárd656f5a2021-10-25 15:13:02 +02001455 if (hdrsz > kwbheader_size(img)) {
1456 hdr->headersz_msb = hdrsz >> 16;
1457 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1458 }
Pali Rohár04ced022021-09-24 23:07:03 +02001459 }
1460}
1461
Pali Rohárca272042021-09-24 23:07:05 +02001462static void *
1463kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1464{
1465 struct main_hdr_v1 *hdr = img;
1466 struct opt_hdr_v1 *ohdr;
Pali Rohára85a71d2021-10-21 16:46:06 +02001467 uint32_t num_args;
1468 uint32_t offset;
Pali Rohárca272042021-09-24 23:07:05 +02001469 uint32_t ohdrsz;
Pali Roháre511cc32021-10-25 15:13:01 +02001470 uint8_t *prev_ext;
Pali Rohárca272042021-09-24 23:07:05 +02001471
Pali Rohár44691032022-01-12 18:20:52 +01001472 if (hdr->ext) {
Pali Rohárca272042021-09-24 23:07:05 +02001473 for_each_opt_hdr_v1 (ohdr, img)
1474 if (opt_hdr_v1_next(ohdr) == NULL)
1475 break;
1476
Pali Roháre511cc32021-10-25 15:13:01 +02001477 prev_ext = opt_hdr_v1_ext(ohdr);
1478 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohárca272042021-09-24 23:07:05 +02001479 } else {
Pali Rohárca272042021-09-24 23:07:05 +02001480 ohdr = (void *)(hdr + 1);
Pali Roháre511cc32021-10-25 15:13:01 +02001481 prev_ext = &hdr->ext;
Pali Rohárca272042021-09-24 23:07:05 +02001482 }
1483
Pali Rohára85a71d2021-10-21 16:46:06 +02001484 /*
1485 * ARM executable code inside the BIN header on some mvebu platforms
1486 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
1487 * This requirement can be met by inserting dummy arguments into
1488 * BIN header, if needed.
1489 */
1490 offset = &ohdr->data[4] - (char *)img;
1491 num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
1492
1493 ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
1494 kwboot_img_grow_hdr(hdr, size, ohdrsz);
1495
Pali Rohár44691032022-01-12 18:20:52 +01001496 *prev_ext = 1;
Pali Roháre511cc32021-10-25 15:13:01 +02001497
Pali Rohárca272042021-09-24 23:07:05 +02001498 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1499 ohdr->headersz_msb = ohdrsz >> 16;
1500 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1501
1502 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
Pali Rohára85a71d2021-10-21 16:46:06 +02001503 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohárca272042021-09-24 23:07:05 +02001504
Pali Rohára85a71d2021-10-21 16:46:06 +02001505 return &ohdr->data[4 + 4 * num_args];
Pali Rohárca272042021-09-24 23:07:05 +02001506}
1507
1508static void
Pali Rohár8dbe0272021-10-27 20:57:02 +02001509_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Rohár063cb352021-10-25 15:12:56 +02001510 int old_baud, int new_baud)
Pali Rohárca272042021-09-24 23:07:05 +02001511{
Pali Rohár063cb352021-10-25 15:12:56 +02001512 struct main_hdr_v1 *hdr = img;
Pali Rohár8dbe0272021-10-27 20:57:02 +02001513 uint32_t orig_datasz;
1514 uint32_t codesz;
Pali Rohár063cb352021-10-25 15:12:56 +02001515 uint8_t *code;
Pali Rohárca272042021-09-24 23:07:05 +02001516
Pali Rohár8dbe0272021-10-27 20:57:02 +02001517 if (for_data) {
Pali Rohár063cb352021-10-25 15:12:56 +02001518 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1519
Pali Rohár8dbe0272021-10-27 20:57:02 +02001520 codesz = sizeof(kwboot_baud_code) +
1521 sizeof(kwboot_baud_code_data_jump);
1522 code = kwboot_img_grow_data_right(img, size, codesz);
Pali Rohár063cb352021-10-25 15:12:56 +02001523 } else {
Pali Rohár8dbe0272021-10-27 20:57:02 +02001524 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1525 sizeof(kwboot_baud_code) +
1526 sizeof(kwboot_baud_code_binhdr_post);
Pali Rohár063cb352021-10-25 15:12:56 +02001527 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár8dbe0272021-10-27 20:57:02 +02001528
1529 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1530 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1531 code += codesz;
Pali Rohárca272042021-09-24 23:07:05 +02001532 }
1533
Pali Rohár8dbe0272021-10-27 20:57:02 +02001534 codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
1535 memcpy(code, kwboot_baud_code, codesz);
1536 code += codesz;
1537 *(uint32_t *)code = cpu_to_le32(old_baud);
1538 code += sizeof(uint32_t);
1539 *(uint32_t *)code = cpu_to_le32(new_baud);
1540 code += sizeof(uint32_t);
1541
1542 if (for_data) {
1543 codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
1544 memcpy(code, kwboot_baud_code_data_jump, codesz);
1545 code += codesz;
1546 *(uint32_t *)code = hdr->execaddr;
1547 code += sizeof(uint32_t);
1548 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
1549 } else {
1550 codesz = sizeof(kwboot_baud_code_binhdr_post);
1551 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1552 code += codesz;
1553 }
Pali Rohárca272042021-09-24 23:07:05 +02001554}
1555
Pali Rohár550c9302021-09-24 23:06:57 +02001556static int
Pali Rohárca272042021-09-24 23:07:05 +02001557kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001558{
Stefan Roesee29f1db2015-09-29 09:19:59 +02001559 struct main_hdr_v1 *hdr;
Pali Rohár792e4232021-09-24 23:06:58 +02001560 uint32_t srcaddr;
Luka Perkovd131ad62012-05-27 11:44:51 +00001561 uint8_t csum;
Marek Behún5c8f8122021-09-24 23:07:04 +02001562 size_t hdrsz;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001563 int image_ver;
Pali Rohár550c9302021-09-24 23:06:57 +02001564 int is_secure;
Luka Perkovd131ad62012-05-27 11:44:51 +00001565
Luka Perkovd131ad62012-05-27 11:44:51 +00001566 hdr = img;
1567
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001568 if (*size < sizeof(struct main_hdr_v1))
1569 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001570
Marek Behúnacb0b382021-09-24 23:07:00 +02001571 image_ver = kwbimage_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +02001572 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +02001573 fprintf(stderr, "Invalid image header version\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001574 goto err;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001575 }
1576
Marek Behúnfe2fd732021-09-24 23:07:01 +02001577 hdrsz = kwbheader_size(hdr);
Stefan Roesee29f1db2015-09-29 09:19:59 +02001578
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001579 if (*size < hdrsz)
1580 goto err;
Pali Rohár825a2ca2021-07-23 11:14:21 +02001581
Marek Behúnfe2fd732021-09-24 23:07:01 +02001582 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001583 if (csum != hdr->checksum)
1584 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001585
Pali Rohár792e4232021-09-24 23:06:58 +02001586 srcaddr = le32_to_cpu(hdr->srcaddr);
1587
1588 switch (hdr->blockid) {
1589 case IBR_HDR_SATA_ID:
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001590 if (srcaddr < 1)
1591 goto err;
1592
Pali Rohár792e4232021-09-24 23:06:58 +02001593 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
1594 break;
1595
1596 case IBR_HDR_SDIO_ID:
1597 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
1598 break;
1599
1600 case IBR_HDR_PEX_ID:
1601 if (srcaddr == 0xFFFFFFFF)
1602 hdr->srcaddr = cpu_to_le32(hdrsz);
1603 break;
Pali Rohárf2c644e2021-09-24 23:06:59 +02001604
1605 case IBR_HDR_SPI_ID:
1606 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1607 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1608 hdr->destaddr = cpu_to_le32(0x00800000);
1609 hdr->execaddr = cpu_to_le32(0x00800000);
1610 }
1611 break;
Pali Rohár792e4232021-09-24 23:06:58 +02001612 }
1613
Pali Rohár04ced022021-09-24 23:07:03 +02001614 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001615 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1616 goto err;
Pali Rohár04ced022021-09-24 23:07:03 +02001617
Pali Rohárad9a3ac2021-10-25 15:12:55 +02001618 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1619 goto err;
1620
Pali Rohár550c9302021-09-24 23:06:57 +02001621 is_secure = kwboot_img_is_secure(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001622
Pali Rohár550c9302021-09-24 23:06:57 +02001623 if (hdr->blockid != IBR_HDR_UART_ID) {
1624 if (is_secure) {
1625 fprintf(stderr,
1626 "Image has secure header with signature for non-UART booting\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001627 goto err;
Pali Rohár550c9302021-09-24 23:06:57 +02001628 }
1629
1630 kwboot_printv("Patching image boot signature to UART\n");
1631 hdr->blockid = IBR_HDR_UART_ID;
1632 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001633
Pali Rohár0089f612021-10-22 12:37:47 +02001634 if (!is_secure) {
Pali Rohár4bebab62021-10-25 15:12:58 +02001635 if (image_ver == 1) {
1636 /*
1637 * Tell BootROM to send BootROM messages to UART port
1638 * number 0 (used also for UART booting) with default
1639 * baudrate (which should be 115200) and do not touch
1640 * UART MPP configuration.
1641 */
Pali Rohárffccee22022-01-25 18:13:13 +01001642 hdr->flags |= 0x1;
Pali Rohár4bebab62021-10-25 15:12:58 +02001643 hdr->options &= ~0x1F;
1644 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
1645 hdr->options |= 0 << 3;
1646 }
Pali Rohár0089f612021-10-22 12:37:47 +02001647 if (image_ver == 0)
1648 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
1649 hdr->nandpagesize = 0;
1650 }
1651
Pali Rohárca272042021-09-24 23:07:05 +02001652 if (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +02001653 if (image_ver == 0) {
1654 fprintf(stderr,
1655 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001656 goto err;
Pali Rohárca272042021-09-24 23:07:05 +02001657 }
1658
1659 if (is_secure) {
1660 fprintf(stderr,
1661 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001662 goto err;
Pali Rohárca272042021-09-24 23:07:05 +02001663 }
1664
1665 /*
1666 * First inject code that changes the baudrate from the default
1667 * value of 115200 Bd to requested value. This code is inserted
1668 * as a new opt hdr, so it is executed by BootROM after the
1669 * header part is received.
1670 */
1671 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
1672 baudrate);
Pali Rohár063cb352021-10-25 15:12:56 +02001673 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohárca272042021-09-24 23:07:05 +02001674
1675 /*
1676 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Rohár063cb352021-10-25 15:12:56 +02001677 * This code is appended after the data part of the image, and
1678 * execaddr is changed so that it is executed before U-Boot
1679 * proper.
Pali Rohárca272042021-09-24 23:07:05 +02001680 */
1681 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Rohár063cb352021-10-25 15:12:56 +02001682 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohárca272042021-09-24 23:07:05 +02001683
Pali Rohár82c5a0a2021-10-25 15:12:57 +02001684 /* Update the 32-bit data checksum */
1685 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
1686
Pali Rohárca272042021-09-24 23:07:05 +02001687 /* recompute header size */
1688 hdrsz = kwbheader_size(hdr);
1689 }
1690
Pali Rohár04ced022021-09-24 23:07:03 +02001691 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Roháred792c22021-10-25 15:13:00 +02001692 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohár04ced022021-09-24 23:07:03 +02001693
1694 if (is_secure) {
1695 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001696 goto err;
Pali Rohár04ced022021-09-24 23:07:03 +02001697 }
1698
1699 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Roháred792c22021-10-25 15:13:00 +02001700 kwboot_img_grow_hdr(img, size, grow);
Pali Rohár04ced022021-09-24 23:07:03 +02001701 }
1702
Marek Behúnfe2fd732021-09-24 23:07:01 +02001703 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +00001704
Pali Rohár04ced022021-09-24 23:07:03 +02001705 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001706 return 0;
1707err:
1708 errno = EINVAL;
1709 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001710}
1711
1712static void
1713kwboot_usage(FILE *stream, char *progname)
1714{
1715 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +00001716 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +02001717 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +00001718 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +02001719 fprintf(stream,
1720 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roese84899e22014-10-22 12:13:21 +02001721 fprintf(stream,
1722 " -D <image>: boot <image> without preamble (Dove)\n");
1723 fprintf(stream, " -d: enter debug mode\n");
1724 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001725 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +00001726 fprintf(stream,
1727 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001728 fprintf(stream, "\n");
1729 fprintf(stream, " -t: mini terminal\n");
1730 fprintf(stream, "\n");
1731 fprintf(stream, " -B <baud>: set baud rate\n");
1732 fprintf(stream, "\n");
1733}
1734
1735int
1736main(int argc, char **argv)
1737{
1738 const char *ttypath, *imgpath;
Pali Rohárddc04fa2021-09-24 23:06:55 +02001739 int rv, rc, tty, term;
Luka Perkovd131ad62012-05-27 11:44:51 +00001740 void *bootmsg;
Stefan Roese84899e22014-10-22 12:13:21 +02001741 void *debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +00001742 void *img;
1743 size_t size;
Pali Rohárca272042021-09-24 23:07:05 +02001744 size_t after_img_rsv;
1745 int baudrate;
Pali Rohárc513fe42022-01-25 18:13:07 +01001746 int prev_optind;
1747 int c;
Luka Perkovd131ad62012-05-27 11:44:51 +00001748
1749 rv = 1;
1750 tty = -1;
1751 bootmsg = NULL;
Stefan Roese84899e22014-10-22 12:13:21 +02001752 debugmsg = NULL;
Luka Perkovd131ad62012-05-27 11:44:51 +00001753 imgpath = NULL;
1754 img = NULL;
1755 term = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001756 size = 0;
Pali Rohárca272042021-09-24 23:07:05 +02001757 after_img_rsv = KWBOOT_XM_BLKSZ;
1758 baudrate = 115200;
Luka Perkovd131ad62012-05-27 11:44:51 +00001759
Pali Rohár75176dc2021-11-05 23:30:42 +01001760 printf("kwboot version %s\n", PLAIN_VERSION);
1761
Luka Perkovd131ad62012-05-27 11:44:51 +00001762 kwboot_verbose = isatty(STDOUT_FILENO);
1763
1764 do {
Pali Rohárc513fe42022-01-25 18:13:07 +01001765 prev_optind = optind;
1766 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +00001767 if (c < 0)
1768 break;
1769
1770 switch (c) {
1771 case 'b':
Pali Rohárc513fe42022-01-25 18:13:07 +01001772 if (imgpath || bootmsg || debugmsg)
1773 goto usage;
Luka Perkovd131ad62012-05-27 11:44:51 +00001774 bootmsg = kwboot_msg_boot;
Pali Rohárc513fe42022-01-25 18:13:07 +01001775 if (prev_optind == optind)
1776 goto usage;
Pali Rohár9e6d71d2022-02-07 10:12:24 +01001777 if (optind < argc - 1 && argv[optind] && argv[optind][0] != '-')
Pali Rohárc513fe42022-01-25 18:13:07 +01001778 imgpath = argv[optind++];
Luka Perkovd131ad62012-05-27 11:44:51 +00001779 break;
1780
Stefan Roese84899e22014-10-22 12:13:21 +02001781 case 'D':
Pali Rohárc513fe42022-01-25 18:13:07 +01001782 if (imgpath || bootmsg || debugmsg)
1783 goto usage;
Stefan Roese84899e22014-10-22 12:13:21 +02001784 bootmsg = NULL;
1785 imgpath = optarg;
1786 break;
1787
1788 case 'd':
Pali Rohárc513fe42022-01-25 18:13:07 +01001789 if (imgpath || bootmsg || debugmsg)
1790 goto usage;
Stefan Roese84899e22014-10-22 12:13:21 +02001791 debugmsg = kwboot_msg_debug;
1792 break;
1793
Luka Perkovd131ad62012-05-27 11:44:51 +00001794 case 'p':
Pali Rohárddc04fa2021-09-24 23:06:55 +02001795 /* nop, for backward compatibility */
Luka Perkovd131ad62012-05-27 11:44:51 +00001796 break;
1797
1798 case 't':
1799 term = 1;
1800 break;
1801
Stefan Roese84899e22014-10-22 12:13:21 +02001802 case 'a':
Stefan Roese84899e22014-10-22 12:13:21 +02001803 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1804 break;
1805
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001806 case 'q':
Pali Rohár132016e2022-03-02 11:49:19 +01001807 /* nop, for backward compatibility */
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001808 break;
1809
1810 case 's':
1811 msg_rsp_timeo = atoi(optarg);
1812 break;
1813
Kevin Smith7497a6a2016-02-16 21:28:19 +00001814 case 'o':
1815 blk_rsp_timeo = atoi(optarg);
1816 break;
1817
Luka Perkovd131ad62012-05-27 11:44:51 +00001818 case 'B':
Pali Rohárca272042021-09-24 23:07:05 +02001819 baudrate = atoi(optarg);
Luka Perkovd131ad62012-05-27 11:44:51 +00001820 break;
1821
1822 case 'h':
1823 rv = 0;
1824 default:
1825 goto usage;
1826 }
1827 } while (1);
1828
Pali Rohára3c64962022-01-25 18:13:12 +01001829 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovd131ad62012-05-27 11:44:51 +00001830 goto usage;
1831
Luka Perkovd131ad62012-05-27 11:44:51 +00001832 ttypath = argv[optind++];
1833
Pali Rohárc513fe42022-01-25 18:13:07 +01001834 if (optind != argc)
1835 goto usage;
1836
Pali Rohárca272042021-09-24 23:07:05 +02001837 tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001838 if (tty < 0) {
1839 perror(ttypath);
1840 goto out;
1841 }
1842
Pali Rohárca272042021-09-24 23:07:05 +02001843 if (baudrate == 115200)
1844 /* do not change baudrate during Xmodem to the same value */
1845 baudrate = 0;
1846 else
1847 /* ensure we have enough space for baudrate change code */
Pali Rohár8dbe0272021-10-27 20:57:02 +02001848 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
1849 sizeof(kwboot_baud_code_binhdr_pre) +
Pali Rohár5923ef62021-10-25 15:12:54 +02001850 sizeof(kwboot_baud_code) +
Pali Rohár8dbe0272021-10-27 20:57:02 +02001851 sizeof(kwboot_baud_code_binhdr_post) +
1852 KWBOOT_XM_BLKSZ +
1853 sizeof(kwboot_baud_code) +
1854 sizeof(kwboot_baud_code_data_jump) +
Pali Rohár5923ef62021-10-25 15:12:54 +02001855 KWBOOT_XM_BLKSZ;
Pali Rohárca272042021-09-24 23:07:05 +02001856
Luka Perkovd131ad62012-05-27 11:44:51 +00001857 if (imgpath) {
Pali Rohárca272042021-09-24 23:07:05 +02001858 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovd131ad62012-05-27 11:44:51 +00001859 if (!img) {
1860 perror(imgpath);
1861 goto out;
1862 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001863
Pali Rohárca272042021-09-24 23:07:05 +02001864 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001865 if (rc) {
1866 fprintf(stderr, "%s: Invalid image.\n", imgpath);
1867 goto out;
1868 }
1869 }
1870
Stefan Roese84899e22014-10-22 12:13:21 +02001871 if (debugmsg) {
1872 rc = kwboot_debugmsg(tty, debugmsg);
1873 if (rc) {
1874 perror("debugmsg");
1875 goto out;
1876 }
Willy Tarreau3475a712018-07-03 12:10:30 -04001877 } else if (bootmsg) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001878 rc = kwboot_bootmsg(tty, bootmsg);
1879 if (rc) {
1880 perror("bootmsg");
1881 goto out;
1882 }
1883 }
1884
1885 if (img) {
Pali Rohárca272042021-09-24 23:07:05 +02001886 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001887 if (rc) {
1888 perror("xmodem");
1889 goto out;
1890 }
1891 }
1892
1893 if (term) {
1894 rc = kwboot_terminal(tty);
1895 if (rc && !(errno == EINTR)) {
1896 perror("terminal");
1897 goto out;
1898 }
1899 }
1900
1901 rv = 0;
1902out:
1903 if (tty >= 0)
1904 close(tty);
1905
1906 if (img)
Pali Rohár04ced022021-09-24 23:07:03 +02001907 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001908
1909 return rv;
1910
1911usage:
1912 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
1913 goto out;
1914}