/src/barebox/common/environment.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | /* |
3 | | * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix |
4 | | */ |
5 | | |
6 | | /** |
7 | | * @file |
8 | | * @brief Environment handling support (host and target) |
9 | | * |
10 | | * Important: This file will also be used on the host to create |
11 | | * the default environment when building the barebox binary. So |
12 | | * do not add any new barebox related functions here! |
13 | | */ |
14 | | #ifdef __BAREBOX__ |
15 | | #include <common.h> |
16 | | #include <command.h> |
17 | | #include <malloc.h> |
18 | | #include <errno.h> |
19 | | #include <fs.h> |
20 | | #include <crc.h> |
21 | | #include <fcntl.h> |
22 | | #include <envfs.h> |
23 | | #include <xfuncs.h> |
24 | | #include <libbb.h> |
25 | | #include <libgen.h> |
26 | | #include <environment.h> |
27 | | #include <globalvar.h> |
28 | | #include <libfile.h> |
29 | | #include <block.h> |
30 | | #include <efi/partition.h> |
31 | | #include <bootsource.h> |
32 | | #include <magicvar.h> |
33 | | #include <security/config.h> |
34 | | #else |
35 | | #define EXPORT_SYMBOL(x) |
36 | | #endif |
37 | | |
38 | | struct envfs_entry { |
39 | | char *name; |
40 | | void *buf; |
41 | | int size; |
42 | | struct envfs_entry *next; |
43 | | mode_t mode; |
44 | | }; |
45 | | |
46 | | struct action_data { |
47 | | const char *base; |
48 | | void *writep; |
49 | | struct envfs_entry *env; |
50 | | }; |
51 | | |
52 | | #ifdef __BAREBOX__ |
53 | | |
54 | 0 | #define TMPDIR "/.defaultenv" |
55 | | |
56 | | static char *default_environment_path; |
57 | | |
58 | | void default_environment_path_set(const char *path) |
59 | 30 | { |
60 | 30 | free(default_environment_path); |
61 | | |
62 | 30 | default_environment_path = xstrdup(path); |
63 | 30 | } |
64 | | |
65 | | static guid_t partition_barebox_env_guid = PARTITION_BAREBOX_ENVIRONMENT_GUID; |
66 | | |
67 | | /* |
68 | | * default_environment_path_search - look for environment partition |
69 | | * |
70 | | * This searches for a barebox environment partition on block devices. barebox |
71 | | * environment partitions are recognized by the guid |
72 | | * 6c3737f2-07f8-45d1-ad45-15d260aab24d. The device barebox itself has booted |
73 | | * from is preferred over other devices. |
74 | | * |
75 | | * @return: The cdev providing the environment of found, NULL otherwise |
76 | | */ |
77 | | static struct cdev *default_environment_path_search(void) |
78 | 0 | { |
79 | 0 | struct cdev *part; |
80 | 0 | struct device_node *boot_node; |
81 | 0 | int max_score = 0; |
82 | 0 | struct cdev *env_cdev = NULL; |
83 | 0 | struct block_device *blk; |
84 | |
|
85 | 0 | if (!IS_ENABLED(CONFIG_BLOCK) || !IS_ENABLED(CONFIG_ENV_HANDLING_AUTOPROBE)) |
86 | 0 | return NULL; |
87 | | |
88 | 0 | boot_node = bootsource_of_node_get(NULL); |
89 | |
|
90 | 0 | if (boot_node) { |
91 | 0 | struct device *dev; |
92 | |
|
93 | 0 | dev = of_find_device_by_node(boot_node); |
94 | 0 | if (dev) |
95 | 0 | device_detect(dev); |
96 | 0 | } |
97 | |
|
98 | 0 | for_each_block_device(blk) { |
99 | 0 | int score = 0; |
100 | |
|
101 | 0 | part = cdev_find_child_by_gpt_typeuuid(&blk->cdev, |
102 | 0 | &partition_barebox_env_guid); |
103 | 0 | if (IS_ERR(part)) |
104 | 0 | continue; |
105 | | |
106 | | /* Prefer eMMC over SD if neither is the boot medium */ |
107 | 0 | if (blk->removable) |
108 | 0 | score += 1; |
109 | 0 | else |
110 | 0 | score += 2; |
111 | |
|
112 | 0 | if (boot_node && boot_node == blk->cdev.device_node) |
113 | 0 | score += 4; |
114 | |
|
115 | 0 | if (score > max_score) { |
116 | 0 | max_score = score; |
117 | 0 | env_cdev = part; |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | 0 | return env_cdev; |
122 | 0 | } |
123 | | |
124 | | const char *default_environment_path_get(void) |
125 | 60 | { |
126 | 60 | struct cdev *cdev; |
127 | | |
128 | 60 | if (default_environment_path && *default_environment_path) |
129 | 60 | return default_environment_path; |
130 | | |
131 | 0 | cdev = default_environment_path_search(); |
132 | 0 | if (cdev) |
133 | 0 | default_environment_path = basprintf("/dev/%s", cdev->name); |
134 | |
|
135 | 0 | return default_environment_path; |
136 | 60 | } |
137 | | |
138 | | static int do_compare_file(const char *filename, const char *base) |
139 | 0 | { |
140 | 0 | int ret; |
141 | 0 | char *cmp; |
142 | 0 | const char *relname = filename + strlen(base) + 1; |
143 | |
|
144 | 0 | cmp = basprintf("%s/%s", TMPDIR, relname); |
145 | 0 | ret = compare_file(cmp, filename); |
146 | |
|
147 | 0 | free(cmp); |
148 | |
|
149 | 0 | return ret; |
150 | 0 | } |
151 | | |
152 | | #else |
153 | | #define ERASE_SIZE_ALL 0 |
154 | | static inline int protect(int fd, size_t count, unsigned long offset, int prot) |
155 | | { |
156 | | return 0; |
157 | | } |
158 | | |
159 | | enum erase_type { |
160 | | ERASE_TO_WRITE |
161 | | }; |
162 | | static inline int erase(int fd, size_t count, unsigned long offset, enum erase_type type) |
163 | | { |
164 | | return 0; |
165 | | } |
166 | | |
167 | | static int do_compare_file(const char *filename, const char *base) |
168 | | { |
169 | | return 1; |
170 | | } |
171 | | #endif |
172 | | |
173 | | static int file_action(const char *filename, struct stat *statbuf, |
174 | | void *userdata, int depth) |
175 | 0 | { |
176 | 0 | struct action_data *data = userdata; |
177 | 0 | struct envfs_entry *env; |
178 | 0 | int fd, ret; |
179 | |
|
180 | 0 | if (!do_compare_file(filename, data->base)) |
181 | 0 | return 1; |
182 | | |
183 | 0 | env = xzalloc(sizeof(*env)); |
184 | 0 | env->name = strdup(filename + strlen(data->base)); |
185 | 0 | env->size = statbuf->st_size; |
186 | |
|
187 | 0 | env->buf = calloc(env->size + 1, 1); |
188 | 0 | if (!env->buf) |
189 | 0 | goto out; |
190 | | |
191 | 0 | env->mode = S_IRWXU | S_IRWXG | S_IRWXO; |
192 | |
|
193 | 0 | if (S_ISLNK(statbuf->st_mode)) { |
194 | 0 | env->size++; |
195 | |
|
196 | 0 | ret = readlink(filename, env->buf, env->size); |
197 | 0 | if (ret < 0) { |
198 | 0 | perror("read"); |
199 | 0 | goto out; |
200 | 0 | } |
201 | | |
202 | 0 | env->mode |= S_IFLNK; |
203 | 0 | } else { |
204 | 0 | fd = open(filename, O_RDONLY); |
205 | 0 | if (fd < 0) |
206 | 0 | return fd; |
207 | | |
208 | 0 | ret = read(fd, env->buf, env->size); |
209 | |
|
210 | 0 | close(fd); |
211 | |
|
212 | 0 | if (ret < env->size) { |
213 | 0 | perror("read"); |
214 | 0 | goto out; |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | 0 | env->next = data->env; |
219 | 0 | data->env = env; |
220 | 0 | out: |
221 | 0 | return 1; |
222 | 0 | } |
223 | | |
224 | | static void envfs_save_inode(struct action_data *data, struct envfs_entry *env) |
225 | 0 | { |
226 | 0 | struct envfs_inode *inode; |
227 | 0 | struct envfs_inode_end *inode_end; |
228 | 0 | int namelen = strlen(env->name) + 1; |
229 | |
|
230 | 0 | inode = data->writep; |
231 | 0 | inode->magic = ENVFS_32(ENVFS_INODE_MAGIC); |
232 | 0 | inode->headerlen = ENVFS_32(PAD4(namelen) + sizeof(struct envfs_inode_end)); |
233 | 0 | inode->size = ENVFS_32(env->size); |
234 | |
|
235 | 0 | data->writep += sizeof(struct envfs_inode); |
236 | |
|
237 | 0 | strcpy(data->writep, env->name); |
238 | 0 | data->writep += PAD4(namelen); |
239 | |
|
240 | 0 | inode_end = data->writep; |
241 | 0 | inode_end->magic = ENVFS_32(ENVFS_INODE_END_MAGIC); |
242 | 0 | inode_end->mode = ENVFS_32(env->mode); |
243 | 0 | data->writep += sizeof(struct envfs_inode_end); |
244 | |
|
245 | 0 | memcpy(data->writep, env->buf, env->size); |
246 | 0 | data->writep += PAD4(env->size); |
247 | 0 | } |
248 | | |
249 | | #ifdef __BAREBOX__ |
250 | | static int file_remove_action(const char *filename, struct stat *statbuf, |
251 | | void *userdata, int depth) |
252 | 0 | { |
253 | 0 | struct action_data *data = userdata; |
254 | 0 | char *envname; |
255 | 0 | struct stat s; |
256 | 0 | int ret; |
257 | 0 | struct envfs_entry *env; |
258 | |
|
259 | 0 | filename += sizeof(TMPDIR) - 1; |
260 | |
|
261 | 0 | envname = basprintf("%s/%s", data->base, filename); |
262 | |
|
263 | 0 | ret = stat(envname, &s); |
264 | 0 | if (ret) { |
265 | 0 | char *base; |
266 | |
|
267 | 0 | base = basename(envname); |
268 | |
|
269 | 0 | env = xzalloc(sizeof(*env)); |
270 | 0 | env->name = strdup(filename); |
271 | 0 | env->size = strlen(base) + 1; |
272 | |
|
273 | 0 | env->buf = strdup(base); |
274 | 0 | if (!env->buf) |
275 | 0 | goto out; |
276 | | |
277 | 0 | env->mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFLNK; |
278 | |
|
279 | 0 | env->next = data->env; |
280 | 0 | data->env = env; |
281 | 0 | } |
282 | 0 | out: |
283 | 0 | free(envname); |
284 | |
|
285 | 0 | return 1; |
286 | 0 | } |
287 | | #else |
288 | | static int file_remove_action(const char *filename, struct stat *statbuf, |
289 | | void *userdata, int depth) |
290 | | { |
291 | | return 0; |
292 | | } |
293 | | #endif |
294 | | |
295 | | /** |
296 | | * Make the current environment persistent |
297 | | * @param[in] filename where to store |
298 | | * @param[in] dirname what to store (all files in this dir) |
299 | | * @param[in] flags superblock flags (refer ENVFS_FLAGS_* macros) |
300 | | * @return 0 on success, anything else in case of failure |
301 | | * |
302 | | * Note: This function will also be used on the host! See note in the header |
303 | | * of this file. |
304 | | */ |
305 | | int envfs_save(const char *filename, const char *dirname, unsigned flags) |
306 | 0 | { |
307 | 0 | struct envfs_super *super; |
308 | 0 | int envfd, size, ret; |
309 | 0 | struct action_data data = {}; |
310 | 0 | void *buf = NULL, *wbuf; |
311 | 0 | struct envfs_entry *env; |
312 | 0 | __maybe_unused const char *defenv_path; |
313 | |
|
314 | 0 | if (!filename) |
315 | 0 | return -ENOENT; |
316 | | |
317 | 0 | if (!dirname) |
318 | 0 | dirname = "/env"; |
319 | |
|
320 | 0 | data.writep = NULL; |
321 | 0 | data.base = dirname; |
322 | |
|
323 | 0 | #ifdef __BAREBOX__ |
324 | 0 | defaultenv_load(TMPDIR, 0); |
325 | 0 | #endif |
326 | |
|
327 | 0 | if (flags & ENVFS_FLAGS_FORCE_BUILT_IN) { |
328 | 0 | size = 0; /* force no content */ |
329 | 0 | } else { |
330 | | /* first pass: calculate size */ |
331 | 0 | recursive_action(dirname, ACTION_RECURSE | ACTION_SORT, file_action, |
332 | 0 | NULL, &data, 0); |
333 | 0 | recursive_action("/.defaultenv", ACTION_RECURSE | ACTION_SORT, |
334 | 0 | file_remove_action, NULL, &data, 0); |
335 | 0 | size = 0; |
336 | |
|
337 | 0 | for (env = data.env; env; env = env->next) { |
338 | 0 | size += PAD4(env->size); |
339 | 0 | size += sizeof(struct envfs_inode); |
340 | 0 | size += PAD4(strlen(env->name) + 1); |
341 | 0 | size += sizeof(struct envfs_inode_end); |
342 | 0 | } |
343 | 0 | } |
344 | |
|
345 | 0 | buf = xzalloc(size + sizeof(struct envfs_super)); |
346 | 0 | data.writep = buf + sizeof(struct envfs_super); |
347 | |
|
348 | 0 | super = buf; |
349 | 0 | super->magic = ENVFS_32(ENVFS_MAGIC); |
350 | 0 | super->major = ENVFS_MAJOR; |
351 | 0 | super->minor = ENVFS_MINOR; |
352 | 0 | super->size = ENVFS_32(size); |
353 | 0 | super->flags = ENVFS_32(flags); |
354 | |
|
355 | 0 | if (!(flags & ENVFS_FLAGS_FORCE_BUILT_IN)) { |
356 | | /* second pass: copy files to buffer */ |
357 | 0 | env = data.env; |
358 | 0 | while (env) { |
359 | 0 | struct envfs_entry *next = env->next; |
360 | |
|
361 | 0 | envfs_save_inode(&data, env); |
362 | |
|
363 | 0 | free(env->buf); |
364 | 0 | free(env->name); |
365 | 0 | free(env); |
366 | 0 | env = next; |
367 | 0 | } |
368 | 0 | } |
369 | |
|
370 | 0 | super->crc = ENVFS_32(crc32(0, buf + sizeof(struct envfs_super), size)); |
371 | 0 | super->sb_crc = ENVFS_32(crc32(0, buf, sizeof(struct envfs_super) - 4)); |
372 | |
|
373 | 0 | envfd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
374 | 0 | if (envfd < 0) { |
375 | 0 | printf("could not open %s: %m\n", filename); |
376 | 0 | ret = -errno; |
377 | 0 | goto out1; |
378 | 0 | } |
379 | | |
380 | 0 | ret = protect(envfd, ~0, 0, 0); |
381 | | |
382 | | /* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ |
383 | 0 | if (ret && errno != ENOSYS && errno != EOPNOTSUPP) { |
384 | 0 | printf("could not unprotect %s: %m\n", filename); |
385 | 0 | goto out; |
386 | 0 | } |
387 | | |
388 | 0 | ret = erase(envfd, ERASE_SIZE_ALL, 0, ERASE_TO_WRITE); |
389 | | |
390 | | /* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ |
391 | 0 | if (ret && errno != ENOSYS && errno != EOPNOTSUPP) { |
392 | 0 | printf("could not erase %s: %m\n", filename); |
393 | 0 | goto out; |
394 | 0 | } |
395 | | |
396 | 0 | size += sizeof(struct envfs_super); |
397 | |
|
398 | 0 | wbuf = buf; |
399 | |
|
400 | 0 | while (size) { |
401 | 0 | ssize_t now = write(envfd, wbuf, size); |
402 | 0 | if (now < 0) { |
403 | 0 | ret = -errno; |
404 | 0 | goto out; |
405 | 0 | } |
406 | | |
407 | 0 | wbuf += now; |
408 | 0 | size -= now; |
409 | 0 | } |
410 | | |
411 | 0 | ret = protect(envfd, ~0, 0, 1); |
412 | | |
413 | | /* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ |
414 | 0 | if (ret && errno != ENOSYS && errno != EOPNOTSUPP) { |
415 | 0 | printf("could not protect %s: %m\n", filename); |
416 | 0 | goto out; |
417 | 0 | } |
418 | | |
419 | 0 | ret = 0; |
420 | |
|
421 | 0 | #ifdef CONFIG_NVVAR |
422 | 0 | defenv_path = default_environment_path_get(); |
423 | 0 | if (defenv_path && !strcmp(filename, defenv_path)) |
424 | 0 | nv_var_set_clean(); |
425 | 0 | #endif |
426 | 0 | out: |
427 | 0 | close(envfd); |
428 | 0 | out1: |
429 | 0 | free(buf); |
430 | 0 | #ifdef __BAREBOX__ |
431 | 0 | unlink_recursive(TMPDIR, NULL); |
432 | 0 | #endif |
433 | 0 | return ret; |
434 | 0 | } |
435 | | EXPORT_SYMBOL(envfs_save); |
436 | | |
437 | | /** |
438 | | * Restore the last environment into the current one |
439 | | * @param[in] filename from where to restore |
440 | | * @param[in] dir where to store the last content |
441 | | * @return 0 on success, anything else in case of failure |
442 | | * |
443 | | * Note: This function will also be used on the host! See note in the header |
444 | | * of this file. |
445 | | */ |
446 | | int envfs_load(const char *filename, const char *dir, unsigned flags) |
447 | 30 | { |
448 | 30 | struct envfs_super super; |
449 | 30 | void *buf = NULL, *rbuf; |
450 | 30 | int envfd; |
451 | 30 | int ret = 0; |
452 | 30 | size_t size, rsize; |
453 | 30 | __maybe_unused const char *defenv_path; |
454 | | |
455 | 30 | #ifdef __BAREBOX__ |
456 | 30 | if (!IS_ALLOWED(SCONFIG_ENVIRONMENT_LOAD)) |
457 | 0 | return -EPERM; |
458 | 30 | #endif |
459 | | |
460 | 30 | if (!filename) |
461 | 0 | filename = default_environment_path_get(); |
462 | 30 | if (!filename) |
463 | 0 | return -ENOENT; |
464 | | |
465 | 30 | if (!dir) |
466 | 0 | dir = "/env"; |
467 | | |
468 | 30 | envfd = open(filename, O_RDONLY); |
469 | 30 | if (envfd < 0) { |
470 | 0 | printf("environment load %s: %m\n", filename); |
471 | 0 | if (errno == ENOENT) |
472 | 0 | printf("Maybe you have to create the partition.\n"); |
473 | 0 | return -1; |
474 | 0 | } |
475 | | |
476 | | /* read superblock */ |
477 | 30 | ret = read(envfd, &super, sizeof(struct envfs_super)); |
478 | 30 | if ( ret < sizeof(struct envfs_super)) { |
479 | 0 | perror("read"); |
480 | 0 | ret = -errno; |
481 | 0 | goto out; |
482 | 0 | } |
483 | | |
484 | 30 | ret = envfs_check_super(&super, &size); |
485 | 30 | if (ret) |
486 | 0 | goto out; |
487 | | |
488 | 30 | if (super.flags & ENVFS_FLAGS_FORCE_BUILT_IN) { |
489 | 0 | printf("found force-builtin environment, using defaultenv\n"); |
490 | 0 | ret = defaultenv_load(dir, 0); |
491 | 0 | if (ret) |
492 | 0 | printf("failed to load default environment: %s\n", |
493 | 0 | strerror(-ret)); |
494 | 0 | goto out; |
495 | 0 | } |
496 | | |
497 | 30 | buf = xmalloc(size); |
498 | | |
499 | 30 | rbuf = buf; |
500 | 30 | rsize = size; |
501 | | |
502 | 30 | while (rsize) { |
503 | 0 | ssize_t now; |
504 | |
|
505 | 0 | now = read(envfd, rbuf, rsize); |
506 | 0 | if (now < 0) { |
507 | 0 | perror("read"); |
508 | 0 | ret = -errno; |
509 | 0 | goto out; |
510 | 0 | } |
511 | | |
512 | 0 | if (!now) { |
513 | 0 | printf("%s: premature end of file\n", filename); |
514 | 0 | ret = -EINVAL; |
515 | 0 | goto out; |
516 | 0 | } |
517 | | |
518 | 0 | rbuf += now; |
519 | 0 | rsize -= now; |
520 | 0 | } |
521 | | |
522 | 30 | ret = envfs_check_data(&super, buf, size); |
523 | 30 | if (ret) |
524 | 0 | goto out; |
525 | | |
526 | 30 | ret = envfs_load_data(&super, buf, size, dir, flags); |
527 | 30 | if (ret) |
528 | 0 | goto out; |
529 | | |
530 | 30 | ret = 0; |
531 | | |
532 | 30 | #ifdef CONFIG_NVVAR |
533 | 30 | defenv_path = default_environment_path_get(); |
534 | 30 | if (defenv_path && !strcmp(filename, defenv_path)) |
535 | 30 | nv_var_set_persistable(); |
536 | 30 | #endif |
537 | | |
538 | 30 | out: |
539 | 30 | close(envfd); |
540 | 30 | free(buf); |
541 | | |
542 | 30 | return ret; |
543 | 30 | } |
544 | | |
545 | | #ifdef __BAREBOX__ |
546 | | static int register_env_vars(void) |
547 | 30 | { |
548 | 30 | globalvar_add_simple_string("env.path", &default_environment_path); |
549 | | |
550 | 30 | return 0; |
551 | 30 | } |
552 | | postcore_initcall(register_env_vars); |
553 | | BAREBOX_MAGICVAR(global.env.path, |
554 | | "The path the environment is stored"); |
555 | | #endif |