blob: f3287359db916f9424d7dde518510fcdf5f1661e [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
70static unsigned char *dfu_free_buf(void)
71{
72 free(dfu_buf);
73 dfu_buf = NULL;
74 return dfu_buf;
75}
76
77static unsigned char *dfu_get_buf(void)
78{
79 char *s;
80
81 if (dfu_buf != NULL)
82 return dfu_buf;
83
84 s = getenv("dfu_bufsiz");
85 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
86 CONFIG_SYS_DFU_DATA_BUF_SIZE;
87
88 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
89 if (dfu_buf == NULL)
90 printf("%s: Could not memalign 0x%lx bytes\n",
91 __func__, dfu_buf_size);
92
93 return dfu_buf;
94}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020095
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000096static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020097{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000098 long w_size;
99 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200100
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000101 /* flush size? */
102 w_size = dfu->i_buf - dfu->i_buf_start;
103 if (w_size == 0)
104 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200105
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000106 /* update CRC32 */
107 dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200108
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000109 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
110 if (ret)
111 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200112
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000113 /* point back */
114 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200115
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000116 /* update offset */
117 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200118
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000119 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200120
121 return ret;
122}
123
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000124int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200125{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200126 int ret = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000127 int tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200128
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000129 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
130 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
131 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200132
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000133 if (!dfu->inited) {
134 /* initial state */
135 dfu->crc = 0;
136 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000137 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000138 dfu->i_blk_seq_num = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200139 dfu->i_buf_start = dfu_get_buf();
140 if (dfu->i_buf_start == NULL)
141 return -ENOMEM;
142 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000143 dfu->i_buf = dfu->i_buf_start;
144
145 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200146 }
147
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000148 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200149 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000150 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200151 return -1;
152 }
153
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000154 /* DFU 1.1 standard says:
155 * The wBlockNum field is a block sequence number. It increments each
156 * time a block is transferred, wrapping to zero from 65,535. It is used
157 * to provide useful context to the DFU loader in the device."
158 *
159 * This means that it's a 16 bit counter that roll-overs at
160 * 0xffff -> 0x0000. By having a typical 4K transfer block
161 * we roll-over at exactly 256MB. Not very fun to debug.
162 *
163 * Handling rollover, and having an inited variable,
164 * makes things work.
165 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200166
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000167 /* handle rollover */
168 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
169
170 /* flush buffer if overflow */
171 if ((dfu->i_buf + size) > dfu->i_buf_end) {
172 tret = dfu_write_buffer_drain(dfu);
173 if (ret == 0)
174 ret = tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200175 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000176
177 /* we should be in buffer now (if not then size too large) */
178 if ((dfu->i_buf + size) > dfu->i_buf_end) {
Lukasz Majewskia7d2c3c2013-09-10 15:29:22 +0200179 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
180 size, dfu->i_buf_end);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000181 return -1;
182 }
183
184 memcpy(dfu->i_buf, buf, size);
185 dfu->i_buf += size;
186
187 /* if end or if buffer full flush */
188 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
189 tret = dfu_write_buffer_drain(dfu);
190 if (ret == 0)
191 ret = tret;
192 }
193
194 /* end? */
195 if (size == 0) {
196 /* Now try and flush to the medium if needed. */
197 if (dfu->flush_medium)
198 ret = dfu->flush_medium(dfu);
199 printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
200
201 /* clear everything */
Heiko Schochere7e75c72013-06-12 06:05:51 +0200202 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000203 dfu->crc = 0;
204 dfu->offset = 0;
205 dfu->i_blk_seq_num = 0;
206 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200207 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000208 dfu->i_buf = dfu->i_buf_start;
209
210 dfu->inited = 0;
211
212 }
213
214 return ret = 0 ? size : ret;
215}
216
217static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
218{
219 long chunk;
220 int ret, readn;
221
222 readn = 0;
223 while (size > 0) {
224 /* get chunk that can be read */
225 chunk = min(size, dfu->b_left);
226 /* consume */
227 if (chunk > 0) {
228 memcpy(buf, dfu->i_buf, chunk);
229 dfu->crc = crc32(dfu->crc, buf, chunk);
230 dfu->i_buf += chunk;
231 dfu->b_left -= chunk;
232 size -= chunk;
233 buf += chunk;
234 readn += chunk;
235 }
236
237 /* all done */
238 if (size > 0) {
239 /* no more to read */
240 if (dfu->r_left == 0)
241 break;
242
243 dfu->i_buf = dfu->i_buf_start;
244 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
245
246 /* got to read, but buffer is empty */
247 if (dfu->b_left > dfu->r_left)
248 dfu->b_left = dfu->r_left;
249 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
250 &dfu->b_left);
251 if (ret != 0) {
252 debug("%s: Read error!\n", __func__);
253 return ret;
254 }
255 dfu->offset += dfu->b_left;
256 dfu->r_left -= dfu->b_left;
257
258 puts("#");
259 }
260 }
261
262 return readn;
263}
264
265int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
266{
267 int ret = 0;
268
269 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
270 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
271
272 if (!dfu->inited) {
Łukasz Majewski9df49552013-06-28 18:41:50 +0200273 dfu->i_buf_start = dfu_get_buf();
274 if (dfu->i_buf_start == NULL)
275 return -ENOMEM;
276
277 ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000278 if (ret != 0) {
279 debug("%s: failed to get r_left\n", __func__);
280 return ret;
281 }
282
283 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
284
285 dfu->i_blk_seq_num = 0;
286 dfu->crc = 0;
287 dfu->offset = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200288 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000289 dfu->i_buf = dfu->i_buf_start;
290 dfu->b_left = 0;
291
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000292 dfu->bad_skip = 0;
293
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000294 dfu->inited = 1;
295 }
296
297 if (dfu->i_blk_seq_num != blk_seq_num) {
298 printf("%s: Wrong sequence number! [%d] [%d]\n",
299 __func__, dfu->i_blk_seq_num, blk_seq_num);
300 return -1;
301 }
302 /* handle rollover */
303 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
304
305 ret = dfu_read_buffer_fill(dfu, buf, size);
306 if (ret < 0) {
307 printf("%s: Failed to fill buffer\n", __func__);
308 return -1;
309 }
310
311 if (ret < size) {
312 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
313 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
314
Heiko Schochere7e75c72013-06-12 06:05:51 +0200315 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000316 dfu->i_blk_seq_num = 0;
317 dfu->crc = 0;
318 dfu->offset = 0;
319 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200320 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000321 dfu->i_buf = dfu->i_buf_start;
322 dfu->b_left = 0;
323
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000324 dfu->bad_skip = 0;
325
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000326 dfu->inited = 0;
327 }
328
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200329 return ret;
330}
331
332static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
Mateusz Zalega16297cf2013-10-04 19:22:26 +0200333 char *interface, int num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200334{
335 char *st;
336
337 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
338 st = strsep(&s, " ");
339 strcpy(dfu->name, st);
340
341 dfu->dev_num = num;
342 dfu->alt = alt;
343
344 /* Specific for mmc device */
345 if (strcmp(interface, "mmc") == 0) {
346 if (dfu_fill_entity_mmc(dfu, s))
347 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000348 } else if (strcmp(interface, "nand") == 0) {
349 if (dfu_fill_entity_nand(dfu, s))
350 return -1;
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530351 } else if (strcmp(interface, "ram") == 0) {
352 if (dfu_fill_entity_ram(dfu, s))
353 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200354 } else {
355 printf("%s: Device %s not (yet) supported!\n",
356 __func__, interface);
357 return -1;
358 }
359
360 return 0;
361}
362
363void dfu_free_entities(void)
364{
365 struct dfu_entity *dfu, *p, *t = NULL;
366
367 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
368 list_del(&dfu->list);
369 t = dfu;
370 }
371 if (t)
372 free(t);
373 INIT_LIST_HEAD(&dfu_list);
374}
375
376int dfu_config_entities(char *env, char *interface, int num)
377{
378 struct dfu_entity *dfu;
379 int i, ret;
380 char *s;
381
382 dfu_alt_num = dfu_find_alt_num(env);
383 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
384
385 dfu = calloc(sizeof(*dfu), dfu_alt_num);
386 if (!dfu)
387 return -1;
388 for (i = 0; i < dfu_alt_num; i++) {
389
390 s = strsep(&env, ";");
391 ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
392 if (ret)
393 return -1;
394
395 list_add_tail(&dfu[i].list, &dfu_list);
396 }
397
398 return 0;
399}
400
401const char *dfu_get_dev_type(enum dfu_device_type t)
402{
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530403 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200404 return dev_t[t];
405}
406
407const char *dfu_get_layout(enum dfu_layout l)
408{
409 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
Afzal Mohammeda9479f02013-09-18 01:15:24 +0530410 "EXT3", "EXT4", "RAM_ADDR" };
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200411 return dfu_layout[l];
412}
413
414void dfu_show_entities(void)
415{
416 struct dfu_entity *dfu;
417
418 puts("DFU alt settings list:\n");
419
420 list_for_each_entry(dfu, &dfu_list, list) {
421 printf("dev: %s alt: %d name: %s layout: %s\n",
422 dfu_get_dev_type(dfu->dev_type), dfu->alt,
423 dfu->name, dfu_get_layout(dfu->layout));
424 }
425}
426
427int dfu_get_alt_number(void)
428{
429 return dfu_alt_num;
430}
431
432struct dfu_entity *dfu_get_entity(int alt)
433{
434 struct dfu_entity *dfu;
435
436 list_for_each_entry(dfu, &dfu_list, list) {
437 if (dfu->alt == alt)
438 return dfu;
439 }
440
441 return NULL;
442}