Merge branch '2019-10-11-master-imports'
[oweals/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 <spl.h>
14 #include <xyzModem.h>
15 #include <asm/u-boot.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
31 static int getcymodem(void) {
32         if (tstc())
33                 return (getc());
34         return -1;
35 }
36
37 static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
38                              ulong size, void *addr)
39 {
40         int res, err;
41         struct ymodem_fit_info *info = load->priv;
42         char *buf = info->buf;
43
44         while (info->image_read < offset) {
45                 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
46                 if (res <= 0)
47                         break;
48
49                 info->image_read += res;
50         }
51
52         if (info->image_read > offset) {
53                 res = info->image_read - offset;
54                 memcpy(addr, &buf[BUF_SIZE - res], res);
55                 addr = addr + res;
56         }
57
58         while (info->image_read < offset + size) {
59                 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
60                 if (res <= 0)
61                         break;
62
63                 memcpy(addr, buf, res);
64                 info->image_read += res;
65                 addr += res;
66         }
67
68         return size;
69 }
70
71 int spl_ymodem_load_image(struct spl_image_info *spl_image,
72                           struct spl_boot_device *bootdev)
73 {
74         ulong size = 0;
75         int err;
76         int res;
77         int ret;
78         connection_info_t info;
79         char buf[BUF_SIZE];
80         struct image_header *ih = NULL;
81         ulong addr = 0;
82
83         info.mode = xyzModem_ymodem;
84         ret = xyzModem_stream_open(&info, &err);
85         if (ret) {
86                 printf("spl: ymodem err - %s\n", xyzModem_error(err));
87                 return ret;
88         }
89
90         res = xyzModem_stream_read(buf, BUF_SIZE, &err);
91         if (res <= 0)
92                 goto end_stream;
93
94         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
95             image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
96                 addr = CONFIG_SYS_LOAD_ADDR;
97                 ih = (struct image_header *)addr;
98
99                 memcpy((void *)addr, buf, res);
100                 size += res;
101                 addr += res;
102
103                 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
104                         memcpy((void *)addr, buf, res);
105                         size += res;
106                         addr += res;
107                 }
108
109                 ret = spl_parse_image_header(spl_image, ih);
110                 if (ret)
111                         return ret;
112         } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
113             image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
114                 struct spl_load_info load;
115                 struct ymodem_fit_info info;
116
117                 debug("Found FIT\n");
118                 load.dev = NULL;
119                 load.priv = (void *)&info;
120                 load.filename = NULL;
121                 load.bl_len = 1;
122                 info.buf = buf;
123                 info.image_read = BUF_SIZE;
124                 load.read = ymodem_read_fit;
125                 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
126                 size = info.image_read;
127
128                 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
129                         size += res;
130         } else {
131                 ih = (struct image_header *)buf;
132                 ret = spl_parse_image_header(spl_image, ih);
133                 if (ret)
134                         goto end_stream;
135 #ifdef CONFIG_SPL_GZIP
136                 if (ih->ih_comp == IH_COMP_GZIP)
137                         addr = CONFIG_SYS_LOAD_ADDR;
138                 else
139 #endif
140                         addr = spl_image->load_addr;
141                 memcpy((void *)addr, buf, res);
142                 ih = (struct image_header *)addr;
143                 size += res;
144                 addr += res;
145
146                 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
147                         memcpy((void *)addr, buf, res);
148                         size += res;
149                         addr += res;
150                 }
151         }
152
153 end_stream:
154         xyzModem_stream_close(&err);
155         xyzModem_stream_terminate(false, &getcymodem);
156
157         printf("Loaded %lu bytes\n", size);
158
159 #ifdef CONFIG_SPL_GZIP
160         if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
161               image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
162             (ih->ih_comp == IH_COMP_GZIP)) {
163                 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
164                            CONFIG_SYS_BOOTM_LEN,
165                            (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
166                            &size)) {
167                         puts("Uncompressing error\n");
168                         return -EIO;
169                 }
170         }
171 #endif
172
173         return ret;
174 }
175 SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);