summaryrefslogtreecommitdiffstats
path: root/common/globalvar.c
blob: a55b38b00fd54e066d49438e378058944a5b2880 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#include <common.h>
#include <malloc.h>
#include <globalvar.h>
#include <errno.h>
#include <init.h>
#include <environment.h>
#include <magicvar.h>
#include <fs.h>
#include <fcntl.h>
#include <libfile.h>
#include <generated/utsrelease.h>
#include <envfs.h>
#include <fnmatch.h>

static int nv_dirty;

struct device_d global_device = {
	.name = "global",
	.id = DEVICE_ID_SINGLE,
};

struct device_d nv_device = {
	.name = "nv",
	.id = DEVICE_ID_SINGLE,
};

void nv_var_set_clean(void)
{
	nv_dirty = 0;
}

void globalvar_remove(const char *name)
{
	struct param_d *p, *tmp;

	list_for_each_entry_safe(p, tmp, &global_device.parameters, list) {
		if (fnmatch(name, p->name, 0))
			continue;

		dev_remove_param(p);
	}
}

static int __nv_save(const char *prefix, const char *name, const char *val)
{
	int fd, ret;
	char *fname;

	ret = make_directory(prefix);
	if (ret)
		return ret;

	fname = basprintf("%s/%s", prefix, name);

	fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC);

	free(fname);

	if (fd < 0)
		return fd;

	dprintf(fd, "%s", val);

	close(fd);

	return 0;
}

static int nv_save(const char *name, const char *val)
{
	int ret;
	static int once = 1;

	ret = __nv_save("/env/nv", name, val);
	if (ret)
		return ret;

	if (IS_ENABLED(CONFIG_ENV_HANDLING)) {
		if (once) {
			pr_info("nv variable modified, will save nv variables on shutdown\n");
			once = 0;
		}
		nv_dirty = 1;
	} else {
		if (once) {
			pr_info("nv variable modified, but won't be saved in this configuration\n");
			once = 0;
		}
	}

	return 0;
}

/**
 * dev_param_init_from_nv - initialize a device parameter from nv variable
 * @dev: The device
 * @name: The parameter name
 *
 * This function initializes a newly created device parameter from the corresponding
 * nv.dev.<devname>.<paramname> variable.
 */
void dev_param_init_from_nv(struct device_d *dev, const char *name)
{
	char *nvname;
	const char *val;
	int ret = 0;

	if (!IS_ENABLED(CONFIG_NVVAR))
		return;

	if (dev == &nv_device)
		return;
	if (dev == &global_device)
		return;

	nvname = basprintf("dev.%s.%s", dev_name(dev), name);
	val = dev_get_param(&nv_device, nvname);
	if (val) {
		ret = dev_set_param(dev, name, val);
		if (ret)
			pr_err("Cannot init param from nv: %s.%s=%s: %s\n",
				dev_name(dev), name, val, strerror(-ret));
	}

	free(nvname);
}

/**
 * nvvar_device_dispatch - dispatch dev.<dev>.<param> name into device and parameter name
 * @name: The incoming name in the form dev.<dev>.<param>
 * @dev: The returned device_d * belonging to <dev>
 * @pname: the parameter name
 *
 * Given a dev.<dev>.<param> string this function finds the device_d * belonging to
 * <dev> and the parameter name from <param>.
 *
 * Return: When incoming string does not belong to the device namespace (does not begin
 * with "dev." this function returns 0. A value > 0 is returned when the incoming string
 * is in the device namespace and the string can be dispatched into a device_d * and a
 * parameter name. A negative error code is returned when the incoming string belongs to
 * the device namespace, but cannot be dispatched.
 */
static int nvvar_device_dispatch(const char *name, struct device_d **dev,
				 const char **pname)
{
	char *devname;
	const char *dot;
	int dotpos;

	if (!IS_ENABLED(CONFIG_NVVAR))
		return -ENOSYS;

	*dev = NULL;

	if (strncmp(name, "dev.", 4))
		return 0;

	name += 4;

	dot = strchr(name, '.');
	if (!dot)
		return -EINVAL;

	dotpos = dot - name;

	devname = xstrndup(name, dotpos);
	*dev = get_device_by_name(devname);
	free(devname);

	if (*dev == &nv_device || *dev == &global_device)
		return -EINVAL;

	*pname = dot + 1;

	return 1;
}

static int nv_set(struct device_d *dev, struct param_d *p, const char *name, const char *val)
{
	int ret;

	if (val) {
		ret = dev_set_param(&global_device, name, val);
		if (ret)
			return ret;
	}

	if (p) {
		free(p->value);
		p->value = xstrdup(val);
	}

	return 0;
}

