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