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> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 16 | |
| 17 | #define BUF_SIZE 1024 |
| 18 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 19 | /* |
| 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 | */ |
| 25 | struct ymodem_fit_info { |
| 26 | int image_read; |
| 27 | char *buf; |
| 28 | }; |
| 29 | |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 30 | static int getcymodem(void) { |
| 31 | if (tstc()) |
| 32 | return (getc()); |
| 33 | return -1; |
| 34 | } |
| 35 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 36 | static 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 Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 69 | static int spl_ymodem_load_image(struct spl_image_info *spl_image, |
| 70 | struct spl_boot_device *bootdev) |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 71 | { |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 72 | ulong size = 0; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 73 | int err; |
| 74 | int res; |
| 75 | int ret; |
| 76 | connection_info_t info; |
| 77 | char buf[BUF_SIZE]; |
Marek Vasut | d574c19 | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 78 | struct image_header *ih = NULL; |
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 | |
Marek Vasut | 792dd57 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 92 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
| 93 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) { |
| 94 | addr = CONFIG_SYS_LOAD_ADDR; |
| 95 | ih = (struct image_header *)addr; |
| 96 | |
| 97 | memcpy((void *)addr, buf, res); |
| 98 | size += res; |
| 99 | addr += res; |
| 100 | |
| 101 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 102 | memcpy((void *)addr, buf, res); |
| 103 | size += res; |
| 104 | addr += res; |
| 105 | } |
| 106 | |
| 107 | ret = spl_parse_image_header(spl_image, ih); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 111 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) { |
| 112 | struct spl_load_info load; |
| 113 | struct ymodem_fit_info info; |
| 114 | |
| 115 | debug("Found FIT\n"); |
| 116 | load.dev = NULL; |
| 117 | load.priv = (void *)&info; |
| 118 | load.filename = NULL; |
| 119 | load.bl_len = 1; |
| 120 | info.buf = buf; |
| 121 | info.image_read = BUF_SIZE; |
| 122 | load.read = ymodem_read_fit; |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 123 | ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 124 | size = info.image_read; |
| 125 | |
| 126 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 127 | size += res; |
| 128 | } else { |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 129 | ih = (struct image_header *)buf; |
| 130 | ret = spl_parse_image_header(spl_image, ih); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 131 | if (ret) |
Marek Vasut | 6d8dbe4 | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 132 | goto end_stream; |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 133 | #ifdef CONFIG_SPL_GZIP |
| 134 | if (ih->ih_comp == IH_COMP_GZIP) |
| 135 | addr = CONFIG_SYS_LOAD_ADDR; |
| 136 | else |
| 137 | #endif |
| 138 | addr = spl_image->load_addr; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 139 | memcpy((void *)addr, buf, res); |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 140 | ih = (struct image_header *)addr; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 141 | size += res; |
| 142 | addr += res; |
| 143 | |
| 144 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 145 | memcpy((void *)addr, buf, res); |
| 146 | size += res; |
| 147 | addr += res; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | end_stream: |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 152 | xyzModem_stream_close(&err); |
| 153 | xyzModem_stream_terminate(false, &getcymodem); |
| 154 | |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 155 | printf("Loaded %lu bytes\n", size); |
Marek Vasut | 6d8dbe4 | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 156 | |
Marek Vasut | d574c19 | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 157 | #ifdef CONFIG_SPL_GZIP |
| 158 | if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 159 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) && |
| 160 | (ih->ih_comp == IH_COMP_GZIP)) { |
| 161 | if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)), |
| 162 | CONFIG_SYS_BOOTM_LEN, |
| 163 | (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)), |
| 164 | &size)) { |
| 165 | puts("Uncompressing error\n"); |
| 166 | return -EIO; |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | |
Marek Vasut | 6d8dbe4 | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 171 | return ret; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 172 | } |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 173 | SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image); |