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