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