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