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)
40 int res, err, buf_offset;
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 if (info->image_read % BUF_SIZE)
55 buf_offset = (info->image_read % BUF_SIZE);
57 buf_offset = BUF_SIZE;
58 memcpy(addr, &buf[buf_offset - res], res);
62 while (info->image_read < offset + size) {
63 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
67 memcpy(addr, buf, res);
68 info->image_read += res;
75 int spl_ymodem_load_image(struct spl_image_info *spl_image,
76 struct spl_boot_device *bootdev)
82 connection_info_t info;
84 struct image_header *ih = NULL;
87 info.mode = xyzModem_ymodem;
88 ret = xyzModem_stream_open(&info, &err);
90 printf("spl: ymodem err - %s\n", xyzModem_error(err));
94 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
98 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
99 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
100 addr = CONFIG_SYS_LOAD_ADDR;
101 ih = (struct image_header *)addr;
103 memcpy((void *)addr, buf, res);
107 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
108 memcpy((void *)addr, buf, res);
113 ret = spl_parse_image_header(spl_image, ih);
116 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
117 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
118 struct spl_load_info load;
119 struct ymodem_fit_info info;
121 debug("Found FIT\n");
123 load.priv = (void *)&info;
124 load.filename = NULL;
127 info.image_read = BUF_SIZE;
128 load.read = ymodem_read_fit;
129 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
130 size = info.image_read;
132 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
135 ih = (struct image_header *)buf;
136 ret = spl_parse_image_header(spl_image, ih);
139 #ifdef CONFIG_SPL_GZIP
140 if (ih->ih_comp == IH_COMP_GZIP)
141 addr = CONFIG_SYS_LOAD_ADDR;
144 addr = spl_image->load_addr;
145 memcpy((void *)addr, buf, res);
146 ih = (struct image_header *)addr;
150 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
151 memcpy((void *)addr, buf, res);
158 xyzModem_stream_close(&err);
159 xyzModem_stream_terminate(false, &getcymodem);
161 printf("Loaded %lu bytes\n", size);
163 #ifdef CONFIG_SPL_GZIP
164 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
165 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
166 (ih->ih_comp == IH_COMP_GZIP)) {
167 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
168 CONFIG_SYS_BOOTM_LEN,
169 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
171 puts("Uncompressing error\n");
179 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);