1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2004
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2011
7  * Texas Instruments, <www.ti.com>
8  *
9  * Matt Porter <mporter@ti.com>
10  */
11 #include <gzip.h>
12 #include <image.h>
13 #include <log.h>
14 #include <spl.h>
15 #include <xyzModem.h>
16 #include <linux/libfdt.h>
17 
18 #define BUF_SIZE 1024
19 
20 /*
21  * Information required to load image using ymodem.
22  *
23  * @image_read: Now of bytes read from the image.
24  * @buf: pointer to the previous read block.
25  */
26 struct ymodem_fit_info {
27 	int image_read;
28 	char *buf;
29 };
30 
getcymodem(void)31 static int getcymodem(void) {
32 	if (tstc())
33 		return (getchar());
34 	return -1;
35 }
36 
ymodem_read_fit(struct spl_load_info * load,ulong offset,ulong size,void * addr)37 static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
38 			     ulong size, void *addr)
39 {
40 	int res, err, buf_offset;
41 	struct ymodem_fit_info *info = load->priv;
42 	char *buf = info->buf;
43 	ulong copy_size = size;
44 
45 	while (info->image_read < offset) {
46 		res = xyzModem_stream_read(buf, BUF_SIZE, &err);
47 		if (res <= 0)
48 			break;
49 
50 		info->image_read += res;
51 	}
52 
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);
57 		else
58 			buf_offset = BUF_SIZE;
59 
60 		if (res > copy_size) {
61 			memcpy(addr, &buf[buf_offset - res], copy_size);
62 			goto done;
63 		}
64 		memcpy(addr, &buf[buf_offset - res], res);
65 		addr = addr + res;
66 		copy_size -= res;
67 	}
68 
69 	while (info->image_read < offset + size) {
70 		res = xyzModem_stream_read(buf, BUF_SIZE, &err);
71 		if (res <= 0)
72 			break;
73 
74 		info->image_read += res;
75 		if (res > copy_size) {
76 			memcpy(addr, buf, copy_size);
77 			goto done;
78 		}
79 		memcpy(addr, buf, res);
80 		addr += res;
81 		copy_size -= res;
82 	}
83 
84 done:
85 	return size;
86 }
87 
spl_ymodem_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)88 int spl_ymodem_load_image(struct spl_image_info *spl_image,
89 			  struct spl_boot_device *bootdev)
90 {
91 	ulong size = 0;
92 	int err;
93 	int res;
94 	int ret;
95 	connection_info_t info;
96 	char buf[BUF_SIZE];
97 	struct legacy_img_hdr *ih = NULL;
98 	ulong addr = 0;
99 
100 	info.mode = xyzModem_ymodem;
101 	ret = xyzModem_stream_open(&info, &err);
102 	if (ret) {
103 		printf("spl: ymodem err - %s\n", xyzModem_error(err));
104 		return ret;
105 	}
106 
107 	res = xyzModem_stream_read(buf, BUF_SIZE, &err);
108 	if (res <= 0)
109 		goto end_stream;
110 
111 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
112 	    image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
113 		addr = CONFIG_SYS_LOAD_ADDR;
114 		ih = (struct legacy_img_hdr *)addr;
115 
116 		memcpy((void *)addr, buf, res);
117 		size += res;
118 		addr += res;
119 
120 		while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
121 			memcpy((void *)addr, buf, res);
122 			size += res;
123 			addr += res;
124 		}
125 
126 		ret = spl_parse_image_header(spl_image, bootdev, ih);
127 		if (ret)
128 			return ret;
129 	} else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
130 	    image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
131 		struct spl_load_info load;
132 		struct ymodem_fit_info info;
133 
134 		debug("Found FIT\n");
135 		spl_load_init(&load, ymodem_read_fit, (void *)&info, 1);
136 		info.buf = buf;
137 		info.image_read = BUF_SIZE;
138 		ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
139 		size = info.image_read;
140 
141 		while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
142 			size += res;
143 	} else {
144 		ih = (struct legacy_img_hdr *)buf;
145 		ret = spl_parse_image_header(spl_image, bootdev, ih);
146 		if (ret)
147 			goto end_stream;
148 #ifdef CONFIG_SPL_GZIP
149 		if (ih->ih_comp == IH_COMP_GZIP)
150 			addr = CONFIG_SYS_LOAD_ADDR;
151 		else
152 #endif
153 			addr = spl_image->load_addr;
154 		memcpy((void *)addr, buf, res);
155 		ih = (struct legacy_img_hdr *)addr;
156 		size += res;
157 		addr += res;
158 
159 		while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
160 			memcpy((void *)addr, buf, res);
161 			size += res;
162 			addr += res;
163 		}
164 	}
165 
166 end_stream:
167 	xyzModem_stream_close(&err);
168 	xyzModem_stream_terminate(false, &getcymodem);
169 
170 	printf("Loaded %lu bytes\n", size);
171 
172 #ifdef CONFIG_SPL_GZIP
173 	if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
174 	      image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) &&
175 	    (ih->ih_comp == IH_COMP_GZIP)) {
176 		if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
177 			   CONFIG_SYS_BOOTM_LEN,
178 			   (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
179 			   &size)) {
180 			puts("Uncompressing error\n");
181 			return -EIO;
182 		}
183 	}
184 #endif
185 
186 	return ret;
187 }
188 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);
189