static const char *nv_param_get(struct device_d *dev, struct param_d *p)
{
	return p->value ? p->value : "";
}

static int nv_param_set(struct device_d *dev, struct param_d *p, const char *val)
{
	int ret;

	ret = nv_set(dev, p, p->name, val);
	if (ret)
		return ret;

	return nv_save(p->name, val);
}

static int __nvvar_add(const char *name, const char *value)
{
	struct param_d *p;
	struct device_d *dev = NULL;
	const char *pname;
	int ret;

	if (!IS_ENABLED(CONFIG_NVVAR))
		return -ENOSYS;

	/* Get param. If it doesn't exist yet, create it */
	p = get_param_by_name(&nv_device, name);
	if (!p) {
		p = dev_add_param(&nv_device, name, nv_param_set, nv_param_get, 0);
		if (IS_ERR(p))
			return PTR_ERR(p);
	}

	/* Create corresponding globalvar if it doesn't exist yet */
	ret = globalvar_add_simple(name, value);
	if (ret && ret != -EEXIST)
		return ret;

	if (value)
		return nv_set(&nv_device, p, name, value);

	ret = nvvar_device_dispatch(name, &dev, &pname);
	if (ret > 0)
		value = dev_get_param(dev, pname);
	else
		value = dev_get_param(&global_device, name);

	if (value && p) {
		free(p->value);
		p->value = xstrdup(value);
	}

	return 0;
}

int nvvar_add(const char *name, const char *value)
{
	int ret;

	if (!strncmp(name, "nv.", 3))
		name += 3;

	ret = __nvvar_add(name, value);
	if (ret)
		return ret;

	return nv_save(name, value);
}

int nvvar_remove(const char *name)
{
	struct param_d *p, *tmp;
	char *fname;
	int ret = -ENOENT;

	if (!IS_ENABLED(CONFIG_NVVAR))
		return -ENOSYS;

	list_for_each_entry_safe(p, tmp, &nv_device.parameters, list) {
		if (fnmatch(name, p->name, 0))
			continue;

		fname = basprintf("/env/nv/%s", p->name);

		dev_remove_param(p);

		unlink(fname);
		free(fname);

		ret = 0;
	}

	return ret;
}

struct globalvar_deprecated {
	char *newname;
	char *oldname;
	struct list_head list;
};

static LIST_HEAD(globalvar_deprecated_list);

/*
 * globalvar_alias_deprecated - add an alias
 *
 * @oldname: The old name for the variable
 * @newname: The new name for the variable
 *
 * This function is a helper for globalvars that are renamed from one
 * release to another. when a variable @oldname is found in the persistent
 * environment a warning is issued and its value is written to @newname.
 *
 * Note that when both @oldname and @newname contain values then the values
 * existing in @newname are overwritten.
 */
void globalvar_alias_deprecated(const char *oldname, const char *newname)
{
	struct globalvar_deprecated *gd;

	gd = xzalloc(sizeof(*gd));
	gd->newname = xstrdup(newname);
	gd->oldname = xstrdup(oldname);
	list_add_tail(&gd->list, &globalvar_deprecated_list);
}

static const char *globalvar_new_name(const char *oldname)
{
	struct globalvar_deprecated *gd;

	list_for_each_entry(gd, &globalvar_deprecated_list, list) {
		if (!strcmp(oldname, gd->oldname)) {
			pr_warn("nv.%s is deprecated, converting to nv.%s\n", oldname,
				gd->newname);
			nv_dirty = 1;
			return gd->newname;
		}
	}

	return oldname;
}

int nvvar_load(void)
{
	char *val;
	int ret;
	DIR *dir;
	struct dirent *d;

	if (!IS_ENABLED(CONFIG_NVVAR))
		return -ENOSYS;

	dir = opendir("/env/nv");
	if (!dir)
		return -ENOENT;

	while ((d = readdir(dir))) {
		const char *n;

		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
			continue;

		val = read_file_line("/env/nv/%s", d->d_name);

		pr_debug("%s: Setting \"%s\" to \"%s\"\n",
				__func__, d->d_name, val);

		n = globalvar_new_name(d->d_name);
		ret = __nvvar_add(n, val);
		if (ret)
			pr_err("failed to create nv variable %s: %s\n",
					n, strerror(-ret));
	}

	closedir(dir);

	return 0;
}

