blob: fd48f80f75e3977b7d14a917b2d125278a50e78c [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>
16#include <asm/u-boot.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Matt Porter79bfa732012-01-31 12:03:57 +000018
19#define BUF_SIZE 1024
20
Lokesh Vutla37d26322016-05-24 10:34:44 +053021/*
22 * Information required to load image using ymodem.
23 *
24 * @image_read: Now of bytes read from the image.
25 * @buf: pointer to the previous read block.
26 */
27struct ymodem_fit_info {
28 int image_read;
29 char *buf;
30};
31
Matt Porter79bfa732012-01-31 12:03:57 +000032static int getcymodem(void) {
33 if (tstc())
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +020034 return (getchar());
Matt Porter79bfa732012-01-31 12:03:57 +000035 return -1;
36}
37
Lokesh Vutla37d26322016-05-24 10:34:44 +053038static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
39 ulong size, void *addr)
40{
Lokesh Vutla7ac012a2019-11-14 18:33:30 +053041 int res, err, buf_offset;
Lokesh Vutla37d26322016-05-24 10:34:44 +053042 struct ymodem_fit_info *info = load->priv;
43 char *buf = info->buf;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053044 ulong copy_size = size;
Lokesh Vutla37d26322016-05-24 10:34:44 +053045
46 while (info->image_read < offset) {
47 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
48 if (res <= 0)
Andreas Dannenberge58d0362019-08-15 15:55:26 -050049 break;
50
Lokesh Vutla37d26322016-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 Vutla7ac012a2019-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;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053060
61 if (res > copy_size) {
62 memcpy(addr, &buf[buf_offset - res], copy_size);
63 goto done;
64 }
Lokesh Vutla7ac012a2019-11-14 18:33:30 +053065 memcpy(addr, &buf[buf_offset - res], res);
Lokesh Vutla37d26322016-05-24 10:34:44 +053066 addr = addr + res;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053067 copy_size -= res;
Lokesh Vutla37d26322016-05-24 10:34:44 +053068 }
69
70 while (info->image_read < offset + size) {
71 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
72 if (res <= 0)
Andreas Dannenberge58d0362019-08-15 15:55:26 -050073 break;
Lokesh Vutla37d26322016-05-24 10:34:44 +053074
Lokesh Vutla37d26322016-05-24 10:34:44 +053075 info->image_read += res;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053076 if (res > copy_size) {
77 memcpy(addr, buf, copy_size);
78 goto done;
79 }
80 memcpy(addr, buf, res);
Lokesh Vutla37d26322016-05-24 10:34:44 +053081 addr += res;
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053082 copy_size -= res;
Lokesh Vutla37d26322016-05-24 10:34:44 +053083 }
84
Vignesh Raghavendra247e1782022-01-31 09:49:19 +053085done:
Lokesh Vutla37d26322016-05-24 10:34:44 +053086 return size;
87}
88
Andreas Dannenberg58892082019-08-15 15:55:27 -050089int spl_ymodem_load_image(struct spl_image_info *spl_image,
90 struct spl_boot_device *bootdev)
Matt Porter79bfa732012-01-31 12:03:57 +000091{
Marek Vasutd9637072019-01-07 21:23:22 +010092 ulong size = 0;
Matt Porter79bfa732012-01-31 12:03:57 +000093 int err;
94 int res;
95 int ret;
96 connection_info_t info;
97 char buf[BUF_SIZE];
Simon Glassbb7d3bb2022-09-06 20:26:52 -060098 struct legacy_img_hdr *ih = NULL;
Matt Porter79bfa732012-01-31 12:03:57 +000099 ulong addr = 0;
100
101 info.mode = xyzModem_ymodem;
102 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530103 if (ret) {
104 printf("spl: ymodem err - %s\n", xyzModem_error(err));
105 return ret;
106 }
107
108 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
109 if (res <= 0)
110 goto end_stream;
111
Marek Vasut3114a952019-03-06 22:04:31 +0100112 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600113 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
Marek Vasut3114a952019-03-06 22:04:31 +0100114 addr = CONFIG_SYS_LOAD_ADDR;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600115 ih = (struct legacy_img_hdr *)addr;
Marek Vasut3114a952019-03-06 22:04:31 +0100116
117 memcpy((void *)addr, buf, res);
118 size += res;
119 addr += res;
120
121 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
122 memcpy((void *)addr, buf, res);
123 size += res;
124 addr += res;
125 }
126
Pali Rohárdda8f882022-01-14 14:31:38 +0100127 ret = spl_parse_image_header(spl_image, bootdev, ih);
Marek Vasut3114a952019-03-06 22:04:31 +0100128 if (ret)
129 return ret;
130 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600131 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) {
Lokesh Vutla37d26322016-05-24 10:34:44 +0530132 struct spl_load_info load;
133 struct ymodem_fit_info info;
134
135 debug("Found FIT\n");
Lokesh Vutla37d26322016-05-24 10:34:44 +0530136 load.priv = (void *)&info;
Sean Anderson35f15fe2023-11-08 11:48:43 -0500137 spl_set_bl_len(&load, 1);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530138 info.buf = buf;
139 info.image_read = BUF_SIZE;
140 load.read = ymodem_read_fit;
Simon Glass43a734f2016-09-24 18:20:16 -0600141 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530142 size = info.image_read;
Matt Porter79bfa732012-01-31 12:03:57 +0000143
Lokesh Vutla37d26322016-05-24 10:34:44 +0530144 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
145 size += res;
146 } else {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600147 ih = (struct legacy_img_hdr *)buf;
Pali Rohárdda8f882022-01-14 14:31:38 +0100148 ret = spl_parse_image_header(spl_image, bootdev, ih);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530149 if (ret)
Marek Vasut09e84ec2019-03-12 04:02:39 +0100150 goto end_stream;
Marek Vasutd9637072019-01-07 21:23:22 +0100151#ifdef CONFIG_SPL_GZIP
152 if (ih->ih_comp == IH_COMP_GZIP)
153 addr = CONFIG_SYS_LOAD_ADDR;
154 else
155#endif
156 addr = spl_image->load_addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530157 memcpy((void *)addr, buf, res);
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600158 ih = (struct legacy_img_hdr *)addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530159 size += res;
160 addr += res;
161
162 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
163 memcpy((void *)addr, buf, res);
Matt Porter79bfa732012-01-31 12:03:57 +0000164 size += res;
165 addr += res;
Matt Porter79bfa732012-01-31 12:03:57 +0000166 }
Matt Porter79bfa732012-01-31 12:03:57 +0000167 }
168
Lokesh Vutla37d26322016-05-24 10:34:44 +0530169end_stream:
Matt Porter79bfa732012-01-31 12:03:57 +0000170 xyzModem_stream_close(&err);
171 xyzModem_stream_terminate(false, &getcymodem);
172
Marek Vasutd9637072019-01-07 21:23:22 +0100173 printf("Loaded %lu bytes\n", size);
Marek Vasut09e84ec2019-03-12 04:02:39 +0100174
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100175#ifdef CONFIG_SPL_GZIP
176 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600177 image_get_magic((struct legacy_img_hdr *)buf) == FDT_MAGIC) &&
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100178 (ih->ih_comp == IH_COMP_GZIP)) {
179 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
180 CONFIG_SYS_BOOTM_LEN,
181 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
182 &size)) {
183 puts("Uncompressing error\n");
184 return -EIO;
185 }
186 }
187#endif
188
Marek Vasut09e84ec2019-03-12 04:02:39 +0100189 return ret;
Matt Porter79bfa732012-01-31 12:03:57 +0000190}
Simon Glass4fc1f252016-11-30 15:30:50 -0700191SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);