blob: 359b43c0d8b7ee63d1e4bbe51ee4cfc5cf9c3271 [file] [log] [blame]
Luka Perkovdff289642012-05-27 11:44:51 +00001/*
Stefan Roesebb5c4282014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
Marek Behúndb5b53e2021-09-24 23:07:13 +02003 * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
4 * Armada 39x
Luka Perkovdff289642012-05-27 11:44:51 +00005 *
6 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
Pali Roháraa98a1e2021-09-24 23:07:14 +02007 * (c) 2021 Pali Rohár <pali@kernel.org>
8 * (c) 2021 Marek Behún <marek.behun@nic.cz>
Luka Perkovdff289642012-05-27 11:44:51 +00009 *
10 * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
11 * Integrated Controller: Functional Specifications" December 2,
12 * 2008. Chapter 24.2 "BootROM Firmware".
13 */
14
Stefan Roese04ec0d32016-01-07 14:12:04 +010015#include "kwbimage.h"
16#include "mkimage.h"
Pali Rohár3c703aaf2021-09-24 23:06:42 +020017#include "version.h"
Stefan Roese04ec0d32016-01-07 14:12:04 +010018
Luka Perkovdff289642012-05-27 11:44:51 +000019#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdarg.h>
Stefan Roese04ec0d32016-01-07 14:12:04 +010023#include <image.h>
Luka Perkovdff289642012-05-27 11:44:51 +000024#include <libgen.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <unistd.h>
28#include <stdint.h>
Marek Behún2d9f2452021-09-24 23:06:52 +020029#include <time.h>
Luka Perkovdff289642012-05-27 11:44:51 +000030#include <sys/stat.h>
31
Pali Rohárfd935e92021-09-24 23:07:06 +020032#ifdef __linux__
33#include "termios_linux.h"
34#else
35#include <termios.h>
36#endif
37
Luka Perkovdff289642012-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 Roesebb5c4282014-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 Perkovdff289642012-05-27 11:44:51 +000051#define KWBOOT_MSG_REQ_DELAY 10 /* ms */
52#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
53
Stefan Roesebb5c4282014-10-22 12:13:21 +020054/* Defines known to work on Armada XP */
55#define KWBOOT_MSG_REQ_DELAY_AXP 1000 /* ms */
56#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
57
Luka Perkovdff289642012-05-27 11:44:51 +000058/*
59 * Xmodem Transfers
60 */
61
62#define SOH 1 /* sender start of block header */
63#define EOT 4 /* sender end of block transfer */
64#define ACK 6 /* target block ack */
65#define NAK 21 /* target block negative ack */
66#define CAN 24 /* target/sender transfer cancellation */
67
Pali Rohárbed18ef2021-09-24 23:06:48 +020068#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
69
Luka Perkovdff289642012-05-27 11:44:51 +000070struct kwboot_block {
71 uint8_t soh;
72 uint8_t pnum;
73 uint8_t _pnum;
Pali Rohárbed18ef2021-09-24 23:06:48 +020074 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovdff289642012-05-27 11:44:51 +000075 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +020076} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +000077
78#define KWBOOT_BLK_RSP_TIMEO 1000 /* ms */
Marek Behún2d9f2452021-09-24 23:06:52 +020079#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovdff289642012-05-27 11:44:51 +000080
Pali Rohár6303a232021-10-27 20:57:02 +020081/* ARM code to change baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +020082static unsigned char kwboot_baud_code[] = {
83 /* ; #define UART_BASE 0xd0012000 */
84 /* ; #define THR 0x00 */
85 /* ; #define DLL 0x00 */
86 /* ; #define DLH 0x04 */
87 /* ; #define LCR 0x0c */
88 /* ; #define DLAB 0x80 */
89 /* ; #define LSR 0x14 */
90 /* ; #define THRE 0x20 */
91 /* ; #define TEMT 0x40 */
92 /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
93 /* ; */
94 /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
95 /* ; const u8 *str = "$baudratechange"; */
96 /* ; u8 c; */
97 /* ; do { */
98 /* ; c = *str++; */
99 /* ; writel(UART_BASE + THR, c); */
100 /* ; } while (c); */
101 /* ; while */
102 /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
103 /* ; u32 lcr = readl(UART_BASE + LCR); */
104 /* ; writel(UART_BASE + LCR, lcr | DLAB); */
105 /* ; u8 old_dll = readl(UART_BASE + DLL); */
106 /* ; u8 old_dlh = readl(UART_BASE + DLH); */
107 /* ; u16 old_dl = old_dll | (old_dlh << 8); */
108 /* ; u32 clk = old_b * old_dl; */
109 /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
110 /* ; u8 new_dll = new_dl & 0xff; */
111 /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
112 /* ; writel(UART_BASE + DLL, new_dll); */
113 /* ; writel(UART_BASE + DLH, new_dlh); */
114 /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
Pali Rohár9e624c92021-10-27 20:57:00 +0200115 /* ; msleep(5); */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200116 /* ; return 0; */
117 /* ; } */
118
Pali Rohár2a8b7692021-09-24 23:07:05 +0200119 /* ; r0 = UART_BASE */
Pali Rohár15b16e52021-10-27 20:57:01 +0200120 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
121 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200122
123 /* ; r2 = address of preamble string */
Pali Rohár6303a232021-10-27 20:57:02 +0200124 0xc8, 0x20, 0x8f, 0xe2, /* adr r2, preamble */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200125
126 /* ; Send preamble string over UART */
127 /* .Lloop_preamble: */
128 /* */
129 /* ; Wait until Transmitter Holding is Empty */
130 /* .Lloop_thre: */
131 /* ; r1 = UART_BASE[LSR] & THRE */
132 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
133 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
134 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
135
136 /* ; Put character into Transmitter FIFO */
137 /* ; r1 = *r2++ */
138 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
139 /* ; UART_BASE[THR] = r1 */
140 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
141
142 /* ; Loop until end of preamble string */
143 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
144 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
145
146 /* ; Wait until Transmitter FIFO is Empty */
147 /* .Lloop_txempty: */
148 /* ; r1 = UART_BASE[LSR] & TEMT */
149 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
150 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
151 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
152
153 /* ; Set Divisor Latch Access Bit */
154 /* ; UART_BASE[LCR] |= DLAB */
155 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
156 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
157 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
158
159 /* ; Read current Divisor Latch */
160 /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
161 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
162 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
163 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
164 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
165 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
166 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
167 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
168
169 /* ; Read old baudrate value */
170 /* ; r2 = old_baudrate */
Pali Rohár6303a232021-10-27 20:57:02 +0200171 0x84, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200172
173 /* ; Calculate base clock */
174 /* ; r1 = r2 * r1 */
175 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
176
177 /* ; Read new baudrate value */
Pali Rohár9e624c92021-10-27 20:57:00 +0200178 /* ; r2 = new_baudrate */
Pali Rohár6303a232021-10-27 20:57:02 +0200179 0x80, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200180
181 /* ; Calculate new Divisor Latch */
182 /* ; r1 = DIV_ROUND(r1, r2) = */
183 /* ; = (r1 + r2/2) / r2 */
184 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
185 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
186 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
187 /* .Lloop_div1: */
188 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
189 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
190 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
191 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
192 /* .Lloop_div2: */
193 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
194 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
195 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
196 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
197 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
198 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
199 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
200
201 /* ; Set new Divisor Latch Low */
202 /* ; UART_BASE[DLL] = r1 & 0xff */
203 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
204 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
205 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
206
207 /* ; Set new Divisor Latch High */
208 /* ; UART_BASE[DLH] = r1>>8 & 0xff */
209 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
210 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
211 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
212
213 /* ; Clear Divisor Latch Access Bit */
214 /* ; UART_BASE[LCR] &= ~DLAB */
215 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
216 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
217 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
218
Pali Rohár9e624c92021-10-27 20:57:00 +0200219 /* ; Loop 0x2dc000 (2998272) cycles */
220 /* ; which is about 5ms on 1200 MHz CPU */
221 /* ; r1 = 0x2dc000 */
222 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200223 /* .Lloop_sleep: */
224 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
225 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
226 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
227
Pali Rohár6303a232021-10-27 20:57:02 +0200228 0x05, 0x00, 0x00, 0xea, /* b end */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200229
230 /* ; Preamble string */
231 /* preamble: */
232 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
233 0x64, 0x72, 0x61, 0x74,
234 0x65, 0x63, 0x68, 0x61,
235 0x6e, 0x67, 0x65, 0x00,
236
237 /* ; Placeholder for old baudrate value */
238 /* old_baudrate: */
239 0x00, 0x00, 0x00, 0x00, /* .word 0 */
240
241 /* ; Placeholder for new baudrate value */
242 /* new_baudrate: */
243 0x00, 0x00, 0x00, 0x00, /* .word 0 */
Pali Rohár6303a232021-10-27 20:57:02 +0200244
245 /* end: */
246};
247
248/* ARM code for storing registers for future returning back to the bootrom */
249static unsigned char kwboot_baud_code_binhdr_pre[] = {
250 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
251};
252
253/* ARM code for returning back to the bootrom */
254static unsigned char kwboot_baud_code_binhdr_post[] = {
255 /* ; Return 0 - no error */
256 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
257 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200258};
259
Pali Rohár6303a232021-10-27 20:57:02 +0200260/* ARM code for jumping to the original image exec_addr */
261static unsigned char kwboot_baud_code_data_jump[] = {
262 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */
263 /* ; Placeholder for exec_addr */
264 /* exec_addr: */
265 0x00, 0x00, 0x00, 0x00, /* .word 0 */
266};
Pali Rohár2a8b7692021-09-24 23:07:05 +0200267
268static const char kwb_baud_magic[16] = "$baudratechange";
269
Luka Perkovdff289642012-05-27 11:44:51 +0000270static int kwboot_verbose;
271
Stefan Roesebb5c4282014-10-22 12:13:21 +0200272static int msg_req_delay = KWBOOT_MSG_REQ_DELAY;
273static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +0000274static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200275
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200276static ssize_t
277kwboot_write(int fd, const char *buf, size_t len)
278{
279 size_t tot = 0;
280
281 while (tot < len) {
282 ssize_t wr = write(fd, buf + tot, len - tot);
283
284 if (wr < 0)
285 return -1;
286
287 tot += wr;
288 }
289
290 return tot;
291}
292
Luka Perkovdff289642012-05-27 11:44:51 +0000293static void
294kwboot_printv(const char *fmt, ...)
295{
296 va_list ap;
297
298 if (kwboot_verbose) {
299 va_start(ap, fmt);
300 vprintf(fmt, ap);
301 va_end(ap);
302 fflush(stdout);
303 }
304}
305
306static void
307__spinner(void)
308{
309 const char seq[] = { '-', '\\', '|', '/' };
310 const int div = 8;
311 static int state, bs;
312
313 if (state % div == 0) {
314 fputc(bs, stdout);
315 fputc(seq[state / div % sizeof(seq)], stdout);
316 fflush(stdout);
317 }
318
319 bs = '\b';
320 state++;
321}
322
323static void
324kwboot_spinner(void)
325{
326 if (kwboot_verbose)
327 __spinner();
328}
329
330static void
331__progress(int pct, char c)
332{
333 const int width = 70;
334 static const char *nl = "";
335 static int pos;
336
337 if (pos % width == 0)
338 printf("%s%3d %% [", nl, pct);
339
340 fputc(c, stdout);
341
342 nl = "]\n";
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200343 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000344
345 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200346 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000347 fputc(' ', stdout);
348 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200349 nl = "";
350 pos = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000351 }
352
353 fflush(stdout);
354
355}
356
357static void
358kwboot_progress(int _pct, char c)
359{
360 static int pct;
361
362 if (_pct != -1)
363 pct = _pct;
364
365 if (kwboot_verbose)
366 __progress(pct, c);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200367
368 if (pct == 100)
369 pct = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000370}
371
372static int
373kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
374{
375 int rc, nfds;
376 fd_set rfds;
377 struct timeval tv;
378 ssize_t n;
379
380 rc = -1;
381
382 FD_ZERO(&rfds);
383 FD_SET(fd, &rfds);
384
385 tv.tv_sec = 0;
386 tv.tv_usec = timeo * 1000;
387 if (tv.tv_usec > 1000000) {
388 tv.tv_sec += tv.tv_usec / 1000000;
389 tv.tv_usec %= 1000000;
390 }
391
392 do {
393 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
394 if (nfds < 0)
395 goto out;
396 if (!nfds) {
397 errno = ETIMEDOUT;
398 goto out;
399 }
400
401 n = read(fd, buf, len);
Willy Tarreauab7e8a12018-07-03 12:10:31 -0400402 if (n <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000403 goto out;
404
405 buf = (char *)buf + n;
406 len -= n;
407 } while (len > 0);
408
409 rc = 0;
410out:
411 return rc;
412}
413
414static int
Pali Rohárf2acbba2021-10-27 20:56:59 +0200415kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovdff289642012-05-27 11:44:51 +0000416{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200417 if (!buf)
418 return 0;
419
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200420 if (kwboot_write(fd, buf, len) < 0)
421 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000422
Pali Rohárf2acbba2021-10-27 20:56:59 +0200423 if (nodrain)
424 return 0;
425
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200426 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000427}
428
429static int
430kwboot_tty_send_char(int fd, unsigned char c)
431{
Pali Rohárf2acbba2021-10-27 20:56:59 +0200432 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovdff289642012-05-27 11:44:51 +0000433}
434
435static speed_t
Pali Rohár2a8b7692021-09-24 23:07:05 +0200436kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000437{
438 switch (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +0200439#ifdef B4000000
440 case 4000000:
441 return B4000000;
442#endif
443#ifdef B3500000
444 case 3500000:
445 return B3500000;
446#endif
447#ifdef B3000000
448 case 3000000:
449 return B3000000;
450#endif
451#ifdef B2500000
452 case 2500000:
453 return B2500000;
454#endif
455#ifdef B2000000
456 case 2000000:
457 return B2000000;
458#endif
459#ifdef B1500000
460 case 1500000:
461 return B1500000;
462#endif
463#ifdef B1152000
464 case 1152000:
465 return B1152000;
466#endif
467#ifdef B1000000
468 case 1000000:
469 return B1000000;
470#endif
471#ifdef B921600
472 case 921600:
473 return B921600;
474#endif
475#ifdef B614400
476 case 614400:
477 return B614400;
478#endif
479#ifdef B576000
480 case 576000:
481 return B576000;
482#endif
483#ifdef B500000
484 case 500000:
485 return B500000;
486#endif
487#ifdef B460800
488 case 460800:
489 return B460800;
490#endif
491#ifdef B307200
492 case 307200:
493 return B307200;
494#endif
495#ifdef B230400
496 case 230400:
497 return B230400;
498#endif
499#ifdef B153600
500 case 153600:
501 return B153600;
502#endif
503#ifdef B115200
Luka Perkovdff289642012-05-27 11:44:51 +0000504 case 115200:
505 return B115200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200506#endif
507#ifdef B76800
508 case 76800:
509 return B76800;
510#endif
511#ifdef B57600
Luka Perkovdff289642012-05-27 11:44:51 +0000512 case 57600:
513 return B57600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200514#endif
515#ifdef B38400
Luka Perkovdff289642012-05-27 11:44:51 +0000516 case 38400:
517 return B38400;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200518#endif
519#ifdef B19200
Luka Perkovdff289642012-05-27 11:44:51 +0000520 case 19200:
521 return B19200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200522#endif
523#ifdef B9600
Luka Perkovdff289642012-05-27 11:44:51 +0000524 case 9600:
525 return B9600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200526#endif
527#ifdef B4800
528 case 4800:
529 return B4800;
530#endif
531#ifdef B2400
532 case 2400:
533 return B2400;
534#endif
535#ifdef B1800
536 case 1800:
537 return B1800;
538#endif
539#ifdef B1200
540 case 1200:
541 return B1200;
542#endif
543#ifdef B600
544 case 600:
545 return B600;
546#endif
547#ifdef B300
548 case 300:
549 return B300;
550#endif
551#ifdef B200
552 case 200:
553 return B200;
554#endif
555#ifdef B150
556 case 150:
557 return B150;
558#endif
559#ifdef B134
560 case 134:
561 return B134;
562#endif
563#ifdef B110
564 case 110:
565 return B110;
566#endif
567#ifdef B75
568 case 75:
569 return B75;
570#endif
571#ifdef B50
572 case 50:
573 return B50;
574#endif
575 default:
Pali Rohárfd935e92021-09-24 23:07:06 +0200576#ifdef BOTHER
577 return BOTHER;
578#else
Pali Rohár2a8b7692021-09-24 23:07:05 +0200579 return B0;
Pali Rohárfd935e92021-09-24 23:07:06 +0200580#endif
Luka Perkovdff289642012-05-27 11:44:51 +0000581 }
Pali Rohár2a8b7692021-09-24 23:07:05 +0200582}
583
584static int
Marek Behún67835492021-09-24 23:07:07 +0200585_is_within_tolerance(int value, int reference, int tolerance)
586{
587 return 100 * value >= reference * (100 - tolerance) &&
588 100 * value <= reference * (100 + tolerance);
589}
590
591static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200592kwboot_tty_change_baudrate(int fd, int baudrate)
593{
594 struct termios tio;
595 speed_t speed;
596 int rc;
597
598 rc = tcgetattr(fd, &tio);
599 if (rc)
600 return rc;
601
602 speed = kwboot_tty_baudrate_to_speed(baudrate);
603 if (speed == B0) {
604 errno = EINVAL;
605 return -1;
606 }
607
Pali Rohárfd935e92021-09-24 23:07:06 +0200608#ifdef BOTHER
609 if (speed == BOTHER)
610 tio.c_ospeed = tio.c_ispeed = baudrate;
611#endif
612
Pali Rohár2a8b7692021-09-24 23:07:05 +0200613 rc = cfsetospeed(&tio, speed);
614 if (rc)
615 return rc;
616
617 rc = cfsetispeed(&tio, speed);
618 if (rc)
619 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000620
Pali Rohár2a8b7692021-09-24 23:07:05 +0200621 rc = tcsetattr(fd, TCSANOW, &tio);
622 if (rc)
623 return rc;
624
Marek Behún67835492021-09-24 23:07:07 +0200625 rc = tcgetattr(fd, &tio);
626 if (rc)
627 return rc;
628
629 if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed)
630 goto baud_fail;
631
632#ifdef BOTHER
633 /*
634 * Check whether set baudrate is within 3% tolerance.
635 * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed
636 * with real values.
637 */
638 if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3))
639 goto baud_fail;
640
641 if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3))
642 goto baud_fail;
643#endif
644
Pali Rohár2a8b7692021-09-24 23:07:05 +0200645 return 0;
Marek Behún67835492021-09-24 23:07:07 +0200646
647baud_fail:
648 fprintf(stderr, "Could not set baudrate to requested value\n");
649 errno = EINVAL;
650 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000651}
652
653static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200654kwboot_open_tty(const char *path, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000655{
Pali Rohár2bffe242021-09-24 23:07:10 +0200656 int rc, fd, flags;
Luka Perkovdff289642012-05-27 11:44:51 +0000657 struct termios tio;
658
659 rc = -1;
660
Marek Behún6c598c32021-09-24 23:07:11 +0200661 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovdff289642012-05-27 11:44:51 +0000662 if (fd < 0)
663 goto out;
664
Pali Rohár909a0b92021-09-24 23:07:08 +0200665 rc = tcgetattr(fd, &tio);
666 if (rc)
667 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +0000668
Pali Rohár909a0b92021-09-24 23:07:08 +0200669 cfmakeraw(&tio);
Marek Behún6c598c32021-09-24 23:07:11 +0200670 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár40003a02021-10-25 15:12:53 +0200671 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovdff289642012-05-27 11:44:51 +0000672 tio.c_cc[VMIN] = 1;
Pali Rohár12095b22021-09-24 23:07:09 +0200673 tio.c_cc[VTIME] = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000674
Luka Perkovdff289642012-05-27 11:44:51 +0000675 rc = tcsetattr(fd, TCSANOW, &tio);
676 if (rc)
677 goto out;
678
Pali Rohár2bffe242021-09-24 23:07:10 +0200679 flags = fcntl(fd, F_GETFL);
680 if (flags < 0)
681 goto out;
682
683 rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY);
684 if (rc)
685 goto out;
686
Pali Rohár2a8b7692021-09-24 23:07:05 +0200687 rc = kwboot_tty_change_baudrate(fd, baudrate);
688 if (rc)
689 goto out;
690
Luka Perkovdff289642012-05-27 11:44:51 +0000691 rc = fd;
692out:
693 if (rc < 0) {
694 if (fd >= 0)
695 close(fd);
696 }
697
698 return rc;
699}
700
701static int
702kwboot_bootmsg(int tty, void *msg)
703{
704 int rc;
705 char c;
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300706 int count;
Luka Perkovdff289642012-05-27 11:44:51 +0000707
Stefan Roesebb5c4282014-10-22 12:13:21 +0200708 if (msg == NULL)
709 kwboot_printv("Please reboot the target into UART boot mode...");
710 else
711 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovdff289642012-05-27 11:44:51 +0000712
713 do {
714 rc = tcflush(tty, TCIOFLUSH);
715 if (rc)
716 break;
717
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300718 for (count = 0; count < 128; count++) {
Pali Rohárf2acbba2021-10-27 20:56:59 +0200719 rc = kwboot_tty_send(tty, msg, 8, 0);
Jon Nettleton0e428ee2018-08-13 18:24:38 +0300720 if (rc) {
721 usleep(msg_req_delay * 1000);
722 continue;
723 }
Luka Perkovdff289642012-05-27 11:44:51 +0000724 }
725
Stefan Roesebb5c4282014-10-22 12:13:21 +0200726 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
Luka Perkovdff289642012-05-27 11:44:51 +0000727
728 kwboot_spinner();
729
730 } while (rc || c != NAK);
731
732 kwboot_printv("\n");
733
734 return rc;
735}
736
737static int
Stefan Roesebb5c4282014-10-22 12:13:21 +0200738kwboot_debugmsg(int tty, void *msg)
739{
740 int rc;
741
742 kwboot_printv("Sending debug message. Please reboot the target...");
743
744 do {
745 char buf[16];
746
747 rc = tcflush(tty, TCIOFLUSH);
748 if (rc)
749 break;
750
Pali Rohárf2acbba2021-10-27 20:56:59 +0200751 rc = kwboot_tty_send(tty, msg, 8, 0);
Stefan Roesebb5c4282014-10-22 12:13:21 +0200752 if (rc) {
753 usleep(msg_req_delay * 1000);
754 continue;
755 }
756
757 rc = kwboot_tty_recv(tty, buf, 16, msg_rsp_timeo);
758
759 kwboot_spinner();
760
761 } while (rc);
762
763 kwboot_printv("\n");
764
765 return rc;
766}
767
Pali Rohár58cf04de2021-09-24 23:06:44 +0200768static size_t
Luka Perkovdff289642012-05-27 11:44:51 +0000769kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
770 size_t size, int pnum)
771{
Marek Behúnedb63242021-09-24 23:06:45 +0200772 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +0000773
Stefan Roesebb5c4282014-10-22 12:13:21 +0200774 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +0000775 block->pnum = pnum;
776 block->_pnum = ~block->pnum;
777
Pali Rohárbed18ef2021-09-24 23:06:48 +0200778 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovdff289642012-05-27 11:44:51 +0000779 memcpy(&block->data[0], data, n);
Pali Rohárbed18ef2021-09-24 23:06:48 +0200780 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovdff289642012-05-27 11:44:51 +0000781
782 block->csum = 0;
783 for (i = 0; i < n; i++)
784 block->csum += block->data[i];
785
786 return n;
787}
788
Marek Behún2d9f2452021-09-24 23:06:52 +0200789static uint64_t
790_now(void)
791{
792 struct timespec ts;
793
794 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
795 static int err_print;
796
797 if (!err_print) {
798 perror("clock_gettime() does not work");
799 err_print = 1;
800 }
801
802 /* this will just make the timeout not work */
803 return -1ULL;
804 }
805
806 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
807}
808
Luka Perkovdff289642012-05-27 11:44:51 +0000809static int
Marek Behúneabacb82021-09-24 23:06:49 +0200810_is_xm_reply(char c)
811{
812 return c == ACK || c == NAK || c == CAN;
813}
814
815static int
Pali Rohár42c61a42021-09-24 23:06:54 +0200816_xm_reply_to_error(int c)
817{
818 int rc = -1;
819
820 switch (c) {
821 case ACK:
822 rc = 0;
823 break;
824 case NAK:
825 errno = EBADMSG;
826 break;
827 case CAN:
828 errno = ECANCELED;
829 break;
830 default:
831 errno = EPROTO;
832 break;
833 }
834
835 return rc;
836}
837
838static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200839kwboot_baud_magic_handle(int fd, char c, int baudrate)
840{
841 static size_t rcv_len;
842
843 if (rcv_len < sizeof(kwb_baud_magic)) {
844 /* try to recognize whole magic word */
845 if (c == kwb_baud_magic[rcv_len]) {
846 rcv_len++;
847 } else {
848 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
849 fflush(stdout);
850 rcv_len = 0;
851 }
852 }
853
854 if (rcv_len == sizeof(kwb_baud_magic)) {
855 /* magic word received */
856 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
857
858 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
859 } else {
860 return 0;
861 }
862}
863
864static int
Pali Rohár959e8502021-10-25 15:13:04 +0200865kwboot_xm_recv_reply(int fd, char *c, int nak_on_non_xm,
866 int allow_non_xm, int *non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +0200867 int baudrate, int *baud_changed)
Pali Rohárd06d5202021-09-24 23:06:50 +0200868{
Marek Behún2d9f2452021-09-24 23:06:52 +0200869 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún0a3b85a2021-09-24 23:06:53 +0200870 uint64_t recv_until = _now() + timeout;
Pali Rohárd06d5202021-09-24 23:06:50 +0200871 int rc;
872
873 while (1) {
Marek Behún2d9f2452021-09-24 23:06:52 +0200874 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohárd06d5202021-09-24 23:06:50 +0200875 if (rc) {
876 if (errno != ETIMEDOUT)
877 return rc;
Marek Behún0a3b85a2021-09-24 23:06:53 +0200878 else if (allow_non_xm && *non_xm_print)
Marek Behún2d9f2452021-09-24 23:06:52 +0200879 return -1;
880 else
881 *c = NAK;
Pali Rohárd06d5202021-09-24 23:06:50 +0200882 }
883
884 /* If received xmodem reply, end. */
885 if (_is_xm_reply(*c))
886 break;
887
888 /*
Pali Rohár2a8b7692021-09-24 23:07:05 +0200889 * If receiving/printing non-xmodem text output is allowed and
890 * such a byte was received, we want to increase receiving time
891 * and either:
892 * - print the byte, if it is not part of baudrate change magic
893 * sequence while baudrate change was requested (-B option)
894 * - change baudrate
Marek Behún0a3b85a2021-09-24 23:06:53 +0200895 * Otherwise decrease timeout by time elapsed.
Pali Rohárd06d5202021-09-24 23:06:50 +0200896 */
897 if (allow_non_xm) {
Marek Behún2d9f2452021-09-24 23:06:52 +0200898 recv_until = _now() + timeout;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200899
900 if (baudrate && !*baud_changed) {
901 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
902 if (rc == 1)
903 *baud_changed = 1;
904 else if (!rc)
905 *non_xm_print = 1;
906 else
907 return rc;
908 } else if (!baudrate || !*baud_changed) {
909 putchar(*c);
910 fflush(stdout);
911 *non_xm_print = 1;
912 }
Marek Behún0a3b85a2021-09-24 23:06:53 +0200913 } else {
Pali Rohár959e8502021-10-25 15:13:04 +0200914 if (nak_on_non_xm) {
915 *c = NAK;
916 break;
917 }
Marek Behún0a3b85a2021-09-24 23:06:53 +0200918 timeout = recv_until - _now();
919 if (timeout < 0) {
920 errno = ETIMEDOUT;
921 return -1;
922 }
Pali Rohárd06d5202021-09-24 23:06:50 +0200923 }
924 }
925
926 return 0;
927}
928
929static int
930kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár2a8b7692021-09-24 23:07:05 +0200931 int *done_print, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000932{
Pali Rohár2a8b7692021-09-24 23:07:05 +0200933 int non_xm_print, baud_changed;
934 int rc, err, retries;
Luka Perkovdff289642012-05-27 11:44:51 +0000935 char c;
936
Pali Rohárd06d5202021-09-24 23:06:50 +0200937 *done_print = 0;
Pali Rohár6daa5612021-10-27 20:56:58 +0200938 non_xm_print = 0;
939 baud_changed = 0;
Pali Rohárd06d5202021-09-24 23:06:50 +0200940
Pali Roháre665c6a2021-10-25 15:13:03 +0200941 retries = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000942 do {
Pali Rohárf2acbba2021-10-27 20:56:59 +0200943 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovdff289642012-05-27 11:44:51 +0000944 if (rc)
Pali Rohár2c02a342021-09-24 23:06:43 +0200945 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000946
Pali Rohárd06d5202021-09-24 23:06:50 +0200947 if (allow_non_xm && !*done_print) {
948 kwboot_progress(100, '.');
949 kwboot_printv("Done\n");
950 *done_print = 1;
951 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200952
Pali Rohár959e8502021-10-25 15:13:04 +0200953 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
954 allow_non_xm, &non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +0200955 baudrate, &baud_changed);
Pali Rohárd06d5202021-09-24 23:06:50 +0200956 if (rc)
Pali Rohár2a8b7692021-09-24 23:07:05 +0200957 goto can;
Luka Perkovdff289642012-05-27 11:44:51 +0000958
Pali Rohárd06d5202021-09-24 23:06:50 +0200959 if (!allow_non_xm && c != ACK)
Luka Perkovdff289642012-05-27 11:44:51 +0000960 kwboot_progress(-1, '+');
Pali Roháre665c6a2021-10-25 15:13:03 +0200961 } while (c == NAK && retries++ < 16);
Luka Perkovdff289642012-05-27 11:44:51 +0000962
Marek Behúnbcc5e042021-09-24 23:06:51 +0200963 if (non_xm_print)
964 kwboot_printv("\n");
965
Pali Rohár2a8b7692021-09-24 23:07:05 +0200966 if (allow_non_xm && baudrate && !baud_changed) {
967 fprintf(stderr, "Baudrate was not changed\n");
968 rc = -1;
969 errno = EPROTO;
970 goto can;
971 }
972
Pali Rohár42c61a42021-09-24 23:06:54 +0200973 return _xm_reply_to_error(c);
Pali Rohár2a8b7692021-09-24 23:07:05 +0200974can:
975 err = errno;
976 kwboot_tty_send_char(fd, CAN);
977 kwboot_printv("\n");
978 errno = err;
979 return rc;
Pali Rohár42c61a42021-09-24 23:06:54 +0200980}
Luka Perkovdff289642012-05-27 11:44:51 +0000981
Pali Rohár42c61a42021-09-24 23:06:54 +0200982static int
983kwboot_xm_finish(int fd)
984{
985 int rc, retries;
986 char c;
Luka Perkovdff289642012-05-27 11:44:51 +0000987
Pali Rohár42c61a42021-09-24 23:06:54 +0200988 kwboot_printv("Finishing transfer\n");
989
Pali Roháre665c6a2021-10-25 15:13:03 +0200990 retries = 0;
Pali Rohár42c61a42021-09-24 23:06:54 +0200991 do {
992 rc = kwboot_tty_send_char(fd, EOT);
993 if (rc)
994 return rc;
995
Pali Rohár959e8502021-10-25 15:13:04 +0200996 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
997 0, NULL, 0, NULL);
Pali Rohár42c61a42021-09-24 23:06:54 +0200998 if (rc)
999 return rc;
Pali Roháre665c6a2021-10-25 15:13:03 +02001000 } while (c == NAK && retries++ < 16);
Pali Rohár42c61a42021-09-24 23:06:54 +02001001
1002 return _xm_reply_to_error(c);
Luka Perkovdff289642012-05-27 11:44:51 +00001003}
1004
1005static int
Pali Rohárbed18ef2021-09-24 23:06:48 +02001006kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001007 size_t size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001008{
Pali Rohárd06d5202021-09-24 23:06:50 +02001009 int done_print = 0;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001010 size_t sent, left;
1011 int rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001012
Pali Rohárbed18ef2021-09-24 23:06:48 +02001013 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1014 header ? "header" : "data", size);
Luka Perkovdff289642012-05-27 11:44:51 +00001015
Pali Rohárbed18ef2021-09-24 23:06:48 +02001016 left = size;
1017 sent = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001018
Pali Rohárbed18ef2021-09-24 23:06:48 +02001019 while (sent < size) {
Luka Perkovdff289642012-05-27 11:44:51 +00001020 struct kwboot_block block;
Pali Rohárd06d5202021-09-24 23:06:50 +02001021 int last_block;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001022 size_t blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001023
Pali Rohárbed18ef2021-09-24 23:06:48 +02001024 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1025 data += blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001026
Pali Rohárd06d5202021-09-24 23:06:50 +02001027 last_block = (left <= blksz);
1028
1029 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001030 &done_print, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00001031 if (rc)
1032 goto out;
1033
Pali Rohárbed18ef2021-09-24 23:06:48 +02001034 sent += blksz;
1035 left -= blksz;
1036
Pali Rohárd06d5202021-09-24 23:06:50 +02001037 if (!done_print)
1038 kwboot_progress(sent * 100 / size, '.');
Pali Rohárbed18ef2021-09-24 23:06:48 +02001039 }
Luka Perkovdff289642012-05-27 11:44:51 +00001040
Pali Rohárd06d5202021-09-24 23:06:50 +02001041 if (!done_print)
1042 kwboot_printv("Done\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001043
Pali Rohárbed18ef2021-09-24 23:06:48 +02001044 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001045out:
Pali Rohár5ed896f2021-09-24 23:06:47 +02001046 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001047 return rc;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001048}
1049
1050static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001051kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohárbed18ef2021-09-24 23:06:48 +02001052{
1053 const uint8_t *img = _img;
1054 int rc, pnum;
1055 size_t hdrsz;
1056
Marek Behúnd1b0b032021-09-24 23:07:01 +02001057 hdrsz = kwbheader_size(img);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001058
1059 kwboot_printv("Waiting 2s and flushing tty\n");
1060 sleep(2); /* flush isn't effective without it */
1061 tcflush(tty, TCIOFLUSH);
1062
1063 pnum = 1;
1064
Pali Rohár2a8b7692021-09-24 23:07:05 +02001065 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001066 if (rc)
1067 return rc;
1068
1069 img += hdrsz;
1070 size -= hdrsz;
1071
Pali Rohár2a8b7692021-09-24 23:07:05 +02001072 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
1073 if (rc)
1074 return rc;
1075
1076 rc = kwboot_xm_finish(tty);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001077 if (rc)
1078 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001079
Pali Rohár2a8b7692021-09-24 23:07:05 +02001080 if (baudrate) {
1081 char buf[sizeof(kwb_baud_magic)];
1082
Pali Rohárddee5d82021-10-25 15:12:59 +02001083 kwboot_printv("Waiting 1s for baudrate change magic\n");
Pali Rohár2a8b7692021-09-24 23:07:05 +02001084 rc = kwboot_tty_recv(tty, buf, sizeof(buf), 1000);
1085 if (rc)
1086 return rc;
1087
1088 if (memcmp(buf, kwb_baud_magic, sizeof(buf))) {
1089 errno = EPROTO;
1090 return -1;
1091 }
1092
1093 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1094 rc = kwboot_tty_change_baudrate(tty, 115200);
1095 if (rc)
1096 return rc;
1097 }
1098
1099 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001100}
1101
1102static int
Marek Behúnea5b2b32021-09-24 23:06:40 +02001103kwboot_term_pipe(int in, int out, const char *quit, int *s)
Luka Perkovdff289642012-05-27 11:44:51 +00001104{
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001105 ssize_t nin;
Luka Perkovdff289642012-05-27 11:44:51 +00001106 char _buf[128], *buf = _buf;
1107
Pali Rohár8210ea02021-07-23 11:14:17 +02001108 nin = read(in, buf, sizeof(_buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -04001109 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +00001110 return -1;
1111
1112 if (quit) {
1113 int i;
1114
1115 for (i = 0; i < nin; i++) {
1116 if (*buf == quit[*s]) {
1117 (*s)++;
1118 if (!quit[*s])
1119 return 0;
1120 buf++;
1121 nin--;
Pali Rohár48615ba2021-07-23 11:14:20 +02001122 } else {
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001123 if (kwboot_write(out, quit, *s) < 0)
1124 return -1;
1125 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +02001126 }
Luka Perkovdff289642012-05-27 11:44:51 +00001127 }
1128 }
1129
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001130 if (kwboot_write(out, buf, nin) < 0)
1131 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001132
1133 return 0;
1134}
1135
1136static int
1137kwboot_terminal(int tty)
1138{
1139 int rc, in, s;
Marek Behúnea5b2b32021-09-24 23:06:40 +02001140 const char *quit = "\34c";
Luka Perkovdff289642012-05-27 11:44:51 +00001141 struct termios otio, tio;
1142
1143 rc = -1;
1144
1145 in = STDIN_FILENO;
1146 if (isatty(in)) {
1147 rc = tcgetattr(in, &otio);
1148 if (!rc) {
1149 tio = otio;
1150 cfmakeraw(&tio);
1151 rc = tcsetattr(in, TCSANOW, &tio);
1152 }
1153 if (rc) {
1154 perror("tcsetattr");
1155 goto out;
1156 }
1157
1158 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún6c598c32021-09-24 23:07:11 +02001159 quit[0] | 0100, quit[1]);
Luka Perkovdff289642012-05-27 11:44:51 +00001160 } else
1161 in = -1;
1162
1163 rc = 0;
1164 s = 0;
1165
1166 do {
1167 fd_set rfds;
1168 int nfds = 0;
1169
Pali Rohárc1efca42021-10-25 15:12:52 +02001170 FD_ZERO(&rfds);
Luka Perkovdff289642012-05-27 11:44:51 +00001171 FD_SET(tty, &rfds);
1172 nfds = nfds < tty ? tty : nfds;
1173
1174 if (in >= 0) {
1175 FD_SET(in, &rfds);
1176 nfds = nfds < in ? in : nfds;
1177 }
1178
1179 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1180 if (nfds < 0)
1181 break;
1182
1183 if (FD_ISSET(tty, &rfds)) {
1184 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL);
1185 if (rc)
1186 break;
1187 }
1188
Marek Behún4eb55de2021-09-24 23:06:39 +02001189 if (in >= 0 && FD_ISSET(in, &rfds)) {
Luka Perkovdff289642012-05-27 11:44:51 +00001190 rc = kwboot_term_pipe(in, tty, quit, &s);
1191 if (rc)
1192 break;
1193 }
1194 } while (quit[s] != 0);
1195
Pali Roháreafc9692021-07-23 11:14:18 +02001196 if (in >= 0)
1197 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +02001198 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001199out:
1200 return rc;
1201}
1202
1203static void *
Pali Rohárd9355bf2021-09-24 23:07:03 +02001204kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovdff289642012-05-27 11:44:51 +00001205{
Pali Rohár4d276012021-09-24 23:06:55 +02001206 int rc, fd;
Luka Perkovdff289642012-05-27 11:44:51 +00001207 struct stat st;
1208 void *img;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001209 off_t tot;
Luka Perkovdff289642012-05-27 11:44:51 +00001210
1211 rc = -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001212 img = NULL;
1213
1214 fd = open(path, O_RDONLY);
1215 if (fd < 0)
1216 goto out;
1217
1218 rc = fstat(fd, &st);
1219 if (rc)
1220 goto out;
1221
Pali Rohárd9355bf2021-09-24 23:07:03 +02001222 img = malloc(st.st_size + reserve);
1223 if (!img)
Luka Perkovdff289642012-05-27 11:44:51 +00001224 goto out;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001225
1226 tot = 0;
1227 while (tot < st.st_size) {
1228 ssize_t rd = read(fd, img + tot, st.st_size - tot);
1229
1230 if (rd < 0)
1231 goto out;
1232
1233 tot += rd;
1234
1235 if (!rd && tot < st.st_size) {
1236 errno = EIO;
1237 goto out;
1238 }
Luka Perkovdff289642012-05-27 11:44:51 +00001239 }
1240
1241 rc = 0;
1242 *size = st.st_size;
1243out:
1244 if (rc && img) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001245 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001246 img = NULL;
1247 }
1248 if (fd >= 0)
1249 close(fd);
1250
1251 return img;
1252}
1253
1254static uint8_t
Marek Behúnd1b0b032021-09-24 23:07:01 +02001255kwboot_hdr_csum8(const void *hdr)
Luka Perkovdff289642012-05-27 11:44:51 +00001256{
Marek Behúnd1b0b032021-09-24 23:07:01 +02001257 const uint8_t *data = hdr;
1258 uint8_t csum;
1259 size_t size;
1260
1261 size = kwbheader_size_for_csum(hdr);
Luka Perkovdff289642012-05-27 11:44:51 +00001262
1263 for (csum = 0; size-- > 0; data++)
1264 csum += *data;
1265
1266 return csum;
1267}
1268
Pali Rohár0b659e52021-10-25 15:12:55 +02001269static uint32_t *
1270kwboot_img_csum32_ptr(void *img)
1271{
1272 struct main_hdr_v1 *hdr = img;
1273 uint32_t datasz;
1274
1275 datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1276
1277 return img + le32_to_cpu(hdr->srcaddr) + datasz;
1278}
1279
1280static uint32_t
1281kwboot_img_csum32(const void *img)
1282{
1283 const struct main_hdr_v1 *hdr = img;
1284 uint32_t datasz, csum = 0;
1285 const uint32_t *data;
1286
1287 datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
1288 if (datasz % sizeof(uint32_t))
1289 return 0;
1290
1291 data = img + le32_to_cpu(hdr->srcaddr);
1292 while (datasz > 0) {
1293 csum += le32_to_cpu(*data++);
1294 datasz -= 4;
1295 }
1296
1297 return cpu_to_le32(csum);
1298}
1299
Luka Perkovdff289642012-05-27 11:44:51 +00001300static int
Pali Rohár5725e0c2021-09-24 23:06:57 +02001301kwboot_img_is_secure(void *img)
1302{
1303 struct opt_hdr_v1 *ohdr;
1304
1305 for_each_opt_hdr_v1 (ohdr, img)
1306 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1307 return 1;
1308
1309 return 0;
1310}
1311
Pali Rohár2a8b7692021-09-24 23:07:05 +02001312static void *
Pali Roháraed39f22021-10-25 15:12:56 +02001313kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001314{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001315 struct main_hdr_v1 *hdr = img;
Pali Roháraed39f22021-10-25 15:12:56 +02001316 void *result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001317
Pali Roháraed39f22021-10-25 15:12:56 +02001318 /*
1319 * 32-bit checksum comes after end of image code, so we will be putting
1320 * new code there. So we get this pointer and then increase data size
1321 * (since increasing data size changes kwboot_img_csum32_ptr() return
1322 * value).
1323 */
1324 result = kwboot_img_csum32_ptr(img);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001325 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Roháraed39f22021-10-25 15:12:56 +02001326 *size += grow;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001327
Pali Roháraed39f22021-10-25 15:12:56 +02001328 return result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001329}
1330
Pali Rohárd9355bf2021-09-24 23:07:03 +02001331static void
1332kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1333{
1334 uint32_t hdrsz, datasz, srcaddr;
1335 struct main_hdr_v1 *hdr = img;
Pali Rohár6fa97092021-10-25 15:13:02 +02001336 struct opt_hdr_v1 *ohdr;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001337 uint8_t *data;
1338
1339 srcaddr = le32_to_cpu(hdr->srcaddr);
1340
Pali Rohár6fa97092021-10-25 15:13:02 +02001341 /* calculate real used space in kwbimage header */
1342 if (kwbimage_version(img) == 0) {
1343 hdrsz = kwbheader_size(img);
1344 } else {
1345 hdrsz = sizeof(*hdr);
1346 for_each_opt_hdr_v1 (ohdr, hdr)
1347 hdrsz += opt_hdr_v1_size(ohdr);
1348 }
1349
Pali Rohárd9355bf2021-09-24 23:07:03 +02001350 data = (uint8_t *)img + srcaddr;
1351 datasz = *size - srcaddr;
1352
1353 /* only move data if there is not enough space */
1354 if (hdrsz + grow > srcaddr) {
1355 size_t need = hdrsz + grow - srcaddr;
1356
1357 /* move data by enough bytes */
1358 memmove(data + need, data, datasz);
1359
1360 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1361 *size += need;
1362 }
1363
1364 if (kwbimage_version(img) == 1) {
1365 hdrsz += grow;
Pali Rohár6fa97092021-10-25 15:13:02 +02001366 if (hdrsz > kwbheader_size(img)) {
1367 hdr->headersz_msb = hdrsz >> 16;
1368 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1369 }
Pali Rohárd9355bf2021-09-24 23:07:03 +02001370 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001371}
1372
1373static void *
1374kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1375{
1376 struct main_hdr_v1 *hdr = img;
1377 struct opt_hdr_v1 *ohdr;
Pali Rohár87cc3c92021-10-21 16:46:06 +02001378 uint32_t num_args;
1379 uint32_t offset;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001380 uint32_t ohdrsz;
Pali Rohár90eb9002021-10-25 15:13:01 +02001381 uint8_t *prev_ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001382
Pali Rohár2a8b7692021-09-24 23:07:05 +02001383 if (hdr->ext & 0x1) {
1384 for_each_opt_hdr_v1 (ohdr, img)
1385 if (opt_hdr_v1_next(ohdr) == NULL)
1386 break;
1387
Pali Rohár90eb9002021-10-25 15:13:01 +02001388 prev_ext = opt_hdr_v1_ext(ohdr);
1389 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001390 } else {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001391 ohdr = (void *)(hdr + 1);
Pali Rohár90eb9002021-10-25 15:13:01 +02001392 prev_ext = &hdr->ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001393 }
1394
Pali Rohár87cc3c92021-10-21 16:46:06 +02001395 /*
1396 * ARM executable code inside the BIN header on some mvebu platforms
1397 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
1398 * This requirement can be met by inserting dummy arguments into
1399 * BIN header, if needed.
1400 */
1401 offset = &ohdr->data[4] - (char *)img;
1402 num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
1403
1404 ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
1405 kwboot_img_grow_hdr(hdr, size, ohdrsz);
1406
Pali Rohár90eb9002021-10-25 15:13:01 +02001407 *prev_ext |= 1;
1408
Pali Rohár2a8b7692021-09-24 23:07:05 +02001409 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1410 ohdr->headersz_msb = ohdrsz >> 16;
1411 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1412
1413 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
Pali Rohár87cc3c92021-10-21 16:46:06 +02001414 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001415
Pali Rohár87cc3c92021-10-21 16:46:06 +02001416 return &ohdr->data[4 + 4 * num_args];
Pali Rohárd9355bf2021-09-24 23:07:03 +02001417}
1418
Pali Rohár2a8b7692021-09-24 23:07:05 +02001419static void
Pali Rohár6303a232021-10-27 20:57:02 +02001420_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Roháraed39f22021-10-25 15:12:56 +02001421 int old_baud, int new_baud)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001422{
Pali Roháraed39f22021-10-25 15:12:56 +02001423 struct main_hdr_v1 *hdr = img;
Pali Rohár6303a232021-10-27 20:57:02 +02001424 uint32_t orig_datasz;
1425 uint32_t codesz;
Pali Roháraed39f22021-10-25 15:12:56 +02001426 uint8_t *code;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001427
Pali Rohár6303a232021-10-27 20:57:02 +02001428 if (for_data) {
Pali Roháraed39f22021-10-25 15:12:56 +02001429 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1430
Pali Rohár6303a232021-10-27 20:57:02 +02001431 codesz = sizeof(kwboot_baud_code) +
1432 sizeof(kwboot_baud_code_data_jump);
1433 code = kwboot_img_grow_data_right(img, size, codesz);
1434 } else {
1435 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1436 sizeof(kwboot_baud_code) +
1437 sizeof(kwboot_baud_code_binhdr_post);
1438 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001439
Pali Rohár6303a232021-10-27 20:57:02 +02001440 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1441 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1442 code += codesz;
1443 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001444
Pali Rohár6303a232021-10-27 20:57:02 +02001445 codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
1446 memcpy(code, kwboot_baud_code, codesz);
1447 code += codesz;
1448 *(uint32_t *)code = cpu_to_le32(old_baud);
1449 code += sizeof(uint32_t);
1450 *(uint32_t *)code = cpu_to_le32(new_baud);
1451 code += sizeof(uint32_t);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001452
Pali Rohár6303a232021-10-27 20:57:02 +02001453 if (for_data) {
1454 codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
1455 memcpy(code, kwboot_baud_code_data_jump, codesz);
1456 code += codesz;
1457 *(uint32_t *)code = hdr->execaddr;
1458 code += sizeof(uint32_t);
1459 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
Pali Roháraed39f22021-10-25 15:12:56 +02001460 } else {
Pali Rohár6303a232021-10-27 20:57:02 +02001461 codesz = sizeof(kwboot_baud_code_binhdr_post);
1462 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1463 code += codesz;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001464 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001465}
1466
Pali Rohár5725e0c2021-09-24 23:06:57 +02001467static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001468kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001469{
Stefan Roesec74165d2015-09-29 09:19:59 +02001470 struct main_hdr_v1 *hdr;
Pali Rohár88255af2021-09-24 23:06:58 +02001471 uint32_t srcaddr;
Luka Perkovdff289642012-05-27 11:44:51 +00001472 uint8_t csum;
Marek Behún811821e2021-09-24 23:07:04 +02001473 size_t hdrsz;
Stefan Roesec74165d2015-09-29 09:19:59 +02001474 int image_ver;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001475 int is_secure;
Luka Perkovdff289642012-05-27 11:44:51 +00001476
Luka Perkovdff289642012-05-27 11:44:51 +00001477 hdr = img;
1478
Marek Behún3e6117e2021-09-24 23:07:12 +02001479 if (*size < sizeof(struct main_hdr_v1))
1480 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001481
Marek Behúnfa9caec2021-09-24 23:07:00 +02001482 image_ver = kwbimage_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +02001483 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +02001484 fprintf(stderr, "Invalid image header version\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001485 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001486 }
1487
Marek Behúnd1b0b032021-09-24 23:07:01 +02001488 hdrsz = kwbheader_size(hdr);
Stefan Roesec74165d2015-09-29 09:19:59 +02001489
Marek Behún3e6117e2021-09-24 23:07:12 +02001490 if (*size < hdrsz)
1491 goto err;
Pali Rohár12281162021-07-23 11:14:21 +02001492
Marek Behúnd1b0b032021-09-24 23:07:01 +02001493 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behún3e6117e2021-09-24 23:07:12 +02001494 if (csum != hdr->checksum)
1495 goto err;
Stefan Roesec74165d2015-09-29 09:19:59 +02001496
Pali Rohár88255af2021-09-24 23:06:58 +02001497 srcaddr = le32_to_cpu(hdr->srcaddr);
1498
1499 switch (hdr->blockid) {
1500 case IBR_HDR_SATA_ID:
Marek Behún3e6117e2021-09-24 23:07:12 +02001501 if (srcaddr < 1)
1502 goto err;
1503
Pali Rohár88255af2021-09-24 23:06:58 +02001504 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
1505 break;
1506
1507 case IBR_HDR_SDIO_ID:
1508 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
1509 break;
1510
1511 case IBR_HDR_PEX_ID:
1512 if (srcaddr == 0xFFFFFFFF)
1513 hdr->srcaddr = cpu_to_le32(hdrsz);
1514 break;
Pali Rohár398d4152021-09-24 23:06:59 +02001515
1516 case IBR_HDR_SPI_ID:
1517 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1518 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1519 hdr->destaddr = cpu_to_le32(0x00800000);
1520 hdr->execaddr = cpu_to_le32(0x00800000);
1521 }
1522 break;
Pali Rohár88255af2021-09-24 23:06:58 +02001523 }
1524
Pali Rohárd9355bf2021-09-24 23:07:03 +02001525 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behún3e6117e2021-09-24 23:07:12 +02001526 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1527 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001528
Pali Rohár0b659e52021-10-25 15:12:55 +02001529 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1530 goto err;
1531
Pali Rohár5725e0c2021-09-24 23:06:57 +02001532 is_secure = kwboot_img_is_secure(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001533
Pali Rohár5725e0c2021-09-24 23:06:57 +02001534 if (hdr->blockid != IBR_HDR_UART_ID) {
1535 if (is_secure) {
1536 fprintf(stderr,
1537 "Image has secure header with signature for non-UART booting\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001538 goto err;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001539 }
1540
1541 kwboot_printv("Patching image boot signature to UART\n");
1542 hdr->blockid = IBR_HDR_UART_ID;
1543 }
Luka Perkovdff289642012-05-27 11:44:51 +00001544
Pali Rohár3024ebd2021-10-22 12:37:47 +02001545 if (!is_secure) {
Pali Rohár71db3622021-10-25 15:12:58 +02001546 if (image_ver == 1) {
1547 /*
1548 * Tell BootROM to send BootROM messages to UART port
1549 * number 0 (used also for UART booting) with default
1550 * baudrate (which should be 115200) and do not touch
1551 * UART MPP configuration.
1552 */
1553 hdr->options &= ~0x1F;
1554 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
1555 hdr->options |= 0 << 3;
1556 }
Pali Rohár3024ebd2021-10-22 12:37:47 +02001557 if (image_ver == 0)
1558 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
1559 hdr->nandpagesize = 0;
1560 }
1561
Pali Rohár2a8b7692021-09-24 23:07:05 +02001562 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001563 if (image_ver == 0) {
1564 fprintf(stderr,
1565 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001566 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001567 }
1568
1569 if (is_secure) {
1570 fprintf(stderr,
1571 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001572 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001573 }
1574
1575 /*
1576 * First inject code that changes the baudrate from the default
1577 * value of 115200 Bd to requested value. This code is inserted
1578 * as a new opt hdr, so it is executed by BootROM after the
1579 * header part is received.
1580 */
1581 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
1582 baudrate);
Pali Roháraed39f22021-10-25 15:12:56 +02001583 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001584
1585 /*
1586 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Roháraed39f22021-10-25 15:12:56 +02001587 * This code is appended after the data part of the image, and
1588 * execaddr is changed so that it is executed before U-Boot
1589 * proper.
Pali Rohár2a8b7692021-09-24 23:07:05 +02001590 */
1591 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Roháraed39f22021-10-25 15:12:56 +02001592 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001593
Pali Rohárf625a222021-10-25 15:12:57 +02001594 /* Update the 32-bit data checksum */
1595 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
1596
Pali Rohár2a8b7692021-09-24 23:07:05 +02001597 /* recompute header size */
1598 hdrsz = kwbheader_size(hdr);
1599 }
1600
Pali Rohárd9355bf2021-09-24 23:07:03 +02001601 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Rohár085d9ce2021-10-25 15:13:00 +02001602 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001603
1604 if (is_secure) {
1605 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001606 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001607 }
1608
1609 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Rohár085d9ce2021-10-25 15:13:00 +02001610 kwboot_img_grow_hdr(img, size, grow);
Pali Rohárd9355bf2021-09-24 23:07:03 +02001611 }
1612
Marek Behúnd1b0b032021-09-24 23:07:01 +02001613 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +00001614
Pali Rohárd9355bf2021-09-24 23:07:03 +02001615 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behún3e6117e2021-09-24 23:07:12 +02001616 return 0;
1617err:
1618 errno = EINVAL;
1619 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001620}
1621
1622static void
1623kwboot_usage(FILE *stream, char *progname)
1624{
Pali Rohár3c703aaf2021-09-24 23:06:42 +02001625 fprintf(stream, "kwboot version %s\n", PLAIN_VERSION);
Luka Perkovdff289642012-05-27 11:44:51 +00001626 fprintf(stream,
Kevin Smith6c7c1ba2016-02-16 21:28:17 +00001627 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +02001628 progname);
Luka Perkovdff289642012-05-27 11:44:51 +00001629 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02001630 fprintf(stream,
1631 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02001632 fprintf(stream,
1633 " -D <image>: boot <image> without preamble (Dove)\n");
1634 fprintf(stream, " -d: enter debug mode\n");
1635 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roesef7f509d2015-05-29 13:25:04 +02001636 fprintf(stream, " -q <req-delay>: use specific request-delay\n");
1637 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +00001638 fprintf(stream,
1639 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001640 fprintf(stream, "\n");
1641 fprintf(stream, " -t: mini terminal\n");
1642 fprintf(stream, "\n");
1643 fprintf(stream, " -B <baud>: set baud rate\n");
1644 fprintf(stream, "\n");
1645}
1646
1647int
1648main(int argc, char **argv)
1649{
1650 const char *ttypath, *imgpath;
Pali Rohár4d276012021-09-24 23:06:55 +02001651 int rv, rc, tty, term;
Luka Perkovdff289642012-05-27 11:44:51 +00001652 void *bootmsg;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001653 void *debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +00001654 void *img;
1655 size_t size;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001656 size_t after_img_rsv;
1657 int baudrate;
Luka Perkovdff289642012-05-27 11:44:51 +00001658
1659 rv = 1;
1660 tty = -1;
1661 bootmsg = NULL;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001662 debugmsg = NULL;
Luka Perkovdff289642012-05-27 11:44:51 +00001663 imgpath = NULL;
1664 img = NULL;
1665 term = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001666 size = 0;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001667 after_img_rsv = KWBOOT_XM_BLKSZ;
1668 baudrate = 115200;
Luka Perkovdff289642012-05-27 11:44:51 +00001669
1670 kwboot_verbose = isatty(STDOUT_FILENO);
1671
1672 do {
Kevin Smith4d31a842016-02-16 21:28:19 +00001673 int c = getopt(argc, argv, "hb:ptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +00001674 if (c < 0)
1675 break;
1676
1677 switch (c) {
1678 case 'b':
1679 bootmsg = kwboot_msg_boot;
1680 imgpath = optarg;
1681 break;
1682
Stefan Roesebb5c4282014-10-22 12:13:21 +02001683 case 'D':
1684 bootmsg = NULL;
1685 imgpath = optarg;
1686 break;
1687
1688 case 'd':
1689 debugmsg = kwboot_msg_debug;
1690 break;
1691
Luka Perkovdff289642012-05-27 11:44:51 +00001692 case 'p':
Pali Rohár4d276012021-09-24 23:06:55 +02001693 /* nop, for backward compatibility */
Luka Perkovdff289642012-05-27 11:44:51 +00001694 break;
1695
1696 case 't':
1697 term = 1;
1698 break;
1699
Stefan Roesebb5c4282014-10-22 12:13:21 +02001700 case 'a':
1701 msg_req_delay = KWBOOT_MSG_REQ_DELAY_AXP;
1702 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
1703 break;
1704
Stefan Roesef7f509d2015-05-29 13:25:04 +02001705 case 'q':
1706 msg_req_delay = atoi(optarg);
1707 break;
1708
1709 case 's':
1710 msg_rsp_timeo = atoi(optarg);
1711 break;
1712
Kevin Smith4d31a842016-02-16 21:28:19 +00001713 case 'o':
1714 blk_rsp_timeo = atoi(optarg);
1715 break;
1716
Luka Perkovdff289642012-05-27 11:44:51 +00001717 case 'B':
Pali Rohár2a8b7692021-09-24 23:07:05 +02001718 baudrate = atoi(optarg);
Luka Perkovdff289642012-05-27 11:44:51 +00001719 break;
1720
1721 case 'h':
1722 rv = 0;
1723 default:
1724 goto usage;
1725 }
1726 } while (1);
1727
Stefan Roesebb5c4282014-10-22 12:13:21 +02001728 if (!bootmsg && !term && !debugmsg)
Luka Perkovdff289642012-05-27 11:44:51 +00001729 goto usage;
1730
Luka Perkovdff289642012-05-27 11:44:51 +00001731 if (argc - optind < 1)
1732 goto usage;
1733
1734 ttypath = argv[optind++];
1735
Pali Rohár2a8b7692021-09-24 23:07:05 +02001736 tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00001737 if (tty < 0) {
1738 perror(ttypath);
1739 goto out;
1740 }
1741
Pali Rohár2a8b7692021-09-24 23:07:05 +02001742 if (baudrate == 115200)
1743 /* do not change baudrate during Xmodem to the same value */
1744 baudrate = 0;
1745 else
1746 /* ensure we have enough space for baudrate change code */
Pali Rohár6303a232021-10-27 20:57:02 +02001747 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
1748 sizeof(kwboot_baud_code_binhdr_pre) +
1749 sizeof(kwboot_baud_code) +
1750 sizeof(kwboot_baud_code_binhdr_post) +
Pali Rohárd8774392021-10-25 15:12:54 +02001751 KWBOOT_XM_BLKSZ +
Pali Rohárd8774392021-10-25 15:12:54 +02001752 sizeof(kwboot_baud_code) +
Pali Rohár6303a232021-10-27 20:57:02 +02001753 sizeof(kwboot_baud_code_data_jump) +
Pali Rohárd8774392021-10-25 15:12:54 +02001754 KWBOOT_XM_BLKSZ;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001755
Luka Perkovdff289642012-05-27 11:44:51 +00001756 if (imgpath) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001757 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovdff289642012-05-27 11:44:51 +00001758 if (!img) {
1759 perror(imgpath);
1760 goto out;
1761 }
Luka Perkovdff289642012-05-27 11:44:51 +00001762
Pali Rohár2a8b7692021-09-24 23:07:05 +02001763 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00001764 if (rc) {
1765 fprintf(stderr, "%s: Invalid image.\n", imgpath);
1766 goto out;
1767 }
1768 }
1769
Stefan Roesebb5c4282014-10-22 12:13:21 +02001770 if (debugmsg) {
1771 rc = kwboot_debugmsg(tty, debugmsg);
1772 if (rc) {
1773 perror("debugmsg");
1774 goto out;
1775 }
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -04001776 } else if (bootmsg) {
Luka Perkovdff289642012-05-27 11:44:51 +00001777 rc = kwboot_bootmsg(tty, bootmsg);
1778 if (rc) {
1779 perror("bootmsg");
1780 goto out;
1781 }
1782 }
1783
1784 if (img) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001785 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00001786 if (rc) {
1787 perror("xmodem");
1788 goto out;
1789 }
1790 }
1791
1792 if (term) {
1793 rc = kwboot_terminal(tty);
1794 if (rc && !(errno == EINTR)) {
1795 perror("terminal");
1796 goto out;
1797 }
1798 }
1799
1800 rv = 0;
1801out:
1802 if (tty >= 0)
1803 close(tty);
1804
1805 if (img)
Pali Rohárd9355bf2021-09-24 23:07:03 +02001806 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001807
1808 return rv;
1809
1810usage:
1811 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
1812 goto out;
1813}