blob: c02c05624d48b1997efb227ea1680af3cb326b9b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Matt Porter79bfa732012-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 Porter79bfa732012-01-31 12:03:57 +000010 */
11#include <common.h>
Simon Glass1a974af2019-08-01 09:46:36 -060012#include <gzip.h>
Tom Rini28591df2012-08-13 12:03:19 -070013#include <spl.h>
Matt Porter79bfa732012-01-31 12:03:57 +000014#include <xyzModem.h>
15#include <asm/u-boot.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Matt Porter79bfa732012-01-31 12:03:57 +000017
18#define BUF_SIZE 1024
19
Lokesh Vutla37d26322016-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 Porter79bfa732012-01-31 12:03:57 +000031static int getcymodem(void) {
32 if (tstc())
33 return (getc());
34 return -1;
35}
36
Lokesh Vutla37d26322016-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)
Andreas Dannenberge58d0362019-08-15 15:55:26 -050047 break;
48
Lokesh Vutla37d26322016-05-24 10:34:44 +053049 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)
Andreas Dannenberge58d0362019-08-15 15:55:26 -050061 break;
Lokesh Vutla37d26322016-05-24 10:34:44 +053062
63 memcpy(addr, buf, res);
64 info->image_read += res;
65 addr += res;
66 }
67
68 return size;
69}
70
Andreas Dannenberg58892082019-08-15 15:55:27 -050071int spl_ymodem_load_image(struct spl_image_info *spl_image,
72 struct spl_boot_device *bootdev)
Matt Porter79bfa732012-01-31 12:03:57 +000073{
Marek Vasutd9637072019-01-07 21:23:22 +010074 ulong size = 0;
Matt Porter79bfa732012-01-31 12:03:57 +000075 int err;
76 int res;
77 int ret;
78 connection_info_t info;
79 char buf[BUF_SIZE];
Marek Vasutf0c7fdc2019-03-12 04:00:09 +010080 struct image_header *ih = NULL;
Matt Porter79bfa732012-01-31 12:03:57 +000081 ulong addr = 0;
82
83 info.mode = xyzModem_ymodem;
84 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutla37d26322016-05-24 10:34:44 +053085 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
Marek Vasut3114a952019-03-06 22:04:31 +010094 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) &&
Lokesh Vutla37d26322016-05-24 10:34:44 +0530113 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;
Simon Glass43a734f2016-09-24 18:20:16 -0600125 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530126 size = info.image_read;
Matt Porter79bfa732012-01-31 12:03:57 +0000127
Lokesh Vutla37d26322016-05-24 10:34:44 +0530128 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
129 size += res;
130 } else {
Marek Vasutd9637072019-01-07 21:23:22 +0100131 ih = (struct image_header *)buf;
132 ret = spl_parse_image_header(spl_image, ih);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530133 if (ret)
Marek Vasut09e84ec2019-03-12 04:02:39 +0100134 goto end_stream;
Marek Vasutd9637072019-01-07 21:23:22 +0100135#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;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530141 memcpy((void *)addr, buf, res);
Marek Vasutd9637072019-01-07 21:23:22 +0100142 ih = (struct image_header *)addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530143 size += res;
144 addr += res;
145
146 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
147 memcpy((void *)addr, buf, res);
Matt Porter79bfa732012-01-31 12:03:57 +0000148 size += res;
149 addr += res;
Matt Porter79bfa732012-01-31 12:03:57 +0000150 }
Matt Porter79bfa732012-01-31 12:03:57 +0000151 }
152
Lokesh Vutla37d26322016-05-24 10:34:44 +0530153end_stream:
Matt Porter79bfa732012-01-31 12:03:57 +0000154 xyzModem_stream_close(&err);
155 xyzModem_stream_terminate(false, &getcymodem);
156
Marek Vasutd9637072019-01-07 21:23:22 +0100157 printf("Loaded %lu bytes\n", size);
Marek Vasut09e84ec2019-03-12 04:02:39 +0100158
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100159#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
Marek Vasut09e84ec2019-03-12 04:02:39 +0100173 return ret;
Matt Porter79bfa732012-01-31 12:03:57 +0000174}
Simon Glass4fc1f252016-11-30 15:30:50 -0700175SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);