blob: 4e2acb52458a010121eb890c9d837d255ea29e41 [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
Pali Rohárc1d911f2022-03-02 11:49:20 +0100721kwboot_bootmsg(int tty)
Luka Perkovd131ad62012-05-27 11:44:51 +0000722{
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
Pali Rohárc1d911f2022-03-02 11:49:20 +0100728 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000729
730 do {
731 rc = tcflush(tty, TCIOFLUSH);
732 if (rc)
733 break;
734
Jon Nettleton9ca6fae2018-08-13 18:24:38 +0300735 for (count = 0; count < 128; count++) {
Pali Rohárc1d911f2022-03-02 11:49:20 +0100736 rc = kwboot_tty_send(tty, kwboot_msg_boot, sizeof(kwboot_msg_boot), 0);
Pali Rohárd8865f82022-03-02 11:49:18 +0100737 if (rc)
738 break;
Luka Perkovd131ad62012-05-27 11:44:51 +0000739 }
740
Stefan Roese84899e22014-10-22 12:13:21 +0200741 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovd131ad62012-05-27 11:44:51 +0000742
743 kwboot_spinner();
744
745 } while (rc || c != NAK);
746
747 kwboot_printv("\n");
748
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100749 if (rc)
750 return rc;
751
752 /*
753 * At this stage we have sent more boot message patterns and BootROM
754 * (at least on Armada XP and 385) started interpreting sent bytes as
755 * part of xmodem packets. If BootROM is expecting SOH byte as start of
756 * a xmodem packet and it receives byte 0xff, then it throws it away and
757 * sends a NAK reply to host. If BootROM does not receive any byte for
758 * 2s when expecting some continuation of the xmodem packet, it throws
759 * away the partially received xmodem data and sends NAK reply to host.
760 *
761 * Therefore for starting xmodem transfer we have two options: Either
762 * wait 2s or send 132 0xff bytes (which is the size of xmodem packet)
763 * to ensure that BootROM throws away any partially received data.
764 */
765
766 /* flush output queue with remaining boot message patterns */
Pali Rohárd8865f82022-03-02 11:49:18 +0100767 rc = tcflush(tty, TCOFLUSH);
768 if (rc) {
769 perror("Failed to flush output queue");
770 return rc;
771 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100772
773 /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */
774 memset(&block, 0xff, sizeof(block));
Pali Rohárd8865f82022-03-02 11:49:18 +0100775 rc = kwboot_tty_send(tty, &block, sizeof(block), 0);
776 if (rc) {
777 perror("Failed to send sync sequence");
778 return rc;
779 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100780
781 /*
782 * Sending 132 bytes via 115200B/8-N-1 takes 11.45 ms, reading 132 bytes
783 * takes 11.45 ms, so waiting for 30 ms should be enough.
784 */
785 usleep(30 * 1000);
786
787 /* flush remaining NAK replies from input queue */
Pali Rohárd8865f82022-03-02 11:49:18 +0100788 rc = tcflush(tty, TCIFLUSH);
789 if (rc) {
790 perror("Failed to flush input queue");
791 return rc;
792 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100793
794 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000795}
796
797static int
Pali Rohárc1d911f2022-03-02 11:49:20 +0100798kwboot_debugmsg(int tty)
Stefan Roese84899e22014-10-22 12:13:21 +0200799{
800 int rc;
801
802 kwboot_printv("Sending debug message. Please reboot the target...");
803
804 do {
805 char buf[16];
806
807 rc = tcflush(tty, TCIOFLUSH);
808 if (rc)
809 break;
810
Pali Rohárc1d911f2022-03-02 11:49:20 +0100811 rc = kwboot_tty_send(tty, kwboot_msg_debug, sizeof(kwboot_msg_debug), 0);
Pali Rohárd8865f82022-03-02 11:49:18 +0100812 if (rc)
813 break;
Stefan Roese84899e22014-10-22 12:13:21 +0200814
815 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
816
817 kwboot_spinner();
818
819 } while (rc);
820
821 kwboot_printv("\n");
822
823 return rc;
824}
825
Pali Rohárc5d666a2021-09-24 23:06:44 +0200826static size_t
Luka Perkovd131ad62012-05-27 11:44:51 +0000827kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
828 size_t size, int pnum)
829{
Marek Behúnd8cc8512021-09-24 23:06:45 +0200830 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +0000831
Stefan Roese84899e22014-10-22 12:13:21 +0200832 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +0000833 block->pnum = pnum;
834 block->_pnum = ~block->pnum;
835
Pali Rohár2ef87f72021-09-24 23:06:48 +0200836 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovd131ad62012-05-27 11:44:51 +0000837 memcpy(&block->data[0], data, n);
Pali Rohár2ef87f72021-09-24 23:06:48 +0200838 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovd131ad62012-05-27 11:44:51 +0000839
840 block->csum = 0;
841 for (i = 0; i < n; i++)
842 block->csum += block->data[i];
843
844 return n;
845}
846
Marek Behún12df7b72021-09-24 23:06:52 +0200847static uint64_t
848_now(void)
849{
850 struct timespec ts;
851
852 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
853 static int err_print;
854
855 if (!err_print) {
856 perror("clock_gettime() does not work");
857 err_print = 1;
858 }
859
860 /* this will just make the timeout not work */
861 return -1ULL;
862 }
863
864 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
865}
866
Luka Perkovd131ad62012-05-27 11:44:51 +0000867static int
Marek Behún408ea612021-09-24 23:06:49 +0200868_is_xm_reply(char c)
869{
Pali Rohár94c906a2022-01-25 18:13:03 +0100870 return c == ACK || c == NAK;
Marek Behún408ea612021-09-24 23:06:49 +0200871}
872
873static int
Pali Rohár9cdc2642021-09-24 23:06:54 +0200874_xm_reply_to_error(int c)
875{
876 int rc = -1;
877
878 switch (c) {
879 case ACK:
880 rc = 0;
881 break;
882 case NAK:
883 errno = EBADMSG;
884 break;
Pali Rohár9cdc2642021-09-24 23:06:54 +0200885 default:
886 errno = EPROTO;
887 break;
888 }
889
890 return rc;
891}
892
893static int
Pali Rohárca272042021-09-24 23:07:05 +0200894kwboot_baud_magic_handle(int fd, char c, int baudrate)
895{
896 static size_t rcv_len;
897
898 if (rcv_len < sizeof(kwb_baud_magic)) {
899 /* try to recognize whole magic word */
900 if (c == kwb_baud_magic[rcv_len]) {
901 rcv_len++;
902 } else {
903 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
904 fflush(stdout);
905 rcv_len = 0;
906 }
907 }
908
909 if (rcv_len == sizeof(kwb_baud_magic)) {
910 /* magic word received */
911 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
912
913 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
914 } else {
915 return 0;
916 }
917}
918
919static int
Pali Rohár950ed242022-01-25 18:13:04 +0100920kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Rohár82a9e132022-01-25 18:13:02 +0100921 int ignore_nak_reply,
Pali Rohára6fcac22021-10-25 15:13:04 +0200922 int allow_non_xm, int *non_xm_print,
Pali Rohárca272042021-09-24 23:07:05 +0200923 int baudrate, int *baud_changed)
Pali Rohár48b3ea62021-09-24 23:06:50 +0200924{
Marek Behún12df7b72021-09-24 23:06:52 +0200925 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún819cd322021-09-24 23:06:53 +0200926 uint64_t recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200927 int rc;
928
929 while (1) {
Marek Behún12df7b72021-09-24 23:06:52 +0200930 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohár48b3ea62021-09-24 23:06:50 +0200931 if (rc) {
932 if (errno != ETIMEDOUT)
933 return rc;
Marek Behún819cd322021-09-24 23:06:53 +0200934 else if (allow_non_xm && *non_xm_print)
Marek Behún12df7b72021-09-24 23:06:52 +0200935 return -1;
936 else
937 *c = NAK;
Pali Rohár48b3ea62021-09-24 23:06:50 +0200938 }
939
940 /* If received xmodem reply, end. */
Pali Rohár82a9e132022-01-25 18:13:02 +0100941 if (_is_xm_reply(*c)) {
942 if (*c == NAK && ignore_nak_reply) {
943 timeout = recv_until - _now();
944 if (timeout >= 0)
945 continue;
946 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200947 break;
Pali Rohár82a9e132022-01-25 18:13:02 +0100948 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200949
950 /*
Pali Rohárca272042021-09-24 23:07:05 +0200951 * If receiving/printing non-xmodem text output is allowed and
952 * such a byte was received, we want to increase receiving time
953 * and either:
954 * - print the byte, if it is not part of baudrate change magic
955 * sequence while baudrate change was requested (-B option)
956 * - change baudrate
Marek Behún819cd322021-09-24 23:06:53 +0200957 * Otherwise decrease timeout by time elapsed.
Pali Rohár48b3ea62021-09-24 23:06:50 +0200958 */
959 if (allow_non_xm) {
Marek Behún12df7b72021-09-24 23:06:52 +0200960 recv_until = _now() + timeout;
Pali Rohárca272042021-09-24 23:07:05 +0200961
962 if (baudrate && !*baud_changed) {
963 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
964 if (rc == 1)
965 *baud_changed = 1;
966 else if (!rc)
967 *non_xm_print = 1;
968 else
969 return rc;
970 } else if (!baudrate || !*baud_changed) {
971 putchar(*c);
972 fflush(stdout);
973 *non_xm_print = 1;
974 }
Marek Behún819cd322021-09-24 23:06:53 +0200975 } else {
Pali Rohár950ed242022-01-25 18:13:04 +0100976 if (stop_on_non_xm)
Pali Rohára6fcac22021-10-25 15:13:04 +0200977 break;
Marek Behún819cd322021-09-24 23:06:53 +0200978 timeout = recv_until - _now();
979 if (timeout < 0) {
980 errno = ETIMEDOUT;
981 return -1;
982 }
Pali Rohár48b3ea62021-09-24 23:06:50 +0200983 }
984 }
985
986 return 0;
987}
988
989static int
990kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár5875ad42022-01-25 18:13:05 +0100991 int *done_print, int baudrate, int allow_retries)
Luka Perkovd131ad62012-05-27 11:44:51 +0000992{
Pali Rohárca272042021-09-24 23:07:05 +0200993 int non_xm_print, baud_changed;
994 int rc, err, retries;
Luka Perkovd131ad62012-05-27 11:44:51 +0000995 char c;
996
Pali Rohár48b3ea62021-09-24 23:06:50 +0200997 *done_print = 0;
Pali Rohár455c0d22021-10-27 20:56:58 +0200998 non_xm_print = 0;
999 baud_changed = 0;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001000
Pali Rohárd14a3422021-10-25 15:13:03 +02001001 retries = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001002 do {
Pali Rohárcab817d2021-10-27 20:56:59 +02001003 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovd131ad62012-05-27 11:44:51 +00001004 if (rc)
Pali Rohár94c906a2022-01-25 18:13:03 +01001005 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001006
Pali Rohár48b3ea62021-09-24 23:06:50 +02001007 if (allow_non_xm && !*done_print) {
1008 kwboot_progress(100, '.');
1009 kwboot_printv("Done\n");
1010 *done_print = 1;
1011 }
Stefan Roese84899e22014-10-22 12:13:21 +02001012
Pali Rohára6fcac22021-10-25 15:13:04 +02001013 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Rohár82a9e132022-01-25 18:13:02 +01001014 retries > 8,
Pali Rohára6fcac22021-10-25 15:13:04 +02001015 allow_non_xm, &non_xm_print,
Pali Rohárca272042021-09-24 23:07:05 +02001016 baudrate, &baud_changed);
Pali Rohár48b3ea62021-09-24 23:06:50 +02001017 if (rc)
Pali Rohár94c906a2022-01-25 18:13:03 +01001018 goto err;
Stefan Roese84899e22014-10-22 12:13:21 +02001019
Pali Rohár5d8aa4c2022-01-25 18:13:06 +01001020 if (!allow_non_xm && c != ACK) {
1021 if (c == NAK && allow_retries && retries + 1 < 16)
1022 kwboot_progress(-1, '+');
1023 else
1024 kwboot_progress(-1, 'E');
1025 }
Pali Rohár5875ad42022-01-25 18:13:05 +01001026 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovd131ad62012-05-27 11:44:51 +00001027
Marek Behún2e81b3a2021-09-24 23:06:51 +02001028 if (non_xm_print)
1029 kwboot_printv("\n");
1030
Pali Rohárca272042021-09-24 23:07:05 +02001031 if (allow_non_xm && baudrate && !baud_changed) {
1032 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohárca272042021-09-24 23:07:05 +02001033 errno = EPROTO;
Pali Rohár94c906a2022-01-25 18:13:03 +01001034 return -1;
Pali Rohárca272042021-09-24 23:07:05 +02001035 }
1036
Pali Rohár9cdc2642021-09-24 23:06:54 +02001037 return _xm_reply_to_error(c);
Pali Rohár94c906a2022-01-25 18:13:03 +01001038err:
Pali Rohárca272042021-09-24 23:07:05 +02001039 err = errno;
Pali Rohárca272042021-09-24 23:07:05 +02001040 kwboot_printv("\n");
1041 errno = err;
1042 return rc;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001043}
Luka Perkovd131ad62012-05-27 11:44:51 +00001044
Pali Rohár9cdc2642021-09-24 23:06:54 +02001045static int
1046kwboot_xm_finish(int fd)
1047{
1048 int rc, retries;
1049 char c;
Luka Perkovd131ad62012-05-27 11:44:51 +00001050
Pali Rohár9cdc2642021-09-24 23:06:54 +02001051 kwboot_printv("Finishing transfer\n");
1052
Pali Rohárd14a3422021-10-25 15:13:03 +02001053 retries = 0;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001054 do {
1055 rc = kwboot_tty_send_char(fd, EOT);
1056 if (rc)
1057 return rc;
1058
Pali Rohára6fcac22021-10-25 15:13:04 +02001059 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Rohár82a9e132022-01-25 18:13:02 +01001060 retries > 8,
Pali Rohára6fcac22021-10-25 15:13:04 +02001061 0, NULL, 0, NULL);
Pali Rohár9cdc2642021-09-24 23:06:54 +02001062 if (rc)
1063 return rc;
Pali Rohárd14a3422021-10-25 15:13:03 +02001064 } while (c == NAK && retries++ < 16);
Pali Rohár9cdc2642021-09-24 23:06:54 +02001065
1066 return _xm_reply_to_error(c);
Luka Perkovd131ad62012-05-27 11:44:51 +00001067}
1068
1069static int
Pali Rohár2ef87f72021-09-24 23:06:48 +02001070kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohárca272042021-09-24 23:07:05 +02001071 size_t size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001072{
Pali Rohár48b3ea62021-09-24 23:06:50 +02001073 int done_print = 0;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001074 size_t sent, left;
1075 int rc;
Luka Perkovd131ad62012-05-27 11:44:51 +00001076
Pali Rohár2ef87f72021-09-24 23:06:48 +02001077 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1078 header ? "header" : "data", size);
Luka Perkovd131ad62012-05-27 11:44:51 +00001079
Pali Rohár2ef87f72021-09-24 23:06:48 +02001080 left = size;
1081 sent = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001082
Pali Rohár2ef87f72021-09-24 23:06:48 +02001083 while (sent < size) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001084 struct kwboot_block block;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001085 int last_block;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001086 size_t blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001087
Pali Rohár2ef87f72021-09-24 23:06:48 +02001088 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1089 data += blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001090
Pali Rohár48b3ea62021-09-24 23:06:50 +02001091 last_block = (left <= blksz);
1092
Pali Rohár5875ad42022-01-25 18:13:05 +01001093 /*
1094 * Handling of repeated xmodem packets is completely broken in
1095 * Armada 385 BootROM - it completely ignores xmodem packet
1096 * numbers, they are only used for checksum verification.
1097 * BootROM can handle a retry of the xmodem packet only during
1098 * the transmission of kwbimage header and only if BootROM
1099 * itself sent NAK response to previous attempt (it does it on
1100 * checksum failure). During the transmission of kwbimage data
1101 * part, BootROM always expects next xmodem packet, even if it
1102 * sent NAK to previous attempt - there is absolutely no way to
1103 * repair incorrectly transmitted xmodem packet during kwbimage
1104 * data part upload. Also, if kwboot receives non-ACK/NAK
1105 * response (meaning that original BootROM response was damaged
1106 * on UART) there is no way to detect if BootROM accepted xmodem
1107 * packet or not and no way to check if kwboot could repeat the
1108 * packet or not.
1109 *
1110 * Stop transfer and return failure if kwboot receives unknown
1111 * reply if non-xmodem reply is not allowed (for all xmodem
1112 * packets except the last header packet) or when non-ACK reply
1113 * is received during data part transfer.
1114 */
Pali Rohár48b3ea62021-09-24 23:06:50 +02001115 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár5875ad42022-01-25 18:13:05 +01001116 &done_print, baudrate, header);
Luka Perkovd131ad62012-05-27 11:44:51 +00001117 if (rc)
1118 goto out;
1119
Pali Rohár2ef87f72021-09-24 23:06:48 +02001120 sent += blksz;
1121 left -= blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001122
Pali Rohár48b3ea62021-09-24 23:06:50 +02001123 if (!done_print)
1124 kwboot_progress(sent * 100 / size, '.');
Pali Rohár2ef87f72021-09-24 23:06:48 +02001125 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001126
Pali Rohár48b3ea62021-09-24 23:06:50 +02001127 if (!done_print)
1128 kwboot_printv("Done\n");
Pali Rohár2ef87f72021-09-24 23:06:48 +02001129
1130 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001131out:
Pali Rohárd5ba8db2021-09-24 23:06:47 +02001132 kwboot_printv("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001133 return rc;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001134}
Luka Perkovd131ad62012-05-27 11:44:51 +00001135
Pali Rohár2ef87f72021-09-24 23:06:48 +02001136static int
Pali Rohárca272042021-09-24 23:07:05 +02001137kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohár2ef87f72021-09-24 23:06:48 +02001138{
1139 const uint8_t *img = _img;
1140 int rc, pnum;
1141 size_t hdrsz;
1142
Marek Behúnfe2fd732021-09-24 23:07:01 +02001143 hdrsz = kwbheader_size(img);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001144
Pali Rohárf8017c32021-11-05 23:29:58 +01001145 /*
1146 * If header size is not aligned to xmodem block size (which applies
1147 * for all images in kwbimage v0 format) then we have to ensure that
1148 * the last xmodem block of header contains beginning of the data
1149 * followed by the header. So align header size to xmodem block size.
1150 */
1151 hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
1152
Pali Rohár2ef87f72021-09-24 23:06:48 +02001153 pnum = 1;
1154
Pali Rohárca272042021-09-24 23:07:05 +02001155 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001156 if (rc)
1157 return rc;
1158
Pali Rohárf8017c32021-11-05 23:29:58 +01001159 /*
1160 * If we have already sent image data as a part of the last
1161 * xmodem header block then we have nothing more to send.
1162 */
1163 if (hdrsz < size) {
1164 img += hdrsz;
1165 size -= hdrsz;
1166 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
1167 if (rc)
1168 return rc;
1169 }
Pali Rohár2ef87f72021-09-24 23:06:48 +02001170
Pali Rohárca272042021-09-24 23:07:05 +02001171 rc = kwboot_xm_finish(tty);
1172 if (rc)
1173 return rc;
1174
1175 if (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +02001176 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1177 rc = kwboot_tty_change_baudrate(tty, 115200);
1178 if (rc)
1179 return rc;
1180 }
1181
1182 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001183}
1184
1185static int
Marek Behún46237e62021-09-24 23:06:40 +02001186kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovd131ad62012-05-27 11:44:51 +00001187{
Pali Rohárde751402022-02-03 17:45:20 +01001188 char buf[128];
Marek Behúne453bb42021-09-24 23:06:41 +02001189 ssize_t nin;
Luka Perkovd131ad62012-05-27 11:44:51 +00001190
Pali Rohárde751402022-02-03 17:45:20 +01001191 nin = read(in, buf, sizeof(buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -04001192 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +00001193 return -1;
1194
1195 if (quit) {
1196 int i;
1197
1198 for (i = 0; i < nin; i++) {
Pali Rohárde751402022-02-03 17:45:20 +01001199 if (buf[i] == quit[*s]) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001200 (*s)++;
Pali Rohárde751402022-02-03 17:45:20 +01001201 if (!quit[*s]) {
Pali Rohár7938b3b2022-02-18 12:24:13 +01001202 nin = (i > *s) ? (i - *s) : 0;
Pali Rohárde751402022-02-03 17:45:20 +01001203 break;
1204 }
Pali Rohárb943eee2021-07-23 11:14:20 +02001205 } else {
Pali Rohárde751402022-02-03 17:45:20 +01001206 if (*s > i && kwboot_write(out, quit, *s - i) < 0)
Marek Behúne453bb42021-09-24 23:06:41 +02001207 return -1;
1208 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +02001209 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001210 }
Pali Rohárde751402022-02-03 17:45:20 +01001211
1212 if (i == nin)
Pali Rohár7938b3b2022-02-18 12:24:13 +01001213 nin -= (nin > *s) ? *s : nin;
Luka Perkovd131ad62012-05-27 11:44:51 +00001214 }
1215
Marek Behúne453bb42021-09-24 23:06:41 +02001216 if (kwboot_write(out, buf, nin) < 0)
1217 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001218
1219 return 0;
1220}
1221
1222static int
1223kwboot_terminal(int tty)
1224{
1225 int rc, in, s;
Marek Behún46237e62021-09-24 23:06:40 +02001226 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +00001227 struct termios otio, tio;
1228
1229 rc = -1;
1230
1231 in = STDIN_FILENO;
1232 if (isatty(in)) {
1233 rc = tcgetattr(in, &otio);
1234 if (!rc) {
1235 tio = otio;
1236 cfmakeraw(&tio);
1237 rc = tcsetattr(in, TCSANOW, &tio);
1238 }
1239 if (rc) {
1240 perror("tcsetattr");
1241 goto out;
1242 }
1243
1244 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún5fa04f42021-09-24 23:07:11 +02001245 quit[0] | 0100, quit[1]);
Luka Perkovd131ad62012-05-27 11:44:51 +00001246 } else
1247 in = -1;
1248
1249 rc = 0;
1250 s = 0;
1251
1252 do {
1253 fd_set rfds;
1254 int nfds = 0;
1255
Pali Rohár0a143412021-10-25 15:12:52 +02001256 FD_ZERO(&rfds);
Luka Perkovd131ad62012-05-27 11:44:51 +00001257 FD_SET(tty, &rfds);
1258 nfds = nfds < tty ? tty : nfds;
1259
1260 if (in >= 0) {
1261 FD_SET(in, &rfds);
1262 nfds = nfds < in ? in : nfds;
1263 }
1264
1265 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1266 if (nfds < 0)
1267 break;
1268
1269 if (FD_ISSET(tty, &rfds)) {
1270 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
1271 if (rc)
1272 break;
1273 }
1274
Marek Behúnf30cb0d2021-09-24 23:06:39 +02001275 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001276 rc = kwboot_term_pipe(in, tty, quit, &s);
1277 if (rc)
1278 break;
1279 }
1280 } while (quit[s] != 0);
1281
Pali Rohárec0fe5b2021-07-23 11:14:18 +02001282 if (in >= 0)
1283 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +02001284 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001285out:
1286 return rc;
1287}
1288
1289static void *
Pali Rohár04ced022021-09-24 23:07:03 +02001290kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovd131ad62012-05-27 11:44:51 +00001291{
Pali Rohárddc04fa2021-09-24 23:06:55 +02001292 int rc, fd;
Luka Perkovd131ad62012-05-27 11:44:51 +00001293 struct stat st;
1294 void *img;
Pali Rohár04ced022021-09-24 23:07:03 +02001295 off_t tot;
Luka Perkovd131ad62012-05-27 11:44:51 +00001296
1297 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001298 img = NULL;
1299
1300 fd = open(path, O_RDONLY);
1301 if (fd < 0)
1302 goto out;
1303
1304 rc = fstat(fd, &st);
1305 if (rc)
1306 goto out;
1307
Pali Rohár04ced022021-09-24 23:07:03 +02001308 img = malloc(st.st_size + reserve);
1309 if (!img)
Luka Perkovd131ad62012-05-27 11:44:51 +00001310 goto out;
Pali Rohár04ced022021-09-24 23:07:03 +02001311
1312 tot = 0;
1313 while (tot < st.st_size) {
1314 ssize_t rd = read(fd, img + tot, st.st_size - tot);
1315
1316 if (rd < 0)
1317 goto out;
1318
1319 tot += rd;
1320
1321 if (!rd && tot < st.st_size) {
1322 errno = EIO;
1323 goto out;
1324 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001325 }
1326
1327 rc = 0;
1328 *size = st.st_size;
1329out:
1330 if (rc && img) {
Pali Rohár04ced022021-09-24 23:07:03 +02001331 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001332 img = NULL;
1333 }
1334 if (fd >= 0)
1335 close(fd);
1336
1337 return img;
1338}
1339
1340static uint8_t
Marek Behúnfe2fd732021-09-24 23:07:01 +02001341kwboot_hdr_csum8(const void *hdr)
Luka Perkovd131ad62012-05-27 11:44:51 +00001342{
Marek Behúnfe2fd732021-09-24 23:07:01 +02001343 const uint8_t *data = hdr;
1344 uint8_t csum;
1345 size_t size;
1346
1347 size = kwbheader_size_for_csum(hdr);
Luka Perkovd131ad62012-05-27 11:44:51 +00001348
1349 for (csum = 0; size-- > 0; data++)
1350 csum += *data;
1351
1352 return csum;
1353}
1354
Pali Rohárad9a3ac2021-10-25 15:12:55 +02001355static uint32_t *
1356kwboot_img_csum32_ptr(void *img)
1357{
1358 struct main_hdr_v1 *hdr = img;
1359 uint32_t datasz;
1360
1361 datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1362
1363 return img + le32_to_cpu(hdr->srcaddr) + datasz;
1364}
1365
1366static uint32_t
1367kwboot_img_csum32(const void *img)
1368{
1369 const struct main_hdr_v1 *hdr = img;
1370 uint32_t datasz, csum = 0;
1371 const uint32_t *data;
1372
1373 datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
1374 if (datasz % sizeof(uint32_t))
1375 return 0;
1376
1377 data = img + le32_to_cpu(hdr->srcaddr);
1378 while (datasz > 0) {
1379 csum += le32_to_cpu(*data++);
1380 datasz -= 4;
1381 }
1382
1383 return cpu_to_le32(csum);
1384}
1385
Luka Perkovd131ad62012-05-27 11:44:51 +00001386static int
Pali Rohár550c9302021-09-24 23:06:57 +02001387kwboot_img_is_secure(void *img)
1388{
1389 struct opt_hdr_v1 *ohdr;
1390
1391 for_each_opt_hdr_v1 (ohdr, img)
1392 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1393 return 1;
1394
1395 return 0;
1396}
1397
Pali Rohárca272042021-09-24 23:07:05 +02001398static void *
Pali Rohár063cb352021-10-25 15:12:56 +02001399kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohárca272042021-09-24 23:07:05 +02001400{
Pali Rohárca272042021-09-24 23:07:05 +02001401 struct main_hdr_v1 *hdr = img;
Pali Rohár063cb352021-10-25 15:12:56 +02001402 void *result;
Pali Rohárca272042021-09-24 23:07:05 +02001403
Pali Rohár063cb352021-10-25 15:12:56 +02001404 /*
1405 * 32-bit checksum comes after end of image code, so we will be putting
1406 * new code there. So we get this pointer and then increase data size
1407 * (since increasing data size changes kwboot_img_csum32_ptr() return
1408 * value).
1409 */
1410 result = kwboot_img_csum32_ptr(img);
Pali Rohárca272042021-09-24 23:07:05 +02001411 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Rohár063cb352021-10-25 15:12:56 +02001412 *size += grow;
Pali Rohárca272042021-09-24 23:07:05 +02001413
Pali Rohár063cb352021-10-25 15:12:56 +02001414 return result;
Pali Rohárca272042021-09-24 23:07:05 +02001415}
1416
Pali Rohár04ced022021-09-24 23:07:03 +02001417static void
1418kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1419{
1420 uint32_t hdrsz, datasz, srcaddr;
1421 struct main_hdr_v1 *hdr = img;
Pali Rohárd656f5a2021-10-25 15:13:02 +02001422 struct opt_hdr_v1 *ohdr;
Pali Rohár04ced022021-09-24 23:07:03 +02001423 uint8_t *data;
1424
1425 srcaddr = le32_to_cpu(hdr->srcaddr);
1426
Pali Rohárd656f5a2021-10-25 15:13:02 +02001427 /* calculate real used space in kwbimage header */
1428 if (kwbimage_version(img) == 0) {
1429 hdrsz = kwbheader_size(img);
1430 } else {
1431 hdrsz = sizeof(*hdr);
1432 for_each_opt_hdr_v1 (ohdr, hdr)
1433 hdrsz += opt_hdr_v1_size(ohdr);
1434 }
1435
Pali Rohár04ced022021-09-24 23:07:03 +02001436 data = (uint8_t *)img + srcaddr;
1437 datasz = *size - srcaddr;
1438
1439 /* only move data if there is not enough space */
1440 if (hdrsz + grow > srcaddr) {
1441 size_t need = hdrsz + grow - srcaddr;
1442
1443 /* move data by enough bytes */
1444 memmove(data + need, data, datasz);
1445
1446 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1447 *size += need;
1448 }
1449
1450 if (kwbimage_version(img) == 1) {
1451 hdrsz += grow;
Pali Rohárd656f5a2021-10-25 15:13:02 +02001452 if (hdrsz > kwbheader_size(img)) {
1453 hdr->headersz_msb = hdrsz >> 16;
1454 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1455 }
Pali Rohár04ced022021-09-24 23:07:03 +02001456 }
1457}
1458
Pali Rohárca272042021-09-24 23:07:05 +02001459static void *
1460kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1461{
1462 struct main_hdr_v1 *hdr = img;
1463 struct opt_hdr_v1 *ohdr;
Pali Rohára85a71d2021-10-21 16:46:06 +02001464 uint32_t num_args;
1465 uint32_t offset;
Pali Rohárca272042021-09-24 23:07:05 +02001466 uint32_t ohdrsz;
Pali Roháre511cc32021-10-25 15:13:01 +02001467 uint8_t *prev_ext;
Pali Rohárca272042021-09-24 23:07:05 +02001468
Pali Rohár44691032022-01-12 18:20:52 +01001469 if (hdr->ext) {
Pali Rohárca272042021-09-24 23:07:05 +02001470 for_each_opt_hdr_v1 (ohdr, img)
1471 if (opt_hdr_v1_next(ohdr) == NULL)
1472 break;
1473
Pali Roháre511cc32021-10-25 15:13:01 +02001474 prev_ext = opt_hdr_v1_ext(ohdr);
1475 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohárca272042021-09-24 23:07:05 +02001476 } else {
Pali Rohárca272042021-09-24 23:07:05 +02001477 ohdr = (void *)(hdr + 1);
Pali Roháre511cc32021-10-25 15:13:01 +02001478 prev_ext = &hdr->ext;
Pali Rohárca272042021-09-24 23:07:05 +02001479 }
1480
Pali Rohára85a71d2021-10-21 16:46:06 +02001481 /*
1482 * ARM executable code inside the BIN header on some mvebu platforms
1483 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
1484 * This requirement can be met by inserting dummy arguments into
1485 * BIN header, if needed.
1486 */
1487 offset = &ohdr->data[4] - (char *)img;
1488 num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
1489
1490 ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
1491 kwboot_img_grow_hdr(hdr, size, ohdrsz);
1492
Pali Rohár44691032022-01-12 18:20:52 +01001493 *prev_ext = 1;
Pali Roháre511cc32021-10-25 15:13:01 +02001494
Pali Rohárca272042021-09-24 23:07:05 +02001495 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1496 ohdr->headersz_msb = ohdrsz >> 16;
1497 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1498
1499 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
Pali Rohára85a71d2021-10-21 16:46:06 +02001500 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohárca272042021-09-24 23:07:05 +02001501
Pali Rohára85a71d2021-10-21 16:46:06 +02001502 return &ohdr->data[4 + 4 * num_args];
Pali Rohárca272042021-09-24 23:07:05 +02001503}
1504
1505static void
Pali Rohár8dbe0272021-10-27 20:57:02 +02001506_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Rohár063cb352021-10-25 15:12:56 +02001507 int old_baud, int new_baud)
Pali Rohárca272042021-09-24 23:07:05 +02001508{
Pali Rohár063cb352021-10-25 15:12:56 +02001509 struct main_hdr_v1 *hdr = img;
Pali Rohár8dbe0272021-10-27 20:57:02 +02001510 uint32_t orig_datasz;
1511 uint32_t codesz;
Pali Rohár063cb352021-10-25 15:12:56 +02001512 uint8_t *code;
Pali Rohárca272042021-09-24 23:07:05 +02001513
Pali Rohár8dbe0272021-10-27 20:57:02 +02001514 if (for_data) {
Pali Rohár063cb352021-10-25 15:12:56 +02001515 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1516
Pali Rohár8dbe0272021-10-27 20:57:02 +02001517 codesz = sizeof(kwboot_baud_code) +
1518 sizeof(kwboot_baud_code_data_jump);
1519 code = kwboot_img_grow_data_right(img, size, codesz);
Pali Rohár063cb352021-10-25 15:12:56 +02001520 } else {
Pali Rohár8dbe0272021-10-27 20:57:02 +02001521 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1522 sizeof(kwboot_baud_code) +
1523 sizeof(kwboot_baud_code_binhdr_post);
Pali Rohár063cb352021-10-25 15:12:56 +02001524 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár8dbe0272021-10-27 20:57:02 +02001525
1526 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1527 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1528 code += codesz;
Pali Rohárca272042021-09-24 23:07:05 +02001529 }
1530
Pali Rohár8dbe0272021-10-27 20:57:02 +02001531 codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
1532 memcpy(code, kwboot_baud_code, codesz);
1533 code += codesz;
1534 *(uint32_t *)code = cpu_to_le32(old_baud);
1535 code += sizeof(uint32_t);
1536 *(uint32_t *)code = cpu_to_le32(new_baud);
1537 code += sizeof(uint32_t);
1538
1539 if (for_data) {
1540 codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
1541 memcpy(code, kwboot_baud_code_data_jump, codesz);
1542 code += codesz;
1543 *(uint32_t *)code = hdr->execaddr;
1544 code += sizeof(uint32_t);
1545 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
1546 } else {
1547 codesz = sizeof(kwboot_baud_code_binhdr_post);
1548 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1549 code += codesz;
1550 }
Pali Rohárca272042021-09-24 23:07:05 +02001551}
1552
Pali Rohár550c9302021-09-24 23:06:57 +02001553static int
Pali Rohárca272042021-09-24 23:07:05 +02001554kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001555{
Stefan Roesee29f1db2015-09-29 09:19:59 +02001556 struct main_hdr_v1 *hdr;
Pali Rohár792e4232021-09-24 23:06:58 +02001557 uint32_t srcaddr;
Luka Perkovd131ad62012-05-27 11:44:51 +00001558 uint8_t csum;
Marek Behún5c8f8122021-09-24 23:07:04 +02001559 size_t hdrsz;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001560 int image_ver;
Pali Rohár550c9302021-09-24 23:06:57 +02001561 int is_secure;
Luka Perkovd131ad62012-05-27 11:44:51 +00001562
Luka Perkovd131ad62012-05-27 11:44:51 +00001563 hdr = img;
1564
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001565 if (*size < sizeof(struct main_hdr_v1))
1566 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001567
Marek Behúnacb0b382021-09-24 23:07:00 +02001568 image_ver = kwbimage_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +02001569 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +02001570 fprintf(stderr, "Invalid image header version\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001571 goto err;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001572 }
1573
Marek Behúnfe2fd732021-09-24 23:07:01 +02001574 hdrsz = kwbheader_size(hdr);
Stefan Roesee29f1db2015-09-29 09:19:59 +02001575
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001576 if (*size < hdrsz)
1577 goto err;
Pali Rohár825a2ca2021-07-23 11:14:21 +02001578
Marek Behúnfe2fd732021-09-24 23:07:01 +02001579 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001580 if (csum != hdr->checksum)
1581 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001582
Pali Rohár792e4232021-09-24 23:06:58 +02001583 srcaddr = le32_to_cpu(hdr->srcaddr);
1584
1585 switch (hdr->blockid) {
1586 case IBR_HDR_SATA_ID:
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001587 if (srcaddr < 1)
1588 goto err;
1589
Pali Rohár792e4232021-09-24 23:06:58 +02001590 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
1591 break;
1592
1593 case IBR_HDR_SDIO_ID:
1594 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
1595 break;
1596
1597 case IBR_HDR_PEX_ID:
1598 if (srcaddr == 0xFFFFFFFF)
1599 hdr->srcaddr = cpu_to_le32(hdrsz);
1600 break;
Pali Rohárf2c644e2021-09-24 23:06:59 +02001601
1602 case IBR_HDR_SPI_ID:
1603 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1604 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1605 hdr->destaddr = cpu_to_le32(0x00800000);
1606 hdr->execaddr = cpu_to_le32(0x00800000);
1607 }
1608 break;
Pali Rohár792e4232021-09-24 23:06:58 +02001609 }
1610
Pali Rohár04ced022021-09-24 23:07:03 +02001611 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001612 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1613 goto err;
Pali Rohár04ced022021-09-24 23:07:03 +02001614
Pali Rohárad9a3ac2021-10-25 15:12:55 +02001615 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1616 goto err;
1617
Pali Rohár550c9302021-09-24 23:06:57 +02001618 is_secure = kwboot_img_is_secure(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001619
Pali Rohár550c9302021-09-24 23:06:57 +02001620 if (hdr->blockid != IBR_HDR_UART_ID) {
1621 if (is_secure) {
1622 fprintf(stderr,
1623 "Image has secure header with signature for non-UART booting\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001624 goto err;
Pali Rohár550c9302021-09-24 23:06:57 +02001625 }
1626
1627 kwboot_printv("Patching image boot signature to UART\n");
1628 hdr->blockid = IBR_HDR_UART_ID;
1629 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001630
Pali Rohár0089f612021-10-22 12:37:47 +02001631 if (!is_secure) {
Pali Rohár4bebab62021-10-25 15:12:58 +02001632 if (image_ver == 1) {
1633 /*
1634 * Tell BootROM to send BootROM messages to UART port
1635 * number 0 (used also for UART booting) with default
1636 * baudrate (which should be 115200) and do not touch
1637 * UART MPP configuration.
1638 */
Pali Rohárffccee22022-01-25 18:13:13 +01001639 hdr->flags |= 0x1;
Pali Rohár4bebab62021-10-25 15:12:58 +02001640 hdr->options &= ~0x1F;
1641 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
1642 hdr->options |= 0 << 3;
1643 }
Pali Rohár0089f612021-10-22 12:37:47 +02001644 if (image_ver == 0)
1645 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
1646 hdr->nandpagesize = 0;
1647 }
1648
Pali Rohárca272042021-09-24 23:07:05 +02001649 if (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +02001650 if (image_ver == 0) {
1651 fprintf(stderr,
1652 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001653 goto err;
Pali Rohárca272042021-09-24 23:07:05 +02001654 }
1655
1656 if (is_secure) {
1657 fprintf(stderr,
1658 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001659 goto err;
Pali Rohárca272042021-09-24 23:07:05 +02001660 }
1661
1662 /*
1663 * First inject code that changes the baudrate from the default
1664 * value of 115200 Bd to requested value. This code is inserted
1665 * as a new opt hdr, so it is executed by BootROM after the
1666 * header part is received.
1667 */
1668 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
1669 baudrate);
Pali Rohár063cb352021-10-25 15:12:56 +02001670 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohárca272042021-09-24 23:07:05 +02001671
1672 /*
1673 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Rohár063cb352021-10-25 15:12:56 +02001674 * This code is appended after the data part of the image, and
1675 * execaddr is changed so that it is executed before U-Boot
1676 * proper.
Pali Rohárca272042021-09-24 23:07:05 +02001677 */
1678 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Rohár063cb352021-10-25 15:12:56 +02001679 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohárca272042021-09-24 23:07:05 +02001680
Pali Rohár82c5a0a2021-10-25 15:12:57 +02001681 /* Update the 32-bit data checksum */
1682 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
1683
Pali Rohárca272042021-09-24 23:07:05 +02001684 /* recompute header size */
1685 hdrsz = kwbheader_size(hdr);
1686 }
1687
Pali Rohár04ced022021-09-24 23:07:03 +02001688 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Roháred792c22021-10-25 15:13:00 +02001689 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohár04ced022021-09-24 23:07:03 +02001690
1691 if (is_secure) {
1692 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001693 goto err;
Pali Rohár04ced022021-09-24 23:07:03 +02001694 }
1695
1696 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Roháred792c22021-10-25 15:13:00 +02001697 kwboot_img_grow_hdr(img, size, grow);
Pali Rohár04ced022021-09-24 23:07:03 +02001698 }
1699
Marek Behúnfe2fd732021-09-24 23:07:01 +02001700 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +00001701
Pali Rohár04ced022021-09-24 23:07:03 +02001702 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001703 return 0;
1704err:
1705 errno = EINVAL;
1706 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001707}
1708
1709static void
1710kwboot_usage(FILE *stream, char *progname)
1711{
1712 fprintf(stream,
Kevin Smith8669dac2016-02-16 21:28:17 +00001713 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +02001714 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +00001715 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +02001716 fprintf(stream,
1717 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roese84899e22014-10-22 12:13:21 +02001718 fprintf(stream,
1719 " -D <image>: boot <image> without preamble (Dove)\n");
1720 fprintf(stream, " -d: enter debug mode\n");
1721 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001722 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +00001723 fprintf(stream,
1724 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001725 fprintf(stream, "\n");
1726 fprintf(stream, " -t: mini terminal\n");
1727 fprintf(stream, "\n");
1728 fprintf(stream, " -B <baud>: set baud rate\n");
1729 fprintf(stream, "\n");
1730}
1731
1732int
1733main(int argc, char **argv)
1734{
1735 const char *ttypath, *imgpath;
Pali Rohárddc04fa2021-09-24 23:06:55 +02001736 int rv, rc, tty, term;
Pali Rohárc1d911f2022-03-02 11:49:20 +01001737 int bootmsg;
1738 int debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +00001739 void *img;
1740 size_t size;
Pali Rohárca272042021-09-24 23:07:05 +02001741 size_t after_img_rsv;
1742 int baudrate;
Pali Rohárc513fe42022-01-25 18:13:07 +01001743 int prev_optind;
1744 int c;
Luka Perkovd131ad62012-05-27 11:44:51 +00001745
1746 rv = 1;
1747 tty = -1;
Pali Rohárc1d911f2022-03-02 11:49:20 +01001748 bootmsg = 0;
1749 debugmsg = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001750 imgpath = NULL;
1751 img = NULL;
1752 term = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001753 size = 0;
Pali Rohárca272042021-09-24 23:07:05 +02001754 after_img_rsv = KWBOOT_XM_BLKSZ;
1755 baudrate = 115200;
Luka Perkovd131ad62012-05-27 11:44:51 +00001756
Pali Rohár75176dc2021-11-05 23:30:42 +01001757 printf("kwboot version %s\n", PLAIN_VERSION);
1758
Luka Perkovd131ad62012-05-27 11:44:51 +00001759 kwboot_verbose = isatty(STDOUT_FILENO);
1760
1761 do {
Pali Rohárc513fe42022-01-25 18:13:07 +01001762 prev_optind = optind;
1763 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +00001764 if (c < 0)
1765 break;
1766
1767 switch (c) {
1768 case 'b':
Pali Rohárc513fe42022-01-25 18:13:07 +01001769 if (imgpath || bootmsg || debugmsg)
1770 goto usage;
Pali Rohárc1d911f2022-03-02 11:49:20 +01001771 bootmsg = 1;
Pali Rohárc513fe42022-01-25 18:13:07 +01001772 if (prev_optind == optind)
1773 goto usage;
Pali Rohár9e6d71d2022-02-07 10:12:24 +01001774 if (optind < argc - 1 && argv[optind] && argv[optind][0] != '-')
Pali Rohárc513fe42022-01-25 18:13:07 +01001775 imgpath = argv[optind++];
Luka Perkovd131ad62012-05-27 11:44:51 +00001776 break;
1777
Stefan Roese84899e22014-10-22 12:13:21 +02001778 case 'D':
Pali Rohárc513fe42022-01-25 18:13:07 +01001779 if (imgpath || bootmsg || debugmsg)
1780 goto usage;
Pali Rohárc1d911f2022-03-02 11:49:20 +01001781 bootmsg = 0;
Stefan Roese84899e22014-10-22 12:13:21 +02001782 imgpath = optarg;
1783 break;
1784
1785 case 'd':
Pali Rohárc513fe42022-01-25 18:13:07 +01001786 if (imgpath || bootmsg || debugmsg)
1787 goto usage;
Pali Rohárc1d911f2022-03-02 11:49:20 +01001788 debugmsg = 1;
Stefan Roese84899e22014-10-22 12:13:21 +02001789 break;
1790
Luka Perkovd131ad62012-05-27 11:44:51 +00001791 case 'p':
Pali Rohárddc04fa2021-09-24 23:06:55 +02001792 /* nop, for backward compatibility */
Luka Perkovd131ad62012-05-27 11:44:51 +00001793 break;
1794
1795 case 't':
1796 term = 1;
1797 break;
1798
Stefan Roese84899e22014-10-22 12:13:21 +02001799 case 'a':
Stefan Roese84899e22014-10-22 12:13:21 +02001800 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1801 break;
1802
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001803 case 'q':
Pali Rohár132016e2022-03-02 11:49:19 +01001804 /* nop, for backward compatibility */
Stefan Roese1c0df9e2015-05-29 13:25:04 +02001805 break;
1806
1807 case 's':
1808 msg_rsp_timeo = atoi(optarg);
1809 break;
1810
Kevin Smith7497a6a2016-02-16 21:28:19 +00001811 case 'o':
1812 blk_rsp_timeo = atoi(optarg);
1813 break;
1814
Luka Perkovd131ad62012-05-27 11:44:51 +00001815 case 'B':
Pali Rohárca272042021-09-24 23:07:05 +02001816 baudrate = atoi(optarg);
Luka Perkovd131ad62012-05-27 11:44:51 +00001817 break;
1818
1819 case 'h':
1820 rv = 0;
1821 default:
1822 goto usage;
1823 }
1824 } while (1);
1825
Pali Rohára3c64962022-01-25 18:13:12 +01001826 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovd131ad62012-05-27 11:44:51 +00001827 goto usage;
1828
Luka Perkovd131ad62012-05-27 11:44:51 +00001829 ttypath = argv[optind++];
1830
Pali Rohárc513fe42022-01-25 18:13:07 +01001831 if (optind != argc)
1832 goto usage;
1833
Pali Rohárca272042021-09-24 23:07:05 +02001834 tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001835 if (tty < 0) {
1836 perror(ttypath);
1837 goto out;
1838 }
1839
Pali Rohárca272042021-09-24 23:07:05 +02001840 if (baudrate == 115200)
1841 /* do not change baudrate during Xmodem to the same value */
1842 baudrate = 0;
1843 else
1844 /* ensure we have enough space for baudrate change code */
Pali Rohár8dbe0272021-10-27 20:57:02 +02001845 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
1846 sizeof(kwboot_baud_code_binhdr_pre) +
Pali Rohár5923ef62021-10-25 15:12:54 +02001847 sizeof(kwboot_baud_code) +
Pali Rohár8dbe0272021-10-27 20:57:02 +02001848 sizeof(kwboot_baud_code_binhdr_post) +
1849 KWBOOT_XM_BLKSZ +
1850 sizeof(kwboot_baud_code) +
1851 sizeof(kwboot_baud_code_data_jump) +
Pali Rohár5923ef62021-10-25 15:12:54 +02001852 KWBOOT_XM_BLKSZ;
Pali Rohárca272042021-09-24 23:07:05 +02001853
Luka Perkovd131ad62012-05-27 11:44:51 +00001854 if (imgpath) {
Pali Rohárca272042021-09-24 23:07:05 +02001855 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovd131ad62012-05-27 11:44:51 +00001856 if (!img) {
1857 perror(imgpath);
1858 goto out;
1859 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001860
Pali Rohárca272042021-09-24 23:07:05 +02001861 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001862 if (rc) {
1863 fprintf(stderr, "%s: Invalid image.\n", imgpath);
1864 goto out;
1865 }
1866 }
1867
Stefan Roese84899e22014-10-22 12:13:21 +02001868 if (debugmsg) {
Pali Rohárc1d911f2022-03-02 11:49:20 +01001869 rc = kwboot_debugmsg(tty);
Stefan Roese84899e22014-10-22 12:13:21 +02001870 if (rc) {
1871 perror("debugmsg");
1872 goto out;
1873 }
Willy Tarreau3475a712018-07-03 12:10:30 -04001874 } else if (bootmsg) {
Pali Rohárc1d911f2022-03-02 11:49:20 +01001875 rc = kwboot_bootmsg(tty);
Luka Perkovd131ad62012-05-27 11:44:51 +00001876 if (rc) {
1877 perror("bootmsg");
1878 goto out;
1879 }
1880 }
1881
1882 if (img) {
Pali Rohárca272042021-09-24 23:07:05 +02001883 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00001884 if (rc) {
1885 perror("xmodem");
1886 goto out;
1887 }
1888 }
1889
1890 if (term) {
1891 rc = kwboot_terminal(tty);
1892 if (rc && !(errno == EINTR)) {
1893 perror("terminal");
1894 goto out;
1895 }
1896 }
1897
1898 rv = 0;
1899out:
1900 if (tty >= 0)
1901 close(tty);
1902
1903 if (img)
Pali Rohár04ced022021-09-24 23:07:03 +02001904 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001905
1906 return rv;
1907
1908usage:
1909 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
1910 goto out;
1911}