blob: e979f780ad099c89fabce823898c85136095b0c4 [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>
Simon Glass4d72caa2020-05-10 11:40:01 -060013#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070015#include <spl.h>
Matt Porter24de3572012-01-31 12:03:57 +000016#include <xyzModem.h>
17#include <asm/u-boot.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Matt Porter24de3572012-01-31 12:03:57 +000019
20#define BUF_SIZE 1024
21
Lokesh Vutlafa715192016-05-24 10:34:44 +053022/*
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 */
28struct ymodem_fit_info {
29 int image_read;
30 char *buf;
31};
32
Matt Porter24de3572012-01-31 12:03:57 +000033static int getcymodem(void) {
34 if (tstc())
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020035 return (getchar());
Matt Porter24de3572012-01-31 12:03:57 +000036 return -1;
37}
38
Lokesh Vutlafa715192016-05-24 10:34:44 +053039static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
40 ulong size, void *addr)
41{
Lokesh Vutla095764e2019-11-14 18:33:30 +053042 int res, err, buf_offset;
Lokesh Vutlafa715192016-05-24 10:34:44 +053043 struct ymodem_fit_info *info = load->priv;
44 char *buf = info->buf;
45
46 while (info->image_read < offset) {
47 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
48 if (res <= 0)
Andreas Dannenberg9d6ee3e2019-08-15 15:55:26 -050049 break;
50
Lokesh Vutlafa715192016-05-24 10:34:44 +053051 info->image_read += res;
52 }
53
54 if (info->image_read > offset) {
55 res = info->image_read - offset;
Lokesh Vutla095764e2019-11-14 18:33:30 +053056 if (info->image_read % BUF_SIZE)
57 buf_offset = (info->image_read % BUF_SIZE);
58 else
59 buf_offset = BUF_SIZE;
60 memcpy(addr, &buf[buf_offset - res], res);
Lokesh Vutlafa715192016-05-24 10:34:44 +053061 addr = addr + res;
62 }
63
64 while (info->image_read < offset + size) {
65 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
66 if (res <= 0)
Andreas Dannenberg9d6ee3e2019-08-15 15:55:26 -050067 break;
Lokesh Vutlafa715192016-05-24 10:34:44 +053068
69 memcpy(addr, buf, res);
70 info->image_read += res;
71 addr += res;
72 }
73
74 return size;
75}
76
Andreas Dannenberge4130332019-08-15 15:55:27 -050077int spl_ymodem_load_image(struct spl_image_info *spl_image,
78 struct spl_boot_device *bootdev)
Matt Porter24de3572012-01-31 12:03:57 +000079{
Marek Vasut92e5cb82019-01-07 21:23:22 +010080 ulong size = 0;
Matt Porter24de3572012-01-31 12:03:57 +000081 int err;
82 int res;
83 int ret;
84 connection_info_t info;
85 char buf[BUF_SIZE];
Marek Vasutd574c192019-03-12 04:00:09 +010086 struct image_header *ih = NULL;
Matt Porter24de3572012-01-31 12:03:57 +000087 ulong addr = 0;
88
89 info.mode = xyzModem_ymodem;
90 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutlafa715192016-05-24 10:34:44 +053091 if (ret) {
Matt Porter24de3572012-01-31 12:03:57 +000092 printf("spl: ymodem err - %s\n", xyzModem_error(err));
Nikita Kiryanov36afd452015-11-08 17:11:49 +020093 return ret;
Matt Porter24de3572012-01-31 12:03:57 +000094 }
95
Lokesh Vutlafa715192016-05-24 10:34:44 +053096 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
97 if (res <= 0)
98 goto end_stream;
99
Marek Vasut792dd572019-03-06 22:04:31 +0100100 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
101 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
102 addr = CONFIG_SYS_LOAD_ADDR;
103 ih = (struct image_header *)addr;
104
105 memcpy((void *)addr, buf, res);
106 size += res;
107 addr += res;
108
109 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
110 memcpy((void *)addr, buf, res);
111 size += res;
112 addr += res;
113 }
114
115 ret = spl_parse_image_header(spl_image, ih);
116 if (ret)
117 return ret;
118 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Lokesh Vutlafa715192016-05-24 10:34:44 +0530119 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
120 struct spl_load_info load;
121 struct ymodem_fit_info info;
122
123 debug("Found FIT\n");
124 load.dev = NULL;
125 load.priv = (void *)&info;
126 load.filename = NULL;
127 load.bl_len = 1;
128 info.buf = buf;
129 info.image_read = BUF_SIZE;
130 load.read = ymodem_read_fit;
Simon Glassf4d7d852016-09-24 18:20:16 -0600131 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutlafa715192016-05-24 10:34:44 +0530132 size = info.image_read;
133
134 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
135 size += res;
136 } else {
Marek Vasut92e5cb82019-01-07 21:23:22 +0100137 ih = (struct image_header *)buf;
138 ret = spl_parse_image_header(spl_image, ih);
Lokesh Vutlafa715192016-05-24 10:34:44 +0530139 if (ret)
Marek Vasut6d8dbe42019-03-12 04:02:39 +0100140 goto end_stream;
Marek Vasut92e5cb82019-01-07 21:23:22 +0100141#ifdef CONFIG_SPL_GZIP
142 if (ih->ih_comp == IH_COMP_GZIP)
143 addr = CONFIG_SYS_LOAD_ADDR;
144 else
145#endif
146 addr = spl_image->load_addr;
Lokesh Vutlafa715192016-05-24 10:34:44 +0530147 memcpy((void *)addr, buf, res);
Marek Vasut92e5cb82019-01-07 21:23:22 +0100148 ih = (struct image_header *)addr;
Lokesh Vutlafa715192016-05-24 10:34:44 +0530149 size += res;
150 addr += res;
151
152 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
153 memcpy((void *)addr, buf, res);
154 size += res;
155 addr += res;
156 }
157 }
158
159end_stream:
Matt Porter24de3572012-01-31 12:03:57 +0000160 xyzModem_stream_close(&err);
161 xyzModem_stream_terminate(false, &getcymodem);
162
Marek Vasut92e5cb82019-01-07 21:23:22 +0100163 printf("Loaded %lu bytes\n", size);
Marek Vasut6d8dbe42019-03-12 04:02:39 +0100164
Marek Vasutd574c192019-03-12 04:00:09 +0100165#ifdef CONFIG_SPL_GZIP
166 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
167 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
168 (ih->ih_comp == IH_COMP_GZIP)) {
169 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
170 CONFIG_SYS_BOOTM_LEN,
171 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
172 &size)) {
173 puts("Uncompressing error\n");
174 return -EIO;
175 }
176 }
177#endif
178
Marek Vasut6d8dbe42019-03-12 04:02:39 +0100179 return ret;
Matt Porter24de3572012-01-31 12:03:57 +0000180}
Simon Glassebc4ef62016-11-30 15:30:50 -0700181SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);