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>
16 #include <asm/u-boot.h>
17 #include <linux/libfdt.h>
22 * Information required to load image using ymodem.
24 * @image_read: Now of bytes read from the image.
25 * @buf: pointer to the previous read block.
27 struct ymodem_fit_info {
32 static int getcymodem(void) {
38 static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
39 ulong size, void *addr)
41 int res, err, buf_offset;
42 struct ymodem_fit_info *info = load->priv;
43 char *buf = info->buf;
45 while (info->image_read < offset) {
46 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
50 info->image_read += res;
53 if (info->image_read > offset) {
54 res = info->image_read - offset;
55 if (info->image_read % BUF_SIZE)
56 buf_offset = (info->image_read % BUF_SIZE);
58 buf_offset = BUF_SIZE;
59 memcpy(addr, &buf[buf_offset - res], res);
63 while (info->image_read < offset + size) {
64 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
68 memcpy(addr, buf, res);
69 info->image_read += res;
76 int spl_ymodem_load_image(struct spl_image_info *spl_image,
77 struct spl_boot_device *bootdev)
83 connection_info_t info;
85 struct image_header *ih = NULL;
88 info.mode = xyzModem_ymodem;
89 ret = xyzModem_stream_open(&info, &err);
91 printf("spl: ymodem err - %s\n", xyzModem_error(err));
95 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
99 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
100 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
101 addr = CONFIG_SYS_LOAD_ADDR;
102 ih = (struct image_header *)addr;
104 memcpy((void *)addr, buf, res);
108 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
109 memcpy((void *)addr, buf, res);
114 ret = spl_parse_image_header(spl_image, ih);
117 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
118 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
119 struct spl_load_info load;
120 struct ymodem_fit_info info;
122 debug("Found FIT\n");
124 load.priv = (void *)&info;
125 load.filename = NULL;
128 info.image_read = BUF_SIZE;
129 load.read = ymodem_read_fit;
130 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
131 size = info.image_read;
133 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
136 ih = (struct image_header *)buf;
137 ret = spl_parse_image_header(spl_image, ih);
140 #ifdef CONFIG_SPL_GZIP
141 if (ih->ih_comp == IH_COMP_GZIP)
142 addr = CONFIG_SYS_LOAD_ADDR;
145 addr = spl_image->load_addr;
146 memcpy((void *)addr, buf, res);
147 ih = (struct image_header *)addr;
151 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
152 memcpy((void *)addr, buf, res);
159 xyzModem_stream_close(&err);
160 xyzModem_stream_terminate(false, &getcymodem);
162 printf("Loaded %lu bytes\n", size);
164 #ifdef CONFIG_SPL_GZIP
165 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
166 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
167 (ih->ih_comp == IH_COMP_GZIP)) {
168 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
169 CONFIG_SYS_BOOTM_LEN,
170 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
172 puts("Uncompressing error\n");
180 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);