xref: /openbmc/u-boot/drivers/dfu/dfu_tftp.c (revision ee7bb5be)
1 /*
2  * (C) Copyright 2015
3  * Lukasz Majewski <l.majewski@majess.pl>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <malloc.h>
10 #include <errno.h>
11 #include <dfu.h>
12 
13 int dfu_tftp_write(char *dfu_entity_name, unsigned int addr, unsigned int len,
14 		   char *interface, char *devstring)
15 {
16 	char *s, *sb;
17 	int alt_setting_num, ret;
18 	struct dfu_entity *dfu;
19 
20 	debug("%s: name: %s addr: 0x%x len: %d device: %s:%s\n", __func__,
21 	      dfu_entity_name, addr, len, interface, devstring);
22 
23 	ret = dfu_init_env_entities(interface, devstring);
24 	if (ret)
25 		goto done;
26 
27 	/*
28 	 * We need to copy name pointed by *dfu_entity_name since this text
29 	 * is the integral part of the FDT image.
30 	 * Any implicit modification (i.e. done by strsep()) will corrupt
31 	 * the FDT image and prevent other images to be stored.
32 	 */
33 	s = strdup(dfu_entity_name);
34 	sb = s;
35 	if (!s) {
36 		ret = -ENOMEM;
37 		goto done;
38 	}
39 
40 	strsep(&s, "@");
41 	debug("%s: image name: %s strlen: %d\n", __func__, sb, strlen(sb));
42 
43 	alt_setting_num = dfu_get_alt(sb);
44 	free(sb);
45 	if (alt_setting_num < 0) {
46 		error("Alt setting [%d] to write not found!",
47 		      alt_setting_num);
48 		ret = -ENODEV;
49 		goto done;
50 	}
51 
52 	dfu = dfu_get_entity(alt_setting_num);
53 	if (!dfu) {
54 		error("DFU entity for alt: %d not found!", alt_setting_num);
55 		ret = -ENODEV;
56 		goto done;
57 	}
58 
59 	ret = dfu_write_from_mem_addr(dfu, (void *)addr, len);
60 
61 done:
62 	dfu_free_entities();
63 
64 	return ret;
65 }
66