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