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