static void device_param_print(struct device_d *dev)
{
	struct param_d *param;

	list_for_each_entry(param, &dev->parameters, list) {
		const char *p = dev_get_param(dev, param->name);
		const char *nv = NULL;

		if (IS_ENABLED(CONFIG_NVVAR) && dev != &nv_device)
			nv = dev_get_param(&nv_device, param->name);

		printf("%s%s: %s", nv ? "* " : "  ", param->name, p);
		if (param->info)
			param->info(param);
		printf("\n");
	}
}

void nvvar_print(void)
{
	if (!IS_ENABLED(CONFIG_NVVAR))
		return;

	device_param_print(&nv_device);
}

void globalvar_print(void)
{
	device_param_print(&global_device);
}

/*
 * globalvar_get_match
 *
 * get a concatenated string of all globalvars beginning with 'match'.
 * This adds whitespaces between the different globalvars
 */
char *globalvar_get_match(const char *match, const char *separator)
{
	char *val = NULL;
	struct param_d *param;

	list_for_each_entry(param, &global_device.parameters, list) {
		if (!strncmp(match, param->name, strlen(match))) {
			const char *p = dev_get_param(&global_device, param->name);
			if (val) {
				char *new = basprintf("%s%s%s", val,
							separator, p);
				free(val);
				val = new;
			} else {
				val = xstrdup(p);
			}
		}
	}

	if (!val)
		val = xstrdup("");

	return val;
}

void globalvar_set_match(const char *match, const char *val)
{
	struct param_d *param;

	list_for_each_entry(param, &global_device.parameters, list) {
		if (!strncmp(match, param->name, strlen(match)))
			dev_set_param(&global_device, param->name, val);
	}
}

void globalvar_set(const char *name, const char *val)
{
	dev_set_param(&global_device, name, val);
}

static int globalvar_simple_set(struct device_d *dev, struct param_d *p, const char *val)
{
	struct device_d *rdev;
	const char *pname = NULL;
	int ret;

	ret = nvvar_device_dispatch(p->name, &rdev, &pname);
	if (ret < 0)
		return ret;

	if (ret && rdev) {
		ret = dev_set_param(rdev, pname, val);
		if (ret)
			pr_err("Cannot init param from global: %s.%s=%s: %s\n",
				dev_name(rdev), pname, val, strerror(-ret));
	}

	/* Pass to the generic function we have overwritten */
	return dev_param_set_generic(dev, p, val);
}

/*
 * globalvar_add_simple
 *
 * add a new globalvar named 'name'
 */
int globalvar_add_simple(const char *name, const char *value)
{
	struct param_d *param;

	param = dev_add_param(&global_device, name, globalvar_simple_set, NULL,
			      PARAM_GLOBALVAR_UNQUALIFIED);
	if (IS_ERR(param)) {
		if (PTR_ERR(param) != -EEXIST)
			return PTR_ERR(param);
	}

	if (!value)
		return 0;

	return dev_set_param(&global_device, name, value);
}

static int globalvar_remove_unqualified(const char *name)
{
	struct param_d *p;

	p = get_param_by_name(&global_device, name);
	if (!p)
		return 0;

	if (!(p->flags & PARAM_GLOBALVAR_UNQUALIFIED))
		return -EEXIST;

	dev_remove_param(p);

	return 0;
}

static void globalvar_nv_sync(const char *name)
{
	const char *val;

	if (!IS_ENABLED(CONFIG_NVVAR))
		return;

	val = dev_get_param(&nv_device, name);
	if (val)
		dev_set_param(&global_device, name, val);
}

int globalvar_add_simple_string(const char *name, char **value)
{
	struct param_d *p;
	int ret;

	ret = globalvar_remove_unqualified(name);
	if (ret)
		return ret;

	p = dev_add_param_string(&global_device, name, NULL, NULL,
		value, NULL);

	if (IS_ERR(p))
		return PTR_ERR(p);

	globalvar_nv_sync(name);

	if (!*value)
		*value = xstrdup("");

	return 0;
}

int globalvar_add_simple_int(const char *name, int *value,
			     const char *format)
{
	struct param_d *p;
	int ret;

	ret = globalvar_remove_unqualified(name);
	if (ret)
		return ret;

	p = dev_add_param_int(&global_device, name, NULL, NULL,
		value, format, NULL);

	if (IS_ERR(p))
		return PTR_ERR(p);

	globalvar_nv_sync(name);

	return 0;
}

