Merge branch 'master' of git://git.denx.de/u-boot-usb
[oweals/u-boot.git] / drivers / fastboot / fb_getvar.c
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (C) 2016 The Android Open Source Project
4  */
5
6 #include <common.h>
7 #include <fastboot.h>
8 #include <fastboot-internal.h>
9 #include <fb_mmc.h>
10 #include <fb_nand.h>
11 #include <fs.h>
12 #include <version.h>
13
14 static void getvar_version(char *var_parameter, char *response);
15 static void getvar_version_bootloader(char *var_parameter, char *response);
16 static void getvar_downloadsize(char *var_parameter, char *response);
17 static void getvar_serialno(char *var_parameter, char *response);
18 static void getvar_version_baseband(char *var_parameter, char *response);
19 static void getvar_product(char *var_parameter, char *response);
20 static void getvar_platform(char *var_parameter, char *response);
21 static void getvar_current_slot(char *var_parameter, char *response);
22 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
23 static void getvar_has_slot(char *var_parameter, char *response);
24 #endif
25 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
26 static void getvar_partition_type(char *part_name, char *response);
27 #endif
28 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
29 static void getvar_partition_size(char *part_name, char *response);
30 #endif
31 static void getvar_is_userspace(char *var_parameter, char *response);
32
33 static const struct {
34         const char *variable;
35         void (*dispatch)(char *var_parameter, char *response);
36 } getvar_dispatch[] = {
37         {
38                 .variable = "version",
39                 .dispatch = getvar_version
40         }, {
41                 .variable = "version-bootloader",
42                 .dispatch = getvar_version_bootloader
43         }, {
44                 .variable = "downloadsize",
45                 .dispatch = getvar_downloadsize
46         }, {
47                 .variable = "max-download-size",
48                 .dispatch = getvar_downloadsize
49         }, {
50                 .variable = "serialno",
51                 .dispatch = getvar_serialno
52         }, {
53                 .variable = "version-baseband",
54                 .dispatch = getvar_version_baseband
55         }, {
56                 .variable = "product",
57                 .dispatch = getvar_product
58         }, {
59                 .variable = "platform",
60                 .dispatch = getvar_platform
61         }, {
62                 .variable = "current-slot",
63                 .dispatch = getvar_current_slot
64 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
65         }, {
66                 .variable = "has-slot",
67                 .dispatch = getvar_has_slot
68 #endif
69 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
70         }, {
71                 .variable = "partition-type",
72                 .dispatch = getvar_partition_type
73 #endif
74 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
75         }, {
76                 .variable = "partition-size",
77                 .dispatch = getvar_partition_size
78 #endif
79         }, {
80                 .variable = "is-userspace",
81                 .dispatch = getvar_is_userspace
82         }
83 };
84
85 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
86 /**
87  * Get partition number and size for any storage type.
88  *
89  * Can be used to check if partition with specified name exists.
90  *
91  * If error occurs, this function guarantees to fill @p response with fail
92  * string. @p response can be rewritten in caller, if needed.
93  *
94  * @param[in] part_name Info for which partition name to look for
95  * @param[in,out] response Pointer to fastboot response buffer
96  * @param[out] size If not NULL, will contain partition size (in blocks)
97  * @return Partition number or negative value on error
98  */
99 static int getvar_get_part_info(const char *part_name, char *response,
100                                 size_t *size)
101 {
102         int r;
103 # if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
104         struct blk_desc *dev_desc;
105         disk_partition_t part_info;
106
107         r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
108                                        response);
109         if (r >= 0 && size)
110                 *size = part_info.size;
111 # elif CONFIG_IS_ENABLED(FASTBOOT_FLASH_NAND)
112         struct part_info *part_info;
113
114         r = fastboot_nand_get_part_info(part_name, &part_info, response);
115         if (r >= 0 && size)
116                 *size = part_info->size;
117 # else
118         fastboot_fail("this storage is not supported in bootloader", response);
119         r = -ENODEV;
120 # endif
121
122         return r;
123 }
124 #endif
125
126 static void getvar_version(char *var_parameter, char *response)
127 {
128         fastboot_okay(FASTBOOT_VERSION, response);
129 }
130
131 static void getvar_version_bootloader(char *var_parameter, char *response)
132 {
133         fastboot_okay(U_BOOT_VERSION, response);
134 }
135
136 static void getvar_downloadsize(char *var_parameter, char *response)
137 {
138         fastboot_response("OKAY", response, "0x%08x", fastboot_buf_size);
139 }
140
141 static void getvar_serialno(char *var_parameter, char *response)
142 {
143         const char *tmp = env_get("serial#");
144
145         if (tmp)
146                 fastboot_okay(tmp, response);
147         else
148                 fastboot_fail("Value not set", response);
149 }
150
151 static void getvar_version_baseband(char *var_parameter, char *response)
152 {
153         fastboot_okay("N/A", response);
154 }
155
156 static void getvar_product(char *var_parameter, char *response)
157 {
158         const char *board = env_get("board");
159
160         if (board)
161                 fastboot_okay(board, response);
162         else
163                 fastboot_fail("Board not set", response);
164 }
165
166 static void getvar_platform(char *var_parameter, char *response)
167 {
168         const char *p = env_get("platform");
169
170         if (p)
171                 fastboot_okay(p, response);
172         else
173                 fastboot_fail("platform not set", response);
174 }
175
176 static void getvar_current_slot(char *var_parameter, char *response)
177 {
178         /* A/B not implemented, for now always return "a" */
179         fastboot_okay("a", response);
180 }
181
182 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
183 static void getvar_has_slot(char *part_name, char *response)
184 {
185         char part_name_wslot[PART_NAME_LEN];
186         size_t len;
187         int r;
188
189         if (!part_name || part_name[0] == '\0')
190                 goto fail;
191
192         /* part_name_wslot = part_name + "_a" */
193         len = strlcpy(part_name_wslot, part_name, PART_NAME_LEN - 3);
194         if (len > PART_NAME_LEN - 3)
195                 goto fail;
196         strcat(part_name_wslot, "_a");
197
198         r = getvar_get_part_info(part_name_wslot, response, NULL);
199         if (r >= 0) {
200                 fastboot_okay("yes", response); /* part exists and slotted */
201                 return;
202         }
203
204         r = getvar_get_part_info(part_name, response, NULL);
205         if (r >= 0)
206                 fastboot_okay("no", response); /* part exists but not slotted */
207
208         /* At this point response is filled with okay or fail string */
209         return;
210
211 fail:
212         fastboot_fail("invalid partition name", response);
213 }
214 #endif
215
216 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH_MMC)
217 static void getvar_partition_type(char *part_name, char *response)
218 {
219         int r;
220         struct blk_desc *dev_desc;
221         disk_partition_t part_info;
222
223         r = fastboot_mmc_get_part_info(part_name, &dev_desc, &part_info,
224                                        response);
225         if (r >= 0) {
226                 r = fs_set_blk_dev_with_part(dev_desc, r);
227                 if (r < 0)
228                         fastboot_fail("failed to set partition", response);
229                 else
230                         fastboot_okay(fs_get_type_name(), response);
231         }
232 }
233 #endif
234
235 #if CONFIG_IS_ENABLED(FASTBOOT_FLASH)
236 static void getvar_partition_size(char *part_name, char *response)
237 {
238         int r;
239         size_t size;
240
241         r = getvar_get_part_info(part_name, response, &size);
242         if (r >= 0)
243                 fastboot_response("OKAY", response, "0x%016zx", size);
244 }
245 #endif
246
247 static void getvar_is_userspace(char *var_parameter, char *response)
248 {
249         fastboot_okay("no", response);
250 }
251
252 /**
253  * fastboot_getvar() - Writes variable indicated by cmd_parameter to response.
254  *
255  * @cmd_parameter: Pointer to command parameter
256  * @response: Pointer to fastboot response buffer
257  *
258  * Look up cmd_parameter first as an environment variable of the form
259  * fastboot.<cmd_parameter>, if that exists return use its value to set
260  * response.
261  *
262  * Otherwise lookup the name of variable and execute the appropriate
263  * function to return the requested value.
264  */
265 void fastboot_getvar(char *cmd_parameter, char *response)
266 {
267         if (!cmd_parameter) {
268                 fastboot_fail("missing var", response);
269         } else {
270 #define FASTBOOT_ENV_PREFIX     "fastboot."
271                 int i;
272                 char *var_parameter = cmd_parameter;
273                 char envstr[FASTBOOT_RESPONSE_LEN];
274                 const char *s;
275
276                 snprintf(envstr, sizeof(envstr) - 1,
277                          FASTBOOT_ENV_PREFIX "%s", cmd_parameter);
278                 s = env_get(envstr);
279                 if (s) {
280                         fastboot_response("OKAY", response, "%s", s);
281                         return;
282                 }
283
284                 strsep(&var_parameter, ":");
285                 for (i = 0; i < ARRAY_SIZE(getvar_dispatch); ++i) {
286                         if (!strcmp(getvar_dispatch[i].variable,
287                                     cmd_parameter)) {
288                                 getvar_dispatch[i].dispatch(var_parameter,
289                                                             response);
290                                 return;
291                         }
292                 }
293                 pr_warn("WARNING: unknown variable: %s\n", cmd_parameter);
294                 fastboot_fail("Variable not implemented", response);
295         }
296 }