Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 2 | /* |
| 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 Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 10 | */ |
| 11 | #include <common.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 12 | #include <spl.h> |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 13 | #include <xyzModem.h> |
| 14 | #include <asm/u-boot.h> |
| 15 | #include <asm/utils.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 16 | #include <linux/libfdt.h> |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 17 | |
| 18 | #define BUF_SIZE 1024 |
| 19 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 20 | /* |
| 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 | */ |
| 26 | struct ymodem_fit_info { |
| 27 | int image_read; |
| 28 | char *buf; |
| 29 | }; |
| 30 | |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 31 | static int getcymodem(void) { |
| 32 | if (tstc()) |
| 33 | return (getc()); |
| 34 | return -1; |
| 35 | } |
| 36 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 37 | static ulong ymodem_read_fit(struct spl_load_info *load, ulong offset, |
| 38 | ulong size, void *addr) |
| 39 | { |
| 40 | int res, err; |
| 41 | struct ymodem_fit_info *info = load->priv; |
| 42 | char *buf = info->buf; |
| 43 | |
| 44 | while (info->image_read < offset) { |
| 45 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 46 | if (res <= 0) |
| 47 | return res; |
| 48 | info->image_read += res; |
| 49 | } |
| 50 | |
| 51 | if (info->image_read > offset) { |
| 52 | res = info->image_read - offset; |
| 53 | memcpy(addr, &buf[BUF_SIZE - res], res); |
| 54 | addr = addr + res; |
| 55 | } |
| 56 | |
| 57 | while (info->image_read < offset + size) { |
| 58 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 59 | if (res <= 0) |
| 60 | return res; |
| 61 | |
| 62 | memcpy(addr, buf, res); |
| 63 | info->image_read += res; |
| 64 | addr += res; |
| 65 | } |
| 66 | |
| 67 | return size; |
| 68 | } |
| 69 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 70 | static int spl_ymodem_load_image(struct spl_image_info *spl_image, |
| 71 | struct spl_boot_device *bootdev) |
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; |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 105 | ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 106 | size = info.image_read; |
| 107 | |
| 108 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 109 | size += res; |
| 110 | } else { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 111 | ret = spl_parse_image_header(spl_image, |
Simon Glass | 71316c1 | 2016-09-24 18:19:53 -0600 | [diff] [blame] | 112 | (struct image_header *)buf); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 113 | if (ret) |
| 114 | return ret; |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 115 | addr = spl_image->load_addr; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 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 | } |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 134 | SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image); |