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 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 10 #include <common.h> 11 #include <errno.h> 12 #include <malloc.h> 13 #include <mmc.h> 14 #include <fat.h> 15 #include <dfu.h> 16 #include <hash.h> 17 #include <linux/list.h> 18 #include <linux/compiler.h> 19 20 static bool dfu_detach_request; 21 static LIST_HEAD(dfu_list); 22 static int dfu_alt_num; 23 static int alt_num_cnt; 24 static struct hash_algo *dfu_hash_algo; 25 26 /* 27 * The purpose of the dfu_usb_get_reset() function is to 28 * provide information if after USB_DETACH request 29 * being sent the dfu-util performed reset of USB 30 * bus. 31 * 32 * Described behaviour is the only way to distinct if 33 * user has typed -e (detach) or -R (reset) when invoking 34 * dfu-util command. 35 * 36 */ 37 __weak bool dfu_usb_get_reset(void) 38 { 39 return true; 40 } 41 42 bool dfu_detach(void) 43 { 44 return dfu_detach_request; 45 } 46 47 void dfu_trigger_detach(void) 48 { 49 dfu_detach_request = true; 50 } 51 52 void dfu_clear_detach(void) 53 { 54 dfu_detach_request = false; 55 } 56 57 static int dfu_find_alt_num(const char *s) 58 { 59 int i = 0; 60 61 for (; *s; s++) 62 if (*s == ';') 63 i++; 64 65 return ++i; 66 } 67 68 int dfu_init_env_entities(char *interface, char *devstr) 69 { 70 const char *str_env; 71 char *env_bkp; 72 int ret; 73 74 str_env = getenv("dfu_alt_info"); 75 if (!str_env) { 76 error("\"dfu_alt_info\" env variable not defined!\n"); 77 return -EINVAL; 78 } 79 80 env_bkp = strdup(str_env); 81 ret = dfu_config_entities(env_bkp, interface, devstr); 82 if (ret) { 83 error("DFU entities configuration failed!\n"); 84 return ret; 85 } 86 87 free(env_bkp); 88 return 0; 89 } 90 91 static unsigned char *dfu_buf; 92 static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE; 93 94 unsigned char *dfu_free_buf(void) 95 { 96 free(dfu_buf); 97 dfu_buf = NULL; 98 return dfu_buf; 99 } 100 101 unsigned long dfu_get_buf_size(void) 102 { 103 return dfu_buf_size; 104 } 105 106 unsigned char *dfu_get_buf(struct dfu_entity *dfu) 107 { 108 char *s; 109 110 if (dfu_buf != NULL) 111 return dfu_buf; 112 113 s = getenv("dfu_bufsiz"); 114 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) : 115 CONFIG_SYS_DFU_DATA_BUF_SIZE; 116 if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size) 117 dfu_buf_size = dfu->max_buf_size; 118 119 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); 120 if (dfu_buf == NULL) 121 printf("%s: Could not memalign 0x%lx bytes\n", 122 __func__, dfu_buf_size); 123 124 return dfu_buf; 125 } 126 127 static char *dfu_get_hash_algo(void) 128 { 129 char *s; 130 131 s = getenv("dfu_hash_algo"); 132 if (!s) 133 return NULL; 134 135 if (!strcmp(s, "crc32")) { 136 debug("%s: DFU hash method: %s\n", __func__, s); 137 return s; 138 } 139 140 error("DFU hash method: %s not supported!\n", s); 141 return NULL; 142 } 143 144 static int dfu_write_buffer_drain(struct dfu_entity *dfu) 145 { 146 long w_size; 147 int ret; 148 149 /* flush size? */ 150 w_size = dfu->i_buf - dfu->i_buf_start; 151 if (w_size == 0) 152 return 0; 153 154 if (dfu_hash_algo) 155 dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc, 156 dfu->i_buf_start, w_size, 0); 157 158 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size); 159 if (ret) 160 debug("%s: Write error!\n", __func__); 161 162 /* point back */ 163 dfu->i_buf = dfu->i_buf_start; 164 165 /* update offset */ 166 dfu->offset += w_size; 167 168 puts("#"); 169 170 return ret; 171 } 172 173 void dfu_write_transaction_cleanup(struct dfu_entity *dfu) 174 { 175 /* clear everything */ 176 dfu_free_buf(); 177 dfu->crc = 0; 178 dfu->offset = 0; 179 dfu->i_blk_seq_num = 0; 180 dfu->i_buf_start = dfu_buf; 181 dfu->i_buf_end = dfu_buf; 182 dfu->i_buf = dfu->i_buf_start; 183 dfu->inited = 0; 184 } 185 186 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) 187 { 188 int ret = 0; 189 190 ret = dfu_write_buffer_drain(dfu); 191 if (ret) 192 return ret; 193 194 if (dfu->flush_medium) 195 ret = dfu->flush_medium(dfu); 196 197 if (dfu_hash_algo) 198 printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name, 199 dfu->crc); 200 201 dfu_write_transaction_cleanup(dfu); 202 203 return ret; 204 } 205 206 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) 207 { 208 int ret; 209 210 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n", 211 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset, 212 dfu->i_buf - dfu->i_buf_start); 213 214 if (!dfu->inited) { 215 /* initial state */ 216 dfu->crc = 0; 217 dfu->offset = 0; 218 dfu->bad_skip = 0; 219 dfu->i_blk_seq_num = 0; 220 dfu->i_buf_start = dfu_get_buf(dfu); 221 if (dfu->i_buf_start == NULL) 222 return -ENOMEM; 223 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size; 224 dfu->i_buf = dfu->i_buf_start; 225 226 dfu->inited = 1; 227 } 228 229 if (dfu->i_blk_seq_num != blk_seq_num) { 230 printf("%s: Wrong sequence number! [%d] [%d]\n", 231 __func__, dfu->i_blk_seq_num, blk_seq_num); 232 dfu_write_transaction_cleanup(dfu); 233 return -1; 234 } 235 236 /* DFU 1.1 standard says: 237 * The wBlockNum field is a block sequence number. It increments each 238 * time a block is transferred, wrapping to zero from 65,535. It is used 239 * to provide useful context to the DFU loader in the device." 240 * 241 * This means that it's a 16 bit counter that roll-overs at 242 * 0xffff -> 0x0000. By having a typical 4K transfer block 243 * we roll-over at exactly 256MB. Not very fun to debug. 244 * 245 * Handling rollover, and having an inited variable, 246 * makes things work. 247 */ 248 249 /* handle rollover */ 250 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; 251 252 /* flush buffer if overflow */ 253 if ((dfu->i_buf + size) > dfu->i_buf_end) { 254 ret = dfu_write_buffer_drain(dfu); 255 if (ret) { 256 dfu_write_transaction_cleanup(dfu); 257 return ret; 258 } 259 } 260 261 /* we should be in buffer now (if not then size too large) */ 262 if ((dfu->i_buf + size) > dfu->i_buf_end) { 263 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf, 264 size, dfu->i_buf_end); 265 dfu_write_transaction_cleanup(dfu); 266 return -1; 267 } 268 269 memcpy(dfu->i_buf, buf, size); 270 dfu->i_buf += size; 271 272 /* if end or if buffer full flush */ 273 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { 274 ret = dfu_write_buffer_drain(dfu); 275 if (ret) { 276 dfu_write_transaction_cleanup(dfu); 277 return ret; 278 } 279 } 280 281 return 0; 282 } 283 284 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) 285 { 286 long chunk; 287 int ret, readn; 288 289 readn = 0; 290 while (size > 0) { 291 /* get chunk that can be read */ 292 chunk = min((long)size, dfu->b_left); 293 /* consume */ 294 if (chunk > 0) { 295 memcpy(buf, dfu->i_buf, chunk); 296 if (dfu_hash_algo) 297 dfu_hash_algo->hash_update(dfu_hash_algo, 298 &dfu->crc, buf, 299 chunk, 0); 300 301 dfu->i_buf += chunk; 302 dfu->b_left -= chunk; 303 size -= chunk; 304 buf += chunk; 305 readn += chunk; 306 } 307 308 /* all done */ 309 if (size > 0) { 310 /* no more to read */ 311 if (dfu->r_left == 0) 312 break; 313 314 dfu->i_buf = dfu->i_buf_start; 315 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start; 316 317 /* got to read, but buffer is empty */ 318 if (dfu->b_left > dfu->r_left) 319 dfu->b_left = dfu->r_left; 320 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf, 321 &dfu->b_left); 322 if (ret != 0) { 323 debug("%s: Read error!\n", __func__); 324 return ret; 325 } 326 dfu->offset += dfu->b_left; 327 dfu->r_left -= dfu->b_left; 328 329 puts("#"); 330 } 331 } 332 333 return readn; 334 } 335 336 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) 337 { 338 int ret = 0; 339 340 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n", 341 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); 342 343 if (!dfu->inited) { 344 dfu->i_buf_start = dfu_get_buf(dfu); 345 if (dfu->i_buf_start == NULL) 346 return -ENOMEM; 347 348 dfu->r_left = dfu->get_medium_size(dfu); 349 if (dfu->r_left < 0) 350 return dfu->r_left; 351 switch (dfu->layout) { 352 case DFU_RAW_ADDR: 353 case DFU_RAM_ADDR: 354 break; 355 default: 356 if (dfu->r_left > dfu_buf_size) { 357 printf("%s: File too big for buffer\n", 358 __func__); 359 return -EOVERFLOW; 360 } 361 } 362 363 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left); 364 365 dfu->i_blk_seq_num = 0; 366 dfu->crc = 0; 367 dfu->offset = 0; 368 dfu->i_buf_end = dfu_get_buf(dfu) + dfu_buf_size; 369 dfu->i_buf = dfu->i_buf_start; 370 dfu->b_left = 0; 371 372 dfu->bad_skip = 0; 373 374 dfu->inited = 1; 375 } 376 377 if (dfu->i_blk_seq_num != blk_seq_num) { 378 printf("%s: Wrong sequence number! [%d] [%d]\n", 379 __func__, dfu->i_blk_seq_num, blk_seq_num); 380 return -1; 381 } 382 /* handle rollover */ 383 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; 384 385 ret = dfu_read_buffer_fill(dfu, buf, size); 386 if (ret < 0) { 387 printf("%s: Failed to fill buffer\n", __func__); 388 return -1; 389 } 390 391 if (ret < size) { 392 if (dfu_hash_algo) 393 debug("%s: %s %s: 0x%x\n", __func__, dfu->name, 394 dfu_hash_algo->name, dfu->crc); 395 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n"); 396 397 dfu_free_buf(); 398 dfu->i_blk_seq_num = 0; 399 dfu->crc = 0; 400 dfu->offset = 0; 401 dfu->i_buf_start = dfu_buf; 402 dfu->i_buf_end = dfu_buf; 403 dfu->i_buf = dfu->i_buf_start; 404 dfu->b_left = 0; 405 406 dfu->bad_skip = 0; 407 408 dfu->inited = 0; 409 } 410 411 return ret; 412 } 413 414 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, 415 char *interface, char *devstr) 416 { 417 char *st; 418 419 debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr); 420 st = strsep(&s, " "); 421 strcpy(dfu->name, st); 422 423 dfu->alt = alt; 424 dfu->max_buf_size = 0; 425 dfu->free_entity = NULL; 426 427 /* Specific for mmc device */ 428 if (strcmp(interface, "mmc") == 0) { 429 if (dfu_fill_entity_mmc(dfu, devstr, s)) 430 return -1; 431 } else if (strcmp(interface, "nand") == 0) { 432 if (dfu_fill_entity_nand(dfu, devstr, s)) 433 return -1; 434 } else if (strcmp(interface, "ram") == 0) { 435 if (dfu_fill_entity_ram(dfu, devstr, s)) 436 return -1; 437 } else if (strcmp(interface, "sf") == 0) { 438 if (dfu_fill_entity_sf(dfu, devstr, s)) 439 return -1; 440 } else { 441 printf("%s: Device %s not (yet) supported!\n", 442 __func__, interface); 443 return -1; 444 } 445 446 return 0; 447 } 448 449 void dfu_free_entities(void) 450 { 451 struct dfu_entity *dfu, *p, *t = NULL; 452 453 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { 454 list_del(&dfu->list); 455 if (dfu->free_entity) 456 dfu->free_entity(dfu); 457 t = dfu; 458 } 459 if (t) 460 free(t); 461 INIT_LIST_HEAD(&dfu_list); 462 463 alt_num_cnt = 0; 464 } 465 466 int dfu_config_entities(char *env, char *interface, char *devstr) 467 { 468 struct dfu_entity *dfu; 469 int i, ret; 470 char *s; 471 472 dfu_alt_num = dfu_find_alt_num(env); 473 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num); 474 475 dfu_hash_algo = NULL; 476 s = dfu_get_hash_algo(); 477 if (s) { 478 ret = hash_lookup_algo(s, &dfu_hash_algo); 479 if (ret) 480 error("Hash algorithm %s not supported\n", s); 481 } 482 483 dfu = calloc(sizeof(*dfu), dfu_alt_num); 484 if (!dfu) 485 return -1; 486 for (i = 0; i < dfu_alt_num; i++) { 487 488 s = strsep(&env, ";"); 489 ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, 490 devstr); 491 if (ret) 492 return -1; 493 494 list_add_tail(&dfu[i].list, &dfu_list); 495 alt_num_cnt++; 496 } 497 498 return 0; 499 } 500 501 const char *dfu_get_dev_type(enum dfu_device_type t) 502 { 503 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" }; 504 return dev_t[t]; 505 } 506 507 const char *dfu_get_layout(enum dfu_layout l) 508 { 509 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2", 510 "EXT3", "EXT4", "RAM_ADDR" }; 511 return dfu_layout[l]; 512 } 513 514 void dfu_show_entities(void) 515 { 516 struct dfu_entity *dfu; 517 518 puts("DFU alt settings list:\n"); 519 520 list_for_each_entry(dfu, &dfu_list, list) { 521 printf("dev: %s alt: %d name: %s layout: %s\n", 522 dfu_get_dev_type(dfu->dev_type), dfu->alt, 523 dfu->name, dfu_get_layout(dfu->layout)); 524 } 525 } 526 527 int dfu_get_alt_number(void) 528 { 529 return dfu_alt_num; 530 } 531 532 struct dfu_entity *dfu_get_entity(int alt) 533 { 534 struct dfu_entity *dfu; 535 536 list_for_each_entry(dfu, &dfu_list, list) { 537 if (dfu->alt == alt) 538 return dfu; 539 } 540 541 return NULL; 542 } 543 544 int dfu_get_alt(char *name) 545 { 546 struct dfu_entity *dfu; 547 548 list_for_each_entry(dfu, &dfu_list, list) { 549 if (!strncmp(dfu->name, name, strlen(dfu->name))) 550 return dfu->alt; 551 } 552 553 return -ENODEV; 554 } 555