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; |
| 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; |
| 60 | memcpy(addr, &buf[buf_offset - res], res); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 61 | addr = addr + res; |
| 62 | } |
| 63 | |
| 64 | while (info->image_read < offset + size) { |
| 65 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 66 | if (res <= 0) |
Andreas Dannenberg | e58d036 | 2019-08-15 15:55:26 -0500 | [diff] [blame] | 67 | break; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 68 | |
| 69 | memcpy(addr, buf, res); |
| 70 | info->image_read += res; |
| 71 | addr += res; |
| 72 | } |
| 73 | |
| 74 | return size; |
| 75 | } |
| 76 | |
Andreas Dannenberg | 5889208 | 2019-08-15 15:55:27 -0500 | [diff] [blame] | 77 | int spl_ymodem_load_image(struct spl_image_info *spl_image, |
| 78 | struct spl_boot_device *bootdev) |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 79 | { |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 80 | ulong size = 0; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 81 | int err; |
| 82 | int res; |
| 83 | int ret; |
| 84 | connection_info_t info; |
| 85 | char buf[BUF_SIZE]; |
Marek Vasut | f0c7fdc | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 86 | struct image_header *ih = NULL; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 87 | ulong addr = 0; |
| 88 | |
| 89 | info.mode = xyzModem_ymodem; |
| 90 | ret = xyzModem_stream_open(&info, &err); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 91 | if (ret) { |
| 92 | printf("spl: ymodem err - %s\n", xyzModem_error(err)); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 97 | if (res <= 0) |
| 98 | goto end_stream; |
| 99 | |
Marek Vasut | 3114a95 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 100 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
| 101 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) { |
| 102 | addr = CONFIG_SYS_LOAD_ADDR; |
| 103 | ih = (struct image_header *)addr; |
| 104 | |
| 105 | memcpy((void *)addr, buf, res); |
| 106 | size += res; |
| 107 | addr += res; |
| 108 | |
| 109 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 110 | memcpy((void *)addr, buf, res); |
| 111 | size += res; |
| 112 | addr += res; |
| 113 | } |
| 114 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame^] | 115 | ret = spl_parse_image_header(spl_image, bootdev, ih); |
Marek Vasut | 3114a95 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 116 | if (ret) |
| 117 | return ret; |
| 118 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 119 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) { |
| 120 | struct spl_load_info load; |
| 121 | struct ymodem_fit_info info; |
| 122 | |
| 123 | debug("Found FIT\n"); |
| 124 | load.dev = NULL; |
| 125 | load.priv = (void *)&info; |
| 126 | load.filename = NULL; |
| 127 | load.bl_len = 1; |
| 128 | info.buf = buf; |
| 129 | info.image_read = BUF_SIZE; |
| 130 | load.read = ymodem_read_fit; |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 131 | ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 132 | size = info.image_read; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 133 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 134 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 135 | size += res; |
| 136 | } else { |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 137 | ih = (struct image_header *)buf; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame^] | 138 | ret = spl_parse_image_header(spl_image, bootdev, ih); |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 139 | if (ret) |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 140 | goto end_stream; |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 141 | #ifdef CONFIG_SPL_GZIP |
| 142 | if (ih->ih_comp == IH_COMP_GZIP) |
| 143 | addr = CONFIG_SYS_LOAD_ADDR; |
| 144 | else |
| 145 | #endif |
| 146 | addr = spl_image->load_addr; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 147 | memcpy((void *)addr, buf, res); |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 148 | ih = (struct image_header *)addr; |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 149 | size += res; |
| 150 | addr += res; |
| 151 | |
| 152 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 153 | memcpy((void *)addr, buf, res); |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 154 | size += res; |
| 155 | addr += res; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 156 | } |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Lokesh Vutla | 37d2632 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 159 | end_stream: |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 160 | xyzModem_stream_close(&err); |
| 161 | xyzModem_stream_terminate(false, &getcymodem); |
| 162 | |
Marek Vasut | d963707 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 163 | printf("Loaded %lu bytes\n", size); |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 164 | |
Marek Vasut | f0c7fdc | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 165 | #ifdef CONFIG_SPL_GZIP |
| 166 | if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 167 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) && |
| 168 | (ih->ih_comp == IH_COMP_GZIP)) { |
| 169 | if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)), |
| 170 | CONFIG_SYS_BOOTM_LEN, |
| 171 | (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)), |
| 172 | &size)) { |
| 173 | puts("Uncompressing error\n"); |
| 174 | return -EIO; |
| 175 | } |
| 176 | } |
| 177 | #endif |
| 178 | |
Marek Vasut | 09e84ec | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 179 | return ret; |
Matt Porter | 79bfa73 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 180 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 181 | SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image); |