Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 1 | /* |
| 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 Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 11 | */ |
| 12 | #include <common.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 13 | #include <spl.h> |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 14 | #include <xyzModem.h> |
| 15 | #include <asm/u-boot.h> |
| 16 | #include <asm/utils.h> |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 17 | #include <libfdt.h> |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 18 | |
| 19 | #define BUF_SIZE 1024 |
| 20 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 21 | /* |
| 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 | */ |
| 27 | struct ymodem_fit_info { |
| 28 | int image_read; |
| 29 | char *buf; |
| 30 | }; |
| 31 | |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 32 | static int getcymodem(void) { |
| 33 | if (tstc()) |
| 34 | return (getc()); |
| 35 | return -1; |
| 36 | } |
| 37 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 38 | static 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 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 71 | int spl_ymodem_load_image(void) |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 72 | { |
| 73 | int size = 0; |
| 74 | int err; |
| 75 | int res; |
| 76 | int ret; |
| 77 | connection_info_t info; |
| 78 | char buf[BUF_SIZE]; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 79 | ulong addr = 0; |
| 80 | |
| 81 | info.mode = xyzModem_ymodem; |
| 82 | ret = xyzModem_stream_open(&info, &err); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 83 | if (ret) { |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 84 | printf("spl: ymodem err - %s\n", xyzModem_error(err)); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 85 | return ret; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 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; |
| 105 | ret = spl_load_simple_fit(&load, 0, (void *)buf); |
| 106 | size = info.image_read; |
| 107 | |
| 108 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 109 | size += res; |
| 110 | } else { |
| 111 | spl_parse_image_header((struct image_header *)buf); |
| 112 | ret = spl_parse_image_header((struct image_header *)buf); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | addr = spl_image.load_addr; |
| 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 | } |
| 126 | |
| 127 | end_stream: |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 128 | xyzModem_stream_close(&err); |
| 129 | xyzModem_stream_terminate(false, &getcymodem); |
| 130 | |
| 131 | printf("Loaded %d bytes\n", size); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 132 | return 0; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 133 | } |