xref: /openbmc/u-boot/common/spl/spl_ymodem.c (revision 25fde1c0)
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 <common.h>
12 #include <spl.h>
13 #include <xyzModem.h>
14 #include <asm/u-boot.h>
15 #include <linux/libfdt.h>
16 
17 #define BUF_SIZE 1024
18 
19 /*
20  * Information required to load image using ymodem.
21  *
22  * @image_read: Now of bytes read from the image.
23  * @buf: pointer to the previous read block.
24  */
25 struct ymodem_fit_info {
26 	int image_read;
27 	char *buf;
28 };
29 
30 static int getcymodem(void) {
31 	if (tstc())
32 		return (getc());
33 	return -1;
34 }
35 
36 static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
37 			     ulong size, void *addr)
38 {
39 	int res, err;
40 	struct ymodem_fit_info *info = load->priv;
41 	char *buf = info->buf;
42 
43 	while (info->image_read < offset) {
44 		res = xyzModem_stream_read(buf, BUF_SIZE, &err);
45 		if (res <= 0)
46 			break;
47 
48 		info->image_read += res;
49 	}
50 
51 	if (info->image_read > offset) {
52 		res = info->image_read - offset;
53 		memcpy(addr, &buf[BUF_SIZE - res], res);
54 		addr = addr + res;
55 	}
56 
57 	while (info->image_read < offset + size) {
58 		res = xyzModem_stream_read(buf, BUF_SIZE, &err);
59 		if (res <= 0)
60 			break;
61 
62 		memcpy(addr, buf, res);
63 		info->image_read += res;
64 		addr += res;
65 	}
66 
67 	return size;
68 }
69 
70 static int spl_ymodem_load_image(struct spl_image_info *spl_image,
71 				 struct spl_boot_device *bootdev)
72 {
73 	ulong size = 0;
74 	int err;
75 	int res;
76 	int ret;
77 	connection_info_t info;
78 	char buf[BUF_SIZE];
79 	struct image_header *ih;
80 	ulong addr = 0;
81 
82 	info.mode = xyzModem_ymodem;
83 	ret = xyzModem_stream_open(&info, &err);
84 	if (ret) {
85 		printf("spl: ymodem err - %s\n", xyzModem_error(err));
86 		return ret;
87 	}
88 
89 	res = xyzModem_stream_read(buf, BUF_SIZE, &err);
90 	if (res <= 0)
91 		goto end_stream;
92 
93 	if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
94 	    image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
95 		struct spl_load_info load;
96 		struct ymodem_fit_info info;
97 
98 		debug("Found FIT\n");
99 		load.dev = NULL;
100 		load.priv = (void *)&info;
101 		load.filename = NULL;
102 		load.bl_len = 1;
103 		info.buf = buf;
104 		info.image_read = BUF_SIZE;
105 		load.read = ymodem_read_fit;
106 		ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
107 		size = info.image_read;
108 
109 		while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
110 			size += res;
111 	} else {
112 		ih = (struct image_header *)buf;
113 		ret = spl_parse_image_header(spl_image, ih);
114 		if (ret)
115 			return ret;
116 #ifdef CONFIG_SPL_GZIP
117 		if (ih->ih_comp == IH_COMP_GZIP)
118 			addr = CONFIG_SYS_LOAD_ADDR;
119 		else
120 #endif
121 			addr = spl_image->load_addr;
122 		memcpy((void *)addr, buf, res);
123 		ih = (struct image_header *)addr;
124 		size += res;
125 		addr += res;
126 
127 		while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
128 			memcpy((void *)addr, buf, res);
129 			size += res;
130 			addr += res;
131 		}
132 
133 #ifdef CONFIG_SPL_GZIP
134 		if (ih->ih_comp == IH_COMP_GZIP) {
135 			if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
136 				   CONFIG_SYS_BOOTM_LEN,
137 				   (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
138 				   &size)) {
139 				puts("Uncompressing error\n");
140 				return -EIO;
141 			}
142 		}
143 #endif
144 	}
145 
146 end_stream:
147 	xyzModem_stream_close(&err);
148 	xyzModem_stream_terminate(false, &getcymodem);
149 
150 	printf("Loaded %lu bytes\n", size);
151 	return 0;
152 }
153 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);
154