blob: 1aced26c4e8824995ac29b540fcacf290b07b9d8 [file] [log] [blame]
Lukasz Majewskid311c6e2012-08-06 14:41:07 +02001/*
2 * dfu.c -- DFU back-end routines
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskid311c6e2012-08-06 14:41:07 +02008 */
9
10#include <common.h>
Heiko Schochera2f831e2013-06-12 06:05:51 +020011#include <errno.h>
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020012#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
Lukasz Majewski2968aa22013-07-18 13:19:14 +020019static bool dfu_reset_request;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020020static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
22
Lukasz Majewski2968aa22013-07-18 13:19:14 +020023bool dfu_reset(void)
24{
25 return dfu_reset_request;
26}
27
28void dfu_trigger_reset()
29{
30 dfu_reset_request = true;
31}
32
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020033static 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
Lukasz Majewski35e591e2013-09-11 14:53:35 +020044int 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
Heiko Schochera2f831e2013-06-12 06:05:51 +020067static unsigned char *dfu_buf;
68static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
69
Lukasz Majewski34264232013-10-08 14:30:39 +020070unsigned char *dfu_free_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020071{
72 free(dfu_buf);
73 dfu_buf = NULL;
74 return dfu_buf;
75}
76
Lukasz Majewski031d4132013-12-09 16:20:13 +010077unsigned long dfu_get_buf_size(void)
78{
79 return dfu_buf_size;
80}
81
Lukasz Majewski34264232013-10-08 14:30:39 +020082unsigned char *dfu_get_buf(void)
Heiko Schochera2f831e2013-06-12 06:05:51 +020083{
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}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200100
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000101static 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
Heiko Schocher941b3842014-03-18 08:09:55 +0100129int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
130{
131 int ret = 0;
132
133 if (dfu->flush_medium)
134 ret = dfu->flush_medium(dfu);
135
136 printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
137
138 /* clear everything */
139 dfu_free_buf();
140 dfu->crc = 0;
141 dfu->offset = 0;
142 dfu->i_blk_seq_num = 0;
143 dfu->i_buf_start = dfu_buf;
144 dfu->i_buf_end = dfu_buf;
145 dfu->i_buf = dfu->i_buf_start;
146 dfu->inited = 0;
147
148 return ret;
149}
150
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200151int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
152{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200153 int ret = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000154 int tret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200155
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000156 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
157 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
158 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200159
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000160 if (!dfu->inited) {
161 /* initial state */
162 dfu->crc = 0;
163 dfu->offset = 0;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000164 dfu->bad_skip = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000165 dfu->i_blk_seq_num = 0;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200166 dfu->i_buf_start = dfu_get_buf();
167 if (dfu->i_buf_start == NULL)
168 return -ENOMEM;
169 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000170 dfu->i_buf = dfu->i_buf_start;
171
172 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200173 }
174
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000175 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200176 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000177 __func__, dfu->i_blk_seq_num, blk_seq_num);
178 return -1;
179 }
180
181 /* DFU 1.1 standard says:
182 * The wBlockNum field is a block sequence number. It increments each
183 * time a block is transferred, wrapping to zero from 65,535. It is used
184 * to provide useful context to the DFU loader in the device."
185 *
186 * This means that it's a 16 bit counter that roll-overs at
187 * 0xffff -> 0x0000. By having a typical 4K transfer block
188 * we roll-over at exactly 256MB. Not very fun to debug.
189 *
190 * Handling rollover, and having an inited variable,
191 * makes things work.
192 */
193
194 /* handle rollover */
195 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
196
197 /* flush buffer if overflow */
198 if ((dfu->i_buf + size) > dfu->i_buf_end) {
199 tret = dfu_write_buffer_drain(dfu);
200 if (ret == 0)
201 ret = tret;
202 }
203
204 /* we should be in buffer now (if not then size too large) */
205 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewski385c3212013-09-10 15:29:22 +0200206 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
207 size, dfu->i_buf_end);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200208 return -1;
209 }
210
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000211 memcpy(dfu->i_buf, buf, size);
212 dfu->i_buf += size;
213
214 /* if end or if buffer full flush */
215 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
216 tret = dfu_write_buffer_drain(dfu);
217 if (ret == 0)
218 ret = tret;
219 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200220
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000221 /* end? */
Heiko Schocher941b3842014-03-18 08:09:55 +0100222 if (size == 0)
223 ret = dfu_flush(dfu, buf, size, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200224
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000225 return ret = 0 ? size : ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200226}
227
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000228static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
229{
230 long chunk;
231 int ret, readn;
232
233 readn = 0;
234 while (size > 0) {
235 /* get chunk that can be read */
236 chunk = min(size, dfu->b_left);
237 /* consume */
238 if (chunk > 0) {
239 memcpy(buf, dfu->i_buf, chunk);
240 dfu->crc = crc32(dfu->crc, buf, chunk);
241 dfu->i_buf += chunk;
242 dfu->b_left -= chunk;
Bo Shenf1f2af52013-10-16 17:26:37 +0800243 dfu->r_left -= chunk;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000244 size -= chunk;
245 buf += chunk;
246 readn += chunk;
247 }
248
249 /* all done */
250 if (size > 0) {
251 /* no more to read */
252 if (dfu->r_left == 0)
253 break;
254
255 dfu->i_buf = dfu->i_buf_start;
256 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
257
258 /* got to read, but buffer is empty */
259 if (dfu->b_left > dfu->r_left)
260 dfu->b_left = dfu->r_left;
261 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
262 &dfu->b_left);
263 if (ret != 0) {
264 debug("%s: Read error!\n", __func__);
265 return ret;
266 }
267 dfu->offset += dfu->b_left;
268 dfu->r_left -= dfu->b_left;
269
270 puts("#");
271 }
272 }
273
274 return readn;
275}
276
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200277int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
278{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200279 int ret = 0;
280
281 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000282 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
283
284 if (!dfu->inited) {
Łukasz Majewski063ae5a2013-06-28 18:41:50 +0200285 dfu->i_buf_start = dfu_get_buf();
286 if (dfu->i_buf_start == NULL)
287 return -ENOMEM;
288
289 ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000290 if (ret != 0) {
291 debug("%s: failed to get r_left\n", __func__);
292 return ret;
293 }
294
295 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
296
297 dfu->i_blk_seq_num = 0;
298 dfu->crc = 0;
299 dfu->offset = 0;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200300 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000301 dfu->i_buf = dfu->i_buf_start;
Bo Shen247b98f2013-10-16 17:26:38 +0800302 dfu->b_left = min(dfu_buf_size, dfu->r_left);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200303
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000304 dfu->bad_skip = 0;
305
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000306 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200307 }
308
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000309 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200310 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000311 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200312 return -1;
313 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000314 /* handle rollover */
315 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200316
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000317 ret = dfu_read_buffer_fill(dfu, buf, size);
318 if (ret < 0) {
319 printf("%s: Failed to fill buffer\n", __func__);
320 return -1;
321 }
322
323 if (ret < size) {
324 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
325 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200326
Heiko Schochera2f831e2013-06-12 06:05:51 +0200327 dfu_free_buf();
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000328 dfu->i_blk_seq_num = 0;
329 dfu->crc = 0;
330 dfu->offset = 0;
331 dfu->i_buf_start = dfu_buf;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200332 dfu->i_buf_end = dfu_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000333 dfu->i_buf = dfu->i_buf_start;
334 dfu->b_left = 0;
335
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000336 dfu->bad_skip = 0;
337
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000338 dfu->inited = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200339 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000340
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200341 return ret;
342}
343
344static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Mateusz Zalegad862f892013-10-04 19:22:26 +0200345 char *interface, int num)
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200346{
347 char *st;
348
349 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
350 st = strsep(&s, " ");
351 strcpy(dfu->name, st);
352
353 dfu->dev_num = num;
354 dfu->alt = alt;
355
356 /* Specific for mmc device */
357 if (strcmp(interface, "mmc") == 0) {
358 if (dfu_fill_entity_mmc(dfu, s))
359 return -1;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000360 } else if (strcmp(interface, "nand") == 0) {
361 if (dfu_fill_entity_nand(dfu, s))
362 return -1;
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530363 } else if (strcmp(interface, "ram") == 0) {
364 if (dfu_fill_entity_ram(dfu, s))
365 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200366 } else {
367 printf("%s: Device %s not (yet) supported!\n",
368 __func__, interface);
369 return -1;
370 }
371
372 return 0;
373}
374
375void dfu_free_entities(void)
376{
377 struct dfu_entity *dfu, *p, *t = NULL;
378
379 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
380 list_del(&dfu->list);
381 t = dfu;
382 }
383 if (t)
384 free(t);
385 INIT_LIST_HEAD(&dfu_list);
386}
387
388int dfu_config_entities(char *env, char *interface, int num)
389{
390 struct dfu_entity *dfu;
391 int i, ret;
392 char *s;
393
394 dfu_alt_num = dfu_find_alt_num(env);
395 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
396
397 dfu = calloc(sizeof(*dfu), dfu_alt_num);
398 if (!dfu)
399 return -1;
400 for (i = 0; i < dfu_alt_num; i++) {
401
402 s = strsep(&env, ";");
403 ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
404 if (ret)
405 return -1;
406
407 list_add_tail(&dfu[i].list, &dfu_list);
408 }
409
410 return 0;
411}
412
413const char *dfu_get_dev_type(enum dfu_device_type t)
414{
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530415 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200416 return dev_t[t];
417}
418
419const char *dfu_get_layout(enum dfu_layout l)
420{
421 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammede3c687a2013-09-18 01:15:24 +0530422 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200423 return dfu_layout[l];
424}
425
426void dfu_show_entities(void)
427{
428 struct dfu_entity *dfu;
429
430 puts("DFU alt settings list:\n");
431
432 list_for_each_entry(dfu, &dfu_list, list) {
433 printf("dev: %s alt: %d name: %s layout: %s\n",
434 dfu_get_dev_type(dfu->dev_type), dfu->alt,
435 dfu->name, dfu_get_layout(dfu->layout));
436 }
437}
438
439int dfu_get_alt_number(void)
440{
441 return dfu_alt_num;
442}
443
444struct dfu_entity *dfu_get_entity(int alt)
445{
446 struct dfu_entity *dfu;
447
448 list_for_each_entry(dfu, &dfu_list, list) {
449 if (dfu->alt == alt)
450 return dfu;
451 }
452
453 return NULL;
454}
Lukasz Majewski1737f0f2013-10-08 14:30:38 +0200455
456int dfu_get_alt(char *name)
457{
458 struct dfu_entity *dfu;
459
460 list_for_each_entry(dfu, &dfu_list, list) {
461 if (!strncmp(dfu->name, name, strlen(dfu->name)))
462 return dfu->alt;
463 }
464
465 return -ENODEV;
466}