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