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