blob: 25226e9a33565d0c1d14076e187a38b63094d664 [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>
Tom Rini28591df2012-08-13 12:03:19 -070012#include <spl.h>
Matt Porter79bfa732012-01-31 12:03:57 +000013#include <xyzModem.h>
14#include <asm/u-boot.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Matt Porter79bfa732012-01-31 12:03:57 +000016
17#define BUF_SIZE 1024
18
Lokesh Vutla37d26322016-05-24 10:34:44 +053019/*
20 * Information required to load image using ymodem.
21 *
22 * @image_read: Now of bytes read from the image.
23 * @buf: pointer to the previous read block.
24 */
25struct ymodem_fit_info {
26 int image_read;
27 char *buf;
28};
29
Matt Porter79bfa732012-01-31 12:03:57 +000030static int getcymodem(void) {
31 if (tstc())
32 return (getc());
33 return -1;
34}
35
Lokesh Vutla37d26322016-05-24 10:34:44 +053036static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset,
37 ulong size, void *addr)
38{
39 int res, err;
40 struct ymodem_fit_info *info = load->priv;
41 char *buf = info->buf;
42
43 while (info->image_read < offset) {
44 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
45 if (res <= 0)
46 return res;
47 info->image_read += res;
48 }
49
50 if (info->image_read > offset) {
51 res = info->image_read - offset;
52 memcpy(addr, &buf[BUF_SIZE - res], res);
53 addr = addr + res;
54 }
55
56 while (info->image_read < offset + size) {
57 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
58 if (res <= 0)
59 return res;
60
61 memcpy(addr, buf, res);
62 info->image_read += res;
63 addr += res;
64 }
65
66 return size;
67}
68
Simon Glassee306792016-09-24 18:20:13 -060069static int spl_ymodem_load_image(struct spl_image_info *spl_image,
70 struct spl_boot_device *bootdev)
Matt Porter79bfa732012-01-31 12:03:57 +000071{
Marek Vasutd9637072019-01-07 21:23:22 +010072 ulong size = 0;
Matt Porter79bfa732012-01-31 12:03:57 +000073 int err;
74 int res;
75 int ret;
76 connection_info_t info;
77 char buf[BUF_SIZE];
Marek Vasutf0c7fdc2019-03-12 04:00:09 +010078 struct image_header *ih = NULL;
Matt Porter79bfa732012-01-31 12:03:57 +000079 ulong addr = 0;
80
81 info.mode = xyzModem_ymodem;
82 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutla37d26322016-05-24 10:34:44 +053083 if (ret) {
84 printf("spl: ymodem err - %s\n", xyzModem_error(err));
85 return ret;
86 }
87
88 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
89 if (res <= 0)
90 goto end_stream;
91
92 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
93 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
94 struct spl_load_info load;
95 struct ymodem_fit_info info;
96
97 debug("Found FIT\n");
98 load.dev = NULL;
99 load.priv = (void *)&info;
100 load.filename = NULL;
101 load.bl_len = 1;
102 info.buf = buf;
103 info.image_read = BUF_SIZE;
104 load.read = ymodem_read_fit;
Simon Glass43a734f2016-09-24 18:20:16 -0600105 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530106 size = info.image_read;
Matt Porter79bfa732012-01-31 12:03:57 +0000107
Lokesh Vutla37d26322016-05-24 10:34:44 +0530108 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
109 size += res;
110 } else {
Marek Vasutd9637072019-01-07 21:23:22 +0100111 ih = (struct image_header *)buf;
112 ret = spl_parse_image_header(spl_image, ih);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530113 if (ret)
Marek Vasut09e84ec2019-03-12 04:02:39 +0100114 goto end_stream;
Marek Vasutd9637072019-01-07 21:23:22 +0100115#ifdef CONFIG_SPL_GZIP
116 if (ih->ih_comp == IH_COMP_GZIP)
117 addr = CONFIG_SYS_LOAD_ADDR;
118 else
119#endif
120 addr = spl_image->load_addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530121 memcpy((void *)addr, buf, res);
Marek Vasutd9637072019-01-07 21:23:22 +0100122 ih = (struct image_header *)addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530123 size += res;
124 addr += res;
125
126 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) {
127 memcpy((void *)addr, buf, res);
Matt Porter79bfa732012-01-31 12:03:57 +0000128 size += res;
129 addr += res;
Matt Porter79bfa732012-01-31 12:03:57 +0000130 }
Matt Porter79bfa732012-01-31 12:03:57 +0000131 }
132
Lokesh Vutla37d26322016-05-24 10:34:44 +0530133end_stream:
Matt Porter79bfa732012-01-31 12:03:57 +0000134 xyzModem_stream_close(&err);
135 xyzModem_stream_terminate(false, &getcymodem);
136
Marek Vasutd9637072019-01-07 21:23:22 +0100137 printf("Loaded %lu bytes\n", size);
Marek Vasut09e84ec2019-03-12 04:02:39 +0100138
Marek Vasutf0c7fdc2019-03-12 04:00:09 +0100139#ifdef CONFIG_SPL_GZIP
140 if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
141 image_get_magic((struct image_header *)buf) == FDT_MAGIC) &&
142 (ih->ih_comp == IH_COMP_GZIP)) {
143 if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)),
144 CONFIG_SYS_BOOTM_LEN,
145 (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)),
146 &size)) {
147 puts("Uncompressing error\n");
148 return -EIO;
149 }
150 }
151#endif
152
Marek Vasut09e84ec2019-03-12 04:02:39 +0100153 return ret;
Matt Porter79bfa732012-01-31 12:03:57 +0000154}
Simon Glass4fc1f252016-11-30 15:30:50 -0700155SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);