blob: f0afbac477e38ebae04cd42aa4627ec63e2e46bf [file] [log] [blame]
Lukasz Majewski2d50d682015-08-24 00:21:45 +02001/*
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
13int 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, "@");
Marek Vasut36dd7e72018-02-16 16:41:19 +010041 debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb));
Lukasz Majewski2d50d682015-08-24 00:21:45 +020042
43 alt_setting_num = dfu_get_alt(sb);
44 free(sb);
45 if (alt_setting_num < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090046 pr_err("Alt setting [%d] to write not found!",
Lukasz Majewski2d50d682015-08-24 00:21:45 +020047 alt_setting_num);
48 ret = -ENODEV;
49 goto done;
50 }
51
52 dfu = dfu_get_entity(alt_setting_num);
53 if (!dfu) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090054 pr_err("DFU entity for alt: %d not found!", alt_setting_num);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020055 ret = -ENODEV;
56 goto done;
57 }
58
Marek Vasut36dd7e72018-02-16 16:41:19 +010059 ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020060
61done:
62 dfu_free_entities();
63
64 return ret;
65}