blob: 6af6890d09158049a9fc5f9d250565c9ed34cc25 [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>
23#include <malloc.h>
24#include <mmc.h>
25#include <fat.h>
26#include <dfu.h>
27#include <linux/list.h>
28#include <linux/compiler.h>
29
30static LIST_HEAD(dfu_list);
31static int dfu_alt_num;
32
33static 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
44static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
45 dfu_buf[DFU_DATA_BUF_SIZE];
46
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000047static int dfu_write_buffer_drain(struct dfu_entity *dfu)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020048{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000049 long w_size;
50 int ret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020051
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000052 /* flush size? */
53 w_size = dfu->i_buf - dfu->i_buf_start;
54 if (w_size == 0)
55 return 0;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020056
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000057 /* update CRC32 */
58 dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020059
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000060 ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
61 if (ret)
62 debug("%s: Write error!\n", __func__);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020063
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000064 /* point back */
65 dfu->i_buf = dfu->i_buf_start;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020066
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000067 /* update offset */
68 dfu->offset += w_size;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020069
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000070 puts("#");
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020071
72 return ret;
73}
74
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000075int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020076{
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020077 int ret = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000078 int tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020079
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000080 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
81 __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
82 dfu->i_buf - dfu->i_buf_start);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020083
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000084 if (!dfu->inited) {
85 /* initial state */
86 dfu->crc = 0;
87 dfu->offset = 0;
Pantelis Antoniouc6631762013-03-14 05:32:52 +000088 dfu->bad_skip = 0;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000089 dfu->i_blk_seq_num = 0;
90 dfu->i_buf_start = dfu_buf;
91 dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
92 dfu->i_buf = dfu->i_buf_start;
93
94 dfu->inited = 1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020095 }
96
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000097 if (dfu->i_blk_seq_num != blk_seq_num) {
Lukasz Majewskif22b11c2012-08-06 14:41:07 +020098 printf("%s: Wrong sequence number! [%d] [%d]\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000099 __func__, dfu->i_blk_seq_num, blk_seq_num);
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200100 return -1;
101 }
102
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000103 /* DFU 1.1 standard says:
104 * The wBlockNum field is a block sequence number. It increments each
105 * time a block is transferred, wrapping to zero from 65,535. It is used
106 * to provide useful context to the DFU loader in the device."
107 *
108 * This means that it's a 16 bit counter that roll-overs at
109 * 0xffff -> 0x0000. By having a typical 4K transfer block
110 * we roll-over at exactly 256MB. Not very fun to debug.
111 *
112 * Handling rollover, and having an inited variable,
113 * makes things work.
114 */
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200115
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000116 /* handle rollover */
117 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
118
119 /* flush buffer if overflow */
120 if ((dfu->i_buf + size) > dfu->i_buf_end) {
121 tret = dfu_write_buffer_drain(dfu);
122 if (ret == 0)
123 ret = tret;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200124 }
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000125
126 /* we should be in buffer now (if not then size too large) */
127 if ((dfu->i_buf + size) > dfu->i_buf_end) {
128 printf("%s: Wrong size! [%d] [%d] - %d\n",
129 __func__, dfu->i_blk_seq_num, blk_seq_num, size);
130 return -1;
131 }
132
133 memcpy(dfu->i_buf, buf, size);
134 dfu->i_buf += size;
135
136 /* if end or if buffer full flush */
137 if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
138 tret = dfu_write_buffer_drain(dfu);
139 if (ret == 0)
140 ret = tret;
141 }
142
143 /* end? */
144 if (size == 0) {
145 /* Now try and flush to the medium if needed. */
146 if (dfu->flush_medium)
147 ret = dfu->flush_medium(dfu);
148 printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
149
150 /* clear everything */
151 dfu->crc = 0;
152 dfu->offset = 0;
153 dfu->i_blk_seq_num = 0;
154 dfu->i_buf_start = dfu_buf;
155 dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
156 dfu->i_buf = dfu->i_buf_start;
157
158 dfu->inited = 0;
159
160 }
161
162 return ret = 0 ? size : ret;
163}
164
165static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
166{
167 long chunk;
168 int ret, readn;
169
170 readn = 0;
171 while (size > 0) {
172 /* get chunk that can be read */
173 chunk = min(size, dfu->b_left);
174 /* consume */
175 if (chunk > 0) {
176 memcpy(buf, dfu->i_buf, chunk);
177 dfu->crc = crc32(dfu->crc, buf, chunk);
178 dfu->i_buf += chunk;
179 dfu->b_left -= chunk;
180 size -= chunk;
181 buf += chunk;
182 readn += chunk;
183 }
184
185 /* all done */
186 if (size > 0) {
187 /* no more to read */
188 if (dfu->r_left == 0)
189 break;
190
191 dfu->i_buf = dfu->i_buf_start;
192 dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
193
194 /* got to read, but buffer is empty */
195 if (dfu->b_left > dfu->r_left)
196 dfu->b_left = dfu->r_left;
197 ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
198 &dfu->b_left);
199 if (ret != 0) {
200 debug("%s: Read error!\n", __func__);
201 return ret;
202 }
203 dfu->offset += dfu->b_left;
204 dfu->r_left -= dfu->b_left;
205
206 puts("#");
207 }
208 }
209
210 return readn;
211}
212
213int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
214{
215 int ret = 0;
216
217 debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
218 __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
219
220 if (!dfu->inited) {
221 ret = dfu->read_medium(dfu, 0, buf, &dfu->r_left);
222 if (ret != 0) {
223 debug("%s: failed to get r_left\n", __func__);
224 return ret;
225 }
226
227 debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
228
229 dfu->i_blk_seq_num = 0;
230 dfu->crc = 0;
231 dfu->offset = 0;
232 dfu->i_buf_start = dfu_buf;
233 dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
234 dfu->i_buf = dfu->i_buf_start;
235 dfu->b_left = 0;
236
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000237 dfu->bad_skip = 0;
238
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000239 dfu->inited = 1;
240 }
241
242 if (dfu->i_blk_seq_num != blk_seq_num) {
243 printf("%s: Wrong sequence number! [%d] [%d]\n",
244 __func__, dfu->i_blk_seq_num, blk_seq_num);
245 return -1;
246 }
247 /* handle rollover */
248 dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
249
250 ret = dfu_read_buffer_fill(dfu, buf, size);
251 if (ret < 0) {
252 printf("%s: Failed to fill buffer\n", __func__);
253 return -1;
254 }
255
256 if (ret < size) {
257 debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
258 puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
259
260 dfu->i_blk_seq_num = 0;
261 dfu->crc = 0;
262 dfu->offset = 0;
263 dfu->i_buf_start = dfu_buf;
264 dfu->i_buf_end = dfu_buf + sizeof(dfu_buf);
265 dfu->i_buf = dfu->i_buf_start;
266 dfu->b_left = 0;
267
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000268 dfu->bad_skip = 0;
269
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000270 dfu->inited = 0;
271 }
272
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200273 return ret;
274}
275
276static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
277 char *interface, int num)
278{
279 char *st;
280
281 debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
282 st = strsep(&s, " ");
283 strcpy(dfu->name, st);
284
285 dfu->dev_num = num;
286 dfu->alt = alt;
287
288 /* Specific for mmc device */
289 if (strcmp(interface, "mmc") == 0) {
290 if (dfu_fill_entity_mmc(dfu, s))
291 return -1;
Pantelis Antoniouc6631762013-03-14 05:32:52 +0000292 } else if (strcmp(interface, "nand") == 0) {
293 if (dfu_fill_entity_nand(dfu, s))
294 return -1;
Lukasz Majewskif22b11c2012-08-06 14:41:07 +0200295 } else {
296 printf("%s: Device %s not (yet) supported!\n",
297 __func__, interface);
298 return -1;
299 }
300
301 return 0;
302}
303
304void dfu_free_entities(void)
305{
306 struct dfu_entity *dfu, *p, *t = NULL;
307
308 list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
309 list_del(&dfu->list);
310 t = dfu;
311 }
312 if (t)
313 free(t);
314 INIT_LIST_HEAD(&dfu_list);
315}
316
317int dfu_config_entities(char *env, char *interface, int num)
318{
319 struct dfu_entity *dfu;
320 int i, ret;
321 char *s;
322
323 dfu_alt_num = dfu_find_alt_num(env);
324 debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
325
326 dfu = calloc(sizeof(*dfu), dfu_alt_num);
327 if (!dfu)
328 return -1;
329 for (i = 0; i < dfu_alt_num; i++) {
330
331 s = strsep(&env, ";");
332 ret = dfu_fill_entity(&dfu[i], s, i, interface, num);
333 if (ret)
334 return -1;
335
336 list_add_tail(&dfu[i].list, &dfu_list);
337 }
338
339 return 0;
340}
341
342const char *dfu_get_dev_type(enum dfu_device_type t)
343{
344 const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND" };
345 return dev_t[t];
346}
347
348const char *dfu_get_layout(enum dfu_layout l)
349{
350 const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
351 "EXT3", "EXT4" };
352 return dfu_layout[l];
353}
354
355void dfu_show_entities(void)
356{
357 struct dfu_entity *dfu;
358
359 puts("DFU alt settings list:\n");
360
361 list_for_each_entry(dfu, &dfu_list, list) {
362 printf("dev: %s alt: %d name: %s layout: %s\n",
363 dfu_get_dev_type(dfu->dev_type), dfu->alt,
364 dfu->name, dfu_get_layout(dfu->layout));
365 }
366}
367
368int dfu_get_alt_number(void)
369{
370 return dfu_alt_num;
371}
372
373struct dfu_entity *dfu_get_entity(int alt)
374{
375 struct dfu_entity *dfu;
376
377 list_for_each_entry(dfu, &dfu_list, list) {
378 if (dfu->alt == alt)
379 return dfu;
380 }
381
382 return NULL;
383}