blob: da4fe32da2267b50301321651cd0d6cad0ba774d [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.
Pali Rohárf4e9e872022-03-02 11:49:26 +01003 * supports Kirkwood, Dove, Avanta, Armada 370, Armada XP, Armada 375,
4 * Armada 38x and 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>
Marek Behúnd63726e2022-06-01 17:17:06 +02008 * (c) 2021 Marek Behún <kabel@kernel.org>
Luka Perkovdff289642012-05-27 11:44:51 +00009 *
Pali Rohár38db9762022-03-02 11:49:27 +010010 * References:
11 * - "88F6180, 88F6190, 88F6192, and 88F6281: Integrated Controller: Functional
12 * Specifications" December 2, 2008. Chapter 24.2 "BootROM Firmware".
13 * https://web.archive.org/web/20130730091033/https://www.marvell.com/embedded-processors/kirkwood/assets/FS_88F6180_9x_6281_OpenSource.pdf
14 * - "88AP510: High-Performance SoC with Integrated CPU, 2D/3D Graphics
15 * Processor, and High-Definition Video Decoder: Functional Specifications"
16 * August 3, 2011. Chapter 5 "BootROM Firmware"
17 * https://web.archive.org/web/20120130172443/https://www.marvell.com/application-processors/armada-500/assets/Armada-510-Functional-Spec.pdf
18 * - "88F6710, 88F6707, and 88F6W11: ARMADA(R) 370 SoC: Functional Specifications"
19 * May 26, 2014. Chapter 6 "BootROM Firmware".
20 * https://web.archive.org/web/20140617183701/https://www.marvell.com/embedded-processors/armada-300/assets/ARMADA370-FunctionalSpec-datasheet.pdf
21 * - "MV78230, MV78260, and MV78460: ARMADA(R) XP Family of Highly Integrated
22 * Multi-Core ARMv7 Based SoC Processors: Functional Specifications"
23 * May 29, 2014. Chapter 6 "BootROM Firmware".
24 * https://web.archive.org/web/20180829171131/https://www.marvell.com/embedded-processors/armada-xp/assets/ARMADA-XP-Functional-SpecDatasheet.pdf
25 * - "ARMADA(R) 375 Value-Performance Dual Core CPU System on Chip: Functional
26 * Specifications" Doc. No. MV-S109377-00, Rev. A. September 18, 2013.
27 * Chapter 7 "Boot Sequence"
28 * CONFIDENTIAL, no public documentation available
29 * - "88F6810, 88F6811, 88F6821, 88F6W21, 88F6820, and 88F6828: ARMADA(R) 38x
30 * Family High-Performance Single/Dual CPU System on Chip: Functional
31 * Specifications" Doc. No. MV-S109094-00, Rev. C. August 2, 2015.
32 * Chapter 7 "Boot Flow"
33 * CONFIDENTIAL, no public documentation available
34 * - "88F6920, 88F6925 and 88F6928: ARMADA(R) 39x High-Performance Dual Core CPU
35 * System on Chip Functional Specifications" Doc. No. MV-S109896-00, Rev. B.
36 * December 22, 2015. Chapter 7 "Boot Flow"
37 * CONFIDENTIAL, no public documentation available
Luka Perkovdff289642012-05-27 11:44:51 +000038 */
39
Stefan Roese04ec0d32016-01-07 14:12:04 +010040#include "kwbimage.h"
41#include "mkimage.h"
Pali Rohár3c703aaf2021-09-24 23:06:42 +020042#include "version.h"
Stefan Roese04ec0d32016-01-07 14:12:04 +010043
Luka Perkovdff289642012-05-27 11:44:51 +000044#include <stdlib.h>
45#include <stdio.h>
46#include <string.h>
47#include <stdarg.h>
Stefan Roese04ec0d32016-01-07 14:12:04 +010048#include <image.h>
Luka Perkovdff289642012-05-27 11:44:51 +000049#include <libgen.h>
50#include <fcntl.h>
51#include <errno.h>
52#include <unistd.h>
53#include <stdint.h>
Marek Behún2d9f2452021-09-24 23:06:52 +020054#include <time.h>
Luka Perkovdff289642012-05-27 11:44:51 +000055#include <sys/stat.h>
Pali Rohár3a2e9bc2022-03-02 11:49:21 +010056#include <pthread.h>
Luka Perkovdff289642012-05-27 11:44:51 +000057
Pali Rohárfd935e92021-09-24 23:07:06 +020058#ifdef __linux__
59#include "termios_linux.h"
60#else
61#include <termios.h>
62#endif
63
Luka Perkovdff289642012-05-27 11:44:51 +000064/*
Pali Roháred3a9752022-03-02 11:49:23 +010065 * These functions are in <term.h> header file, but this header file conflicts
66 * with "termios_linux.h" header file. So declare these functions manually.
67 */
68extern int setupterm(const char *, int, int *);
69extern char *tigetstr(const char *);
70
71/*
Luka Perkovdff289642012-05-27 11:44:51 +000072 * Marvell BootROM UART Sensing
73 */
74
75static unsigned char kwboot_msg_boot[] = {
76 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
77};
78
Stefan Roesebb5c4282014-10-22 12:13:21 +020079static unsigned char kwboot_msg_debug[] = {
80 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
81};
82
83/* Defines known to work on Kirkwood */
Luka Perkovdff289642012-05-27 11:44:51 +000084#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
85
Stefan Roesebb5c4282014-10-22 12:13:21 +020086/* Defines known to work on Armada XP */
Stefan Roese52859842022-08-19 09:43:59 +020087#define KWBOOT_MSG_RSP_TIMEO_AXP 10 /* ms */
Stefan Roesebb5c4282014-10-22 12:13:21 +020088
Luka Perkovdff289642012-05-27 11:44:51 +000089/*
90 * Xmodem Transfers
91 */
92
93#define SOH 1 /* sender start of block header */
94#define EOT 4 /* sender end of block transfer */
95#define ACK 6 /* target block ack */
96#define NAK 21 /* target block negative ack */
Luka Perkovdff289642012-05-27 11:44:51 +000097
Pali Rohárbed18ef2021-09-24 23:06:48 +020098#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
99
Luka Perkovdff289642012-05-27 11:44:51 +0000100struct kwboot_block {
101 uint8_t soh;
102 uint8_t pnum;
103 uint8_t _pnum;
Pali Rohárbed18ef2021-09-24 23:06:48 +0200104 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovdff289642012-05-27 11:44:51 +0000105 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +0200106} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +0000107
Pali Rohárdef98382022-01-25 18:13:00 +0100108#define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */
Marek Behún2d9f2452021-09-24 23:06:52 +0200109#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovdff289642012-05-27 11:44:51 +0000110
Pali Rohár6303a232021-10-27 20:57:02 +0200111/* ARM code to change baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200112static unsigned char kwboot_baud_code[] = {
113 /* ; #define UART_BASE 0xd0012000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200114 /* ; #define DLL 0x00 */
115 /* ; #define DLH 0x04 */
116 /* ; #define LCR 0x0c */
117 /* ; #define DLAB 0x80 */
118 /* ; #define LSR 0x14 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200119 /* ; #define TEMT 0x40 */
120 /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
121 /* ; */
122 /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200123 /* ; while */
124 /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
125 /* ; u32 lcr = readl(UART_BASE + LCR); */
126 /* ; writel(UART_BASE + LCR, lcr | DLAB); */
127 /* ; u8 old_dll = readl(UART_BASE + DLL); */
128 /* ; u8 old_dlh = readl(UART_BASE + DLH); */
129 /* ; u16 old_dl = old_dll | (old_dlh << 8); */
130 /* ; u32 clk = old_b * old_dl; */
131 /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
132 /* ; u8 new_dll = new_dl & 0xff; */
133 /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
134 /* ; writel(UART_BASE + DLL, new_dll); */
135 /* ; writel(UART_BASE + DLH, new_dlh); */
136 /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
Pali Rohár9e624c92021-10-27 20:57:00 +0200137 /* ; msleep(5); */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200138 /* ; return 0; */
139 /* ; } */
140
Pali Rohár2a8b7692021-09-24 23:07:05 +0200141 /* ; r0 = UART_BASE */
Pali Rohár15b16e52021-10-27 20:57:01 +0200142 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
143 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200144
Pali Rohár2a8b7692021-09-24 23:07:05 +0200145 /* ; Wait until Transmitter FIFO is Empty */
146 /* .Lloop_txempty: */
147 /* ; r1 = UART_BASE[LSR] & TEMT */
148 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
149 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
150 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
151
152 /* ; Set Divisor Latch Access Bit */
153 /* ; UART_BASE[LCR] |= DLAB */
154 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
155 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
156 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
157
158 /* ; Read current Divisor Latch */
159 /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
160 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
161 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
162 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
163 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
164 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
165 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
166 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
167
168 /* ; Read old baudrate value */
169 /* ; r2 = old_baudrate */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100170 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200171
172 /* ; Calculate base clock */
173 /* ; r1 = r2 * r1 */
174 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
175
176 /* ; Read new baudrate value */
Pali Rohár9e624c92021-10-27 20:57:00 +0200177 /* ; r2 = new_baudrate */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100178 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200179
180 /* ; Calculate new Divisor Latch */
181 /* ; r1 = DIV_ROUND(r1, r2) = */
182 /* ; = (r1 + r2/2) / r2 */
183 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
184 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
185 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
186 /* .Lloop_div1: */
187 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
188 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
189 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
190 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
191 /* .Lloop_div2: */
192 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
193 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
194 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
195 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
196 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
197 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
198 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
199
200 /* ; Set new Divisor Latch Low */
201 /* ; UART_BASE[DLL] = r1 & 0xff */
202 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
203 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
204 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
205
206 /* ; Set new Divisor Latch High */
207 /* ; UART_BASE[DLH] = r1>>8 & 0xff */
208 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
209 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
210 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
211
212 /* ; Clear Divisor Latch Access Bit */
213 /* ; UART_BASE[LCR] &= ~DLAB */
214 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
215 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
216 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
217
Pali Rohár9e624c92021-10-27 20:57:00 +0200218 /* ; Loop 0x2dc000 (2998272) cycles */
219 /* ; which is about 5ms on 1200 MHz CPU */
220 /* ; r1 = 0x2dc000 */
221 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200222 /* .Lloop_sleep: */
223 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
224 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
225 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
226
Pali Rohár2d760ed2021-11-01 14:00:02 +0100227 /* ; Jump to the end of execution */
228 0x01, 0x00, 0x00, 0xea, /* b end */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200229
230 /* ; Placeholder for old baudrate value */
231 /* old_baudrate: */
232 0x00, 0x00, 0x00, 0x00, /* .word 0 */
233
234 /* ; Placeholder for new baudrate value */
235 /* new_baudrate: */
236 0x00, 0x00, 0x00, 0x00, /* .word 0 */
Pali Rohár6303a232021-10-27 20:57:02 +0200237
238 /* end: */
239};
240
Pali Rohár2d760ed2021-11-01 14:00:02 +0100241/* ARM code from binary header executed by BootROM before changing baudrate */
Pali Rohár6303a232021-10-27 20:57:02 +0200242static unsigned char kwboot_baud_code_binhdr_pre[] = {
Pali Rohár2d760ed2021-11-01 14:00:02 +0100243 /* ; #define UART_BASE 0xd0012000 */
244 /* ; #define THR 0x00 */
245 /* ; #define LSR 0x14 */
246 /* ; #define THRE 0x20 */
247 /* ; */
248 /* ; void send_preamble(void) { */
249 /* ; const u8 *str = "$baudratechange"; */
250 /* ; u8 c; */
251 /* ; do { */
252 /* ; while */
253 /* ; ((readl(UART_BASE + LSR) & THRE)); */
254 /* ; c = *str++; */
255 /* ; writel(UART_BASE + THR, c); */
256 /* ; } while (c); */
257 /* ; } */
258
259 /* ; Preserve registers for BootROM */
Pali Rohár6303a232021-10-27 20:57:02 +0200260 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100261
262 /* ; r0 = UART_BASE */
263 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
264 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
265
266 /* ; r2 = address of preamble string */
267 0x00, 0x20, 0x8f, 0xe2, /* adr r2, .Lstr_preamble */
268
269 /* ; Skip preamble data section */
270 0x03, 0x00, 0x00, 0xea, /* b .Lloop_preamble */
271
272 /* ; Preamble string */
273 /* .Lstr_preamble: */
274 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
275 0x64, 0x72, 0x61, 0x74,
276 0x65, 0x63, 0x68, 0x61,
277 0x6e, 0x67, 0x65, 0x00,
278
279 /* ; Send preamble string over UART */
280 /* .Lloop_preamble: */
281 /* */
282 /* ; Wait until Transmitter Holding is Empty */
283 /* .Lloop_thre: */
284 /* ; r1 = UART_BASE[LSR] & THRE */
285 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
286 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
287 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
288
289 /* ; Put character into Transmitter FIFO */
290 /* ; r1 = *r2++ */
291 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
292 /* ; UART_BASE[THR] = r1 */
293 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
294
295 /* ; Loop until end of preamble string */
296 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
297 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
Pali Rohár6303a232021-10-27 20:57:02 +0200298};
299
Pali Rohár2d760ed2021-11-01 14:00:02 +0100300/* ARM code for returning from binary header back to BootROM */
Pali Rohár6303a232021-10-27 20:57:02 +0200301static unsigned char kwboot_baud_code_binhdr_post[] = {
302 /* ; Return 0 - no error */
303 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
304 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200305};
306
Pali Rohár6303a232021-10-27 20:57:02 +0200307/* ARM code for jumping to the original image exec_addr */
308static unsigned char kwboot_baud_code_data_jump[] = {
309 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */
310 /* ; Placeholder for exec_addr */
311 /* exec_addr: */
312 0x00, 0x00, 0x00, 0x00, /* .word 0 */
313};
Pali Rohár2a8b7692021-09-24 23:07:05 +0200314
315static const char kwb_baud_magic[16] = "$baudratechange";
316
Luka Perkovdff289642012-05-27 11:44:51 +0000317static int kwboot_verbose;
318
Stefan Roesebb5c4282014-10-22 12:13:21 +0200319static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +0000320static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200321
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200322static ssize_t
323kwboot_write(int fd, const char *buf, size_t len)
324{
Pali Rohár5350df12022-01-25 18:13:10 +0100325 ssize_t tot = 0;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200326
327 while (tot < len) {
328 ssize_t wr = write(fd, buf + tot, len - tot);
329
Pali Rohár5350df12022-01-25 18:13:10 +0100330 if (wr < 0 && errno == EINTR)
331 continue;
332 else if (wr < 0)
333 return wr;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200334
335 tot += wr;
336 }
337
338 return tot;
339}
340
Luka Perkovdff289642012-05-27 11:44:51 +0000341static void
342kwboot_printv(const char *fmt, ...)
343{
344 va_list ap;
345
346 if (kwboot_verbose) {
347 va_start(ap, fmt);
348 vprintf(fmt, ap);
349 va_end(ap);
350 fflush(stdout);
351 }
352}
353
354static void
355__spinner(void)
356{
357 const char seq[] = { '-', '\\', '|', '/' };
358 const int div = 8;
359 static int state, bs;
360
361 if (state % div == 0) {
362 fputc(bs, stdout);
363 fputc(seq[state / div % sizeof(seq)], stdout);
364 fflush(stdout);
365 }
366
367 bs = '\b';
368 state++;
369}
370
371static void
372kwboot_spinner(void)
373{
374 if (kwboot_verbose)
375 __spinner();
376}
377
378static void
379__progress(int pct, char c)
380{
381 const int width = 70;
382 static const char *nl = "";
383 static int pos;
384
385 if (pos % width == 0)
386 printf("%s%3d %% [", nl, pct);
387
388 fputc(c, stdout);
389
390 nl = "]\n";
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200391 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000392
393 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200394 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000395 fputc(' ', stdout);
396 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200397 nl = "";
398 pos = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000399 }
400
401 fflush(stdout);
402
403}
404
405static void
406kwboot_progress(int _pct, char c)
407{
408 static int pct;
409
410 if (_pct != -1)
411 pct = _pct;
412
413 if (kwboot_verbose)
414 __progress(pct, c);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200415
416 if (pct == 100)
417 pct = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000418}
419
420static int
421kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
422{
423 int rc, nfds;
424 fd_set rfds;
425 struct timeval tv;
426 ssize_t n;
427
428 rc = -1;
429
430 FD_ZERO(&rfds);
431 FD_SET(fd, &rfds);
432
433 tv.tv_sec = 0;
434 tv.tv_usec = timeo * 1000;
435 if (tv.tv_usec > 1000000) {
436 tv.tv_sec += tv.tv_usec / 1000000;
437 tv.tv_usec %= 1000000;
438 }
439
440 do {
441 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
Pali Rohár613bd8b2022-01-25 18:13:11 +0100442 if (nfds < 0 && errno == EINTR)
443 continue;
444 else if (nfds < 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000445 goto out;
Pali Rohár613bd8b2022-01-25 18:13:11 +0100446 else if (!nfds) {
Luka Perkovdff289642012-05-27 11:44:51 +0000447 errno = ETIMEDOUT;
448 goto out;
449 }
450
451 n = read(fd, buf, len);
Pali Rohár613bd8b2022-01-25 18:13:11 +0100452 if (n < 0 && errno == EINTR)
453 continue;
454 else if (n <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000455 goto out;
456
457 buf = (char *)buf + n;
458 len -= n;
459 } while (len > 0);
460
461 rc = 0;
462out:
463 return rc;
464}
465
466static int
Pali Rohárf2acbba2021-10-27 20:56:59 +0200467kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovdff289642012-05-27 11:44:51 +0000468{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200469 if (!buf)
470 return 0;
471
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200472 if (kwboot_write(fd, buf, len) < 0)
473 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000474
Pali Rohárf2acbba2021-10-27 20:56:59 +0200475 if (nodrain)
476 return 0;
477
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200478 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000479}
480
481static int
482kwboot_tty_send_char(int fd, unsigned char c)
483{
Pali Rohárf2acbba2021-10-27 20:56:59 +0200484 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovdff289642012-05-27 11:44:51 +0000485}
486
487static speed_t
Pali Rohár2a8b7692021-09-24 23:07:05 +0200488kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000489{
490 switch (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +0200491#ifdef B4000000
492 case 4000000:
493 return B4000000;
494#endif
495#ifdef B3500000
496 case 3500000:
497 return B3500000;
498#endif
499#ifdef B3000000
500 case 3000000:
501 return B3000000;
502#endif
503#ifdef B2500000
504 case 2500000:
505 return B2500000;
506#endif
507#ifdef B2000000
508 case 2000000:
509 return B2000000;
510#endif
511#ifdef B1500000
512 case 1500000:
513 return B1500000;
514#endif
515#ifdef B1152000
516 case 1152000:
517 return B1152000;
518#endif
519#ifdef B1000000
520 case 1000000:
521 return B1000000;
522#endif
523#ifdef B921600
524 case 921600:
525 return B921600;
526#endif
527#ifdef B614400
528 case 614400:
529 return B614400;
530#endif
531#ifdef B576000
532 case 576000:
533 return B576000;
534#endif
535#ifdef B500000
536 case 500000:
537 return B500000;
538#endif
539#ifdef B460800
540 case 460800:
541 return B460800;
542#endif
543#ifdef B307200
544 case 307200:
545 return B307200;
546#endif
547#ifdef B230400
548 case 230400:
549 return B230400;
550#endif
551#ifdef B153600
552 case 153600:
553 return B153600;
554#endif
555#ifdef B115200
Luka Perkovdff289642012-05-27 11:44:51 +0000556 case 115200:
557 return B115200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200558#endif
559#ifdef B76800
560 case 76800:
561 return B76800;
562#endif
563#ifdef B57600
Luka Perkovdff289642012-05-27 11:44:51 +0000564 case 57600:
565 return B57600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200566#endif
567#ifdef B38400
Luka Perkovdff289642012-05-27 11:44:51 +0000568 case 38400:
569 return B38400;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200570#endif
571#ifdef B19200
Luka Perkovdff289642012-05-27 11:44:51 +0000572 case 19200:
573 return B19200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200574#endif
575#ifdef B9600
Luka Perkovdff289642012-05-27 11:44:51 +0000576 case 9600:
577 return B9600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200578#endif
579#ifdef B4800
580 case 4800:
581 return B4800;
582#endif
583#ifdef B2400
584 case 2400:
585 return B2400;
586#endif
587#ifdef B1800
588 case 1800:
589 return B1800;
590#endif
591#ifdef B1200
592 case 1200:
593 return B1200;
594#endif
595#ifdef B600
596 case 600:
597 return B600;
598#endif
599#ifdef B300
600 case 300:
601 return B300;
602#endif
603#ifdef B200
604 case 200:
605 return B200;
606#endif
607#ifdef B150
608 case 150:
609 return B150;
610#endif
611#ifdef B134
612 case 134:
613 return B134;
614#endif
615#ifdef B110
616 case 110:
617 return B110;
618#endif
619#ifdef B75
620 case 75:
621 return B75;
622#endif
623#ifdef B50
624 case 50:
625 return B50;
626#endif
627 default:
Pali Rohárfd935e92021-09-24 23:07:06 +0200628#ifdef BOTHER
629 return BOTHER;
630#else
Pali Rohár2a8b7692021-09-24 23:07:05 +0200631 return B0;
Pali Rohárfd935e92021-09-24 23:07:06 +0200632#endif
Luka Perkovdff289642012-05-27 11:44:51 +0000633 }
Pali Rohár2a8b7692021-09-24 23:07:05 +0200634}
635
636static int
Marek Behún67835492021-09-24 23:07:07 +0200637_is_within_tolerance(int value, int reference, int tolerance)
638{
639 return 100 * value >= reference * (100 - tolerance) &&
640 100 * value <= reference * (100 + tolerance);
641}
642
643static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200644kwboot_tty_change_baudrate(int fd, int baudrate)
645{
646 struct termios tio;
647 speed_t speed;
648 int rc;
649
650 rc = tcgetattr(fd, &tio);
651 if (rc)
652 return rc;
653
654 speed = kwboot_tty_baudrate_to_speed(baudrate);
655 if (speed == B0) {
656 errno = EINVAL;
657 return -1;
658 }
659
Pali Rohárfd935e92021-09-24 23:07:06 +0200660#ifdef BOTHER
661 if (speed == BOTHER)
662 tio.c_ospeed = tio.c_ispeed = baudrate;
663#endif
664
Pali Rohár2a8b7692021-09-24 23:07:05 +0200665 rc = cfsetospeed(&tio, speed);
666 if (rc)
667 return rc;
668
669 rc = cfsetispeed(&tio, speed);
670 if (rc)
671 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +0000672
Pali Rohár2a8b7692021-09-24 23:07:05 +0200673 rc = tcsetattr(fd, TCSANOW, &tio);
674 if (rc)
675 return rc;
676
Marek Behún67835492021-09-24 23:07:07 +0200677 rc = tcgetattr(fd, &tio);
678 if (rc)
679 return rc;
680
681 if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed)
682 goto baud_fail;
683
684#ifdef BOTHER
685 /*
686 * Check whether set baudrate is within 3% tolerance.
687 * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed
688 * with real values.
689 */
690 if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3))
691 goto baud_fail;
692
693 if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3))
694 goto baud_fail;
695#endif
696
Pali Rohár2a8b7692021-09-24 23:07:05 +0200697 return 0;
Marek Behún67835492021-09-24 23:07:07 +0200698
699baud_fail:
700 fprintf(stderr, "Could not set baudrate to requested value\n");
701 errno = EINVAL;
702 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000703}
704
705static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200706kwboot_open_tty(const char *path, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000707{
Pali Rohár2bffe242021-09-24 23:07:10 +0200708 int rc, fd, flags;
Luka Perkovdff289642012-05-27 11:44:51 +0000709 struct termios tio;
710
711 rc = -1;
712
Marek Behún6c598c32021-09-24 23:07:11 +0200713 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovdff289642012-05-27 11:44:51 +0000714 if (fd < 0)
715 goto out;
716
Pali Rohár909a0b92021-09-24 23:07:08 +0200717 rc = tcgetattr(fd, &tio);
718 if (rc)
719 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +0000720
Pali Rohár909a0b92021-09-24 23:07:08 +0200721 cfmakeraw(&tio);
Marek Behún6c598c32021-09-24 23:07:11 +0200722 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár40003a02021-10-25 15:12:53 +0200723 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovdff289642012-05-27 11:44:51 +0000724 tio.c_cc[VMIN] = 1;
Pali Rohár12095b22021-09-24 23:07:09 +0200725 tio.c_cc[VTIME] = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000726
Luka Perkovdff289642012-05-27 11:44:51 +0000727 rc = tcsetattr(fd, TCSANOW, &tio);
728 if (rc)
729 goto out;
730
Pali Rohár2bffe242021-09-24 23:07:10 +0200731 flags = fcntl(fd, F_GETFL);
732 if (flags < 0)
733 goto out;
734
735 rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY);
736 if (rc)
737 goto out;
738
Pali Rohár2a8b7692021-09-24 23:07:05 +0200739 rc = kwboot_tty_change_baudrate(fd, baudrate);
740 if (rc)
741 goto out;
742
Luka Perkovdff289642012-05-27 11:44:51 +0000743 rc = fd;
744out:
745 if (rc < 0) {
746 if (fd >= 0)
747 close(fd);
748 }
749
750 return rc;
751}
752
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100753static void *
754kwboot_msg_write_handler(void *arg)
755{
756 int tty = *(int *)((void **)arg)[0];
757 const void *msg = ((void **)arg)[1];
758 int rsp_timeo = msg_rsp_timeo;
759 int i, dummy_oldtype;
760
761 /* allow to cancel this thread at any time */
762 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &dummy_oldtype);
763
764 while (1) {
765 /* write 128 samples of message pattern into the output queue without waiting */
766 for (i = 0; i < 128; i++) {
767 if (kwboot_tty_send(tty, msg, 8, 1) < 0) {
768 perror("\nFailed to send message pattern");
769 exit(1);
770 }
771 }
772 /* wait until output queue is transmitted and then make pause */
773 if (tcdrain(tty) < 0) {
774 perror("\nFailed to send message pattern");
775 exit(1);
776 }
777 /* BootROM requires pause on UART after it detects message pattern */
778 usleep(rsp_timeo * 1000);
779 }
780}
781
782static int
783kwboot_msg_start_thread(pthread_t *thread, int *tty, void *msg)
784{
785 void *arg[2];
786 int rc;
787
788 arg[0] = tty;
789 arg[1] = msg;
790 rc = pthread_create(thread, NULL, kwboot_msg_write_handler, arg);
791 if (rc) {
792 errno = rc;
793 return -1;
794 }
795
796 return 0;
797}
798
799static int
800kwboot_msg_stop_thread(pthread_t thread)
801{
802 int rc;
803
804 rc = pthread_cancel(thread);
805 if (rc) {
806 errno = rc;
807 return -1;
808 }
809
810 rc = pthread_join(thread, NULL);
811 if (rc) {
812 errno = rc;
813 return -1;
814 }
815
816 return 0;
817}
818
Luka Perkovdff289642012-05-27 11:44:51 +0000819static int
Pali Rohár224110d2022-03-02 11:49:20 +0100820kwboot_bootmsg(int tty)
Luka Perkovdff289642012-05-27 11:44:51 +0000821{
Pali Roháre68c27bd2022-01-25 18:13:08 +0100822 struct kwboot_block block;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100823 pthread_t write_thread;
824 int rc, err;
Luka Perkovdff289642012-05-27 11:44:51 +0000825 char c;
826
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100827 /* flush input and output queue */
828 tcflush(tty, TCIOFLUSH);
Luka Perkovdff289642012-05-27 11:44:51 +0000829
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100830 rc = kwboot_msg_start_thread(&write_thread, &tty, kwboot_msg_boot);
831 if (rc) {
832 perror("Failed to start write thread");
833 return rc;
834 }
Luka Perkovdff289642012-05-27 11:44:51 +0000835
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100836 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovdff289642012-05-27 11:44:51 +0000837
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100838 err = 0;
839 while (1) {
Luka Perkovdff289642012-05-27 11:44:51 +0000840 kwboot_spinner();
841
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100842 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
843 if (rc && errno == ETIMEDOUT) {
844 continue;
845 } else if (rc) {
846 err = errno;
847 break;
848 }
849
850 if (c == NAK)
851 break;
852 }
Luka Perkovdff289642012-05-27 11:44:51 +0000853
854 kwboot_printv("\n");
855
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100856 rc = kwboot_msg_stop_thread(write_thread);
857 if (rc) {
858 perror("Failed to stop write thread");
Pali Roháre68c27bd2022-01-25 18:13:08 +0100859 return rc;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100860 }
861
862 if (err) {
863 errno = err;
864 perror("Failed to read response for boot message pattern");
865 return -1;
866 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100867
868 /*
869 * At this stage we have sent more boot message patterns and BootROM
870 * (at least on Armada XP and 385) started interpreting sent bytes as
871 * part of xmodem packets. If BootROM is expecting SOH byte as start of
872 * a xmodem packet and it receives byte 0xff, then it throws it away and
873 * sends a NAK reply to host. If BootROM does not receive any byte for
874 * 2s when expecting some continuation of the xmodem packet, it throws
875 * away the partially received xmodem data and sends NAK reply to host.
876 *
877 * Therefore for starting xmodem transfer we have two options: Either
878 * wait 2s or send 132 0xff bytes (which is the size of xmodem packet)
879 * to ensure that BootROM throws away any partially received data.
880 */
881
882 /* flush output queue with remaining boot message patterns */
Pali Roháre3a18282022-03-02 11:49:18 +0100883 rc = tcflush(tty, TCOFLUSH);
884 if (rc) {
885 perror("Failed to flush output queue");
886 return rc;
887 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100888
889 /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */
890 memset(&block, 0xff, sizeof(block));
Pali Roháre3a18282022-03-02 11:49:18 +0100891 rc = kwboot_tty_send(tty, &block, sizeof(block), 0);
892 if (rc) {
893 perror("Failed to send sync sequence");
894 return rc;
895 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100896
897 /*
898 * Sending 132 bytes via 115200B/8-N-1 takes 11.45 ms, reading 132 bytes
899 * takes 11.45 ms, so waiting for 30 ms should be enough.
900 */
901 usleep(30 * 1000);
902
903 /* flush remaining NAK replies from input queue */
Pali Roháre3a18282022-03-02 11:49:18 +0100904 rc = tcflush(tty, TCIFLUSH);
905 if (rc) {
906 perror("Failed to flush input queue");
907 return rc;
908 }
Pali Roháre68c27bd2022-01-25 18:13:08 +0100909
910 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000911}
912
913static int
Pali Rohár224110d2022-03-02 11:49:20 +0100914kwboot_debugmsg(int tty)
Stefan Roesebb5c4282014-10-22 12:13:21 +0200915{
Pali Rohár2af673b2022-03-02 11:49:22 +0100916 unsigned char buf[8192];
917 pthread_t write_thread;
918 int rc, err, i, pos;
919 size_t off;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200920
Pali Rohár2af673b2022-03-02 11:49:22 +0100921 /* flush input and output queue */
922 tcflush(tty, TCIOFLUSH);
Stefan Roesebb5c4282014-10-22 12:13:21 +0200923
Pali Rohár2af673b2022-03-02 11:49:22 +0100924 rc = kwboot_msg_start_thread(&write_thread, &tty, kwboot_msg_debug);
925 if (rc) {
926 perror("Failed to start write thread");
927 return rc;
928 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200929
Pali Rohár2af673b2022-03-02 11:49:22 +0100930 kwboot_printv("Sending debug message. Please reboot the target...");
931 kwboot_spinner();
Stefan Roesebb5c4282014-10-22 12:13:21 +0200932
Pali Rohár2af673b2022-03-02 11:49:22 +0100933 err = 0;
934 off = 0;
935 while (1) {
936 /* Read immediately all bytes in queue without waiting */
937 rc = read(tty, buf + off, sizeof(buf) - off);
938 if ((rc < 0 && errno == EINTR) || rc == 0) {
939 continue;
940 } else if (rc < 0) {
941 err = errno;
Pali Roháre3a18282022-03-02 11:49:18 +0100942 break;
Pali Rohár2af673b2022-03-02 11:49:22 +0100943 }
944 off += rc - 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200945
946 kwboot_spinner();
947
Pali Rohár2af673b2022-03-02 11:49:22 +0100948 /*
949 * Check if we received at least 4 debug message patterns
950 * (console echo from BootROM) in cyclic buffer
951 */
952
953 for (pos = 0; pos < sizeof(kwboot_msg_debug); pos++)
954 if (buf[off] == kwboot_msg_debug[(pos + off) % sizeof(kwboot_msg_debug)])
955 break;
956
957 for (i = off; i >= 0; i--)
958 if (buf[i] != kwboot_msg_debug[(pos + i) % sizeof(kwboot_msg_debug)])
959 break;
960
961 off -= i;
962
963 if (off >= 4 * sizeof(kwboot_msg_debug))
964 break;
965
966 /* If not move valid suffix from end of the buffer to the beginning of buffer */
967 memmove(buf, buf + i + 1, off);
968 }
Stefan Roesebb5c4282014-10-22 12:13:21 +0200969
970 kwboot_printv("\n");
971
Pali Rohár2af673b2022-03-02 11:49:22 +0100972 rc = kwboot_msg_stop_thread(write_thread);
973 if (rc) {
974 perror("Failed to stop write thread");
975 return rc;
976 }
977
978 if (err) {
979 errno = err;
980 perror("Failed to read response for debug message pattern");
981 return -1;
982 }
983
984 /* flush output queue with remaining debug message patterns */
985 rc = tcflush(tty, TCOFLUSH);
986 if (rc) {
987 perror("Failed to flush output queue");
988 return rc;
989 }
990
991 kwboot_printv("Clearing input buffer...\n");
992
993 /*
994 * Wait until BootROM transmit all remaining echo characters.
995 * Experimentally it was measured that for Armada 385 BootROM
996 * it is required to wait at least 0.415s. So wait 0.5s.
997 */
998 usleep(500 * 1000);
999
1000 /*
1001 * In off variable is stored number of characters received after the
1002 * successful detection of echo reply. So these characters are console
1003 * echo for other following debug message patterns. BootROM may have in
1004 * its output queue other echo characters which were being transmitting
1005 * before above sleep call. So read remaining number of echo characters
1006 * sent by the BootROM now.
1007 */
1008 while ((rc = kwboot_tty_recv(tty, &buf[0], 1, 0)) == 0)
1009 off++;
1010 if (errno != ETIMEDOUT) {
1011 perror("Failed to read response");
1012 return rc;
1013 }
1014
1015 /*
1016 * Clear every echo character set by the BootROM by backspace byte.
1017 * This is required prior writing any command to the BootROM debug
1018 * because BootROM command line buffer has limited size. If length
1019 * of the command is larger than buffer size then it looks like
1020 * that Armada 385 BootROM crashes after sending ENTER. So erase it.
1021 * Experimentally it was measured that for Armada 385 BootROM it is
1022 * required to send at least 3 backspace bytes for one echo character.
1023 * This is unknown why. But lets do it.
1024 */
1025 off *= 3;
1026 memset(buf, '\x08', sizeof(buf));
1027 while (off > sizeof(buf)) {
1028 rc = kwboot_tty_send(tty, buf, sizeof(buf), 1);
1029 if (rc) {
1030 perror("Failed to send clear sequence");
1031 return rc;
1032 }
1033 off -= sizeof(buf);
1034 }
1035 rc = kwboot_tty_send(tty, buf, off, 0);
1036 if (rc) {
1037 perror("Failed to send clear sequence");
1038 return rc;
1039 }
1040
1041 usleep(msg_rsp_timeo * 1000);
1042 rc = tcflush(tty, TCIFLUSH);
1043 if (rc) {
1044 perror("Failed to flush input queue");
1045 return rc;
1046 }
1047
1048 return 0;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001049}
1050
Pali Rohár58cf04de2021-09-24 23:06:44 +02001051static size_t
Luka Perkovdff289642012-05-27 11:44:51 +00001052kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
1053 size_t size, int pnum)
1054{
Marek Behúnedb63242021-09-24 23:06:45 +02001055 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +00001056
Stefan Roesebb5c4282014-10-22 12:13:21 +02001057 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +00001058 block->pnum = pnum;
1059 block->_pnum = ~block->pnum;
1060
Pali Rohárbed18ef2021-09-24 23:06:48 +02001061 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovdff289642012-05-27 11:44:51 +00001062 memcpy(&block->data[0], data, n);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001063 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovdff289642012-05-27 11:44:51 +00001064
1065 block->csum = 0;
1066 for (i = 0; i < n; i++)
1067 block->csum += block->data[i];
1068
1069 return n;
1070}
1071
Marek Behún2d9f2452021-09-24 23:06:52 +02001072static uint64_t
1073_now(void)
1074{
1075 struct timespec ts;
1076
1077 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
1078 static int err_print;
1079
1080 if (!err_print) {
1081 perror("clock_gettime() does not work");
1082 err_print = 1;
1083 }
1084
1085 /* this will just make the timeout not work */
1086 return -1ULL;
1087 }
1088
1089 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
1090}
1091
Luka Perkovdff289642012-05-27 11:44:51 +00001092static int
Marek Behúneabacb82021-09-24 23:06:49 +02001093_is_xm_reply(char c)
1094{
Pali Rohár6af14292022-01-25 18:13:03 +01001095 return c == ACK || c == NAK;
Marek Behúneabacb82021-09-24 23:06:49 +02001096}
1097
1098static int
Pali Rohár42c61a42021-09-24 23:06:54 +02001099_xm_reply_to_error(int c)
1100{
1101 int rc = -1;
1102
1103 switch (c) {
1104 case ACK:
1105 rc = 0;
1106 break;
1107 case NAK:
1108 errno = EBADMSG;
1109 break;
Pali Rohár42c61a42021-09-24 23:06:54 +02001110 default:
1111 errno = EPROTO;
1112 break;
1113 }
1114
1115 return rc;
1116}
1117
1118static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001119kwboot_baud_magic_handle(int fd, char c, int baudrate)
1120{
1121 static size_t rcv_len;
1122
1123 if (rcv_len < sizeof(kwb_baud_magic)) {
1124 /* try to recognize whole magic word */
1125 if (c == kwb_baud_magic[rcv_len]) {
1126 rcv_len++;
1127 } else {
1128 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
1129 fflush(stdout);
1130 rcv_len = 0;
1131 }
1132 }
1133
1134 if (rcv_len == sizeof(kwb_baud_magic)) {
1135 /* magic word received */
1136 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
1137
1138 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
1139 } else {
1140 return 0;
1141 }
1142}
1143
1144static int
Pali Rohár230d4f82022-01-25 18:13:04 +01001145kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Roháre4529bd2022-01-25 18:13:02 +01001146 int ignore_nak_reply,
Pali Rohár959e8502021-10-25 15:13:04 +02001147 int allow_non_xm, int *non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001148 int baudrate, int *baud_changed)
Pali Rohárd06d5202021-09-24 23:06:50 +02001149{
Marek Behún2d9f2452021-09-24 23:06:52 +02001150 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001151 uint64_t recv_until = _now() + timeout;
Pali Rohárd06d5202021-09-24 23:06:50 +02001152 int rc;
1153
1154 while (1) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001155 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohárd06d5202021-09-24 23:06:50 +02001156 if (rc) {
1157 if (errno != ETIMEDOUT)
1158 return rc;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001159 else if (allow_non_xm && *non_xm_print)
Marek Behún2d9f2452021-09-24 23:06:52 +02001160 return -1;
1161 else
1162 *c = NAK;
Pali Rohárd06d5202021-09-24 23:06:50 +02001163 }
1164
1165 /* If received xmodem reply, end. */
Pali Roháre4529bd2022-01-25 18:13:02 +01001166 if (_is_xm_reply(*c)) {
1167 if (*c == NAK && ignore_nak_reply) {
1168 timeout = recv_until - _now();
1169 if (timeout >= 0)
1170 continue;
1171 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001172 break;
Pali Roháre4529bd2022-01-25 18:13:02 +01001173 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001174
1175 /*
Pali Rohár2a8b7692021-09-24 23:07:05 +02001176 * If receiving/printing non-xmodem text output is allowed and
1177 * such a byte was received, we want to increase receiving time
1178 * and either:
1179 * - print the byte, if it is not part of baudrate change magic
1180 * sequence while baudrate change was requested (-B option)
1181 * - change baudrate
Marek Behún0a3b85a2021-09-24 23:06:53 +02001182 * Otherwise decrease timeout by time elapsed.
Pali Rohárd06d5202021-09-24 23:06:50 +02001183 */
1184 if (allow_non_xm) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001185 recv_until = _now() + timeout;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001186
1187 if (baudrate && !*baud_changed) {
1188 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
1189 if (rc == 1)
1190 *baud_changed = 1;
1191 else if (!rc)
1192 *non_xm_print = 1;
1193 else
1194 return rc;
1195 } else if (!baudrate || !*baud_changed) {
1196 putchar(*c);
1197 fflush(stdout);
1198 *non_xm_print = 1;
1199 }
Marek Behún0a3b85a2021-09-24 23:06:53 +02001200 } else {
Pali Rohár230d4f82022-01-25 18:13:04 +01001201 if (stop_on_non_xm)
Pali Rohár959e8502021-10-25 15:13:04 +02001202 break;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001203 timeout = recv_until - _now();
1204 if (timeout < 0) {
1205 errno = ETIMEDOUT;
1206 return -1;
1207 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001208 }
1209 }
1210
1211 return 0;
1212}
1213
1214static int
1215kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár80241ce2022-01-25 18:13:05 +01001216 int *done_print, int baudrate, int allow_retries)
Luka Perkovdff289642012-05-27 11:44:51 +00001217{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001218 int non_xm_print, baud_changed;
1219 int rc, err, retries;
Luka Perkovdff289642012-05-27 11:44:51 +00001220 char c;
1221
Pali Rohárd06d5202021-09-24 23:06:50 +02001222 *done_print = 0;
Pali Rohár6daa5612021-10-27 20:56:58 +02001223 non_xm_print = 0;
1224 baud_changed = 0;
Pali Rohárd06d5202021-09-24 23:06:50 +02001225
Pali Roháre665c6a2021-10-25 15:13:03 +02001226 retries = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001227 do {
Pali Rohárf2acbba2021-10-27 20:56:59 +02001228 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovdff289642012-05-27 11:44:51 +00001229 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001230 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001231
Pali Rohárd06d5202021-09-24 23:06:50 +02001232 if (allow_non_xm && !*done_print) {
1233 kwboot_progress(100, '.');
1234 kwboot_printv("Done\n");
1235 *done_print = 1;
1236 }
Stefan Roesebb5c4282014-10-22 12:13:21 +02001237
Pali Rohár959e8502021-10-25 15:13:04 +02001238 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001239 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001240 allow_non_xm, &non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001241 baudrate, &baud_changed);
Pali Rohárd06d5202021-09-24 23:06:50 +02001242 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001243 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001244
Pali Rohárb3760f92022-01-25 18:13:06 +01001245 if (!allow_non_xm && c != ACK) {
1246 if (c == NAK && allow_retries && retries + 1 < 16)
1247 kwboot_progress(-1, '+');
1248 else
1249 kwboot_progress(-1, 'E');
1250 }
Pali Rohár80241ce2022-01-25 18:13:05 +01001251 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovdff289642012-05-27 11:44:51 +00001252
Marek Behúnbcc5e042021-09-24 23:06:51 +02001253 if (non_xm_print)
1254 kwboot_printv("\n");
1255
Pali Rohár2a8b7692021-09-24 23:07:05 +02001256 if (allow_non_xm && baudrate && !baud_changed) {
1257 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohár2a8b7692021-09-24 23:07:05 +02001258 errno = EPROTO;
Pali Rohár6af14292022-01-25 18:13:03 +01001259 return -1;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001260 }
1261
Pali Rohár42c61a42021-09-24 23:06:54 +02001262 return _xm_reply_to_error(c);
Pali Rohár6af14292022-01-25 18:13:03 +01001263err:
Pali Rohár2a8b7692021-09-24 23:07:05 +02001264 err = errno;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001265 kwboot_printv("\n");
1266 errno = err;
1267 return rc;
Pali Rohár42c61a42021-09-24 23:06:54 +02001268}
Luka Perkovdff289642012-05-27 11:44:51 +00001269
Pali Rohár42c61a42021-09-24 23:06:54 +02001270static int
1271kwboot_xm_finish(int fd)
1272{
1273 int rc, retries;
1274 char c;
Luka Perkovdff289642012-05-27 11:44:51 +00001275
Pali Rohár42c61a42021-09-24 23:06:54 +02001276 kwboot_printv("Finishing transfer\n");
1277
Pali Roháre665c6a2021-10-25 15:13:03 +02001278 retries = 0;
Pali Rohár42c61a42021-09-24 23:06:54 +02001279 do {
1280 rc = kwboot_tty_send_char(fd, EOT);
1281 if (rc)
1282 return rc;
1283
Pali Rohár959e8502021-10-25 15:13:04 +02001284 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001285 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001286 0, NULL, 0, NULL);
Pali Rohár42c61a42021-09-24 23:06:54 +02001287 if (rc)
1288 return rc;
Pali Roháre665c6a2021-10-25 15:13:03 +02001289 } while (c == NAK && retries++ < 16);
Pali Rohár42c61a42021-09-24 23:06:54 +02001290
1291 return _xm_reply_to_error(c);
Luka Perkovdff289642012-05-27 11:44:51 +00001292}
1293
1294static int
Pali Rohárbed18ef2021-09-24 23:06:48 +02001295kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001296 size_t size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001297{
Pali Rohárd06d5202021-09-24 23:06:50 +02001298 int done_print = 0;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001299 size_t sent, left;
1300 int rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001301
Pali Rohárbed18ef2021-09-24 23:06:48 +02001302 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1303 header ? "header" : "data", size);
Luka Perkovdff289642012-05-27 11:44:51 +00001304
Pali Rohárbed18ef2021-09-24 23:06:48 +02001305 left = size;
1306 sent = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001307
Pali Rohárbed18ef2021-09-24 23:06:48 +02001308 while (sent < size) {
Luka Perkovdff289642012-05-27 11:44:51 +00001309 struct kwboot_block block;
Pali Rohárd06d5202021-09-24 23:06:50 +02001310 int last_block;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001311 size_t blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001312
Pali Rohárbed18ef2021-09-24 23:06:48 +02001313 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1314 data += blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001315
Pali Rohárd06d5202021-09-24 23:06:50 +02001316 last_block = (left <= blksz);
1317
Pali Rohár80241ce2022-01-25 18:13:05 +01001318 /*
1319 * Handling of repeated xmodem packets is completely broken in
1320 * Armada 385 BootROM - it completely ignores xmodem packet
1321 * numbers, they are only used for checksum verification.
1322 * BootROM can handle a retry of the xmodem packet only during
1323 * the transmission of kwbimage header and only if BootROM
1324 * itself sent NAK response to previous attempt (it does it on
1325 * checksum failure). During the transmission of kwbimage data
1326 * part, BootROM always expects next xmodem packet, even if it
1327 * sent NAK to previous attempt - there is absolutely no way to
1328 * repair incorrectly transmitted xmodem packet during kwbimage
1329 * data part upload. Also, if kwboot receives non-ACK/NAK
1330 * response (meaning that original BootROM response was damaged
1331 * on UART) there is no way to detect if BootROM accepted xmodem
1332 * packet or not and no way to check if kwboot could repeat the
1333 * packet or not.
1334 *
1335 * Stop transfer and return failure if kwboot receives unknown
1336 * reply if non-xmodem reply is not allowed (for all xmodem
1337 * packets except the last header packet) or when non-ACK reply
1338 * is received during data part transfer.
1339 */
Pali Rohárd06d5202021-09-24 23:06:50 +02001340 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár80241ce2022-01-25 18:13:05 +01001341 &done_print, baudrate, header);
Luka Perkovdff289642012-05-27 11:44:51 +00001342 if (rc)
1343 goto out;
1344
Pali Rohárbed18ef2021-09-24 23:06:48 +02001345 sent += blksz;
1346 left -= blksz;
1347
Pali Rohárd06d5202021-09-24 23:06:50 +02001348 if (!done_print)
1349 kwboot_progress(sent * 100 / size, '.');
Pali Rohárbed18ef2021-09-24 23:06:48 +02001350 }
Luka Perkovdff289642012-05-27 11:44:51 +00001351
Pali Rohárd06d5202021-09-24 23:06:50 +02001352 if (!done_print)
1353 kwboot_printv("Done\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001354
Pali Rohárbed18ef2021-09-24 23:06:48 +02001355 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001356out:
Pali Rohár5ed896f2021-09-24 23:06:47 +02001357 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001358 return rc;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001359}
1360
1361static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001362kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohárbed18ef2021-09-24 23:06:48 +02001363{
1364 const uint8_t *img = _img;
1365 int rc, pnum;
1366 size_t hdrsz;
1367
Marek Behúnd1b0b032021-09-24 23:07:01 +02001368 hdrsz = kwbheader_size(img);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001369
Pali Rohár400b5cf2021-11-05 23:29:58 +01001370 /*
1371 * If header size is not aligned to xmodem block size (which applies
1372 * for all images in kwbimage v0 format) then we have to ensure that
1373 * the last xmodem block of header contains beginning of the data
1374 * followed by the header. So align header size to xmodem block size.
1375 */
1376 hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
1377
Pali Rohárbed18ef2021-09-24 23:06:48 +02001378 pnum = 1;
1379
Pali Rohár2a8b7692021-09-24 23:07:05 +02001380 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001381 if (rc)
1382 return rc;
1383
Pali Rohár400b5cf2021-11-05 23:29:58 +01001384 /*
1385 * If we have already sent image data as a part of the last
1386 * xmodem header block then we have nothing more to send.
1387 */
1388 if (hdrsz < size) {
1389 img += hdrsz;
1390 size -= hdrsz;
1391 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
1392 if (rc)
1393 return rc;
1394 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001395
1396 rc = kwboot_xm_finish(tty);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001397 if (rc)
1398 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001399
Pali Rohár2a8b7692021-09-24 23:07:05 +02001400 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001401 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1402 rc = kwboot_tty_change_baudrate(tty, 115200);
1403 if (rc)
1404 return rc;
1405 }
1406
1407 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001408}
1409
1410static int
Pali Roháred3a9752022-03-02 11:49:23 +01001411kwboot_term_pipe(int in, int out, const char *quit, int *s, const char *kbs, int *k)
Luka Perkovdff289642012-05-27 11:44:51 +00001412{
Pali Rohár111779f2022-02-03 17:45:20 +01001413 char buf[128];
Pali Roháred3a9752022-03-02 11:49:23 +01001414 ssize_t nin, noff;
Luka Perkovdff289642012-05-27 11:44:51 +00001415
Pali Rohár111779f2022-02-03 17:45:20 +01001416 nin = read(in, buf, sizeof(buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -04001417 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +00001418 return -1;
1419
Pali Roháred3a9752022-03-02 11:49:23 +01001420 noff = 0;
1421
1422 if (quit || kbs) {
Luka Perkovdff289642012-05-27 11:44:51 +00001423 int i;
1424
1425 for (i = 0; i < nin; i++) {
Pali Roháred3a9752022-03-02 11:49:23 +01001426 if ((quit || kbs) &&
1427 (!quit || buf[i] != quit[*s]) &&
1428 (!kbs || buf[i] != kbs[*k])) {
1429 const char *prefix;
1430 int plen;
1431
1432 if (quit && kbs) {
1433 prefix = (*s >= *k) ? quit : kbs;
1434 plen = (*s >= *k) ? *s : *k;
1435 } else if (quit) {
1436 prefix = quit;
1437 plen = *s;
1438 } else {
1439 prefix = kbs;
1440 plen = *k;
1441 }
1442
1443 if (plen > i && kwboot_write(out, prefix, plen - i) < 0)
1444 return -1;
1445 }
1446
1447 if (quit && buf[i] == quit[*s]) {
Luka Perkovdff289642012-05-27 11:44:51 +00001448 (*s)++;
Pali Rohár111779f2022-02-03 17:45:20 +01001449 if (!quit[*s]) {
Pali Rohár6a5d1312022-02-18 12:24:13 +01001450 nin = (i > *s) ? (i - *s) : 0;
Pali Rohár111779f2022-02-03 17:45:20 +01001451 break;
1452 }
Pali Roháred3a9752022-03-02 11:49:23 +01001453 } else if (quit) {
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001454 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +02001455 }
Pali Roháred3a9752022-03-02 11:49:23 +01001456
1457 if (kbs && buf[i] == kbs[*k]) {
1458 (*k)++;
1459 if (!kbs[*k]) {
1460 if (i > *k + noff &&
1461 kwboot_write(out, buf + noff, i - *k - noff) < 0)
1462 return -1;
1463 /*
1464 * Replace backspace key by '\b' (0x08)
1465 * byte which is the only recognized
1466 * backspace byte by Marvell BootROM.
1467 */
1468 if (write(out, "\x08", 1) < 0)
1469 return -1;
1470 noff = i + 1;
1471 *k = 0;
1472 }
1473 } else if (kbs) {
1474 *k = 0;
1475 }
Luka Perkovdff289642012-05-27 11:44:51 +00001476 }
Pali Rohár111779f2022-02-03 17:45:20 +01001477
Pali Roháred3a9752022-03-02 11:49:23 +01001478 if (i == nin) {
1479 i = 0;
1480 if (quit && i < *s)
1481 i = *s;
1482 if (kbs && i < *k)
1483 i = *k;
1484 nin -= (nin > i) ? i : nin;
1485 }
Luka Perkovdff289642012-05-27 11:44:51 +00001486 }
1487
Pali Roháred3a9752022-03-02 11:49:23 +01001488 if (nin > noff && kwboot_write(out, buf + noff, nin - noff) < 0)
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001489 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001490
1491 return 0;
1492}
1493
1494static int
1495kwboot_terminal(int tty)
1496{
Pali Roháred3a9752022-03-02 11:49:23 +01001497 int rc, in, s, k;
1498 const char *kbs = NULL;
Marek Behúnea5b2b32021-09-24 23:06:40 +02001499 const char *quit = "\34c";
Luka Perkovdff289642012-05-27 11:44:51 +00001500 struct termios otio, tio;
1501
1502 rc = -1;
1503
1504 in = STDIN_FILENO;
1505 if (isatty(in)) {
1506 rc = tcgetattr(in, &otio);
1507 if (!rc) {
1508 tio = otio;
1509 cfmakeraw(&tio);
1510 rc = tcsetattr(in, TCSANOW, &tio);
1511 }
1512 if (rc) {
1513 perror("tcsetattr");
1514 goto out;
1515 }
1516
Pali Roháred3a9752022-03-02 11:49:23 +01001517 /*
1518 * Get sequence for backspace key used by the current
1519 * terminal. Every occurrence of this sequence will be
1520 * replaced by '\b' byte which is the only recognized
1521 * backspace byte by Marvell BootROM.
1522 *
1523 * Note that we cannot read this sequence from termios
1524 * c_cc[VERASE] as VERASE is valid only when ICANON is
1525 * set in termios c_lflag, which is not case for us.
1526 *
1527 * Also most terminals do not set termios c_cc[VERASE]
1528 * as c_cc[VERASE] can specify only one-byte sequence
1529 * and instead let applications to read (possible
1530 * multi-byte) sequence for backspace key from "kbs"
1531 * terminfo database based on $TERM env variable.
1532 *
1533 * So read "kbs" from terminfo database via tigetstr()
1534 * call after successful setupterm(). Most terminals
1535 * use byte 0x7F for backspace key, so replacement with
1536 * '\b' is required.
1537 */
1538 if (setupterm(NULL, STDOUT_FILENO, &rc) == 0) {
1539 kbs = tigetstr("kbs");
1540 if (kbs == (char *)-1)
1541 kbs = NULL;
1542 }
1543
Luka Perkovdff289642012-05-27 11:44:51 +00001544 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún6c598c32021-09-24 23:07:11 +02001545 quit[0] | 0100, quit[1]);
Luka Perkovdff289642012-05-27 11:44:51 +00001546 } else
1547 in = -1;
1548
1549 rc = 0;
1550 s = 0;
Pali Roháred3a9752022-03-02 11:49:23 +01001551 k = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001552
1553 do {
1554 fd_set rfds;
1555 int nfds = 0;
1556
Pali Rohárc1efca42021-10-25 15:12:52 +02001557 FD_ZERO(&rfds);
Luka Perkovdff289642012-05-27 11:44:51 +00001558 FD_SET(tty, &rfds);
1559 nfds = nfds < tty ? tty : nfds;
1560
1561 if (in >= 0) {
1562 FD_SET(in, &rfds);
1563 nfds = nfds < in ? in : nfds;
1564 }
1565
1566 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1567 if (nfds < 0)
1568 break;
1569
1570 if (FD_ISSET(tty, &rfds)) {
Pali Roháred3a9752022-03-02 11:49:23 +01001571 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL, NULL, NULL);
Luka Perkovdff289642012-05-27 11:44:51 +00001572 if (rc)
1573 break;
1574 }
1575
Marek Behún4eb55de2021-09-24 23:06:39 +02001576 if (in >= 0 && FD_ISSET(in, &rfds)) {
Pali Roháred3a9752022-03-02 11:49:23 +01001577 rc = kwboot_term_pipe(in, tty, quit, &s, kbs, &k);
Luka Perkovdff289642012-05-27 11:44:51 +00001578 if (rc)
1579 break;
1580 }
1581 } while (quit[s] != 0);
1582
Pali Roháreafc9692021-07-23 11:14:18 +02001583 if (in >= 0)
1584 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +02001585 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001586out:
1587 return rc;
1588}
1589
1590static void *
Pali Rohárd9355bf2021-09-24 23:07:03 +02001591kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovdff289642012-05-27 11:44:51 +00001592{
Pali Rohár4d276012021-09-24 23:06:55 +02001593 int rc, fd;
Luka Perkovdff289642012-05-27 11:44:51 +00001594 void *img;
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001595 off_t len;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001596 off_t tot;
Luka Perkovdff289642012-05-27 11:44:51 +00001597
1598 rc = -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001599 img = NULL;
1600
1601 fd = open(path, O_RDONLY);
1602 if (fd < 0)
1603 goto out;
1604
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001605 len = lseek(fd, 0, SEEK_END);
1606 if (len == (off_t)-1)
Luka Perkovdff289642012-05-27 11:44:51 +00001607 goto out;
1608
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001609 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
1610 goto out;
1611
1612 img = malloc(len + reserve);
Pali Rohárd9355bf2021-09-24 23:07:03 +02001613 if (!img)
Luka Perkovdff289642012-05-27 11:44:51 +00001614 goto out;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001615
1616 tot = 0;
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001617 while (tot < len) {
1618 ssize_t rd = read(fd, img + tot, len - tot);
Pali Rohárd9355bf2021-09-24 23:07:03 +02001619
1620 if (rd < 0)
1621 goto out;
1622
1623 tot += rd;
1624
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001625 if (!rd && tot < len) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001626 errno = EIO;
1627 goto out;
1628 }
Luka Perkovdff289642012-05-27 11:44:51 +00001629 }
1630
1631 rc = 0;
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001632 *size = len;
Luka Perkovdff289642012-05-27 11:44:51 +00001633out:
1634 if (rc && img) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001635 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001636 img = NULL;
1637 }
1638 if (fd >= 0)
1639 close(fd);
1640
1641 return img;
1642}
1643
1644static uint8_t
Marek Behúnd1b0b032021-09-24 23:07:01 +02001645kwboot_hdr_csum8(const void *hdr)
Luka Perkovdff289642012-05-27 11:44:51 +00001646{
Marek Behúnd1b0b032021-09-24 23:07:01 +02001647 const uint8_t *data = hdr;
1648 uint8_t csum;
1649 size_t size;
1650
1651 size = kwbheader_size_for_csum(hdr);
Luka Perkovdff289642012-05-27 11:44:51 +00001652
1653 for (csum = 0; size-- > 0; data++)
1654 csum += *data;
1655
1656 return csum;
1657}
1658
Pali Rohár0b659e52021-10-25 15:12:55 +02001659static uint32_t *
1660kwboot_img_csum32_ptr(void *img)
1661{
1662 struct main_hdr_v1 *hdr = img;
1663 uint32_t datasz;
1664
1665 datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1666
1667 return img + le32_to_cpu(hdr->srcaddr) + datasz;
1668}
1669
1670static uint32_t
1671kwboot_img_csum32(const void *img)
1672{
1673 const struct main_hdr_v1 *hdr = img;
1674 uint32_t datasz, csum = 0;
1675 const uint32_t *data;
1676
1677 datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
1678 if (datasz % sizeof(uint32_t))
1679 return 0;
1680
1681 data = img + le32_to_cpu(hdr->srcaddr);
1682 while (datasz > 0) {
1683 csum += le32_to_cpu(*data++);
1684 datasz -= 4;
1685 }
1686
1687 return cpu_to_le32(csum);
1688}
1689
Luka Perkovdff289642012-05-27 11:44:51 +00001690static int
Pali Rohár5725e0c2021-09-24 23:06:57 +02001691kwboot_img_is_secure(void *img)
1692{
1693 struct opt_hdr_v1 *ohdr;
1694
1695 for_each_opt_hdr_v1 (ohdr, img)
1696 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1697 return 1;
1698
1699 return 0;
1700}
1701
Pali Rohár2a8b7692021-09-24 23:07:05 +02001702static void *
Pali Roháraed39f22021-10-25 15:12:56 +02001703kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001704{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001705 struct main_hdr_v1 *hdr = img;
Pali Roháraed39f22021-10-25 15:12:56 +02001706 void *result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001707
Pali Roháraed39f22021-10-25 15:12:56 +02001708 /*
1709 * 32-bit checksum comes after end of image code, so we will be putting
1710 * new code there. So we get this pointer and then increase data size
1711 * (since increasing data size changes kwboot_img_csum32_ptr() return
1712 * value).
1713 */
1714 result = kwboot_img_csum32_ptr(img);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001715 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Roháraed39f22021-10-25 15:12:56 +02001716 *size += grow;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001717
Pali Roháraed39f22021-10-25 15:12:56 +02001718 return result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001719}
1720
Pali Rohárd9355bf2021-09-24 23:07:03 +02001721static void
1722kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1723{
1724 uint32_t hdrsz, datasz, srcaddr;
1725 struct main_hdr_v1 *hdr = img;
Pali Rohár6fa97092021-10-25 15:13:02 +02001726 struct opt_hdr_v1 *ohdr;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001727 uint8_t *data;
1728
1729 srcaddr = le32_to_cpu(hdr->srcaddr);
1730
Pali Rohár6fa97092021-10-25 15:13:02 +02001731 /* calculate real used space in kwbimage header */
1732 if (kwbimage_version(img) == 0) {
1733 hdrsz = kwbheader_size(img);
1734 } else {
1735 hdrsz = sizeof(*hdr);
1736 for_each_opt_hdr_v1 (ohdr, hdr)
1737 hdrsz += opt_hdr_v1_size(ohdr);
1738 }
1739
Pali Rohárd9355bf2021-09-24 23:07:03 +02001740 data = (uint8_t *)img + srcaddr;
1741 datasz = *size - srcaddr;
1742
1743 /* only move data if there is not enough space */
1744 if (hdrsz + grow > srcaddr) {
1745 size_t need = hdrsz + grow - srcaddr;
1746
1747 /* move data by enough bytes */
1748 memmove(data + need, data, datasz);
1749
1750 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1751 *size += need;
1752 }
1753
1754 if (kwbimage_version(img) == 1) {
1755 hdrsz += grow;
Pali Rohár6fa97092021-10-25 15:13:02 +02001756 if (hdrsz > kwbheader_size(img)) {
1757 hdr->headersz_msb = hdrsz >> 16;
1758 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1759 }
Pali Rohárd9355bf2021-09-24 23:07:03 +02001760 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001761}
1762
1763static void *
1764kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1765{
1766 struct main_hdr_v1 *hdr = img;
1767 struct opt_hdr_v1 *ohdr;
Pali Rohár87cc3c92021-10-21 16:46:06 +02001768 uint32_t num_args;
1769 uint32_t offset;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001770 uint32_t ohdrsz;
Pali Rohár90eb9002021-10-25 15:13:01 +02001771 uint8_t *prev_ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001772
Pali Rohár32eec192022-01-12 18:20:52 +01001773 if (hdr->ext) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001774 for_each_opt_hdr_v1 (ohdr, img)
1775 if (opt_hdr_v1_next(ohdr) == NULL)
1776 break;
1777
Pali Rohár90eb9002021-10-25 15:13:01 +02001778 prev_ext = opt_hdr_v1_ext(ohdr);
1779 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001780 } else {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001781 ohdr = (void *)(hdr + 1);
Pali Rohár90eb9002021-10-25 15:13:01 +02001782 prev_ext = &hdr->ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001783 }
1784
Pali Rohár87cc3c92021-10-21 16:46:06 +02001785 /*
1786 * ARM executable code inside the BIN header on some mvebu platforms
1787 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
1788 * This requirement can be met by inserting dummy arguments into
1789 * BIN header, if needed.
1790 */
1791 offset = &ohdr->data[4] - (char *)img;
1792 num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
1793
1794 ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
1795 kwboot_img_grow_hdr(hdr, size, ohdrsz);
1796
Pali Rohár32eec192022-01-12 18:20:52 +01001797 *prev_ext = 1;
Pali Rohár90eb9002021-10-25 15:13:01 +02001798
Pali Rohár2a8b7692021-09-24 23:07:05 +02001799 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1800 ohdr->headersz_msb = ohdrsz >> 16;
1801 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1802
1803 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
Pali Rohár87cc3c92021-10-21 16:46:06 +02001804 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001805
Pali Rohár87cc3c92021-10-21 16:46:06 +02001806 return &ohdr->data[4 + 4 * num_args];
Pali Rohárd9355bf2021-09-24 23:07:03 +02001807}
1808
Pali Rohár2a8b7692021-09-24 23:07:05 +02001809static void
Pali Rohár6303a232021-10-27 20:57:02 +02001810_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Roháraed39f22021-10-25 15:12:56 +02001811 int old_baud, int new_baud)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001812{
Pali Roháraed39f22021-10-25 15:12:56 +02001813 struct main_hdr_v1 *hdr = img;
Pali Rohár6303a232021-10-27 20:57:02 +02001814 uint32_t orig_datasz;
1815 uint32_t codesz;
Pali Roháraed39f22021-10-25 15:12:56 +02001816 uint8_t *code;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001817
Pali Rohár6303a232021-10-27 20:57:02 +02001818 if (for_data) {
Pali Roháraed39f22021-10-25 15:12:56 +02001819 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1820
Pali Rohár6303a232021-10-27 20:57:02 +02001821 codesz = sizeof(kwboot_baud_code) +
1822 sizeof(kwboot_baud_code_data_jump);
1823 code = kwboot_img_grow_data_right(img, size, codesz);
1824 } else {
1825 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1826 sizeof(kwboot_baud_code) +
1827 sizeof(kwboot_baud_code_binhdr_post);
1828 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001829
Pali Rohár6303a232021-10-27 20:57:02 +02001830 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1831 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1832 code += codesz;
1833 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001834
Pali Rohár6303a232021-10-27 20:57:02 +02001835 codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
1836 memcpy(code, kwboot_baud_code, codesz);
1837 code += codesz;
1838 *(uint32_t *)code = cpu_to_le32(old_baud);
1839 code += sizeof(uint32_t);
1840 *(uint32_t *)code = cpu_to_le32(new_baud);
1841 code += sizeof(uint32_t);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001842
Pali Rohár6303a232021-10-27 20:57:02 +02001843 if (for_data) {
1844 codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
1845 memcpy(code, kwboot_baud_code_data_jump, codesz);
1846 code += codesz;
1847 *(uint32_t *)code = hdr->execaddr;
1848 code += sizeof(uint32_t);
1849 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
Pali Roháraed39f22021-10-25 15:12:56 +02001850 } else {
Pali Rohár6303a232021-10-27 20:57:02 +02001851 codesz = sizeof(kwboot_baud_code_binhdr_post);
1852 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1853 code += codesz;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001854 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001855}
1856
Pali Rohár5725e0c2021-09-24 23:06:57 +02001857static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001858kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001859{
Stefan Roesec74165d2015-09-29 09:19:59 +02001860 struct main_hdr_v1 *hdr;
Pali Rohár88255af2021-09-24 23:06:58 +02001861 uint32_t srcaddr;
Luka Perkovdff289642012-05-27 11:44:51 +00001862 uint8_t csum;
Marek Behún811821e2021-09-24 23:07:04 +02001863 size_t hdrsz;
Stefan Roesec74165d2015-09-29 09:19:59 +02001864 int image_ver;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001865 int is_secure;
Luka Perkovdff289642012-05-27 11:44:51 +00001866
Luka Perkovdff289642012-05-27 11:44:51 +00001867 hdr = img;
1868
Marek Behún3e6117e2021-09-24 23:07:12 +02001869 if (*size < sizeof(struct main_hdr_v1))
1870 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001871
Marek Behúnfa9caec2021-09-24 23:07:00 +02001872 image_ver = kwbimage_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +02001873 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +02001874 fprintf(stderr, "Invalid image header version\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001875 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001876 }
1877
Marek Behúnd1b0b032021-09-24 23:07:01 +02001878 hdrsz = kwbheader_size(hdr);
Stefan Roesec74165d2015-09-29 09:19:59 +02001879
Marek Behún3e6117e2021-09-24 23:07:12 +02001880 if (*size < hdrsz)
1881 goto err;
Pali Rohár12281162021-07-23 11:14:21 +02001882
Marek Behúnd1b0b032021-09-24 23:07:01 +02001883 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behún3e6117e2021-09-24 23:07:12 +02001884 if (csum != hdr->checksum)
1885 goto err;
Stefan Roesec74165d2015-09-29 09:19:59 +02001886
Pali Rohár88255af2021-09-24 23:06:58 +02001887 srcaddr = le32_to_cpu(hdr->srcaddr);
1888
1889 switch (hdr->blockid) {
1890 case IBR_HDR_SATA_ID:
Marek Behún3e6117e2021-09-24 23:07:12 +02001891 if (srcaddr < 1)
1892 goto err;
1893
Pali Rohár88255af2021-09-24 23:06:58 +02001894 hdr->srcaddr = cpu_to_le32((srcaddr - 1) * 512);
1895 break;
1896
1897 case IBR_HDR_SDIO_ID:
1898 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
1899 break;
1900
1901 case IBR_HDR_PEX_ID:
1902 if (srcaddr == 0xFFFFFFFF)
1903 hdr->srcaddr = cpu_to_le32(hdrsz);
1904 break;
Pali Rohár398d4152021-09-24 23:06:59 +02001905
1906 case IBR_HDR_SPI_ID:
1907 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1908 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1909 hdr->destaddr = cpu_to_le32(0x00800000);
1910 hdr->execaddr = cpu_to_le32(0x00800000);
1911 }
1912 break;
Pali Rohár88255af2021-09-24 23:06:58 +02001913 }
1914
Pali Rohárd9355bf2021-09-24 23:07:03 +02001915 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behún3e6117e2021-09-24 23:07:12 +02001916 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1917 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001918
Pali Rohár0b659e52021-10-25 15:12:55 +02001919 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1920 goto err;
1921
Pali Rohár5725e0c2021-09-24 23:06:57 +02001922 is_secure = kwboot_img_is_secure(img);
Luka Perkovdff289642012-05-27 11:44:51 +00001923
Pali Rohár5725e0c2021-09-24 23:06:57 +02001924 if (hdr->blockid != IBR_HDR_UART_ID) {
1925 if (is_secure) {
1926 fprintf(stderr,
1927 "Image has secure header with signature for non-UART booting\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001928 goto err;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001929 }
1930
1931 kwboot_printv("Patching image boot signature to UART\n");
1932 hdr->blockid = IBR_HDR_UART_ID;
1933 }
Luka Perkovdff289642012-05-27 11:44:51 +00001934
Pali Rohár3024ebd2021-10-22 12:37:47 +02001935 if (!is_secure) {
Pali Rohár71db3622021-10-25 15:12:58 +02001936 if (image_ver == 1) {
1937 /*
1938 * Tell BootROM to send BootROM messages to UART port
1939 * number 0 (used also for UART booting) with default
1940 * baudrate (which should be 115200) and do not touch
1941 * UART MPP configuration.
1942 */
Pali Roháre4b8fca2022-01-25 18:13:13 +01001943 hdr->flags |= 0x1;
Pali Rohár71db3622021-10-25 15:12:58 +02001944 hdr->options &= ~0x1F;
1945 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
1946 hdr->options |= 0 << 3;
1947 }
Pali Rohár3024ebd2021-10-22 12:37:47 +02001948 if (image_ver == 0)
1949 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
1950 hdr->nandpagesize = 0;
1951 }
1952
Pali Rohár2a8b7692021-09-24 23:07:05 +02001953 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001954 if (image_ver == 0) {
1955 fprintf(stderr,
1956 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001957 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001958 }
1959
1960 if (is_secure) {
1961 fprintf(stderr,
1962 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001963 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001964 }
1965
1966 /*
1967 * First inject code that changes the baudrate from the default
1968 * value of 115200 Bd to requested value. This code is inserted
1969 * as a new opt hdr, so it is executed by BootROM after the
1970 * header part is received.
1971 */
1972 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
1973 baudrate);
Pali Roháraed39f22021-10-25 15:12:56 +02001974 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001975
1976 /*
1977 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Roháraed39f22021-10-25 15:12:56 +02001978 * This code is appended after the data part of the image, and
1979 * execaddr is changed so that it is executed before U-Boot
1980 * proper.
Pali Rohár2a8b7692021-09-24 23:07:05 +02001981 */
1982 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Roháraed39f22021-10-25 15:12:56 +02001983 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001984
Pali Rohárf625a222021-10-25 15:12:57 +02001985 /* Update the 32-bit data checksum */
1986 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
1987
Pali Rohár2a8b7692021-09-24 23:07:05 +02001988 /* recompute header size */
1989 hdrsz = kwbheader_size(hdr);
1990 }
1991
Pali Rohárd9355bf2021-09-24 23:07:03 +02001992 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Rohár085d9ce2021-10-25 15:13:00 +02001993 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001994
1995 if (is_secure) {
1996 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001997 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001998 }
1999
2000 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Rohár085d9ce2021-10-25 15:13:00 +02002001 kwboot_img_grow_hdr(img, size, grow);
Pali Rohárd9355bf2021-09-24 23:07:03 +02002002 }
2003
Marek Behúnd1b0b032021-09-24 23:07:01 +02002004 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +00002005
Pali Rohárd9355bf2021-09-24 23:07:03 +02002006 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behún3e6117e2021-09-24 23:07:12 +02002007 return 0;
2008err:
2009 errno = EINVAL;
2010 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00002011}
2012
2013static void
2014kwboot_usage(FILE *stream, char *progname)
2015{
2016 fprintf(stream,
Pali Rohár8564cbe2022-03-02 11:49:24 +01002017 "Usage: %s [OPTIONS] [-b <image> | -D <image> | -b | -d ] [-B <baud> ] [-t] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +02002018 progname);
Luka Perkovdff289642012-05-27 11:44:51 +00002019 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02002020 fprintf(stream,
Pali Rohárf4e9e872022-03-02 11:49:26 +01002021 " -b <image>: boot <image> with preamble (Kirkwood, Avanta, Armada 370/XP/375/38x/39x)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02002022 fprintf(stream,
2023 " -D <image>: boot <image> without preamble (Dove)\n");
Pali Rohár8564cbe2022-03-02 11:49:24 +01002024 fprintf(stream, " -b: enter xmodem boot mode\n");
2025 fprintf(stream, " -d: enter console debug mode\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02002026 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roesef7f509d2015-05-29 13:25:04 +02002027 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +00002028 fprintf(stream,
2029 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +00002030 fprintf(stream, "\n");
2031 fprintf(stream, " -t: mini terminal\n");
2032 fprintf(stream, "\n");
2033 fprintf(stream, " -B <baud>: set baud rate\n");
2034 fprintf(stream, "\n");
2035}
2036
2037int
2038main(int argc, char **argv)
2039{
2040 const char *ttypath, *imgpath;
Pali Rohár4d276012021-09-24 23:06:55 +02002041 int rv, rc, tty, term;
Pali Rohár224110d2022-03-02 11:49:20 +01002042 int bootmsg;
2043 int debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +00002044 void *img;
2045 size_t size;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002046 size_t after_img_rsv;
2047 int baudrate;
Pali Rohár48953362022-01-25 18:13:07 +01002048 int prev_optind;
2049 int c;
Luka Perkovdff289642012-05-27 11:44:51 +00002050
2051 rv = 1;
2052 tty = -1;
Pali Rohár224110d2022-03-02 11:49:20 +01002053 bootmsg = 0;
2054 debugmsg = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00002055 imgpath = NULL;
2056 img = NULL;
2057 term = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00002058 size = 0;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002059 after_img_rsv = KWBOOT_XM_BLKSZ;
2060 baudrate = 115200;
Luka Perkovdff289642012-05-27 11:44:51 +00002061
Pali Rohár73ae7aa2021-11-05 23:30:42 +01002062 printf("kwboot version %s\n", PLAIN_VERSION);
2063
Luka Perkovdff289642012-05-27 11:44:51 +00002064 kwboot_verbose = isatty(STDOUT_FILENO);
2065
2066 do {
Pali Rohár48953362022-01-25 18:13:07 +01002067 prev_optind = optind;
2068 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +00002069 if (c < 0)
2070 break;
2071
2072 switch (c) {
2073 case 'b':
Pali Rohár48953362022-01-25 18:13:07 +01002074 if (imgpath || bootmsg || debugmsg)
2075 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002076 bootmsg = 1;
Pali Rohár48953362022-01-25 18:13:07 +01002077 if (prev_optind == optind)
2078 goto usage;
Pali Rohárfefee1a2022-03-07 19:03:09 +01002079 /* Option -b could have optional argument which specify image path */
2080 if (optind < argc && argv[optind] && argv[optind][0] != '-')
Pali Rohár48953362022-01-25 18:13:07 +01002081 imgpath = argv[optind++];
Luka Perkovdff289642012-05-27 11:44:51 +00002082 break;
2083
Stefan Roesebb5c4282014-10-22 12:13:21 +02002084 case 'D':
Pali Rohár48953362022-01-25 18:13:07 +01002085 if (imgpath || bootmsg || debugmsg)
2086 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002087 bootmsg = 0;
Stefan Roesebb5c4282014-10-22 12:13:21 +02002088 imgpath = optarg;
2089 break;
2090
2091 case 'd':
Pali Rohár48953362022-01-25 18:13:07 +01002092 if (imgpath || bootmsg || debugmsg)
2093 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002094 debugmsg = 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +02002095 break;
2096
Luka Perkovdff289642012-05-27 11:44:51 +00002097 case 'p':
Pali Rohár4d276012021-09-24 23:06:55 +02002098 /* nop, for backward compatibility */
Luka Perkovdff289642012-05-27 11:44:51 +00002099 break;
2100
2101 case 't':
2102 term = 1;
2103 break;
2104
Stefan Roesebb5c4282014-10-22 12:13:21 +02002105 case 'a':
Stefan Roesebb5c4282014-10-22 12:13:21 +02002106 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
2107 break;
2108
Stefan Roesef7f509d2015-05-29 13:25:04 +02002109 case 'q':
Pali Rohárd51b8342022-03-02 11:49:19 +01002110 /* nop, for backward compatibility */
Stefan Roesef7f509d2015-05-29 13:25:04 +02002111 break;
2112
2113 case 's':
2114 msg_rsp_timeo = atoi(optarg);
2115 break;
2116
Kevin Smith4d31a842016-02-16 21:28:19 +00002117 case 'o':
2118 blk_rsp_timeo = atoi(optarg);
2119 break;
2120
Luka Perkovdff289642012-05-27 11:44:51 +00002121 case 'B':
Pali Rohár2a8b7692021-09-24 23:07:05 +02002122 baudrate = atoi(optarg);
Luka Perkovdff289642012-05-27 11:44:51 +00002123 break;
2124
2125 case 'h':
2126 rv = 0;
2127 default:
2128 goto usage;
2129 }
2130 } while (1);
2131
Pali Rohár577506c2022-01-25 18:13:12 +01002132 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovdff289642012-05-27 11:44:51 +00002133 goto usage;
2134
Pali Rohárfefee1a2022-03-07 19:03:09 +01002135 /*
2136 * If there is no remaining argument but optional imgpath was parsed
2137 * then it means that optional imgpath was eaten by getopt parser.
2138 * Reassing imgpath to required ttypath argument.
2139 */
2140 if (optind == argc && imgpath) {
2141 ttypath = imgpath;
2142 imgpath = NULL;
2143 } else if (optind + 1 == argc) {
2144 ttypath = argv[optind];
2145 } else {
Pali Rohár48953362022-01-25 18:13:07 +01002146 goto usage;
Pali Rohárfefee1a2022-03-07 19:03:09 +01002147 }
Pali Rohár48953362022-01-25 18:13:07 +01002148
Pali Rohár6c05cfa2022-03-07 19:03:07 +01002149 /* boot and debug message use baudrate 115200 */
2150 if (((bootmsg && !imgpath) || debugmsg) && baudrate != 115200) {
2151 fprintf(stderr, "Baudrate other than 115200 cannot be used for this operation.\n");
2152 goto usage;
2153 }
2154
Pali Rohárfab87482022-03-07 19:03:08 +01002155 tty = kwboot_open_tty(ttypath, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002156 if (tty < 0) {
2157 perror(ttypath);
2158 goto out;
2159 }
2160
Pali Rohárfab87482022-03-07 19:03:08 +01002161 /*
2162 * initial baudrate for image transfer is always 115200,
2163 * the change to different baudrate is done only after the header is sent
2164 */
2165 if (imgpath && baudrate != 115200) {
2166 rc = kwboot_tty_change_baudrate(tty, 115200);
2167 if (rc) {
2168 perror(ttypath);
2169 goto out;
2170 }
2171 }
2172
Pali Rohár2a8b7692021-09-24 23:07:05 +02002173 if (baudrate == 115200)
2174 /* do not change baudrate during Xmodem to the same value */
2175 baudrate = 0;
2176 else
2177 /* ensure we have enough space for baudrate change code */
Pali Rohár6303a232021-10-27 20:57:02 +02002178 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
2179 sizeof(kwboot_baud_code_binhdr_pre) +
2180 sizeof(kwboot_baud_code) +
2181 sizeof(kwboot_baud_code_binhdr_post) +
Pali Rohárd8774392021-10-25 15:12:54 +02002182 KWBOOT_XM_BLKSZ +
Pali Rohárd8774392021-10-25 15:12:54 +02002183 sizeof(kwboot_baud_code) +
Pali Rohár6303a232021-10-27 20:57:02 +02002184 sizeof(kwboot_baud_code_data_jump) +
Pali Rohárd8774392021-10-25 15:12:54 +02002185 KWBOOT_XM_BLKSZ;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002186
Luka Perkovdff289642012-05-27 11:44:51 +00002187 if (imgpath) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002188 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovdff289642012-05-27 11:44:51 +00002189 if (!img) {
2190 perror(imgpath);
2191 goto out;
2192 }
Luka Perkovdff289642012-05-27 11:44:51 +00002193
Pali Rohár2a8b7692021-09-24 23:07:05 +02002194 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002195 if (rc) {
2196 fprintf(stderr, "%s: Invalid image.\n", imgpath);
2197 goto out;
2198 }
2199 }
2200
Stefan Roesebb5c4282014-10-22 12:13:21 +02002201 if (debugmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002202 rc = kwboot_debugmsg(tty);
Pali Rohár2af673b2022-03-02 11:49:22 +01002203 if (rc)
Stefan Roesebb5c4282014-10-22 12:13:21 +02002204 goto out;
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -04002205 } else if (bootmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002206 rc = kwboot_bootmsg(tty);
Pali Rohár3a2e9bc2022-03-02 11:49:21 +01002207 if (rc)
Luka Perkovdff289642012-05-27 11:44:51 +00002208 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +00002209 }
2210
2211 if (img) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002212 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002213 if (rc) {
2214 perror("xmodem");
2215 goto out;
2216 }
2217 }
2218
2219 if (term) {
2220 rc = kwboot_terminal(tty);
2221 if (rc && !(errno == EINTR)) {
2222 perror("terminal");
2223 goto out;
2224 }
2225 }
2226
2227 rv = 0;
2228out:
2229 if (tty >= 0)
2230 close(tty);
2231
2232 if (img)
Pali Rohárd9355bf2021-09-24 23:07:03 +02002233 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00002234
2235 return rv;
2236
2237usage:
2238 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
2239 goto out;
2240}