blob: 26cfe6dea6a7f9465697f992810e359d851faa20 [file] [log] [blame]
Luka Perkovdff289642012-05-27 11:44:51 +00001/*
Stefan Roesebb5c4282014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
Marek Behúndb5b53e2021-09-24 23:07:13 +02003 * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and
4 * Armada 39x
Luka Perkovdff289642012-05-27 11:44:51 +00005 *
6 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
Pali Roháraa98a1e2021-09-24 23:07:14 +02007 * (c) 2021 Pali Rohár <pali@kernel.org>
8 * (c) 2021 Marek Behún <marek.behun@nic.cz>
Luka Perkovdff289642012-05-27 11:44:51 +00009 *
10 * References: marvell.com, "88F6180, 88F6190, 88F6192, and 88F6281
11 * Integrated Controller: Functional Specifications" December 2,
12 * 2008. Chapter 24.2 "BootROM Firmware".
13 */
14
Stefan Roese04ec0d32016-01-07 14:12:04 +010015#include "kwbimage.h"
16#include "mkimage.h"
Pali Rohár3c703aaf2021-09-24 23:06:42 +020017#include "version.h"
Stefan Roese04ec0d32016-01-07 14:12:04 +010018
Luka Perkovdff289642012-05-27 11:44:51 +000019#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <stdarg.h>
Stefan Roese04ec0d32016-01-07 14:12:04 +010023#include <image.h>
Luka Perkovdff289642012-05-27 11:44:51 +000024#include <libgen.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <unistd.h>
28#include <stdint.h>
Marek Behún2d9f2452021-09-24 23:06:52 +020029#include <time.h>
Luka Perkovdff289642012-05-27 11:44:51 +000030#include <sys/stat.h>
Pali Rohár3a2e9bc2022-03-02 11:49:21 +010031#include <pthread.h>
Luka Perkovdff289642012-05-27 11:44:51 +000032
Pali Rohárfd935e92021-09-24 23:07:06 +020033#ifdef __linux__
34#include "termios_linux.h"
35#else
36#include <termios.h>
37#endif
38
Luka Perkovdff289642012-05-27 11:44:51 +000039/*
Pali Roháred3a9752022-03-02 11:49:23 +010040 * These functions are in <term.h> header file, but this header file conflicts
41 * with "termios_linux.h" header file. So declare these functions manually.
42 */
43extern int setupterm(const char *, int, int *);
44extern char *tigetstr(const char *);
45
46/*
Luka Perkovdff289642012-05-27 11:44:51 +000047 * Marvell BootROM UART Sensing
48 */
49
50static unsigned char kwboot_msg_boot[] = {
51 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
52};
53
Stefan Roesebb5c4282014-10-22 12:13:21 +020054static unsigned char kwboot_msg_debug[] = {
55 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
56};
57
58/* Defines known to work on Kirkwood */
Luka Perkovdff289642012-05-27 11:44:51 +000059#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
60
Stefan Roesebb5c4282014-10-22 12:13:21 +020061/* Defines known to work on Armada XP */
Stefan Roesebb5c4282014-10-22 12:13:21 +020062#define KWBOOT_MSG_RSP_TIMEO_AXP 1000 /* ms */
63
Luka Perkovdff289642012-05-27 11:44:51 +000064/*
65 * Xmodem Transfers
66 */
67
68#define SOH 1 /* sender start of block header */
69#define EOT 4 /* sender end of block transfer */
70#define ACK 6 /* target block ack */
71#define NAK 21 /* target block negative ack */
Luka Perkovdff289642012-05-27 11:44:51 +000072
Pali Rohárbed18ef2021-09-24 23:06:48 +020073#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
74
Luka Perkovdff289642012-05-27 11:44:51 +000075struct kwboot_block {
76 uint8_t soh;
77 uint8_t pnum;
78 uint8_t _pnum;
Pali Rohárbed18ef2021-09-24 23:06:48 +020079 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovdff289642012-05-27 11:44:51 +000080 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +020081} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +000082
Pali Rohárdef98382022-01-25 18:13:00 +010083#define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */
Marek Behún2d9f2452021-09-24 23:06:52 +020084#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovdff289642012-05-27 11:44:51 +000085
Pali Rohár6303a232021-10-27 20:57:02 +020086/* ARM code to change baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +020087static unsigned char kwboot_baud_code[] = {
88 /* ; #define UART_BASE 0xd0012000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +020089 /* ; #define DLL 0x00 */
90 /* ; #define DLH 0x04 */
91 /* ; #define LCR 0x0c */
92 /* ; #define DLAB 0x80 */
93 /* ; #define LSR 0x14 */
Pali Rohár2a8b7692021-09-24 23:07:05 +020094 /* ; #define TEMT 0x40 */
95 /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
96 /* ; */
97 /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
Pali Rohár2a8b7692021-09-24 23:07:05 +020098 /* ; while */
99 /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
100 /* ; u32 lcr = readl(UART_BASE + LCR); */
101 /* ; writel(UART_BASE + LCR, lcr | DLAB); */
102 /* ; u8 old_dll = readl(UART_BASE + DLL); */
103 /* ; u8 old_dlh = readl(UART_BASE + DLH); */
104 /* ; u16 old_dl = old_dll | (old_dlh << 8); */
105 /* ; u32 clk = old_b * old_dl; */
106 /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
107 /* ; u8 new_dll = new_dl & 0xff; */
108 /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
109 /* ; writel(UART_BASE + DLL, new_dll); */
110 /* ; writel(UART_BASE + DLH, new_dlh); */
111 /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
Pali Rohár9e624c92021-10-27 20:57:00 +0200112 /* ; msleep(5); */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200113 /* ; return 0; */
114 /* ; } */
115
Pali Rohár2a8b7692021-09-24 23:07:05 +0200116 /* ; r0 = UART_BASE */
Pali Rohár15b16e52021-10-27 20:57:01 +0200117 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
118 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200119
Pali Rohár2a8b7692021-09-24 23:07:05 +0200120 /* ; Wait until Transmitter FIFO is Empty */
121 /* .Lloop_txempty: */
122 /* ; r1 = UART_BASE[LSR] & TEMT */
123 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
124 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
125 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
126
127 /* ; Set Divisor Latch Access Bit */
128 /* ; UART_BASE[LCR] |= DLAB */
129 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
130 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
131 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
132
133 /* ; Read current Divisor Latch */
134 /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
135 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
136 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
137 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
138 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
139 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
140 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
141 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
142
143 /* ; Read old baudrate value */
144 /* ; r2 = old_baudrate */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100145 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200146
147 /* ; Calculate base clock */
148 /* ; r1 = r2 * r1 */
149 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
150
151 /* ; Read new baudrate value */
Pali Rohár9e624c92021-10-27 20:57:00 +0200152 /* ; r2 = new_baudrate */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100153 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200154
155 /* ; Calculate new Divisor Latch */
156 /* ; r1 = DIV_ROUND(r1, r2) = */
157 /* ; = (r1 + r2/2) / r2 */
158 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
159 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
160 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
161 /* .Lloop_div1: */
162 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
163 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
164 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
165 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
166 /* .Lloop_div2: */
167 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
168 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
169 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
170 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
171 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
172 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
173 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
174
175 /* ; Set new Divisor Latch Low */
176 /* ; UART_BASE[DLL] = r1 & 0xff */
177 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
178 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
179 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
180
181 /* ; Set new Divisor Latch High */
182 /* ; UART_BASE[DLH] = r1>>8 & 0xff */
183 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
184 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
185 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
186
187 /* ; Clear Divisor Latch Access Bit */
188 /* ; UART_BASE[LCR] &= ~DLAB */
189 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
190 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
191 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
192
Pali Rohár9e624c92021-10-27 20:57:00 +0200193 /* ; Loop 0x2dc000 (2998272) cycles */
194 /* ; which is about 5ms on 1200 MHz CPU */
195 /* ; r1 = 0x2dc000 */
196 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200197 /* .Lloop_sleep: */
198 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
199 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
200 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
201
Pali Rohár2d760ed2021-11-01 14:00:02 +0100202 /* ; Jump to the end of execution */
203 0x01, 0x00, 0x00, 0xea, /* b end */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200204
205 /* ; Placeholder for old baudrate value */
206 /* old_baudrate: */
207 0x00, 0x00, 0x00, 0x00, /* .word 0 */
208
209 /* ; Placeholder for new baudrate value */
210 /* new_baudrate: */
211 0x00, 0x00, 0x00, 0x00, /* .word 0 */
Pali Rohár6303a232021-10-27 20:57:02 +0200212
213 /* end: */
214};
215
Pali Rohár2d760ed2021-11-01 14:00:02 +0100216/* ARM code from binary header executed by BootROM before changing baudrate */
Pali Rohár6303a232021-10-27 20:57:02 +0200217static unsigned char kwboot_baud_code_binhdr_pre[] = {
Pali Rohár2d760ed2021-11-01 14:00:02 +0100218 /* ; #define UART_BASE 0xd0012000 */
219 /* ; #define THR 0x00 */
220 /* ; #define LSR 0x14 */
221 /* ; #define THRE 0x20 */
222 /* ; */
223 /* ; void send_preamble(void) { */
224 /* ; const u8 *str = "$baudratechange"; */
225 /* ; u8 c; */
226 /* ; do { */
227 /* ; while */
228 /* ; ((readl(UART_BASE + LSR) & THRE)); */
229 /* ; c = *str++; */
230 /* ; writel(UART_BASE + THR, c); */
231 /* ; } while (c); */
232 /* ; } */
233
234 /* ; Preserve registers for BootROM */
Pali Rohár6303a232021-10-27 20:57:02 +0200235 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100236
237 /* ; r0 = UART_BASE */
238 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
239 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
240
241 /* ; r2 = address of preamble string */
242 0x00, 0x20, 0x8f, 0xe2, /* adr r2, .Lstr_preamble */
243
244 /* ; Skip preamble data section */
245 0x03, 0x00, 0x00, 0xea, /* b .Lloop_preamble */
246
247 /* ; Preamble string */
248 /* .Lstr_preamble: */
249 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
250 0x64, 0x72, 0x61, 0x74,
251 0x65, 0x63, 0x68, 0x61,
252 0x6e, 0x67, 0x65, 0x00,
253
254 /* ; Send preamble string over UART */
255 /* .Lloop_preamble: */
256 /* */
257 /* ; Wait until Transmitter Holding is Empty */
258 /* .Lloop_thre: */
259 /* ; r1 = UART_BASE[LSR] & THRE */
260 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
261 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
262 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
263
264 /* ; Put character into Transmitter FIFO */
265 /* ; r1 = *r2++ */
266 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
267 /* ; UART_BASE[THR] = r1 */
268 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
269
270 /* ; Loop until end of preamble string */
271 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
272 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
Pali Rohár6303a232021-10-27 20:57:02 +0200273};
274
Pali Rohár2d760ed2021-11-01 14:00:02 +0100275/* ARM code for returning from binary header back to BootROM */
Pali Rohár6303a232021-10-27 20:57:02 +0200276static unsigned char kwboot_baud_code_binhdr_post[] = {
277 /* ; Return 0 - no error */
278 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
279 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200280};
281
Pali Rohár6303a232021-10-27 20:57:02 +0200282/* ARM code for jumping to the original image exec_addr */
283static unsigned char kwboot_baud_code_data_jump[] = {
284 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */
285 /* ; Placeholder for exec_addr */
286 /* exec_addr: */
287 0x00, 0x00, 0x00, 0x00, /* .word 0 */
288};
Pali Rohár2a8b7692021-09-24 23:07:05 +0200289
290static const char kwb_baud_magic[16] = "$baudratechange";
291
Luka Perkovdff289642012-05-27 11:44:51 +0000292static int kwboot_verbose;
293
Stefan Roesebb5c4282014-10-22 12:13:21 +0200294static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +0000295static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200296
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200297static ssize_t
298kwboot_write(int fd, const char *buf, size_t len)
299{
Pali Rohár5350df12022-01-25 18:13:10 +0100300 ssize_t tot = 0;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200301
302 while (tot < len) {
303 ssize_t wr = write(fd, buf + tot, len - tot);
304
Pali Rohár5350df12022-01-25 18:13:10 +0100305 if (wr < 0 && errno == EINTR)
306 continue;
307 else if (wr < 0)
308 return wr;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200309
310 tot += wr;
311 }
312
313 return tot;
314}
315
Luka Perkovdff289642012-05-27 11:44:51 +0000316static void
317kwboot_printv(const char *fmt, ...)
318{
319 va_list ap;
320
321 if (kwboot_verbose) {
322 va_start(ap, fmt);
323 vprintf(fmt, ap);
324 va_end(ap);
325 fflush(stdout);
326 }
327}
328
329static void
330__spinner(void)
331{
332 const char seq[] = { '-', '\\', '|', '/' };
333 const int div = 8;
334 static int state, bs;
335
336 if (state % div == 0) {
337 fputc(bs, stdout);
338 fputc(seq[state / div % sizeof(seq)], stdout);
339 fflush(stdout);
340 }
341
342 bs = '\b';
343 state++;
344}
345
346static void
347kwboot_spinner(void)
348{
349 if (kwboot_verbose)
350 __spinner();
351}
352
353static void
354__progress(int pct, char c)
355{
356 const int width = 70;
357 static const char *nl = "";
358 static int pos;
359
360 if (pos % width == 0)
361 printf("%s%3d %% [", nl, pct);
362
363 fputc(c, stdout);
364
365 nl = "]\n";
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200366 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000367
368 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200369 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000370 fputc(' ', stdout);
371 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200372 nl = "";
373 pos = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000374 }
375
376 fflush(stdout);
377
378}
379
380static void
381kwboot_progress(int _pct, char c)
382{
383 static int pct;
384
385 if (_pct != -1)
386 pct = _pct;
387
388 if (kwboot_verbose)
389 __progress(pct, c);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200390
391 if (pct == 100)
392 pct = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000393}
394
395static int
396kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
397{
398 int rc, nfds;
399 fd_set rfds;
400 struct timeval tv;
401 ssize_t n;
402
403 rc = -1;
404
405 FD_ZERO(&rfds);
406 FD_SET(fd, &rfds);
407
408 tv.tv_sec = 0;
409 tv.tv_usec = timeo * 1000;
410 if (tv.tv_usec > 1000000) {
411 tv.tv_sec += tv.tv_usec / 1000000;
412 tv.tv_usec %= 1000000;
413 }
414
415 do {
416 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
Pali Rohár613bd8b2022-01-25 18:13:11 +0100417 if (nfds < 0 && errno == EINTR)
418 continue;
419 else if (nfds < 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000420 goto out;
Pali Rohár613bd8b2022-01-25 18:13:11 +0100421 else if (!nfds) {
Luka Perkovdff289642012-05-27 11:44:51 +0000422 errno = ETIMEDOUT;
423 goto out;
424 }
425
426 n = read(fd, buf, len);
Pali Rohár613bd8b2022-01-25 18:13:11 +0100427 if (n < 0 && errno == EINTR)
428 continue;
429 else if (n <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000430 goto out;
431
432 buf = (char *)buf + n;
433 len -= n;
434 } while (len > 0);
435
436 rc = 0;
437out:
438 return rc;
439}
440
441static int
Pali Rohárf2acbba2021-10-27 20:56:59 +0200442kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovdff289642012-05-27 11:44:51 +0000443{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200444 if (!buf)
445 return 0;
446
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200447 if (kwboot_write(fd, buf, len) < 0)
448 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000449
Pali Rohárf2acbba2021-10-27 20:56:59 +0200450 if (nodrain)
451 return 0;
452
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200453 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000454}
455
456static int
457kwboot_tty_send_char(int fd, unsigned char c)
458{
Pali Rohárf2acbba2021-10-27 20:56:59 +0200459 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovdff289642012-05-27 11:44:51 +0000460}
461
462static speed_t
Pali Rohár2a8b7692021-09-24 23:07:05 +0200463kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000464{
465 switch (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +0200466#ifdef B4000000
467 case 4000000:
468 return B4000000;
469#endif
470#ifdef B3500000
471 case 3500000:
472 return B3500000;
473#endif
474#ifdef B3000000
475 case 3000000:
476 return B3000000;
477#endif
478#ifdef B2500000
479 case 2500000:
480 return B2500000;
481#endif
482#ifdef B2000000
483 case 2000000:
484 return B2000000;
485#endif
486#ifdef B1500000
487 case 1500000:
488 return B1500000;
489#endif
490#ifdef B1152000
491 case 1152000:
492 return B1152000;
493#endif
494#ifdef B1000000
495 case 1000000:
496 return B1000000;
497#endif
498#ifdef B921600
499 case 921600:
500 return B921600;
501#endif
502#ifdef B614400
503 case 614400:
504 return B614400;
505#endif
506#ifdef B576000
507 case 576000:
508 return B576000;
509#endif
510#ifdef B500000
511 case 500000:
512 return B500000;
513#endif
514#ifdef B460800
515 case 460800:
516 return B460800;
517#endif
518#ifdef B307200
519 case 307200:
520 return B307200;
521#endif
522#ifdef B230400
523 case 230400:
524 return B230400;
525#endif
526#ifdef B153600
527 case 153600:
528 return B153600;
529#endif
530#ifdef B115200
Luka Perkovdff289642012-05-27 11:44:51 +0000531 case 115200:
532 return B115200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200533#endif
534#ifdef B76800
535 case 76800:
536 return B76800;
537#endif
538#ifdef B57600
Luka Perkovdff289642012-05-27 11:44:51 +0000539 case 57600:
540 return B57600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200541#endif
542#ifdef B38400
Luka Perkovdff289642012-05-27 11:44:51 +0000543 case 38400:
544 return B38400;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200545#endif
546#ifdef B19200
Luka Perkovdff289642012-05-27 11:44:51 +0000547 case 19200:
548 return B19200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200549#endif
550#ifdef B9600
Luka Perkovdff289642012-05-27 11:44:51 +0000551 case 9600:
552 return B9600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200553#endif
554#ifdef B4800
555 case 4800:
556 return B4800;
557#endif
558#ifdef B2400
559 case 2400:
560 return B2400;
561#endif
562#ifdef B1800
563 case 1800:
564 return B1800;
565#endif
566#ifdef B1200
567 case 1200:
568 return B1200;
569#endif
570#ifdef B600
571 case 600:
572 return B600;
573#endif
574#ifdef B300
575 case 300:
576 return B300;
577#endif
578#ifdef B200
579 case 200:
580 return B200;
581#endif
582#ifdef B150
583 case 150:
584 return B150;
585#endif
586#ifdef B134
587 case 134:
588 return B134;
589#endif
590#ifdef B110
591 case 110:
592 return B110;
593#endif
594#ifdef B75
595 case 75:
596 return B75;
597#endif
598#ifdef B50
599 case 50:
600 return B50;
601#endif
602 default:
Pali Rohárfd935e92021-09-24 23:07:06 +0200603#ifdef BOTHER
604 return BOTHER;
605#else
Pali Rohár2a8b7692021-09-24 23:07:05 +0200606 return B0;
Pali Rohárfd935e92021-09-24 23:07:06 +0200607#endif
Luka Perkovdff289642012-05-27 11:44:51 +0000608 }
Pali Rohár2a8b7692021-09-24 23:07:05 +0200609}
610
611static int
Marek Behún67835492021-09-24 23:07:07 +0200612_is_within_tolerance(int value, int reference, int tolerance)
613{
614 return 100 * value >= reference * (100 - tolerance) &&
615 100 * value <= reference * (100 + tolerance);
616}
617
618static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200619kwboot_tty_change_baudrate(int fd, int baudrate)
620{
621 struct termios tio;
622 speed_t speed;
623 int rc;
624
625 rc = tcgetattr(fd, &tio);
626 if (rc)
627 return rc;
628
629 speed = kwboot_tty_baudrate_to_speed(baudrate);
630 if (speed == B0) {
631 errno = EINVAL;
632 return -1;
633 }
634
Pali Rohárfd935e92021-09-24 23:07:06 +0200635#ifdef BOTHER
636 if (speed == BOTHER)
637 tio.c_ospeed = tio.c_ispeed = baudrate;
638#endif
639
Pali Rohár2a8b7692021-09-24 23:07:05 +0200640 rc = cfsetospeed(&tio, speed);
641 if (rc)
642 return rc;
643
644 rc = cfsetispeed(&tio, speed);
645 if (rc)
646 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000647
Pali Rohár2a8b7692021-09-24 23:07:05 +0200648 rc = tcsetattr(fd, TCSANOW, &tio);
649 if (rc)
650 return rc;
651
Marek Behún67835492021-09-24 23:07:07 +0200652 rc = tcgetattr(fd, &tio);
653 if (rc)
654 return rc;
655
656 if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed)
657 goto baud_fail;
658
659#ifdef BOTHER
660 /*
661 * Check whether set baudrate is within 3% tolerance.
662 * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed
663 * with real values.
664 */
665 if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3))
666 goto baud_fail;
667
668 if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3))
669 goto baud_fail;
670#endif
671
Pali Rohár2a8b7692021-09-24 23:07:05 +0200672 return 0;
Marek Behún67835492021-09-24 23:07:07 +0200673
674baud_fail:
675 fprintf(stderr, "Could not set baudrate to requested value\n");
676 errno = EINVAL;
677 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000678}
679
680static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200681kwboot_open_tty(const char *path, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000682{
Pali Rohár2bffe242021-09-24 23:07:10 +0200683 int rc, fd, flags;
Luka Perkovdff289642012-05-27 11:44:51 +0000684 struct termios tio;
685
686 rc = -1;
687
Marek Behún6c598c32021-09-24 23:07:11 +0200688 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovdff289642012-05-27 11:44:51 +0000689 if (fd < 0)
690 goto out;
691
Pali Rohár909a0b92021-09-24 23:07:08 +0200692 rc = tcgetattr(fd, &tio);
693 if (rc)
694 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +0000695
Pali Rohár909a0b92021-09-24 23:07:08 +0200696 cfmakeraw(&tio);
Marek Behún6c598c32021-09-24 23:07:11 +0200697 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár40003a02021-10-25 15:12:53 +0200698 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovdff289642012-05-27 11:44:51 +0000699 tio.c_cc[VMIN] = 1;
Pali Rohár12095b22021-09-24 23:07:09 +0200700 tio.c_cc[VTIME] = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000701
Luka Perkovdff289642012-05-27 11:44:51 +0000702 rc = tcsetattr(fd, TCSANOW, &tio);
703 if (rc)
704 goto out;
705
Pali Rohár2bffe242021-09-24 23:07:10 +0200706 flags = fcntl(fd, F_GETFL);
707 if (flags < 0)
708 goto out;
709
710 rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY);
711 if (rc)
712 goto out;
713
Pali Rohár2a8b7692021-09-24 23:07:05 +0200714 rc = kwboot_tty_change_baudrate(fd, baudrate);
715 if (rc)
716 goto out;
717
Luka Perkovdff289642012-05-27 11:44:51 +0000718 rc = fd;
719out:
720 if (rc < 0) {
721 if (fd >= 0)
722 close(fd);
723 }
724
725 return rc;
726}
727
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100728static void *
729kwboot_msg_write_handler(void *arg)
730{
731 int tty = *(int *)((void **)arg)[0];
732 const void *msg = ((void **)arg)[1];
733 int rsp_timeo = msg_rsp_timeo;
734 int i, dummy_oldtype;
735
736 /* allow to cancel this thread at any time */
737 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &dummy_oldtype);
738
739 while (1) {
740 /* write 128 samples of message pattern into the output queue without waiting */
741 for (i = 0; i < 128; i++) {
742 if (kwboot_tty_send(tty, msg, 8, 1) < 0) {
743 perror("\nFailed to send message pattern");
744 exit(1);
745 }
746 }
747 /* wait until output queue is transmitted and then make pause */
748 if (tcdrain(tty) < 0) {
749 perror("\nFailed to send message pattern");
750 exit(1);
751 }
752 /* BootROM requires pause on UART after it detects message pattern */
753 usleep(rsp_timeo * 1000);
754 }
755}
756
757static int
758kwboot_msg_start_thread(pthread_t *thread, int *tty, void *msg)
759{
760 void *arg[2];
761 int rc;
762
763 arg[0] = tty;
764 arg[1] = msg;
765 rc = pthread_create(thread, NULL, kwboot_msg_write_handler, arg);
766 if (rc) {
767 errno = rc;
768 return -1;
769 }
770
771 return 0;
772}
773
774static int
775kwboot_msg_stop_thread(pthread_t thread)
776{
777 int rc;
778
779 rc = pthread_cancel(thread);
780 if (rc) {
781 errno = rc;
782 return -1;
783 }
784
785 rc = pthread_join(thread, NULL);
786 if (rc) {
787 errno = rc;
788 return -1;
789 }
790
791 return 0;
792}
793
Luka Perkovdff289642012-05-27 11:44:51 +0000794static int
Pali Rohár224110d2022-03-02 11:49:20 +0100795kwboot_bootmsg(int tty)
Luka Perkovdff289642012-05-27 11:44:51 +0000796{
Pali Roháre68c27bd2022-01-25 18:13:08 +0100797 struct kwboot_block block;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100798 pthread_t write_thread;
799 int rc, err;
Luka Perkovdff289642012-05-27 11:44:51 +0000800 char c;
801
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100802 /* flush input and output queue */
803 tcflush(tty, TCIOFLUSH);
Luka Perkovdff289642012-05-27 11:44:51 +0000804
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100805 rc = kwboot_msg_start_thread(&write_thread, &tty, kwboot_msg_boot);
806 if (rc) {
807 perror("Failed to start write thread");
808 return rc;
809 }
Luka Perkovdff289642012-05-27 11:44:51 +0000810
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100811 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovdff289642012-05-27 11:44:51 +0000812
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100813 err = 0;
814 while (1) {
Luka Perkovdff289642012-05-27 11:44:51 +0000815 kwboot_spinner();
816
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100817 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
818 if (rc && errno == ETIMEDOUT) {
819 continue;
820 } else if (rc) {
821 err = errno;
822 break;
823 }
824
825 if (c == NAK)
826 break;
827 }
Luka Perkovdff289642012-05-27 11:44:51 +0000828
829 kwboot_printv("\n");
830
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100831 rc = kwboot_msg_stop_thread(write_thread);
832 if (rc) {
833 perror("Failed to stop write thread");
Pali Roháre68c27bd2022-01-25 18:13:08 +0100834 return rc;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100835 }
836
837 if (err) {
838 errno = err;
839 perror("Failed to read response for boot message pattern");
840 return -1;
841 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100842
843 /*
844 * At this stage we have sent more boot message patterns and BootROM
845 * (at least on Armada XP and 385) started interpreting sent bytes as
846 * part of xmodem packets. If BootROM is expecting SOH byte as start of
847 * a xmodem packet and it receives byte 0xff, then it throws it away and
848 * sends a NAK reply to host. If BootROM does not receive any byte for
849 * 2s when expecting some continuation of the xmodem packet, it throws
850 * away the partially received xmodem data and sends NAK reply to host.
851 *
852 * Therefore for starting xmodem transfer we have two options: Either
853 * wait 2s or send 132 0xff bytes (which is the size of xmodem packet)
854 * to ensure that BootROM throws away any partially received data.
855 */
856
857 /* flush output queue with remaining boot message patterns */
Pali Roháre3a18282022-03-02 11:49:18 +0100858 rc = tcflush(tty, TCOFLUSH);
859 if (rc) {
860 perror("Failed to flush output queue");
861 return rc;
862 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100863
864 /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */
865 memset(&block, 0xff, sizeof(block));
Pali Roháre3a18282022-03-02 11:49:18 +0100866 rc = kwboot_tty_send(tty, &block, sizeof(block), 0);
867 if (rc) {
868 perror("Failed to send sync sequence");
869 return rc;
870 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100871
872 /*
873 * Sending 132 bytes via 115200B/8-N-1 takes 11.45 ms, reading 132 bytes
874 * takes 11.45 ms, so waiting for 30 ms should be enough.
875 */
876 usleep(30 * 1000);
877
878 /* flush remaining NAK replies from input queue */
Pali Roháre3a18282022-03-02 11:49:18 +0100879 rc = tcflush(tty, TCIFLUSH);
880 if (rc) {
881 perror("Failed to flush input queue");
882 return rc;
883 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100884
885 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000886}
887
888static int
Pali Rohár224110d2022-03-02 11:49:20 +0100889kwboot_debugmsg(int tty)
Stefan Roesebb5c4282014-10-22 12:13:21 +0200890{
Pali Rohár2af673b2022-03-02 11:49:22 +0100891 unsigned char buf[8192];
892 pthread_t write_thread;
893 int rc, err, i, pos;
894 size_t off;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200895
Pali Rohár2af673b2022-03-02 11:49:22 +0100896 /* flush input and output queue */
897 tcflush(tty, TCIOFLUSH);
Stefan Roesebb5c4282014-10-22 12:13:21 +0200898
Pali Rohár2af673b2022-03-02 11:49:22 +0100899 rc = kwboot_msg_start_thread(&write_thread, &tty, kwboot_msg_debug);
900 if (rc) {
901 perror("Failed to start write thread");
902 return rc;
903 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200904
Pali Rohár2af673b2022-03-02 11:49:22 +0100905 kwboot_printv("Sending debug message. Please reboot the target...");
906 kwboot_spinner();
Stefan Roesebb5c4282014-10-22 12:13:21 +0200907
Pali Rohár2af673b2022-03-02 11:49:22 +0100908 err = 0;
909 off = 0;
910 while (1) {
911 /* Read immediately all bytes in queue without waiting */
912 rc = read(tty, buf + off, sizeof(buf) - off);
913 if ((rc < 0 && errno == EINTR) || rc == 0) {
914 continue;
915 } else if (rc < 0) {
916 err = errno;
Pali Roháre3a18282022-03-02 11:49:18 +0100917 break;
Pali Rohár2af673b2022-03-02 11:49:22 +0100918 }
919 off += rc - 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200920
921 kwboot_spinner();
922
Pali Rohár2af673b2022-03-02 11:49:22 +0100923 /*
924 * Check if we received at least 4 debug message patterns
925 * (console echo from BootROM) in cyclic buffer
926 */
927
928 for (pos = 0; pos < sizeof(kwboot_msg_debug); pos++)
929 if (buf[off] == kwboot_msg_debug[(pos + off) % sizeof(kwboot_msg_debug)])
930 break;
931
932 for (i = off; i >= 0; i--)
933 if (buf[i] != kwboot_msg_debug[(pos + i) % sizeof(kwboot_msg_debug)])
934 break;
935
936 off -= i;
937
938 if (off >= 4 * sizeof(kwboot_msg_debug))
939 break;
940
941 /* If not move valid suffix from end of the buffer to the beginning of buffer */
942 memmove(buf, buf + i + 1, off);
943 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200944
945 kwboot_printv("\n");
946
Pali Rohár2af673b2022-03-02 11:49:22 +0100947 rc = kwboot_msg_stop_thread(write_thread);
948 if (rc) {
949 perror("Failed to stop write thread");
950 return rc;
951 }
952
953 if (err) {
954 errno = err;
955 perror("Failed to read response for debug message pattern");
956 return -1;
957 }
958
959 /* flush output queue with remaining debug message patterns */
960 rc = tcflush(tty, TCOFLUSH);
961 if (rc) {
962 perror("Failed to flush output queue");
963 return rc;
964 }
965
966 kwboot_printv("Clearing input buffer...\n");
967
968 /*
969 * Wait until BootROM transmit all remaining echo characters.
970 * Experimentally it was measured that for Armada 385 BootROM
971 * it is required to wait at least 0.415s. So wait 0.5s.
972 */
973 usleep(500 * 1000);
974
975 /*
976 * In off variable is stored number of characters received after the
977 * successful detection of echo reply. So these characters are console
978 * echo for other following debug message patterns. BootROM may have in
979 * its output queue other echo characters which were being transmitting
980 * before above sleep call. So read remaining number of echo characters
981 * sent by the BootROM now.
982 */
983 while ((rc = kwboot_tty_recv(tty, &buf[0], 1, 0)) == 0)
984 off++;
985 if (errno != ETIMEDOUT) {
986 perror("Failed to read response");
987 return rc;
988 }
989
990 /*
991 * Clear every echo character set by the BootROM by backspace byte.
992 * This is required prior writing any command to the BootROM debug
993 * because BootROM command line buffer has limited size. If length
994 * of the command is larger than buffer size then it looks like
995 * that Armada 385 BootROM crashes after sending ENTER. So erase it.
996 * Experimentally it was measured that for Armada 385 BootROM it is
997 * required to send at least 3 backspace bytes for one echo character.
998 * This is unknown why. But lets do it.
999 */
1000 off *= 3;
1001 memset(buf, '\x08', sizeof(buf));
1002 while (off > sizeof(buf)) {
1003 rc = kwboot_tty_send(tty, buf, sizeof(buf), 1);
1004 if (rc) {
1005 perror("Failed to send clear sequence");
1006 return rc;
1007 }
1008 off -= sizeof(buf);
1009 }
1010 rc = kwboot_tty_send(tty, buf, off, 0);
1011 if (rc) {
1012 perror("Failed to send clear sequence");
1013 return rc;
1014 }
1015
1016 usleep(msg_rsp_timeo * 1000);
1017 rc = tcflush(tty, TCIFLUSH);
1018 if (rc) {
1019 perror("Failed to flush input queue");
1020 return rc;
1021 }
1022
1023 return 0;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001024}
1025
Pali Rohár58cf04de2021-09-24 23:06:44 +02001026static size_t
Luka Perkovdff289642012-05-27 11:44:51 +00001027kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
1028 size_t size, int pnum)
1029{
Marek Behúnedb63242021-09-24 23:06:45 +02001030 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +00001031
Stefan Roesebb5c4282014-10-22 12:13:21 +02001032 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +00001033 block->pnum = pnum;
1034 block->_pnum = ~block->pnum;
1035
Pali Rohárbed18ef2021-09-24 23:06:48 +02001036 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovdff289642012-05-27 11:44:51 +00001037 memcpy(&block->data[0], data, n);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001038 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovdff289642012-05-27 11:44:51 +00001039
1040 block->csum = 0;
1041 for (i = 0; i < n; i++)
1042 block->csum += block->data[i];
1043
1044 return n;
1045}
1046
Marek Behún2d9f2452021-09-24 23:06:52 +02001047static uint64_t
1048_now(void)
1049{
1050 struct timespec ts;
1051
1052 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
1053 static int err_print;
1054
1055 if (!err_print) {
1056 perror("clock_gettime() does not work");
1057 err_print = 1;
1058 }
1059
1060 /* this will just make the timeout not work */
1061 return -1ULL;
1062 }
1063
1064 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
1065}
1066
Luka Perkovdff289642012-05-27 11:44:51 +00001067static int
Marek Behúneabacb82021-09-24 23:06:49 +02001068_is_xm_reply(char c)
1069{
Pali Rohár6af14292022-01-25 18:13:03 +01001070 return c == ACK || c == NAK;
Marek Behúneabacb82021-09-24 23:06:49 +02001071}
1072
1073static int
Pali Rohár42c61a42021-09-24 23:06:54 +02001074_xm_reply_to_error(int c)
1075{
1076 int rc = -1;
1077
1078 switch (c) {
1079 case ACK:
1080 rc = 0;
1081 break;
1082 case NAK:
1083 errno = EBADMSG;
1084 break;
Pali Rohár42c61a42021-09-24 23:06:54 +02001085 default:
1086 errno = EPROTO;
1087 break;
1088 }
1089
1090 return rc;
1091}
1092
1093static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001094kwboot_baud_magic_handle(int fd, char c, int baudrate)
1095{
1096 static size_t rcv_len;
1097
1098 if (rcv_len < sizeof(kwb_baud_magic)) {
1099 /* try to recognize whole magic word */
1100 if (c == kwb_baud_magic[rcv_len]) {
1101 rcv_len++;
1102 } else {
1103 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
1104 fflush(stdout);
1105 rcv_len = 0;
1106 }
1107 }
1108
1109 if (rcv_len == sizeof(kwb_baud_magic)) {
1110 /* magic word received */
1111 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
1112
1113 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
1114 } else {
1115 return 0;
1116 }
1117}
1118
1119static int
Pali Rohár230d4f82022-01-25 18:13:04 +01001120kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Roháre4529bd2022-01-25 18:13:02 +01001121 int ignore_nak_reply,
Pali Rohár959e8502021-10-25 15:13:04 +02001122 int allow_non_xm, int *non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001123 int baudrate, int *baud_changed)
Pali Rohárd06d5202021-09-24 23:06:50 +02001124{
Marek Behún2d9f2452021-09-24 23:06:52 +02001125 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001126 uint64_t recv_until = _now() + timeout;
Pali Rohárd06d5202021-09-24 23:06:50 +02001127 int rc;
1128
1129 while (1) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001130 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohárd06d5202021-09-24 23:06:50 +02001131 if (rc) {
1132 if (errno != ETIMEDOUT)
1133 return rc;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001134 else if (allow_non_xm && *non_xm_print)
Marek Behún2d9f2452021-09-24 23:06:52 +02001135 return -1;
1136 else
1137 *c = NAK;
Pali Rohárd06d5202021-09-24 23:06:50 +02001138 }
1139
1140 /* If received xmodem reply, end. */
Pali Roháre4529bd2022-01-25 18:13:02 +01001141 if (_is_xm_reply(*c)) {
1142 if (*c == NAK && ignore_nak_reply) {
1143 timeout = recv_until - _now();
1144 if (timeout >= 0)
1145 continue;
1146 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001147 break;
Pali Roháre4529bd2022-01-25 18:13:02 +01001148 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001149
1150 /*
Pali Rohár2a8b7692021-09-24 23:07:05 +02001151 * If receiving/printing non-xmodem text output is allowed and
1152 * such a byte was received, we want to increase receiving time
1153 * and either:
1154 * - print the byte, if it is not part of baudrate change magic
1155 * sequence while baudrate change was requested (-B option)
1156 * - change baudrate
Marek Behún0a3b85a2021-09-24 23:06:53 +02001157 * Otherwise decrease timeout by time elapsed.
Pali Rohárd06d5202021-09-24 23:06:50 +02001158 */
1159 if (allow_non_xm) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001160 recv_until = _now() + timeout;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001161
1162 if (baudrate && !*baud_changed) {
1163 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
1164 if (rc == 1)
1165 *baud_changed = 1;
1166 else if (!rc)
1167 *non_xm_print = 1;
1168 else
1169 return rc;
1170 } else if (!baudrate || !*baud_changed) {
1171 putchar(*c);
1172 fflush(stdout);
1173 *non_xm_print = 1;
1174 }
Marek Behún0a3b85a2021-09-24 23:06:53 +02001175 } else {
Pali Rohár230d4f82022-01-25 18:13:04 +01001176 if (stop_on_non_xm)
Pali Rohár959e8502021-10-25 15:13:04 +02001177 break;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001178 timeout = recv_until - _now();
1179 if (timeout < 0) {
1180 errno = ETIMEDOUT;
1181 return -1;
1182 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001183 }
1184 }
1185
1186 return 0;
1187}
1188
1189static int
1190kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár80241ce2022-01-25 18:13:05 +01001191 int *done_print, int baudrate, int allow_retries)
Luka Perkovdff289642012-05-27 11:44:51 +00001192{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001193 int non_xm_print, baud_changed;
1194 int rc, err, retries;
Luka Perkovdff289642012-05-27 11:44:51 +00001195 char c;
1196
Pali Rohárd06d5202021-09-24 23:06:50 +02001197 *done_print = 0;
Pali Rohár6daa5612021-10-27 20:56:58 +02001198 non_xm_print = 0;
1199 baud_changed = 0;
Pali Rohárd06d5202021-09-24 23:06:50 +02001200
Pali Roháre665c6a2021-10-25 15:13:03 +02001201 retries = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001202 do {
Pali Rohárf2acbba2021-10-27 20:56:59 +02001203 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovdff289642012-05-27 11:44:51 +00001204 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001205 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001206
Pali Rohárd06d5202021-09-24 23:06:50 +02001207 if (allow_non_xm && !*done_print) {
1208 kwboot_progress(100, '.');
1209 kwboot_printv("Done\n");
1210 *done_print = 1;
1211 }
Stefan Roesebb5c4282014-10-22 12:13:21 +02001212
Pali Rohár959e8502021-10-25 15:13:04 +02001213 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001214 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001215 allow_non_xm, &non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001216 baudrate, &baud_changed);
Pali Rohárd06d5202021-09-24 23:06:50 +02001217 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001218 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001219
Pali Rohárb3760f92022-01-25 18:13:06 +01001220 if (!allow_non_xm && c != ACK) {
1221 if (c == NAK && allow_retries && retries + 1 < 16)
1222 kwboot_progress(-1, '+');
1223 else
1224 kwboot_progress(-1, 'E');
1225 }
Pali Rohár80241ce2022-01-25 18:13:05 +01001226 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovdff289642012-05-27 11:44:51 +00001227
Marek Behúnbcc5e042021-09-24 23:06:51 +02001228 if (non_xm_print)
1229 kwboot_printv("\n");
1230
Pali Rohár2a8b7692021-09-24 23:07:05 +02001231 if (allow_non_xm && baudrate && !baud_changed) {
1232 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohár2a8b7692021-09-24 23:07:05 +02001233 errno = EPROTO;
Pali Rohár6af14292022-01-25 18:13:03 +01001234 return -1;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001235 }
1236
Pali Rohár42c61a42021-09-24 23:06:54 +02001237 return _xm_reply_to_error(c);
Pali Rohár6af14292022-01-25 18:13:03 +01001238err:
Pali Rohár2a8b7692021-09-24 23:07:05 +02001239 err = errno;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001240 kwboot_printv("\n");
1241 errno = err;
1242 return rc;
Pali Rohár42c61a42021-09-24 23:06:54 +02001243}
Luka Perkovdff289642012-05-27 11:44:51 +00001244
Pali Rohár42c61a42021-09-24 23:06:54 +02001245static int
1246kwboot_xm_finish(int fd)
1247{
1248 int rc, retries;
1249 char c;
Luka Perkovdff289642012-05-27 11:44:51 +00001250
Pali Rohár42c61a42021-09-24 23:06:54 +02001251 kwboot_printv("Finishing transfer\n");
1252
Pali Roháre665c6a2021-10-25 15:13:03 +02001253 retries = 0;
Pali Rohár42c61a42021-09-24 23:06:54 +02001254 do {
1255 rc = kwboot_tty_send_char(fd, EOT);
1256 if (rc)
1257 return rc;
1258
Pali Rohár959e8502021-10-25 15:13:04 +02001259 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001260 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001261 0, NULL, 0, NULL);
Pali Rohár42c61a42021-09-24 23:06:54 +02001262 if (rc)
1263 return rc;
Pali Roháre665c6a2021-10-25 15:13:03 +02001264 } while (c == NAK && retries++ < 16);
Pali Rohár42c61a42021-09-24 23:06:54 +02001265
1266 return _xm_reply_to_error(c);
Luka Perkovdff289642012-05-27 11:44:51 +00001267}
1268
1269static int
Pali Rohárbed18ef2021-09-24 23:06:48 +02001270kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001271 size_t size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001272{
Pali Rohárd06d5202021-09-24 23:06:50 +02001273 int done_print = 0;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001274 size_t sent, left;
1275 int rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001276
Pali Rohárbed18ef2021-09-24 23:06:48 +02001277 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1278 header ? "header" : "data", size);
Luka Perkovdff289642012-05-27 11:44:51 +00001279
Pali Rohárbed18ef2021-09-24 23:06:48 +02001280 left = size;
1281 sent = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001282
Pali Rohárbed18ef2021-09-24 23:06:48 +02001283 while (sent < size) {
Luka Perkovdff289642012-05-27 11:44:51 +00001284 struct kwboot_block block;
Pali Rohárd06d5202021-09-24 23:06:50 +02001285 int last_block;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001286 size_t blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001287
Pali Rohárbed18ef2021-09-24 23:06:48 +02001288 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1289 data += blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001290
Pali Rohárd06d5202021-09-24 23:06:50 +02001291 last_block = (left <= blksz);
1292
Pali Rohár80241ce2022-01-25 18:13:05 +01001293 /*
1294 * Handling of repeated xmodem packets is completely broken in
1295 * Armada 385 BootROM - it completely ignores xmodem packet
1296 * numbers, they are only used for checksum verification.
1297 * BootROM can handle a retry of the xmodem packet only during
1298 * the transmission of kwbimage header and only if BootROM
1299 * itself sent NAK response to previous attempt (it does it on
1300 * checksum failure). During the transmission of kwbimage data
1301 * part, BootROM always expects next xmodem packet, even if it
1302 * sent NAK to previous attempt - there is absolutely no way to
1303 * repair incorrectly transmitted xmodem packet during kwbimage
1304 * data part upload. Also, if kwboot receives non-ACK/NAK
1305 * response (meaning that original BootROM response was damaged
1306 * on UART) there is no way to detect if BootROM accepted xmodem
1307 * packet or not and no way to check if kwboot could repeat the
1308 * packet or not.
1309 *
1310 * Stop transfer and return failure if kwboot receives unknown
1311 * reply if non-xmodem reply is not allowed (for all xmodem
1312 * packets except the last header packet) or when non-ACK reply
1313 * is received during data part transfer.
1314 */
Pali Rohárd06d5202021-09-24 23:06:50 +02001315 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár80241ce2022-01-25 18:13:05 +01001316 &done_print, baudrate, header);
Luka Perkovdff289642012-05-27 11:44:51 +00001317 if (rc)
1318 goto out;
1319
Pali Rohárbed18ef2021-09-24 23:06:48 +02001320 sent += blksz;
1321 left -= blksz;
1322
Pali Rohárd06d5202021-09-24 23:06:50 +02001323 if (!done_print)
1324 kwboot_progress(sent * 100 / size, '.');
Pali Rohárbed18ef2021-09-24 23:06:48 +02001325 }
Luka Perkovdff289642012-05-27 11:44:51 +00001326
Pali Rohárd06d5202021-09-24 23:06:50 +02001327 if (!done_print)
1328 kwboot_printv("Done\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001329
Pali Rohárbed18ef2021-09-24 23:06:48 +02001330 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001331out:
Pali Rohár5ed896f2021-09-24 23:06:47 +02001332 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001333 return rc;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001334}
1335
1336static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001337kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohárbed18ef2021-09-24 23:06:48 +02001338{
1339 const uint8_t *img = _img;
1340 int rc, pnum;
1341 size_t hdrsz;
1342
Marek Behúnd1b0b032021-09-24 23:07:01 +02001343 hdrsz = kwbheader_size(img);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001344
Pali Rohár400b5cf2021-11-05 23:29:58 +01001345 /*
1346 * If header size is not aligned to xmodem block size (which applies
1347 * for all images in kwbimage v0 format) then we have to ensure that
1348 * the last xmodem block of header contains beginning of the data
1349 * followed by the header. So align header size to xmodem block size.
1350 */
1351 hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
1352
Pali Rohárbed18ef2021-09-24 23:06:48 +02001353 pnum = 1;
1354
Pali Rohár2a8b7692021-09-24 23:07:05 +02001355 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001356 if (rc)
1357 return rc;
1358
Pali Rohár400b5cf2021-11-05 23:29:58 +01001359 /*
1360 * If we have already sent image data as a part of the last
1361 * xmodem header block then we have nothing more to send.
1362 */
1363 if (hdrsz < size) {
1364 img += hdrsz;
1365 size -= hdrsz;
1366 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
1367 if (rc)
1368 return rc;
1369 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001370
1371 rc = kwboot_xm_finish(tty);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001372 if (rc)
1373 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001374
Pali Rohár2a8b7692021-09-24 23:07:05 +02001375 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001376 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1377 rc = kwboot_tty_change_baudrate(tty, 115200);
1378 if (rc)
1379 return rc;
1380 }
1381
1382 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001383}
1384
1385static int
Pali Roháred3a9752022-03-02 11:49:23 +01001386kwboot_term_pipe(int in, int out, const char *quit, int *s, const char *kbs, int *k)
Luka Perkovdff289642012-05-27 11:44:51 +00001387{
Pali Rohár111779f2022-02-03 17:45:20 +01001388 char buf[128];
Pali Roháred3a9752022-03-02 11:49:23 +01001389 ssize_t nin, noff;
Luka Perkovdff289642012-05-27 11:44:51 +00001390
Pali Rohár111779f2022-02-03 17:45:20 +01001391 nin = read(in, buf, sizeof(buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -04001392 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +00001393 return -1;
1394
Pali Roháred3a9752022-03-02 11:49:23 +01001395 noff = 0;
1396
1397 if (quit || kbs) {
Luka Perkovdff289642012-05-27 11:44:51 +00001398 int i;
1399
1400 for (i = 0; i < nin; i++) {
Pali Roháred3a9752022-03-02 11:49:23 +01001401 if ((quit || kbs) &&
1402 (!quit || buf[i] != quit[*s]) &&
1403 (!kbs || buf[i] != kbs[*k])) {
1404 const char *prefix;
1405 int plen;
1406
1407 if (quit && kbs) {
1408 prefix = (*s >= *k) ? quit : kbs;
1409 plen = (*s >= *k) ? *s : *k;
1410 } else if (quit) {
1411 prefix = quit;
1412 plen = *s;
1413 } else {
1414 prefix = kbs;
1415 plen = *k;
1416 }
1417
1418 if (plen > i && kwboot_write(out, prefix, plen - i) < 0)
1419 return -1;
1420 }
1421
1422 if (quit && buf[i] == quit[*s]) {
Luka Perkovdff289642012-05-27 11:44:51 +00001423 (*s)++;
Pali Rohár111779f2022-02-03 17:45:20 +01001424 if (!quit[*s]) {
Pali Rohár6a5d1312022-02-18 12:24:13 +01001425 nin = (i > *s) ? (i - *s) : 0;
Pali Rohár111779f2022-02-03 17:45:20 +01001426 break;
1427 }
Pali Roháred3a9752022-03-02 11:49:23 +01001428 } else if (quit) {
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001429 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +02001430 }
Pali Roháred3a9752022-03-02 11:49:23 +01001431
1432 if (kbs && buf[i] == kbs[*k]) {
1433 (*k)++;
1434 if (!kbs[*k]) {
1435 if (i > *k + noff &&
1436 kwboot_write(out, buf + noff, i - *k - noff) < 0)
1437 return -1;
1438 /*
1439 * Replace backspace key by '\b' (0x08)
1440 * byte which is the only recognized
1441 * backspace byte by Marvell BootROM.
1442 */
1443 if (write(out, "\x08", 1) < 0)
1444 return -1;
1445 noff = i + 1;
1446 *k = 0;
1447 }
1448 } else if (kbs) {
1449 *k = 0;
1450 }
Luka Perkovdff289642012-05-27 11:44:51 +00001451 }
Pali Rohár111779f2022-02-03 17:45:20 +01001452
Pali Roháred3a9752022-03-02 11:49:23 +01001453 if (i == nin) {
1454 i = 0;
1455 if (quit && i < *s)
1456 i = *s;
1457 if (kbs && i < *k)
1458 i = *k;
1459 nin -= (nin > i) ? i : nin;
1460 }
Luka Perkovdff289642012-05-27 11:44:51 +00001461 }
1462
Pali Roháred3a9752022-03-02 11:49:23 +01001463 if (nin > noff && kwboot_write(out, buf + noff, nin - noff) < 0)
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001464 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001465
1466 return 0;
1467}
1468
1469static int
1470kwboot_terminal(int tty)
1471{
Pali Roháred3a9752022-03-02 11:49:23 +01001472 int rc, in, s, k;
1473 const char *kbs = NULL;
Marek Behúnea5b2b32021-09-24 23:06:40 +02001474 const char *quit = "\34c";
Luka Perkovdff289642012-05-27 11:44:51 +00001475 struct termios otio, tio;
1476
1477 rc = -1;
1478
1479 in = STDIN_FILENO;
1480 if (isatty(in)) {
1481 rc = tcgetattr(in, &otio);
1482 if (!rc) {
1483 tio = otio;
1484 cfmakeraw(&tio);
1485 rc = tcsetattr(in, TCSANOW, &tio);
1486 }
1487 if (rc) {
1488 perror("tcsetattr");
1489 goto out;
1490 }
1491
Pali Roháred3a9752022-03-02 11:49:23 +01001492 /*
1493 * Get sequence for backspace key used by the current
1494 * terminal. Every occurrence of this sequence will be
1495 * replaced by '\b' byte which is the only recognized
1496 * backspace byte by Marvell BootROM.
1497 *
1498 * Note that we cannot read this sequence from termios
1499 * c_cc[VERASE] as VERASE is valid only when ICANON is
1500 * set in termios c_lflag, which is not case for us.
1501 *
1502 * Also most terminals do not set termios c_cc[VERASE]
1503 * as c_cc[VERASE] can specify only one-byte sequence
1504 * and instead let applications to read (possible
1505 * multi-byte) sequence for backspace key from "kbs"
1506 * terminfo database based on $TERM env variable.
1507 *
1508 * So read "kbs" from terminfo database via tigetstr()
1509 * call after successful setupterm(). Most terminals
1510 * use byte 0x7F for backspace key, so replacement with
1511 * '\b' is required.
1512 */
1513 if (setupterm(NULL, STDOUT_FILENO, &rc) == 0) {
1514 kbs = tigetstr("kbs");
1515 if (kbs == (char *)-1)
1516 kbs = NULL;
1517 }
1518
Luka Perkovdff289642012-05-27 11:44:51 +00001519 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún6c598c32021-09-24 23:07:11 +02001520 quit[0] | 0100, quit[1]);
Luka Perkovdff289642012-05-27 11:44:51 +00001521 } else
1522 in = -1;
1523
1524 rc = 0;
1525 s = 0;
Pali Roháred3a9752022-03-02 11:49:23 +01001526 k = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001527
1528 do {
1529 fd_set rfds;
1530 int nfds = 0;
1531
Pali Rohárc1efca42021-10-25 15:12:52 +02001532 FD_ZERO(&rfds);
Luka Perkovdff289642012-05-27 11:44:51 +00001533 FD_SET(tty, &rfds);
1534 nfds = nfds < tty ? tty : nfds;
1535
1536 if (in >= 0) {
1537 FD_SET(in, &rfds);
1538 nfds = nfds < in ? in : nfds;
1539 }
1540
1541 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1542 if (nfds < 0)
1543 break;
1544
1545 if (FD_ISSET(tty, &rfds)) {
Pali Roháred3a9752022-03-02 11:49:23 +01001546 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL, NULL, NULL);
Luka Perkovdff289642012-05-27 11:44:51 +00001547 if (rc)
1548 break;
1549 }
1550
Marek Behún4eb55de2021-09-24 23:06:39 +02001551 if (in >= 0 && FD_ISSET(in, &rfds)) {
Pali Roháred3a9752022-03-02 11:49:23 +01001552 rc = kwboot_term_pipe(in, tty, quit, &s, kbs, &k);
Luka Perkovdff289642012-05-27 11:44:51 +00001553 if (rc)
1554 break;
1555 }
1556 } while (quit[s] != 0);
1557
Pali Roháreafc9692021-07-23 11:14:18 +02001558 if (in >= 0)
1559 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +02001560 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001561out:
1562 return rc;
1563}
1564
1565static void *
Pali Rohárd9355bf2021-09-24 23:07:03 +02001566kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovdff289642012-05-27 11:44:51 +00001567{
Pali Rohár4d276012021-09-24 23:06:55 +02001568 int rc, fd;
Luka Perkovdff289642012-05-27 11:44:51 +00001569 struct stat st;
1570 void *img;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001571 off_t tot;
Luka Perkovdff289642012-05-27 11:44:51 +00001572
1573 rc = -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001574 img = NULL;
1575
1576 fd = open(path, O_RDONLY);
1577 if (fd < 0)
1578 goto out;
1579
1580 rc = fstat(fd, &st);
1581 if (rc)
1582 goto out;
1583
Pali Rohárd9355bf2021-09-24 23:07:03 +02001584 img = malloc(st.st_size + reserve);
1585 if (!img)
Luka Perkovdff289642012-05-27 11:44:51 +00001586 goto out;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001587
1588 tot = 0;
1589 while (tot < st.st_size) {
1590 ssize_t rd = read(fd, img + tot, st.st_size - tot);
1591
1592 if (rd < 0)
1593 goto out;
1594
1595 tot += rd;
1596
1597 if (!rd && tot < st.st_size) {
1598 errno = EIO;
1599 goto out;
1600 }
Luka Perkovdff289642012-05-27 11:44:51 +00001601 }
1602
1603 rc = 0;
1604 *size = st.st_size;
1605out:
1606 if (rc && img) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001607 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001608 img = NULL;
1609 }
1610 if (fd >= 0)
1611 close(fd);
1612
1613 return img;
1614}
1615
1616static uint8_t
Marek Behúnd1b0b032021-09-24 23:07:01 +02001617kwboot_hdr_csum8(const void *hdr)
Luka Perkovdff289642012-05-27 11:44:51 +00001618{
Marek Behúnd1b0b032021-09-24 23:07:01 +02001619 const uint8_t *data = hdr;
1620 uint8_t csum;
1621 size_t size;
1622
1623 size = kwbheader_size_for_csum(hdr);
Luka Perkovdff289642012-05-27 11:44:51 +00001624
1625 for (csum = 0; size-- > 0; data++)
1626 csum += *data;
1627
1628 return csum;
1629}
1630
Pali Rohár0b659e52021-10-25 15:12:55 +02001631static uint32_t *
1632kwboot_img_csum32_ptr(void *img)
1633{
1634 struct main_hdr_v1 *hdr = img;
1635 uint32_t datasz;
1636
1637 datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1638
1639 return img + le32_to_cpu(hdr->srcaddr) + datasz;
1640}
1641
1642static uint32_t
1643kwboot_img_csum32(const void *img)
1644{
1645 const struct main_hdr_v1 *hdr = img;
1646 uint32_t datasz, csum = 0;
1647 const uint32_t *data;
1648
1649 datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
1650 if (datasz % sizeof(uint32_t))
1651 return 0;
1652
1653 data = img + le32_to_cpu(hdr->srcaddr);
1654 while (datasz > 0) {
1655 csum += le32_to_cpu(*data++);
1656 datasz -= 4;
1657 }
1658
1659 return cpu_to_le32(csum);
1660}
1661
Luka Perkovdff289642012-05-27 11:44:51 +00001662static int
Pali Rohár5725e0c2021-09-24 23:06:57 +02001663kwboot_img_is_secure(void *img)
1664{
1665 struct opt_hdr_v1 *ohdr;
1666
1667 for_each_opt_hdr_v1 (ohdr, img)
1668 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1669 return 1;
1670
1671 return 0;
1672}
1673
Pali Rohár2a8b7692021-09-24 23:07:05 +02001674static void *
Pali Roháraed39f22021-10-25 15:12:56 +02001675kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001676{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001677 struct main_hdr_v1 *hdr = img;
Pali Roháraed39f22021-10-25 15:12:56 +02001678 void *result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001679
Pali Roháraed39f22021-10-25 15:12:56 +02001680 /*
1681 * 32-bit checksum comes after end of image code, so we will be putting
1682 * new code there. So we get this pointer and then increase data size
1683 * (since increasing data size changes kwboot_img_csum32_ptr() return
1684 * value).
1685 */
1686 result = kwboot_img_csum32_ptr(img);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001687 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Roháraed39f22021-10-25 15:12:56 +02001688 *size += grow;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001689
Pali Roháraed39f22021-10-25 15:12:56 +02001690 return result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001691}
1692
Pali Rohárd9355bf2021-09-24 23:07:03 +02001693static void
1694kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1695{
1696 uint32_t hdrsz, datasz, srcaddr;
1697 struct main_hdr_v1 *hdr = img;
Pali Rohár6fa97092021-10-25 15:13:02 +02001698 struct opt_hdr_v1 *ohdr;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001699 uint8_t *data;
1700
1701 srcaddr = le32_to_cpu(hdr->srcaddr);
1702
Pali Rohár6fa97092021-10-25 15:13:02 +02001703 /* calculate real used space in kwbimage header */
1704 if (kwbimage_version(img) == 0) {
1705 hdrsz = kwbheader_size(img);
1706 } else {
1707 hdrsz = sizeof(*hdr);
1708 for_each_opt_hdr_v1 (ohdr, hdr)
1709 hdrsz += opt_hdr_v1_size(ohdr);
1710 }
1711
Pali Rohárd9355bf2021-09-24 23:07:03 +02001712 data = (uint8_t *)img + srcaddr;
1713 datasz = *size - srcaddr;
1714
1715 /* only move data if there is not enough space */
1716 if (hdrsz + grow > srcaddr) {
1717 size_t need = hdrsz + grow - srcaddr;
1718
1719 /* move data by enough bytes */
1720 memmove(data + need, data, datasz);
1721
1722 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1723 *size += need;
1724 }
1725
1726 if (kwbimage_version(img) == 1) {
1727 hdrsz += grow;
Pali Rohár6fa97092021-10-25 15:13:02 +02001728 if (hdrsz > kwbheader_size(img)) {
1729 hdr->headersz_msb = hdrsz >> 16;
1730 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1731 }
Pali Rohárd9355bf2021-09-24 23:07:03 +02001732 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001733}
1734
1735static void *
1736kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1737{
1738 struct main_hdr_v1 *hdr = img;
1739 struct opt_hdr_v1 *ohdr;
Pali Rohár87cc3c92021-10-21 16:46:06 +02001740 uint32_t num_args;
1741 uint32_t offset;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001742 uint32_t ohdrsz;
Pali Rohár90eb9002021-10-25 15:13:01 +02001743 uint8_t *prev_ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001744
Pali Rohár32eec192022-01-12 18:20:52 +01001745 if (hdr->ext) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001746 for_each_opt_hdr_v1 (ohdr, img)
1747 if (opt_hdr_v1_next(ohdr) == NULL)
1748 break;
1749
Pali Rohár90eb9002021-10-25 15:13:01 +02001750 prev_ext = opt_hdr_v1_ext(ohdr);
1751 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001752 } else {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001753 ohdr = (void *)(hdr + 1);
Pali Rohár90eb9002021-10-25 15:13:01 +02001754 prev_ext = &hdr->ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001755 }
1756
Pali Rohár87cc3c92021-10-21 16:46:06 +02001757 /*
1758 * ARM executable code inside the BIN header on some mvebu platforms
1759 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
1760 * This requirement can be met by inserting dummy arguments into
1761 * BIN header, if needed.
1762 */
1763 offset = &ohdr->data[4] - (char *)img;
1764 num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
1765
1766 ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
1767 kwboot_img_grow_hdr(hdr, size, ohdrsz);
1768
Pali Rohár32eec192022-01-12 18:20:52 +01001769 *prev_ext = 1;
Pali Rohár90eb9002021-10-25 15:13:01 +02001770
Pali Rohár2a8b7692021-09-24 23:07:05 +02001771 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1772 ohdr->headersz_msb = ohdrsz >> 16;
1773 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1774
1775 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
Pali Rohár87cc3c92021-10-21 16:46:06 +02001776 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001777
Pali Rohár87cc3c92021-10-21 16:46:06 +02001778 return &ohdr->data[4 + 4 * num_args];
Pali Rohárd9355bf2021-09-24 23:07:03 +02001779}
1780
Pali Rohár2a8b7692021-09-24 23:07:05 +02001781static void
Pali Rohár6303a232021-10-27 20:57:02 +02001782_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Roháraed39f22021-10-25 15:12:56 +02001783 int old_baud, int new_baud)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001784{
Pali Roháraed39f22021-10-25 15:12:56 +02001785 struct main_hdr_v1 *hdr = img;
Pali Rohár6303a232021-10-27 20:57:02 +02001786 uint32_t orig_datasz;
1787 uint32_t codesz;
Pali Roháraed39f22021-10-25 15:12:56 +02001788 uint8_t *code;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001789
Pali Rohár6303a232021-10-27 20:57:02 +02001790 if (for_data) {
Pali Roháraed39f22021-10-25 15:12:56 +02001791 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1792
Pali Rohár6303a232021-10-27 20:57:02 +02001793 codesz = sizeof(kwboot_baud_code) +
1794 sizeof(kwboot_baud_code_data_jump);
1795 code = kwboot_img_grow_data_right(img, size, codesz);
1796 } else {
1797 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1798 sizeof(kwboot_baud_code) +
1799 sizeof(kwboot_baud_code_binhdr_post);
1800 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001801
Pali Rohár6303a232021-10-27 20:57:02 +02001802 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1803 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1804 code += codesz;
1805 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001806
Pali Rohár6303a232021-10-27 20:57:02 +02001807 codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
1808 memcpy(code, kwboot_baud_code, codesz);
1809 code += codesz;
1810 *(uint32_t *)code = cpu_to_le32(old_baud);
1811 code += sizeof(uint32_t);
1812 *(uint32_t *)code = cpu_to_le32(new_baud);
1813 code += sizeof(uint32_t);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001814
Pali Rohár6303a232021-10-27 20:57:02 +02001815 if (for_data) {
1816 codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
1817 memcpy(code, kwboot_baud_code_data_jump, codesz);
1818 code += codesz;
1819 *(uint32_t *)code = hdr->execaddr;
1820 code += sizeof(uint32_t);
1821 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
Pali Roháraed39f22021-10-25 15:12:56 +02001822 } else {
Pali Rohár6303a232021-10-27 20:57:02 +02001823 codesz = sizeof(kwboot_baud_code_binhdr_post);
1824 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1825 code += codesz;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001826 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001827}
1828
Pali Rohár5725e0c2021-09-24 23:06:57 +02001829static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001830kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001831{
Stefan Roesec74165d2015-09-29 09:19:59 +02001832 struct main_hdr_v1 *hdr;
Pali Rohár88255af2021-09-24 23:06:58 +02001833 uint32_t srcaddr;
Luka Perkovdff289642012-05-27 11:44:51 +00001834 uint8_t csum;
Marek Behún811821e2021-09-24 23:07:04 +02001835 size_t hdrsz;
Stefan Roesec74165d2015-09-29 09:19:59 +02001836 int image_ver;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001837 int is_secure;
Luka Perkovdff289642012-05-27 11:44:51 +00001838
Luka Perkovdff289642012-05-27 11:44:51 +00001839 hdr = img;
1840
Marek Behún3e6117e2021-09-24 23:07:12 +02001841 if (*size < sizeof(struct main_hdr_v1))
1842 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001843
Marek Behúnfa9caec2021-09-24 23:07:00 +02001844 image_ver = kwbimage_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +02001845 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +02001846 fprintf(stderr, "Invalid image header version\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001847 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001848 }
1849
Marek Behúnd1b0b032021-09-24 23:07:01 +02001850 hdrsz = kwbheader_size(hdr);
Stefan Roesec74165d2015-09-29 09:19:59 +02001851
Marek Behún3e6117e2021-09-24 23:07:12 +02001852 if (*size < hdrsz)
1853 goto err;
Pali Rohár12281162021-07-23 11:14:21 +02001854
Marek Behúnd1b0b032021-09-24 23:07:01 +02001855 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behún3e6117e2021-09-24 23:07:12 +02001856 if (csum != hdr->checksum)
1857 goto err;
Stefan Roesec74165d2015-09-29 09:19:59 +02001858
Pali Rohár88255af2021-09-24 23:06:58 +02001859 srcaddr = le32_to_cpu(hdr->srcaddr);
1860
1861 switch (hdr->blockid) {
1862 case IBR_HDR_SATA_ID:
Marek Behún3e6117e2021-09-24 23:07:12 +02001863 if (srcaddr < 1)
1864 goto err;
1865
Pali Rohár88255af2021-09-24 23:06:58 +02001866 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
1867 break;
1868
1869 case IBR_HDR_SDIO_ID:
1870 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
1871 break;
1872
1873 case IBR_HDR_PEX_ID:
1874 if (srcaddr == 0xFFFFFFFF)
1875 hdr->srcaddr = cpu_to_le32(hdrsz);
1876 break;
Pali Rohár398d4152021-09-24 23:06:59 +02001877
1878 case IBR_HDR_SPI_ID:
1879 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1880 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1881 hdr->destaddr = cpu_to_le32(0x00800000);
1882 hdr->execaddr = cpu_to_le32(0x00800000);
1883 }
1884 break;
Pali Rohár88255af2021-09-24 23:06:58 +02001885 }
1886
Pali Rohárd9355bf2021-09-24 23:07:03 +02001887 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behún3e6117e2021-09-24 23:07:12 +02001888 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1889 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001890
Pali Rohár0b659e52021-10-25 15:12:55 +02001891 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1892 goto err;
1893
Pali Rohár5725e0c2021-09-24 23:06:57 +02001894 is_secure = kwboot_img_is_secure(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001895
Pali Rohár5725e0c2021-09-24 23:06:57 +02001896 if (hdr->blockid != IBR_HDR_UART_ID) {
1897 if (is_secure) {
1898 fprintf(stderr,
1899 "Image has secure header with signature for non-UART booting\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001900 goto err;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001901 }
1902
1903 kwboot_printv("Patching image boot signature to UART\n");
1904 hdr->blockid = IBR_HDR_UART_ID;
1905 }
Luka Perkovdff289642012-05-27 11:44:51 +00001906
Pali Rohár3024ebd2021-10-22 12:37:47 +02001907 if (!is_secure) {
Pali Rohár71db3622021-10-25 15:12:58 +02001908 if (image_ver == 1) {
1909 /*
1910 * Tell BootROM to send BootROM messages to UART port
1911 * number 0 (used also for UART booting) with default
1912 * baudrate (which should be 115200) and do not touch
1913 * UART MPP configuration.
1914 */
Pali Roháre4b8fca2022-01-25 18:13:13 +01001915 hdr->flags |= 0x1;
Pali Rohár71db3622021-10-25 15:12:58 +02001916 hdr->options &= ~0x1F;
1917 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
1918 hdr->options |= 0 << 3;
1919 }
Pali Rohár3024ebd2021-10-22 12:37:47 +02001920 if (image_ver == 0)
1921 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
1922 hdr->nandpagesize = 0;
1923 }
1924
Pali Rohár2a8b7692021-09-24 23:07:05 +02001925 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001926 if (image_ver == 0) {
1927 fprintf(stderr,
1928 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001929 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001930 }
1931
1932 if (is_secure) {
1933 fprintf(stderr,
1934 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001935 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001936 }
1937
1938 /*
1939 * First inject code that changes the baudrate from the default
1940 * value of 115200 Bd to requested value. This code is inserted
1941 * as a new opt hdr, so it is executed by BootROM after the
1942 * header part is received.
1943 */
1944 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
1945 baudrate);
Pali Roháraed39f22021-10-25 15:12:56 +02001946 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001947
1948 /*
1949 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Roháraed39f22021-10-25 15:12:56 +02001950 * This code is appended after the data part of the image, and
1951 * execaddr is changed so that it is executed before U-Boot
1952 * proper.
Pali Rohár2a8b7692021-09-24 23:07:05 +02001953 */
1954 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Roháraed39f22021-10-25 15:12:56 +02001955 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001956
Pali Rohárf625a222021-10-25 15:12:57 +02001957 /* Update the 32-bit data checksum */
1958 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
1959
Pali Rohár2a8b7692021-09-24 23:07:05 +02001960 /* recompute header size */
1961 hdrsz = kwbheader_size(hdr);
1962 }
1963
Pali Rohárd9355bf2021-09-24 23:07:03 +02001964 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Rohár085d9ce2021-10-25 15:13:00 +02001965 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001966
1967 if (is_secure) {
1968 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001969 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001970 }
1971
1972 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Rohár085d9ce2021-10-25 15:13:00 +02001973 kwboot_img_grow_hdr(img, size, grow);
Pali Rohárd9355bf2021-09-24 23:07:03 +02001974 }
1975
Marek Behúnd1b0b032021-09-24 23:07:01 +02001976 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +00001977
Pali Rohárd9355bf2021-09-24 23:07:03 +02001978 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behún3e6117e2021-09-24 23:07:12 +02001979 return 0;
1980err:
1981 errno = EINVAL;
1982 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001983}
1984
1985static void
1986kwboot_usage(FILE *stream, char *progname)
1987{
1988 fprintf(stream,
Kevin Smith6c7c1ba2016-02-16 21:28:17 +00001989 "Usage: %s [OPTIONS] [-b <image> | -D <image> ] [-B <baud> ] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +02001990 progname);
Luka Perkovdff289642012-05-27 11:44:51 +00001991 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02001992 fprintf(stream,
1993 " -b <image>: boot <image> with preamble (Kirkwood, Armada 370/XP)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02001994 fprintf(stream,
1995 " -D <image>: boot <image> without preamble (Dove)\n");
1996 fprintf(stream, " -d: enter debug mode\n");
1997 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roesef7f509d2015-05-29 13:25:04 +02001998 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +00001999 fprintf(stream,
2000 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +00002001 fprintf(stream, "\n");
2002 fprintf(stream, " -t: mini terminal\n");
2003 fprintf(stream, "\n");
2004 fprintf(stream, " -B <baud>: set baud rate\n");
2005 fprintf(stream, "\n");
2006}
2007
2008int
2009main(int argc, char **argv)
2010{
2011 const char *ttypath, *imgpath;
Pali Rohár4d276012021-09-24 23:06:55 +02002012 int rv, rc, tty, term;
Pali Rohár224110d2022-03-02 11:49:20 +01002013 int bootmsg;
2014 int debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +00002015 void *img;
2016 size_t size;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002017 size_t after_img_rsv;
2018 int baudrate;
Pali Rohár48953362022-01-25 18:13:07 +01002019 int prev_optind;
2020 int c;
Luka Perkovdff289642012-05-27 11:44:51 +00002021
2022 rv = 1;
2023 tty = -1;
Pali Rohár224110d2022-03-02 11:49:20 +01002024 bootmsg = 0;
2025 debugmsg = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00002026 imgpath = NULL;
2027 img = NULL;
2028 term = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00002029 size = 0;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002030 after_img_rsv = KWBOOT_XM_BLKSZ;
2031 baudrate = 115200;
Luka Perkovdff289642012-05-27 11:44:51 +00002032
Pali Rohár73ae7aa2021-11-05 23:30:42 +01002033 printf("kwboot version %s\n", PLAIN_VERSION);
2034
Luka Perkovdff289642012-05-27 11:44:51 +00002035 kwboot_verbose = isatty(STDOUT_FILENO);
2036
2037 do {
Pali Rohár48953362022-01-25 18:13:07 +01002038 prev_optind = optind;
2039 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +00002040 if (c < 0)
2041 break;
2042
2043 switch (c) {
2044 case 'b':
Pali Rohár48953362022-01-25 18:13:07 +01002045 if (imgpath || bootmsg || debugmsg)
2046 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002047 bootmsg = 1;
Pali Rohár48953362022-01-25 18:13:07 +01002048 if (prev_optind == optind)
2049 goto usage;
Pali Rohárcf91fa82022-02-07 10:12:24 +01002050 if (optind < argc - 1 && argv[optind] && argv[optind][0] != '-')
Pali Rohár48953362022-01-25 18:13:07 +01002051 imgpath = argv[optind++];
Luka Perkovdff289642012-05-27 11:44:51 +00002052 break;
2053
Stefan Roesebb5c4282014-10-22 12:13:21 +02002054 case 'D':
Pali Rohár48953362022-01-25 18:13:07 +01002055 if (imgpath || bootmsg || debugmsg)
2056 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002057 bootmsg = 0;
Stefan Roesebb5c4282014-10-22 12:13:21 +02002058 imgpath = optarg;
2059 break;
2060
2061 case 'd':
Pali Rohár48953362022-01-25 18:13:07 +01002062 if (imgpath || bootmsg || debugmsg)
2063 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002064 debugmsg = 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +02002065 break;
2066
Luka Perkovdff289642012-05-27 11:44:51 +00002067 case 'p':
Pali Rohár4d276012021-09-24 23:06:55 +02002068 /* nop, for backward compatibility */
Luka Perkovdff289642012-05-27 11:44:51 +00002069 break;
2070
2071 case 't':
2072 term = 1;
2073 break;
2074
Stefan Roesebb5c4282014-10-22 12:13:21 +02002075 case 'a':
Stefan Roesebb5c4282014-10-22 12:13:21 +02002076 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
2077 break;
2078
Stefan Roesef7f509d2015-05-29 13:25:04 +02002079 case 'q':
Pali Rohárd51b8342022-03-02 11:49:19 +01002080 /* nop, for backward compatibility */
Stefan Roesef7f509d2015-05-29 13:25:04 +02002081 break;
2082
2083 case 's':
2084 msg_rsp_timeo = atoi(optarg);
2085 break;
2086
Kevin Smith4d31a842016-02-16 21:28:19 +00002087 case 'o':
2088 blk_rsp_timeo = atoi(optarg);
2089 break;
2090
Luka Perkovdff289642012-05-27 11:44:51 +00002091 case 'B':
Pali Rohár2a8b7692021-09-24 23:07:05 +02002092 baudrate = atoi(optarg);
Luka Perkovdff289642012-05-27 11:44:51 +00002093 break;
2094
2095 case 'h':
2096 rv = 0;
2097 default:
2098 goto usage;
2099 }
2100 } while (1);
2101
Pali Rohár577506c2022-01-25 18:13:12 +01002102 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovdff289642012-05-27 11:44:51 +00002103 goto usage;
2104
Luka Perkovdff289642012-05-27 11:44:51 +00002105 ttypath = argv[optind++];
2106
Pali Rohár48953362022-01-25 18:13:07 +01002107 if (optind != argc)
2108 goto usage;
2109
Pali Rohár2a8b7692021-09-24 23:07:05 +02002110 tty = kwboot_open_tty(ttypath, imgpath ? 115200 : baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002111 if (tty < 0) {
2112 perror(ttypath);
2113 goto out;
2114 }
2115
Pali Rohár2a8b7692021-09-24 23:07:05 +02002116 if (baudrate == 115200)
2117 /* do not change baudrate during Xmodem to the same value */
2118 baudrate = 0;
2119 else
2120 /* ensure we have enough space for baudrate change code */
Pali Rohár6303a232021-10-27 20:57:02 +02002121 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
2122 sizeof(kwboot_baud_code_binhdr_pre) +
2123 sizeof(kwboot_baud_code) +
2124 sizeof(kwboot_baud_code_binhdr_post) +
Pali Rohárd8774392021-10-25 15:12:54 +02002125 KWBOOT_XM_BLKSZ +
Pali Rohárd8774392021-10-25 15:12:54 +02002126 sizeof(kwboot_baud_code) +
Pali Rohár6303a232021-10-27 20:57:02 +02002127 sizeof(kwboot_baud_code_data_jump) +
Pali Rohárd8774392021-10-25 15:12:54 +02002128 KWBOOT_XM_BLKSZ;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002129
Luka Perkovdff289642012-05-27 11:44:51 +00002130 if (imgpath) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002131 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovdff289642012-05-27 11:44:51 +00002132 if (!img) {
2133 perror(imgpath);
2134 goto out;
2135 }
Luka Perkovdff289642012-05-27 11:44:51 +00002136
Pali Rohár2a8b7692021-09-24 23:07:05 +02002137 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002138 if (rc) {
2139 fprintf(stderr, "%s: Invalid image.\n", imgpath);
2140 goto out;
2141 }
2142 }
2143
Stefan Roesebb5c4282014-10-22 12:13:21 +02002144 if (debugmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002145 rc = kwboot_debugmsg(tty);
Pali Rohár2af673b2022-03-02 11:49:22 +01002146 if (rc)
Stefan Roesebb5c4282014-10-22 12:13:21 +02002147 goto out;
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -04002148 } else if (bootmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002149 rc = kwboot_bootmsg(tty);
Pali Rohár3a2e9bc2022-03-02 11:49:21 +01002150 if (rc)
Luka Perkovdff289642012-05-27 11:44:51 +00002151 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +00002152 }
2153
2154 if (img) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002155 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002156 if (rc) {
2157 perror("xmodem");
2158 goto out;
2159 }
2160 }
2161
2162 if (term) {
2163 rc = kwboot_terminal(tty);
2164 if (rc && !(errno == EINTR)) {
2165 perror("terminal");
2166 goto out;
2167 }
2168 }
2169
2170 rv = 0;
2171out:
2172 if (tty >= 0)
2173 close(tty);
2174
2175 if (img)
Pali Rohárd9355bf2021-09-24 23:07:03 +02002176 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00002177
2178 return rv;
2179
2180usage:
2181 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
2182 goto out;
2183}