blob: 2be957134c1b7a851f41b0cf390f6d89461c5c99 [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 */
Simon Glass1a974af2019-08-01 09:46:36 -060011#include <gzip.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Tom Rini28591df2012-08-13 12:03:19 -070014#include <spl.h>
Matt Porter79bfa732012-01-31 12:03:57 +000015#include <xyzModem.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())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020033 return (getchar());
Matt Porter79bfa732012-01-31 12:03:57 +000034 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{
Lokesh Vutla7ac012a2019-11-14 18:33:30 +053040 int res, err, buf_offset;
Lokesh Vutla37d26322016-05-24 10:34:44 +053041 struct ymodem_fit_info *info = load->priv;
42 char *buf = info->buf;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053043 ulong copy_size = size;
Lokesh Vutla37d26322016-05-24 10:34:44 +053044
45 while (info->image_read < offset) {
46 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
47 if (res <= 0)
Andreas Dannenberge58d0362019-08-15 15:55:26 -050048 break;
49
Lokesh Vutla37d26322016-05-24 10:34:44 +053050 info->image_read += res;
51 }
52
53 if (info->image_read > offset) {
54 res = info->image_read - offset;
Lokesh Vutla7ac012a2019-11-14 18:33:30 +053055 if (info->image_read % BUF_SIZE)
56 buf_offset = (info->image_read % BUF_SIZE);
57 else
58 buf_offset = BUF_SIZE;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053059
60 if (res > copy_size) {
61 memcpy(addr, &buf[buf_offset - res], copy_size);
62 goto done;
63 }
Lokesh Vutla7ac012a2019-11-14 18:33:30 +053064 memcpy(addr, &buf[buf_offset - res], res);
Lokesh Vutla37d26322016-05-24 10:34:44 +053065 addr = addr + res;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053066 copy_size -= res;
Lokesh Vutla37d26322016-05-24 10:34:44 +053067 }
68
69 while (info->image_read < offset + size) {
70 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
71 if (res <= 0)
Andreas Dannenberge58d0362019-08-15 15:55:26 -050072 break;
Lokesh Vutla37d26322016-05-24 10:34:44 +053073
Lokesh Vutla37d26322016-05-24 10:34:44 +053074 info->image_read += res;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053075 if (res > copy_size) {
76 memcpy(addr, buf, copy_size);
77 goto done;
78 }
79 memcpy(addr, buf, res);
Lokesh Vutla37d26322016-05-24 10:34:44 +053080 addr += res;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053081 copy_size -= res;
Lokesh Vutla37d26322016-05-24 10:34:44 +053082 }
83
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053084done:
Lokesh Vutla37d26322016-05-24 10:34:44 +053085 return size;
86}
87
Andreas Dannenberg58892082019-08-15 15:55:27 -050088int spl_ymodem_load_image(struct spl_image_info *spl_image,
89 struct spl_boot_device *bootdev)
Matt Porter79bfa732012-01-31 12:03:57 +000090{
Marek Vasutd9637072019-01-07 21:23:22 +010091 ulong size = 0;
Matt Porter79bfa732012-01-31 12:03:57 +000092 int err;
93 int res;
94 int ret;
95 connection_info_t info;
96 char buf[BUF_SIZE];
Simon Glassbb7d3bb2022-09-06 20:26:52 -060097 struct legacy_img_hdr *ih = NULL;
Matt Porter79bfa732012-01-31 12:03:57 +000098 ulong addr = 0;
99
100 info.mode = xyzModem_ymodem;
101 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530102 if (ret) {
103 printf("spl: ymodem err - %s\n", xyzModem_error(err));
104 return ret;
105 }
106
107 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
108 if (res <= 0)
109 goto end_stream;
110
Marek Vasut3114a952019-03-06 22:04:31 +0100111 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600112 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
Marek Vasut3114a952019-03-06 22:04:31 +0100113 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600114 ih = (struct legacy_img_hdr *)addr;
Marek Vasut3114a952019-03-06 22:04:31 +0100115
116 memcpy((void *)addr, buf, res);
117 size += res;
118 addr += res;
119
120 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
121 memcpy((void *)addr, buf, res);
122 size += res;
123 addr += res;
124 }
125
Pali Rohárdda8f882022-01-14 14:31:38 +0100126 ret = spl_parse_image_header(spl_image, bootdev, ih);
Marek Vasut3114a952019-03-06 22:04:31 +0100127 if (ret)
128 return ret;
129 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600130 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
Lokesh Vutla37d26322016-05-24 10:34:44 +0530131 struct spl_load_info load;
132 struct ymodem_fit_info info;
133
134 debug("Found FIT\n");
Simon Glasse8066862024-08-22 07:55:02 -0600135 spl_load_init(&load, ymodem_read_fit, (void *)&info, 1);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530136 info.buf = buf;
137 info.image_read = BUF_SIZE;
Simon Glass43a734f2016-09-24 18:20:16 -0600138 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530139 size = info.image_read;
Matt Porter79bfa732012-01-31 12:03:57 +0000140
Lokesh Vutla37d26322016-05-24 10:34:44 +0530141 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
142 size += res;
143 } else {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600144 ih = (struct legacy_img_hdr *)buf;
Pali Rohárdda8f882022-01-14 14:31:38 +0100145 ret = spl_parse_image_header(spl_image, bootdev, ih);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530146 if (ret)
Marek Vasut09e84ec2019-03-12 04:02:39 +0100147 goto end_stream;
Marek Vasutd9637072019-01-07 21:23:22 +0100148#ifdef CONFIG_SPL_GZIP
149 if (ih->ih_comp == IH_COMP_GZIP)
150 addr = CONFIG_SYS_LOAD_ADDR;
151 else
152#endif
153 addr = spl_image->load_addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530154 memcpy((void *)addr, buf, res);
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600155 ih = (struct legacy_img_hdr *)addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530156 size += res;
157 addr += res;
158
159 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
160 memcpy((void *)addr, buf, res);
Matt Porter79bfa732012-01-31 12:03:57 +0000161 size += res;
162 addr += res;
Matt Porter79bfa732012-01-31 12:03:57 +0000163 }
Matt Porter79bfa732012-01-31 12:03:57 +0000164 }
165
Lokesh Vutla37d26322016-05-24 10:34:44 +0530166end_stream:
Matt Porter79bfa732012-01-31 12:03:57 +0000167 xyzModem_stream_close(&err);
168 xyzModem_stream_terminate(false, &getcymodem);
169
Marek Vasutd9637072019-01-07 21:23:22 +0100170 printf("Loaded %lu bytes\n", size);
Marek Vasut09e84ec2019-03-12 04:02:39 +0100171
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100172#ifdef CONFIG_SPL_GZIP
173 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600174 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) &&
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100175 (ih->ih_comp == IH_COMP_GZIP)) {
176 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
177 CONFIG_SYS_BOOTM_LEN,
178 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
179 &size)) {
180 puts("Uncompressing error\n");
181 return -EIO;
182 }
183 }
184#endif
185
Marek Vasut09e84ec2019-03-12 04:02:39 +0100186 return ret;
Matt Porter79bfa732012-01-31 12:03:57 +0000187}
Simon Glass4fc1f252016-11-30 15:30:50 -0700188SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);