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