blob: c8c7a8d2465829dd4f5db873320e78fd8c5d347c [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
Pali Rohár1817da82023-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ár38db9762022-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ár1817da82023-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ár38db9762022-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ár1817da82023-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ár84178222023-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 Perkovdff289642012-05-27 11:44:51 +0000119 */
120
Stefan Roese04ec0d32016-01-07 14:12:04 +0100121#include "kwbimage.h"
122#include "mkimage.h"
Pali Rohár3c703aaf2021-09-24 23:06:42 +0200123#include "version.h"
Stefan Roese04ec0d32016-01-07 14:12:04 +0100124
Luka Perkovdff289642012-05-27 11:44:51 +0000125#include <stdlib.h>
126#include <stdio.h>
127#include <string.h>
128#include <stdarg.h>
Stefan Roese04ec0d32016-01-07 14:12:04 +0100129#include <image.h>
Luka Perkovdff289642012-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ún2d9f2452021-09-24 23:06:52 +0200135#include <time.h>
Luka Perkovdff289642012-05-27 11:44:51 +0000136#include <sys/stat.h>
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100137#include <pthread.h>
Luka Perkovdff289642012-05-27 11:44:51 +0000138
Pali Rohárfd935e92021-09-24 23:07:06 +0200139#ifdef __linux__
140#include "termios_linux.h"
141#else
142#include <termios.h>
143#endif
144
Luka Perkovdff289642012-05-27 11:44:51 +0000145/*
Pali Roháred3a9752022-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 Perkovdff289642012-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 Roesebb5c4282014-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 Perkovdff289642012-05-27 11:44:51 +0000165#define KWBOOT_MSG_RSP_TIMEO 50 /* ms */
166
Stefan Roesebb5c4282014-10-22 12:13:21 +0200167/* Defines known to work on Armada XP */
Stefan Roese52859842022-08-19 09:43:59 +0200168#define KWBOOT_MSG_RSP_TIMEO_AXP 10 /* ms */
Stefan Roesebb5c4282014-10-22 12:13:21 +0200169
Luka Perkovdff289642012-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 Perkovdff289642012-05-27 11:44:51 +0000178
Pali Rohárbed18ef2021-09-24 23:06:48 +0200179#define KWBOOT_XM_BLKSZ 128 /* xmodem block size */
180
Luka Perkovdff289642012-05-27 11:44:51 +0000181struct kwboot_block {
182 uint8_t soh;
183 uint8_t pnum;
184 uint8_t _pnum;
Pali Rohárbed18ef2021-09-24 23:06:48 +0200185 uint8_t data[KWBOOT_XM_BLKSZ];
Luka Perkovdff289642012-05-27 11:44:51 +0000186 uint8_t csum;
Pali Rohárf01adfd2021-07-23 11:14:14 +0200187} __packed;
Luka Perkovdff289642012-05-27 11:44:51 +0000188
Pali Rohárdef98382022-01-25 18:13:00 +0100189#define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */
Marek Behún2d9f2452021-09-24 23:06:52 +0200190#define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */
Luka Perkovdff289642012-05-27 11:44:51 +0000191
Pali Rohár6303a232021-10-27 20:57:02 +0200192/* ARM code to change baudrate */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200193static unsigned char kwboot_baud_code[] = {
194 /* ; #define UART_BASE 0xd0012000 */
Pali Rohár2a8b7692021-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ár2a8b7692021-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ár2a8b7692021-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ár9e624c92021-10-27 20:57:00 +0200218 /* ; msleep(5); */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200219 /* ; return 0; */
220 /* ; } */
221
Pali Rohár2a8b7692021-09-24 23:07:05 +0200222 /* ; r0 = UART_BASE */
Pali Rohár15b16e52021-10-27 20:57:01 +0200223 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */
224 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200225
Pali Rohár2a8b7692021-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ár2d760ed2021-11-01 14:00:02 +0100251 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */
Pali Rohár2a8b7692021-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ár9e624c92021-10-27 20:57:00 +0200258 /* ; r2 = new_baudrate */
Pali Rohár2d760ed2021-11-01 14:00:02 +0100259 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */
Pali Rohár2a8b7692021-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ár9e624c92021-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ár2a8b7692021-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ár2d760ed2021-11-01 14:00:02 +0100308 /* ; Jump to the end of execution */
309 0x01, 0x00, 0x00, 0xea, /* b end */
Pali Rohár2a8b7692021-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ár6303a232021-10-27 20:57:02 +0200318
319 /* end: */
320};
321
Pali Rohár2d760ed2021-11-01 14:00:02 +0100322/* ARM code from binary header executed by BootROM before changing baudrate */
Pali Rohár6303a232021-10-27 20:57:02 +0200323static unsigned char kwboot_baud_code_binhdr_pre[] = {
Pali Rohár2d760ed2021-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ár6303a232021-10-27 20:57:02 +0200341 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */
Pali Rohár2d760ed2021-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ár6303a232021-10-27 20:57:02 +0200379};
380
Pali Rohár2d760ed2021-11-01 14:00:02 +0100381/* ARM code for returning from binary header back to BootROM */
Pali Rohár6303a232021-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 } */
Pali Rohár2a8b7692021-09-24 23:07:05 +0200386};
387
Pali Rohár6303a232021-10-27 20:57:02 +0200388/* 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ár2a8b7692021-09-24 23:07:05 +0200395
396static const char kwb_baud_magic[16] = "$baudratechange";
397
Luka Perkovdff289642012-05-27 11:44:51 +0000398static int kwboot_verbose;
399
Stefan Roesebb5c4282014-10-22 12:13:21 +0200400static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO;
Kevin Smith4d31a842016-02-16 21:28:19 +0000401static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO;
Stefan Roesebb5c4282014-10-22 12:13:21 +0200402
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200403static ssize_t
404kwboot_write(int fd, const char *buf, size_t len)
405{
Pali Rohár5350df12022-01-25 18:13:10 +0100406 ssize_t tot = 0;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200407
408 while (tot < len) {
409 ssize_t wr = write(fd, buf + tot, len - tot);
410
Pali Rohár5350df12022-01-25 18:13:10 +0100411 if (wr < 0 && errno == EINTR)
412 continue;
413 else if (wr < 0)
414 return wr;
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200415
416 tot += wr;
417 }
418
419 return tot;
420}
421
Luka Perkovdff289642012-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árd01b9ce2021-09-24 23:06:46 +0200472 pos = (pos + 1) % width;
Luka Perkovdff289642012-05-27 11:44:51 +0000473
474 if (pct == 100) {
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200475 while (pos && pos++ < width)
Luka Perkovdff289642012-05-27 11:44:51 +0000476 fputc(' ', stdout);
477 fputs(nl, stdout);
Pali Rohárd01b9ce2021-09-24 23:06:46 +0200478 nl = "";
479 pos = 0;
Luka Perkovdff289642012-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árd01b9ce2021-09-24 23:06:46 +0200496
497 if (pct == 100)
498 pct = 0;
Luka Perkovdff289642012-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ár613bd8b2022-01-25 18:13:11 +0100523 if (nfds < 0 && errno == EINTR)
524 continue;
525 else if (nfds < 0)
Luka Perkovdff289642012-05-27 11:44:51 +0000526 goto out;
Pali Rohár613bd8b2022-01-25 18:13:11 +0100527 else if (!nfds) {
Luka Perkovdff289642012-05-27 11:44:51 +0000528 errno = ETIMEDOUT;
529 goto out;
530 }
531
532 n = read(fd, buf, len);
Pali Rohár613bd8b2022-01-25 18:13:11 +0100533 if (n < 0 && errno == EINTR)
534 continue;
535 else if (n <= 0)
Luka Perkovdff289642012-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árf2acbba2021-10-27 20:56:59 +0200548kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain)
Luka Perkovdff289642012-05-27 11:44:51 +0000549{
Stefan Roesebb5c4282014-10-22 12:13:21 +0200550 if (!buf)
551 return 0;
552
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200553 if (kwboot_write(fd, buf, len) < 0)
554 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +0000555
Pali Rohárf2acbba2021-10-27 20:56:59 +0200556 if (nodrain)
557 return 0;
558
Marek Behúnd3bc5c32021-09-24 23:06:41 +0200559 return tcdrain(fd);
Luka Perkovdff289642012-05-27 11:44:51 +0000560}
561
562static int
563kwboot_tty_send_char(int fd, unsigned char c)
564{
Pali Rohárf2acbba2021-10-27 20:56:59 +0200565 return kwboot_tty_send(fd, &c, 1, 0);
Luka Perkovdff289642012-05-27 11:44:51 +0000566}
567
568static speed_t
Pali Rohár2a8b7692021-09-24 23:07:05 +0200569kwboot_tty_baudrate_to_speed(int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000570{
571 switch (baudrate) {
Pali Rohár2a8b7692021-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 Perkovdff289642012-05-27 11:44:51 +0000637 case 115200:
638 return B115200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200639#endif
640#ifdef B76800
641 case 76800:
642 return B76800;
643#endif
644#ifdef B57600
Luka Perkovdff289642012-05-27 11:44:51 +0000645 case 57600:
646 return B57600;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200647#endif
648#ifdef B38400
Luka Perkovdff289642012-05-27 11:44:51 +0000649 case 38400:
650 return B38400;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200651#endif
652#ifdef B19200
Luka Perkovdff289642012-05-27 11:44:51 +0000653 case 19200:
654 return B19200;
Pali Rohár2a8b7692021-09-24 23:07:05 +0200655#endif
656#ifdef B9600
Luka Perkovdff289642012-05-27 11:44:51 +0000657 case 9600:
658 return B9600;
Pali Rohár2a8b7692021-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árfd935e92021-09-24 23:07:06 +0200709#ifdef BOTHER
710 return BOTHER;
711#else
Pali Rohár2a8b7692021-09-24 23:07:05 +0200712 return B0;
Pali Rohárfd935e92021-09-24 23:07:06 +0200713#endif
Luka Perkovdff289642012-05-27 11:44:51 +0000714 }
Pali Rohár2a8b7692021-09-24 23:07:05 +0200715}
716
717static int
Marek Behún67835492021-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ár2a8b7692021-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árfd935e92021-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ár2a8b7692021-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;
Luka Perkovdff289642012-05-27 11:44:51 +0000753
Pali Rohár2a8b7692021-09-24 23:07:05 +0200754 rc = tcsetattr(fd, TCSANOW, &tio);
755 if (rc)
756 return rc;
757
Marek Behún67835492021-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ár2a8b7692021-09-24 23:07:05 +0200778 return 0;
Marek Behún67835492021-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;
Luka Perkovdff289642012-05-27 11:44:51 +0000784}
785
786static int
Pali Rohár2a8b7692021-09-24 23:07:05 +0200787kwboot_open_tty(const char *path, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +0000788{
Pali Rohár2bffe242021-09-24 23:07:10 +0200789 int rc, fd, flags;
Luka Perkovdff289642012-05-27 11:44:51 +0000790 struct termios tio;
791
792 rc = -1;
793
Marek Behún6c598c32021-09-24 23:07:11 +0200794 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
Luka Perkovdff289642012-05-27 11:44:51 +0000795 if (fd < 0)
796 goto out;
797
Pali Rohár909a0b92021-09-24 23:07:08 +0200798 rc = tcgetattr(fd, &tio);
799 if (rc)
800 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +0000801
Pali Rohár909a0b92021-09-24 23:07:08 +0200802 cfmakeraw(&tio);
Marek Behún6c598c32021-09-24 23:07:11 +0200803 tio.c_cflag |= CREAD | CLOCAL;
Pali Rohár40003a02021-10-25 15:12:53 +0200804 tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS);
Luka Perkovdff289642012-05-27 11:44:51 +0000805 tio.c_cc[VMIN] = 1;
Pali Rohár12095b22021-09-24 23:07:09 +0200806 tio.c_cc[VTIME] = 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000807
Luka Perkovdff289642012-05-27 11:44:51 +0000808 rc = tcsetattr(fd, TCSANOW, &tio);
809 if (rc)
810 goto out;
811
Pali Rohár2bffe242021-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ár2a8b7692021-09-24 23:07:05 +0200820 rc = kwboot_tty_change_baudrate(fd, baudrate);
821 if (rc)
822 goto out;
823
Luka Perkovdff289642012-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ár3a2e9bc2022-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 Perkovdff289642012-05-27 11:44:51 +0000900static int
Pali Rohár224110d2022-03-02 11:49:20 +0100901kwboot_bootmsg(int tty)
Luka Perkovdff289642012-05-27 11:44:51 +0000902{
Pali Roháre68c27bd2022-01-25 18:13:08 +0100903 struct kwboot_block block;
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100904 pthread_t write_thread;
905 int rc, err;
Luka Perkovdff289642012-05-27 11:44:51 +0000906 char c;
907
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100908 /* flush input and output queue */
909 tcflush(tty, TCIOFLUSH);
Luka Perkovdff289642012-05-27 11:44:51 +0000910
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100911 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 Perkovdff289642012-05-27 11:44:51 +0000916
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100917 kwboot_printv("Sending boot message. Please reboot the target...");
Luka Perkovdff289642012-05-27 11:44:51 +0000918
Pali Rohár3a2e9bc2022-03-02 11:49:21 +0100919 err = 0;
920 while (1) {
Luka Perkovdff289642012-05-27 11:44:51 +0000921 kwboot_spinner();
922
Pali Rohár3a2e9bc2022-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 Perkovdff289642012-05-27 11:44:51 +0000934
935 kwboot_printv("\n");
936
Pali Rohár3a2e9bc2022-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áre68c27bd2022-01-25 18:13:08 +0100940 return rc;
Pali Rohár3a2e9bc2022-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áre68c27bd2022-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áre3a18282022-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áre68c27bd2022-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áre3a18282022-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áre68c27bd2022-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áre3a18282022-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áre68c27bd2022-01-25 18:13:08 +0100990
991 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +0000992}
993
994static int
Pali Rohár224110d2022-03-02 11:49:20 +0100995kwboot_debugmsg(int tty)
Stefan Roesebb5c4282014-10-22 12:13:21 +0200996{
Pali Rohár2af673b2022-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;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001001
Pali Rohár2af673b2022-03-02 11:49:22 +01001002 /* flush input and output queue */
1003 tcflush(tty, TCIOFLUSH);
Stefan Roesebb5c4282014-10-22 12:13:21 +02001004
Pali Rohár2af673b2022-03-02 11:49:22 +01001005 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 Roesebb5c4282014-10-22 12:13:21 +02001010
Pali Rohár2af673b2022-03-02 11:49:22 +01001011 kwboot_printv("Sending debug message. Please reboot the target...");
1012 kwboot_spinner();
Stefan Roesebb5c4282014-10-22 12:13:21 +02001013
Pali Rohár2af673b2022-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;
Pali Roháre3a18282022-03-02 11:49:18 +01001023 break;
Pali Rohár2af673b2022-03-02 11:49:22 +01001024 }
1025 off += rc - 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +02001026
1027 kwboot_spinner();
1028
Pali Rohár2af673b2022-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 Roesebb5c4282014-10-22 12:13:21 +02001050
1051 kwboot_printv("\n");
1052
Pali Rohár2af673b2022-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 Roesebb5c4282014-10-22 12:13:21 +02001130}
1131
Pali Rohár58cf04de2021-09-24 23:06:44 +02001132static size_t
Luka Perkovdff289642012-05-27 11:44:51 +00001133kwboot_xm_makeblock(struct kwboot_block *block, const void *data,
1134 size_t size, int pnum)
1135{
Marek Behúnedb63242021-09-24 23:06:45 +02001136 size_t i, n;
Luka Perkovdff289642012-05-27 11:44:51 +00001137
Stefan Roesebb5c4282014-10-22 12:13:21 +02001138 block->soh = SOH;
Luka Perkovdff289642012-05-27 11:44:51 +00001139 block->pnum = pnum;
1140 block->_pnum = ~block->pnum;
1141
Pali Rohárbed18ef2021-09-24 23:06:48 +02001142 n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ;
Luka Perkovdff289642012-05-27 11:44:51 +00001143 memcpy(&block->data[0], data, n);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001144 memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n);
Luka Perkovdff289642012-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ún2d9f2452021-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 Perkovdff289642012-05-27 11:44:51 +00001173static int
Marek Behúneabacb82021-09-24 23:06:49 +02001174_is_xm_reply(char c)
1175{
Pali Rohár6af14292022-01-25 18:13:03 +01001176 return c == ACK || c == NAK;
Marek Behúneabacb82021-09-24 23:06:49 +02001177}
1178
1179static int
Pali Rohár42c61a42021-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ár42c61a42021-09-24 23:06:54 +02001191 default:
1192 errno = EPROTO;
1193 break;
1194 }
1195
1196 return rc;
1197}
1198
1199static int
Pali Rohár2a8b7692021-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ár230d4f82022-01-25 18:13:04 +01001226kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm,
Pali Roháre4529bd2022-01-25 18:13:02 +01001227 int ignore_nak_reply,
Pali Rohár959e8502021-10-25 15:13:04 +02001228 int allow_non_xm, int *non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001229 int baudrate, int *baud_changed)
Pali Rohárd06d5202021-09-24 23:06:50 +02001230{
Marek Behún2d9f2452021-09-24 23:06:52 +02001231 int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001232 uint64_t recv_until = _now() + timeout;
Pali Rohárd06d5202021-09-24 23:06:50 +02001233 int rc;
1234
1235 while (1) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001236 rc = kwboot_tty_recv(fd, c, 1, timeout);
Pali Rohárd06d5202021-09-24 23:06:50 +02001237 if (rc) {
1238 if (errno != ETIMEDOUT)
1239 return rc;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001240 else if (allow_non_xm && *non_xm_print)
Marek Behún2d9f2452021-09-24 23:06:52 +02001241 return -1;
1242 else
1243 *c = NAK;
Pali Rohárd06d5202021-09-24 23:06:50 +02001244 }
1245
1246 /* If received xmodem reply, end. */
Pali Roháre4529bd2022-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árd06d5202021-09-24 23:06:50 +02001253 break;
Pali Roháre4529bd2022-01-25 18:13:02 +01001254 }
Pali Rohárd06d5202021-09-24 23:06:50 +02001255
1256 /*
Pali Rohár2a8b7692021-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ún0a3b85a2021-09-24 23:06:53 +02001263 * Otherwise decrease timeout by time elapsed.
Pali Rohárd06d5202021-09-24 23:06:50 +02001264 */
1265 if (allow_non_xm) {
Marek Behún2d9f2452021-09-24 23:06:52 +02001266 recv_until = _now() + timeout;
Pali Rohár2a8b7692021-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ún0a3b85a2021-09-24 23:06:53 +02001281 } else {
Pali Rohár230d4f82022-01-25 18:13:04 +01001282 if (stop_on_non_xm)
Pali Rohár959e8502021-10-25 15:13:04 +02001283 break;
Marek Behún0a3b85a2021-09-24 23:06:53 +02001284 timeout = recv_until - _now();
1285 if (timeout < 0) {
1286 errno = ETIMEDOUT;
1287 return -1;
1288 }
Pali Rohárd06d5202021-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ár80241ce2022-01-25 18:13:05 +01001297 int *done_print, int baudrate, int allow_retries)
Luka Perkovdff289642012-05-27 11:44:51 +00001298{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001299 int non_xm_print, baud_changed;
1300 int rc, err, retries;
Luka Perkovdff289642012-05-27 11:44:51 +00001301 char c;
1302
Pali Rohárd06d5202021-09-24 23:06:50 +02001303 *done_print = 0;
Pali Rohár6daa5612021-10-27 20:56:58 +02001304 non_xm_print = 0;
1305 baud_changed = 0;
Pali Rohárd06d5202021-09-24 23:06:50 +02001306
Pali Roháre665c6a2021-10-25 15:13:03 +02001307 retries = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001308 do {
Pali Rohárf2acbba2021-10-27 20:56:59 +02001309 rc = kwboot_tty_send(fd, block, sizeof(*block), 1);
Luka Perkovdff289642012-05-27 11:44:51 +00001310 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001311 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001312
Pali Rohárd06d5202021-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 Roesebb5c4282014-10-22 12:13:21 +02001318
Pali Rohár959e8502021-10-25 15:13:04 +02001319 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001320 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001321 allow_non_xm, &non_xm_print,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001322 baudrate, &baud_changed);
Pali Rohárd06d5202021-09-24 23:06:50 +02001323 if (rc)
Pali Rohár6af14292022-01-25 18:13:03 +01001324 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001325
Pali Rohárb3760f92022-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ár80241ce2022-01-25 18:13:05 +01001332 } while (c == NAK && allow_retries && retries++ < 16);
Luka Perkovdff289642012-05-27 11:44:51 +00001333
Marek Behúnbcc5e042021-09-24 23:06:51 +02001334 if (non_xm_print)
1335 kwboot_printv("\n");
1336
Pali Rohár2a8b7692021-09-24 23:07:05 +02001337 if (allow_non_xm && baudrate && !baud_changed) {
1338 fprintf(stderr, "Baudrate was not changed\n");
Pali Rohár2a8b7692021-09-24 23:07:05 +02001339 errno = EPROTO;
Pali Rohár6af14292022-01-25 18:13:03 +01001340 return -1;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001341 }
1342
Pali Rohár42c61a42021-09-24 23:06:54 +02001343 return _xm_reply_to_error(c);
Pali Rohár6af14292022-01-25 18:13:03 +01001344err:
Pali Rohár2a8b7692021-09-24 23:07:05 +02001345 err = errno;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001346 kwboot_printv("\n");
1347 errno = err;
1348 return rc;
Pali Rohár42c61a42021-09-24 23:06:54 +02001349}
Luka Perkovdff289642012-05-27 11:44:51 +00001350
Pali Rohár42c61a42021-09-24 23:06:54 +02001351static int
1352kwboot_xm_finish(int fd)
1353{
1354 int rc, retries;
1355 char c;
Luka Perkovdff289642012-05-27 11:44:51 +00001356
Pali Rohár42c61a42021-09-24 23:06:54 +02001357 kwboot_printv("Finishing transfer\n");
1358
Pali Roháre665c6a2021-10-25 15:13:03 +02001359 retries = 0;
Pali Rohár42c61a42021-09-24 23:06:54 +02001360 do {
1361 rc = kwboot_tty_send_char(fd, EOT);
1362 if (rc)
1363 return rc;
1364
Pali Rohár959e8502021-10-25 15:13:04 +02001365 rc = kwboot_xm_recv_reply(fd, &c, retries < 3,
Pali Roháre4529bd2022-01-25 18:13:02 +01001366 retries > 8,
Pali Rohár959e8502021-10-25 15:13:04 +02001367 0, NULL, 0, NULL);
Pali Rohár42c61a42021-09-24 23:06:54 +02001368 if (rc)
1369 return rc;
Pali Roháre665c6a2021-10-25 15:13:03 +02001370 } while (c == NAK && retries++ < 16);
Pali Rohár42c61a42021-09-24 23:06:54 +02001371
1372 return _xm_reply_to_error(c);
Luka Perkovdff289642012-05-27 11:44:51 +00001373}
1374
1375static int
Pali Rohárbed18ef2021-09-24 23:06:48 +02001376kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data,
Pali Rohár2a8b7692021-09-24 23:07:05 +02001377 size_t size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001378{
Pali Rohárd06d5202021-09-24 23:06:50 +02001379 int done_print = 0;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001380 size_t sent, left;
1381 int rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001382
Pali Rohárbed18ef2021-09-24 23:06:48 +02001383 kwboot_printv("Sending boot image %s (%zu bytes)...\n",
1384 header ? "header" : "data", size);
Luka Perkovdff289642012-05-27 11:44:51 +00001385
Pali Rohárbed18ef2021-09-24 23:06:48 +02001386 left = size;
1387 sent = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001388
Pali Rohárbed18ef2021-09-24 23:06:48 +02001389 while (sent < size) {
Luka Perkovdff289642012-05-27 11:44:51 +00001390 struct kwboot_block block;
Pali Rohárd06d5202021-09-24 23:06:50 +02001391 int last_block;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001392 size_t blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001393
Pali Rohárbed18ef2021-09-24 23:06:48 +02001394 blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++);
1395 data += blksz;
Luka Perkovdff289642012-05-27 11:44:51 +00001396
Pali Rohárd06d5202021-09-24 23:06:50 +02001397 last_block = (left <= blksz);
1398
Pali Rohár80241ce2022-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árd06d5202021-09-24 23:06:50 +02001421 rc = kwboot_xm_sendblock(tty, &block, header && last_block,
Pali Rohár80241ce2022-01-25 18:13:05 +01001422 &done_print, baudrate, header);
Luka Perkovdff289642012-05-27 11:44:51 +00001423 if (rc)
1424 goto out;
1425
Pali Rohárbed18ef2021-09-24 23:06:48 +02001426 sent += blksz;
1427 left -= blksz;
1428
Pali Rohárd06d5202021-09-24 23:06:50 +02001429 if (!done_print)
1430 kwboot_progress(sent * 100 / size, '.');
Pali Rohárbed18ef2021-09-24 23:06:48 +02001431 }
Luka Perkovdff289642012-05-27 11:44:51 +00001432
Pali Rohárd06d5202021-09-24 23:06:50 +02001433 if (!done_print)
1434 kwboot_printv("Done\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001435
Pali Rohárbed18ef2021-09-24 23:06:48 +02001436 return 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001437out:
Pali Rohár5ed896f2021-09-24 23:06:47 +02001438 kwboot_printv("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001439 return rc;
Pali Rohárbed18ef2021-09-24 23:06:48 +02001440}
1441
1442static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001443kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate)
Pali Rohárbed18ef2021-09-24 23:06:48 +02001444{
1445 const uint8_t *img = _img;
1446 int rc, pnum;
1447 size_t hdrsz;
1448
Marek Behúnd1b0b032021-09-24 23:07:01 +02001449 hdrsz = kwbheader_size(img);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001450
Pali Rohár400b5cf2021-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árbed18ef2021-09-24 23:06:48 +02001459 pnum = 1;
1460
Pali Rohár2a8b7692021-09-24 23:07:05 +02001461 rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001462 if (rc)
1463 return rc;
1464
Pali Rohár400b5cf2021-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ár2a8b7692021-09-24 23:07:05 +02001476
1477 rc = kwboot_xm_finish(tty);
Pali Rohárbed18ef2021-09-24 23:06:48 +02001478 if (rc)
1479 return rc;
Luka Perkovdff289642012-05-27 11:44:51 +00001480
Pali Rohár2a8b7692021-09-24 23:07:05 +02001481 if (baudrate) {
Pali Rohár2a8b7692021-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 Perkovdff289642012-05-27 11:44:51 +00001489}
1490
1491static int
Pali Roháred3a9752022-03-02 11:49:23 +01001492kwboot_term_pipe(int in, int out, const char *quit, int *s, const char *kbs, int *k)
Luka Perkovdff289642012-05-27 11:44:51 +00001493{
Pali Rohár111779f2022-02-03 17:45:20 +01001494 char buf[128];
Pali Roháred3a9752022-03-02 11:49:23 +01001495 ssize_t nin, noff;
Luka Perkovdff289642012-05-27 11:44:51 +00001496
Pali Rohár111779f2022-02-03 17:45:20 +01001497 nin = read(in, buf, sizeof(buf));
Willy Tarreauab7e8a12018-07-03 12:10:31 -04001498 if (nin <= 0)
Luka Perkovdff289642012-05-27 11:44:51 +00001499 return -1;
1500
Pali Roháred3a9752022-03-02 11:49:23 +01001501 noff = 0;
1502
1503 if (quit || kbs) {
Luka Perkovdff289642012-05-27 11:44:51 +00001504 int i;
1505
1506 for (i = 0; i < nin; i++) {
Pali Roháred3a9752022-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 Perkovdff289642012-05-27 11:44:51 +00001529 (*s)++;
Pali Rohár111779f2022-02-03 17:45:20 +01001530 if (!quit[*s]) {
Pali Rohár6a5d1312022-02-18 12:24:13 +01001531 nin = (i > *s) ? (i - *s) : 0;
Pali Rohár111779f2022-02-03 17:45:20 +01001532 break;
1533 }
Pali Roháred3a9752022-03-02 11:49:23 +01001534 } else if (quit) {
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001535 *s = 0;
Pali Rohár48615ba2021-07-23 11:14:20 +02001536 }
Pali Roháred3a9752022-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 Perkovdff289642012-05-27 11:44:51 +00001557 }
Pali Rohár111779f2022-02-03 17:45:20 +01001558
Pali Roháred3a9752022-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 Perkovdff289642012-05-27 11:44:51 +00001567 }
1568
Pali Roháred3a9752022-03-02 11:49:23 +01001569 if (nin > noff && kwboot_write(out, buf + noff, nin - noff) < 0)
Marek Behúnd3bc5c32021-09-24 23:06:41 +02001570 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00001571
1572 return 0;
1573}
1574
1575static int
1576kwboot_terminal(int tty)
1577{
Pali Roháred3a9752022-03-02 11:49:23 +01001578 int rc, in, s, k;
1579 const char *kbs = NULL;
Marek Behúnea5b2b32021-09-24 23:06:40 +02001580 const char *quit = "\34c";
Luka Perkovdff289642012-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áred3a9752022-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 Perkovdff289642012-05-27 11:44:51 +00001625 kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n",
Marek Behún6c598c32021-09-24 23:07:11 +02001626 quit[0] | 0100, quit[1]);
Luka Perkovdff289642012-05-27 11:44:51 +00001627 } else
1628 in = -1;
1629
1630 rc = 0;
1631 s = 0;
Pali Roháred3a9752022-03-02 11:49:23 +01001632 k = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00001633
1634 do {
1635 fd_set rfds;
1636 int nfds = 0;
1637
Pali Rohárc1efca42021-10-25 15:12:52 +02001638 FD_ZERO(&rfds);
Luka Perkovdff289642012-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áred3a9752022-03-02 11:49:23 +01001652 rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL, NULL, NULL);
Luka Perkovdff289642012-05-27 11:44:51 +00001653 if (rc)
1654 break;
1655 }
1656
Marek Behún4eb55de2021-09-24 23:06:39 +02001657 if (in >= 0 && FD_ISSET(in, &rfds)) {
Pali Roháred3a9752022-03-02 11:49:23 +01001658 rc = kwboot_term_pipe(in, tty, quit, &s, kbs, &k);
Luka Perkovdff289642012-05-27 11:44:51 +00001659 if (rc)
1660 break;
1661 }
1662 } while (quit[s] != 0);
1663
Pali Roháreafc9692021-07-23 11:14:18 +02001664 if (in >= 0)
1665 tcsetattr(in, TCSANOW, &otio);
Pali Rohár049cf912021-07-23 11:14:19 +02001666 printf("\n");
Luka Perkovdff289642012-05-27 11:44:51 +00001667out:
1668 return rc;
1669}
1670
1671static void *
Pali Rohárd9355bf2021-09-24 23:07:03 +02001672kwboot_read_image(const char *path, size_t *size, size_t reserve)
Luka Perkovdff289642012-05-27 11:44:51 +00001673{
Pali Rohár4d276012021-09-24 23:06:55 +02001674 int rc, fd;
Luka Perkovdff289642012-05-27 11:44:51 +00001675 void *img;
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001676 off_t len;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001677 off_t tot;
Luka Perkovdff289642012-05-27 11:44:51 +00001678
1679 rc = -1;
Luka Perkovdff289642012-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ár6f2aa8e2022-04-06 15:18:59 +02001686 len = lseek(fd, 0, SEEK_END);
1687 if (len == (off_t)-1)
Luka Perkovdff289642012-05-27 11:44:51 +00001688 goto out;
1689
Pali Rohár6f2aa8e2022-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árd9355bf2021-09-24 23:07:03 +02001694 if (!img)
Luka Perkovdff289642012-05-27 11:44:51 +00001695 goto out;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001696
1697 tot = 0;
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001698 while (tot < len) {
1699 ssize_t rd = read(fd, img + tot, len - tot);
Pali Rohárd9355bf2021-09-24 23:07:03 +02001700
1701 if (rd < 0)
1702 goto out;
1703
1704 tot += rd;
1705
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001706 if (!rd && tot < len) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001707 errno = EIO;
1708 goto out;
1709 }
Luka Perkovdff289642012-05-27 11:44:51 +00001710 }
1711
1712 rc = 0;
Pali Rohár6f2aa8e2022-04-06 15:18:59 +02001713 *size = len;
Luka Perkovdff289642012-05-27 11:44:51 +00001714out:
1715 if (rc && img) {
Pali Rohárd9355bf2021-09-24 23:07:03 +02001716 free(img);
Luka Perkovdff289642012-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únd1b0b032021-09-24 23:07:01 +02001726kwboot_hdr_csum8(const void *hdr)
Luka Perkovdff289642012-05-27 11:44:51 +00001727{
Marek Behúnd1b0b032021-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 Perkovdff289642012-05-27 11:44:51 +00001733
1734 for (csum = 0; size-- > 0; data++)
1735 csum += *data;
1736
1737 return csum;
1738}
1739
Pali Rohár0b659e52021-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 Perkovdff289642012-05-27 11:44:51 +00001771static int
Pali Rohár5725e0c2021-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ár2a8b7692021-09-24 23:07:05 +02001783static void *
Pali Roháraed39f22021-10-25 15:12:56 +02001784kwboot_img_grow_data_right(void *img, size_t *size, size_t grow)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001785{
Pali Rohár2a8b7692021-09-24 23:07:05 +02001786 struct main_hdr_v1 *hdr = img;
Pali Roháraed39f22021-10-25 15:12:56 +02001787 void *result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001788
Pali Roháraed39f22021-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ár2a8b7692021-09-24 23:07:05 +02001796 hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow);
Pali Roháraed39f22021-10-25 15:12:56 +02001797 *size += grow;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001798
Pali Roháraed39f22021-10-25 15:12:56 +02001799 return result;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001800}
1801
Pali Rohárd9355bf2021-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ár6fa97092021-10-25 15:13:02 +02001807 struct opt_hdr_v1 *ohdr;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001808 uint8_t *data;
1809
1810 srcaddr = le32_to_cpu(hdr->srcaddr);
1811
Pali Rohár6fa97092021-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árd9355bf2021-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ár6fa97092021-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árd9355bf2021-09-24 23:07:03 +02001841 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001842}
1843
1844static 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ár87cc3c92021-10-21 16:46:06 +02001849 uint32_t num_args;
1850 uint32_t offset;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001851 uint32_t ohdrsz;
Pali Rohár90eb9002021-10-25 15:13:01 +02001852 uint8_t *prev_ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001853
Pali Rohár32eec192022-01-12 18:20:52 +01001854 if (hdr->ext) {
Pali Rohár2a8b7692021-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ár90eb9002021-10-25 15:13:01 +02001859 prev_ext = opt_hdr_v1_ext(ohdr);
1860 ohdr = _opt_hdr_v1_next(ohdr);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001861 } else {
Pali Rohár2a8b7692021-09-24 23:07:05 +02001862 ohdr = (void *)(hdr + 1);
Pali Rohár90eb9002021-10-25 15:13:01 +02001863 prev_ext = &hdr->ext;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001864 }
1865
Pali Rohár87cc3c92021-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ár32eec192022-01-12 18:20:52 +01001878 *prev_ext = 1;
Pali Rohár90eb9002021-10-25 15:13:01 +02001879
Pali Rohár2a8b7692021-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ár87cc3c92021-10-21 16:46:06 +02001885 *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001886
Pali Rohár87cc3c92021-10-21 16:46:06 +02001887 return &ohdr->data[4 + 4 * num_args];
Pali Rohárd9355bf2021-09-24 23:07:03 +02001888}
1889
Pali Rohár2a8b7692021-09-24 23:07:05 +02001890static void
Pali Rohár6303a232021-10-27 20:57:02 +02001891_inject_baudrate_change_code(void *img, size_t *size, int for_data,
Pali Roháraed39f22021-10-25 15:12:56 +02001892 int old_baud, int new_baud)
Pali Rohár2a8b7692021-09-24 23:07:05 +02001893{
Pali Roháraed39f22021-10-25 15:12:56 +02001894 struct main_hdr_v1 *hdr = img;
Pali Rohár6303a232021-10-27 20:57:02 +02001895 uint32_t orig_datasz;
1896 uint32_t codesz;
Pali Roháraed39f22021-10-25 15:12:56 +02001897 uint8_t *code;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001898
Pali Rohár6303a232021-10-27 20:57:02 +02001899 if (for_data) {
Pali Roháraed39f22021-10-25 15:12:56 +02001900 orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t);
1901
Pali Rohár6303a232021-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);
1905 } else {
1906 codesz = sizeof(kwboot_baud_code_binhdr_pre) +
1907 sizeof(kwboot_baud_code) +
1908 sizeof(kwboot_baud_code_binhdr_post);
1909 code = kwboot_add_bin_ohdr_v1(img, size, codesz);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001910
Pali Rohár6303a232021-10-27 20:57:02 +02001911 codesz = sizeof(kwboot_baud_code_binhdr_pre);
1912 memcpy(code, kwboot_baud_code_binhdr_pre, codesz);
1913 code += codesz;
1914 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001915
Pali Rohár6303a232021-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);
Pali Rohár2a8b7692021-09-24 23:07:05 +02001923
Pali Rohár6303a232021-10-27 20:57:02 +02001924 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);
Pali Roháraed39f22021-10-25 15:12:56 +02001931 } else {
Pali Rohár6303a232021-10-27 20:57:02 +02001932 codesz = sizeof(kwboot_baud_code_binhdr_post);
1933 memcpy(code, kwboot_baud_code_binhdr_post, codesz);
1934 code += codesz;
Pali Rohár2a8b7692021-09-24 23:07:05 +02001935 }
Pali Rohár2a8b7692021-09-24 23:07:05 +02001936}
1937
Pali Rohár5725e0c2021-09-24 23:06:57 +02001938static int
Pali Rohár2a8b7692021-09-24 23:07:05 +02001939kwboot_img_patch(void *img, size_t *size, int baudrate)
Luka Perkovdff289642012-05-27 11:44:51 +00001940{
Stefan Roesec74165d2015-09-29 09:19:59 +02001941 struct main_hdr_v1 *hdr;
Pali Rohár2f9ce972023-01-08 13:42:07 +01001942 struct opt_hdr_v1 *ohdr;
Pali Rohár88255af2021-09-24 23:06:58 +02001943 uint32_t srcaddr;
Luka Perkovdff289642012-05-27 11:44:51 +00001944 uint8_t csum;
Marek Behún811821e2021-09-24 23:07:04 +02001945 size_t hdrsz;
Stefan Roesec74165d2015-09-29 09:19:59 +02001946 int image_ver;
Pali Rohár5725e0c2021-09-24 23:06:57 +02001947 int is_secure;
Luka Perkovdff289642012-05-27 11:44:51 +00001948
Luka Perkovdff289642012-05-27 11:44:51 +00001949 hdr = img;
1950
Marek Behún3e6117e2021-09-24 23:07:12 +02001951 if (*size < sizeof(struct main_hdr_v1))
1952 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001953
Marek Behúnfa9caec2021-09-24 23:07:00 +02001954 image_ver = kwbimage_version(img);
Pali Rohárb572ac42021-07-23 11:14:22 +02001955 if (image_ver != 0 && image_ver != 1) {
Stefan Roesec74165d2015-09-29 09:19:59 +02001956 fprintf(stderr, "Invalid image header version\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02001957 goto err;
Luka Perkovdff289642012-05-27 11:44:51 +00001958 }
1959
Marek Behúnd1b0b032021-09-24 23:07:01 +02001960 hdrsz = kwbheader_size(hdr);
Stefan Roesec74165d2015-09-29 09:19:59 +02001961
Marek Behún3e6117e2021-09-24 23:07:12 +02001962 if (*size < hdrsz)
1963 goto err;
Pali Rohár12281162021-07-23 11:14:21 +02001964
Marek Behúnd1b0b032021-09-24 23:07:01 +02001965 csum = kwboot_hdr_csum8(hdr) - hdr->checksum;
Marek Behún3e6117e2021-09-24 23:07:12 +02001966 if (csum != hdr->checksum)
1967 goto err;
Stefan Roesec74165d2015-09-29 09:19:59 +02001968
Pali Rohár88255af2021-09-24 23:06:58 +02001969 srcaddr = le32_to_cpu(hdr->srcaddr);
1970
1971 switch (hdr->blockid) {
1972 case IBR_HDR_SATA_ID:
Pali Rohár091e6922023-01-21 13:45:36 +01001973 hdr->srcaddr = cpu_to_le32(srcaddr * 512);
Pali Rohár88255af2021-09-24 23:06:58 +02001974 break;
1975
Pali Rohár88255af2021-09-24 23:06:58 +02001976 case IBR_HDR_PEX_ID:
1977 if (srcaddr == 0xFFFFFFFF)
1978 hdr->srcaddr = cpu_to_le32(hdrsz);
1979 break;
Pali Rohár398d4152021-09-24 23:06:59 +02001980
1981 case IBR_HDR_SPI_ID:
1982 if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) {
1983 kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n");
1984 hdr->destaddr = cpu_to_le32(0x00800000);
1985 hdr->execaddr = cpu_to_le32(0x00800000);
1986 }
1987 break;
Pali Rohár88255af2021-09-24 23:06:58 +02001988 }
1989
Pali Rohárd9355bf2021-09-24 23:07:03 +02001990 if (hdrsz > le32_to_cpu(hdr->srcaddr) ||
Marek Behún3e6117e2021-09-24 23:07:12 +02001991 *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize))
1992 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02001993
Pali Rohár2f9ce972023-01-08 13:42:07 +01001994 for_each_opt_hdr_v1 (ohdr, hdr) {
1995 if (!opt_hdr_v1_valid_size(ohdr, (const uint8_t *)hdr + hdrsz)) {
1996 fprintf(stderr, "Invalid optional image header\n");
1997 goto err;
1998 }
1999 }
2000
Pali Roháre0860662023-01-08 13:38:27 +01002001 /*
2002 * The 32-bit data checksum is optional for UART image. If it is not
2003 * present (checksum detected as invalid) then grow data part of the
2004 * image for the checksum, so it can be inserted there.
2005 */
2006 if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img)) {
2007 if (hdr->blockid != IBR_HDR_UART_ID) {
2008 fprintf(stderr, "Image has invalid data checksum\n");
2009 goto err;
2010 }
2011 kwboot_img_grow_data_right(img, size, sizeof(uint32_t));
2012 }
Pali Rohár0b659e52021-10-25 15:12:55 +02002013
Pali Rohár5725e0c2021-09-24 23:06:57 +02002014 is_secure = kwboot_img_is_secure(img);
Luka Perkovdff289642012-05-27 11:44:51 +00002015
Pali Rohár5725e0c2021-09-24 23:06:57 +02002016 if (hdr->blockid != IBR_HDR_UART_ID) {
2017 if (is_secure) {
2018 fprintf(stderr,
2019 "Image has secure header with signature for non-UART booting\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02002020 goto err;
Pali Rohár5725e0c2021-09-24 23:06:57 +02002021 }
2022
2023 kwboot_printv("Patching image boot signature to UART\n");
2024 hdr->blockid = IBR_HDR_UART_ID;
2025 }
Luka Perkovdff289642012-05-27 11:44:51 +00002026
Pali Rohár3024ebd2021-10-22 12:37:47 +02002027 if (!is_secure) {
Pali Rohár71db3622021-10-25 15:12:58 +02002028 if (image_ver == 1) {
2029 /*
2030 * Tell BootROM to send BootROM messages to UART port
2031 * number 0 (used also for UART booting) with default
2032 * baudrate (which should be 115200) and do not touch
2033 * UART MPP configuration.
2034 */
Pali Roháre4b8fca2022-01-25 18:13:13 +01002035 hdr->flags |= 0x1;
Pali Rohár71db3622021-10-25 15:12:58 +02002036 hdr->options &= ~0x1F;
2037 hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT;
2038 hdr->options |= 0 << 3;
2039 }
Pali Rohár3024ebd2021-10-22 12:37:47 +02002040 if (image_ver == 0)
2041 ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED;
2042 hdr->nandpagesize = 0;
2043 }
2044
Pali Rohár2a8b7692021-09-24 23:07:05 +02002045 if (baudrate) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002046 if (image_ver == 0) {
2047 fprintf(stderr,
2048 "Cannot inject code for changing baudrate into v0 image header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02002049 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002050 }
2051
2052 if (is_secure) {
2053 fprintf(stderr,
2054 "Cannot inject code for changing baudrate into image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02002055 goto err;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002056 }
2057
2058 /*
2059 * First inject code that changes the baudrate from the default
2060 * value of 115200 Bd to requested value. This code is inserted
2061 * as a new opt hdr, so it is executed by BootROM after the
2062 * header part is received.
2063 */
2064 kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n",
2065 baudrate);
Pali Roháraed39f22021-10-25 15:12:56 +02002066 _inject_baudrate_change_code(img, size, 0, 115200, baudrate);
Pali Rohár2a8b7692021-09-24 23:07:05 +02002067
2068 /*
2069 * Now inject code that changes the baudrate back to 115200 Bd.
Pali Roháraed39f22021-10-25 15:12:56 +02002070 * This code is appended after the data part of the image, and
2071 * execaddr is changed so that it is executed before U-Boot
2072 * proper.
Pali Rohár2a8b7692021-09-24 23:07:05 +02002073 */
2074 kwboot_printv("Injecting code for changing baudrate back\n");
Pali Roháraed39f22021-10-25 15:12:56 +02002075 _inject_baudrate_change_code(img, size, 1, baudrate, 115200);
Pali Rohár2a8b7692021-09-24 23:07:05 +02002076
Pali Rohárf625a222021-10-25 15:12:57 +02002077 /* Update the 32-bit data checksum */
2078 *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img);
2079
Pali Rohár2a8b7692021-09-24 23:07:05 +02002080 /* recompute header size */
2081 hdrsz = kwbheader_size(hdr);
2082 }
2083
Pali Rohárd9355bf2021-09-24 23:07:03 +02002084 if (hdrsz % KWBOOT_XM_BLKSZ) {
Pali Rohár085d9ce2021-10-25 15:13:00 +02002085 size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ;
Pali Rohárd9355bf2021-09-24 23:07:03 +02002086
2087 if (is_secure) {
2088 fprintf(stderr, "Cannot align image with secure header\n");
Marek Behún3e6117e2021-09-24 23:07:12 +02002089 goto err;
Pali Rohárd9355bf2021-09-24 23:07:03 +02002090 }
2091
2092 kwboot_printv("Aligning image header to Xmodem block size\n");
Pali Rohár085d9ce2021-10-25 15:13:00 +02002093 kwboot_img_grow_hdr(img, size, grow);
Pali Rohárd9355bf2021-09-24 23:07:03 +02002094 }
2095
Marek Behúnd1b0b032021-09-24 23:07:01 +02002096 hdr->checksum = kwboot_hdr_csum8(hdr) - csum;
Luka Perkovdff289642012-05-27 11:44:51 +00002097
Pali Rohárd9355bf2021-09-24 23:07:03 +02002098 *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize);
Marek Behún3e6117e2021-09-24 23:07:12 +02002099 return 0;
2100err:
2101 errno = EINVAL;
2102 return -1;
Luka Perkovdff289642012-05-27 11:44:51 +00002103}
2104
2105static void
2106kwboot_usage(FILE *stream, char *progname)
2107{
2108 fprintf(stream,
Pali Rohár8564cbe2022-03-02 11:49:24 +01002109 "Usage: %s [OPTIONS] [-b <image> | -D <image> | -b | -d ] [-B <baud> ] [-t] <TTY>\n",
Stefan Roesebb5c4282014-10-22 12:13:21 +02002110 progname);
Luka Perkovdff289642012-05-27 11:44:51 +00002111 fprintf(stream, "\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02002112 fprintf(stream,
Pali Rohárf4e9e872022-03-02 11:49:26 +01002113 " -b <image>: boot <image> with preamble (Kirkwood, Avanta, Armada 370/XP/375/38x/39x)\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02002114 fprintf(stream,
2115 " -D <image>: boot <image> without preamble (Dove)\n");
Pali Rohár8564cbe2022-03-02 11:49:24 +01002116 fprintf(stream, " -b: enter xmodem boot mode\n");
2117 fprintf(stream, " -d: enter console debug mode\n");
Stefan Roesebb5c4282014-10-22 12:13:21 +02002118 fprintf(stream, " -a: use timings for Armada XP\n");
Stefan Roesef7f509d2015-05-29 13:25:04 +02002119 fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n");
Kevin Smith4d31a842016-02-16 21:28:19 +00002120 fprintf(stream,
2121 " -o <block-timeo>: use specific xmodem block timeout\n");
Luka Perkovdff289642012-05-27 11:44:51 +00002122 fprintf(stream, "\n");
2123 fprintf(stream, " -t: mini terminal\n");
2124 fprintf(stream, "\n");
2125 fprintf(stream, " -B <baud>: set baud rate\n");
2126 fprintf(stream, "\n");
2127}
2128
2129int
2130main(int argc, char **argv)
2131{
2132 const char *ttypath, *imgpath;
Pali Rohár4d276012021-09-24 23:06:55 +02002133 int rv, rc, tty, term;
Pali Rohár224110d2022-03-02 11:49:20 +01002134 int bootmsg;
2135 int debugmsg;
Luka Perkovdff289642012-05-27 11:44:51 +00002136 void *img;
2137 size_t size;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002138 size_t after_img_rsv;
2139 int baudrate;
Pali Rohár48953362022-01-25 18:13:07 +01002140 int prev_optind;
2141 int c;
Luka Perkovdff289642012-05-27 11:44:51 +00002142
2143 rv = 1;
2144 tty = -1;
Pali Rohár224110d2022-03-02 11:49:20 +01002145 bootmsg = 0;
2146 debugmsg = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00002147 imgpath = NULL;
2148 img = NULL;
2149 term = 0;
Luka Perkovdff289642012-05-27 11:44:51 +00002150 size = 0;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002151 after_img_rsv = KWBOOT_XM_BLKSZ;
2152 baudrate = 115200;
Luka Perkovdff289642012-05-27 11:44:51 +00002153
Pali Rohár73ae7aa2021-11-05 23:30:42 +01002154 printf("kwboot version %s\n", PLAIN_VERSION);
2155
Luka Perkovdff289642012-05-27 11:44:51 +00002156 kwboot_verbose = isatty(STDOUT_FILENO);
2157
2158 do {
Pali Rohár48953362022-01-25 18:13:07 +01002159 prev_optind = optind;
2160 c = getopt(argc, argv, "hbptaB:dD:q:s:o:");
Luka Perkovdff289642012-05-27 11:44:51 +00002161 if (c < 0)
2162 break;
2163
2164 switch (c) {
2165 case 'b':
Pali Rohár48953362022-01-25 18:13:07 +01002166 if (imgpath || bootmsg || debugmsg)
2167 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002168 bootmsg = 1;
Pali Rohár48953362022-01-25 18:13:07 +01002169 if (prev_optind == optind)
2170 goto usage;
Pali Rohárfefee1a2022-03-07 19:03:09 +01002171 /* Option -b could have optional argument which specify image path */
2172 if (optind < argc && argv[optind] && argv[optind][0] != '-')
Pali Rohár48953362022-01-25 18:13:07 +01002173 imgpath = argv[optind++];
Luka Perkovdff289642012-05-27 11:44:51 +00002174 break;
2175
Stefan Roesebb5c4282014-10-22 12:13:21 +02002176 case 'D':
Pali Rohár48953362022-01-25 18:13:07 +01002177 if (imgpath || bootmsg || debugmsg)
2178 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002179 bootmsg = 0;
Stefan Roesebb5c4282014-10-22 12:13:21 +02002180 imgpath = optarg;
2181 break;
2182
2183 case 'd':
Pali Rohár48953362022-01-25 18:13:07 +01002184 if (imgpath || bootmsg || debugmsg)
2185 goto usage;
Pali Rohár224110d2022-03-02 11:49:20 +01002186 debugmsg = 1;
Stefan Roesebb5c4282014-10-22 12:13:21 +02002187 break;
2188
Luka Perkovdff289642012-05-27 11:44:51 +00002189 case 'p':
Pali Rohár4d276012021-09-24 23:06:55 +02002190 /* nop, for backward compatibility */
Luka Perkovdff289642012-05-27 11:44:51 +00002191 break;
2192
2193 case 't':
2194 term = 1;
2195 break;
2196
Stefan Roesebb5c4282014-10-22 12:13:21 +02002197 case 'a':
Stefan Roesebb5c4282014-10-22 12:13:21 +02002198 msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP;
2199 break;
2200
Stefan Roesef7f509d2015-05-29 13:25:04 +02002201 case 'q':
Pali Rohárd51b8342022-03-02 11:49:19 +01002202 /* nop, for backward compatibility */
Stefan Roesef7f509d2015-05-29 13:25:04 +02002203 break;
2204
2205 case 's':
2206 msg_rsp_timeo = atoi(optarg);
2207 break;
2208
Kevin Smith4d31a842016-02-16 21:28:19 +00002209 case 'o':
2210 blk_rsp_timeo = atoi(optarg);
2211 break;
2212
Luka Perkovdff289642012-05-27 11:44:51 +00002213 case 'B':
Pali Rohár2a8b7692021-09-24 23:07:05 +02002214 baudrate = atoi(optarg);
Luka Perkovdff289642012-05-27 11:44:51 +00002215 break;
2216
2217 case 'h':
2218 rv = 0;
2219 default:
2220 goto usage;
2221 }
2222 } while (1);
2223
Pali Rohár577506c2022-01-25 18:13:12 +01002224 if (!bootmsg && !term && !debugmsg && !imgpath)
Luka Perkovdff289642012-05-27 11:44:51 +00002225 goto usage;
2226
Pali Rohárfefee1a2022-03-07 19:03:09 +01002227 /*
2228 * If there is no remaining argument but optional imgpath was parsed
2229 * then it means that optional imgpath was eaten by getopt parser.
2230 * Reassing imgpath to required ttypath argument.
2231 */
2232 if (optind == argc && imgpath) {
2233 ttypath = imgpath;
2234 imgpath = NULL;
2235 } else if (optind + 1 == argc) {
2236 ttypath = argv[optind];
2237 } else {
Pali Rohár48953362022-01-25 18:13:07 +01002238 goto usage;
Pali Rohárfefee1a2022-03-07 19:03:09 +01002239 }
Pali Rohár48953362022-01-25 18:13:07 +01002240
Pali Rohár6c05cfa2022-03-07 19:03:07 +01002241 /* boot and debug message use baudrate 115200 */
2242 if (((bootmsg && !imgpath) || debugmsg) && baudrate != 115200) {
2243 fprintf(stderr, "Baudrate other than 115200 cannot be used for this operation.\n");
2244 goto usage;
2245 }
2246
Pali Rohárfab87482022-03-07 19:03:08 +01002247 tty = kwboot_open_tty(ttypath, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002248 if (tty < 0) {
2249 perror(ttypath);
2250 goto out;
2251 }
2252
Pali Rohárfab87482022-03-07 19:03:08 +01002253 /*
2254 * initial baudrate for image transfer is always 115200,
2255 * the change to different baudrate is done only after the header is sent
2256 */
2257 if (imgpath && baudrate != 115200) {
2258 rc = kwboot_tty_change_baudrate(tty, 115200);
2259 if (rc) {
2260 perror(ttypath);
2261 goto out;
2262 }
2263 }
2264
Pali Rohár2a8b7692021-09-24 23:07:05 +02002265 if (baudrate == 115200)
2266 /* do not change baudrate during Xmodem to the same value */
2267 baudrate = 0;
2268 else
2269 /* ensure we have enough space for baudrate change code */
Pali Rohár6303a232021-10-27 20:57:02 +02002270 after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 +
2271 sizeof(kwboot_baud_code_binhdr_pre) +
2272 sizeof(kwboot_baud_code) +
2273 sizeof(kwboot_baud_code_binhdr_post) +
Pali Rohárd8774392021-10-25 15:12:54 +02002274 KWBOOT_XM_BLKSZ +
Pali Rohárd8774392021-10-25 15:12:54 +02002275 sizeof(kwboot_baud_code) +
Pali Rohár6303a232021-10-27 20:57:02 +02002276 sizeof(kwboot_baud_code_data_jump) +
Pali Roháre0860662023-01-08 13:38:27 +01002277 sizeof(uint32_t) +
Pali Rohárd8774392021-10-25 15:12:54 +02002278 KWBOOT_XM_BLKSZ;
Pali Rohár2a8b7692021-09-24 23:07:05 +02002279
Luka Perkovdff289642012-05-27 11:44:51 +00002280 if (imgpath) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002281 img = kwboot_read_image(imgpath, &size, after_img_rsv);
Luka Perkovdff289642012-05-27 11:44:51 +00002282 if (!img) {
2283 perror(imgpath);
2284 goto out;
2285 }
Luka Perkovdff289642012-05-27 11:44:51 +00002286
Pali Rohár2a8b7692021-09-24 23:07:05 +02002287 rc = kwboot_img_patch(img, &size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002288 if (rc) {
2289 fprintf(stderr, "%s: Invalid image.\n", imgpath);
2290 goto out;
2291 }
2292 }
2293
Stefan Roesebb5c4282014-10-22 12:13:21 +02002294 if (debugmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002295 rc = kwboot_debugmsg(tty);
Pali Rohár2af673b2022-03-02 11:49:22 +01002296 if (rc)
Stefan Roesebb5c4282014-10-22 12:13:21 +02002297 goto out;
Willy Tarreaue8f8a7c2018-07-03 12:10:30 -04002298 } else if (bootmsg) {
Pali Rohár224110d2022-03-02 11:49:20 +01002299 rc = kwboot_bootmsg(tty);
Pali Rohár3a2e9bc2022-03-02 11:49:21 +01002300 if (rc)
Luka Perkovdff289642012-05-27 11:44:51 +00002301 goto out;
Luka Perkovdff289642012-05-27 11:44:51 +00002302 }
2303
2304 if (img) {
Pali Rohár2a8b7692021-09-24 23:07:05 +02002305 rc = kwboot_xmodem(tty, img, size, baudrate);
Luka Perkovdff289642012-05-27 11:44:51 +00002306 if (rc) {
2307 perror("xmodem");
2308 goto out;
2309 }
2310 }
2311
2312 if (term) {
2313 rc = kwboot_terminal(tty);
2314 if (rc && !(errno == EINTR)) {
2315 perror("terminal");
2316 goto out;
2317 }
2318 }
2319
2320 rv = 0;
2321out:
2322 if (tty >= 0)
2323 close(tty);
2324
2325 if (img)
Pali Rohárd9355bf2021-09-24 23:07:03 +02002326 free(img);
Luka Perkovdff289642012-05-27 11:44:51 +00002327
2328 return rv;
2329
2330usage:
2331 kwboot_usage(rv ? stderr : stdout, basename(argv[0]));
2332 goto out;
2333}