blob: 4ab3dcd624d4e6e0576b28710f2dea81954aee11 [file] [log] [blame]
Matt Porter79bfa732012-01-31 12:03:57 +00001/*
2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2011
6 * Texas Instruments, <www.ti.com>
7 *
8 * Matt Porter <mporter@ti.com>
9 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Matt Porter79bfa732012-01-31 12:03:57 +000011 */
12#include <common.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>
16#include <asm/utils.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())
34 return (getc());
35 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{
41 int res, err;
42 struct ymodem_fit_info *info = load->priv;
43 char *buf = info->buf;
44
45 while (info->image_read < offset) {
46 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
47 if (res <= 0)
48 return res;
49 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)
61 return res;
62
63 memcpy(addr, buf, res);
64 info->image_read += res;
65 addr += res;
66 }
67
68 return size;
69}
70
Simon Glassee306792016-09-24 18:20:13 -060071static int spl_ymodem_load_image(struct spl_image_info *spl_image,
72 struct spl_boot_device *bootdev)
Matt Porter79bfa732012-01-31 12:03:57 +000073{
74 int size = 0;
75 int err;
76 int res;
77 int ret;
78 connection_info_t info;
79 char buf[BUF_SIZE];
Matt Porter79bfa732012-01-31 12:03:57 +000080 ulong addr = 0;
81
82 info.mode = xyzModem_ymodem;
83 ret = xyzModem_stream_open(&info, &err);
Lokesh Vutla37d26322016-05-24 10:34:44 +053084 if (ret) {
85 printf("spl: ymodem err - %s\n", xyzModem_error(err));
86 return ret;
87 }
88
89 res = xyzModem_stream_read(buf, BUF_SIZE, &err);
90 if (res <= 0)
91 goto end_stream;
92
93 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
94 image_get_magic((struct image_header *)buf) == FDT_MAGIC) {
95 struct spl_load_info load;
96 struct ymodem_fit_info info;
97
98 debug("Found FIT\n");
99 load.dev = NULL;
100 load.priv = (void *)&info;
101 load.filename = NULL;
102 load.bl_len = 1;
103 info.buf = buf;
104 info.image_read = BUF_SIZE;
105 load.read = ymodem_read_fit;
Simon Glass43a734f2016-09-24 18:20:16 -0600106 ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530107 size = info.image_read;
Matt Porter79bfa732012-01-31 12:03:57 +0000108
Lokesh Vutla37d26322016-05-24 10:34:44 +0530109 while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0)
110 size += res;
111 } else {
Simon Glassee306792016-09-24 18:20:13 -0600112 ret = spl_parse_image_header(spl_image,
Simon Glass6b2e4db2016-09-24 18:19:53 -0600113 (struct image_header *)buf);
Lokesh Vutla37d26322016-05-24 10:34:44 +0530114 if (ret)
115 return ret;
Simon Glassee306792016-09-24 18:20:13 -0600116 addr = spl_image->load_addr;
Lokesh Vutla37d26322016-05-24 10:34:44 +0530117 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);
Matt Porter79bfa732012-01-31 12:03:57 +0000123 size += res;
124 addr += res;
Matt Porter79bfa732012-01-31 12:03:57 +0000125 }
Matt Porter79bfa732012-01-31 12:03:57 +0000126 }
127
Lokesh Vutla37d26322016-05-24 10:34:44 +0530128end_stream:
Matt Porter79bfa732012-01-31 12:03:57 +0000129 xyzModem_stream_close(&err);
130 xyzModem_stream_terminate(false, &getcymodem);
131
132 printf("Loaded %d bytes\n", size);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200133 return 0;
Matt Porter79bfa732012-01-31 12:03:57 +0000134}
Simon Glass4fc1f252016-11-30 15:30:50 -0700135SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image);