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