xref: /openbmc/u-boot/drivers/dfu/dfu.c (revision cd23aac4)
1 /*
2  * dfu.c -- DFU back-end routines
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * author: Lukasz Majewski <l.majewski@samsung.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <errno.h>
12 #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 
19 static bool dfu_reset_request;
20 static LIST_HEAD(dfu_list);
21 static int dfu_alt_num;
22 static int alt_num_cnt;
23 
24 bool dfu_reset(void)
25 {
26 	return dfu_reset_request;
27 }
28 
29 void dfu_trigger_reset()
30 {
31 	dfu_reset_request = true;
32 }
33 
34 static 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 
45 int dfu_init_env_entities(char *interface, int dev)
46 {
47 	const char *str_env;
48 	char *env_bkp;
49 	int ret;
50 
51 	str_env = getenv("dfu_alt_info");
52 	if (!str_env) {
53 		error("\"dfu_alt_info\" env variable not defined!\n");
54 		return -EINVAL;
55 	}
56 
57 	env_bkp = strdup(str_env);
58 	ret = dfu_config_entities(env_bkp, interface, dev);
59 	if (ret) {
60 		error("DFU entities configuration failed!\n");
61 		return ret;
62 	}
63 
64 	free(env_bkp);
65 	return 0;
66 }
67 
68 static unsigned char *dfu_buf;
69 static unsigned long dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE;
70 
71 unsigned char *dfu_free_buf(void)
72 {
73 	free(dfu_buf);
74 	dfu_buf = NULL;
75 	return dfu_buf;
76 }
77 
78 unsigned long dfu_get_buf_size(void)
79 {
80 	return dfu_buf_size;
81 }
82 
83 unsigned char *dfu_get_buf(void)
84 {
85 	char *s;
86 
87 	if (dfu_buf != NULL)
88 		return dfu_buf;
89 
90 	s = getenv("dfu_bufsiz");
91 	dfu_buf_size = s ? (unsigned long)simple_strtol(s, NULL, 16) :
92 			CONFIG_SYS_DFU_DATA_BUF_SIZE;
93 
94 	dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size);
95 	if (dfu_buf == NULL)
96 		printf("%s: Could not memalign 0x%lx bytes\n",
97 		       __func__, dfu_buf_size);
98 
99 	return dfu_buf;
100 }
101 
102 static int dfu_write_buffer_drain(struct dfu_entity *dfu)
103 {
104 	long w_size;
105 	int ret;
106 
107 	/* flush size? */
108 	w_size = dfu->i_buf - dfu->i_buf_start;
109 	if (w_size == 0)
110 		return 0;
111 
112 	/* update CRC32 */
113 	dfu->crc = crc32(dfu->crc, dfu->i_buf_start, w_size);
114 
115 	ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size);
116 	if (ret)
117 		debug("%s: Write error!\n", __func__);
118 
119 	/* point back */
120 	dfu->i_buf = dfu->i_buf_start;
121 
122 	/* update offset */
123 	dfu->offset += w_size;
124 
125 	puts("#");
126 
127 	return ret;
128 }
129 
130 int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
131 {
132 	int ret = 0;
133 
134 	ret = dfu_write_buffer_drain(dfu);
135 	if (ret)
136 		return ret;
137 
138 	if (dfu->flush_medium)
139 		ret = dfu->flush_medium(dfu);
140 
141 	printf("\nDFU complete CRC32: 0x%08x\n", dfu->crc);
142 
143 	/* clear everything */
144 	dfu_free_buf();
145 	dfu->crc = 0;
146 	dfu->offset = 0;
147 	dfu->i_blk_seq_num = 0;
148 	dfu->i_buf_start = dfu_buf;
149 	dfu->i_buf_end = dfu_buf;
150 	dfu->i_buf = dfu->i_buf_start;
151 	dfu->inited = 0;
152 
153 	return ret;
154 }
155 
156 int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
157 {
158 	int ret = 0;
159 	int tret;
160 
161 	debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%x\n",
162 	      __func__, dfu->name, buf, size, blk_seq_num, dfu->offset,
163 	      dfu->i_buf - dfu->i_buf_start);
164 
165 	if (!dfu->inited) {
166 		/* initial state */
167 		dfu->crc = 0;
168 		dfu->offset = 0;
169 		dfu->bad_skip = 0;
170 		dfu->i_blk_seq_num = 0;
171 		dfu->i_buf_start = dfu_get_buf();
172 		if (dfu->i_buf_start == NULL)
173 			return -ENOMEM;
174 		dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
175 		dfu->i_buf = dfu->i_buf_start;
176 
177 		dfu->inited = 1;
178 	}
179 
180 	if (dfu->i_blk_seq_num != blk_seq_num) {
181 		printf("%s: Wrong sequence number! [%d] [%d]\n",
182 		       __func__, dfu->i_blk_seq_num, blk_seq_num);
183 		return -1;
184 	}
185 
186 	/* DFU 1.1 standard says:
187 	 * The wBlockNum field is a block sequence number. It increments each
188 	 * time a block is transferred, wrapping to zero from 65,535. It is used
189 	 * to provide useful context to the DFU loader in the device."
190 	 *
191 	 * This means that it's a 16 bit counter that roll-overs at
192 	 * 0xffff -> 0x0000. By having a typical 4K transfer block
193 	 * we roll-over at exactly 256MB. Not very fun to debug.
194 	 *
195 	 * Handling rollover, and having an inited variable,
196 	 * makes things work.
197 	 */
198 
199 	/* handle rollover */
200 	dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
201 
202 	/* flush buffer if overflow */
203 	if ((dfu->i_buf + size) > dfu->i_buf_end) {
204 		tret = dfu_write_buffer_drain(dfu);
205 		if (ret == 0)
206 			ret = tret;
207 	}
208 
209 	/* we should be in buffer now (if not then size too large) */
210 	if ((dfu->i_buf + size) > dfu->i_buf_end) {
211 		error("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf,
212 		      size, dfu->i_buf_end);
213 		return -1;
214 	}
215 
216 	memcpy(dfu->i_buf, buf, size);
217 	dfu->i_buf += size;
218 
219 	/* if end or if buffer full flush */
220 	if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) {
221 		tret = dfu_write_buffer_drain(dfu);
222 		if (ret == 0)
223 			ret = tret;
224 	}
225 
226 	return ret;
227 }
228 
229 static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
230 {
231 	long chunk;
232 	int ret, readn;
233 
234 	readn = 0;
235 	while (size > 0) {
236 		/* get chunk that can be read */
237 		chunk = min(size, dfu->b_left);
238 		/* consume */
239 		if (chunk > 0) {
240 			memcpy(buf, dfu->i_buf, chunk);
241 			dfu->crc = crc32(dfu->crc, buf, chunk);
242 			dfu->i_buf += chunk;
243 			dfu->b_left -= chunk;
244 			dfu->r_left -= chunk;
245 			size -= chunk;
246 			buf += chunk;
247 			readn += chunk;
248 		}
249 
250 		/* all done */
251 		if (size > 0) {
252 			/* no more to read */
253 			if (dfu->r_left == 0)
254 				break;
255 
256 			dfu->i_buf = dfu->i_buf_start;
257 			dfu->b_left = dfu->i_buf_end - dfu->i_buf_start;
258 
259 			/* got to read, but buffer is empty */
260 			if (dfu->b_left > dfu->r_left)
261 				dfu->b_left = dfu->r_left;
262 			ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf,
263 					&dfu->b_left);
264 			if (ret != 0) {
265 				debug("%s: Read error!\n", __func__);
266 				return ret;
267 			}
268 			dfu->offset += dfu->b_left;
269 			dfu->r_left -= dfu->b_left;
270 
271 			puts("#");
272 		}
273 	}
274 
275 	return readn;
276 }
277 
278 int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num)
279 {
280 	int ret = 0;
281 
282 	debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n",
283 	       __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf);
284 
285 	if (!dfu->inited) {
286 		dfu->i_buf_start = dfu_get_buf();
287 		if (dfu->i_buf_start == NULL)
288 			return -ENOMEM;
289 
290 		ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left);
291 		if (ret != 0) {
292 			debug("%s: failed to get r_left\n", __func__);
293 			return ret;
294 		}
295 
296 		debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left);
297 
298 		dfu->i_blk_seq_num = 0;
299 		dfu->crc = 0;
300 		dfu->offset = 0;
301 		dfu->i_buf_end = dfu_get_buf() + dfu_buf_size;
302 		dfu->i_buf = dfu->i_buf_start;
303 		dfu->b_left = min(dfu_buf_size, dfu->r_left);
304 
305 		dfu->bad_skip = 0;
306 
307 		dfu->inited = 1;
308 	}
309 
310 	if (dfu->i_blk_seq_num != blk_seq_num) {
311 		printf("%s: Wrong sequence number! [%d] [%d]\n",
312 		       __func__, dfu->i_blk_seq_num, blk_seq_num);
313 		return -1;
314 	}
315 	/* handle rollover */
316 	dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff;
317 
318 	ret = dfu_read_buffer_fill(dfu, buf, size);
319 	if (ret < 0) {
320 		printf("%s: Failed to fill buffer\n", __func__);
321 		return -1;
322 	}
323 
324 	if (ret < size) {
325 		debug("%s: %s CRC32: 0x%x\n", __func__, dfu->name, dfu->crc);
326 		puts("\nUPLOAD ... done\nCtrl+C to exit ...\n");
327 
328 		dfu_free_buf();
329 		dfu->i_blk_seq_num = 0;
330 		dfu->crc = 0;
331 		dfu->offset = 0;
332 		dfu->i_buf_start = dfu_buf;
333 		dfu->i_buf_end = dfu_buf;
334 		dfu->i_buf = dfu->i_buf_start;
335 		dfu->b_left = 0;
336 
337 		dfu->bad_skip = 0;
338 
339 		dfu->inited = 0;
340 	}
341 
342 	return ret;
343 }
344 
345 static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt,
346 			   char *interface, int num)
347 {
348 	char *st;
349 
350 	debug("%s: %s interface: %s num: %d\n", __func__, s, interface, num);
351 	st = strsep(&s, " ");
352 	strcpy(dfu->name, st);
353 
354 	dfu->dev_num = num;
355 	dfu->alt = alt;
356 
357 	/* Specific for mmc device */
358 	if (strcmp(interface, "mmc") == 0) {
359 		if (dfu_fill_entity_mmc(dfu, s))
360 			return -1;
361 	} else if (strcmp(interface, "nand") == 0) {
362 		if (dfu_fill_entity_nand(dfu, s))
363 			return -1;
364 	} else if (strcmp(interface, "ram") == 0) {
365 		if (dfu_fill_entity_ram(dfu, s))
366 			return -1;
367 	} else {
368 		printf("%s: Device %s not (yet) supported!\n",
369 		       __func__,  interface);
370 		return -1;
371 	}
372 
373 	return 0;
374 }
375 
376 void dfu_free_entities(void)
377 {
378 	struct dfu_entity *dfu, *p, *t = NULL;
379 
380 	list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) {
381 		list_del(&dfu->list);
382 		t = dfu;
383 	}
384 	if (t)
385 		free(t);
386 	INIT_LIST_HEAD(&dfu_list);
387 
388 	alt_num_cnt = 0;
389 }
390 
391 int dfu_config_entities(char *env, char *interface, int num)
392 {
393 	struct dfu_entity *dfu;
394 	int i, ret;
395 	char *s;
396 
397 	dfu_alt_num = dfu_find_alt_num(env);
398 	debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num);
399 
400 	dfu = calloc(sizeof(*dfu), dfu_alt_num);
401 	if (!dfu)
402 		return -1;
403 	for (i = 0; i < dfu_alt_num; i++) {
404 
405 		s = strsep(&env, ";");
406 		ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, num);
407 		if (ret)
408 			return -1;
409 
410 		list_add_tail(&dfu[i].list, &dfu_list);
411 		alt_num_cnt++;
412 	}
413 
414 	return 0;
415 }
416 
417 const char *dfu_get_dev_type(enum dfu_device_type t)
418 {
419 	const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM" };
420 	return dev_t[t];
421 }
422 
423 const char *dfu_get_layout(enum dfu_layout l)
424 {
425 	const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2",
426 					   "EXT3", "EXT4", "RAM_ADDR" };
427 	return dfu_layout[l];
428 }
429 
430 void dfu_show_entities(void)
431 {
432 	struct dfu_entity *dfu;
433 
434 	puts("DFU alt settings list:\n");
435 
436 	list_for_each_entry(dfu, &dfu_list, list) {
437 		printf("dev: %s alt: %d name: %s layout: %s\n",
438 		       dfu_get_dev_type(dfu->dev_type), dfu->alt,
439 		       dfu->name, dfu_get_layout(dfu->layout));
440 	}
441 }
442 
443 int dfu_get_alt_number(void)
444 {
445 	return dfu_alt_num;
446 }
447 
448 struct dfu_entity *dfu_get_entity(int alt)
449 {
450 	struct dfu_entity *dfu;
451 
452 	list_for_each_entry(dfu, &dfu_list, list) {
453 		if (dfu->alt == alt)
454 			return dfu;
455 	}
456 
457 	return NULL;
458 }
459 
460 int dfu_get_alt(char *name)
461 {
462 	struct dfu_entity *dfu;
463 
464 	list_for_each_entry(dfu, &dfu_list, list) {
465 		if (!strncmp(dfu->name, name, strlen(dfu->name)))
466 			return dfu->alt;
467 	}
468 
469 	return -ENODEV;
470 }
471