blob: 4c7222af612dc92d59d85899c46d524923f3182f [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");
Lokesh Vutla37d26322016-05-24 10:34:44 +0530135 load.priv = (void *)&info;
Sean Anderson35f15fe2023-11-08 11:48:43 -0500136 spl_set_bl_len(&load, 1);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530137 info.buf = buf;
138 info.image_read = BUF_SIZE;
139 load.read = ymodem_read_fit;
Simon Glass43a734f2016-09-24 18:20:16 -0600140 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530141 size = info.image_read;
Matt Porter79bfa732012-01-31 12:03:57 +0000142
Lokesh Vutla37d26322016-05-24 10:34:44 +0530143 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
144 size += res;
145 } else {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600146 ih = (struct legacy_img_hdr *)buf;
Pali Rohárdda8f882022-01-14 14:31:38 +0100147 ret = spl_parse_image_header(spl_image, bootdev, ih);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530148 if (ret)
Marek Vasut09e84ec2019-03-12 04:02:39 +0100149 goto end_stream;
Marek Vasutd9637072019-01-07 21:23:22 +0100150#ifdef CONFIG_SPL_GZIP
151 if (ih->ih_comp == IH_COMP_GZIP)
152 addr = CONFIG_SYS_LOAD_ADDR;
153 else
154#endif
155 addr = spl_image->load_addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530156 memcpy((void *)addr, buf, res);
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600157 ih = (struct legacy_img_hdr *)addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530158 size += res;
159 addr += res;
160
161 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
162 memcpy((void *)addr, buf, res);
Matt Porter79bfa732012-01-31 12:03:57 +0000163 size += res;
164 addr += res;
Matt Porter79bfa732012-01-31 12:03:57 +0000165 }
Matt Porter79bfa732012-01-31 12:03:57 +0000166 }
167
Lokesh Vutla37d26322016-05-24 10:34:44 +0530168end_stream:
Matt Porter79bfa732012-01-31 12:03:57 +0000169 xyzModem_stream_close(&err);
170 xyzModem_stream_terminate(false, &getcymodem);
171
Marek Vasutd9637072019-01-07 21:23:22 +0100172 printf("Loaded %lu bytes\n", size);
Marek Vasut09e84ec2019-03-12 04:02:39 +0100173
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100174#ifdef CONFIG_SPL_GZIP
175 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600176 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) &&
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100177 (ih->ih_comp == IH_COMP_GZIP)) {
178 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
179 CONFIG_SYS_BOOTM_LEN,
180 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
181 &size)) {
182 puts("Uncompressing error\n");
183 return -EIO;
184 }
185 }
186#endif
187
Marek Vasut09e84ec2019-03-12 04:02:39 +0100188 return ret;
Matt Porter79bfa732012-01-31 12:03:57 +0000189}
Simon Glass4fc1f252016-11-30 15:30:50 -0700190SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);