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