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 | */ |
| 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> |
| 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"); |
| 137 | load.dev = NULL; |
| 138 | load.priv = (void *)&info; |
| 139 | load.filename = NULL; |
| 140 | load.bl_len = 1; |
| 141 | info.buf = buf; |
| 142 | info.image_read = BUF_SIZE; |
| 143 | load.read = ymodem_read_fit; |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 144 | ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 145 | size = info.image_read; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 146 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 147 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 148 | size += res; |
| 149 | } else { |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 150 | ih = (struct legacy_img_hdr *)buf; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 151 | ret = spl_parse_image_header(spl_image, bootdev, ih); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 152 | if (ret) |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 153 | goto end_stream; |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 154 | #ifdef CONFIG_SPL_GZIP |
| 155 | if (ih->ih_comp == IH_COMP_GZIP) |
| 156 | addr = CONFIG_SYS_LOAD_ADDR; |
| 157 | else |
| 158 | #endif |
| 159 | addr = spl_image->load_addr; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 160 | memcpy((void *)addr, buf, res); |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 161 | ih = (struct legacy_img_hdr *)addr; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 162 | size += res; |
| 163 | addr += res; |
| 164 | |
| 165 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 166 | memcpy((void *)addr, buf, res); |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 167 | size += res; |
| 168 | addr += res; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 169 | } |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 172 | end_stream: |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 173 | xyzModem_stream_close(&err); |
| 174 | xyzModem_stream_terminate(false, &getcymodem); |
| 175 | |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 176 | printf("Loaded %lu bytes\n", size); |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 177 | |
Marek Vasut | f0c7fdc | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 178 | #ifdef CONFIG_SPL_GZIP |
| 179 | if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 180 | image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) && |
Marek Vasut | f0c7fdc | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 181 | (ih->ih_comp == IH_COMP_GZIP)) { |
| 182 | if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)), |
| 183 | CONFIG_SYS_BOOTM_LEN, |
| 184 | (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)), |
| 185 | &size)) { |
| 186 | puts("Uncompressing error\n"); |
| 187 | return -EIO; |
| 188 | } |
| 189 | } |
| 190 | #endif |
| 191 | |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 192 | return ret; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 193 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 194 | SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image); |