Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2011 |
| 7 | * Texas Instruments, <www.ti.com> |
| 8 | * |
| 9 | * Matt Porter <mporter@ti.com> |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 10 | */ |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | 1a974af | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 12 | #include <gzip.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 13 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 15 | #include <spl.h> |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 16 | #include <xyzModem.h> |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 17 | #include <asm/u-boot.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 18 | #include <linux/libfdt.h> |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 19 | |
| 20 | #define BUF_SIZE 1024 |
| 21 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 22 | /* |
| 23 | * Information required to load image using ymodem. |
| 24 | * |
| 25 | * @image_read: Now of bytes read from the image. |
| 26 | * @buf: pointer to the previous read block. |
| 27 | */ |
| 28 | struct ymodem_fit_info { |
| 29 | int image_read; |
| 30 | char *buf; |
| 31 | }; |
| 32 | |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 33 | static int getcymodem(void) { |
| 34 | if (tstc()) |
Heinrich Schuchardt | c4954fb | 2020-10-07 18:11:48 +0200 | [diff] [blame] | 35 | return (getchar()); |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 36 | return -1; |
| 37 | } |
| 38 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 39 | static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset, |
| 40 | ulong size, void *addr) |
| 41 | { |
Lokesh Vutla | 7ac012a | 2019-11-14 18:33:30 +0530 | [diff] [blame] | 42 | int res, err, buf_offset; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 43 | struct ymodem_fit_info *info = load->priv; |
| 44 | char *buf = info->buf; |
Vignesh Raghavendra | 247e178 | 2022-01-31 09:49:19 +0530 | [diff] [blame] | 45 | ulong copy_size = size; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 46 | |
| 47 | while (info->image_read < offset) { |
| 48 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 49 | if (res <= 0) |
Andreas Dannenberg | e58d036 | 2019-08-15 15:55:26 -0500 | [diff] [blame] | 50 | break; |
| 51 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 52 | info->image_read += res; |
| 53 | } |
| 54 | |
| 55 | if (info->image_read > offset) { |
| 56 | res = info->image_read - offset; |
Lokesh Vutla | 7ac012a | 2019-11-14 18:33:30 +0530 | [diff] [blame] | 57 | if (info->image_read % BUF_SIZE) |
| 58 | buf_offset = (info->image_read % BUF_SIZE); |
| 59 | else |
| 60 | buf_offset = BUF_SIZE; |
Vignesh Raghavendra | 247e178 | 2022-01-31 09:49:19 +0530 | [diff] [blame] | 61 | |
| 62 | if (res > copy_size) { |
| 63 | memcpy(addr, &buf[buf_offset - res], copy_size); |
| 64 | goto done; |
| 65 | } |
Lokesh Vutla | 7ac012a | 2019-11-14 18:33:30 +0530 | [diff] [blame] | 66 | memcpy(addr, &buf[buf_offset - res], res); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 67 | addr = addr + res; |
Vignesh Raghavendra | 247e178 | 2022-01-31 09:49:19 +0530 | [diff] [blame] | 68 | copy_size -= res; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | while (info->image_read < offset + size) { |
| 72 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 73 | if (res <= 0) |
Andreas Dannenberg | e58d036 | 2019-08-15 15:55:26 -0500 | [diff] [blame] | 74 | break; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 75 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 76 | info->image_read += res; |
Vignesh Raghavendra | 247e178 | 2022-01-31 09:49:19 +0530 | [diff] [blame] | 77 | if (res > copy_size) { |
| 78 | memcpy(addr, buf, copy_size); |
| 79 | goto done; |
| 80 | } |
| 81 | memcpy(addr, buf, res); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 82 | addr += res; |
Vignesh Raghavendra | 247e178 | 2022-01-31 09:49:19 +0530 | [diff] [blame] | 83 | copy_size -= res; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 84 | } |
| 85 | |
Vignesh Raghavendra | 247e178 | 2022-01-31 09:49:19 +0530 | [diff] [blame] | 86 | done: |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 87 | return size; |
| 88 | } |
| 89 | |
Andreas Dannenberg | 5889208 | 2019-08-15 15:55:27 -0500 | [diff] [blame] | 90 | int spl_ymodem_load_image(struct spl_image_info *spl_image, |
| 91 | struct spl_boot_device *bootdev) |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 92 | { |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 93 | ulong size = 0; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 94 | int err; |
| 95 | int res; |
| 96 | int ret; |
| 97 | connection_info_t info; |
| 98 | char buf[BUF_SIZE]; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 99 | struct legacy_img_hdr *ih = NULL; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 100 | ulong addr = 0; |
| 101 | |
| 102 | info.mode = xyzModem_ymodem; |
| 103 | ret = xyzModem_stream_open(&info, &err); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 104 | if (ret) { |
| 105 | printf("spl: ymodem err - %s\n", xyzModem_error(err)); |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 110 | if (res <= 0) |
| 111 | goto end_stream; |
| 112 | |
Marek Vasut | 3114a95 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 113 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 114 | image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) { |
Marek Vasut | 3114a95 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 115 | addr = CONFIG_SYS_LOAD_ADDR; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 116 | ih = (struct legacy_img_hdr *)addr; |
Marek Vasut | 3114a95 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 117 | |
| 118 | memcpy((void *)addr, buf, res); |
| 119 | size += res; |
| 120 | addr += res; |
| 121 | |
| 122 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 123 | memcpy((void *)addr, buf, res); |
| 124 | size += res; |
| 125 | addr += res; |
| 126 | } |
| 127 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 128 | ret = spl_parse_image_header(spl_image, bootdev, ih); |
Marek Vasut | 3114a95 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 129 | if (ret) |
| 130 | return ret; |
| 131 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 132 | image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) { |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 133 | struct spl_load_info load; |
| 134 | struct ymodem_fit_info info; |
| 135 | |
| 136 | debug("Found FIT\n"); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 137 | load.priv = (void *)&info; |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 138 | spl_set_bl_len(&load, 1); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 139 | info.buf = buf; |
| 140 | info.image_read = BUF_SIZE; |
| 141 | load.read = ymodem_read_fit; |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 142 | ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 143 | size = info.image_read; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 144 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 145 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 146 | size += res; |
| 147 | } else { |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 148 | ih = (struct legacy_img_hdr *)buf; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 149 | ret = spl_parse_image_header(spl_image, bootdev, ih); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 150 | if (ret) |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 151 | goto end_stream; |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 152 | #ifdef CONFIG_SPL_GZIP |
| 153 | if (ih->ih_comp == IH_COMP_GZIP) |
| 154 | addr = CONFIG_SYS_LOAD_ADDR; |
| 155 | else |
| 156 | #endif |
| 157 | addr = spl_image->load_addr; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 158 | memcpy((void *)addr, buf, res); |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 159 | ih = (struct legacy_img_hdr *)addr; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 160 | size += res; |
| 161 | addr += res; |
| 162 | |
| 163 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 164 | memcpy((void *)addr, buf, res); |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 165 | size += res; |
| 166 | addr += res; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 167 | } |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 170 | end_stream: |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 171 | xyzModem_stream_close(&err); |
| 172 | xyzModem_stream_terminate(false, &getcymodem); |
| 173 | |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 174 | printf("Loaded %lu bytes\n", size); |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 175 | |
Marek Vasut | f0c7fdc | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 176 | #ifdef CONFIG_SPL_GZIP |
| 177 | if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 178 | image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) && |
Marek Vasut | f0c7fdc | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 179 | (ih->ih_comp == IH_COMP_GZIP)) { |
| 180 | if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)), |
| 181 | CONFIG_SYS_BOOTM_LEN, |
| 182 | (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)), |
| 183 | &size)) { |
| 184 | puts("Uncompressing error\n"); |
| 185 | return -EIO; |
| 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 190 | return ret; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 191 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 192 | SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image); |