Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2 | * Boot a Marvell SoC, with Xmodem over UART0. |
Pali Rohár | f4e9e87 | 2022-03-02 11:49:26 +0100 | [diff] [blame] | 3 | * supports Kirkwood, Dove, Avanta, Armada 370, Armada XP, Armada 375, |
| 4 | * Armada 38x and Armada 39x. |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 5 | * |
| 6 | * (c) 2012 Daniel Stodden <daniel.stodden@gmail.com> |
Pali Rohár | aa98a1e | 2021-09-24 23:07:14 +0200 | [diff] [blame] | 7 | * (c) 2021 Pali Rohár <pali@kernel.org> |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 8 | * (c) 2021 Marek Behún <kabel@kernel.org> |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 9 | * |
Pali Rohár | 38db976 | 2022-03-02 11:49:27 +0100 | [diff] [blame] | 10 | * 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ár | 1817da8 | 2023-01-08 13:34:24 +0100 | [diff] [blame] | 18 | * - "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ár | 38db976 | 2022-03-02 11:49:27 +0100 | [diff] [blame] | 24 | * - "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ár | 1817da8 | 2023-01-08 13:34:24 +0100 | [diff] [blame] | 31 | * - "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ár | 38db976 | 2022-03-02 11:49:27 +0100 | [diff] [blame] | 40 | * - "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ár | 1817da8 | 2023-01-08 13:34:24 +0100 | [diff] [blame] | 53 | * - "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ár | 8417822 | 2023-01-21 19:57:28 +0100 | [diff] [blame] | 57 | * |
| 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 119 | */ |
| 120 | |
Stefan Roese | 04ec0d3 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 121 | #include "kwbimage.h" |
| 122 | #include "mkimage.h" |
Pali Rohár | 3c703aaf | 2021-09-24 23:06:42 +0200 | [diff] [blame] | 123 | #include "version.h" |
Stefan Roese | 04ec0d3 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 124 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 125 | #include <stdlib.h> |
| 126 | #include <stdio.h> |
| 127 | #include <string.h> |
| 128 | #include <stdarg.h> |
Stefan Roese | 04ec0d3 | 2016-01-07 14:12:04 +0100 | [diff] [blame] | 129 | #include <image.h> |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 130 | #include <libgen.h> |
| 131 | #include <fcntl.h> |
| 132 | #include <errno.h> |
| 133 | #include <unistd.h> |
| 134 | #include <stdint.h> |
Marek Behún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 135 | #include <time.h> |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 136 | #include <sys/stat.h> |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 137 | #include <pthread.h> |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 138 | |
Pali Rohár | fd935e9 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 139 | #ifdef __linux__ |
| 140 | #include "termios_linux.h" |
| 141 | #else |
| 142 | #include <termios.h> |
| 143 | #endif |
| 144 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 145 | /* |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 146 | * 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 | */ |
| 149 | extern int setupterm(const char *, int, int *); |
| 150 | extern char *tigetstr(const char *); |
| 151 | |
| 152 | /* |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 153 | * Marvell BootROM UART Sensing |
| 154 | */ |
| 155 | |
| 156 | static unsigned char kwboot_msg_boot[] = { |
| 157 | 0xBB, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 |
| 158 | }; |
| 159 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 160 | static unsigned char kwboot_msg_debug[] = { |
| 161 | 0xDD, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 |
| 162 | }; |
| 163 | |
| 164 | /* Defines known to work on Kirkwood */ |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 165 | #define KWBOOT_MSG_RSP_TIMEO 50 /* ms */ |
| 166 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 167 | /* Defines known to work on Armada XP */ |
Stefan Roese | 5285984 | 2022-08-19 09:43:59 +0200 | [diff] [blame] | 168 | #define KWBOOT_MSG_RSP_TIMEO_AXP 10 /* ms */ |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 169 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 170 | /* |
| 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 178 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 179 | #define KWBOOT_XM_BLKSZ 128 /* xmodem block size */ |
| 180 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 181 | struct kwboot_block { |
| 182 | uint8_t soh; |
| 183 | uint8_t pnum; |
| 184 | uint8_t _pnum; |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 185 | uint8_t data[KWBOOT_XM_BLKSZ]; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 186 | uint8_t csum; |
Pali Rohár | f01adfd | 2021-07-23 11:14:14 +0200 | [diff] [blame] | 187 | } __packed; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 188 | |
Pali Rohár | def9838 | 2022-01-25 18:13:00 +0100 | [diff] [blame] | 189 | #define KWBOOT_BLK_RSP_TIMEO 2000 /* ms */ |
Marek Behún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 190 | #define KWBOOT_HDR_RSP_TIMEO 10000 /* ms */ |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 191 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 192 | /* ARM code to change baudrate */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 193 | static unsigned char kwboot_baud_code[] = { |
| 194 | /* ; #define UART_BASE 0xd0012000 */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 195 | /* ; #define DLL 0x00 */ |
| 196 | /* ; #define DLH 0x04 */ |
| 197 | /* ; #define LCR 0x0c */ |
| 198 | /* ; #define DLAB 0x80 */ |
| 199 | /* ; #define LSR 0x14 */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 200 | /* ; #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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 204 | /* ; 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ár | 9e624c9 | 2021-10-27 20:57:00 +0200 | [diff] [blame] | 218 | /* ; msleep(5); */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 219 | /* ; return 0; */ |
| 220 | /* ; } */ |
| 221 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 222 | /* ; r0 = UART_BASE */ |
Pali Rohár | 15b16e5 | 2021-10-27 20:57:01 +0200 | [diff] [blame] | 223 | 0x0d, 0x02, 0xa0, 0xe3, /* mov r0, #0xd0000000 */ |
| 224 | 0x12, 0x0a, 0x80, 0xe3, /* orr r0, r0, #0x12000 */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 225 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 226 | /* ; 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ár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 251 | 0x74, 0x20, 0x9f, 0xe5, /* ldr r2, old_baudrate */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 252 | |
| 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ár | 9e624c9 | 2021-10-27 20:57:00 +0200 | [diff] [blame] | 258 | /* ; r2 = new_baudrate */ |
Pali Rohár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 259 | 0x70, 0x20, 0x9f, 0xe5, /* ldr r2, new_baudrate */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 260 | |
| 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ár | 9e624c9 | 2021-10-27 20:57:00 +0200 | [diff] [blame] | 299 | /* ; 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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 303 | /* .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ár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 308 | /* ; Jump to the end of execution */ |
| 309 | 0x01, 0x00, 0x00, 0xea, /* b end */ |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 310 | |
| 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ár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 318 | |
| 319 | /* end: */ |
| 320 | }; |
| 321 | |
Pali Rohár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 322 | /* ARM code from binary header executed by BootROM before changing baudrate */ |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 323 | static unsigned char kwboot_baud_code_binhdr_pre[] = { |
Pali Rohár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 324 | /* ; #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ár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 341 | 0xfe, 0x5f, 0x2d, 0xe9, /* push { r1 - r12, lr } */ |
Pali Rohár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 342 | |
| 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ár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 379 | }; |
| 380 | |
Pali Rohár | 2d760ed | 2021-11-01 14:00:02 +0100 | [diff] [blame] | 381 | /* ARM code for returning from binary header back to BootROM */ |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 382 | static 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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 386 | }; |
| 387 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 388 | /* ARM code for jumping to the original image exec_addr */ |
| 389 | static 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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 395 | |
| 396 | static const char kwb_baud_magic[16] = "$baudratechange"; |
| 397 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 398 | static int kwboot_verbose; |
| 399 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 400 | static int msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO; |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 401 | static int blk_rsp_timeo = KWBOOT_BLK_RSP_TIMEO; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 402 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 403 | static ssize_t |
| 404 | kwboot_write(int fd, const char *buf, size_t len) |
| 405 | { |
Pali Rohár | 5350df1 | 2022-01-25 18:13:10 +0100 | [diff] [blame] | 406 | ssize_t tot = 0; |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 407 | |
| 408 | while (tot < len) { |
| 409 | ssize_t wr = write(fd, buf + tot, len - tot); |
| 410 | |
Pali Rohár | 5350df1 | 2022-01-25 18:13:10 +0100 | [diff] [blame] | 411 | if (wr < 0 && errno == EINTR) |
| 412 | continue; |
| 413 | else if (wr < 0) |
| 414 | return wr; |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 415 | |
| 416 | tot += wr; |
| 417 | } |
| 418 | |
| 419 | return tot; |
| 420 | } |
| 421 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 422 | static void |
| 423 | kwboot_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 | |
| 435 | static 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 | |
| 452 | static void |
| 453 | kwboot_spinner(void) |
| 454 | { |
| 455 | if (kwboot_verbose) |
| 456 | __spinner(); |
| 457 | } |
| 458 | |
| 459 | static 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ár | d01b9ce | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 472 | pos = (pos + 1) % width; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 473 | |
| 474 | if (pct == 100) { |
Pali Rohár | d01b9ce | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 475 | while (pos && pos++ < width) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 476 | fputc(' ', stdout); |
| 477 | fputs(nl, stdout); |
Pali Rohár | d01b9ce | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 478 | nl = ""; |
| 479 | pos = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | fflush(stdout); |
| 483 | |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | kwboot_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ár | d01b9ce | 2021-09-24 23:06:46 +0200 | [diff] [blame] | 496 | |
| 497 | if (pct == 100) |
| 498 | pct = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static int |
| 502 | kwboot_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ár | 613bd8b | 2022-01-25 18:13:11 +0100 | [diff] [blame] | 523 | if (nfds < 0 && errno == EINTR) |
| 524 | continue; |
| 525 | else if (nfds < 0) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 526 | goto out; |
Pali Rohár | 613bd8b | 2022-01-25 18:13:11 +0100 | [diff] [blame] | 527 | else if (!nfds) { |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 528 | errno = ETIMEDOUT; |
| 529 | goto out; |
| 530 | } |
| 531 | |
| 532 | n = read(fd, buf, len); |
Pali Rohár | 613bd8b | 2022-01-25 18:13:11 +0100 | [diff] [blame] | 533 | if (n < 0 && errno == EINTR) |
| 534 | continue; |
| 535 | else if (n <= 0) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 536 | goto out; |
| 537 | |
| 538 | buf = (char *)buf + n; |
| 539 | len -= n; |
| 540 | } while (len > 0); |
| 541 | |
| 542 | rc = 0; |
| 543 | out: |
| 544 | return rc; |
| 545 | } |
| 546 | |
| 547 | static int |
Pali Rohár | f2acbba | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 548 | kwboot_tty_send(int fd, const void *buf, size_t len, int nodrain) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 549 | { |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 550 | if (!buf) |
| 551 | return 0; |
| 552 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 553 | if (kwboot_write(fd, buf, len) < 0) |
| 554 | return -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 555 | |
Pali Rohár | f2acbba | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 556 | if (nodrain) |
| 557 | return 0; |
| 558 | |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 559 | return tcdrain(fd); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static int |
| 563 | kwboot_tty_send_char(int fd, unsigned char c) |
| 564 | { |
Pali Rohár | f2acbba | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 565 | return kwboot_tty_send(fd, &c, 1, 0); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static speed_t |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 569 | kwboot_tty_baudrate_to_speed(int baudrate) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 570 | { |
| 571 | switch (baudrate) { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 572 | #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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 637 | case 115200: |
| 638 | return B115200; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 639 | #endif |
| 640 | #ifdef B76800 |
| 641 | case 76800: |
| 642 | return B76800; |
| 643 | #endif |
| 644 | #ifdef B57600 |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 645 | case 57600: |
| 646 | return B57600; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 647 | #endif |
| 648 | #ifdef B38400 |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 649 | case 38400: |
| 650 | return B38400; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 651 | #endif |
| 652 | #ifdef B19200 |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 653 | case 19200: |
| 654 | return B19200; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 655 | #endif |
| 656 | #ifdef B9600 |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 657 | case 9600: |
| 658 | return B9600; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 659 | #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ár | fd935e9 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 709 | #ifdef BOTHER |
| 710 | return BOTHER; |
| 711 | #else |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 712 | return B0; |
Pali Rohár | fd935e9 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 713 | #endif |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 714 | } |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static int |
Marek Behún | 6783549 | 2021-09-24 23:07:07 +0200 | [diff] [blame] | 718 | _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 | |
| 724 | static int |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 725 | kwboot_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ár | fd935e9 | 2021-09-24 23:07:06 +0200 | [diff] [blame] | 741 | #ifdef BOTHER |
| 742 | if (speed == BOTHER) |
| 743 | tio.c_ospeed = tio.c_ispeed = baudrate; |
| 744 | #endif |
| 745 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 746 | rc = cfsetospeed(&tio, speed); |
| 747 | if (rc) |
| 748 | return rc; |
| 749 | |
| 750 | rc = cfsetispeed(&tio, speed); |
| 751 | if (rc) |
| 752 | return rc; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 753 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 754 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 755 | if (rc) |
| 756 | return rc; |
| 757 | |
Marek Behún | 6783549 | 2021-09-24 23:07:07 +0200 | [diff] [blame] | 758 | 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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 778 | return 0; |
Marek Behún | 6783549 | 2021-09-24 23:07:07 +0200 | [diff] [blame] | 779 | |
| 780 | baud_fail: |
| 781 | fprintf(stderr, "Could not set baudrate to requested value\n"); |
| 782 | errno = EINVAL; |
| 783 | return -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static int |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 787 | kwboot_open_tty(const char *path, int baudrate) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 788 | { |
Pali Rohár | 2bffe24 | 2021-09-24 23:07:10 +0200 | [diff] [blame] | 789 | int rc, fd, flags; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 790 | struct termios tio; |
| 791 | |
| 792 | rc = -1; |
| 793 | |
Marek Behún | 6c598c3 | 2021-09-24 23:07:11 +0200 | [diff] [blame] | 794 | fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 795 | if (fd < 0) |
| 796 | goto out; |
| 797 | |
Pali Rohár | 909a0b9 | 2021-09-24 23:07:08 +0200 | [diff] [blame] | 798 | rc = tcgetattr(fd, &tio); |
| 799 | if (rc) |
| 800 | goto out; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 801 | |
Pali Rohár | 909a0b9 | 2021-09-24 23:07:08 +0200 | [diff] [blame] | 802 | cfmakeraw(&tio); |
Marek Behún | 6c598c3 | 2021-09-24 23:07:11 +0200 | [diff] [blame] | 803 | tio.c_cflag |= CREAD | CLOCAL; |
Pali Rohár | 40003a0 | 2021-10-25 15:12:53 +0200 | [diff] [blame] | 804 | tio.c_cflag &= ~(CSTOPB | HUPCL | CRTSCTS); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 805 | tio.c_cc[VMIN] = 1; |
Pali Rohár | 12095b2 | 2021-09-24 23:07:09 +0200 | [diff] [blame] | 806 | tio.c_cc[VTIME] = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 807 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 808 | rc = tcsetattr(fd, TCSANOW, &tio); |
| 809 | if (rc) |
| 810 | goto out; |
| 811 | |
Pali Rohár | 2bffe24 | 2021-09-24 23:07:10 +0200 | [diff] [blame] | 812 | 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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 820 | rc = kwboot_tty_change_baudrate(fd, baudrate); |
| 821 | if (rc) |
| 822 | goto out; |
| 823 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 824 | rc = fd; |
| 825 | out: |
| 826 | if (rc < 0) { |
| 827 | if (fd >= 0) |
| 828 | close(fd); |
| 829 | } |
| 830 | |
| 831 | return rc; |
| 832 | } |
| 833 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 834 | static void * |
| 835 | kwboot_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 | |
| 863 | static int |
| 864 | kwboot_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 | |
| 880 | static int |
| 881 | kwboot_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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 900 | static int |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 901 | kwboot_bootmsg(int tty) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 902 | { |
Pali Rohár | e68c27bd | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 903 | struct kwboot_block block; |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 904 | pthread_t write_thread; |
| 905 | int rc, err; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 906 | char c; |
| 907 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 908 | /* flush input and output queue */ |
| 909 | tcflush(tty, TCIOFLUSH); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 910 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 916 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 917 | kwboot_printv("Sending boot message. Please reboot the target..."); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 918 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 919 | err = 0; |
| 920 | while (1) { |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 921 | kwboot_spinner(); |
| 922 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 923 | 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 934 | |
| 935 | kwboot_printv("\n"); |
| 936 | |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 937 | rc = kwboot_msg_stop_thread(write_thread); |
| 938 | if (rc) { |
| 939 | perror("Failed to stop write thread"); |
Pali Rohár | e68c27bd | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 940 | return rc; |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (err) { |
| 944 | errno = err; |
| 945 | perror("Failed to read response for boot message pattern"); |
| 946 | return -1; |
| 947 | } |
Pali Rohár | e68c27bd | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 948 | |
| 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ár | e3a1828 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 964 | rc = tcflush(tty, TCOFLUSH); |
| 965 | if (rc) { |
| 966 | perror("Failed to flush output queue"); |
| 967 | return rc; |
| 968 | } |
Pali Rohár | e68c27bd | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 969 | |
| 970 | /* send one xmodem packet with 0xff bytes to force BootROM to re-sync */ |
| 971 | memset(&block, 0xff, sizeof(block)); |
Pali Rohár | e3a1828 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 972 | 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ár | e68c27bd | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 977 | |
| 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ár | e3a1828 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 985 | rc = tcflush(tty, TCIFLUSH); |
| 986 | if (rc) { |
| 987 | perror("Failed to flush input queue"); |
| 988 | return rc; |
| 989 | } |
Pali Rohár | e68c27bd | 2022-01-25 18:13:08 +0100 | [diff] [blame] | 990 | |
| 991 | return 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static int |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 995 | kwboot_debugmsg(int tty) |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 996 | { |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 997 | unsigned char buf[8192]; |
| 998 | pthread_t write_thread; |
| 999 | int rc, err, i, pos; |
| 1000 | size_t off; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1001 | |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 1002 | /* flush input and output queue */ |
| 1003 | tcflush(tty, TCIOFLUSH); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1004 | |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 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 Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1010 | |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 1011 | kwboot_printv("Sending debug message. Please reboot the target..."); |
| 1012 | kwboot_spinner(); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1013 | |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 1014 | 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ár | e3a1828 | 2022-03-02 11:49:18 +0100 | [diff] [blame] | 1023 | break; |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 1024 | } |
| 1025 | off += rc - 1; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1026 | |
| 1027 | kwboot_spinner(); |
| 1028 | |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 1029 | /* |
| 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 Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1050 | |
| 1051 | kwboot_printv("\n"); |
| 1052 | |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 1053 | 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 Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Pali Rohár | 58cf04de | 2021-09-24 23:06:44 +0200 | [diff] [blame] | 1132 | static size_t |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1133 | kwboot_xm_makeblock(struct kwboot_block *block, const void *data, |
| 1134 | size_t size, int pnum) |
| 1135 | { |
Marek Behún | edb6324 | 2021-09-24 23:06:45 +0200 | [diff] [blame] | 1136 | size_t i, n; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1137 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1138 | block->soh = SOH; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1139 | block->pnum = pnum; |
| 1140 | block->_pnum = ~block->pnum; |
| 1141 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1142 | n = size < KWBOOT_XM_BLKSZ ? size : KWBOOT_XM_BLKSZ; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1143 | memcpy(&block->data[0], data, n); |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1144 | memset(&block->data[n], 0, KWBOOT_XM_BLKSZ - n); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1145 | |
| 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ún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 1153 | static 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1173 | static int |
Marek Behún | eabacb8 | 2021-09-24 23:06:49 +0200 | [diff] [blame] | 1174 | _is_xm_reply(char c) |
| 1175 | { |
Pali Rohár | 6af1429 | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1176 | return c == ACK || c == NAK; |
Marek Behún | eabacb8 | 2021-09-24 23:06:49 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static int |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1180 | _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ár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1191 | default: |
| 1192 | errno = EPROTO; |
| 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | return rc; |
| 1197 | } |
| 1198 | |
| 1199 | static int |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1200 | kwboot_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 | |
| 1225 | static int |
Pali Rohár | 230d4f8 | 2022-01-25 18:13:04 +0100 | [diff] [blame] | 1226 | kwboot_xm_recv_reply(int fd, char *c, int stop_on_non_xm, |
Pali Rohár | e4529bd | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1227 | int ignore_nak_reply, |
Pali Rohár | 959e850 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1228 | int allow_non_xm, int *non_xm_print, |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1229 | int baudrate, int *baud_changed) |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1230 | { |
Marek Behún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 1231 | int timeout = allow_non_xm ? KWBOOT_HDR_RSP_TIMEO : blk_rsp_timeo; |
Marek Behún | 0a3b85a | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 1232 | uint64_t recv_until = _now() + timeout; |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1233 | int rc; |
| 1234 | |
| 1235 | while (1) { |
Marek Behún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 1236 | rc = kwboot_tty_recv(fd, c, 1, timeout); |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1237 | if (rc) { |
| 1238 | if (errno != ETIMEDOUT) |
| 1239 | return rc; |
Marek Behún | 0a3b85a | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 1240 | else if (allow_non_xm && *non_xm_print) |
Marek Behún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 1241 | return -1; |
| 1242 | else |
| 1243 | *c = NAK; |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* If received xmodem reply, end. */ |
Pali Rohár | e4529bd | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1247 | 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ár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1253 | break; |
Pali Rohár | e4529bd | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1254 | } |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1255 | |
| 1256 | /* |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1257 | * 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ún | 0a3b85a | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 1263 | * Otherwise decrease timeout by time elapsed. |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1264 | */ |
| 1265 | if (allow_non_xm) { |
Marek Behún | 2d9f245 | 2021-09-24 23:06:52 +0200 | [diff] [blame] | 1266 | recv_until = _now() + timeout; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1267 | |
| 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ún | 0a3b85a | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 1281 | } else { |
Pali Rohár | 230d4f8 | 2022-01-25 18:13:04 +0100 | [diff] [blame] | 1282 | if (stop_on_non_xm) |
Pali Rohár | 959e850 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1283 | break; |
Marek Behún | 0a3b85a | 2021-09-24 23:06:53 +0200 | [diff] [blame] | 1284 | timeout = recv_until - _now(); |
| 1285 | if (timeout < 0) { |
| 1286 | errno = ETIMEDOUT; |
| 1287 | return -1; |
| 1288 | } |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | static int |
| 1296 | kwboot_xm_sendblock(int fd, struct kwboot_block *block, int allow_non_xm, |
Pali Rohár | 80241ce | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1297 | int *done_print, int baudrate, int allow_retries) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1298 | { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1299 | int non_xm_print, baud_changed; |
| 1300 | int rc, err, retries; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1301 | char c; |
| 1302 | |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1303 | *done_print = 0; |
Pali Rohár | 6daa561 | 2021-10-27 20:56:58 +0200 | [diff] [blame] | 1304 | non_xm_print = 0; |
| 1305 | baud_changed = 0; |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1306 | |
Pali Rohár | e665c6a | 2021-10-25 15:13:03 +0200 | [diff] [blame] | 1307 | retries = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1308 | do { |
Pali Rohár | f2acbba | 2021-10-27 20:56:59 +0200 | [diff] [blame] | 1309 | rc = kwboot_tty_send(fd, block, sizeof(*block), 1); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1310 | if (rc) |
Pali Rohár | 6af1429 | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1311 | goto err; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1312 | |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1313 | if (allow_non_xm && !*done_print) { |
| 1314 | kwboot_progress(100, '.'); |
| 1315 | kwboot_printv("Done\n"); |
| 1316 | *done_print = 1; |
| 1317 | } |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 1318 | |
Pali Rohár | 959e850 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1319 | rc = kwboot_xm_recv_reply(fd, &c, retries < 3, |
Pali Rohár | e4529bd | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1320 | retries > 8, |
Pali Rohár | 959e850 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1321 | allow_non_xm, &non_xm_print, |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1322 | baudrate, &baud_changed); |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1323 | if (rc) |
Pali Rohár | 6af1429 | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1324 | goto err; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1325 | |
Pali Rohár | b3760f9 | 2022-01-25 18:13:06 +0100 | [diff] [blame] | 1326 | 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ár | 80241ce | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1332 | } while (c == NAK && allow_retries && retries++ < 16); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1333 | |
Marek Behún | bcc5e04 | 2021-09-24 23:06:51 +0200 | [diff] [blame] | 1334 | if (non_xm_print) |
| 1335 | kwboot_printv("\n"); |
| 1336 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1337 | if (allow_non_xm && baudrate && !baud_changed) { |
| 1338 | fprintf(stderr, "Baudrate was not changed\n"); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1339 | errno = EPROTO; |
Pali Rohár | 6af1429 | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1340 | return -1; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1341 | } |
| 1342 | |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1343 | return _xm_reply_to_error(c); |
Pali Rohár | 6af1429 | 2022-01-25 18:13:03 +0100 | [diff] [blame] | 1344 | err: |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1345 | err = errno; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1346 | kwboot_printv("\n"); |
| 1347 | errno = err; |
| 1348 | return rc; |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1349 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1350 | |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1351 | static int |
| 1352 | kwboot_xm_finish(int fd) |
| 1353 | { |
| 1354 | int rc, retries; |
| 1355 | char c; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1356 | |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1357 | kwboot_printv("Finishing transfer\n"); |
| 1358 | |
Pali Rohár | e665c6a | 2021-10-25 15:13:03 +0200 | [diff] [blame] | 1359 | retries = 0; |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1360 | do { |
| 1361 | rc = kwboot_tty_send_char(fd, EOT); |
| 1362 | if (rc) |
| 1363 | return rc; |
| 1364 | |
Pali Rohár | 959e850 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1365 | rc = kwboot_xm_recv_reply(fd, &c, retries < 3, |
Pali Rohár | e4529bd | 2022-01-25 18:13:02 +0100 | [diff] [blame] | 1366 | retries > 8, |
Pali Rohár | 959e850 | 2021-10-25 15:13:04 +0200 | [diff] [blame] | 1367 | 0, NULL, 0, NULL); |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1368 | if (rc) |
| 1369 | return rc; |
Pali Rohár | e665c6a | 2021-10-25 15:13:03 +0200 | [diff] [blame] | 1370 | } while (c == NAK && retries++ < 16); |
Pali Rohár | 42c61a4 | 2021-09-24 23:06:54 +0200 | [diff] [blame] | 1371 | |
| 1372 | return _xm_reply_to_error(c); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | static int |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1376 | kwboot_xmodem_one(int tty, int *pnum, int header, const uint8_t *data, |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1377 | size_t size, int baudrate) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1378 | { |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1379 | int done_print = 0; |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1380 | size_t sent, left; |
| 1381 | int rc; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1382 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1383 | kwboot_printv("Sending boot image %s (%zu bytes)...\n", |
| 1384 | header ? "header" : "data", size); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1385 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1386 | left = size; |
| 1387 | sent = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1388 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1389 | while (sent < size) { |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1390 | struct kwboot_block block; |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1391 | int last_block; |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1392 | size_t blksz; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1393 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1394 | blksz = kwboot_xm_makeblock(&block, data, left, (*pnum)++); |
| 1395 | data += blksz; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1396 | |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1397 | last_block = (left <= blksz); |
| 1398 | |
Pali Rohár | 80241ce | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1399 | /* |
| 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ár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1421 | rc = kwboot_xm_sendblock(tty, &block, header && last_block, |
Pali Rohár | 80241ce | 2022-01-25 18:13:05 +0100 | [diff] [blame] | 1422 | &done_print, baudrate, header); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1423 | if (rc) |
| 1424 | goto out; |
| 1425 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1426 | sent += blksz; |
| 1427 | left -= blksz; |
| 1428 | |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1429 | if (!done_print) |
| 1430 | kwboot_progress(sent * 100 / size, '.'); |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1431 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1432 | |
Pali Rohár | d06d520 | 2021-09-24 23:06:50 +0200 | [diff] [blame] | 1433 | if (!done_print) |
| 1434 | kwboot_printv("Done\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1435 | |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1436 | return 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1437 | out: |
Pali Rohár | 5ed896f | 2021-09-24 23:06:47 +0200 | [diff] [blame] | 1438 | kwboot_printv("\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1439 | return rc; |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static int |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1443 | kwboot_xmodem(int tty, const void *_img, size_t size, int baudrate) |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1444 | { |
| 1445 | const uint8_t *img = _img; |
| 1446 | int rc, pnum; |
| 1447 | size_t hdrsz; |
| 1448 | |
Marek Behún | d1b0b03 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1449 | hdrsz = kwbheader_size(img); |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1450 | |
Pali Rohár | 400b5cf | 2021-11-05 23:29:58 +0100 | [diff] [blame] | 1451 | /* |
| 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ár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1459 | pnum = 1; |
| 1460 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1461 | rc = kwboot_xmodem_one(tty, &pnum, 1, img, hdrsz, baudrate); |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1462 | if (rc) |
| 1463 | return rc; |
| 1464 | |
Pali Rohár | 400b5cf | 2021-11-05 23:29:58 +0100 | [diff] [blame] | 1465 | /* |
| 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ár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1476 | |
| 1477 | rc = kwboot_xm_finish(tty); |
Pali Rohár | bed18ef | 2021-09-24 23:06:48 +0200 | [diff] [blame] | 1478 | if (rc) |
| 1479 | return rc; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1480 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1481 | if (baudrate) { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1482 | 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | static int |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1492 | kwboot_term_pipe(int in, int out, const char *quit, int *s, const char *kbs, int *k) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1493 | { |
Pali Rohár | 111779f | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1494 | char buf[128]; |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1495 | ssize_t nin, noff; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1496 | |
Pali Rohár | 111779f | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1497 | nin = read(in, buf, sizeof(buf)); |
Willy Tarreau | ab7e8a1 | 2018-07-03 12:10:31 -0400 | [diff] [blame] | 1498 | if (nin <= 0) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1499 | return -1; |
| 1500 | |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1501 | noff = 0; |
| 1502 | |
| 1503 | if (quit || kbs) { |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1504 | int i; |
| 1505 | |
| 1506 | for (i = 0; i < nin; i++) { |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1507 | 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1529 | (*s)++; |
Pali Rohár | 111779f | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1530 | if (!quit[*s]) { |
Pali Rohár | 6a5d131 | 2022-02-18 12:24:13 +0100 | [diff] [blame] | 1531 | nin = (i > *s) ? (i - *s) : 0; |
Pali Rohár | 111779f | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1532 | break; |
| 1533 | } |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1534 | } else if (quit) { |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 1535 | *s = 0; |
Pali Rohár | 48615ba | 2021-07-23 11:14:20 +0200 | [diff] [blame] | 1536 | } |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1537 | |
| 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1557 | } |
Pali Rohár | 111779f | 2022-02-03 17:45:20 +0100 | [diff] [blame] | 1558 | |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1559 | 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1569 | if (nin > noff && kwboot_write(out, buf + noff, nin - noff) < 0) |
Marek Behún | d3bc5c3 | 2021-09-24 23:06:41 +0200 | [diff] [blame] | 1570 | return -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
| 1575 | static int |
| 1576 | kwboot_terminal(int tty) |
| 1577 | { |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1578 | int rc, in, s, k; |
| 1579 | const char *kbs = NULL; |
Marek Behún | ea5b2b3 | 2021-09-24 23:06:40 +0200 | [diff] [blame] | 1580 | const char *quit = "\34c"; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1581 | 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ár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1598 | /* |
| 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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1625 | kwboot_printv("[Type Ctrl-%c + %c to quit]\r\n", |
Marek Behún | 6c598c3 | 2021-09-24 23:07:11 +0200 | [diff] [blame] | 1626 | quit[0] | 0100, quit[1]); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1627 | } else |
| 1628 | in = -1; |
| 1629 | |
| 1630 | rc = 0; |
| 1631 | s = 0; |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1632 | k = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1633 | |
| 1634 | do { |
| 1635 | fd_set rfds; |
| 1636 | int nfds = 0; |
| 1637 | |
Pali Rohár | c1efca4 | 2021-10-25 15:12:52 +0200 | [diff] [blame] | 1638 | FD_ZERO(&rfds); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1639 | 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ár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1652 | rc = kwboot_term_pipe(tty, STDOUT_FILENO, NULL, NULL, NULL, NULL); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1653 | if (rc) |
| 1654 | break; |
| 1655 | } |
| 1656 | |
Marek Behún | 4eb55de | 2021-09-24 23:06:39 +0200 | [diff] [blame] | 1657 | if (in >= 0 && FD_ISSET(in, &rfds)) { |
Pali Rohár | ed3a975 | 2022-03-02 11:49:23 +0100 | [diff] [blame] | 1658 | rc = kwboot_term_pipe(in, tty, quit, &s, kbs, &k); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1659 | if (rc) |
| 1660 | break; |
| 1661 | } |
| 1662 | } while (quit[s] != 0); |
| 1663 | |
Pali Rohár | eafc969 | 2021-07-23 11:14:18 +0200 | [diff] [blame] | 1664 | if (in >= 0) |
| 1665 | tcsetattr(in, TCSANOW, &otio); |
Pali Rohár | 049cf91 | 2021-07-23 11:14:19 +0200 | [diff] [blame] | 1666 | printf("\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1667 | out: |
| 1668 | return rc; |
| 1669 | } |
| 1670 | |
| 1671 | static void * |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1672 | kwboot_read_image(const char *path, size_t *size, size_t reserve) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1673 | { |
Pali Rohár | 4d27601 | 2021-09-24 23:06:55 +0200 | [diff] [blame] | 1674 | int rc, fd; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1675 | void *img; |
Pali Rohár | 6f2aa8e | 2022-04-06 15:18:59 +0200 | [diff] [blame] | 1676 | off_t len; |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1677 | off_t tot; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1678 | |
| 1679 | rc = -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1680 | img = NULL; |
| 1681 | |
| 1682 | fd = open(path, O_RDONLY); |
| 1683 | if (fd < 0) |
| 1684 | goto out; |
| 1685 | |
Pali Rohár | 6f2aa8e | 2022-04-06 15:18:59 +0200 | [diff] [blame] | 1686 | len = lseek(fd, 0, SEEK_END); |
| 1687 | if (len == (off_t)-1) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1688 | goto out; |
| 1689 | |
Pali Rohár | 6f2aa8e | 2022-04-06 15:18:59 +0200 | [diff] [blame] | 1690 | if (lseek(fd, 0, SEEK_SET) == (off_t)-1) |
| 1691 | goto out; |
| 1692 | |
| 1693 | img = malloc(len + reserve); |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1694 | if (!img) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1695 | goto out; |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1696 | |
| 1697 | tot = 0; |
Pali Rohár | 6f2aa8e | 2022-04-06 15:18:59 +0200 | [diff] [blame] | 1698 | while (tot < len) { |
| 1699 | ssize_t rd = read(fd, img + tot, len - tot); |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1700 | |
| 1701 | if (rd < 0) |
| 1702 | goto out; |
| 1703 | |
| 1704 | tot += rd; |
| 1705 | |
Pali Rohár | 6f2aa8e | 2022-04-06 15:18:59 +0200 | [diff] [blame] | 1706 | if (!rd && tot < len) { |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1707 | errno = EIO; |
| 1708 | goto out; |
| 1709 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | rc = 0; |
Pali Rohár | 6f2aa8e | 2022-04-06 15:18:59 +0200 | [diff] [blame] | 1713 | *size = len; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1714 | out: |
| 1715 | if (rc && img) { |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1716 | free(img); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1717 | img = NULL; |
| 1718 | } |
| 1719 | if (fd >= 0) |
| 1720 | close(fd); |
| 1721 | |
| 1722 | return img; |
| 1723 | } |
| 1724 | |
| 1725 | static uint8_t |
Marek Behún | d1b0b03 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1726 | kwboot_hdr_csum8(const void *hdr) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1727 | { |
Marek Behún | d1b0b03 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1728 | const uint8_t *data = hdr; |
| 1729 | uint8_t csum; |
| 1730 | size_t size; |
| 1731 | |
| 1732 | size = kwbheader_size_for_csum(hdr); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1733 | |
| 1734 | for (csum = 0; size-- > 0; data++) |
| 1735 | csum += *data; |
| 1736 | |
| 1737 | return csum; |
| 1738 | } |
| 1739 | |
Pali Rohár | 0b659e5 | 2021-10-25 15:12:55 +0200 | [diff] [blame] | 1740 | static uint32_t * |
| 1741 | kwboot_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 | |
| 1751 | static uint32_t |
| 1752 | kwboot_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 Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1771 | static int |
Pali Rohár | 5725e0c | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1772 | kwboot_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ár | 08022e2 | 2023-01-08 13:46:14 +0100 | [diff] [blame] | 1783 | static int |
| 1784 | kwboot_img_has_ddr_init(void *img) |
| 1785 | { |
| 1786 | const struct register_set_hdr_v1 *rhdr; |
| 1787 | const struct main_hdr_v0 *hdr0; |
| 1788 | struct opt_hdr_v1 *ohdr; |
| 1789 | u32 ohdrsz; |
| 1790 | int last; |
| 1791 | |
| 1792 | /* |
| 1793 | * kwbimage v0 image headers contain DDR init code either in |
| 1794 | * extension header or in binary code header. |
| 1795 | */ |
| 1796 | if (kwbimage_version(img) == 0) { |
| 1797 | hdr0 = img; |
| 1798 | return hdr0->ext || hdr0->bin; |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | * kwbimage v1 image headers contain DDR init code either in binary |
| 1803 | * code header or in a register set list header with SDRAM_SETUP. |
| 1804 | */ |
| 1805 | for_each_opt_hdr_v1 (ohdr, img) { |
| 1806 | if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) |
| 1807 | return 1; |
| 1808 | if (ohdr->headertype == OPT_HDR_V1_REGISTER_TYPE) { |
| 1809 | rhdr = (const struct register_set_hdr_v1 *)ohdr; |
| 1810 | ohdrsz = opt_hdr_v1_size(ohdr); |
| 1811 | if (ohdrsz >= sizeof(*ohdr) + sizeof(rhdr->data[0].last_entry)) { |
| 1812 | ohdrsz -= sizeof(*ohdr) + sizeof(rhdr->data[0].last_entry); |
| 1813 | last = ohdrsz / sizeof(rhdr->data[0].entry); |
| 1814 | if (rhdr->data[last].last_entry.delay == |
| 1815 | REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP) |
| 1816 | return 1; |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1824 | static void * |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1825 | kwboot_img_grow_data_right(void *img, size_t *size, size_t grow) |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1826 | { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1827 | struct main_hdr_v1 *hdr = img; |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1828 | void *result; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1829 | |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1830 | /* |
| 1831 | * 32-bit checksum comes after end of image code, so we will be putting |
| 1832 | * new code there. So we get this pointer and then increase data size |
| 1833 | * (since increasing data size changes kwboot_img_csum32_ptr() return |
| 1834 | * value). |
| 1835 | */ |
| 1836 | result = kwboot_img_csum32_ptr(img); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1837 | hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow); |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1838 | *size += grow; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1839 | |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1840 | return result; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1843 | static void |
| 1844 | kwboot_img_grow_hdr(void *img, size_t *size, size_t grow) |
| 1845 | { |
| 1846 | uint32_t hdrsz, datasz, srcaddr; |
| 1847 | struct main_hdr_v1 *hdr = img; |
Pali Rohár | 6fa9709 | 2021-10-25 15:13:02 +0200 | [diff] [blame] | 1848 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1849 | uint8_t *data; |
| 1850 | |
| 1851 | srcaddr = le32_to_cpu(hdr->srcaddr); |
| 1852 | |
Pali Rohár | 6fa9709 | 2021-10-25 15:13:02 +0200 | [diff] [blame] | 1853 | /* calculate real used space in kwbimage header */ |
| 1854 | if (kwbimage_version(img) == 0) { |
| 1855 | hdrsz = kwbheader_size(img); |
| 1856 | } else { |
| 1857 | hdrsz = sizeof(*hdr); |
| 1858 | for_each_opt_hdr_v1 (ohdr, hdr) |
| 1859 | hdrsz += opt_hdr_v1_size(ohdr); |
| 1860 | } |
| 1861 | |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1862 | data = (uint8_t *)img + srcaddr; |
| 1863 | datasz = *size - srcaddr; |
| 1864 | |
| 1865 | /* only move data if there is not enough space */ |
| 1866 | if (hdrsz + grow > srcaddr) { |
| 1867 | size_t need = hdrsz + grow - srcaddr; |
| 1868 | |
| 1869 | /* move data by enough bytes */ |
| 1870 | memmove(data + need, data, datasz); |
| 1871 | |
| 1872 | hdr->srcaddr = cpu_to_le32(srcaddr + need); |
| 1873 | *size += need; |
| 1874 | } |
| 1875 | |
| 1876 | if (kwbimage_version(img) == 1) { |
| 1877 | hdrsz += grow; |
Pali Rohár | 6fa9709 | 2021-10-25 15:13:02 +0200 | [diff] [blame] | 1878 | if (hdrsz > kwbheader_size(img)) { |
| 1879 | hdr->headersz_msb = hdrsz >> 16; |
| 1880 | hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff); |
| 1881 | } |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1882 | } |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | static void * |
| 1886 | kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz) |
| 1887 | { |
| 1888 | struct main_hdr_v1 *hdr = img; |
| 1889 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | 87cc3c9 | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1890 | uint32_t num_args; |
| 1891 | uint32_t offset; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1892 | uint32_t ohdrsz; |
Pali Rohár | 90eb900 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1893 | uint8_t *prev_ext; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1894 | |
Pali Rohár | 32eec19 | 2022-01-12 18:20:52 +0100 | [diff] [blame] | 1895 | if (hdr->ext) { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1896 | for_each_opt_hdr_v1 (ohdr, img) |
| 1897 | if (opt_hdr_v1_next(ohdr) == NULL) |
| 1898 | break; |
| 1899 | |
Pali Rohár | 90eb900 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1900 | prev_ext = opt_hdr_v1_ext(ohdr); |
| 1901 | ohdr = _opt_hdr_v1_next(ohdr); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1902 | } else { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1903 | ohdr = (void *)(hdr + 1); |
Pali Rohár | 90eb900 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1904 | prev_ext = &hdr->ext; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
Pali Rohár | 87cc3c9 | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1907 | /* |
| 1908 | * ARM executable code inside the BIN header on some mvebu platforms |
| 1909 | * (e.g. A370, AXP) must always be aligned with the 128-bit boundary. |
| 1910 | * This requirement can be met by inserting dummy arguments into |
| 1911 | * BIN header, if needed. |
| 1912 | */ |
| 1913 | offset = &ohdr->data[4] - (char *)img; |
| 1914 | num_args = ((16 - offset % 16) % 16) / sizeof(uint32_t); |
| 1915 | |
| 1916 | ohdrsz = sizeof(*ohdr) + 4 + 4 * num_args + binsz + 4; |
| 1917 | kwboot_img_grow_hdr(hdr, size, ohdrsz); |
| 1918 | |
Pali Rohár | 32eec19 | 2022-01-12 18:20:52 +0100 | [diff] [blame] | 1919 | *prev_ext = 1; |
Pali Rohár | 90eb900 | 2021-10-25 15:13:01 +0200 | [diff] [blame] | 1920 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1921 | ohdr->headertype = OPT_HDR_V1_BINARY_TYPE; |
| 1922 | ohdr->headersz_msb = ohdrsz >> 16; |
| 1923 | ohdr->headersz_lsb = cpu_to_le16(ohdrsz & 0xffff); |
| 1924 | |
| 1925 | memset(&ohdr->data[0], 0, ohdrsz - sizeof(*ohdr)); |
Pali Rohár | 87cc3c9 | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1926 | *(uint32_t *)&ohdr->data[0] = cpu_to_le32(num_args); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1927 | |
Pali Rohár | 87cc3c9 | 2021-10-21 16:46:06 +0200 | [diff] [blame] | 1928 | return &ohdr->data[4 + 4 * num_args]; |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 1929 | } |
| 1930 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1931 | static void |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1932 | _inject_baudrate_change_code(void *img, size_t *size, int for_data, |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1933 | int old_baud, int new_baud) |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1934 | { |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1935 | struct main_hdr_v1 *hdr = img; |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1936 | uint32_t orig_datasz; |
| 1937 | uint32_t codesz; |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1938 | uint8_t *code; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1939 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1940 | if (for_data) { |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1941 | orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t); |
| 1942 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1943 | codesz = sizeof(kwboot_baud_code) + |
| 1944 | sizeof(kwboot_baud_code_data_jump); |
| 1945 | code = kwboot_img_grow_data_right(img, size, codesz); |
| 1946 | } else { |
| 1947 | codesz = sizeof(kwboot_baud_code_binhdr_pre) + |
| 1948 | sizeof(kwboot_baud_code) + |
| 1949 | sizeof(kwboot_baud_code_binhdr_post); |
| 1950 | code = kwboot_add_bin_ohdr_v1(img, size, codesz); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1951 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1952 | codesz = sizeof(kwboot_baud_code_binhdr_pre); |
| 1953 | memcpy(code, kwboot_baud_code_binhdr_pre, codesz); |
| 1954 | code += codesz; |
| 1955 | } |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1956 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1957 | codesz = sizeof(kwboot_baud_code) - 2 * sizeof(uint32_t); |
| 1958 | memcpy(code, kwboot_baud_code, codesz); |
| 1959 | code += codesz; |
| 1960 | *(uint32_t *)code = cpu_to_le32(old_baud); |
| 1961 | code += sizeof(uint32_t); |
| 1962 | *(uint32_t *)code = cpu_to_le32(new_baud); |
| 1963 | code += sizeof(uint32_t); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1964 | |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1965 | if (for_data) { |
| 1966 | codesz = sizeof(kwboot_baud_code_data_jump) - sizeof(uint32_t); |
| 1967 | memcpy(code, kwboot_baud_code_data_jump, codesz); |
| 1968 | code += codesz; |
| 1969 | *(uint32_t *)code = hdr->execaddr; |
| 1970 | code += sizeof(uint32_t); |
| 1971 | hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + orig_datasz); |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 1972 | } else { |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 1973 | codesz = sizeof(kwboot_baud_code_binhdr_post); |
| 1974 | memcpy(code, kwboot_baud_code_binhdr_post, codesz); |
| 1975 | code += codesz; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1976 | } |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 1979 | static const char * |
| 1980 | kwboot_img_type(uint8_t blockid) |
| 1981 | { |
| 1982 | switch (blockid) { |
| 1983 | case IBR_HDR_I2C_ID: return "I2C"; |
| 1984 | case IBR_HDR_SPI_ID: return "SPI"; |
| 1985 | case IBR_HDR_NAND_ID: return "NAND"; |
| 1986 | case IBR_HDR_SATA_ID: return "SATA"; |
| 1987 | case IBR_HDR_PEX_ID: return "PEX"; |
| 1988 | case IBR_HDR_UART_ID: return "UART"; |
| 1989 | case IBR_HDR_SDIO_ID: return "SDIO"; |
| 1990 | default: return "unknown"; |
| 1991 | } |
| 1992 | } |
| 1993 | |
Pali Rohár | 5725e0c | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 1994 | static int |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 1995 | kwboot_img_patch(void *img, size_t *size, int baudrate) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 1996 | { |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 1997 | struct main_hdr_v1 *hdr; |
Pali Rohár | 2f9ce97 | 2023-01-08 13:42:07 +0100 | [diff] [blame] | 1998 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | 88255af | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 1999 | uint32_t srcaddr; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2000 | uint8_t csum; |
Marek Behún | 811821e | 2021-09-24 23:07:04 +0200 | [diff] [blame] | 2001 | size_t hdrsz; |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 2002 | int image_ver; |
Pali Rohár | 5725e0c | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 2003 | int is_secure; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2004 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2005 | hdr = img; |
| 2006 | |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2007 | if (*size < sizeof(struct main_hdr_v1)) { |
| 2008 | fprintf(stderr, "Invalid image header size\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2009 | goto err; |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2010 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2011 | |
Marek Behún | fa9caec | 2021-09-24 23:07:00 +0200 | [diff] [blame] | 2012 | image_ver = kwbimage_version(img); |
Pali Rohár | b572ac4 | 2021-07-23 11:14:22 +0200 | [diff] [blame] | 2013 | if (image_ver != 0 && image_ver != 1) { |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 2014 | fprintf(stderr, "Invalid image header version\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2015 | goto err; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Marek Behún | d1b0b03 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 2018 | hdrsz = kwbheader_size(hdr); |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 2019 | |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2020 | if (*size < hdrsz) { |
| 2021 | fprintf(stderr, "Invalid image header size\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2022 | goto err; |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | kwboot_printv("Detected kwbimage v%d with %s boot signature\n", image_ver, kwboot_img_type(hdr->blockid)); |
Pali Rohár | 1228116 | 2021-07-23 11:14:21 +0200 | [diff] [blame] | 2026 | |
Marek Behún | d1b0b03 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 2027 | csum = kwboot_hdr_csum8(hdr) - hdr->checksum; |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2028 | if (csum != hdr->checksum) { |
| 2029 | fprintf(stderr, "Image has invalid header checksum stored in image header\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2030 | goto err; |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2031 | } |
Stefan Roese | c74165d | 2015-09-29 09:19:59 +0200 | [diff] [blame] | 2032 | |
Pali Rohár | 88255af | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 2033 | srcaddr = le32_to_cpu(hdr->srcaddr); |
| 2034 | |
| 2035 | switch (hdr->blockid) { |
| 2036 | case IBR_HDR_SATA_ID: |
Pali Rohár | 091e692 | 2023-01-21 13:45:36 +0100 | [diff] [blame] | 2037 | hdr->srcaddr = cpu_to_le32(srcaddr * 512); |
Pali Rohár | 88255af | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 2038 | break; |
| 2039 | |
Pali Rohár | 88255af | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 2040 | case IBR_HDR_PEX_ID: |
| 2041 | if (srcaddr == 0xFFFFFFFF) |
| 2042 | hdr->srcaddr = cpu_to_le32(hdrsz); |
| 2043 | break; |
Pali Rohár | 398d415 | 2021-09-24 23:06:59 +0200 | [diff] [blame] | 2044 | |
| 2045 | case IBR_HDR_SPI_ID: |
| 2046 | if (hdr->destaddr == cpu_to_le32(0xFFFFFFFF)) { |
| 2047 | kwboot_printv("Patching destination and execution addresses from SPI/NOR XIP area to DDR area 0x00800000\n"); |
Pali Rohár | e2ca67e | 2023-01-21 12:59:20 +0100 | [diff] [blame] | 2048 | hdr->destaddr = cpu_to_le32(0x00800000 + le32_to_cpu(hdr->srcaddr)); |
| 2049 | hdr->execaddr = cpu_to_le32(0x00800000 + le32_to_cpu(hdr->execaddr)); |
Pali Rohár | 398d415 | 2021-09-24 23:06:59 +0200 | [diff] [blame] | 2050 | } |
| 2051 | break; |
Pali Rohár | 88255af | 2021-09-24 23:06:58 +0200 | [diff] [blame] | 2052 | } |
| 2053 | |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2054 | if (hdrsz > le32_to_cpu(hdr->srcaddr)) { |
| 2055 | fprintf(stderr, "Image has invalid data offset stored in image header\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2056 | goto err; |
Pali Rohár | b7021d6 | 2023-01-10 22:33:56 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | if (*size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) { |
| 2060 | fprintf(stderr, "Image has invalid data size stored in image header\n"); |
| 2061 | goto err; |
| 2062 | } |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2063 | |
Pali Rohár | 2f9ce97 | 2023-01-08 13:42:07 +0100 | [diff] [blame] | 2064 | for_each_opt_hdr_v1 (ohdr, hdr) { |
| 2065 | if (!opt_hdr_v1_valid_size(ohdr, (const uint8_t *)hdr + hdrsz)) { |
| 2066 | fprintf(stderr, "Invalid optional image header\n"); |
| 2067 | goto err; |
| 2068 | } |
| 2069 | } |
| 2070 | |
Pali Rohár | e086066 | 2023-01-08 13:38:27 +0100 | [diff] [blame] | 2071 | /* |
| 2072 | * The 32-bit data checksum is optional for UART image. If it is not |
| 2073 | * present (checksum detected as invalid) then grow data part of the |
| 2074 | * image for the checksum, so it can be inserted there. |
| 2075 | */ |
| 2076 | if (kwboot_img_csum32(img) != *kwboot_img_csum32_ptr(img)) { |
| 2077 | if (hdr->blockid != IBR_HDR_UART_ID) { |
| 2078 | fprintf(stderr, "Image has invalid data checksum\n"); |
| 2079 | goto err; |
| 2080 | } |
| 2081 | kwboot_img_grow_data_right(img, size, sizeof(uint32_t)); |
Pali Rohár | f539634 | 2023-03-23 20:57:53 +0100 | [diff] [blame^] | 2082 | /* Update the 32-bit data checksum */ |
| 2083 | *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img); |
Pali Rohár | e086066 | 2023-01-08 13:38:27 +0100 | [diff] [blame] | 2084 | } |
Pali Rohár | 0b659e5 | 2021-10-25 15:12:55 +0200 | [diff] [blame] | 2085 | |
Pali Rohár | 08022e2 | 2023-01-08 13:46:14 +0100 | [diff] [blame] | 2086 | if (!kwboot_img_has_ddr_init(img) && |
| 2087 | (le32_to_cpu(hdr->destaddr) < 0x40000000 || |
| 2088 | le32_to_cpu(hdr->destaddr) + le32_to_cpu(hdr->blocksize) > 0x40034000)) { |
| 2089 | fprintf(stderr, "Image does not contain DDR init code needed for UART booting\n"); |
| 2090 | goto err; |
| 2091 | } |
| 2092 | |
Pali Rohár | 5725e0c | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 2093 | is_secure = kwboot_img_is_secure(img); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2094 | |
Pali Rohár | 5725e0c | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 2095 | if (hdr->blockid != IBR_HDR_UART_ID) { |
| 2096 | if (is_secure) { |
| 2097 | fprintf(stderr, |
| 2098 | "Image has secure header with signature for non-UART booting\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2099 | goto err; |
Pali Rohár | 5725e0c | 2021-09-24 23:06:57 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | kwboot_printv("Patching image boot signature to UART\n"); |
| 2103 | hdr->blockid = IBR_HDR_UART_ID; |
| 2104 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2105 | |
Pali Rohár | 3024ebd | 2021-10-22 12:37:47 +0200 | [diff] [blame] | 2106 | if (!is_secure) { |
Pali Rohár | 71db362 | 2021-10-25 15:12:58 +0200 | [diff] [blame] | 2107 | if (image_ver == 1) { |
| 2108 | /* |
| 2109 | * Tell BootROM to send BootROM messages to UART port |
| 2110 | * number 0 (used also for UART booting) with default |
| 2111 | * baudrate (which should be 115200) and do not touch |
| 2112 | * UART MPP configuration. |
| 2113 | */ |
Pali Rohár | e4b8fca | 2022-01-25 18:13:13 +0100 | [diff] [blame] | 2114 | hdr->flags |= 0x1; |
Pali Rohár | 71db362 | 2021-10-25 15:12:58 +0200 | [diff] [blame] | 2115 | hdr->options &= ~0x1F; |
| 2116 | hdr->options |= MAIN_HDR_V1_OPT_BAUD_DEFAULT; |
| 2117 | hdr->options |= 0 << 3; |
| 2118 | } |
Pali Rohár | 3024ebd | 2021-10-22 12:37:47 +0200 | [diff] [blame] | 2119 | if (image_ver == 0) |
| 2120 | ((struct main_hdr_v0 *)img)->nandeccmode = IBR_HDR_ECC_DISABLED; |
| 2121 | hdr->nandpagesize = 0; |
| 2122 | } |
| 2123 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2124 | if (baudrate) { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2125 | if (image_ver == 0) { |
| 2126 | fprintf(stderr, |
| 2127 | "Cannot inject code for changing baudrate into v0 image header\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2128 | goto err; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | if (is_secure) { |
| 2132 | fprintf(stderr, |
| 2133 | "Cannot inject code for changing baudrate into image with secure header\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2134 | goto err; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | /* |
| 2138 | * First inject code that changes the baudrate from the default |
| 2139 | * value of 115200 Bd to requested value. This code is inserted |
| 2140 | * as a new opt hdr, so it is executed by BootROM after the |
| 2141 | * header part is received. |
| 2142 | */ |
| 2143 | kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n", |
| 2144 | baudrate); |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 2145 | _inject_baudrate_change_code(img, size, 0, 115200, baudrate); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2146 | |
| 2147 | /* |
| 2148 | * Now inject code that changes the baudrate back to 115200 Bd. |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 2149 | * This code is appended after the data part of the image, and |
| 2150 | * execaddr is changed so that it is executed before U-Boot |
| 2151 | * proper. |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2152 | */ |
| 2153 | kwboot_printv("Injecting code for changing baudrate back\n"); |
Pali Rohár | aed39f2 | 2021-10-25 15:12:56 +0200 | [diff] [blame] | 2154 | _inject_baudrate_change_code(img, size, 1, baudrate, 115200); |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2155 | |
Pali Rohár | f625a22 | 2021-10-25 15:12:57 +0200 | [diff] [blame] | 2156 | /* Update the 32-bit data checksum */ |
| 2157 | *kwboot_img_csum32_ptr(img) = kwboot_img_csum32(img); |
| 2158 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2159 | /* recompute header size */ |
| 2160 | hdrsz = kwbheader_size(hdr); |
| 2161 | } |
| 2162 | |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2163 | if (hdrsz % KWBOOT_XM_BLKSZ) { |
Pali Rohár | 085d9ce | 2021-10-25 15:13:00 +0200 | [diff] [blame] | 2164 | size_t grow = KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ; |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2165 | |
| 2166 | if (is_secure) { |
| 2167 | fprintf(stderr, "Cannot align image with secure header\n"); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2168 | goto err; |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | kwboot_printv("Aligning image header to Xmodem block size\n"); |
Pali Rohár | 085d9ce | 2021-10-25 15:13:00 +0200 | [diff] [blame] | 2172 | kwboot_img_grow_hdr(img, size, grow); |
Pali Rohár | bf5151b | 2023-03-23 20:57:52 +0100 | [diff] [blame] | 2173 | hdrsz += grow; |
| 2174 | |
| 2175 | /* |
| 2176 | * kwbimage v1 contains header size field and for UART type it |
| 2177 | * must be set to the aligned xmodem header size because BootROM |
| 2178 | * rounds header size down to xmodem block size. |
| 2179 | */ |
| 2180 | if (kwbimage_version(img) == 1) { |
| 2181 | hdr->headersz_msb = hdrsz >> 16; |
| 2182 | hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff); |
| 2183 | } |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Marek Behún | d1b0b03 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 2186 | hdr->checksum = kwboot_hdr_csum8(hdr) - csum; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2187 | |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2188 | *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize); |
Marek Behún | 3e6117e | 2021-09-24 23:07:12 +0200 | [diff] [blame] | 2189 | return 0; |
| 2190 | err: |
| 2191 | errno = EINVAL; |
| 2192 | return -1; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | static void |
| 2196 | kwboot_usage(FILE *stream, char *progname) |
| 2197 | { |
| 2198 | fprintf(stream, |
Pali Rohár | 8564cbe | 2022-03-02 11:49:24 +0100 | [diff] [blame] | 2199 | "Usage: %s [OPTIONS] [-b <image> | -D <image> | -b | -d ] [-B <baud> ] [-t] <TTY>\n", |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2200 | progname); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2201 | fprintf(stream, "\n"); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2202 | fprintf(stream, |
Pali Rohár | f4e9e87 | 2022-03-02 11:49:26 +0100 | [diff] [blame] | 2203 | " -b <image>: boot <image> with preamble (Kirkwood, Avanta, Armada 370/XP/375/38x/39x)\n"); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2204 | fprintf(stream, |
| 2205 | " -D <image>: boot <image> without preamble (Dove)\n"); |
Pali Rohár | 8564cbe | 2022-03-02 11:49:24 +0100 | [diff] [blame] | 2206 | fprintf(stream, " -b: enter xmodem boot mode\n"); |
| 2207 | fprintf(stream, " -d: enter console debug mode\n"); |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2208 | fprintf(stream, " -a: use timings for Armada XP\n"); |
Stefan Roese | f7f509d | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 2209 | fprintf(stream, " -s <resp-timeo>: use specific response-timeout\n"); |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 2210 | fprintf(stream, |
| 2211 | " -o <block-timeo>: use specific xmodem block timeout\n"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2212 | fprintf(stream, "\n"); |
| 2213 | fprintf(stream, " -t: mini terminal\n"); |
| 2214 | fprintf(stream, "\n"); |
| 2215 | fprintf(stream, " -B <baud>: set baud rate\n"); |
| 2216 | fprintf(stream, "\n"); |
| 2217 | } |
| 2218 | |
| 2219 | int |
| 2220 | main(int argc, char **argv) |
| 2221 | { |
| 2222 | const char *ttypath, *imgpath; |
Pali Rohár | 4d27601 | 2021-09-24 23:06:55 +0200 | [diff] [blame] | 2223 | int rv, rc, tty, term; |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2224 | int bootmsg; |
| 2225 | int debugmsg; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2226 | void *img; |
| 2227 | size_t size; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2228 | size_t after_img_rsv; |
| 2229 | int baudrate; |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2230 | int prev_optind; |
| 2231 | int c; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2232 | |
| 2233 | rv = 1; |
| 2234 | tty = -1; |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2235 | bootmsg = 0; |
| 2236 | debugmsg = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2237 | imgpath = NULL; |
| 2238 | img = NULL; |
| 2239 | term = 0; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2240 | size = 0; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2241 | after_img_rsv = KWBOOT_XM_BLKSZ; |
| 2242 | baudrate = 115200; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2243 | |
Pali Rohár | 73ae7aa | 2021-11-05 23:30:42 +0100 | [diff] [blame] | 2244 | printf("kwboot version %s\n", PLAIN_VERSION); |
| 2245 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2246 | kwboot_verbose = isatty(STDOUT_FILENO); |
| 2247 | |
| 2248 | do { |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2249 | prev_optind = optind; |
| 2250 | c = getopt(argc, argv, "hbptaB:dD:q:s:o:"); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2251 | if (c < 0) |
| 2252 | break; |
| 2253 | |
| 2254 | switch (c) { |
| 2255 | case 'b': |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2256 | if (imgpath || bootmsg || debugmsg) |
| 2257 | goto usage; |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2258 | bootmsg = 1; |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2259 | if (prev_optind == optind) |
| 2260 | goto usage; |
Pali Rohár | fefee1a | 2022-03-07 19:03:09 +0100 | [diff] [blame] | 2261 | /* Option -b could have optional argument which specify image path */ |
| 2262 | if (optind < argc && argv[optind] && argv[optind][0] != '-') |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2263 | imgpath = argv[optind++]; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2264 | break; |
| 2265 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2266 | case 'D': |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2267 | if (imgpath || bootmsg || debugmsg) |
| 2268 | goto usage; |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2269 | bootmsg = 0; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2270 | imgpath = optarg; |
| 2271 | break; |
| 2272 | |
| 2273 | case 'd': |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2274 | if (imgpath || bootmsg || debugmsg) |
| 2275 | goto usage; |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2276 | debugmsg = 1; |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2277 | break; |
| 2278 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2279 | case 'p': |
Pali Rohár | 4d27601 | 2021-09-24 23:06:55 +0200 | [diff] [blame] | 2280 | /* nop, for backward compatibility */ |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2281 | break; |
| 2282 | |
| 2283 | case 't': |
| 2284 | term = 1; |
| 2285 | break; |
| 2286 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2287 | case 'a': |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2288 | msg_rsp_timeo = KWBOOT_MSG_RSP_TIMEO_AXP; |
| 2289 | break; |
| 2290 | |
Stefan Roese | f7f509d | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 2291 | case 'q': |
Pali Rohár | d51b834 | 2022-03-02 11:49:19 +0100 | [diff] [blame] | 2292 | /* nop, for backward compatibility */ |
Stefan Roese | f7f509d | 2015-05-29 13:25:04 +0200 | [diff] [blame] | 2293 | break; |
| 2294 | |
| 2295 | case 's': |
| 2296 | msg_rsp_timeo = atoi(optarg); |
| 2297 | break; |
| 2298 | |
Kevin Smith | 4d31a84 | 2016-02-16 21:28:19 +0000 | [diff] [blame] | 2299 | case 'o': |
| 2300 | blk_rsp_timeo = atoi(optarg); |
| 2301 | break; |
| 2302 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2303 | case 'B': |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2304 | baudrate = atoi(optarg); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | |
| 2307 | case 'h': |
| 2308 | rv = 0; |
| 2309 | default: |
| 2310 | goto usage; |
| 2311 | } |
| 2312 | } while (1); |
| 2313 | |
Pali Rohár | 577506c | 2022-01-25 18:13:12 +0100 | [diff] [blame] | 2314 | if (!bootmsg && !term && !debugmsg && !imgpath) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2315 | goto usage; |
| 2316 | |
Pali Rohár | fefee1a | 2022-03-07 19:03:09 +0100 | [diff] [blame] | 2317 | /* |
| 2318 | * If there is no remaining argument but optional imgpath was parsed |
| 2319 | * then it means that optional imgpath was eaten by getopt parser. |
| 2320 | * Reassing imgpath to required ttypath argument. |
| 2321 | */ |
| 2322 | if (optind == argc && imgpath) { |
| 2323 | ttypath = imgpath; |
| 2324 | imgpath = NULL; |
| 2325 | } else if (optind + 1 == argc) { |
| 2326 | ttypath = argv[optind]; |
| 2327 | } else { |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2328 | goto usage; |
Pali Rohár | fefee1a | 2022-03-07 19:03:09 +0100 | [diff] [blame] | 2329 | } |
Pali Rohár | 4895336 | 2022-01-25 18:13:07 +0100 | [diff] [blame] | 2330 | |
Pali Rohár | 6c05cfa | 2022-03-07 19:03:07 +0100 | [diff] [blame] | 2331 | /* boot and debug message use baudrate 115200 */ |
| 2332 | if (((bootmsg && !imgpath) || debugmsg) && baudrate != 115200) { |
| 2333 | fprintf(stderr, "Baudrate other than 115200 cannot be used for this operation.\n"); |
| 2334 | goto usage; |
| 2335 | } |
| 2336 | |
Pali Rohár | fab8748 | 2022-03-07 19:03:08 +0100 | [diff] [blame] | 2337 | tty = kwboot_open_tty(ttypath, baudrate); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2338 | if (tty < 0) { |
| 2339 | perror(ttypath); |
| 2340 | goto out; |
| 2341 | } |
| 2342 | |
Pali Rohár | fab8748 | 2022-03-07 19:03:08 +0100 | [diff] [blame] | 2343 | /* |
| 2344 | * initial baudrate for image transfer is always 115200, |
| 2345 | * the change to different baudrate is done only after the header is sent |
| 2346 | */ |
| 2347 | if (imgpath && baudrate != 115200) { |
| 2348 | rc = kwboot_tty_change_baudrate(tty, 115200); |
| 2349 | if (rc) { |
| 2350 | perror(ttypath); |
| 2351 | goto out; |
| 2352 | } |
| 2353 | } |
| 2354 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2355 | if (baudrate == 115200) |
| 2356 | /* do not change baudrate during Xmodem to the same value */ |
| 2357 | baudrate = 0; |
| 2358 | else |
| 2359 | /* ensure we have enough space for baudrate change code */ |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 2360 | after_img_rsv += sizeof(struct opt_hdr_v1) + 8 + 16 + |
| 2361 | sizeof(kwboot_baud_code_binhdr_pre) + |
| 2362 | sizeof(kwboot_baud_code) + |
| 2363 | sizeof(kwboot_baud_code_binhdr_post) + |
Pali Rohár | d877439 | 2021-10-25 15:12:54 +0200 | [diff] [blame] | 2364 | KWBOOT_XM_BLKSZ + |
Pali Rohár | d877439 | 2021-10-25 15:12:54 +0200 | [diff] [blame] | 2365 | sizeof(kwboot_baud_code) + |
Pali Rohár | 6303a23 | 2021-10-27 20:57:02 +0200 | [diff] [blame] | 2366 | sizeof(kwboot_baud_code_data_jump) + |
Pali Rohár | e086066 | 2023-01-08 13:38:27 +0100 | [diff] [blame] | 2367 | sizeof(uint32_t) + |
Pali Rohár | d877439 | 2021-10-25 15:12:54 +0200 | [diff] [blame] | 2368 | KWBOOT_XM_BLKSZ; |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2369 | |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2370 | if (imgpath) { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2371 | img = kwboot_read_image(imgpath, &size, after_img_rsv); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2372 | if (!img) { |
| 2373 | perror(imgpath); |
| 2374 | goto out; |
| 2375 | } |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2376 | |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2377 | rc = kwboot_img_patch(img, &size, baudrate); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2378 | if (rc) { |
| 2379 | fprintf(stderr, "%s: Invalid image.\n", imgpath); |
| 2380 | goto out; |
| 2381 | } |
| 2382 | } |
| 2383 | |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2384 | if (debugmsg) { |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2385 | rc = kwboot_debugmsg(tty); |
Pali Rohár | 2af673b | 2022-03-02 11:49:22 +0100 | [diff] [blame] | 2386 | if (rc) |
Stefan Roese | bb5c428 | 2014-10-22 12:13:21 +0200 | [diff] [blame] | 2387 | goto out; |
Willy Tarreau | e8f8a7c | 2018-07-03 12:10:30 -0400 | [diff] [blame] | 2388 | } else if (bootmsg) { |
Pali Rohár | 224110d | 2022-03-02 11:49:20 +0100 | [diff] [blame] | 2389 | rc = kwboot_bootmsg(tty); |
Pali Rohár | 3a2e9bc | 2022-03-02 11:49:21 +0100 | [diff] [blame] | 2390 | if (rc) |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2391 | goto out; |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | if (img) { |
Pali Rohár | 2a8b769 | 2021-09-24 23:07:05 +0200 | [diff] [blame] | 2395 | rc = kwboot_xmodem(tty, img, size, baudrate); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2396 | if (rc) { |
| 2397 | perror("xmodem"); |
| 2398 | goto out; |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | if (term) { |
| 2403 | rc = kwboot_terminal(tty); |
| 2404 | if (rc && !(errno == EINTR)) { |
| 2405 | perror("terminal"); |
| 2406 | goto out; |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | rv = 0; |
| 2411 | out: |
| 2412 | if (tty >= 0) |
| 2413 | close(tty); |
| 2414 | |
| 2415 | if (img) |
Pali Rohár | d9355bf | 2021-09-24 23:07:03 +0200 | [diff] [blame] | 2416 | free(img); |
Luka Perkov | dff28964 | 2012-05-27 11:44:51 +0000 | [diff] [blame] | 2417 | |
| 2418 | return rv; |
| 2419 | |
| 2420 | usage: |
| 2421 | kwboot_usage(rv ? stderr : stdout, basename(argv[0])); |
| 2422 | goto out; |
| 2423 | } |