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 | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 20 | static bool dfu_reset_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 | 6bed7ce | 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 | f22b11c | 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 | |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 47 | int dfu_init_env_entities(char *interface, int dev) |
| 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); |
| 60 | ret = dfu_config_entities(env_bkp, interface, dev); |
| 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 | e7e75c7 | 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 | d427826 | 2013-10-08 14:30:39 +0200 | [diff] [blame] | 73 | unsigned char *dfu_free_buf(void) |
Heiko Schocher | e7e75c7 | 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 | 4fb1278 | 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 | |
Lukasz Majewski | d427826 | 2013-10-08 14:30:39 +0200 | [diff] [blame] | 85 | unsigned char *dfu_get_buf(void) |
Heiko Schocher | e7e75c7 | 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; |
| 95 | |
| 96 | dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); |
| 97 | if (dfu_buf == NULL) |
| 98 | printf("%s: Could not memalign 0x%lx bytes\n", |
| 99 | __func__, dfu_buf_size); |
| 100 | |
| 101 | return dfu_buf; |
| 102 | } |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 103 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 104 | static char *dfu_get_hash_algo(void) |
| 105 | { |
| 106 | char *s; |
| 107 | |
| 108 | s = getenv("dfu_hash_algo"); |
| 109 | /* |
| 110 | * By default the legacy behaviour to calculate the crc32 hash |
| 111 | * value is preserved. |
| 112 | * |
| 113 | * To disable calculation of the hash algorithm for received data |
| 114 | * specify the "dfu_hash_algo = disabled" at your board envs. |
| 115 | */ |
| 116 | debug("%s: DFU hash method: %s\n", __func__, s ? s : "not specified"); |
| 117 | |
| 118 | if (!s || !strcmp(s, "crc32")) |
| 119 | return "crc32"; |
| 120 | |
| 121 | if (!strcmp(s, "disabled")) |
| 122 | return NULL; |
| 123 | |
| 124 | return NULL; |
| 125 | } |
| 126 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 127 | static int dfu_write_buffer_drain(struct dfu_entity *dfu) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 128 | { |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 129 | long w_size; |
| 130 | int ret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 131 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 132 | /* flush size? */ |
| 133 | w_size = dfu->i_buf - dfu->i_buf_start; |
| 134 | if (w_size == 0) |
| 135 | return 0; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 136 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 137 | if (dfu_hash_algo) |
| 138 | dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc, |
| 139 | dfu->i_buf_start, w_size, 0); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 140 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 141 | ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size); |
| 142 | if (ret) |
| 143 | debug("%s: Write error!\n", __func__); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 144 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 145 | /* point back */ |
| 146 | dfu->i_buf = dfu->i_buf_start; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 147 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 148 | /* update offset */ |
| 149 | dfu->offset += w_size; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 150 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 151 | puts("#"); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 156 | int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 157 | { |
| 158 | int ret = 0; |
| 159 | |
Przemyslaw Marczak | 1aa4bdc | 2014-05-12 12:05:33 +0200 | [diff] [blame] | 160 | ret = dfu_write_buffer_drain(dfu); |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 164 | if (dfu->flush_medium) |
| 165 | ret = dfu->flush_medium(dfu); |
| 166 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 167 | if (dfu_hash_algo) |
| 168 | printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name, |
| 169 | dfu->crc); |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 170 | |
| 171 | /* clear everything */ |
| 172 | dfu_free_buf(); |
| 173 | dfu->crc = 0; |
| 174 | dfu->offset = 0; |
| 175 | dfu->i_blk_seq_num = 0; |
| 176 | dfu->i_buf_start = dfu_buf; |
| 177 | dfu->i_buf_end = dfu_buf; |
| 178 | dfu->i_buf = dfu->i_buf_start; |
| 179 | dfu->inited = 0; |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 184 | 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] | 185 | { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 186 | int ret = 0; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 187 | int tret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 188 | |
Pantelis Antoniou | ea2453d | 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 | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 192 | |
Pantelis Antoniou | ea2453d | 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 | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 197 | dfu->bad_skip = 0; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 198 | dfu->i_blk_seq_num = 0; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 199 | dfu->i_buf_start = dfu_get_buf(); |
| 200 | if (dfu->i_buf_start == NULL) |
| 201 | return -ENOMEM; |
| 202 | dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 203 | dfu->i_buf = dfu->i_buf_start; |
| 204 | |
| 205 | dfu->inited = 1; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 208 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 209 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 210 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 214 | /* DFU 1.1 standard says: |
| 215 | * The wBlockNum field is a block sequence number. It increments each |
| 216 | * time a block is transferred, wrapping to zero from 65,535. It is used |
| 217 | * to provide useful context to the DFU loader in the device." |
| 218 | * |
| 219 | * This means that it's a 16 bit counter that roll-overs at |
| 220 | * 0xffff -> 0x0000. By having a typical 4K transfer block |
| 221 | * we roll-over at exactly 256MB. Not very fun to debug. |
| 222 | * |
| 223 | * Handling rollover, and having an inited variable, |
| 224 | * makes things work. |
| 225 | */ |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 226 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 227 | /* handle rollover */ |
| 228 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 229 | |
| 230 | /* flush buffer if overflow */ |
| 231 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
| 232 | tret = dfu_write_buffer_drain(dfu); |
| 233 | if (ret == 0) |
| 234 | ret = tret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 235 | } |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 236 | |
| 237 | /* we should be in buffer now (if not then size too large) */ |
| 238 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Lukasz Majewski | a7d2c3c | 2013-09-10 15:29:22 +0200 | [diff] [blame] | 239 | error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf, |
| 240 | size, dfu->i_buf_end); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | memcpy(dfu->i_buf, buf, size); |
| 245 | dfu->i_buf += size; |
| 246 | |
| 247 | /* if end or if buffer full flush */ |
| 248 | if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { |
| 249 | tret = dfu_write_buffer_drain(dfu); |
| 250 | if (ret == 0) |
| 251 | ret = tret; |
| 252 | } |
| 253 | |
Lukasz Majewski | 672ad18 | 2014-04-24 10:24:53 +0200 | [diff] [blame] | 254 | return ret; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) |
| 258 | { |
| 259 | long chunk; |
| 260 | int ret, readn; |
| 261 | |
| 262 | readn = 0; |
| 263 | while (size > 0) { |
| 264 | /* get chunk that can be read */ |
| 265 | chunk = min(size, dfu->b_left); |
| 266 | /* consume */ |
| 267 | if (chunk > 0) { |
| 268 | memcpy(buf, dfu->i_buf, chunk); |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 269 | if (dfu_hash_algo) |
| 270 | dfu_hash_algo->hash_update(dfu_hash_algo, |
| 271 | &dfu->crc, buf, |
| 272 | chunk, 0); |
| 273 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 274 | dfu->i_buf += chunk; |
| 275 | dfu->b_left -= chunk; |
Bo Shen | c2617bc | 2013-10-16 17:26:37 +0800 | [diff] [blame] | 276 | dfu->r_left -= chunk; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 277 | size -= chunk; |
| 278 | buf += chunk; |
| 279 | readn += chunk; |
| 280 | } |
| 281 | |
| 282 | /* all done */ |
| 283 | if (size > 0) { |
| 284 | /* no more to read */ |
| 285 | if (dfu->r_left == 0) |
| 286 | break; |
| 287 | |
| 288 | dfu->i_buf = dfu->i_buf_start; |
| 289 | dfu->b_left = dfu->i_buf_end - dfu->i_buf_start; |
| 290 | |
| 291 | /* got to read, but buffer is empty */ |
| 292 | if (dfu->b_left > dfu->r_left) |
| 293 | dfu->b_left = dfu->r_left; |
| 294 | ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf, |
| 295 | &dfu->b_left); |
| 296 | if (ret != 0) { |
| 297 | debug("%s: Read error!\n", __func__); |
| 298 | return ret; |
| 299 | } |
| 300 | dfu->offset += dfu->b_left; |
| 301 | dfu->r_left -= dfu->b_left; |
| 302 | |
| 303 | puts("#"); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return readn; |
| 308 | } |
| 309 | |
| 310 | int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 311 | { |
| 312 | int ret = 0; |
| 313 | |
| 314 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n", |
| 315 | __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); |
| 316 | |
| 317 | if (!dfu->inited) { |
Łukasz Majewski | 9df4955 | 2013-06-28 18:41:50 +0200 | [diff] [blame] | 318 | dfu->i_buf_start = dfu_get_buf(); |
| 319 | if (dfu->i_buf_start == NULL) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 323 | if (ret != 0) { |
| 324 | debug("%s: failed to get r_left\n", __func__); |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left); |
| 329 | |
| 330 | dfu->i_blk_seq_num = 0; |
| 331 | dfu->crc = 0; |
| 332 | dfu->offset = 0; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 333 | dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 334 | dfu->i_buf = dfu->i_buf_start; |
Bo Shen | 31bae4c | 2013-10-16 17:26:38 +0800 | [diff] [blame] | 335 | dfu->b_left = min(dfu_buf_size, dfu->r_left); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 336 | |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 337 | dfu->bad_skip = 0; |
| 338 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 339 | dfu->inited = 1; |
| 340 | } |
| 341 | |
| 342 | if (dfu->i_blk_seq_num != blk_seq_num) { |
| 343 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
| 344 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
| 345 | return -1; |
| 346 | } |
| 347 | /* handle rollover */ |
| 348 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 349 | |
| 350 | ret = dfu_read_buffer_fill(dfu, buf, size); |
| 351 | if (ret < 0) { |
| 352 | printf("%s: Failed to fill buffer\n", __func__); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | if (ret < size) { |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 357 | if (dfu_hash_algo) |
| 358 | debug("%s: %s %s: 0x%x\n", __func__, dfu->name, |
| 359 | dfu_hash_algo->name, dfu->crc); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 360 | puts("\nUPLOAD ... done\nCtrl+C to exit ...\n"); |
| 361 | |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 362 | dfu_free_buf(); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 363 | dfu->i_blk_seq_num = 0; |
| 364 | dfu->crc = 0; |
| 365 | dfu->offset = 0; |
| 366 | dfu->i_buf_start = dfu_buf; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 367 | dfu->i_buf_end = dfu_buf; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 368 | dfu->i_buf = dfu->i_buf_start; |
| 369 | dfu->b_left = 0; |
| 370 | |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 371 | dfu->bad_skip = 0; |
| 372 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 373 | dfu->inited = 0; |
| 374 | } |
| 375 | |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, |
Mateusz Zalega | 16297cf | 2013-10-04 19:22:26 +0200 | [diff] [blame] | 380 | char *interface, int num) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 381 | { |
| 382 | char *st; |
| 383 | |
| 384 | debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num); |
| 385 | st = strsep(&s, " "); |
| 386 | strcpy(dfu->name, st); |
| 387 | |
| 388 | dfu->dev_num = num; |
| 389 | dfu->alt = alt; |
| 390 | |
| 391 | /* Specific for mmc device */ |
| 392 | if (strcmp(interface, "mmc") == 0) { |
| 393 | if (dfu_fill_entity_mmc(dfu, s)) |
| 394 | return -1; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 395 | } else if (strcmp(interface, "nand") == 0) { |
| 396 | if (dfu_fill_entity_nand(dfu, s)) |
| 397 | return -1; |
Afzal Mohammed | a9479f0 | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 398 | } else if (strcmp(interface, "ram") == 0) { |
| 399 | if (dfu_fill_entity_ram(dfu, s)) |
| 400 | return -1; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 401 | } else { |
| 402 | printf("%s: Device %s not (yet) supported!\n", |
| 403 | __func__, interface); |
| 404 | return -1; |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | void dfu_free_entities(void) |
| 411 | { |
| 412 | struct dfu_entity *dfu, *p, *t = NULL; |
| 413 | |
| 414 | list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { |
| 415 | list_del(&dfu->list); |
| 416 | t = dfu; |
| 417 | } |
| 418 | if (t) |
| 419 | free(t); |
| 420 | INIT_LIST_HEAD(&dfu_list); |
Przemyslaw Marczak | b627eb4 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 421 | |
| 422 | alt_num_cnt = 0; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | int dfu_config_entities(char *env, char *interface, int num) |
| 426 | { |
| 427 | struct dfu_entity *dfu; |
| 428 | int i, ret; |
| 429 | char *s; |
| 430 | |
| 431 | dfu_alt_num = dfu_find_alt_num(env); |
| 432 | debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num); |
| 433 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 434 | dfu_hash_algo = NULL; |
| 435 | s = dfu_get_hash_algo(); |
| 436 | if (s) { |
| 437 | ret = hash_lookup_algo(s, &dfu_hash_algo); |
| 438 | if (ret) |
| 439 | error("Hash algorithm %s not supported\n", s); |
| 440 | } |
| 441 | |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 442 | dfu = calloc(sizeof(*dfu), dfu_alt_num); |
| 443 | if (!dfu) |
| 444 | return -1; |
| 445 | for (i = 0; i < dfu_alt_num; i++) { |
| 446 | |
| 447 | s = strsep(&env, ";"); |
Przemyslaw Marczak | b627eb4 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 448 | ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 449 | if (ret) |
| 450 | return -1; |
| 451 | |
| 452 | list_add_tail(&dfu[i].list, &dfu_list); |
Przemyslaw Marczak | b627eb4 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 453 | alt_num_cnt++; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | const char *dfu_get_dev_type(enum dfu_device_type t) |
| 460 | { |
Afzal Mohammed | a9479f0 | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 461 | const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" }; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 462 | return dev_t[t]; |
| 463 | } |
| 464 | |
| 465 | const char *dfu_get_layout(enum dfu_layout l) |
| 466 | { |
| 467 | const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2", |
Afzal Mohammed | a9479f0 | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 468 | "EXT3", "EXT4", "RAM_ADDR" }; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 469 | return dfu_layout[l]; |
| 470 | } |
| 471 | |
| 472 | void dfu_show_entities(void) |
| 473 | { |
| 474 | struct dfu_entity *dfu; |
| 475 | |
| 476 | puts("DFU alt settings list:\n"); |
| 477 | |
| 478 | list_for_each_entry(dfu, &dfu_list, list) { |
| 479 | printf("dev: %s alt: %d name: %s layout: %s\n", |
| 480 | dfu_get_dev_type(dfu->dev_type), dfu->alt, |
| 481 | dfu->name, dfu_get_layout(dfu->layout)); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | int dfu_get_alt_number(void) |
| 486 | { |
| 487 | return dfu_alt_num; |
| 488 | } |
| 489 | |
| 490 | struct dfu_entity *dfu_get_entity(int alt) |
| 491 | { |
| 492 | struct dfu_entity *dfu; |
| 493 | |
| 494 | list_for_each_entry(dfu, &dfu_list, list) { |
| 495 | if (dfu->alt == alt) |
| 496 | return dfu; |
| 497 | } |
| 498 | |
| 499 | return NULL; |
| 500 | } |
Lukasz Majewski | fed936e | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 501 | |
| 502 | int dfu_get_alt(char *name) |
| 503 | { |
| 504 | struct dfu_entity *dfu; |
| 505 | |
| 506 | list_for_each_entry(dfu, &dfu_list, list) { |
| 507 | if (!strncmp(dfu->name, name, strlen(dfu->name))) |
| 508 | return dfu->alt; |
| 509 | } |
| 510 | |
| 511 | return -ENODEV; |
| 512 | } |