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