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;
46 while (info->image_read < offset) {
47 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
51 info->image_read += res;
54 if (info->image_read > offset) {
55 res = info->image_read - offset;
56 if (info->image_read % BUF_SIZE)
57 buf_offset = (info->image_read % BUF_SIZE);
59 buf_offset = BUF_SIZE;
60 memcpy(addr, &buf[buf_offset - res], res);
64 while (info->image_read < offset + size) {
65 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
69 memcpy(addr, buf, res);
70 info->image_read += res;
77 int spl_ymodem_load_image(struct spl_image_info *spl_image,
78 struct spl_boot_device *bootdev)
84 connection_info_t info;
86 struct image_header *ih = NULL;
89 info.mode = xyzModem_ymodem;
90 ret = xyzModem_stream_open(&info, &err);
92 printf("spl: ymodem err - %s\n", xyzModem_error(err));
96 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
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;
105 memcpy((void *)addr, buf, res);
109 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
110 memcpy((void *)addr, buf, res);
115 ret = spl_parse_image_header(spl_image, ih);
118 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
119 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
120 struct spl_load_info load;
121 struct ymodem_fit_info info;
123 debug("Found FIT\n");
125 load.priv = (void *)&info;
126 load.filename = NULL;
129 info.image_read = BUF_SIZE;
130 load.read = ymodem_read_fit;
131 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
132 size = info.image_read;
134 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
137 ih = (struct image_header *)buf;
138 ret = spl_parse_image_header(spl_image, ih);
141 #ifdef CONFIG_SPL_GZIP
142 if (ih->ih_comp == IH_COMP_GZIP)
143 addr = CONFIG_SYS_LOAD_ADDR;
146 addr = spl_image->load_addr;
147 memcpy((void *)addr, buf, res);
148 ih = (struct image_header *)addr;
152 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
153 memcpy((void *)addr, buf, res);
160 xyzModem_stream_close(&err);
161 xyzModem_stream_terminate(false, &getcymodem);
163 printf("Loaded %lu bytes\n", size);
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)),
173 puts("Uncompressing error\n");
181 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);