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