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 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <common.h> |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 23 | #include <errno.h> |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 24 | #include <malloc.h> |
| 25 | #include <mmc.h> |
| 26 | #include <fat.h> |
| 27 | #include <dfu.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/compiler.h> |
| 30 | |
| 31 | static LIST_HEAD(dfu_list); |
| 32 | static int dfu_alt_num; |
| 33 | |
| 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 | |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 45 | static unsigned char *dfu_buf; |
| 46 | static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE; |
| 47 | |
| 48 | static unsigned char *dfu_free_buf(void) |
| 49 | { |
| 50 | free(dfu_buf); |
| 51 | dfu_buf = NULL; |
| 52 | return dfu_buf; |
| 53 | } |
| 54 | |
| 55 | static unsigned char *dfu_get_buf(void) |
| 56 | { |
| 57 | char *s; |
| 58 | |
| 59 | if (dfu_buf != NULL) |
| 60 | return dfu_buf; |
| 61 | |
| 62 | s = getenv("dfu_bufsiz"); |
| 63 | dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) : |
| 64 | CONFIG_SYS_DFU_DATA_BUF_SIZE; |
| 65 | |
| 66 | dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); |
| 67 | if (dfu_buf == NULL) |
| 68 | printf("%s: Could not memalign 0x%lx bytes\n", |
| 69 | __func__, dfu_buf_size); |
| 70 | |
| 71 | return dfu_buf; |
| 72 | } |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 73 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 74 | static int dfu_write_buffer_drain(struct dfu_entity *dfu) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 75 | { |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 76 | long w_size; |
| 77 | int ret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 78 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 79 | /* flush size? */ |
| 80 | w_size = dfu->i_buf - dfu->i_buf_start; |
| 81 | if (w_size == 0) |
| 82 | return 0; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 83 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 84 | /* update CRC32 */ |
| 85 | dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 86 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 87 | ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size); |
| 88 | if (ret) |
| 89 | debug("%s: Write error!\n", __func__); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 90 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 91 | /* point back */ |
| 92 | dfu->i_buf = dfu->i_buf_start; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 93 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 94 | /* update offset */ |
| 95 | dfu->offset += w_size; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 96 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 97 | puts("#"); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 102 | 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] | 103 | { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 104 | int ret = 0; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 105 | int tret; |
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 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n", |
| 108 | __func__, dfu->name, buf, size, blk_seq_num, dfu->offset, |
| 109 | dfu->i_buf - dfu->i_buf_start); |
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 | if (!dfu->inited) { |
| 112 | /* initial state */ |
| 113 | dfu->crc = 0; |
| 114 | dfu->offset = 0; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 115 | dfu->bad_skip = 0; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 116 | dfu->i_blk_seq_num = 0; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 117 | dfu->i_buf_start = dfu_get_buf(); |
| 118 | if (dfu->i_buf_start == NULL) |
| 119 | return -ENOMEM; |
| 120 | dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 121 | dfu->i_buf = dfu->i_buf_start; |
| 122 | |
| 123 | dfu->inited = 1; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 126 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 127 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 128 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 132 | /* DFU 1.1 standard says: |
| 133 | * The wBlockNum field is a block sequence number. It increments each |
| 134 | * time a block is transferred, wrapping to zero from 65,535. It is used |
| 135 | * to provide useful context to the DFU loader in the device." |
| 136 | * |
| 137 | * This means that it's a 16 bit counter that roll-overs at |
| 138 | * 0xffff -> 0x0000. By having a typical 4K transfer block |
| 139 | * we roll-over at exactly 256MB. Not very fun to debug. |
| 140 | * |
| 141 | * Handling rollover, and having an inited variable, |
| 142 | * makes things work. |
| 143 | */ |
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 | /* handle rollover */ |
| 146 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 147 | |
| 148 | /* flush buffer if overflow */ |
| 149 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
| 150 | tret = dfu_write_buffer_drain(dfu); |
| 151 | if (ret == 0) |
| 152 | ret = tret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 153 | } |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 154 | |
| 155 | /* we should be in buffer now (if not then size too large) */ |
| 156 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
| 157 | printf("%s: Wrong size! [%d] [%d] - %d\n", |
| 158 | __func__, dfu->i_blk_seq_num, blk_seq_num, size); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | memcpy(dfu->i_buf, buf, size); |
| 163 | dfu->i_buf += size; |
| 164 | |
| 165 | /* if end or if buffer full flush */ |
| 166 | if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { |
| 167 | tret = dfu_write_buffer_drain(dfu); |
| 168 | if (ret == 0) |
| 169 | ret = tret; |
| 170 | } |
| 171 | |
| 172 | /* end? */ |
| 173 | if (size == 0) { |
| 174 | /* Now try and flush to the medium if needed. */ |
| 175 | if (dfu->flush_medium) |
| 176 | ret = dfu->flush_medium(dfu); |
| 177 | printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc); |
| 178 | |
| 179 | /* clear everything */ |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 180 | dfu_free_buf(); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 181 | dfu->crc = 0; |
| 182 | dfu->offset = 0; |
| 183 | dfu->i_blk_seq_num = 0; |
| 184 | dfu->i_buf_start = dfu_buf; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 185 | dfu->i_buf_end = dfu_buf; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 186 | dfu->i_buf = dfu->i_buf_start; |
| 187 | |
| 188 | dfu->inited = 0; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | return ret = 0 ? size : ret; |
| 193 | } |
| 194 | |
| 195 | static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) |
| 196 | { |
| 197 | long chunk; |
| 198 | int ret, readn; |
| 199 | |
| 200 | readn = 0; |
| 201 | while (size > 0) { |
| 202 | /* get chunk that can be read */ |
| 203 | chunk = min(size, dfu->b_left); |
| 204 | /* consume */ |
| 205 | if (chunk > 0) { |
| 206 | memcpy(buf, dfu->i_buf, chunk); |
| 207 | dfu->crc = crc32(dfu->crc, buf, chunk); |
| 208 | dfu->i_buf += chunk; |
| 209 | dfu->b_left -= chunk; |
| 210 | size -= chunk; |
| 211 | buf += chunk; |
| 212 | readn += chunk; |
| 213 | } |
| 214 | |
| 215 | /* all done */ |
| 216 | if (size > 0) { |
| 217 | /* no more to read */ |
| 218 | if (dfu->r_left == 0) |
| 219 | break; |
| 220 | |
| 221 | dfu->i_buf = dfu->i_buf_start; |
| 222 | dfu->b_left = dfu->i_buf_end - dfu->i_buf_start; |
| 223 | |
| 224 | /* got to read, but buffer is empty */ |
| 225 | if (dfu->b_left > dfu->r_left) |
| 226 | dfu->b_left = dfu->r_left; |
| 227 | ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf, |
| 228 | &dfu->b_left); |
| 229 | if (ret != 0) { |
| 230 | debug("%s: Read error!\n", __func__); |
| 231 | return ret; |
| 232 | } |
| 233 | dfu->offset += dfu->b_left; |
| 234 | dfu->r_left -= dfu->b_left; |
| 235 | |
| 236 | puts("#"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return readn; |
| 241 | } |
| 242 | |
| 243 | int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 244 | { |
| 245 | int ret = 0; |
| 246 | |
| 247 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n", |
| 248 | __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); |
| 249 | |
| 250 | if (!dfu->inited) { |
Łukasz Majewski | 9df4955 | 2013-06-28 18:41:50 +0200 | [diff] [blame^] | 251 | dfu->i_buf_start = dfu_get_buf(); |
| 252 | if (dfu->i_buf_start == NULL) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | 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] | 256 | if (ret != 0) { |
| 257 | debug("%s: failed to get r_left\n", __func__); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left); |
| 262 | |
| 263 | dfu->i_blk_seq_num = 0; |
| 264 | dfu->crc = 0; |
| 265 | dfu->offset = 0; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 266 | dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 267 | dfu->i_buf = dfu->i_buf_start; |
| 268 | dfu->b_left = 0; |
| 269 | |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 270 | dfu->bad_skip = 0; |
| 271 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 272 | dfu->inited = 1; |
| 273 | } |
| 274 | |
| 275 | if (dfu->i_blk_seq_num != blk_seq_num) { |
| 276 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
| 277 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
| 278 | return -1; |
| 279 | } |
| 280 | /* handle rollover */ |
| 281 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 282 | |
| 283 | ret = dfu_read_buffer_fill(dfu, buf, size); |
| 284 | if (ret < 0) { |
| 285 | printf("%s: Failed to fill buffer\n", __func__); |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | if (ret < size) { |
| 290 | debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc); |
| 291 | puts("\nUPLOAD ... done\nCtrl+C to exit ...\n"); |
| 292 | |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 293 | dfu_free_buf(); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 294 | dfu->i_blk_seq_num = 0; |
| 295 | dfu->crc = 0; |
| 296 | dfu->offset = 0; |
| 297 | dfu->i_buf_start = dfu_buf; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 298 | dfu->i_buf_end = dfu_buf; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 299 | dfu->i_buf = dfu->i_buf_start; |
| 300 | dfu->b_left = 0; |
| 301 | |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 302 | dfu->bad_skip = 0; |
| 303 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 304 | dfu->inited = 0; |
| 305 | } |
| 306 | |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, |
| 311 | char *interface, int num) |
| 312 | { |
| 313 | char *st; |
| 314 | |
| 315 | debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num); |
| 316 | st = strsep(&s, " "); |
| 317 | strcpy(dfu->name, st); |
| 318 | |
| 319 | dfu->dev_num = num; |
| 320 | dfu->alt = alt; |
| 321 | |
| 322 | /* Specific for mmc device */ |
| 323 | if (strcmp(interface, "mmc") == 0) { |
| 324 | if (dfu_fill_entity_mmc(dfu, s)) |
| 325 | return -1; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 326 | } else if (strcmp(interface, "nand") == 0) { |
| 327 | if (dfu_fill_entity_nand(dfu, s)) |
| 328 | return -1; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 329 | } else { |
| 330 | printf("%s: Device %s not (yet) supported!\n", |
| 331 | __func__, interface); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | void dfu_free_entities(void) |
| 339 | { |
| 340 | struct dfu_entity *dfu, *p, *t = NULL; |
| 341 | |
| 342 | list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { |
| 343 | list_del(&dfu->list); |
| 344 | t = dfu; |
| 345 | } |
| 346 | if (t) |
| 347 | free(t); |
| 348 | INIT_LIST_HEAD(&dfu_list); |
| 349 | } |
| 350 | |
| 351 | int dfu_config_entities(char *env, char *interface, int num) |
| 352 | { |
| 353 | struct dfu_entity *dfu; |
| 354 | int i, ret; |
| 355 | char *s; |
| 356 | |
| 357 | dfu_alt_num = dfu_find_alt_num(env); |
| 358 | debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num); |
| 359 | |
| 360 | dfu = calloc(sizeof(*dfu), dfu_alt_num); |
| 361 | if (!dfu) |
| 362 | return -1; |
| 363 | for (i = 0; i < dfu_alt_num; i++) { |
| 364 | |
| 365 | s = strsep(&env, ";"); |
| 366 | ret = dfu_fill_entity(&dfu[i], s, i, interface, num); |
| 367 | if (ret) |
| 368 | return -1; |
| 369 | |
| 370 | list_add_tail(&dfu[i].list, &dfu_list); |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | const char *dfu_get_dev_type(enum dfu_device_type t) |
| 377 | { |
| 378 | const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND" }; |
| 379 | return dev_t[t]; |
| 380 | } |
| 381 | |
| 382 | const char *dfu_get_layout(enum dfu_layout l) |
| 383 | { |
| 384 | const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2", |
| 385 | "EXT3", "EXT4" }; |
| 386 | return dfu_layout[l]; |
| 387 | } |
| 388 | |
| 389 | void dfu_show_entities(void) |
| 390 | { |
| 391 | struct dfu_entity *dfu; |
| 392 | |
| 393 | puts("DFU alt settings list:\n"); |
| 394 | |
| 395 | list_for_each_entry(dfu, &dfu_list, list) { |
| 396 | printf("dev: %s alt: %d name: %s layout: %s\n", |
| 397 | dfu_get_dev_type(dfu->dev_type), dfu->alt, |
| 398 | dfu->name, dfu_get_layout(dfu->layout)); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | int dfu_get_alt_number(void) |
| 403 | { |
| 404 | return dfu_alt_num; |
| 405 | } |
| 406 | |
| 407 | struct dfu_entity *dfu_get_entity(int alt) |
| 408 | { |
| 409 | struct dfu_entity *dfu; |
| 410 | |
| 411 | list_for_each_entry(dfu, &dfu_list, list) { |
| 412 | if (dfu->alt == alt) |
| 413 | return dfu; |
| 414 | } |
| 415 | |
| 416 | return NULL; |
| 417 | } |