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