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