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