int globalvar_add_bool(const char *name,
		       int (*set)(struct param_d *, void *),
		       int *value, void *priv)
{
	struct param_d *p;
	int ret;

	ret = globalvar_remove_unqualified(name);
	if (ret)
		return ret;

	p = dev_add_param_bool(&global_device, name, set, NULL,
		value, priv);

	if (IS_ERR(p))
		return PTR_ERR(p);

	globalvar_nv_sync(name);

	return 0;
}

int globalvar_add_simple_enum(const char *name,	int *value,
			      const char * const *names, int max)
{
	struct param_d *p;
	int ret;

	ret = globalvar_remove_unqualified(name);
	if (ret)
		return ret;

	p = dev_add_param_enum(&global_device, name, NULL, NULL,
		value, names, max, NULL);

	if (IS_ERR(p))
		return PTR_ERR(p);

	globalvar_nv_sync(name);

	return 0;
}

int globalvar_add_simple_bitmask(const char *name, unsigned long *value,
				 const char * const *names, int max)
{
	struct param_d *p;

	p = dev_add_param_bitmask(&global_device, name, NULL, NULL,
		value, names, max, NULL);

	return PTR_ERR_OR_ZERO(p);
}

int globalvar_add_simple_ip(const char *name, IPaddr_t *ip)
{
	struct param_d *p;
	int ret;

	ret = globalvar_remove_unqualified(name);
	if (ret)
		return ret;

	p = dev_add_param_ip(&global_device, name, NULL, NULL,
		ip, NULL);

	if (IS_ERR(p))
		return PTR_ERR(p);

	globalvar_nv_sync(name);

	return 0;
}

static int globalvar_init(void)
{
	register_device(&global_device);

	if (IS_ENABLED(CONFIG_NVVAR))
		register_device(&nv_device);

	globalvar_add_simple("version", UTS_RELEASE);

	if (strlen(buildsystem_version_string) > 0)
		globalvar_add_simple("buildsystem.version", buildsystem_version_string);

	return 0;
}
pure_initcall(globalvar_init);

BAREBOX_MAGICVAR(global.version, "The barebox version");
BAREBOX_MAGICVAR(global.buildsystem.version,
		 "version of buildsystem barebox was built with");

/**
 * nvvar_save - save NV variables to persistent environment
 *
 * This saves the NV variables to the persisitent environment without saving
 * the other files in the environment that might be changed.
 */
int nvvar_save(void)
{
	struct param_d *param;
	const char *env = default_environment_path_get();
	int ret = 0;
#define TMPDIR "/.env.tmp"
	if (!nv_dirty || !env)
		return 0;

	if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT))
		defaultenv_load(TMPDIR, 0);

	envfs_load(env, TMPDIR, 0);
	unlink_recursive(TMPDIR "/nv", NULL);

	list_for_each_entry(param, &nv_device.parameters, list) {
		ret = __nv_save(TMPDIR "/nv", param->name,
				dev_get_param(&nv_device, param->name));
		if (ret) {
			pr_err("Cannot save NV var: %s\n", strerror(-ret));
			goto out;
		}
	}

	envfs_save(env, TMPDIR, 0);
out:
	unlink_recursive(TMPDIR, NULL);

	if (!ret)
		nv_dirty = 0;

	return ret;
}

static void nv_exit(void)
{
	if (nv_dirty)
		pr_info("nv variables modified, saving them\n");

	nvvar_save();
}
predevshutdown_exitcall(nv_exit);

static int nv_global_param_complete(struct device_d *dev, struct string_list *sl,
				 char *instr, int eval)
{
	struct param_d *param;
	int len;

	len = strlen(instr);

	list_for_each_entry(param, &dev->parameters, list) {
		if (strncmp(instr, param->name, len))
			continue;

		string_list_add_asprintf(sl, "%s%c",
			param->name,
			eval ? ' ' : '=');
	}

	return 0;
}

int nv_complete(struct string_list *sl, char *instr)
{
	struct device_d *dev;
	struct param_d *param;
	char *str;
	int len;

	nv_global_param_complete(&global_device, sl, instr, 0);

	len = strlen(instr);

	if (strncmp(instr, "dev.", min_t(int, len, 4)))
		return 0;

	for_each_device(dev) {
		if (dev == &global_device || dev == &nv_device)
			continue;

		list_for_each_entry(param, &dev->parameters, list) {
			str = basprintf("dev.%s.%s=", dev_name(dev), param->name);
			if (strncmp(instr, str, len))
				free(str);
			else
				string_list_add(sl, str);
		}
	}

	return 0;
}

int global_complete(struct string_list *sl, char *instr)
{
	nv_global_param_complete(&global_device, sl, instr, 0);

	return 0;
}