blob: 2f1e2afa199ab53c77d04cb05cc6bfb62fd47493 [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
Heiko Schochera2f831e2013-06-12 06:05:51 +020044static unsigned char *dfu_buf;
45static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
46
47static unsigned char *dfu_free_buf(void)
48{
49 free(dfu_buf);
50 dfu_buf = NULL;
51 return dfu_buf;
52}
53
54static 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}
Lukasz Majewskid311c6e2012-08-06 14:41:07 +020072
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000073static 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
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200101int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
102{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200103 int ret = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000104 int tret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200105
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000106 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);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200109
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000110 if (!dfu->inited) {
111 /* initial state */
112 dfu->crc = 0;
113 dfu->offset = 0;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000114 dfu->bad_skip = 0;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000115 dfu->i_blk_seq_num = 0;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200116 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;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000120 dfu->i_buf = dfu->i_buf_start;
121
122 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200123 }
124
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000125 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200126 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000127 __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) {
Lukasz Majewski385c3212013-09-10 15:29:22 +0200156 error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
157 size, dfu->i_buf_end);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200158 return -1;
159 }
160
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000161 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 }
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200170
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000171 /* end? */
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200172 if (size == 0) {
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000173 /* 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);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200177
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000178 /* clear everything */
Heiko Schochera2f831e2013-06-12 06:05:51 +0200179 dfu_free_buf();
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000180 dfu->crc = 0;
181 dfu->offset = 0;
182 dfu->i_blk_seq_num = 0;
183 dfu->i_buf_start = dfu_buf;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200184 dfu->i_buf_end = dfu_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000185 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200186
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000187 dfu->inited = 0;
188
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200189 }
190
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000191 return ret = 0 ? size : ret;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200192}
193
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000194static 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
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200242int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
243{
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200244 int ret = 0;
245
246 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 +0000247 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
248
249 if (!dfu->inited) {
Łukasz Majewski063ae5a2013-06-28 18:41:50 +0200250 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);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000255 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;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200265 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000266 dfu->i_buf = dfu->i_buf_start;
267 dfu->b_left = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200268
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000269 dfu->bad_skip = 0;
270
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000271 dfu->inited = 1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200272 }
273
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000274 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200275 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000276 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200277 return -1;
278 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000279 /* handle rollover */
280 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200281
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000282 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");
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200291
Heiko Schochera2f831e2013-06-12 06:05:51 +0200292 dfu_free_buf();
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000293 dfu->i_blk_seq_num = 0;
294 dfu->crc = 0;
295 dfu->offset = 0;
296 dfu->i_buf_start = dfu_buf;
Heiko Schochera2f831e2013-06-12 06:05:51 +0200297 dfu->i_buf_end = dfu_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000298 dfu->i_buf = dfu->i_buf_start;
299 dfu->b_left = 0;
300
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000301 dfu->bad_skip = 0;
302
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000303 dfu->inited = 0;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200304 }
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000305
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200306 return ret;
307}
308
309static 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;
Pantelis Antonioucf14d0d2013-03-14 05:32:52 +0000325 } else if (strcmp(interface, "nand") == 0) {
326 if (dfu_fill_entity_nand(dfu, s))
327 return -1;
Lukasz Majewskid311c6e2012-08-06 14:41:07 +0200328 } else {
329 printf("%s: Device %s not (yet) supported!\n",
330 __func__, interface);
331 return -1;
332 }
333
334 return 0;
335}
336
337void 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
350int 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
375const 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
381const 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
388void 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
401int dfu_get_alt_number(void)
402{
403 return dfu_alt_num;
404}
405
406struct 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}