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