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