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> |
Simon Glass | 0c670fc | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 12 | #include <gzip.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 13 | #include <image.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 14 | #include <spl.h> |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 15 | #include <xyzModem.h> |
| 16 | #include <asm/u-boot.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/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 | { |
Lokesh Vutla | 095764e | 2019-11-14 18:33:30 +0530 | [diff] [blame] | 41 | int res, err, buf_offset; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 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) |
Andreas Dannenberg | 9d6ee3e | 2019-08-15 15:55:26 -0500 | [diff] [blame] | 48 | break; |
| 49 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 50 | info->image_read += res; |
| 51 | } |
| 52 | |
| 53 | if (info->image_read > offset) { |
| 54 | res = info->image_read - offset; |
Lokesh Vutla | 095764e | 2019-11-14 18:33:30 +0530 | [diff] [blame] | 55 | if (info->image_read % BUF_SIZE) |
| 56 | buf_offset = (info->image_read % BUF_SIZE); |
| 57 | else |
| 58 | buf_offset = BUF_SIZE; |
| 59 | memcpy(addr, &buf[buf_offset - res], res); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 60 | addr = addr + res; |
| 61 | } |
| 62 | |
| 63 | while (info->image_read < offset + size) { |
| 64 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 65 | if (res <= 0) |
Andreas Dannenberg | 9d6ee3e | 2019-08-15 15:55:26 -0500 | [diff] [blame] | 66 | break; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 67 | |
| 68 | memcpy(addr, buf, res); |
| 69 | info->image_read += res; |
| 70 | addr += res; |
| 71 | } |
| 72 | |
| 73 | return size; |
| 74 | } |
| 75 | |
Andreas Dannenberg | e413033 | 2019-08-15 15:55:27 -0500 | [diff] [blame] | 76 | int spl_ymodem_load_image(struct spl_image_info *spl_image, |
| 77 | struct spl_boot_device *bootdev) |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 78 | { |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 79 | ulong size = 0; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 80 | int err; |
| 81 | int res; |
| 82 | int ret; |
| 83 | connection_info_t info; |
| 84 | char buf[BUF_SIZE]; |
Marek Vasut | d574c19 | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 85 | struct image_header *ih = NULL; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 86 | ulong addr = 0; |
| 87 | |
| 88 | info.mode = xyzModem_ymodem; |
| 89 | ret = xyzModem_stream_open(&info, &err); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 90 | if (ret) { |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 91 | printf("spl: ymodem err - %s\n", xyzModem_error(err)); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 92 | return ret; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 95 | res = xyzModem_stream_read(buf, BUF_SIZE, &err); |
| 96 | if (res <= 0) |
| 97 | goto end_stream; |
| 98 | |
Marek Vasut | 792dd57 | 2019-03-06 22:04:31 +0100 | [diff] [blame] | 99 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) && |
| 100 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) { |
| 101 | addr = CONFIG_SYS_LOAD_ADDR; |
| 102 | ih = (struct image_header *)addr; |
| 103 | |
| 104 | memcpy((void *)addr, buf, res); |
| 105 | size += res; |
| 106 | addr += res; |
| 107 | |
| 108 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 109 | memcpy((void *)addr, buf, res); |
| 110 | size += res; |
| 111 | addr += res; |
| 112 | } |
| 113 | |
| 114 | ret = spl_parse_image_header(spl_image, ih); |
| 115 | if (ret) |
| 116 | return ret; |
| 117 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 118 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) { |
| 119 | struct spl_load_info load; |
| 120 | struct ymodem_fit_info info; |
| 121 | |
| 122 | debug("Found FIT\n"); |
| 123 | load.dev = NULL; |
| 124 | load.priv = (void *)&info; |
| 125 | load.filename = NULL; |
| 126 | load.bl_len = 1; |
| 127 | info.buf = buf; |
| 128 | info.image_read = BUF_SIZE; |
| 129 | load.read = ymodem_read_fit; |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 130 | ret = spl_load_simple_fit(spl_image, &load, 0, (void *)buf); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 131 | size = info.image_read; |
| 132 | |
| 133 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) |
| 134 | size += res; |
| 135 | } else { |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 136 | ih = (struct image_header *)buf; |
| 137 | ret = spl_parse_image_header(spl_image, ih); |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 138 | if (ret) |
Marek Vasut | 6d8dbe4 | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 139 | goto end_stream; |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 140 | #ifdef CONFIG_SPL_GZIP |
| 141 | if (ih->ih_comp == IH_COMP_GZIP) |
| 142 | addr = CONFIG_SYS_LOAD_ADDR; |
| 143 | else |
| 144 | #endif |
| 145 | addr = spl_image->load_addr; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 146 | memcpy((void *)addr, buf, res); |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 147 | ih = (struct image_header *)addr; |
Lokesh Vutla | fa71519 | 2016-05-24 10:34:44 +0530 | [diff] [blame] | 148 | size += res; |
| 149 | addr += res; |
| 150 | |
| 151 | while ((res = xyzModem_stream_read(buf, BUF_SIZE, &err)) > 0) { |
| 152 | memcpy((void *)addr, buf, res); |
| 153 | size += res; |
| 154 | addr += res; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | end_stream: |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 159 | xyzModem_stream_close(&err); |
| 160 | xyzModem_stream_terminate(false, &getcymodem); |
| 161 | |
Marek Vasut | 92e5cb8 | 2019-01-07 21:23:22 +0100 | [diff] [blame] | 162 | printf("Loaded %lu bytes\n", size); |
Marek Vasut | 6d8dbe4 | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 163 | |
Marek Vasut | d574c19 | 2019-03-12 04:00:09 +0100 | [diff] [blame] | 164 | #ifdef CONFIG_SPL_GZIP |
| 165 | if (!(IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 166 | image_get_magic((struct image_header *)buf) == FDT_MAGIC) && |
| 167 | (ih->ih_comp == IH_COMP_GZIP)) { |
| 168 | if (gunzip((void *)(spl_image->load_addr + sizeof(*ih)), |
| 169 | CONFIG_SYS_BOOTM_LEN, |
| 170 | (void *)(CONFIG_SYS_LOAD_ADDR + sizeof(*ih)), |
| 171 | &size)) { |
| 172 | puts("Uncompressing error\n"); |
| 173 | return -EIO; |
| 174 | } |
| 175 | } |
| 176 | #endif |
| 177 | |
Marek Vasut | 6d8dbe4 | 2019-03-12 04:02:39 +0100 | [diff] [blame] | 178 | return ret; |
Matt Porter | 24de357 | 2012-01-31 12:03:57 +0000 | [diff] [blame] | 179 | } |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 180 | SPL_LOAD_IMAGE_METHOD("UART", 0, BOOT_DEVICE_UART, spl_ymodem_load_image); |