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