blob: 07011e99a850fafa7475d02451c8f358986b7f3c [file] [log] [blame]
Lukasz Majewskif22b11c2012-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 Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskif22b11c2012-08-06 14:41:07 +02008 */
9
10#include <common.h>
Heiko Schochere7e75c72013-06-12 06:05:51 +020011#include <errno.h>
Lukasz Majewskif22b11c2012-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 Majewski6bed7ce2013-07-18 13:19:14 +020019static bool dfu_reset_request;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020020static LIST_HEAD(dfu_list);
21static int dfu_alt_num;
22
Lukasz Majewski6bed7ce2013-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 Majewskif22b11c2012-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 Majewski765c5ae2013-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 Schochere7e75c72013-06-12 06:05:51 +020067static unsigned char *dfu_buf;
68static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
69
Lukasz Majewskid4278262013-10-08 14:30:39 +020070unsigned char *dfu_free_buf(void)
Heiko Schochere7e75c72013-06-12 06:05:51 +020071{
72 free(dfu_buf);
73 dfu_buf = NULL;
74 return dfu_buf;
75}
76
Lukasz Majewski4fb12782013-12-09 16:20:13 +010077unsigned long dfu_get_buf_size(void)
78{
79 return dfu_buf_size;
80}
81
Lukasz Majewskid4278262013-10-08 14:30:39 +020082unsigned char *dfu_get_buf(void)
Heiko Schochere7e75c72013-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 Majewskif22b11c2012-08-06 14:41:07 +0200100
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000101static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200102{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000103 long w_size;
104 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200105
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000106 /* flush size? */
107 w_size = dfu->i_buf - dfu->i_buf_start;
108 if (w_size == 0)
109 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200110
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000111 /* update CRC32 */
112 dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200113
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000114 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
115 if (ret)
116 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200117
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000118 /* point back */
119 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200120
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000121 /* update offset */
122 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200123
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000124 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200125
126 return ret;
127}
128
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000129int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200130{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200131 int ret = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000132 int tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200133
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000134 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);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200137
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000138 if (!dfu->inited) {
139 /* initial state */
140 dfu->crc = 0;
141 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000142 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000143 dfu->i_blk_seq_num = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200144 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;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000148 dfu->i_buf = dfu->i_buf_start;
149
150 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200151 }
152
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000153 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200154 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000155 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200156 return -1;
157 }
158
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000159 /* 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 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200171
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000172 /* 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;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200180 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000181
182 /* we should be in buffer now (if not then size too large) */
183 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200184 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
185 size, dfu->i_buf_end);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000186 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 */
Heiko Schochere7e75c72013-06-12 06:05:51 +0200207 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000208 dfu->crc = 0;
209 dfu->offset = 0;
210 dfu->i_blk_seq_num = 0;
211 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200212 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000213 dfu->i_buf = dfu->i_buf_start;
214
215 dfu->inited = 0;
216
217 }
218
219 return ret = 0 ? size : ret;
220}
221
222static 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;
Bo Shenc2617bc2013-10-16 17:26:37 +0800237 dfu->r_left -= chunk;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000238 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
271int 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) {
Łukasz Majewski9df49552013-06-28 18:41:50 +0200279 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);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000284 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;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200294 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000295 dfu->i_buf = dfu->i_buf_start;
Bo Shen31bae4c2013-10-16 17:26:38 +0800296 dfu->b_left = min(dfu_buf_size, dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000297
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000298 dfu->bad_skip = 0;
299
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000300 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
Heiko Schochere7e75c72013-06-12 06:05:51 +0200321 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000322 dfu->i_blk_seq_num = 0;
323 dfu->crc = 0;
324 dfu->offset = 0;
325 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200326 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000327 dfu->i_buf = dfu->i_buf_start;
328 dfu->b_left = 0;
329
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000330 dfu->bad_skip = 0;
331
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000332 dfu->inited = 0;
333 }
334
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200335 return ret;
336}
337
338static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200339 char *interface, int num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200340{
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;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000354 } else if (strcmp(interface, "nand") == 0) {
355 if (dfu_fill_entity_nand(dfu, s))
356 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530357 } else if (strcmp(interface, "ram") == 0) {
358 if (dfu_fill_entity_ram(dfu, s))
359 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200360 } else {
361 printf("%s: Device %s not (yet) supported!\n",
362 __func__, interface);
363 return -1;
364 }
365
366 return 0;
367}
368
369void 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
382int 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
407const char *dfu_get_dev_type(enum dfu_device_type t)
408{
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530409 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200410 return dev_t[t];
411}
412
413const char *dfu_get_layout(enum dfu_layout l)
414{
415 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530416 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200417 return dfu_layout[l];
418}
419
420void 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
433int dfu_get_alt_number(void)
434{
435 return dfu_alt_num;
436}
437
438struct 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}
Lukasz Majewskifed936e2013-10-08 14:30:38 +0200449
450int 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}