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