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