blob: 05217526becf1258f12932e1cf483b9d88ee4e74 [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 *
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>
Heiko Schochere7e75c72013-06-12 06:05:51 +020023#include <errno.h>
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020024#include <malloc.h>
25#include <mmc.h>
26#include <fat.h>
27#include <dfu.h>
28#include <linux/list.h>
29#include <linux/compiler.h>
30
31static LIST_HEAD(dfu_list);
32static int dfu_alt_num;
33
34static int dfu_find_alt_num(const char *s)
35{
36 int i = 0;
37
38 for (; *s; s++)
39 if (*s == ';')
40 i++;
41
42 return ++i;
43}
44
Heiko Schochere7e75c72013-06-12 06:05:51 +020045static unsigned char *dfu_buf;
46static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
47
48static unsigned char *dfu_free_buf(void)
49{
50 free(dfu_buf);
51 dfu_buf = NULL;
52 return dfu_buf;
53}
54
55static unsigned char *dfu_get_buf(void)
56{
57 char *s;
58
59 if (dfu_buf != NULL)
60 return dfu_buf;
61
62 s = getenv("dfu_bufsiz");
63 dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
64 CONFIG_SYS_DFU_DATA_BUF_SIZE;
65
66 dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
67 if (dfu_buf == NULL)
68 printf("%s: Could not memalign 0x%lx bytes\n",
69 __func__, dfu_buf_size);
70
71 return dfu_buf;
72}
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020073
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000074static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020075{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000076 long w_size;
77 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020078
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000079 /* flush size? */
80 w_size = dfu->i_buf - dfu->i_buf_start;
81 if (w_size == 0)
82 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020083
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000084 /* update CRC32 */
85 dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020086
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000087 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
88 if (ret)
89 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020090
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000091 /* point back */
92 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020093
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000094 /* update offset */
95 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020096
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000097 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020098
99 return ret;
100}
101
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000102int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200103{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200104 int ret = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000105 int tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200106
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000107 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
108 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
109 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200110
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000111 if (!dfu->inited) {
112 /* initial state */
113 dfu->crc = 0;
114 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000115 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000116 dfu->i_blk_seq_num = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200117 dfu->i_buf_start = dfu_get_buf();
118 if (dfu->i_buf_start == NULL)
119 return -ENOMEM;
120 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000121 dfu->i_buf = dfu->i_buf_start;
122
123 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200124 }
125
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000126 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200127 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000128 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200129 return -1;
130 }
131
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000132 /* DFU 1.1 standard says:
133 * The wBlockNum field is a block sequence number. It increments each
134 * time a block is transferred, wrapping to zero from 65,535. It is used
135 * to provide useful context to the DFU loader in the device."
136 *
137 * This means that it's a 16 bit counter that roll-overs at
138 * 0xffff -> 0x0000. By having a typical 4K transfer block
139 * we roll-over at exactly 256MB. Not very fun to debug.
140 *
141 * Handling rollover, and having an inited variable,
142 * makes things work.
143 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200144
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000145 /* handle rollover */
146 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
147
148 /* flush buffer if overflow */
149 if ((dfu->i_buf + size) > dfu->i_buf_end) {
150 tret = dfu_write_buffer_drain(dfu);
151 if (ret == 0)
152 ret = tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200153 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000154
155 /* we should be in buffer now (if not then size too large) */
156 if ((dfu->i_buf + size) > dfu->i_buf_end) {
157 printf("%s: Wrong size! [%d] [%d] - %d\n",
158 __func__, dfu->i_blk_seq_num, blk_seq_num, size);
159 return -1;
160 }
161
162 memcpy(dfu->i_buf, buf, size);
163 dfu->i_buf += size;
164
165 /* if end or if buffer full flush */
166 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
167 tret = dfu_write_buffer_drain(dfu);
168 if (ret == 0)
169 ret = tret;
170 }
171
172 /* end? */
173 if (size == 0) {
174 /* Now try and flush to the medium if needed. */
175 if (dfu->flush_medium)
176 ret = dfu->flush_medium(dfu);
177 printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
178
179 /* clear everything */
Heiko Schochere7e75c72013-06-12 06:05:51 +0200180 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000181 dfu->crc = 0;
182 dfu->offset = 0;
183 dfu->i_blk_seq_num = 0;
184 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200185 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000186 dfu->i_buf = dfu->i_buf_start;
187
188 dfu->inited = 0;
189
190 }
191
192 return ret = 0 ? size : ret;
193}
194
195static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
196{
197 long chunk;
198 int ret, readn;
199
200 readn = 0;
201 while (size > 0) {
202 /* get chunk that can be read */
203 chunk = min(size, dfu->b_left);
204 /* consume */
205 if (chunk > 0) {
206 memcpy(buf, dfu->i_buf, chunk);
207 dfu->crc = crc32(dfu->crc, buf, chunk);
208 dfu->i_buf += chunk;
209 dfu->b_left -= chunk;
210 size -= chunk;
211 buf += chunk;
212 readn += chunk;
213 }
214
215 /* all done */
216 if (size > 0) {
217 /* no more to read */
218 if (dfu->r_left == 0)
219 break;
220
221 dfu->i_buf = dfu->i_buf_start;
222 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
223
224 /* got to read, but buffer is empty */
225 if (dfu->b_left > dfu->r_left)
226 dfu->b_left = dfu->r_left;
227 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
228 &dfu->b_left);
229 if (ret != 0) {
230 debug("%s: Read error!\n", __func__);
231 return ret;
232 }
233 dfu->offset += dfu->b_left;
234 dfu->r_left -= dfu->b_left;
235
236 puts("#");
237 }
238 }
239
240 return readn;
241}
242
243int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
244{
245 int ret = 0;
246
247 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
248 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
249
250 if (!dfu->inited) {
Łukasz Majewski9df49552013-06-28 18:41:50 +0200251 dfu->i_buf_start = dfu_get_buf();
252 if (dfu->i_buf_start == NULL)
253 return -ENOMEM;
254
255 ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000256 if (ret != 0) {
257 debug("%s: failed to get r_left\n", __func__);
258 return ret;
259 }
260
261 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
262
263 dfu->i_blk_seq_num = 0;
264 dfu->crc = 0;
265 dfu->offset = 0;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200266 dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000267 dfu->i_buf = dfu->i_buf_start;
268 dfu->b_left = 0;
269
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000270 dfu->bad_skip = 0;
271
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000272 dfu->inited = 1;
273 }
274
275 if (dfu->i_blk_seq_num != blk_seq_num) {
276 printf("%s: Wrong sequence number! [%d] [%d]\n",
277 __func__, dfu->i_blk_seq_num, blk_seq_num);
278 return -1;
279 }
280 /* handle rollover */
281 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
282
283 ret = dfu_read_buffer_fill(dfu, buf, size);
284 if (ret < 0) {
285 printf("%s: Failed to fill buffer\n", __func__);
286 return -1;
287 }
288
289 if (ret < size) {
290 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
291 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
292
Heiko Schochere7e75c72013-06-12 06:05:51 +0200293 dfu_free_buf();
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000294 dfu->i_blk_seq_num = 0;
295 dfu->crc = 0;
296 dfu->offset = 0;
297 dfu->i_buf_start = dfu_buf;
Heiko Schochere7e75c72013-06-12 06:05:51 +0200298 dfu->i_buf_end = dfu_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000299 dfu->i_buf = dfu->i_buf_start;
300 dfu->b_left = 0;
301
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000302 dfu->bad_skip = 0;
303
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000304 dfu->inited = 0;
305 }
306
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200307 return ret;
308}
309
310static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
311 char *interface, int num)
312{
313 char *st;
314
315 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
316 st = strsep(&s, " ");
317 strcpy(dfu->name, st);
318
319 dfu->dev_num = num;
320 dfu->alt = alt;
321
322 /* Specific for mmc device */
323 if (strcmp(interface, "mmc") == 0) {
324 if (dfu_fill_entity_mmc(dfu, s))
325 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000326 } else if (strcmp(interface, "nand") == 0) {
327 if (dfu_fill_entity_nand(dfu, s))
328 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200329 } else {
330 printf("%s: Device %s not (yet) supported!\n",
331 __func__, interface);
332 return -1;
333 }
334
335 return 0;
336}
337
338void dfu_free_entities(void)
339{
340 struct dfu_entity *dfu, *p, *t = NULL;
341
342 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
343 list_del(&dfu->list);
344 t = dfu;
345 }
346 if (t)
347 free(t);
348 INIT_LIST_HEAD(&dfu_list);
349}
350
351int dfu_config_entities(char *env, char *interface, int num)
352{
353 struct dfu_entity *dfu;
354 int i, ret;
355 char *s;
356
357 dfu_alt_num = dfu_find_alt_num(env);
358 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
359
360 dfu = calloc(sizeof(*dfu), dfu_alt_num);
361 if (!dfu)
362 return -1;
363 for (i = 0; i < dfu_alt_num; i++) {
364
365 s = strsep(&env, ";");
366 ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
367 if (ret)
368 return -1;
369
370 list_add_tail(&dfu[i].list, &dfu_list);
371 }
372
373 return 0;
374}
375
376const char *dfu_get_dev_type(enum dfu_device_type t)
377{
378 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND" };
379 return dev_t[t];
380}
381
382const char *dfu_get_layout(enum dfu_layout l)
383{
384 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
385 "EXT3", "EXT4" };
386 return dfu_layout[l];
387}
388
389void dfu_show_entities(void)
390{
391 struct dfu_entity *dfu;
392
393 puts("DFU alt settings list:\n");
394
395 list_for_each_entry(dfu, &dfu_list, list) {
396 printf("dev: %s alt: %d name: %s layout: %s\n",
397 dfu_get_dev_type(dfu->dev_type), dfu->alt,
398 dfu->name, dfu_get_layout(dfu->layout));
399 }
400}
401
402int dfu_get_alt_number(void)
403{
404 return dfu_alt_num;
405}
406
407struct dfu_entity *dfu_get_entity(int alt)
408{
409 struct dfu_entity *dfu;
410
411 list_for_each_entry(dfu, &dfu_list, list) {
412 if (dfu->alt == alt)
413 return dfu;
414 }
415
416 return NULL;
417}