Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 2 | /* |
| 3 | * dfu.c -- DFU back-end routines |
| 4 | * |
| 5 | * Copyright (C) 2012 Samsung Electronics |
| 6 | * author: Lukasz Majewski <l.majewski@samsung.com> |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 11 | #include <errno.h> |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <mmc.h> |
| 14 | #include <fat.h> |
| 15 | #include <dfu.h> |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 16 | #include <hash.h> |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 17 | #include <linux/list.h> |
| 18 | #include <linux/compiler.h> |
| 19 | |
| 20 | static LIST_HEAD(dfu_list); |
| 21 | static int dfu_alt_num; |
Przemyslaw Marczak | 4af3e08 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 22 | static int alt_num_cnt; |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 23 | static struct hash_algo *dfu_hash_algo; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 24 | |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 25 | /* |
| 26 | * The purpose of the dfu_usb_get_reset() function is to |
| 27 | * provide information if after USB_DETACH request |
| 28 | * being sent the dfu-util performed reset of USB |
| 29 | * bus. |
| 30 | * |
| 31 | * Described behaviour is the only way to distinct if |
| 32 | * user has typed -e (detach) or -R (reset) when invoking |
| 33 | * dfu-util command. |
| 34 | * |
| 35 | */ |
| 36 | __weak bool dfu_usb_get_reset(void) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 37 | { |
B, Ravi | 4cca5cf | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 38 | #ifdef CONFIG_SPL_DFU_NO_RESET |
| 39 | return false; |
| 40 | #else |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 41 | return true; |
B, Ravi | 4cca5cf | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 42 | #endif |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 45 | static int dfu_find_alt_num(const char *s) |
| 46 | { |
| 47 | int i = 0; |
| 48 | |
| 49 | for (; *s; s++) |
| 50 | if (*s == ';') |
| 51 | i++; |
| 52 | |
| 53 | return ++i; |
| 54 | } |
| 55 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 56 | int dfu_init_env_entities(char *interface, char *devstr) |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 57 | { |
| 58 | const char *str_env; |
| 59 | char *env_bkp; |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 60 | int ret = 0; |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 61 | |
Przemyslaw Marczak | d0dd609 | 2015-02-17 12:24:11 +0100 | [diff] [blame] | 62 | #ifdef CONFIG_SET_DFU_ALT_INFO |
| 63 | set_dfu_alt_info(interface, devstr); |
| 64 | #endif |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 65 | str_env = env_get("dfu_alt_info"); |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 66 | if (!str_env) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 67 | pr_err("\"dfu_alt_info\" env variable not defined!\n"); |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 68 | return -EINVAL; |
| 69 | } |
| 70 | |
| 71 | env_bkp = strdup(str_env); |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 72 | ret = dfu_config_entities(env_bkp, interface, devstr); |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 73 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 74 | pr_err("DFU entities configuration failed!\n"); |
Sam Protsenko | ab79fb0 | 2018-07-13 16:35:47 +0300 | [diff] [blame] | 75 | pr_err("(partition table does not match dfu_alt_info?)\n"); |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 76 | goto done; |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 79 | done: |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 80 | free(env_bkp); |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 81 | return ret; |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 84 | static unsigned char *dfu_buf; |
Lukasz Majewski | 0af4df8 | 2015-07-08 23:43:18 +0200 | [diff] [blame] | 85 | static unsigned long dfu_buf_size; |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 86 | |
Lukasz Majewski | 3426423 | 2013-10-08 14:30:39 +0200 | [diff] [blame] | 87 | unsigned char *dfu_free_buf(void) |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 88 | { |
| 89 | free(dfu_buf); |
| 90 | dfu_buf = NULL; |
| 91 | return dfu_buf; |
| 92 | } |
| 93 | |
Lukasz Majewski | 031d413 | 2013-12-09 16:20:13 +0100 | [diff] [blame] | 94 | unsigned long dfu_get_buf_size(void) |
| 95 | { |
| 96 | return dfu_buf_size; |
| 97 | } |
| 98 | |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 99 | unsigned char *dfu_get_buf(struct dfu_entity *dfu) |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 100 | { |
| 101 | char *s; |
| 102 | |
| 103 | if (dfu_buf != NULL) |
| 104 | return dfu_buf; |
| 105 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 106 | s = env_get("dfu_bufsiz"); |
Przemyslaw Marczak | 2488350 | 2014-12-15 10:34:11 +0100 | [diff] [blame] | 107 | if (s) |
| 108 | dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0); |
| 109 | |
| 110 | if (!s || !dfu_buf_size) |
| 111 | dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE; |
| 112 | |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 113 | if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size) |
| 114 | dfu_buf_size = dfu->max_buf_size; |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 115 | |
| 116 | dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); |
| 117 | if (dfu_buf == NULL) |
| 118 | printf("%s: Could not memalign 0x%lx bytes\n", |
| 119 | __func__, dfu_buf_size); |
| 120 | |
| 121 | return dfu_buf; |
| 122 | } |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 123 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 124 | static char *dfu_get_hash_algo(void) |
| 125 | { |
| 126 | char *s; |
| 127 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 128 | s = env_get("dfu_hash_algo"); |
Lukasz Majewski | 3e0d028 | 2014-06-10 12:25:59 +0200 | [diff] [blame] | 129 | if (!s) |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 130 | return NULL; |
| 131 | |
Lukasz Majewski | 3e0d028 | 2014-06-10 12:25:59 +0200 | [diff] [blame] | 132 | if (!strcmp(s, "crc32")) { |
| 133 | debug("%s: DFU hash method: %s\n", __func__, s); |
| 134 | return s; |
| 135 | } |
| 136 | |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 137 | pr_err("DFU hash method: %s not supported!\n", s); |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 138 | return NULL; |
| 139 | } |
| 140 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 141 | static int dfu_write_buffer_drain(struct dfu_entity *dfu) |
| 142 | { |
| 143 | long w_size; |
| 144 | int ret; |
| 145 | |
| 146 | /* flush size? */ |
| 147 | w_size = dfu->i_buf - dfu->i_buf_start; |
| 148 | if (w_size == 0) |
| 149 | return 0; |
| 150 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 151 | if (dfu_hash_algo) |
| 152 | dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc, |
| 153 | dfu->i_buf_start, w_size, 0); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 154 | |
| 155 | ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size); |
| 156 | if (ret) |
| 157 | debug("%s: Write error!\n", __func__); |
| 158 | |
| 159 | /* point back */ |
| 160 | dfu->i_buf = dfu->i_buf_start; |
| 161 | |
| 162 | /* update offset */ |
| 163 | dfu->offset += w_size; |
| 164 | |
| 165 | puts("#"); |
| 166 | |
| 167 | return ret; |
| 168 | } |
| 169 | |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 170 | void dfu_transaction_cleanup(struct dfu_entity *dfu) |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 171 | { |
| 172 | /* clear everything */ |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 173 | dfu->crc = 0; |
| 174 | dfu->offset = 0; |
| 175 | dfu->i_blk_seq_num = 0; |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 176 | dfu->i_buf_start = dfu_get_buf(dfu); |
| 177 | dfu->i_buf_end = dfu->i_buf_start; |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 178 | dfu->i_buf = dfu->i_buf_start; |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 179 | dfu->r_left = 0; |
| 180 | dfu->b_left = 0; |
| 181 | dfu->bad_skip = 0; |
| 182 | |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 183 | dfu->inited = 0; |
| 184 | } |
| 185 | |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 186 | int dfu_transaction_initiate(struct dfu_entity *dfu, bool read) |
| 187 | { |
| 188 | int ret = 0; |
| 189 | |
| 190 | if (dfu->inited) |
| 191 | return 0; |
| 192 | |
| 193 | dfu_transaction_cleanup(dfu); |
| 194 | |
| 195 | if (dfu->i_buf_start == NULL) |
| 196 | return -ENOMEM; |
| 197 | |
| 198 | dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size(); |
| 199 | |
| 200 | if (read) { |
| 201 | ret = dfu->get_medium_size(dfu, &dfu->r_left); |
| 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left); |
| 205 | } |
| 206 | |
| 207 | dfu->inited = 1; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 212 | int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 213 | { |
| 214 | int ret = 0; |
| 215 | |
Przemyslaw Marczak | a0f6a38 | 2014-05-12 12:05:33 +0200 | [diff] [blame] | 216 | ret = dfu_write_buffer_drain(dfu); |
| 217 | if (ret) |
| 218 | return ret; |
| 219 | |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 220 | if (dfu->flush_medium) |
| 221 | ret = dfu->flush_medium(dfu); |
| 222 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 223 | if (dfu_hash_algo) |
| 224 | printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name, |
| 225 | dfu->crc); |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 226 | |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 227 | dfu_transaction_cleanup(dfu); |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 232 | int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 233 | { |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 234 | int ret; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 235 | |
Stephen Warren | c00315b | 2015-07-22 14:54:04 -0600 | [diff] [blame] | 236 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 237 | __func__, dfu->name, buf, size, blk_seq_num, dfu->offset, |
Stephen Warren | c00315b | 2015-07-22 14:54:04 -0600 | [diff] [blame] | 238 | (unsigned long)(dfu->i_buf - dfu->i_buf_start)); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 239 | |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 240 | ret = dfu_transaction_initiate(dfu, false); |
| 241 | if (ret < 0) |
| 242 | return ret; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 243 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 244 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 245 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 246 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 247 | dfu_transaction_cleanup(dfu); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | /* DFU 1.1 standard says: |
| 252 | * The wBlockNum field is a block sequence number. It increments each |
| 253 | * time a block is transferred, wrapping to zero from 65,535. It is used |
| 254 | * to provide useful context to the DFU loader in the device." |
| 255 | * |
| 256 | * This means that it's a 16 bit counter that roll-overs at |
| 257 | * 0xffff -> 0x0000. By having a typical 4K transfer block |
| 258 | * we roll-over at exactly 256MB. Not very fun to debug. |
| 259 | * |
| 260 | * Handling rollover, and having an inited variable, |
| 261 | * makes things work. |
| 262 | */ |
| 263 | |
| 264 | /* handle rollover */ |
| 265 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 266 | |
| 267 | /* flush buffer if overflow */ |
| 268 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 269 | ret = dfu_write_buffer_drain(dfu); |
| 270 | if (ret) { |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 271 | dfu_transaction_cleanup(dfu); |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 272 | return ret; |
| 273 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* we should be in buffer now (if not then size too large) */ |
| 277 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 278 | pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf, |
Lukasz Majewski | 385c321 | 2013-09-10 15:29:22 +0200 | [diff] [blame] | 279 | size, dfu->i_buf_end); |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 280 | dfu_transaction_cleanup(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| 283 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 284 | memcpy(dfu->i_buf, buf, size); |
| 285 | dfu->i_buf += size; |
| 286 | |
| 287 | /* if end or if buffer full flush */ |
| 288 | if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 289 | ret = dfu_write_buffer_drain(dfu); |
| 290 | if (ret) { |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 291 | dfu_transaction_cleanup(dfu); |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 292 | return ret; |
| 293 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 294 | } |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 295 | |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 296 | return 0; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 299 | static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) |
| 300 | { |
| 301 | long chunk; |
| 302 | int ret, readn; |
| 303 | |
| 304 | readn = 0; |
| 305 | while (size > 0) { |
| 306 | /* get chunk that can be read */ |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 307 | chunk = min((long)size, dfu->b_left); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 308 | /* consume */ |
| 309 | if (chunk > 0) { |
| 310 | memcpy(buf, dfu->i_buf, chunk); |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 311 | if (dfu_hash_algo) |
| 312 | dfu_hash_algo->hash_update(dfu_hash_algo, |
| 313 | &dfu->crc, buf, |
| 314 | chunk, 0); |
| 315 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 316 | dfu->i_buf += chunk; |
| 317 | dfu->b_left -= chunk; |
| 318 | size -= chunk; |
| 319 | buf += chunk; |
| 320 | readn += chunk; |
| 321 | } |
| 322 | |
| 323 | /* all done */ |
| 324 | if (size > 0) { |
| 325 | /* no more to read */ |
| 326 | if (dfu->r_left == 0) |
| 327 | break; |
| 328 | |
| 329 | dfu->i_buf = dfu->i_buf_start; |
| 330 | dfu->b_left = dfu->i_buf_end - dfu->i_buf_start; |
| 331 | |
| 332 | /* got to read, but buffer is empty */ |
| 333 | if (dfu->b_left > dfu->r_left) |
| 334 | dfu->b_left = dfu->r_left; |
| 335 | ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf, |
| 336 | &dfu->b_left); |
| 337 | if (ret != 0) { |
| 338 | debug("%s: Read error!\n", __func__); |
| 339 | return ret; |
| 340 | } |
| 341 | dfu->offset += dfu->b_left; |
| 342 | dfu->r_left -= dfu->b_left; |
| 343 | |
| 344 | puts("#"); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return readn; |
| 349 | } |
| 350 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 351 | int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 352 | { |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 353 | int ret = 0; |
| 354 | |
| 355 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 356 | __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); |
| 357 | |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 358 | ret = dfu_transaction_initiate(dfu, true); |
| 359 | if (ret < 0) |
| 360 | return ret; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 361 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 362 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 363 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 364 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 365 | return -1; |
| 366 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 367 | /* handle rollover */ |
| 368 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 369 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 370 | ret = dfu_read_buffer_fill(dfu, buf, size); |
| 371 | if (ret < 0) { |
| 372 | printf("%s: Failed to fill buffer\n", __func__); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | if (ret < size) { |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 377 | if (dfu_hash_algo) |
| 378 | debug("%s: %s %s: 0x%x\n", __func__, dfu->name, |
| 379 | dfu_hash_algo->name, dfu->crc); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 380 | puts("\nUPLOAD ... done\nCtrl+C to exit ...\n"); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 381 | |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 382 | dfu_transaction_cleanup(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 383 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 384 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 389 | char *interface, char *devstr) |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 390 | { |
| 391 | char *st; |
| 392 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 393 | debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 394 | st = strsep(&s, " "); |
| 395 | strcpy(dfu->name, st); |
| 396 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 397 | dfu->alt = alt; |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 398 | dfu->max_buf_size = 0; |
Stephen Warren | a5f77f6 | 2014-06-11 16:03:35 -0600 | [diff] [blame] | 399 | dfu->free_entity = NULL; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 400 | |
| 401 | /* Specific for mmc device */ |
| 402 | if (strcmp(interface, "mmc") == 0) { |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 403 | if (dfu_fill_entity_mmc(dfu, devstr, s)) |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 404 | return -1; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 405 | } else if (strcmp(interface, "nand") == 0) { |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 406 | if (dfu_fill_entity_nand(dfu, devstr, s)) |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 407 | return -1; |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 408 | } else if (strcmp(interface, "ram") == 0) { |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 409 | if (dfu_fill_entity_ram(dfu, devstr, s)) |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 410 | return -1; |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 411 | } else if (strcmp(interface, "sf") == 0) { |
| 412 | if (dfu_fill_entity_sf(dfu, devstr, s)) |
| 413 | return -1; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 414 | } else { |
| 415 | printf("%s: Device %s not (yet) supported!\n", |
| 416 | __func__, interface); |
| 417 | return -1; |
| 418 | } |
Stephen Warren | 0898da5 | 2015-09-04 22:03:46 -0600 | [diff] [blame] | 419 | dfu_get_buf(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | void dfu_free_entities(void) |
| 425 | { |
| 426 | struct dfu_entity *dfu, *p, *t = NULL; |
| 427 | |
Stephen Warren | 0898da5 | 2015-09-04 22:03:46 -0600 | [diff] [blame] | 428 | dfu_free_buf(); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 429 | list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { |
| 430 | list_del(&dfu->list); |
Stephen Warren | a5f77f6 | 2014-06-11 16:03:35 -0600 | [diff] [blame] | 431 | if (dfu->free_entity) |
| 432 | dfu->free_entity(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 433 | t = dfu; |
| 434 | } |
| 435 | if (t) |
| 436 | free(t); |
| 437 | INIT_LIST_HEAD(&dfu_list); |
Przemyslaw Marczak | 4af3e08 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 438 | |
| 439 | alt_num_cnt = 0; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 442 | int dfu_config_entities(char *env, char *interface, char *devstr) |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 443 | { |
| 444 | struct dfu_entity *dfu; |
| 445 | int i, ret; |
| 446 | char *s; |
| 447 | |
| 448 | dfu_alt_num = dfu_find_alt_num(env); |
| 449 | debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num); |
| 450 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 451 | dfu_hash_algo = NULL; |
| 452 | s = dfu_get_hash_algo(); |
| 453 | if (s) { |
| 454 | ret = hash_lookup_algo(s, &dfu_hash_algo); |
| 455 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 456 | pr_err("Hash algorithm %s not supported\n", s); |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 459 | dfu = calloc(sizeof(*dfu), dfu_alt_num); |
| 460 | if (!dfu) |
| 461 | return -1; |
| 462 | for (i = 0; i < dfu_alt_num; i++) { |
| 463 | |
| 464 | s = strsep(&env, ";"); |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 465 | ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, |
| 466 | devstr); |
Peng Fan | 529d4fd | 2016-05-03 10:24:52 +0800 | [diff] [blame] | 467 | if (ret) { |
Sam Protsenko | 6d29e5c | 2018-07-13 16:35:45 +0300 | [diff] [blame] | 468 | /* We will free "dfu" in dfu_free_entities() */ |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 469 | return -1; |
Peng Fan | 529d4fd | 2016-05-03 10:24:52 +0800 | [diff] [blame] | 470 | } |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 471 | |
| 472 | list_add_tail(&dfu[i].list, &dfu_list); |
Przemyslaw Marczak | 4af3e08 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 473 | alt_num_cnt++; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | const char *dfu_get_dev_type(enum dfu_device_type t) |
| 480 | { |
Patrick Delaunay | ae224c3 | 2019-10-14 09:27:58 +0200 | [diff] [blame^] | 481 | const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", |
| 482 | "SF"}; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 483 | return dev_t[t]; |
| 484 | } |
| 485 | |
| 486 | const char *dfu_get_layout(enum dfu_layout l) |
| 487 | { |
Patrick Delaunay | ae224c3 | 2019-10-14 09:27:58 +0200 | [diff] [blame^] | 488 | const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2", |
| 489 | "EXT3", "EXT4", "RAM_ADDR" }; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 490 | return dfu_layout[l]; |
| 491 | } |
| 492 | |
| 493 | void dfu_show_entities(void) |
| 494 | { |
| 495 | struct dfu_entity *dfu; |
| 496 | |
| 497 | puts("DFU alt settings list:\n"); |
| 498 | |
| 499 | list_for_each_entry(dfu, &dfu_list, list) { |
| 500 | printf("dev: %s alt: %d name: %s layout: %s\n", |
| 501 | dfu_get_dev_type(dfu->dev_type), dfu->alt, |
| 502 | dfu->name, dfu_get_layout(dfu->layout)); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | int dfu_get_alt_number(void) |
| 507 | { |
| 508 | return dfu_alt_num; |
| 509 | } |
| 510 | |
| 511 | struct dfu_entity *dfu_get_entity(int alt) |
| 512 | { |
| 513 | struct dfu_entity *dfu; |
| 514 | |
| 515 | list_for_each_entry(dfu, &dfu_list, list) { |
| 516 | if (dfu->alt == alt) |
| 517 | return dfu; |
| 518 | } |
| 519 | |
| 520 | return NULL; |
| 521 | } |
Lukasz Majewski | 1737f0f | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 522 | |
| 523 | int dfu_get_alt(char *name) |
| 524 | { |
| 525 | struct dfu_entity *dfu; |
Lukasz Majewski | 3a39d01 | 2014-11-05 10:54:16 +0100 | [diff] [blame] | 526 | char *str; |
Lukasz Majewski | 1737f0f | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 527 | |
| 528 | list_for_each_entry(dfu, &dfu_list, list) { |
Lukasz Majewski | 3a39d01 | 2014-11-05 10:54:16 +0100 | [diff] [blame] | 529 | if (dfu->name[0] != '/') { |
| 530 | if (!strncmp(dfu->name, name, strlen(dfu->name))) |
| 531 | return dfu->alt; |
| 532 | } else { |
| 533 | /* |
| 534 | * One must also consider absolute path |
| 535 | * (/boot/bin/uImage) available at dfu->name when |
| 536 | * compared "plain" file name (uImage) |
| 537 | * |
| 538 | * It is the case for e.g. thor gadget where lthor SW |
| 539 | * sends only the file name, so only the very last part |
| 540 | * of path must be checked for equality |
| 541 | */ |
| 542 | |
| 543 | str = strstr(dfu->name, name); |
| 544 | if (!str) |
| 545 | continue; |
| 546 | |
| 547 | /* |
| 548 | * Check if matching substring is the last element of |
| 549 | * dfu->name (uImage) |
| 550 | */ |
| 551 | if (strlen(dfu->name) == |
| 552 | ((str - dfu->name) + strlen(name))) |
| 553 | return dfu->alt; |
| 554 | } |
Lukasz Majewski | 1737f0f | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return -ENODEV; |
| 558 | } |
Lukasz Majewski | cc2d026 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 559 | |
| 560 | int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size) |
| 561 | { |
| 562 | unsigned long dfu_buf_size, write, left = size; |
| 563 | int i, ret = 0; |
| 564 | void *dp = buf; |
| 565 | |
| 566 | /* |
| 567 | * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size |
| 568 | * has been properly initialized - e.g. if "dfu_bufsiz" has been taken |
| 569 | * into account. |
| 570 | */ |
| 571 | dfu_get_buf(dfu); |
| 572 | dfu_buf_size = dfu_get_buf_size(); |
| 573 | debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size); |
| 574 | |
| 575 | for (i = 0; left > 0; i++) { |
| 576 | write = min(dfu_buf_size, left); |
| 577 | |
| 578 | debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__, |
| 579 | dp, left, write); |
| 580 | ret = dfu_write(dfu, dp, write, i); |
| 581 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 582 | pr_err("DFU write failed\n"); |
Lukasz Majewski | cc2d026 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | dp += write; |
| 587 | left -= write; |
| 588 | } |
| 589 | |
| 590 | ret = dfu_flush(dfu, NULL, 0, i); |
| 591 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 592 | pr_err("DFU flush failed!"); |
Lukasz Majewski | cc2d026 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 593 | |
| 594 | return ret; |
| 595 | } |