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