blob: 20f42600625f389959e9ba9cbf86b74768a712c7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Matt Porter24de3572012-01-31 12:03:57 +00002/*
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>
Matt Porter24de3572012-01-31 12:03:57 +000010 */
11#include <common.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060012#include <gzip.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070013#include <spl.h>
Matt Porter24de3572012-01-31 12:03:57 +000014#include <xyzModem.h>
15#include <asm/u-boot.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Matt Porter24de3572012-01-31 12:03:57 +000017
18#define BUF_SIZE 1024
19
Lokesh Vutlafa715192016-05-24 10:34:44 +053020/*
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 */
26struct ymodem_fit_info {
27 int image_read;
28 char *buf;
29};
30
Matt Porter24de3572012-01-31 12:03:57 +000031static int getcymodem(void) {
32 if (tstc())
33 return (getc());
34 return -1;
35}
36
Lokesh Vutlafa715192016-05-24 10:34:44 +053037static 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 return res;
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 return res;
61
62 memcpy(addr, buf, res);
63 info->image_read += res;
64 addr += res;
65 }
66
67 return size;
68}
69
Simon Glass2a2ee2a2016-09-24 18:20:13 -060070static int spl_ymodem_load_image(struct spl_image_info *spl_image,
71 struct spl_boot_device *bootdev)
Matt Porter24de3572012-01-31 12:03:57 +000072{
Marek Vasut92e5cb82019-01-07 21:23:22 +010073 ulong size = 0;
Matt Porter24de3572012-01-31 12:03:57 +000074 int err;
75 int res;
76 int ret;
77 connection_info_t info;
78 char buf[BUF_SIZE];
Marek Vasutd574c192019-03-12 04:00:09 +010079 struct image_header *ih = NULL;
Matt Porter24de3572012-01-31 12:03:57 +000080 ulong addr = 0;
81
82 info.mode = xyzModem_ymodem;
83 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutlafa715192016-05-24 10:34:44 +053084 if (ret) {
Matt Porter24de3572012-01-31 12:03:57 +000085 printf("spl: ymodem err - %s\n", xyzModem_error(err));
Nikita Kiryanov36afd452015-11-08 17:11:49 +020086 return ret;
Matt Porter24de3572012-01-31 12:03:57 +000087 }
88
Lokesh Vutlafa715192016-05-24 10:34:44 +053089 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
90 if (res <= 0)
91 goto end_stream;
92
Marek Vasut792dd572019-03-06 22:04:31 +010093 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;
97
98 memcpy((void *)addr, buf, res);
99 size += res;
100 addr += res;
101
102 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
103 memcpy((void *)addr, buf, res);
104 size += res;
105 addr += res;
106 }
107
108 ret = spl_parse_image_header(spl_image, ih);
109 if (ret)
110 return ret;
111 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Lokesh Vutlafa715192016-05-24 10:34:44 +0530112 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
113 struct spl_load_info load;
114 struct ymodem_fit_info info;
115
116 debug("Found FIT\n");
117 load.dev = NULL;
118 load.priv = (void *)&info;
119 load.filename = NULL;
120 load.bl_len = 1;
121 info.buf = buf;
122 info.image_read = BUF_SIZE;
123 load.read = ymodem_read_fit;
Simon Glassf4d7d852016-09-24 18:20:16 -0600124 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutlafa715192016-05-24 10:34:44 +0530125 size = info.image_read;
126
127 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
128 size += res;
129 } else {
Marek Vasut92e5cb82019-01-07 21:23:22 +0100130 ih = (struct image_header *)buf;
131 ret = spl_parse_image_header(spl_image, ih);
Lokesh Vutlafa715192016-05-24 10:34:44 +0530132 if (ret)
Marek Vasut6d8dbe42019-03-12 04:02:39 +0100133 goto end_stream;
Marek Vasut92e5cb82019-01-07 21:23:22 +0100134#ifdef CONFIG_SPL_GZIP
135 if (ih->ih_comp == IH_COMP_GZIP)
136 addr = CONFIG_SYS_LOAD_ADDR;
137 else
138#endif
139 addr = spl_image->load_addr;
Lokesh Vutlafa715192016-05-24 10:34:44 +0530140 memcpy((void *)addr, buf, res);
Marek Vasut92e5cb82019-01-07 21:23:22 +0100141 ih = (struct image_header *)addr;
Lokesh Vutlafa715192016-05-24 10:34:44 +0530142 size += res;
143 addr += res;
144
145 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
146 memcpy((void *)addr, buf, res);
147 size += res;
148 addr += res;
149 }
150 }
151
152end_stream:
Matt Porter24de3572012-01-31 12:03:57 +0000153 xyzModem_stream_close(&err);
154 xyzModem_stream_terminate(false, &getcymodem);
155
Marek Vasut92e5cb82019-01-07 21:23:22 +0100156 printf("Loaded %lu bytes\n", size);
Marek Vasut6d8dbe42019-03-12 04:02:39 +0100157
Marek Vasutd574c192019-03-12 04:00:09 +0100158#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)),
165 &size)) {
166 puts("Uncompressing error\n");
167 return -EIO;
168 }
169 }
170#endif
171
Marek Vasut6d8dbe42019-03-12 04:02:39 +0100172 return ret;
Matt Porter24de3572012-01-31 12:03:57 +0000173}
Simon Glassebc4ef62016-11-30 15:30:50 -0700174SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);