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