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