blob: 7a7dd5bf3d7be1c1e31d99543c862f3377ca22b2 [file] [log] [blame]
Luka Perkovd131ad62012-05-27 11:44:51 +00001/*
Stefan Roese84899e22014-10-22 12:13:21 +02002 * Boot a Marvell SoC, with Xmodem over UART0.
Pali Rohár0b5909d2022-03-02 11:49:26 +01003 * supports Kirkwood, Dove, Avanta, Armada 370, Armada XP, Armada 375,
4 * Armada 38x and Armada 39x.
Luka Perkovd131ad62012-05-27 11:44:51 +00005 *
6 * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com>
Pali Rohárcf8c9322021-09-24 23:07:14 +02007 * (c) 2021 Pali Rohár <pali@kernel.org>
Marek Behún61143f72022-06-01 17:17:06 +02008 * (c) 2021 Marek Behún <kabel@kernel.org>
Luka Perkovd131ad62012-05-27 11:44:51 +00009 *
Pali Rohárf4fa9622022-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
Pali Rohár29b92bb2023-01-08 13:34:24 +010018 * - "88F6665, 88F6660, 88F6658, 88F6655, 88F6655F, 88F6650, 88F6650F, 88F6610,
19 * and 88F6610F Avanta LP Family Integrated Single/Dual CPU Ecosystem for
20 * Gateway (GW), Home Gateway Unit (HGU), and Single Family Unit (SFU)
21 * Functional Specifications" Doc. No. MV-S108952-00, Rev. A. November 7, 2013.
22 * Chapter 7 "Boot Flow"
23 * CONFIDENTIAL, no public documentation available
Pali Rohárf4fa9622022-03-02 11:49:27 +010024 * - "88F6710, 88F6707, and 88F6W11: ARMADA(R) 370 SoC: Functional Specifications"
25 * May 26, 2014. Chapter 6 "BootROM Firmware".
26 * https://web.archive.org/web/20140617183701/https://www.marvell.com/embedded-processors/armada-300/assets/ARMADA370-FunctionalSpec-datasheet.pdf
27 * - "MV78230, MV78260, and MV78460: ARMADA(R) XP Family of Highly Integrated
28 * Multi-Core ARMv7 Based SoC Processors: Functional Specifications"
29 * May 29, 2014. Chapter 6 "BootROM Firmware".
30 * https://web.archive.org/web/20180829171131/https://www.marvell.com/embedded-processors/armada-xp/assets/ARMADA-XP-Functional-SpecDatasheet.pdf
Pali Rohár29b92bb2023-01-08 13:34:24 +010031 * - "BobCat2 Control and Management Subsystem Functional Specifications"
32 * Doc. No. MV-S109400-00, Rev. A. December 4, 2014.
33 * Chapter 1.6 BootROM Firmware
34 * CONFIDENTIAL, no public documentation available
35 * - "AlleyCat3 and PONCat3 Highly Integrated 1/10 Gigabit Ethernet Switch
36 * Control and Management Subsystem: Functional Specifications"
37 * Doc. No. MV-S109693-00, Rev. A. May 20, 2014.
38 * Chapter 1.6 BootROM Firmware
39 * CONFIDENTIAL, no public documentation available
Pali Rohárf4fa9622022-03-02 11:49:27 +010040 * - "ARMADA(R) 375 Value-Performance Dual Core CPU System on Chip: Functional
41 * Specifications" Doc. No. MV-S109377-00, Rev. A. September 18, 2013.
42 * Chapter 7 "Boot Sequence"
43 * CONFIDENTIAL, no public documentation available
44 * - "88F6810, 88F6811, 88F6821, 88F6W21, 88F6820, and 88F6828: ARMADA(R) 38x
45 * Family High-Performance Single/Dual CPU System on Chip: Functional
46 * Specifications" Doc. No. MV-S109094-00, Rev. C. August 2, 2015.
47 * Chapter 7 "Boot Flow"
48 * CONFIDENTIAL, no public documentation available
49 * - "88F6920, 88F6925 and 88F6928: ARMADA(R) 39x High-Performance Dual Core CPU
50 * System on Chip Functional Specifications" Doc. No. MV-S109896-00, Rev. B.
51 * December 22, 2015. Chapter 7 "Boot Flow"
52 * CONFIDENTIAL, no public documentation available
Pali Rohár29b92bb2023-01-08 13:34:24 +010053 * - "Marvell boot image parser", Marvell U-Boot 2013.01, version 18.06. September 17, 2015.
54 * https://github.com/MarvellEmbeddedProcessors/u-boot-marvell/blob/u-boot-2013.01-armada-18.06/tools/marvell/doimage_mv/hdrparser.c
55 * - "Marvell doimage Tool", Marvell U-Boot 2013.01, version 18.06. August 30, 2015.
56 * https://github.com/MarvellEmbeddedProcessors/u-boot-marvell/blob/u-boot-2013.01-armada-18.06/tools/marvell/doimage_mv/doimage.c
Pali Rohárfa032792023-01-21 19:57:28 +010057 *
58 * Storage location / offset of different image types:
59 * - IBR_HDR_SPI_ID (0x5A):
60 * SPI image can be stored at any 2 MB aligned offset in the first 16 MB of
61 * SPI-NOR or parallel-NOR. Despite the type name it really can be stored on
62 * parallel-NOR and cannot be stored on other SPI devices, like SPI-NAND.
63 * So it should have been named NOR image, not SPI image. This image type
64 * supports XIP - Execute In Place directly from NOR memory.
65 *
66 * - IBR_HDR_NAND_ID (0x8B):
67 * NAND image can be stored either at any 2 MB aligned offset in the first
68 * 16 MB of SPI-NAND or at any blocksize aligned offset in the first 64 MB
69 * of parallel-NAND.
70 *
71 * - IBR_HDR_PEX_ID (0x9C):
72 * PEX image is used for booting from PCI Express device. Source address
73 * stored in image is ignored by BootROM. It is not the BootROM who parses
74 * or loads data part of the PEX image. BootROM just configures SoC to the
75 * PCIe endpoint mode and let the PCIe device on the other end of the PCIe
76 * link (which must be in Root Complex mode) to load kwbimage into SoC's
77 * memory and tell BootROM physical address.
78 *
79 * - IBR_HDR_UART_ID (0x69):
80 * UART image can be transfered via xmodem protocol over first UART.
81 *
82 * - IBR_HDR_I2C_ID (0x4D):
83 * It is unknown for what kind of storage is used this image. It is not
84 * specified in any document from References section.
85 *
86 * - IBR_HDR_SATA_ID (0x78):
87 * SATA image can be stored at sector 1 (after the MBR table), sector 34
88 * (after the GPT table) or at any next sector which is aligned to 2 MB and
89 * is in the first 16 MB of SATA disk. Note that source address in SATA image
90 * is stored in sector unit and not in bytes like for any other images.
91 * Unfortunately sector size is disk specific, in most cases it is 512 bytes
92 * but there are also Native 4K SATA disks which have 4096 bytes long sectors.
93 *
94 * - IBR_HDR_SDIO_ID (0xAE):
95 * SDIO image can be stored on different medias:
96 * - SD(SC) card
97 * - SDHC/SDXC card
98 * - eMMC HW boot partition
99 * - eMMC user data partition / MMC card
100 * It cannot be stored on SDIO card despite the image name.
101 *
102 * For SD(SC)/SDHC/SDXC cards, image can be stored at the same locations as
103 * the SATA image (sector 1, sector 34 or any 2 MB aligned sector) but within
104 * the first 64 MB. SDHC and SDXC cards have fixed 512 bytes long sector size.
105 * Old SD(SC) cards unfortunately can have also different sector sizes, mostly
106 * 1024 bytes long sector sizes and also can be changed at runtime.
107 *
108 * For MMC-compatible devices, image can be stored at offset 0 or at offset
109 * 2 MB. If MMC device supports HW boot partitions then image must be stored
110 * on the HW partition as is configured in the EXT_CSC register (it can be
111 * either boot or user data).
112 *
113 * Note that source address for SDIO image is stored in byte unit, like for
114 * any other images (except SATA). Marvell Functional Specifications for
115 * A38x and A39x SoCs say that source address is in sector units, but this
116 * is purely incorrect information. A385 BootROM really expects source address
117 * for SDIO images in bytes and also Marvell tools generate SDIO image with
118 * source address in byte units.
Luka Perkovd131ad62012-05-27 11:44:51 +0000119 */
120
Stefan Roesef4db6c92016-01-07 14:12:04 +0100121#include "kwbimage.h"
122#include "mkimage.h"
Pali Rohára050a862021-09-24 23:06:42 +0200123#include "version.h"
Stefan Roesef4db6c92016-01-07 14:12:04 +0100124
Luka Perkovd131ad62012-05-27 11:44:51 +0000125#include <stdlib.h>
126#include <stdio.h>
127#include <string.h>
128#include <stdarg.h>
Stefan Roesef4db6c92016-01-07 14:12:04 +0100129#include <image.h>
Luka Perkovd131ad62012-05-27 11:44:51 +0000130#include <libgen.h>
131#include <fcntl.h>
132#include <errno.h>
133#include <unistd.h>
134#include <stdint.h>
Marek Behún12df7b72021-09-24 23:06:52 +0200135#include <time.h>
Luka Perkovd131ad62012-05-27 11:44:51 +0000136#include <sys/stat.h>
Pali Rohár913866a2022-03-02 11:49:21 +0100137#include <pthread.h>
Luka Perkovd131ad62012-05-27 11:44:51 +0000138
Pali Rohár93b55632021-09-24 23:07:06 +0200139#ifdef __linux__
140#include "termios_linux.h"
141#else
142#include <termios.h>
143#endif
144
Luka Perkovd131ad62012-05-27 11:44:51 +0000145/*
Pali Roháre8d26e82022-03-02 11:49:23 +0100146 * These functions are in <term.h> header file, but this header file conflicts
147 * with "termios_linux.h" header file. So declare these functions manually.
148 */
149extern int setupterm(const char *, int, int *);
150extern char *tigetstr(const char *);
151
152/*
Luka Perkovd131ad62012-05-27 11:44:51 +0000153 * Marvell BootROM UART Sensing
154 */
155
156static unsigned char kwboot_msg_boot[] = {
157 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
158};
159
Stefan Roese84899e22014-10-22 12:13:21 +0200160static unsigned char kwboot_msg_debug[] = {
161 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
162};
163
164/* Defines known to work on Kirkwood */
Luka Perkovd131ad62012-05-27 11:44:51 +0000165#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
166
Stefan Roese84899e22014-10-22 12:13:21 +0200167/* Defines known to work on Armada XP */
Stefan Roeseca076d92022-08-19 09:43:59 +0200168#define KWBOOT_MSG_RSP_TIMEO_AXP 10 /* ms */
Stefan Roese84899e22014-10-22 12:13:21 +0200169
Luka Perkovd131ad62012-05-27 11:44:51 +0000170/*
171 * Xmodem Transfers
172 */
173
174#define SOH 1 /* sender start of block header */
175#define EOT 4 /* sender end of block transfer */
176#define ACK 6 /* target block ack */
177#define NAK 21 /* target block negative ack */
Luka Perkovd131ad62012-05-27 11:44:51 +0000178
Pali Rohár2ef87f72021-09-24 23:06:48 +0200179#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
180
Luka Perkovd131ad62012-05-27 11:44:51 +0000181struct kwboot_block {
182 uint8_t soh;
183 uint8_t pnum;
184 uint8_t _pnum;
Pali Rohár2ef87f72021-09-24 23:06:48 +0200185 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovd131ad62012-05-27 11:44:51 +0000186 uint8_t csum;
Pali Rohára107c612021-07-23 11:14:14 +0200187} __packed;
Luka Perkovd131ad62012-05-27 11:44:51 +0000188
Pali Roháref951432022-01-25 18:13:00 +0100189#define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */
Marek Behún12df7b72021-09-24 23:06:52 +0200190#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovd131ad62012-05-27 11:44:51 +0000191
Pali Rohár8dbe0272021-10-27 20:57:02 +0200192/* ARM code to change baudrate */
Pali Rohárca272042021-09-24 23:07:05 +0200193static unsigned char kwboot_baud_code[] = {
194 /* ; #define UART_BASE 0xd0012000 */
Pali Rohárca272042021-09-24 23:07:05 +0200195 /* ; #define DLL 0x00 */
196 /* ; #define DLH 0x04 */
197 /* ; #define LCR 0x0c */
198 /* ; #define DLAB 0x80 */
199 /* ; #define LSR 0x14 */
Pali Rohárca272042021-09-24 23:07:05 +0200200 /* ; #define TEMT 0x40 */
201 /* ; #define DIV_ROUND(a, b) ((a + b/2) / b) */
202 /* ; */
203 /* ; u32 set_baudrate(u32 old_b, u32 new_b) { */
Pali Rohárca272042021-09-24 23:07:05 +0200204 /* ; while */
205 /* ; (!(readl(UART_BASE + LSR) & TEMT)); */
206 /* ; u32 lcr = readl(UART_BASE + LCR); */
207 /* ; writel(UART_BASE + LCR, lcr | DLAB); */
208 /* ; u8 old_dll = readl(UART_BASE + DLL); */
209 /* ; u8 old_dlh = readl(UART_BASE + DLH); */
210 /* ; u16 old_dl = old_dll | (old_dlh << 8); */
211 /* ; u32 clk = old_b * old_dl; */
212 /* ; u16 new_dl = DIV_ROUND(clk, new_b); */
213 /* ; u8 new_dll = new_dl & 0xff; */
214 /* ; u8 new_dlh = (new_dl >> 8) & 0xff; */
215 /* ; writel(UART_BASE + DLL, new_dll); */
216 /* ; writel(UART_BASE + DLH, new_dlh); */
217 /* ; writel(UART_BASE + LCR, lcr & ~DLAB); */
Pali Rohár56452292021-10-27 20:57:00 +0200218 /* ; msleep(5); */
Pali Rohárca272042021-09-24 23:07:05 +0200219 /* ; return 0; */
220 /* ; } */
221
Pali Rohárca272042021-09-24 23:07:05 +0200222 /* ; r0 = UART_BASE */
Pali Rohár558176d2021-10-27 20:57:01 +0200223 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
224 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohárca272042021-09-24 23:07:05 +0200225
Pali Rohárca272042021-09-24 23:07:05 +0200226 /* ; Wait until Transmitter FIFO is Empty */
227 /* .Lloop_txempty: */
228 /* ; r1 = UART_BASE[LSR] & TEMT */
229 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
230 0x40, 0x00, 0x11, 0xe3, /* tst r1, #0x40 */
231 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_txempty */
232
233 /* ; Set Divisor Latch Access Bit */
234 /* ; UART_BASE[LCR] |= DLAB */
235 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
236 0x80, 0x10, 0x81, 0xe3, /* orr r1, r1, #0x80 */
237 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
238
239 /* ; Read current Divisor Latch */
240 /* ; r1 = UART_BASE[DLH]<<8 | UART_BASE[DLL] */
241 0x00, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x00] */
242 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
243 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
244 0x04, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x04] */
245 0xff, 0x10, 0x01, 0xe2, /* and r1, r1, #0xff */
246 0x41, 0x14, 0xa0, 0xe1, /* asr r1, r1, #8 */
247 0x02, 0x10, 0x81, 0xe1, /* orr r1, r1, r2 */
248
249 /* ; Read old baudrate value */
250 /* ; r2 = old_baudrate */
Pali Rohár62a98f42021-11-01 14:00:02 +0100251 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohárca272042021-09-24 23:07:05 +0200252
253 /* ; Calculate base clock */
254 /* ; r1 = r2 * r1 */
255 0x92, 0x01, 0x01, 0xe0, /* mul r1, r2, r1 */
256
257 /* ; Read new baudrate value */
Pali Rohár56452292021-10-27 20:57:00 +0200258 /* ; r2 = new_baudrate */
Pali Rohár62a98f42021-11-01 14:00:02 +0100259 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohárca272042021-09-24 23:07:05 +0200260
261 /* ; Calculate new Divisor Latch */
262 /* ; r1 = DIV_ROUND(r1, r2) = */
263 /* ; = (r1 + r2/2) / r2 */
264 0xa2, 0x10, 0x81, 0xe0, /* add r1, r1, r2, lsr #1 */
265 0x02, 0x40, 0xa0, 0xe1, /* mov r4, r2 */
266 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
267 /* .Lloop_div1: */
268 0x84, 0x40, 0xa0, 0x91, /* movls r4, r4, lsl #1 */
269 0xa1, 0x00, 0x54, 0xe1, /* cmp r4, r1, lsr #1 */
270 0xfc, 0xff, 0xff, 0x9a, /* bls .Lloop_div1 */
271 0x00, 0x30, 0xa0, 0xe3, /* mov r3, #0 */
272 /* .Lloop_div2: */
273 0x04, 0x00, 0x51, 0xe1, /* cmp r1, r4 */
274 0x04, 0x10, 0x41, 0x20, /* subhs r1, r1, r4 */
275 0x03, 0x30, 0xa3, 0xe0, /* adc r3, r3, r3 */
276 0xa4, 0x40, 0xa0, 0xe1, /* mov r4, r4, lsr #1 */
277 0x02, 0x00, 0x54, 0xe1, /* cmp r4, r2 */
278 0xf9, 0xff, 0xff, 0x2a, /* bhs .Lloop_div2 */
279 0x03, 0x10, 0xa0, 0xe1, /* mov r1, r3 */
280
281 /* ; Set new Divisor Latch Low */
282 /* ; UART_BASE[DLL] = r1 & 0xff */
283 0x01, 0x20, 0xa0, 0xe1, /* mov r2, r1 */
284 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
285 0x00, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x00] */
286
287 /* ; Set new Divisor Latch High */
288 /* ; UART_BASE[DLH] = r1>>8 & 0xff */
289 0x41, 0x24, 0xa0, 0xe1, /* asr r2, r1, #8 */
290 0xff, 0x20, 0x02, 0xe2, /* and r2, r2, #0xff */
291 0x04, 0x20, 0x80, 0xe5, /* str r2, [r0, #0x04] */
292
293 /* ; Clear Divisor Latch Access Bit */
294 /* ; UART_BASE[LCR] &= ~DLAB */
295 0x0c, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x0c] */
296 0x80, 0x10, 0xc1, 0xe3, /* bic r1, r1, #0x80 */
297 0x0c, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0c] */
298
Pali Rohár56452292021-10-27 20:57:00 +0200299 /* ; Loop 0x2dc000 (2998272) cycles */
300 /* ; which is about 5ms on 1200 MHz CPU */
301 /* ; r1 = 0x2dc000 */
302 0xb7, 0x19, 0xa0, 0xe3, /* mov r1, #0x2dc000 */
Pali Rohárca272042021-09-24 23:07:05 +0200303 /* .Lloop_sleep: */
304 0x01, 0x10, 0x41, 0xe2, /* sub r1, r1, #1 */
305 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
306 0xfc, 0xff, 0xff, 0x1a, /* bne .Lloop_sleep */
307
Pali Rohár62a98f42021-11-01 14:00:02 +0100308 /* ; Jump to the end of execution */
309 0x01, 0x00, 0x00, 0xea, /* b end */
Pali Rohárca272042021-09-24 23:07:05 +0200310
311 /* ; Placeholder for old baudrate value */
312 /* old_baudrate: */
313 0x00, 0x00, 0x00, 0x00, /* .word 0 */
314
315 /* ; Placeholder for new baudrate value */
316 /* new_baudrate: */
317 0x00, 0x00, 0x00, 0x00, /* .word 0 */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200318
319 /* end: */
Pali Rohárca272042021-09-24 23:07:05 +0200320};
321
Pali Rohár62a98f42021-11-01 14:00:02 +0100322/* ARM code from binary header executed by BootROM before changing baudrate */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200323static unsigned char kwboot_baud_code_binhdr_pre[] = {
Pali Rohár62a98f42021-11-01 14:00:02 +0100324 /* ; #define UART_BASE 0xd0012000 */
325 /* ; #define THR 0x00 */
326 /* ; #define LSR 0x14 */
327 /* ; #define THRE 0x20 */
328 /* ; */
329 /* ; void send_preamble(void) { */
330 /* ; const u8 *str = "$baudratechange"; */
331 /* ; u8 c; */
332 /* ; do { */
333 /* ; while */
334 /* ; ((readl(UART_BASE + LSR) & THRE)); */
335 /* ; c = *str++; */
336 /* ; writel(UART_BASE + THR, c); */
337 /* ; } while (c); */
338 /* ; } */
339
340 /* ; Preserve registers for BootROM */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200341 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
Pali Rohár62a98f42021-11-01 14:00:02 +0100342
343 /* ; r0 = UART_BASE */
344 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
345 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
346
347 /* ; r2 = address of preamble string */
348 0x00, 0x20, 0x8f, 0xe2, /* adr r2, .Lstr_preamble */
349
350 /* ; Skip preamble data section */
351 0x03, 0x00, 0x00, 0xea, /* b .Lloop_preamble */
352
353 /* ; Preamble string */
354 /* .Lstr_preamble: */
355 0x24, 0x62, 0x61, 0x75, /* .asciz "$baudratechange" */
356 0x64, 0x72, 0x61, 0x74,
357 0x65, 0x63, 0x68, 0x61,
358 0x6e, 0x67, 0x65, 0x00,
359
360 /* ; Send preamble string over UART */
361 /* .Lloop_preamble: */
362 /* */
363 /* ; Wait until Transmitter Holding is Empty */
364 /* .Lloop_thre: */
365 /* ; r1 = UART_BASE[LSR] & THRE */
366 0x14, 0x10, 0x90, 0xe5, /* ldr r1, [r0, #0x14] */
367 0x20, 0x00, 0x11, 0xe3, /* tst r1, #0x20 */
368 0xfc, 0xff, 0xff, 0x0a, /* beq .Lloop_thre */
369
370 /* ; Put character into Transmitter FIFO */
371 /* ; r1 = *r2++ */
372 0x01, 0x10, 0xd2, 0xe4, /* ldrb r1, [r2], #1 */
373 /* ; UART_BASE[THR] = r1 */
374 0x00, 0x10, 0x80, 0xe5, /* str r1, [r0, #0x0] */
375
376 /* ; Loop until end of preamble string */
377 0x00, 0x00, 0x51, 0xe3, /* cmp r1, #0 */
378 0xf8, 0xff, 0xff, 0x1a, /* bne .Lloop_preamble */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200379};
380
Pali Rohár62a98f42021-11-01 14:00:02 +0100381/* ARM code for returning from binary header back to BootROM */
Pali Rohár8dbe0272021-10-27 20:57:02 +0200382static unsigned char kwboot_baud_code_binhdr_post[] = {
383 /* ; Return 0 - no error */
384 0x00, 0x00, 0xa0, 0xe3, /* mov r0, #0 */
385 0xfe, 0x9f, 0xbd, 0xe8, /* pop { r1 - r12, pc } */
386};
387
388/* ARM code for jumping to the original image exec_addr */
389static unsigned char kwboot_baud_code_data_jump[] = {
390 0x04, 0xf0, 0x1f, 0xe5, /* ldr pc, exec_addr */
391 /* ; Placeholder for exec_addr */
392 /* exec_addr: */
393 0x00, 0x00, 0x00, 0x00, /* .word 0 */
394};
Pali Rohárca272042021-09-24 23:07:05 +0200395
396static const char kwb_baud_magic[16] = "$baudratechange";
397
Luka Perkovd131ad62012-05-27 11:44:51 +0000398static int kwboot_verbose;
399
Stefan Roese84899e22014-10-22 12:13:21 +0200400static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith7497a6a2016-02-16 21:28:19 +0000401static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roese84899e22014-10-22 12:13:21 +0200402
Marek Behúne453bb42021-09-24 23:06:41 +0200403static ssize_t
404kwboot_write(int fd, const char *buf, size_t len)
405{
Pali Rohár6ba7d632022-01-25 18:13:10 +0100406 ssize_t tot = 0;
Marek Behúne453bb42021-09-24 23:06:41 +0200407
408 while (tot < len) {
409 ssize_t wr = write(fd, buf + tot, len - tot);
410
Pali Rohár6ba7d632022-01-25 18:13:10 +0100411 if (wr < 0 && errno == EINTR)
412 continue;
413 else if (wr < 0)
414 return wr;
Marek Behúne453bb42021-09-24 23:06:41 +0200415
416 tot += wr;
417 }
418
419 return tot;
420}
421
Luka Perkovd131ad62012-05-27 11:44:51 +0000422static void
423kwboot_printv(const char *fmt, ...)
424{
425 va_list ap;
426
427 if (kwboot_verbose) {
428 va_start(ap, fmt);
429 vprintf(fmt, ap);
430 va_end(ap);
431 fflush(stdout);
432 }
433}
434
435static void
436__spinner(void)
437{
438 const char seq[] = { '-', '\\', '|', '/' };
439 const int div = 8;
440 static int state, bs;
441
442 if (state % div == 0) {
443 fputc(bs, stdout);
444 fputc(seq[state / div % sizeof(seq)], stdout);
445 fflush(stdout);
446 }
447
448 bs = '\b';
449 state++;
450}
451
452static void
453kwboot_spinner(void)
454{
455 if (kwboot_verbose)
456 __spinner();
457}
458
459static void
460__progress(int pct, char c)
461{
462 const int width = 70;
463 static const char *nl = "";
464 static int pos;
465
466 if (pos % width == 0)
467 printf("%s%3d %% [", nl, pct);
468
469 fputc(c, stdout);
470
471 nl = "]\n";
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200472 pos = (pos + 1) % width;
Luka Perkovd131ad62012-05-27 11:44:51 +0000473
474 if (pct == 100) {
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200475 while (pos && pos++ < width)
Luka Perkovd131ad62012-05-27 11:44:51 +0000476 fputc(' ', stdout);
477 fputs(nl, stdout);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200478 nl = "";
479 pos = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000480 }
481
482 fflush(stdout);
483
484}
485
486static void
487kwboot_progress(int _pct, char c)
488{
489 static int pct;
490
491 if (_pct != -1)
492 pct = _pct;
493
494 if (kwboot_verbose)
495 __progress(pct, c);
Pali Rohár5a1f8cb2021-09-24 23:06:46 +0200496
497 if (pct == 100)
498 pct = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000499}
500
501static int
502kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
503{
504 int rc, nfds;
505 fd_set rfds;
506 struct timeval tv;
507 ssize_t n;
508
509 rc = -1;
510
511 FD_ZERO(&rfds);
512 FD_SET(fd, &rfds);
513
514 tv.tv_sec = 0;
515 tv.tv_usec = timeo * 1000;
516 if (tv.tv_usec > 1000000) {
517 tv.tv_sec += tv.tv_usec / 1000000;
518 tv.tv_usec %= 1000000;
519 }
520
521 do {
522 nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
Pali Rohár91fb0952022-01-25 18:13:11 +0100523 if (nfds < 0 && errno == EINTR)
524 continue;
525 else if (nfds < 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000526 goto out;
Pali Rohár91fb0952022-01-25 18:13:11 +0100527 else if (!nfds) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000528 errno = ETIMEDOUT;
529 goto out;
530 }
531
532 n = read(fd, buf, len);
Pali Rohár91fb0952022-01-25 18:13:11 +0100533 if (n < 0 && errno == EINTR)
534 continue;
535 else if (n <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +0000536 goto out;
537
538 buf = (char *)buf + n;
539 len -= n;
540 } while (len > 0);
541
542 rc = 0;
543out:
544 return rc;
545}
546
547static int
Pali Rohárcab817d2021-10-27 20:56:59 +0200548kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovd131ad62012-05-27 11:44:51 +0000549{
Stefan Roese84899e22014-10-22 12:13:21 +0200550 if (!buf)
551 return 0;
552
Marek Behúne453bb42021-09-24 23:06:41 +0200553 if (kwboot_write(fd, buf, len) < 0)
554 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +0000555
Pali Rohárcab817d2021-10-27 20:56:59 +0200556 if (nodrain)
557 return 0;
558
Marek Behúne453bb42021-09-24 23:06:41 +0200559 return tcdrain(fd);
Luka Perkovd131ad62012-05-27 11:44:51 +0000560}
561
562static int
563kwboot_tty_send_char(int fd, unsigned char c)
564{
Pali Rohárcab817d2021-10-27 20:56:59 +0200565 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovd131ad62012-05-27 11:44:51 +0000566}
567
568static speed_t
Pali Rohárca272042021-09-24 23:07:05 +0200569kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000570{
571 switch (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +0200572#ifdef B4000000
573 case 4000000:
574 return B4000000;
575#endif
576#ifdef B3500000
577 case 3500000:
578 return B3500000;
579#endif
580#ifdef B3000000
581 case 3000000:
582 return B3000000;
583#endif
584#ifdef B2500000
585 case 2500000:
586 return B2500000;
587#endif
588#ifdef B2000000
589 case 2000000:
590 return B2000000;
591#endif
592#ifdef B1500000
593 case 1500000:
594 return B1500000;
595#endif
596#ifdef B1152000
597 case 1152000:
598 return B1152000;
599#endif
600#ifdef B1000000
601 case 1000000:
602 return B1000000;
603#endif
604#ifdef B921600
605 case 921600:
606 return B921600;
607#endif
608#ifdef B614400
609 case 614400:
610 return B614400;
611#endif
612#ifdef B576000
613 case 576000:
614 return B576000;
615#endif
616#ifdef B500000
617 case 500000:
618 return B500000;
619#endif
620#ifdef B460800
621 case 460800:
622 return B460800;
623#endif
624#ifdef B307200
625 case 307200:
626 return B307200;
627#endif
628#ifdef B230400
629 case 230400:
630 return B230400;
631#endif
632#ifdef B153600
633 case 153600:
634 return B153600;
635#endif
636#ifdef B115200
Luka Perkovd131ad62012-05-27 11:44:51 +0000637 case 115200:
638 return B115200;
Pali Rohárca272042021-09-24 23:07:05 +0200639#endif
640#ifdef B76800
641 case 76800:
642 return B76800;
643#endif
644#ifdef B57600
Luka Perkovd131ad62012-05-27 11:44:51 +0000645 case 57600:
646 return B57600;
Pali Rohárca272042021-09-24 23:07:05 +0200647#endif
648#ifdef B38400
Luka Perkovd131ad62012-05-27 11:44:51 +0000649 case 38400:
650 return B38400;
Pali Rohárca272042021-09-24 23:07:05 +0200651#endif
652#ifdef B19200
Luka Perkovd131ad62012-05-27 11:44:51 +0000653 case 19200:
654 return B19200;
Pali Rohárca272042021-09-24 23:07:05 +0200655#endif
656#ifdef B9600
Luka Perkovd131ad62012-05-27 11:44:51 +0000657 case 9600:
658 return B9600;
Pali Rohárca272042021-09-24 23:07:05 +0200659#endif
660#ifdef B4800
661 case 4800:
662 return B4800;
663#endif
664#ifdef B2400
665 case 2400:
666 return B2400;
667#endif
668#ifdef B1800
669 case 1800:
670 return B1800;
671#endif
672#ifdef B1200
673 case 1200:
674 return B1200;
675#endif
676#ifdef B600
677 case 600:
678 return B600;
679#endif
680#ifdef B300
681 case 300:
682 return B300;
683#endif
684#ifdef B200
685 case 200:
686 return B200;
687#endif
688#ifdef B150
689 case 150:
690 return B150;
691#endif
692#ifdef B134
693 case 134:
694 return B134;
695#endif
696#ifdef B110
697 case 110:
698 return B110;
699#endif
700#ifdef B75
701 case 75:
702 return B75;
703#endif
704#ifdef B50
705 case 50:
706 return B50;
707#endif
708 default:
Pali Rohár93b55632021-09-24 23:07:06 +0200709#ifdef BOTHER
710 return BOTHER;
711#else
Pali Rohárca272042021-09-24 23:07:05 +0200712 return B0;
Pali Rohár93b55632021-09-24 23:07:06 +0200713#endif
Luka Perkovd131ad62012-05-27 11:44:51 +0000714 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000715}
716
717static int
Marek Behún99a3d0232021-09-24 23:07:07 +0200718_is_within_tolerance(int value, int reference, int tolerance)
719{
720 return 100 * value >= reference * (100 - tolerance) &&
721 100 * value <= reference * (100 + tolerance);
722}
723
724static int
Pali Rohárca272042021-09-24 23:07:05 +0200725kwboot_tty_change_baudrate(int fd, int baudrate)
726{
727 struct termios tio;
728 speed_t speed;
729 int rc;
730
731 rc = tcgetattr(fd, &tio);
732 if (rc)
733 return rc;
734
735 speed = kwboot_tty_baudrate_to_speed(baudrate);
736 if (speed == B0) {
737 errno = EINVAL;
738 return -1;
739 }
740
Pali Rohár93b55632021-09-24 23:07:06 +0200741#ifdef BOTHER
742 if (speed == BOTHER)
743 tio.c_ospeed = tio.c_ispeed = baudrate;
744#endif
745
Pali Rohárca272042021-09-24 23:07:05 +0200746 rc = cfsetospeed(&tio, speed);
747 if (rc)
748 return rc;
749
750 rc = cfsetispeed(&tio, speed);
751 if (rc)
752 return rc;
753
754 rc = tcsetattr(fd, TCSANOW, &tio);
755 if (rc)
756 return rc;
757
Marek Behún99a3d0232021-09-24 23:07:07 +0200758 rc = tcgetattr(fd, &tio);
759 if (rc)
760 return rc;
761
762 if (cfgetospeed(&tio) != speed || cfgetispeed(&tio) != speed)
763 goto baud_fail;
764
765#ifdef BOTHER
766 /*
767 * Check whether set baudrate is within 3% tolerance.
768 * If BOTHER is defined, Linux always fills out c_ospeed / c_ispeed
769 * with real values.
770 */
771 if (!_is_within_tolerance(tio.c_ospeed, baudrate, 3))
772 goto baud_fail;
773
774 if (!_is_within_tolerance(tio.c_ispeed, baudrate, 3))
775 goto baud_fail;
776#endif
777
Pali Rohárca272042021-09-24 23:07:05 +0200778 return 0;
Marek Behún99a3d0232021-09-24 23:07:07 +0200779
780baud_fail:
781 fprintf(stderr, "Could not set baudrate to requested value\n");
782 errno = EINVAL;
783 return -1;
Pali Rohárca272042021-09-24 23:07:05 +0200784}
785
786static int
787kwboot_open_tty(const char *path, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +0000788{
Pali Rohár911515b2021-09-24 23:07:10 +0200789 int rc, fd, flags;
Luka Perkovd131ad62012-05-27 11:44:51 +0000790 struct termios tio;
791
792 rc = -1;
793
Marek Behún5fa04f42021-09-24 23:07:11 +0200794 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovd131ad62012-05-27 11:44:51 +0000795 if (fd < 0)
796 goto out;
797
Pali Rohárc704e0e2021-09-24 23:07:08 +0200798 rc = tcgetattr(fd, &tio);
799 if (rc)
800 goto out;
Luka Perkovd131ad62012-05-27 11:44:51 +0000801
Pali Rohárc704e0e2021-09-24 23:07:08 +0200802 cfmakeraw(&tio);
Marek Behún5fa04f42021-09-24 23:07:11 +0200803 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár2ecca3d2021-10-25 15:12:53 +0200804 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovd131ad62012-05-27 11:44:51 +0000805 tio.c_cc[VMIN] = 1;
Pali Rohár24a471b2021-09-24 23:07:09 +0200806 tio.c_cc[VTIME] = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000807
Luka Perkovd131ad62012-05-27 11:44:51 +0000808 rc = tcsetattr(fd, TCSANOW, &tio);
809 if (rc)
810 goto out;
811
Pali Rohár911515b2021-09-24 23:07:10 +0200812 flags = fcntl(fd, F_GETFL);
813 if (flags < 0)
814 goto out;
815
816 rc = fcntl(fd, F_SETFL, flags & ~O_NDELAY);
817 if (rc)
818 goto out;
819
Pali Rohárca272042021-09-24 23:07:05 +0200820 rc = kwboot_tty_change_baudrate(fd, baudrate);
821 if (rc)
822 goto out;
823
Luka Perkovd131ad62012-05-27 11:44:51 +0000824 rc = fd;
825out:
826 if (rc < 0) {
827 if (fd >= 0)
828 close(fd);
829 }
830
831 return rc;
832}
833
Pali Rohár913866a2022-03-02 11:49:21 +0100834static void *
835kwboot_msg_write_handler(void *arg)
836{
837 int tty = *(int *)((void **)arg)[0];
838 const void *msg = ((void **)arg)[1];
839 int rsp_timeo = msg_rsp_timeo;
840 int i, dummy_oldtype;
841
842 /* allow to cancel this thread at any time */
843 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &dummy_oldtype);
844
845 while (1) {
846 /* write 128 samples of message pattern into the output queue without waiting */
847 for (i = 0; i < 128; i++) {
848 if (kwboot_tty_send(tty, msg, 8, 1) < 0) {
849 perror("\nFailed to send message pattern");
850 exit(1);
851 }
852 }
853 /* wait until output queue is transmitted and then make pause */
854 if (tcdrain(tty) < 0) {
855 perror("\nFailed to send message pattern");
856 exit(1);
857 }
858 /* BootROM requires pause on UART after it detects message pattern */
859 usleep(rsp_timeo * 1000);
860 }
861}
862
863static int
864kwboot_msg_start_thread(pthread_t *thread, int *tty, void *msg)
865{
866 void *arg[2];
867 int rc;
868
869 arg[0] = tty;
870 arg[1] = msg;
871 rc = pthread_create(thread, NULL, kwboot_msg_write_handler, arg);
872 if (rc) {
873 errno = rc;
874 return -1;
875 }
876
877 return 0;
878}
879
880static int
881kwboot_msg_stop_thread(pthread_t thread)
882{
883 int rc;
884
885 rc = pthread_cancel(thread);
886 if (rc) {
887 errno = rc;
888 return -1;
889 }
890
891 rc = pthread_join(thread, NULL);
892 if (rc) {
893 errno = rc;
894 return -1;
895 }
896
897 return 0;
898}
899
Luka Perkovd131ad62012-05-27 11:44:51 +0000900static int
Pali Rohárc1d911f2022-03-02 11:49:20 +0100901kwboot_bootmsg(int tty)
Luka Perkovd131ad62012-05-27 11:44:51 +0000902{
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100903 struct kwboot_block block;
Pali Rohár913866a2022-03-02 11:49:21 +0100904 pthread_t write_thread;
905 int rc, err;
Luka Perkovd131ad62012-05-27 11:44:51 +0000906 char c;
Pali Rohár913866a2022-03-02 11:49:21 +0100907
908 /* flush input and output queue */
909 tcflush(tty, TCIOFLUSH);
910
911 rc = kwboot_msg_start_thread(&write_thread, &tty, kwboot_msg_boot);
912 if (rc) {
913 perror("Failed to start write thread");
914 return rc;
915 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000916
Pali Rohárc1d911f2022-03-02 11:49:20 +0100917 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovd131ad62012-05-27 11:44:51 +0000918
Pali Rohár913866a2022-03-02 11:49:21 +0100919 err = 0;
920 while (1) {
Luka Perkovd131ad62012-05-27 11:44:51 +0000921 kwboot_spinner();
922
Pali Rohár913866a2022-03-02 11:49:21 +0100923 rc = kwboot_tty_recv(tty, &c, 1, msg_rsp_timeo);
924 if (rc && errno == ETIMEDOUT) {
925 continue;
926 } else if (rc) {
927 err = errno;
928 break;
929 }
930
931 if (c == NAK)
932 break;
933 }
Luka Perkovd131ad62012-05-27 11:44:51 +0000934
935 kwboot_printv("\n");
936
Pali Rohár913866a2022-03-02 11:49:21 +0100937 rc = kwboot_msg_stop_thread(write_thread);
938 if (rc) {
939 perror("Failed to stop write thread");
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100940 return rc;
Pali Rohár913866a2022-03-02 11:49:21 +0100941 }
942
943 if (err) {
944 errno = err;
945 perror("Failed to read response for boot message pattern");
946 return -1;
947 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100948
949 /*
950 * At this stage we have sent more boot message patterns and BootROM
951 * (at least on Armada XP and 385) started interpreting sent bytes as
952 * part of xmodem packets. If BootROM is expecting SOH byte as start of
953 * a xmodem packet and it receives byte 0xff, then it throws it away and
954 * sends a NAK reply to host. If BootROM does not receive any byte for
955 * 2s when expecting some continuation of the xmodem packet, it throws
956 * away the partially received xmodem data and sends NAK reply to host.
957 *
958 * Therefore for starting xmodem transfer we have two options: Either
959 * wait 2s or send 132 0xff bytes (which is the size of xmodem packet)
960 * to ensure that BootROM throws away any partially received data.
961 */
962
963 /* flush output queue with remaining boot message patterns */
Pali Rohárd8865f82022-03-02 11:49:18 +0100964 rc = tcflush(tty, TCOFLUSH);
965 if (rc) {
966 perror("Failed to flush output queue");
967 return rc;
968 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100969
970 /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */
971 memset(&block, 0xff, sizeof(block));
Pali Rohárd8865f82022-03-02 11:49:18 +0100972 rc = kwboot_tty_send(tty, &block, sizeof(block), 0);
973 if (rc) {
974 perror("Failed to send sync sequence");
975 return rc;
976 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100977
978 /*
979 * Sending 132 bytes via 115200B/8-N-1 takes 11.45 ms, reading 132 bytes
980 * takes 11.45 ms, so waiting for 30 ms should be enough.
981 */
982 usleep(30 * 1000);
983
984 /* flush remaining NAK replies from input queue */
Pali Rohárd8865f82022-03-02 11:49:18 +0100985 rc = tcflush(tty, TCIFLUSH);
986 if (rc) {
987 perror("Failed to flush input queue");
988 return rc;
989 }
Pali Rohár2bcd5b12022-01-25 18:13:08 +0100990
991 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +0000992}
993
994static int
Pali Rohárc1d911f2022-03-02 11:49:20 +0100995kwboot_debugmsg(int tty)
Stefan Roese84899e22014-10-22 12:13:21 +0200996{
Pali Rohár93976af2022-03-02 11:49:22 +0100997 unsigned char buf[8192];
998 pthread_t write_thread;
999 int rc, err, i, pos;
1000 size_t off;
1001
1002 /* flush input and output queue */
1003 tcflush(tty, TCIOFLUSH);
1004
1005 rc = kwboot_msg_start_thread(&write_thread, &tty, kwboot_msg_debug);
1006 if (rc) {
1007 perror("Failed to start write thread");
1008 return rc;
1009 }
Stefan Roese84899e22014-10-22 12:13:21 +02001010
1011 kwboot_printv("Sending debug message. Please reboot the target...");
Pali Rohár93976af2022-03-02 11:49:22 +01001012 kwboot_spinner();
Stefan Roese84899e22014-10-22 12:13:21 +02001013
Pali Rohár93976af2022-03-02 11:49:22 +01001014 err = 0;
1015 off = 0;
1016 while (1) {
1017 /* Read immediately all bytes in queue without waiting */
1018 rc = read(tty, buf + off, sizeof(buf) - off);
1019 if ((rc < 0 && errno == EINTR) || rc == 0) {
1020 continue;
1021 } else if (rc < 0) {
1022 err = errno;
Stefan Roese84899e22014-10-22 12:13:21 +02001023 break;
Pali Rohár93976af2022-03-02 11:49:22 +01001024 }
1025 off += rc - 1;
Stefan Roese84899e22014-10-22 12:13:21 +02001026
1027 kwboot_spinner();
1028
Pali Rohár93976af2022-03-02 11:49:22 +01001029 /*
1030 * Check if we received at least 4 debug message patterns
1031 * (console echo from BootROM) in cyclic buffer
1032 */
1033
1034 for (pos = 0; pos < sizeof(kwboot_msg_debug); pos++)
1035 if (buf[off] == kwboot_msg_debug[(pos + off) % sizeof(kwboot_msg_debug)])
1036 break;
1037
1038 for (i = off; i >= 0; i--)
1039 if (buf[i] != kwboot_msg_debug[(pos + i) % sizeof(kwboot_msg_debug)])
1040 break;
1041
1042 off -= i;
1043
1044 if (off >= 4 * sizeof(kwboot_msg_debug))
1045 break;
1046
1047 /* If not move valid suffix from end of the buffer to the beginning of buffer */
1048 memmove(buf, buf + i + 1, off);
1049 }
Stefan Roese84899e22014-10-22 12:13:21 +02001050
1051 kwboot_printv("\n");
1052
Pali Rohár93976af2022-03-02 11:49:22 +01001053 rc = kwboot_msg_stop_thread(write_thread);
1054 if (rc) {
1055 perror("Failed to stop write thread");
1056 return rc;
1057 }
1058
1059 if (err) {
1060 errno = err;
1061 perror("Failed to read response for debug message pattern");
1062 return -1;
1063 }
1064
1065 /* flush output queue with remaining debug message patterns */
1066 rc = tcflush(tty, TCOFLUSH);
1067 if (rc) {
1068 perror("Failed to flush output queue");
1069 return rc;
1070 }
1071
1072 kwboot_printv("Clearing input buffer...\n");
1073
1074 /*
1075 * Wait until BootROM transmit all remaining echo characters.
1076 * Experimentally it was measured that for Armada 385 BootROM
1077 * it is required to wait at least 0.415s. So wait 0.5s.
1078 */
1079 usleep(500 * 1000);
1080
1081 /*
1082 * In off variable is stored number of characters received after the
1083 * successful detection of echo reply. So these characters are console
1084 * echo for other following debug message patterns. BootROM may have in
1085 * its output queue other echo characters which were being transmitting
1086 * before above sleep call. So read remaining number of echo characters
1087 * sent by the BootROM now.
1088 */
1089 while ((rc = kwboot_tty_recv(tty, &buf[0], 1, 0)) == 0)
1090 off++;
1091 if (errno != ETIMEDOUT) {
1092 perror("Failed to read response");
1093 return rc;
1094 }
1095
1096 /*
1097 * Clear every echo character set by the BootROM by backspace byte.
1098 * This is required prior writing any command to the BootROM debug
1099 * because BootROM command line buffer has limited size. If length
1100 * of the command is larger than buffer size then it looks like
1101 * that Armada 385 BootROM crashes after sending ENTER. So erase it.
1102 * Experimentally it was measured that for Armada 385 BootROM it is
1103 * required to send at least 3 backspace bytes for one echo character.
1104 * This is unknown why. But lets do it.
1105 */
1106 off *= 3;
1107 memset(buf, '\x08', sizeof(buf));
1108 while (off > sizeof(buf)) {
1109 rc = kwboot_tty_send(tty, buf, sizeof(buf), 1);
1110 if (rc) {
1111 perror("Failed to send clear sequence");
1112 return rc;
1113 }
1114 off -= sizeof(buf);
1115 }
1116 rc = kwboot_tty_send(tty, buf, off, 0);
1117 if (rc) {
1118 perror("Failed to send clear sequence");
1119 return rc;
1120 }
1121
1122 usleep(msg_rsp_timeo * 1000);
1123 rc = tcflush(tty, TCIFLUSH);
1124 if (rc) {
1125 perror("Failed to flush input queue");
1126 return rc;
1127 }
1128
1129 return 0;
Stefan Roese84899e22014-10-22 12:13:21 +02001130}
1131
Pali Rohárc5d666a2021-09-24 23:06:44 +02001132static size_t
Luka Perkovd131ad62012-05-27 11:44:51 +00001133kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
1134 size_t size, int pnum)
1135{
Marek Behúnd8cc8512021-09-24 23:06:45 +02001136 size_t i, n;
Luka Perkovd131ad62012-05-27 11:44:51 +00001137
Stefan Roese84899e22014-10-22 12:13:21 +02001138 block->soh = SOH;
Luka Perkovd131ad62012-05-27 11:44:51 +00001139 block->pnum = pnum;
1140 block->_pnum = ~block->pnum;
1141
Pali Rohár2ef87f72021-09-24 23:06:48 +02001142 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovd131ad62012-05-27 11:44:51 +00001143 memcpy(&block->data[0], data, n);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001144 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovd131ad62012-05-27 11:44:51 +00001145
1146 block->csum = 0;
1147 for (i = 0; i < n; i++)
1148 block->csum += block->data[i];
1149
1150 return n;
1151}
1152
Marek Behún12df7b72021-09-24 23:06:52 +02001153static uint64_t
1154_now(void)
1155{
1156 struct timespec ts;
1157
1158 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
1159 static int err_print;
1160
1161 if (!err_print) {
1162 perror("clock_gettime() does not work");
1163 err_print = 1;
1164 }
1165
1166 /* this will just make the timeout not work */
1167 return -1ULL;
1168 }
1169
1170 return ts.tv_sec * 1000ULL + (ts.tv_nsec + 500000) / 1000000;
1171}
1172
Luka Perkovd131ad62012-05-27 11:44:51 +00001173static int
Marek Behún408ea612021-09-24 23:06:49 +02001174_is_xm_reply(char c)
1175{
Pali Rohár94c906a2022-01-25 18:13:03 +01001176 return c == ACK || c == NAK;
Marek Behún408ea612021-09-24 23:06:49 +02001177}
1178
1179static int
Pali Rohár9cdc2642021-09-24 23:06:54 +02001180_xm_reply_to_error(int c)
1181{
1182 int rc = -1;
1183
1184 switch (c) {
1185 case ACK:
1186 rc = 0;
1187 break;
1188 case NAK:
1189 errno = EBADMSG;
1190 break;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001191 default:
1192 errno = EPROTO;
1193 break;
1194 }
1195
1196 return rc;
1197}
1198
1199static int
Pali Rohárca272042021-09-24 23:07:05 +02001200kwboot_baud_magic_handle(int fd, char c, int baudrate)
1201{
1202 static size_t rcv_len;
1203
1204 if (rcv_len < sizeof(kwb_baud_magic)) {
1205 /* try to recognize whole magic word */
1206 if (c == kwb_baud_magic[rcv_len]) {
1207 rcv_len++;
1208 } else {
1209 printf("%.*s%c", (int)rcv_len, kwb_baud_magic, c);
1210 fflush(stdout);
1211 rcv_len = 0;
1212 }
1213 }
1214
1215 if (rcv_len == sizeof(kwb_baud_magic)) {
1216 /* magic word received */
1217 kwboot_printv("\nChanging baudrate to %d Bd\n", baudrate);
1218
1219 return kwboot_tty_change_baudrate(fd, baudrate) ? : 1;
1220 } else {
1221 return 0;
1222 }
1223}
1224
1225static int
Pali Rohár950ed242022-01-25 18:13:04 +01001226kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Rohár82a9e132022-01-25 18:13:02 +01001227 int ignore_nak_reply,
Pali Rohára6fcac22021-10-25 15:13:04 +02001228 int allow_non_xm, int *non_xm_print,
Pali Rohárca272042021-09-24 23:07:05 +02001229 int baudrate, int *baud_changed)
Pali Rohár48b3ea62021-09-24 23:06:50 +02001230{
Marek Behún12df7b72021-09-24 23:06:52 +02001231 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún819cd322021-09-24 23:06:53 +02001232 uint64_t recv_until = _now() + timeout;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001233 int rc;
1234
1235 while (1) {
Marek Behún12df7b72021-09-24 23:06:52 +02001236 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohár48b3ea62021-09-24 23:06:50 +02001237 if (rc) {
1238 if (errno != ETIMEDOUT)
1239 return rc;
Marek Behún819cd322021-09-24 23:06:53 +02001240 else if (allow_non_xm && *non_xm_print)
Marek Behún12df7b72021-09-24 23:06:52 +02001241 return -1;
1242 else
1243 *c = NAK;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001244 }
1245
1246 /* If received xmodem reply, end. */
Pali Rohár82a9e132022-01-25 18:13:02 +01001247 if (_is_xm_reply(*c)) {
1248 if (*c == NAK && ignore_nak_reply) {
1249 timeout = recv_until - _now();
1250 if (timeout >= 0)
1251 continue;
1252 }
Pali Rohár48b3ea62021-09-24 23:06:50 +02001253 break;
Pali Rohár82a9e132022-01-25 18:13:02 +01001254 }
Pali Rohár48b3ea62021-09-24 23:06:50 +02001255
1256 /*
Pali Rohárca272042021-09-24 23:07:05 +02001257 * If receiving/printing non-xmodem text output is allowed and
1258 * such a byte was received, we want to increase receiving time
1259 * and either:
1260 * - print the byte, if it is not part of baudrate change magic
1261 * sequence while baudrate change was requested (-B option)
1262 * - change baudrate
Marek Behún819cd322021-09-24 23:06:53 +02001263 * Otherwise decrease timeout by time elapsed.
Pali Rohár48b3ea62021-09-24 23:06:50 +02001264 */
1265 if (allow_non_xm) {
Marek Behún12df7b72021-09-24 23:06:52 +02001266 recv_until = _now() + timeout;
Pali Rohárca272042021-09-24 23:07:05 +02001267
1268 if (baudrate && !*baud_changed) {
1269 rc = kwboot_baud_magic_handle(fd, *c, baudrate);
1270 if (rc == 1)
1271 *baud_changed = 1;
1272 else if (!rc)
1273 *non_xm_print = 1;
1274 else
1275 return rc;
1276 } else if (!baudrate || !*baud_changed) {
1277 putchar(*c);
1278 fflush(stdout);
1279 *non_xm_print = 1;
1280 }
Marek Behún819cd322021-09-24 23:06:53 +02001281 } else {
Pali Rohár950ed242022-01-25 18:13:04 +01001282 if (stop_on_non_xm)
Pali Rohára6fcac22021-10-25 15:13:04 +02001283 break;
Marek Behún819cd322021-09-24 23:06:53 +02001284 timeout = recv_until - _now();
1285 if (timeout < 0) {
1286 errno = ETIMEDOUT;
1287 return -1;
1288 }
Pali Rohár48b3ea62021-09-24 23:06:50 +02001289 }
1290 }
1291
1292 return 0;
1293}
1294
1295static int
1296kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm,
Pali Rohár5875ad42022-01-25 18:13:05 +01001297 int *done_print, int baudrate, int allow_retries)
Luka Perkovd131ad62012-05-27 11:44:51 +00001298{
Pali Rohárca272042021-09-24 23:07:05 +02001299 int non_xm_print, baud_changed;
1300 int rc, err, retries;
Luka Perkovd131ad62012-05-27 11:44:51 +00001301 char c;
1302
Pali Rohár48b3ea62021-09-24 23:06:50 +02001303 *done_print = 0;
Pali Rohár455c0d22021-10-27 20:56:58 +02001304 non_xm_print = 0;
1305 baud_changed = 0;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001306
Pali Rohárd14a3422021-10-25 15:13:03 +02001307 retries = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001308 do {
Pali Rohárcab817d2021-10-27 20:56:59 +02001309 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovd131ad62012-05-27 11:44:51 +00001310 if (rc)
Pali Rohár94c906a2022-01-25 18:13:03 +01001311 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001312
Pali Rohár48b3ea62021-09-24 23:06:50 +02001313 if (allow_non_xm && !*done_print) {
1314 kwboot_progress(100, '.');
1315 kwboot_printv("Done\n");
1316 *done_print = 1;
1317 }
Stefan Roese84899e22014-10-22 12:13:21 +02001318
Pali Rohára6fcac22021-10-25 15:13:04 +02001319 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Rohár82a9e132022-01-25 18:13:02 +01001320 retries > 8,
Pali Rohára6fcac22021-10-25 15:13:04 +02001321 allow_non_xm, &non_xm_print,
Pali Rohárca272042021-09-24 23:07:05 +02001322 baudrate, &baud_changed);
Pali Rohár48b3ea62021-09-24 23:06:50 +02001323 if (rc)
Pali Rohár94c906a2022-01-25 18:13:03 +01001324 goto err;
Stefan Roese84899e22014-10-22 12:13:21 +02001325
Pali Rohár5d8aa4c2022-01-25 18:13:06 +01001326 if (!allow_non_xm && c != ACK) {
1327 if (c == NAK && allow_retries && retries + 1 < 16)
1328 kwboot_progress(-1, '+');
1329 else
1330 kwboot_progress(-1, 'E');
1331 }
Pali Rohár5875ad42022-01-25 18:13:05 +01001332 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovd131ad62012-05-27 11:44:51 +00001333
Marek Behún2e81b3a2021-09-24 23:06:51 +02001334 if (non_xm_print)
1335 kwboot_printv("\n");
1336
Pali Rohárca272042021-09-24 23:07:05 +02001337 if (allow_non_xm && baudrate && !baud_changed) {
1338 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohárca272042021-09-24 23:07:05 +02001339 errno = EPROTO;
Pali Rohár94c906a2022-01-25 18:13:03 +01001340 return -1;
Pali Rohárca272042021-09-24 23:07:05 +02001341 }
1342
Pali Rohár9cdc2642021-09-24 23:06:54 +02001343 return _xm_reply_to_error(c);
Pali Rohár94c906a2022-01-25 18:13:03 +01001344err:
Pali Rohárca272042021-09-24 23:07:05 +02001345 err = errno;
Pali Rohárca272042021-09-24 23:07:05 +02001346 kwboot_printv("\n");
1347 errno = err;
1348 return rc;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001349}
Luka Perkovd131ad62012-05-27 11:44:51 +00001350
Pali Rohár9cdc2642021-09-24 23:06:54 +02001351static int
1352kwboot_xm_finish(int fd)
1353{
1354 int rc, retries;
1355 char c;
Luka Perkovd131ad62012-05-27 11:44:51 +00001356
Pali Rohár9cdc2642021-09-24 23:06:54 +02001357 kwboot_printv("Finishing transfer\n");
1358
Pali Rohárd14a3422021-10-25 15:13:03 +02001359 retries = 0;
Pali Rohár9cdc2642021-09-24 23:06:54 +02001360 do {
1361 rc = kwboot_tty_send_char(fd, EOT);
1362 if (rc)
1363 return rc;
1364
Pali Rohára6fcac22021-10-25 15:13:04 +02001365 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Rohár82a9e132022-01-25 18:13:02 +01001366 retries > 8,
Pali Rohára6fcac22021-10-25 15:13:04 +02001367 0, NULL, 0, NULL);
Pali Rohár9cdc2642021-09-24 23:06:54 +02001368 if (rc)
1369 return rc;
Pali Rohárd14a3422021-10-25 15:13:03 +02001370 } while (c == NAK && retries++ < 16);
Pali Rohár9cdc2642021-09-24 23:06:54 +02001371
1372 return _xm_reply_to_error(c);
Luka Perkovd131ad62012-05-27 11:44:51 +00001373}
1374
1375static int
Pali Rohár2ef87f72021-09-24 23:06:48 +02001376kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohárca272042021-09-24 23:07:05 +02001377 size_t size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001378{
Pali Rohár48b3ea62021-09-24 23:06:50 +02001379 int done_print = 0;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001380 size_t sent, left;
1381 int rc;
Luka Perkovd131ad62012-05-27 11:44:51 +00001382
Pali Rohár2ef87f72021-09-24 23:06:48 +02001383 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1384 header ? "header" : "data", size);
Luka Perkovd131ad62012-05-27 11:44:51 +00001385
Pali Rohár2ef87f72021-09-24 23:06:48 +02001386 left = size;
1387 sent = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001388
Pali Rohár2ef87f72021-09-24 23:06:48 +02001389 while (sent < size) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001390 struct kwboot_block block;
Pali Rohár48b3ea62021-09-24 23:06:50 +02001391 int last_block;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001392 size_t blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001393
Pali Rohár2ef87f72021-09-24 23:06:48 +02001394 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1395 data += blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001396
Pali Rohár48b3ea62021-09-24 23:06:50 +02001397 last_block = (left <= blksz);
1398
Pali Rohár5875ad42022-01-25 18:13:05 +01001399 /*
1400 * Handling of repeated xmodem packets is completely broken in
1401 * Armada 385 BootROM - it completely ignores xmodem packet
1402 * numbers, they are only used for checksum verification.
1403 * BootROM can handle a retry of the xmodem packet only during
1404 * the transmission of kwbimage header and only if BootROM
1405 * itself sent NAK response to previous attempt (it does it on
1406 * checksum failure). During the transmission of kwbimage data
1407 * part, BootROM always expects next xmodem packet, even if it
1408 * sent NAK to previous attempt - there is absolutely no way to
1409 * repair incorrectly transmitted xmodem packet during kwbimage
1410 * data part upload. Also, if kwboot receives non-ACK/NAK
1411 * response (meaning that original BootROM response was damaged
1412 * on UART) there is no way to detect if BootROM accepted xmodem
1413 * packet or not and no way to check if kwboot could repeat the
1414 * packet or not.
1415 *
1416 * Stop transfer and return failure if kwboot receives unknown
1417 * reply if non-xmodem reply is not allowed (for all xmodem
1418 * packets except the last header packet) or when non-ACK reply
1419 * is received during data part transfer.
1420 */
Pali Rohár48b3ea62021-09-24 23:06:50 +02001421 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár5875ad42022-01-25 18:13:05 +01001422 &done_print, baudrate, header);
Luka Perkovd131ad62012-05-27 11:44:51 +00001423 if (rc)
1424 goto out;
1425
Pali Rohár2ef87f72021-09-24 23:06:48 +02001426 sent += blksz;
1427 left -= blksz;
Luka Perkovd131ad62012-05-27 11:44:51 +00001428
Pali Rohár48b3ea62021-09-24 23:06:50 +02001429 if (!done_print)
1430 kwboot_progress(sent * 100 / size, '.');
Pali Rohár2ef87f72021-09-24 23:06:48 +02001431 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001432
Pali Rohár48b3ea62021-09-24 23:06:50 +02001433 if (!done_print)
1434 kwboot_printv("Done\n");
Pali Rohár2ef87f72021-09-24 23:06:48 +02001435
1436 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001437out:
Pali Rohárd5ba8db2021-09-24 23:06:47 +02001438 kwboot_printv("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001439 return rc;
Pali Rohár2ef87f72021-09-24 23:06:48 +02001440}
Luka Perkovd131ad62012-05-27 11:44:51 +00001441
Pali Rohár2ef87f72021-09-24 23:06:48 +02001442static int
Pali Rohárca272042021-09-24 23:07:05 +02001443kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohár2ef87f72021-09-24 23:06:48 +02001444{
1445 const uint8_t *img = _img;
1446 int rc, pnum;
1447 size_t hdrsz;
1448
Marek Behúnfe2fd732021-09-24 23:07:01 +02001449 hdrsz = kwbheader_size(img);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001450
Pali Rohárf8017c32021-11-05 23:29:58 +01001451 /*
1452 * If header size is not aligned to xmodem block size (which applies
1453 * for all images in kwbimage v0 format) then we have to ensure that
1454 * the last xmodem block of header contains beginning of the data
1455 * followed by the header. So align header size to xmodem block size.
1456 */
1457 hdrsz += (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % KWBOOT_XM_BLKSZ;
1458
Pali Rohár2ef87f72021-09-24 23:06:48 +02001459 pnum = 1;
1460
Pali Rohárca272042021-09-24 23:07:05 +02001461 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohár2ef87f72021-09-24 23:06:48 +02001462 if (rc)
1463 return rc;
1464
Pali Rohárf8017c32021-11-05 23:29:58 +01001465 /*
1466 * If we have already sent image data as a part of the last
1467 * xmodem header block then we have nothing more to send.
1468 */
1469 if (hdrsz < size) {
1470 img += hdrsz;
1471 size -= hdrsz;
1472 rc = kwboot_xmodem_one(tty, &pnum, 0, img, size, 0);
1473 if (rc)
1474 return rc;
1475 }
Pali Rohár2ef87f72021-09-24 23:06:48 +02001476
Pali Rohárca272042021-09-24 23:07:05 +02001477 rc = kwboot_xm_finish(tty);
1478 if (rc)
1479 return rc;
1480
1481 if (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +02001482 kwboot_printv("\nChanging baudrate back to 115200 Bd\n\n");
1483 rc = kwboot_tty_change_baudrate(tty, 115200);
1484 if (rc)
1485 return rc;
1486 }
1487
1488 return 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001489}
1490
1491static int
Pali Roháre8d26e82022-03-02 11:49:23 +01001492kwboot_term_pipe(int in, int out, const char *quit, int *s, const char *kbs, int *k)
Luka Perkovd131ad62012-05-27 11:44:51 +00001493{
Pali Rohárde751402022-02-03 17:45:20 +01001494 char buf[128];
Pali Roháre8d26e82022-03-02 11:49:23 +01001495 ssize_t nin, noff;
Luka Perkovd131ad62012-05-27 11:44:51 +00001496
Pali Rohárde751402022-02-03 17:45:20 +01001497 nin = read(in, buf, sizeof(buf));
Willy Tarreau4469bd72018-07-03 12:10:31 -04001498 if (nin <= 0)
Luka Perkovd131ad62012-05-27 11:44:51 +00001499 return -1;
1500
Pali Roháre8d26e82022-03-02 11:49:23 +01001501 noff = 0;
1502
1503 if (quit || kbs) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001504 int i;
1505
1506 for (i = 0; i < nin; i++) {
Pali Roháre8d26e82022-03-02 11:49:23 +01001507 if ((quit || kbs) &&
1508 (!quit || buf[i] != quit[*s]) &&
1509 (!kbs || buf[i] != kbs[*k])) {
1510 const char *prefix;
1511 int plen;
1512
1513 if (quit && kbs) {
1514 prefix = (*s >= *k) ? quit : kbs;
1515 plen = (*s >= *k) ? *s : *k;
1516 } else if (quit) {
1517 prefix = quit;
1518 plen = *s;
1519 } else {
1520 prefix = kbs;
1521 plen = *k;
1522 }
1523
1524 if (plen > i && kwboot_write(out, prefix, plen - i) < 0)
1525 return -1;
1526 }
1527
1528 if (quit && buf[i] == quit[*s]) {
Luka Perkovd131ad62012-05-27 11:44:51 +00001529 (*s)++;
Pali Rohárde751402022-02-03 17:45:20 +01001530 if (!quit[*s]) {
Pali Rohár7938b3b2022-02-18 12:24:13 +01001531 nin = (i > *s) ? (i - *s) : 0;
Pali Rohárde751402022-02-03 17:45:20 +01001532 break;
1533 }
Pali Roháre8d26e82022-03-02 11:49:23 +01001534 } else if (quit) {
Marek Behúne453bb42021-09-24 23:06:41 +02001535 *s = 0;
Pali Rohárb943eee2021-07-23 11:14:20 +02001536 }
Pali Roháre8d26e82022-03-02 11:49:23 +01001537
1538 if (kbs && buf[i] == kbs[*k]) {
1539 (*k)++;
1540 if (!kbs[*k]) {
1541 if (i > *k + noff &&
1542 kwboot_write(out, buf + noff, i - *k - noff) < 0)
1543 return -1;
1544 /*
1545 * Replace backspace key by '\b' (0x08)
1546 * byte which is the only recognized
1547 * backspace byte by Marvell BootROM.
1548 */
1549 if (write(out, "\x08", 1) < 0)
1550 return -1;
1551 noff = i + 1;
1552 *k = 0;
1553 }
1554 } else if (kbs) {
1555 *k = 0;
1556 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001557 }
Pali Rohárde751402022-02-03 17:45:20 +01001558
Pali Roháre8d26e82022-03-02 11:49:23 +01001559 if (i == nin) {
1560 i = 0;
1561 if (quit && i < *s)
1562 i = *s;
1563 if (kbs && i < *k)
1564 i = *k;
1565 nin -= (nin > i) ? i : nin;
1566 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001567 }
1568
Pali Roháre8d26e82022-03-02 11:49:23 +01001569 if (nin > noff && kwboot_write(out, buf + noff, nin - noff) < 0)
Marek Behúne453bb42021-09-24 23:06:41 +02001570 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001571
1572 return 0;
1573}
1574
1575static int
1576kwboot_terminal(int tty)
1577{
Pali Roháre8d26e82022-03-02 11:49:23 +01001578 int rc, in, s, k;
1579 const char *kbs = NULL;
Marek Behún46237e62021-09-24 23:06:40 +02001580 const char *quit = "\34c";
Luka Perkovd131ad62012-05-27 11:44:51 +00001581 struct termios otio, tio;
1582
1583 rc = -1;
1584
1585 in = STDIN_FILENO;
1586 if (isatty(in)) {
1587 rc = tcgetattr(in, &otio);
1588 if (!rc) {
1589 tio = otio;
1590 cfmakeraw(&tio);
1591 rc = tcsetattr(in, TCSANOW, &tio);
1592 }
1593 if (rc) {
1594 perror("tcsetattr");
1595 goto out;
1596 }
1597
Pali Roháre8d26e82022-03-02 11:49:23 +01001598 /*
1599 * Get sequence for backspace key used by the current
1600 * terminal. Every occurrence of this sequence will be
1601 * replaced by '\b' byte which is the only recognized
1602 * backspace byte by Marvell BootROM.
1603 *
1604 * Note that we cannot read this sequence from termios
1605 * c_cc[VERASE] as VERASE is valid only when ICANON is
1606 * set in termios c_lflag, which is not case for us.
1607 *
1608 * Also most terminals do not set termios c_cc[VERASE]
1609 * as c_cc[VERASE] can specify only one-byte sequence
1610 * and instead let applications to read (possible
1611 * multi-byte) sequence for backspace key from "kbs"
1612 * terminfo database based on $TERM env variable.
1613 *
1614 * So read "kbs" from terminfo database via tigetstr()
1615 * call after successful setupterm(). Most terminals
1616 * use byte 0x7F for backspace key, so replacement with
1617 * '\b' is required.
1618 */
1619 if (setupterm(NULL, STDOUT_FILENO, &rc) == 0) {
1620 kbs = tigetstr("kbs");
1621 if (kbs == (char *)-1)
1622 kbs = NULL;
1623 }
1624
Luka Perkovd131ad62012-05-27 11:44:51 +00001625 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún5fa04f42021-09-24 23:07:11 +02001626 quit[0] | 0100, quit[1]);
Luka Perkovd131ad62012-05-27 11:44:51 +00001627 } else
1628 in = -1;
1629
1630 rc = 0;
1631 s = 0;
Pali Roháre8d26e82022-03-02 11:49:23 +01001632 k = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00001633
1634 do {
1635 fd_set rfds;
1636 int nfds = 0;
1637
Pali Rohár0a143412021-10-25 15:12:52 +02001638 FD_ZERO(&rfds);
Luka Perkovd131ad62012-05-27 11:44:51 +00001639 FD_SET(tty, &rfds);
1640 nfds = nfds < tty ? tty : nfds;
1641
1642 if (in >= 0) {
1643 FD_SET(in, &rfds);
1644 nfds = nfds < in ? in : nfds;
1645 }
1646
1647 nfds = select(nfds + 1, &rfds, NULL, NULL, NULL);
1648 if (nfds < 0)
1649 break;
1650
1651 if (FD_ISSET(tty, &rfds)) {
Pali Roháre8d26e82022-03-02 11:49:23 +01001652 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL, NULL, NULL);
Luka Perkovd131ad62012-05-27 11:44:51 +00001653 if (rc)
1654 break;
1655 }
1656
Marek Behúnf30cb0d2021-09-24 23:06:39 +02001657 if (in >= 0 && FD_ISSET(in, &rfds)) {
Pali Roháre8d26e82022-03-02 11:49:23 +01001658 rc = kwboot_term_pipe(in, tty, quit, &s, kbs, &k);
Luka Perkovd131ad62012-05-27 11:44:51 +00001659 if (rc)
1660 break;
1661 }
1662 } while (quit[s] != 0);
1663
Pali Rohárec0fe5b2021-07-23 11:14:18 +02001664 if (in >= 0)
1665 tcsetattr(in, TCSANOW, &otio);
Pali Rohár49a0a3b2021-07-23 11:14:19 +02001666 printf("\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00001667out:
1668 return rc;
1669}
1670
1671static void *
Pali Rohár04ced022021-09-24 23:07:03 +02001672kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovd131ad62012-05-27 11:44:51 +00001673{
Pali Rohárddc04fa2021-09-24 23:06:55 +02001674 int rc, fd;
Luka Perkovd131ad62012-05-27 11:44:51 +00001675 void *img;
Pali Rohára339d6c2022-04-06 15:18:59 +02001676 off_t len;
Pali Rohár04ced022021-09-24 23:07:03 +02001677 off_t tot;
Luka Perkovd131ad62012-05-27 11:44:51 +00001678
1679 rc = -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00001680 img = NULL;
1681
1682 fd = open(path, O_RDONLY);
1683 if (fd < 0)
1684 goto out;
1685
Pali Rohára339d6c2022-04-06 15:18:59 +02001686 len = lseek(fd, 0, SEEK_END);
1687 if (len == (off_t)-1)
Luka Perkovd131ad62012-05-27 11:44:51 +00001688 goto out;
1689
Pali Rohára339d6c2022-04-06 15:18:59 +02001690 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
1691 goto out;
1692
1693 img = malloc(len + reserve);
Pali Rohár04ced022021-09-24 23:07:03 +02001694 if (!img)
Luka Perkovd131ad62012-05-27 11:44:51 +00001695 goto out;
Pali Rohár04ced022021-09-24 23:07:03 +02001696
1697 tot = 0;
Pali Rohára339d6c2022-04-06 15:18:59 +02001698 while (tot < len) {
1699 ssize_t rd = read(fd, img + tot, len - tot);
Pali Rohár04ced022021-09-24 23:07:03 +02001700
1701 if (rd < 0)
1702 goto out;
1703
1704 tot += rd;
1705
Pali Rohára339d6c2022-04-06 15:18:59 +02001706 if (!rd && tot < len) {
Pali Rohár04ced022021-09-24 23:07:03 +02001707 errno = EIO;
1708 goto out;
1709 }
Luka Perkovd131ad62012-05-27 11:44:51 +00001710 }
1711
1712 rc = 0;
Pali Rohára339d6c2022-04-06 15:18:59 +02001713 *size = len;
Luka Perkovd131ad62012-05-27 11:44:51 +00001714out:
1715 if (rc && img) {
Pali Rohár04ced022021-09-24 23:07:03 +02001716 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001717 img = NULL;
1718 }
1719 if (fd >= 0)
1720 close(fd);
1721
1722 return img;
1723}
1724
1725static uint8_t
Marek Behúnfe2fd732021-09-24 23:07:01 +02001726kwboot_hdr_csum8(const void *hdr)
Luka Perkovd131ad62012-05-27 11:44:51 +00001727{
Marek Behúnfe2fd732021-09-24 23:07:01 +02001728 const uint8_t *data = hdr;
1729 uint8_t csum;
1730 size_t size;
1731
1732 size = kwbheader_size_for_csum(hdr);
Luka Perkovd131ad62012-05-27 11:44:51 +00001733
1734 for (csum = 0; size-- > 0; data++)
1735 csum += *data;
1736
1737 return csum;
1738}
1739
Pali Rohárad9a3ac2021-10-25 15:12:55 +02001740static uint32_t *
1741kwboot_img_csum32_ptr(void *img)
1742{
1743 struct main_hdr_v1 *hdr = img;
1744 uint32_t datasz;
1745
1746 datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1747
1748 return img + le32_to_cpu(hdr->srcaddr) + datasz;
1749}
1750
1751static uint32_t
1752kwboot_img_csum32(const void *img)
1753{
1754 const struct main_hdr_v1 *hdr = img;
1755 uint32_t datasz, csum = 0;
1756 const uint32_t *data;
1757
1758 datasz = le32_to_cpu(hdr->blocksize) - sizeof(csum);
1759 if (datasz % sizeof(uint32_t))
1760 return 0;
1761
1762 data = img + le32_to_cpu(hdr->srcaddr);
1763 while (datasz > 0) {
1764 csum += le32_to_cpu(*data++);
1765 datasz -= 4;
1766 }
1767
1768 return cpu_to_le32(csum);
1769}
1770
Luka Perkovd131ad62012-05-27 11:44:51 +00001771static int
Pali Rohár550c9302021-09-24 23:06:57 +02001772kwboot_img_is_secure(void *img)
1773{
1774 struct opt_hdr_v1 *ohdr;
1775
1776 for_each_opt_hdr_v1 (ohdr, img)
1777 if (ohdr->headertype == OPT_HDR_V1_SECURE_TYPE)
1778 return 1;
1779
1780 return 0;
1781}
1782
Pali Rohárca272042021-09-24 23:07:05 +02001783static void *
Pali Rohár063cb352021-10-25 15:12:56 +02001784kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohárca272042021-09-24 23:07:05 +02001785{
Pali Rohárca272042021-09-24 23:07:05 +02001786 struct main_hdr_v1 *hdr = img;
Pali Rohár063cb352021-10-25 15:12:56 +02001787 void *result;
Pali Rohárca272042021-09-24 23:07:05 +02001788
Pali Rohár063cb352021-10-25 15:12:56 +02001789 /*
1790 * 32-bit checksum comes after end of image code, so we will be putting
1791 * new code there. So we get this pointer and then increase data size
1792 * (since increasing data size changes kwboot_img_csum32_ptr() return
1793 * value).
1794 */
1795 result = kwboot_img_csum32_ptr(img);
Pali Rohárca272042021-09-24 23:07:05 +02001796 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Rohár063cb352021-10-25 15:12:56 +02001797 *size += grow;
Pali Rohárca272042021-09-24 23:07:05 +02001798
Pali Rohár063cb352021-10-25 15:12:56 +02001799 return result;
Pali Rohárca272042021-09-24 23:07:05 +02001800}
1801
Pali Rohár04ced022021-09-24 23:07:03 +02001802static void
1803kwboot_img_grow_hdr(void *img, size_t *size, size_t grow)
1804{
1805 uint32_t hdrsz, datasz, srcaddr;
1806 struct main_hdr_v1 *hdr = img;
Pali Rohárd656f5a2021-10-25 15:13:02 +02001807 struct opt_hdr_v1 *ohdr;
Pali Rohár04ced022021-09-24 23:07:03 +02001808 uint8_t *data;
1809
1810 srcaddr = le32_to_cpu(hdr->srcaddr);
1811
Pali Rohárd656f5a2021-10-25 15:13:02 +02001812 /* calculate real used space in kwbimage header */
1813 if (kwbimage_version(img) == 0) {
1814 hdrsz = kwbheader_size(img);
1815 } else {
1816 hdrsz = sizeof(*hdr);
1817 for_each_opt_hdr_v1 (ohdr, hdr)
1818 hdrsz += opt_hdr_v1_size(ohdr);
1819 }
1820
Pali Rohár04ced022021-09-24 23:07:03 +02001821 data = (uint8_t *)img + srcaddr;
1822 datasz = *size - srcaddr;
1823
1824 /* only move data if there is not enough space */
1825 if (hdrsz + grow > srcaddr) {
1826 size_t need = hdrsz + grow - srcaddr;
1827
1828 /* move data by enough bytes */
1829 memmove(data + need, data, datasz);
1830
1831 hdr->srcaddr = cpu_to_le32(srcaddr + need);
1832 *size += need;
1833 }
1834
1835 if (kwbimage_version(img) == 1) {
1836 hdrsz += grow;
Pali Rohárd656f5a2021-10-25 15:13:02 +02001837 if (hdrsz > kwbheader_size(img)) {
1838 hdr->headersz_msb = hdrsz >> 16;
1839 hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff);
1840 }
Pali Rohár04ced022021-09-24 23:07:03 +02001841 }
1842}
1843
Pali Rohárca272042021-09-24 23:07:05 +02001844static void *
1845kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz)
1846{
1847 struct main_hdr_v1 *hdr = img;
1848 struct opt_hdr_v1 *ohdr;
Pali Rohára85a71d2021-10-21 16:46:06 +02001849 uint32_t num_args;
1850 uint32_t offset;
Pali Rohárca272042021-09-24 23:07:05 +02001851 uint32_t ohdrsz;
Pali Roháre511cc32021-10-25 15:13:01 +02001852 uint8_t *prev_ext;
Pali Rohárca272042021-09-24 23:07:05 +02001853
Pali Rohár44691032022-01-12 18:20:52 +01001854 if (hdr->ext) {
Pali Rohárca272042021-09-24 23:07:05 +02001855 for_each_opt_hdr_v1 (ohdr, img)
1856 if (opt_hdr_v1_next(ohdr) == NULL)
1857 break;
1858
Pali Roháre511cc32021-10-25 15:13:01 +02001859 prev_ext = opt_hdr_v1_ext(ohdr);
1860 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohárca272042021-09-24 23:07:05 +02001861 } else {
Pali Rohárca272042021-09-24 23:07:05 +02001862 ohdr = (void *)(hdr + 1);
Pali Roháre511cc32021-10-25 15:13:01 +02001863 prev_ext = &hdr->ext;
Pali Rohárca272042021-09-24 23:07:05 +02001864 }
1865
Pali Rohára85a71d2021-10-21 16:46:06 +02001866 /*
1867 * ARM executable code inside the BIN header on some mvebu platforms
1868 * (e.g. A370, AXP) must always be aligned with the 128-bit boundary.
1869 * This requirement can be met by inserting dummy arguments into
1870 * BIN header, if needed.
1871 */
1872 offset = &ohdr->data[4] - (char *)img;
1873 num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t);
1874
1875 ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4;
1876 kwboot_img_grow_hdr(hdr, size, ohdrsz);
1877
Pali Rohár44691032022-01-12 18:20:52 +01001878 *prev_ext = 1;
Pali Roháre511cc32021-10-25 15:13:01 +02001879
Pali Rohárca272042021-09-24 23:07:05 +02001880 ohdr->headertype = OPT_HDR_V1_BINARY_TYPE;
1881 ohdr->headersz_msb = ohdrsz >> 16;
1882 ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff);
1883
1884 memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr));
Pali Rohára85a71d2021-10-21 16:46:06 +02001885 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohárca272042021-09-24 23:07:05 +02001886
Pali Rohára85a71d2021-10-21 16:46:06 +02001887 return &ohdr->data[4 + 4 * num_args];
Pali Rohárca272042021-09-24 23:07:05 +02001888}
1889
1890static void
Pali Rohár8dbe0272021-10-27 20:57:02 +02001891_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Rohár063cb352021-10-25 15:12:56 +02001892 int old_baud, int new_baud)
Pali Rohárca272042021-09-24 23:07:05 +02001893{
Pali Rohár063cb352021-10-25 15:12:56 +02001894 struct main_hdr_v1 *hdr = img;
Pali Rohár8dbe0272021-10-27 20:57:02 +02001895 uint32_t orig_datasz;
1896 uint32_t codesz;
Pali Rohár063cb352021-10-25 15:12:56 +02001897 uint8_t *code;
Pali Rohárca272042021-09-24 23:07:05 +02001898
Pali Rohár8dbe0272021-10-27 20:57:02 +02001899 if (for_data) {
Pali Rohár063cb352021-10-25 15:12:56 +02001900 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1901
Pali Rohár8dbe0272021-10-27 20:57:02 +02001902 codesz = sizeof(kwboot_baud_code) +
1903 sizeof(kwboot_baud_code_data_jump);
1904 code = kwboot_img_grow_data_right(img, size, codesz);
Pali Rohár063cb352021-10-25 15:12:56 +02001905 } else {
Pali Rohár8dbe0272021-10-27 20:57:02 +02001906 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1907 sizeof(kwboot_baud_code) +
1908 sizeof(kwboot_baud_code_binhdr_post);
Pali Rohár063cb352021-10-25 15:12:56 +02001909 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár8dbe0272021-10-27 20:57:02 +02001910
1911 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1912 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1913 code += codesz;
Pali Rohárca272042021-09-24 23:07:05 +02001914 }
1915
Pali Rohár8dbe0272021-10-27 20:57:02 +02001916 codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t);
1917 memcpy(code, kwboot_baud_code, codesz);
1918 code += codesz;
1919 *(uint32_t *)code = cpu_to_le32(old_baud);
1920 code += sizeof(uint32_t);
1921 *(uint32_t *)code = cpu_to_le32(new_baud);
1922 code += sizeof(uint32_t);
1923
1924 if (for_data) {
1925 codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t);
1926 memcpy(code, kwboot_baud_code_data_jump, codesz);
1927 code += codesz;
1928 *(uint32_t *)code = hdr->execaddr;
1929 code += sizeof(uint32_t);
1930 hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz);
1931 } else {
1932 codesz = sizeof(kwboot_baud_code_binhdr_post);
1933 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1934 code += codesz;
1935 }
Pali Rohárca272042021-09-24 23:07:05 +02001936}
1937
Pali Rohár550c9302021-09-24 23:06:57 +02001938static int
Pali Rohárca272042021-09-24 23:07:05 +02001939kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovd131ad62012-05-27 11:44:51 +00001940{
Stefan Roesee29f1db2015-09-29 09:19:59 +02001941 struct main_hdr_v1 *hdr;
Pali Rohár792e4232021-09-24 23:06:58 +02001942 uint32_t srcaddr;
Luka Perkovd131ad62012-05-27 11:44:51 +00001943 uint8_t csum;
Marek Behún5c8f8122021-09-24 23:07:04 +02001944 size_t hdrsz;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001945 int image_ver;
Pali Rohár550c9302021-09-24 23:06:57 +02001946 int is_secure;
Luka Perkovd131ad62012-05-27 11:44:51 +00001947
Luka Perkovd131ad62012-05-27 11:44:51 +00001948 hdr = img;
1949
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001950 if (*size < sizeof(struct main_hdr_v1))
1951 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001952
Marek Behúnacb0b382021-09-24 23:07:00 +02001953 image_ver = kwbimage_version(img);
Pali Rohár5029d7b2021-07-23 11:14:22 +02001954 if (image_ver != 0 && image_ver != 1) {
Stefan Roesee29f1db2015-09-29 09:19:59 +02001955 fprintf(stderr, "Invalid image header version\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001956 goto err;
Stefan Roesee29f1db2015-09-29 09:19:59 +02001957 }
1958
Marek Behúnfe2fd732021-09-24 23:07:01 +02001959 hdrsz = kwbheader_size(hdr);
Stefan Roesee29f1db2015-09-29 09:19:59 +02001960
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001961 if (*size < hdrsz)
1962 goto err;
Pali Rohár825a2ca2021-07-23 11:14:21 +02001963
Marek Behúnfe2fd732021-09-24 23:07:01 +02001964 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001965 if (csum != hdr->checksum)
1966 goto err;
Luka Perkovd131ad62012-05-27 11:44:51 +00001967
Pali Rohár792e4232021-09-24 23:06:58 +02001968 srcaddr = le32_to_cpu(hdr->srcaddr);
1969
1970 switch (hdr->blockid) {
1971 case IBR_HDR_SATA_ID:
Pali Roháre1c4ed52023-01-21 13:45:36 +01001972 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
Pali Rohár792e4232021-09-24 23:06:58 +02001973 break;
1974
Pali Rohár792e4232021-09-24 23:06:58 +02001975 case IBR_HDR_PEX_ID:
1976 if (srcaddr == 0xFFFFFFFF)
1977 hdr->srcaddr = cpu_to_le32(hdrsz);
1978 break;
Pali Rohárf2c644e2021-09-24 23:06:59 +02001979
1980 case IBR_HDR_SPI_ID:
1981 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1982 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1983 hdr->destaddr = cpu_to_le32(0x00800000);
1984 hdr->execaddr = cpu_to_le32(0x00800000);
1985 }
1986 break;
Pali Rohár792e4232021-09-24 23:06:58 +02001987 }
1988
Pali Rohár04ced022021-09-24 23:07:03 +02001989 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behúnb4eea8f2021-09-24 23:07:12 +02001990 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1991 goto err;
Pali Rohár04ced022021-09-24 23:07:03 +02001992
Pali Rohárad9a3ac2021-10-25 15:12:55 +02001993 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img))
1994 goto err;
1995
Pali Rohár550c9302021-09-24 23:06:57 +02001996 is_secure = kwboot_img_is_secure(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00001997
Pali Rohár550c9302021-09-24 23:06:57 +02001998 if (hdr->blockid != IBR_HDR_UART_ID) {
1999 if (is_secure) {
2000 fprintf(stderr,
2001 "Image has secure header with signature for non-UART booting\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02002002 goto err;
Pali Rohár550c9302021-09-24 23:06:57 +02002003 }
2004
2005 kwboot_printv("Patching image boot signature to UART\n");
2006 hdr->blockid = IBR_HDR_UART_ID;
2007 }
Luka Perkovd131ad62012-05-27 11:44:51 +00002008
Pali Rohár0089f612021-10-22 12:37:47 +02002009 if (!is_secure) {
Pali Rohár4bebab62021-10-25 15:12:58 +02002010 if (image_ver == 1) {
2011 /*
2012 * Tell BootROM to send BootROM messages to UART port
2013 * number 0 (used also for UART booting) with default
2014 * baudrate (which should be 115200) and do not touch
2015 * UART MPP configuration.
2016 */
Pali Rohárffccee22022-01-25 18:13:13 +01002017 hdr->flags |= 0x1;
Pali Rohár4bebab62021-10-25 15:12:58 +02002018 hdr->options &= ~0x1F;
2019 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
2020 hdr->options |= 0 << 3;
2021 }
Pali Rohár0089f612021-10-22 12:37:47 +02002022 if (image_ver == 0)
2023 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
2024 hdr->nandpagesize = 0;
2025 }
2026
Pali Rohárca272042021-09-24 23:07:05 +02002027 if (baudrate) {
Pali Rohárca272042021-09-24 23:07:05 +02002028 if (image_ver == 0) {
2029 fprintf(stderr,
2030 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02002031 goto err;
Pali Rohárca272042021-09-24 23:07:05 +02002032 }
2033
2034 if (is_secure) {
2035 fprintf(stderr,
2036 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02002037 goto err;
Pali Rohárca272042021-09-24 23:07:05 +02002038 }
2039
2040 /*
2041 * First inject code that changes the baudrate from the default
2042 * value of 115200 Bd to requested value. This code is inserted
2043 * as a new opt hdr, so it is executed by BootROM after the
2044 * header part is received.
2045 */
2046 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
2047 baudrate);
Pali Rohár063cb352021-10-25 15:12:56 +02002048 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohárca272042021-09-24 23:07:05 +02002049
2050 /*
2051 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Rohár063cb352021-10-25 15:12:56 +02002052 * This code is appended after the data part of the image, and
2053 * execaddr is changed so that it is executed before U-Boot
2054 * proper.
Pali Rohárca272042021-09-24 23:07:05 +02002055 */
2056 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Rohár063cb352021-10-25 15:12:56 +02002057 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohárca272042021-09-24 23:07:05 +02002058
Pali Rohár82c5a0a2021-10-25 15:12:57 +02002059 /* Update the 32-bit data checksum */
2060 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
2061
Pali Rohárca272042021-09-24 23:07:05 +02002062 /* recompute header size */
2063 hdrsz = kwbheader_size(hdr);
2064 }
2065
Pali Rohár04ced022021-09-24 23:07:03 +02002066 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Roháred792c22021-10-25 15:13:00 +02002067 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohár04ced022021-09-24 23:07:03 +02002068
2069 if (is_secure) {
2070 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behúnb4eea8f2021-09-24 23:07:12 +02002071 goto err;
Pali Rohár04ced022021-09-24 23:07:03 +02002072 }
2073
2074 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Roháred792c22021-10-25 15:13:00 +02002075 kwboot_img_grow_hdr(img, size, grow);
Pali Rohár04ced022021-09-24 23:07:03 +02002076 }
2077
Marek Behúnfe2fd732021-09-24 23:07:01 +02002078 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovd131ad62012-05-27 11:44:51 +00002079
Pali Rohár04ced022021-09-24 23:07:03 +02002080 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behúnb4eea8f2021-09-24 23:07:12 +02002081 return 0;
2082err:
2083 errno = EINVAL;
2084 return -1;
Luka Perkovd131ad62012-05-27 11:44:51 +00002085}
2086
2087static void
2088kwboot_usage(FILE *stream, char *progname)
2089{
2090 fprintf(stream,
Pali Rohárbdc4dba2022-03-02 11:49:24 +01002091 "Usage: %s [OPTIONS] [-b <image> | -D <image> | -b | -d ] [-B <baud> ] [-t] <TTY>\n",
Stefan Roese84899e22014-10-22 12:13:21 +02002092 progname);
Luka Perkovd131ad62012-05-27 11:44:51 +00002093 fprintf(stream, "\n");
Stefan Roese84899e22014-10-22 12:13:21 +02002094 fprintf(stream,
Pali Rohár0b5909d2022-03-02 11:49:26 +01002095 " -b <image>: boot <image> with preamble (Kirkwood, Avanta, Armada 370/XP/375/38x/39x)\n");
Stefan Roese84899e22014-10-22 12:13:21 +02002096 fprintf(stream,
2097 " -D <image>: boot <image> without preamble (Dove)\n");
Pali Rohárbdc4dba2022-03-02 11:49:24 +01002098 fprintf(stream, " -b: enter xmodem boot mode\n");
2099 fprintf(stream, " -d: enter console debug mode\n");
Stefan Roese84899e22014-10-22 12:13:21 +02002100 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roese1c0df9e2015-05-29 13:25:04 +02002101 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith7497a6a2016-02-16 21:28:19 +00002102 fprintf(stream,
2103 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovd131ad62012-05-27 11:44:51 +00002104 fprintf(stream, "\n");
2105 fprintf(stream, " -t: mini terminal\n");
2106 fprintf(stream, "\n");
2107 fprintf(stream, " -B <baud>: set baud rate\n");
2108 fprintf(stream, "\n");
2109}
2110
2111int
2112main(int argc, char **argv)
2113{
2114 const char *ttypath, *imgpath;
Pali Rohárddc04fa2021-09-24 23:06:55 +02002115 int rv, rc, tty, term;
Pali Rohárc1d911f2022-03-02 11:49:20 +01002116 int bootmsg;
2117 int debugmsg;
Luka Perkovd131ad62012-05-27 11:44:51 +00002118 void *img;
2119 size_t size;
Pali Rohárca272042021-09-24 23:07:05 +02002120 size_t after_img_rsv;
2121 int baudrate;
Pali Rohárc513fe42022-01-25 18:13:07 +01002122 int prev_optind;
2123 int c;
Luka Perkovd131ad62012-05-27 11:44:51 +00002124
2125 rv = 1;
2126 tty = -1;
Pali Rohárc1d911f2022-03-02 11:49:20 +01002127 bootmsg = 0;
2128 debugmsg = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00002129 imgpath = NULL;
2130 img = NULL;
2131 term = 0;
Luka Perkovd131ad62012-05-27 11:44:51 +00002132 size = 0;
Pali Rohárca272042021-09-24 23:07:05 +02002133 after_img_rsv = KWBOOT_XM_BLKSZ;
2134 baudrate = 115200;
Luka Perkovd131ad62012-05-27 11:44:51 +00002135
Pali Rohár75176dc2021-11-05 23:30:42 +01002136 printf("kwboot version %s\n", PLAIN_VERSION);
2137
Luka Perkovd131ad62012-05-27 11:44:51 +00002138 kwboot_verbose = isatty(STDOUT_FILENO);
2139
2140 do {
Pali Rohárc513fe42022-01-25 18:13:07 +01002141 prev_optind = optind;
2142 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovd131ad62012-05-27 11:44:51 +00002143 if (c < 0)
2144 break;
2145
2146 switch (c) {
2147 case 'b':
Pali Rohárc513fe42022-01-25 18:13:07 +01002148 if (imgpath || bootmsg || debugmsg)
2149 goto usage;
Pali Rohárc1d911f2022-03-02 11:49:20 +01002150 bootmsg = 1;
Pali Rohárc513fe42022-01-25 18:13:07 +01002151 if (prev_optind == optind)
2152 goto usage;
Pali Rohárc497ae72022-03-07 19:03:09 +01002153 /* Option -b could have optional argument which specify image path */
2154 if (optind < argc && argv[optind] && argv[optind][0] != '-')
Pali Rohárc513fe42022-01-25 18:13:07 +01002155 imgpath = argv[optind++];
Luka Perkovd131ad62012-05-27 11:44:51 +00002156 break;
2157
Stefan Roese84899e22014-10-22 12:13:21 +02002158 case 'D':
Pali Rohárc513fe42022-01-25 18:13:07 +01002159 if (imgpath || bootmsg || debugmsg)
2160 goto usage;
Pali Rohárc1d911f2022-03-02 11:49:20 +01002161 bootmsg = 0;
Stefan Roese84899e22014-10-22 12:13:21 +02002162 imgpath = optarg;
2163 break;
2164
2165 case 'd':
Pali Rohárc513fe42022-01-25 18:13:07 +01002166 if (imgpath || bootmsg || debugmsg)
2167 goto usage;
Pali Rohárc1d911f2022-03-02 11:49:20 +01002168 debugmsg = 1;
Stefan Roese84899e22014-10-22 12:13:21 +02002169 break;
2170
Luka Perkovd131ad62012-05-27 11:44:51 +00002171 case 'p':
Pali Rohárddc04fa2021-09-24 23:06:55 +02002172 /* nop, for backward compatibility */
Luka Perkovd131ad62012-05-27 11:44:51 +00002173 break;
2174
2175 case 't':
2176 term = 1;
2177 break;
2178
Stefan Roese84899e22014-10-22 12:13:21 +02002179 case 'a':
Stefan Roese84899e22014-10-22 12:13:21 +02002180 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
2181 break;
2182
Stefan Roese1c0df9e2015-05-29 13:25:04 +02002183 case 'q':
Pali Rohár132016e2022-03-02 11:49:19 +01002184 /* nop, for backward compatibility */
Stefan Roese1c0df9e2015-05-29 13:25:04 +02002185 break;
2186
2187 case 's':
2188 msg_rsp_timeo = atoi(optarg);
2189 break;
2190
Kevin Smith7497a6a2016-02-16 21:28:19 +00002191 case 'o':
2192 blk_rsp_timeo = atoi(optarg);
2193 break;
2194
Luka Perkovd131ad62012-05-27 11:44:51 +00002195 case 'B':
Pali Rohárca272042021-09-24 23:07:05 +02002196 baudrate = atoi(optarg);
Luka Perkovd131ad62012-05-27 11:44:51 +00002197 break;
2198
2199 case 'h':
2200 rv = 0;
2201 default:
2202 goto usage;
2203 }
2204 } while (1);
2205
Pali Rohára3c64962022-01-25 18:13:12 +01002206 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovd131ad62012-05-27 11:44:51 +00002207 goto usage;
2208
Pali Rohárc497ae72022-03-07 19:03:09 +01002209 /*
2210 * If there is no remaining argument but optional imgpath was parsed
2211 * then it means that optional imgpath was eaten by getopt parser.
2212 * Reassing imgpath to required ttypath argument.
2213 */
2214 if (optind == argc && imgpath) {
2215 ttypath = imgpath;
2216 imgpath = NULL;
2217 } else if (optind + 1 == argc) {
2218 ttypath = argv[optind];
2219 } else {
Pali Rohárc513fe42022-01-25 18:13:07 +01002220 goto usage;
Pali Rohárc497ae72022-03-07 19:03:09 +01002221 }
Pali Rohárc513fe42022-01-25 18:13:07 +01002222
Pali Rohára79dea22022-03-07 19:03:07 +01002223 /* boot and debug message use baudrate 115200 */
2224 if (((bootmsg && !imgpath) || debugmsg) && baudrate != 115200) {
2225 fprintf(stderr, "Baudrate other than 115200 cannot be used for this operation.\n");
2226 goto usage;
2227 }
2228
Pali Rohár3782f552022-03-07 19:03:08 +01002229 tty = kwboot_open_tty(ttypath, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00002230 if (tty < 0) {
2231 perror(ttypath);
2232 goto out;
2233 }
2234
Pali Rohár3782f552022-03-07 19:03:08 +01002235 /*
2236 * initial baudrate for image transfer is always 115200,
2237 * the change to different baudrate is done only after the header is sent
2238 */
2239 if (imgpath && baudrate != 115200) {
2240 rc = kwboot_tty_change_baudrate(tty, 115200);
2241 if (rc) {
2242 perror(ttypath);
2243 goto out;
2244 }
2245 }
2246
Pali Rohárca272042021-09-24 23:07:05 +02002247 if (baudrate == 115200)
2248 /* do not change baudrate during Xmodem to the same value */
2249 baudrate = 0;
2250 else
2251 /* ensure we have enough space for baudrate change code */
Pali Rohár8dbe0272021-10-27 20:57:02 +02002252 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
2253 sizeof(kwboot_baud_code_binhdr_pre) +
Pali Rohár5923ef62021-10-25 15:12:54 +02002254 sizeof(kwboot_baud_code) +
Pali Rohár8dbe0272021-10-27 20:57:02 +02002255 sizeof(kwboot_baud_code_binhdr_post) +
2256 KWBOOT_XM_BLKSZ +
2257 sizeof(kwboot_baud_code) +
2258 sizeof(kwboot_baud_code_data_jump) +
Pali Rohár5923ef62021-10-25 15:12:54 +02002259 KWBOOT_XM_BLKSZ;
Pali Rohárca272042021-09-24 23:07:05 +02002260
Luka Perkovd131ad62012-05-27 11:44:51 +00002261 if (imgpath) {
Pali Rohárca272042021-09-24 23:07:05 +02002262 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovd131ad62012-05-27 11:44:51 +00002263 if (!img) {
2264 perror(imgpath);
2265 goto out;
2266 }
Luka Perkovd131ad62012-05-27 11:44:51 +00002267
Pali Rohárca272042021-09-24 23:07:05 +02002268 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00002269 if (rc) {
2270 fprintf(stderr, "%s: Invalid image.\n", imgpath);
2271 goto out;
2272 }
2273 }
2274
Stefan Roese84899e22014-10-22 12:13:21 +02002275 if (debugmsg) {
Pali Rohárc1d911f2022-03-02 11:49:20 +01002276 rc = kwboot_debugmsg(tty);
Pali Rohár93976af2022-03-02 11:49:22 +01002277 if (rc)
Stefan Roese84899e22014-10-22 12:13:21 +02002278 goto out;
Willy Tarreau3475a712018-07-03 12:10:30 -04002279 } else if (bootmsg) {
Pali Rohárc1d911f2022-03-02 11:49:20 +01002280 rc = kwboot_bootmsg(tty);
Pali Rohár913866a2022-03-02 11:49:21 +01002281 if (rc)
Luka Perkovd131ad62012-05-27 11:44:51 +00002282 goto out;
Luka Perkovd131ad62012-05-27 11:44:51 +00002283 }
2284
2285 if (img) {
Pali Rohárca272042021-09-24 23:07:05 +02002286 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovd131ad62012-05-27 11:44:51 +00002287 if (rc) {
2288 perror("xmodem");
2289 goto out;
2290 }
2291 }
2292
2293 if (term) {
2294 rc = kwboot_terminal(tty);
2295 if (rc && !(errno == EINTR)) {
2296 perror("terminal");
2297 goto out;
2298 }
2299 }
2300
2301 rv = 0;
2302out:
2303 if (tty >= 0)
2304 close(tty);
2305
2306 if (img)
Pali Rohár04ced022021-09-24 23:07:03 +02002307 free(img);
Luka Perkovd131ad62012-05-27 11:44:51 +00002308
2309 return rv;
2310
2311usage:
2312 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
2313 goto out;
2314}