blob: e5b35bfd5f71e982ae3c292fcd24add306a1df89 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski2d50d682015-08-24 00:21:45 +02002/*
3 * (C) Copyright 2015
4 * Lukasz Majewski <l.majewski@majess.pl>
Lukasz Majewski2d50d682015-08-24 00:21:45 +02005 */
6
7#include <common.h>
8#include <malloc.h>
9#include <errno.h>
10#include <dfu.h>
11
12int 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, "@");
Marek Vasut36dd7e72018-02-16 16:41:19 +010040 debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb));
Lukasz Majewski2d50d682015-08-24 00:21:45 +020041
42 alt_setting_num = dfu_get_alt(sb);
43 free(sb);
44 if (alt_setting_num < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090045 pr_err("Alt setting [%d] to write not found!",
Lukasz Majewski2d50d682015-08-24 00:21:45 +020046 alt_setting_num);
47 ret = -ENODEV;
48 goto done;
49 }
50
51 dfu = dfu_get_entity(alt_setting_num);
52 if (!dfu) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090053 pr_err("DFU entity for alt: %d not found!", alt_setting_num);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020054 ret = -ENODEV;
55 goto done;
56 }
57
Marek Vasut36dd7e72018-02-16 16:41:19 +010058 ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020059
60done:
61 dfu_free_entities();
62
63 return ret;
64}