1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2004
7 * Texas Instruments, <www.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);
48 info->image_read += res;
51 if (info->image_read > offset) {
52 res = info->image_read - offset;
53 memcpy(addr, &buf[BUF_SIZE - res], res);
57 while (info->image_read < offset + size) {
58 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
62 memcpy(addr, buf, res);
63 info->image_read += res;
70 static int spl_ymodem_load_image(struct spl_image_info *spl_image,
71 struct spl_boot_device *bootdev)
77 connection_info_t info;
79 struct image_header *ih = NULL;
82 info.mode = xyzModem_ymodem;
83 ret = xyzModem_stream_open(&info, &err);
85 printf("spl: ymodem err - %s\n", xyzModem_error(err));
89 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
93 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
94 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
95 addr = CONFIG_SYS_LOAD_ADDR;
96 ih = (struct image_header *)addr;
98 memcpy((void *)addr, buf, res);
102 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
103 memcpy((void *)addr, buf, res);
108 ret = spl_parse_image_header(spl_image, ih);
111 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
112 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
113 struct spl_load_info load;
114 struct ymodem_fit_info info;
116 debug("Found FIT\n");
118 load.priv = (void *)&info;
119 load.filename = NULL;
122 info.image_read = BUF_SIZE;
123 load.read = ymodem_read_fit;
124 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
125 size = info.image_read;
127 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
130 ih = (struct image_header *)buf;
131 ret = spl_parse_image_header(spl_image, ih);
134 #ifdef CONFIG_SPL_GZIP
135 if (ih->ih_comp == IH_COMP_GZIP)
136 addr = CONFIG_SYS_LOAD_ADDR;
139 addr = spl_image->load_addr;
140 memcpy((void *)addr, buf, res);
141 ih = (struct image_header *)addr;
145 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
146 memcpy((void *)addr, buf, res);
153 xyzModem_stream_close(&err);
154 xyzModem_stream_terminate(false, &getcymodem);
156 printf("Loaded %lu bytes\n", size);
158 #ifdef CONFIG_SPL_GZIP
159 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
160 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
161 (ih->ih_comp == IH_COMP_GZIP)) {
162 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
163 CONFIG_SYS_BOOTM_LEN,
164 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
166 puts("Uncompressing error\n");
174 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);