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>
15 #include <asm/u-boot.h>
16 #include <linux/libfdt.h>
21 * Information required to load image using ymodem.
23 * @image_read: Now of bytes read from the image.
24 * @buf: pointer to the previous read block.
26 struct ymodem_fit_info {
31 static int getcymodem(void) {
37 static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
38 ulong size, void *addr)
41 struct ymodem_fit_info *info = load->priv;
42 char *buf = info->buf;
44 while (info->image_read < offset) {
45 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
49 info->image_read += res;
52 if (info->image_read > offset) {
53 res = info->image_read - offset;
54 memcpy(addr, &buf[BUF_SIZE - res], res);
58 while (info->image_read < offset + size) {
59 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
63 memcpy(addr, buf, res);
64 info->image_read += res;
71 int spl_ymodem_load_image(struct spl_image_info *spl_image,
72 struct spl_boot_device *bootdev)
78 connection_info_t info;
80 struct image_header *ih = NULL;
83 info.mode = xyzModem_ymodem;
84 ret = xyzModem_stream_open(&info, &err);
86 printf("spl: ymodem err - %s\n", xyzModem_error(err));
90 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
94 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
95 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
96 addr = CONFIG_SYS_LOAD_ADDR;
97 ih = (struct image_header *)addr;
99 memcpy((void *)addr, buf, res);
103 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
104 memcpy((void *)addr, buf, res);
109 ret = spl_parse_image_header(spl_image, ih);
112 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
113 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
114 struct spl_load_info load;
115 struct ymodem_fit_info info;
117 debug("Found FIT\n");
119 load.priv = (void *)&info;
120 load.filename = NULL;
123 info.image_read = BUF_SIZE;
124 load.read = ymodem_read_fit;
125 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
126 size = info.image_read;
128 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
131 ih = (struct image_header *)buf;
132 ret = spl_parse_image_header(spl_image, ih);
135 #ifdef CONFIG_SPL_GZIP
136 if (ih->ih_comp == IH_COMP_GZIP)
137 addr = CONFIG_SYS_LOAD_ADDR;
140 addr = spl_image->load_addr;
141 memcpy((void *)addr, buf, res);
142 ih = (struct image_header *)addr;
146 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
147 memcpy((void *)addr, buf, res);
154 xyzModem_stream_close(&err);
155 xyzModem_stream_terminate(false, &getcymodem);
157 printf("Loaded %lu bytes\n", size);
159 #ifdef CONFIG_SPL_GZIP
160 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
161 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
162 (ih->ih_comp == IH_COMP_GZIP)) {
163 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
164 CONFIG_SYS_BOOTM_LEN,
165 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
167 puts("Uncompressing error\n");
175 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);