blob: ece3d2236f3dcdd8b122581cb573d5ef36907baf [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Lukasz Majewski2d50d682015-08-24 00:21:45 +02009#include <malloc.h>
10#include <errno.h>
11#include <dfu.h>
12
AKASHI Takahiro045fd8b2020-10-29 13:47:41 +090013/**
14 * dfu_write_by_name() - write data to DFU medium
15 * @dfu_entity_name: Name of DFU entity to write
16 * @addr: Address of data buffer to write
17 * @len: Number of bytes
18 * @interface: Destination DFU medium (e.g. "mmc")
19 * @devstring: Instance number of destination DFU medium (e.g. "1")
20 *
21 * This function is storing data received on DFU supported medium which
22 * is specified by @dfu_entity_name.
23 *
24 * Return: 0 - on success, error code - otherwise
25 */
AKASHI Takahiro1c2d1292020-10-29 13:47:42 +090026int dfu_write_by_name(char *dfu_entity_name, void *addr,
AKASHI Takahiro045fd8b2020-10-29 13:47:41 +090027 unsigned int len, char *interface, char *devstring)
Lukasz Majewski2d50d682015-08-24 00:21:45 +020028{
29 char *s, *sb;
30 int alt_setting_num, ret;
31 struct dfu_entity *dfu;
32
AKASHI Takahiro1c2d1292020-10-29 13:47:42 +090033 debug("%s: name: %s addr: 0x%p len: %d device: %s:%s\n", __func__,
Lukasz Majewski2d50d682015-08-24 00:21:45 +020034 dfu_entity_name, addr, len, interface, devstring);
35
36 ret = dfu_init_env_entities(interface, devstring);
37 if (ret)
38 goto done;
39
40 /*
41 * We need to copy name pointed by *dfu_entity_name since this text
42 * is the integral part of the FDT image.
43 * Any implicit modification (i.e. done by strsep()) will corrupt
44 * the FDT image and prevent other images to be stored.
45 */
46 s = strdup(dfu_entity_name);
47 sb = s;
48 if (!s) {
49 ret = -ENOMEM;
50 goto done;
51 }
52
53 strsep(&s, "@");
Marek Vasut36dd7e72018-02-16 16:41:19 +010054 debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb));
Lukasz Majewski2d50d682015-08-24 00:21:45 +020055
56 alt_setting_num = dfu_get_alt(sb);
57 free(sb);
58 if (alt_setting_num < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090059 pr_err("Alt setting [%d] to write not found!",
AKASHI Takahiro045fd8b2020-10-29 13:47:41 +090060 alt_setting_num);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020061 ret = -ENODEV;
62 goto done;
63 }
64
65 dfu = dfu_get_entity(alt_setting_num);
66 if (!dfu) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090067 pr_err("DFU entity for alt: %d not found!", alt_setting_num);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020068 ret = -ENODEV;
69 goto done;
70 }
71
AKASHI Takahiro1c2d1292020-10-29 13:47:42 +090072 ret = dfu_write_from_mem_addr(dfu, (void *)addr, len);
Lukasz Majewski2d50d682015-08-24 00:21:45 +020073
74done:
75 dfu_free_entities();
76
77 return ret;
78}
AKASHI Takahirof2345662020-10-29 13:47:52 +090079
80/**
81 * dfu_write_by_alt() - write data to DFU medium
82 * @dfu_alt_num: DFU alt setting number
83 * @addr: Address of data buffer to write
84 * @len: Number of bytes
85 * @interface: Destination DFU medium (e.g. "mmc")
86 * @devstring: Instance number of destination DFU medium (e.g. "1")
87 *
88 * This function is storing data received on DFU supported medium which
89 * is specified by @dfu_alt_name.
90 *
91 * Return: 0 - on success, error code - otherwise
92 */
93int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len,
94 char *interface, char *devstring)
95{
96 struct dfu_entity *dfu;
97 int ret;
98
99 debug("%s: alt: %d addr: 0x%p len: %d device: %s:%s\n", __func__,
100 dfu_alt_num, addr, len, interface, devstring);
101
102 ret = dfu_init_env_entities(interface, devstring);
103 if (ret)
104 goto done;
105
106 if (dfu_alt_num < 0) {
107 pr_err("Invalid alt number: %d", dfu_alt_num);
108 ret = -ENODEV;
109 goto done;
110 }
111
112 dfu = dfu_get_entity(dfu_alt_num);
113 if (!dfu) {
114 pr_err("DFU entity for alt: %d not found!", dfu_alt_num);
115 ret = -ENODEV;
116 goto done;
117 }
118
119 ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len);
120
121done:
122 dfu_free_entities();
123
124 return ret;
125}