1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Texas Instruments, <www.ti.com>
9 * Matt Porter <mporter@ti.com>
17 #include <asm/u-boot.h>
18 #include <linux/libfdt.h>
23 * Information required to load image using ymodem.
25 * @image_read: Now of bytes read from the image.
26 * @buf: pointer to the previous read block.
28 struct ymodem_fit_info {
33 static int getcymodem(void) {
39 static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
40 ulong size, void *addr)
42 int res, err, buf_offset;
43 struct ymodem_fit_info *info = load->priv;
44 char *buf = info->buf;
45 ulong copy_size = size;
47 while (info->image_read < offset) {
48 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
52 info->image_read += res;
55 if (info->image_read > offset) {
56 res = info->image_read - offset;
57 if (info->image_read % BUF_SIZE)
58 buf_offset = (info->image_read % BUF_SIZE);
60 buf_offset = BUF_SIZE;
62 if (res > copy_size) {
63 memcpy(addr, &buf[buf_offset - res], copy_size);
66 memcpy(addr, &buf[buf_offset - res], res);
71 while (info->image_read < offset + size) {
72 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
76 info->image_read += res;
77 if (res > copy_size) {
78 memcpy(addr, buf, copy_size);
81 memcpy(addr, buf, res);
90 int spl_ymodem_load_image(struct spl_image_info *spl_image,
91 struct spl_boot_device *bootdev)
97 connection_info_t info;
99 struct image_header *ih = NULL;
102 info.mode = xyzModem_ymodem;
103 ret = xyzModem_stream_open(&info, &err);
105 printf("spl: ymodem err - %s\n", xyzModem_error(err));
109 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
113 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
114 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
115 addr = CONFIG_SYS_LOAD_ADDR;
116 ih = (struct image_header *)addr;
118 memcpy((void *)addr, buf, res);
122 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
123 memcpy((void *)addr, buf, res);
128 ret = spl_parse_image_header(spl_image, bootdev, ih);
131 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
132 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
133 struct spl_load_info load;
134 struct ymodem_fit_info info;
136 debug("Found FIT\n");
138 load.priv = (void *)&info;
139 load.filename = NULL;
142 info.image_read = BUF_SIZE;
143 load.read = ymodem_read_fit;
144 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
145 size = info.image_read;
147 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
150 ih = (struct image_header *)buf;
151 ret = spl_parse_image_header(spl_image, bootdev, ih);
154 #ifdef CONFIG_SPL_GZIP
155 if (ih->ih_comp == IH_COMP_GZIP)
156 addr = CONFIG_SYS_LOAD_ADDR;
159 addr = spl_image->load_addr;
160 memcpy((void *)addr, buf, res);
161 ih = (struct image_header *)addr;
165 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
166 memcpy((void *)addr, buf, res);
173 xyzModem_stream_close(&err);
174 xyzModem_stream_terminate(false, &getcymodem);
176 printf("Loaded %lu bytes\n", size);
178 #ifdef CONFIG_SPL_GZIP
179 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
180 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
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)),
186 puts("Uncompressing error\n");
194 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);