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