/src/u-boot/drivers/dfu/dfu_alt.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0+ |
2 | | /* |
3 | | * (C) Copyright 2015 |
4 | | * Lukasz Majewski <l.majewski@majess.pl> |
5 | | */ |
6 | | |
7 | | #include <log.h> |
8 | | #include <malloc.h> |
9 | | #include <errno.h> |
10 | | #include <dfu.h> |
11 | | |
12 | | /** |
13 | | * dfu_write_by_name() - write data to DFU medium |
14 | | * @dfu_entity_name: Name of DFU entity to write |
15 | | * @addr: Address of data buffer to write |
16 | | * @len: Number of bytes |
17 | | * @interface: Destination DFU medium (e.g. "mmc") |
18 | | * @devstring: Instance number of destination DFU medium (e.g. "1") |
19 | | * |
20 | | * This function is storing data received on DFU supported medium which |
21 | | * is specified by @dfu_entity_name. |
22 | | * |
23 | | * Return: 0 - on success, error code - otherwise |
24 | | */ |
25 | | int dfu_write_by_name(char *dfu_entity_name, void *addr, |
26 | | unsigned int len, char *interface, char *devstring) |
27 | 0 | { |
28 | 0 | char *s, *sb; |
29 | 0 | int alt_setting_num, ret; |
30 | 0 | struct dfu_entity *dfu; |
31 | |
|
32 | 0 | debug("%s: name: %s addr: 0x%p len: %d device: %s:%s\n", __func__, |
33 | 0 | dfu_entity_name, addr, len, interface, devstring); |
34 | |
|
35 | 0 | ret = dfu_init_env_entities(interface, devstring); |
36 | 0 | if (ret) |
37 | 0 | goto done; |
38 | | |
39 | | /* |
40 | | * We need to copy name pointed by *dfu_entity_name since this text |
41 | | * is the integral part of the FDT image. |
42 | | * Any implicit modification (i.e. done by strsep()) will corrupt |
43 | | * the FDT image and prevent other images to be stored. |
44 | | */ |
45 | 0 | s = strdup(dfu_entity_name); |
46 | 0 | sb = s; |
47 | 0 | if (!s) { |
48 | 0 | ret = -ENOMEM; |
49 | 0 | goto done; |
50 | 0 | } |
51 | | |
52 | 0 | strsep(&s, "@"); |
53 | 0 | debug("%s: image name: %s strlen: %zd\n", __func__, sb, strlen(sb)); |
54 | |
|
55 | 0 | alt_setting_num = dfu_get_alt(sb); |
56 | 0 | free(sb); |
57 | 0 | if (alt_setting_num < 0) { |
58 | 0 | pr_err("Alt setting [%d] to write not found!", |
59 | 0 | alt_setting_num); |
60 | 0 | ret = -ENODEV; |
61 | 0 | goto done; |
62 | 0 | } |
63 | | |
64 | 0 | dfu = dfu_get_entity(alt_setting_num); |
65 | 0 | if (!dfu) { |
66 | 0 | pr_err("DFU entity for alt: %d not found!", alt_setting_num); |
67 | 0 | ret = -ENODEV; |
68 | 0 | goto done; |
69 | 0 | } |
70 | | |
71 | 0 | ret = dfu_write_from_mem_addr(dfu, (void *)addr, len); |
72 | |
|
73 | 0 | done: |
74 | 0 | dfu_free_entities(); |
75 | |
|
76 | 0 | return ret; |
77 | 0 | } |
78 | | |
79 | | /** |
80 | | * dfu_write_by_alt() - write data to DFU medium |
81 | | * @dfu_alt_num: DFU alt setting number |
82 | | * @addr: Address of data buffer to write |
83 | | * @len: Number of bytes |
84 | | * @interface: Destination DFU medium (e.g. "mmc") |
85 | | * @devstring: Instance number of destination DFU medium (e.g. "1") |
86 | | * |
87 | | * This function is storing data received on DFU supported medium which |
88 | | * is specified by @dfu_alt_name. |
89 | | * |
90 | | * Return: 0 - on success, error code - otherwise |
91 | | */ |
92 | | int dfu_write_by_alt(int dfu_alt_num, void *addr, unsigned int len, |
93 | | char *interface, char *devstring) |
94 | 0 | { |
95 | 0 | struct dfu_entity *dfu; |
96 | 0 | int ret; |
97 | |
|
98 | 0 | debug("%s: alt: %d addr: 0x%p len: %d device: %s:%s\n", __func__, |
99 | 0 | dfu_alt_num, addr, len, interface, devstring); |
100 | |
|
101 | 0 | ret = dfu_init_env_entities(interface, devstring); |
102 | 0 | if (ret) |
103 | 0 | goto done; |
104 | | |
105 | 0 | if (dfu_alt_num < 0) { |
106 | 0 | pr_err("Invalid alt number: %d", dfu_alt_num); |
107 | 0 | ret = -ENODEV; |
108 | 0 | goto done; |
109 | 0 | } |
110 | | |
111 | 0 | dfu = dfu_get_entity(dfu_alt_num); |
112 | 0 | if (!dfu) { |
113 | 0 | pr_err("DFU entity for alt: %d not found!", dfu_alt_num); |
114 | 0 | ret = -ENODEV; |
115 | 0 | goto done; |
116 | 0 | } |
117 | | |
118 | 0 | ret = dfu_write_from_mem_addr(dfu, (void *)(uintptr_t)addr, len); |
119 | |
|
120 | 0 | done: |
121 | 0 | dfu_free_entities(); |
122 | |
|
123 | 0 | return ret; |
124 | 0 | } |