/
uboot_env.c
1659 lines (1424 loc) · 33 KB
/
uboot_env.c
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* (C) Copyright 2019
* Stefano Babic, DENX Software Engineering, sbabic@denx.de.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
/**
* @file uboot_env.c
*
* @brief This is the implementation of libubootenv library
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#ifdef __FreeBSD__
#include <sys/disk.h>
#define BLKGETSIZE64 DIOCGMEDIASIZE
#else
#include <linux/fs.h>
#endif
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <zlib.h>
#include <yaml.h>
#include "uboot_private.h"
/* yaml_* functions return 1 on success and 0 on failure. */
enum yaml_status {
SUCCESS = 0,
FAILURE = 1
};
enum yaml_state {
STATE_START, /* start state */
STATE_STREAM, /* start/end stream */
STATE_DOCUMENT, /* start/end document */
STATE_SECTION, /* top level */
STATE_NAMESPACE, /* Init Configuration Namespace */
STATE_NAMESPACE_FIELDS, /* namespace key list */
STATE_NKEY, /* Check key names */
STATE_NSIZE, /* Size key-value pair */
STATE_NLOCKFILE, /* Lockfile key-value pair */
STATE_DEVVALUES, /* Devices key names */
STATE_NPATH,
STATE_NOFFSET,
STATE_NSECTORSIZE,
STATE_NUNLOCK,
STATE_STOP /* end state */
};
typedef enum yaml_parse_error_e {
YAML_UNEXPECTED_STATE,
YAML_UNEXPECTED_KEY,
YAML_BAD_DEVICE,
YAML_BAD_DEVNAME,
} yaml_parse_error_type_t;
struct parser_state {
enum yaml_state state; /* The current parse state */
struct uboot_ctx *ctxsets; /* Array of vars set ctx */
struct uboot_ctx *ctx; /* Current ctx in parsing */
unsigned int nelem; /* Number of elemets in ctxsets */
unsigned int cdev; /* current device in parsing */
yaml_parse_error_type_t error; /* error causing parser to stop */
yaml_event_type_t event_type; /* event type causing error */
};
/*
* The default lockfile is the same as defined in U-Boot for
* the fw_printenv utilities. Custom lockfile can be set via
* configuration file.
*/
static const char *default_lockname = "/var/lock/fw_printenv.lock";
static struct uboot_version_info libinfo;
static int libuboot_lock(struct uboot_ctx *ctx)
{
int lockfd = -1;
lockfd = open(ctx->lockfile ?: default_lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (lockfd < 0) {
return -EBUSY;
}
if (flock(lockfd, LOCK_EX) < 0) {
close(lockfd);
return -EIO;
}
ctx->lock = lockfd;
return 0;
}
static void libuboot_unlock(struct uboot_ctx *ctx)
{
if (ctx && (ctx->lock > 0)) {
flock(ctx->lock, LOCK_UN);
close(ctx->lock);
ctx->lock = -1;
}
}
static char attr_tostring(type_attribute a)
{
switch(a) {
case TYPE_ATTR_STRING:
return 's';
case TYPE_ATTR_DECIMAL:
return 'd';
case TYPE_ATTR_HEX:
return 'x';
case TYPE_ATTR_BOOL:
return 'b';
case TYPE_ATTR_IP:
return 'i';
case TYPE_ATTR_MAC:
return 'm';
}
return 's';
}
static char access_tostring(access_attribute a)
{
switch(a) {
case ACCESS_ATTR_ANY:
return 'a';
case ACCESS_ATTR_READ_ONLY:
return 'r';
case ACCESS_ATTR_WRITE_ONCE:
return 'o';
case ACCESS_ATTR_CHANGE_DEFAULT:
return 'c';
}
return 'a';
}
static struct var_entry *__libuboot_get_env(struct vars *envs, const char *varname)
{
struct var_entry *entry;
LIST_FOREACH(entry, envs, next) {
if (strcmp(varname, entry->name) == 0)
return entry;
}
return NULL;
}
static void free_var_entry(struct var_entry *entry)
{
if (entry) {
LIST_REMOVE(entry, next);
free(entry->name);
free(entry->value);
free(entry);
}
}
static enum device_type get_device_type(char *device)
{
enum device_type type = DEVICE_NONE;
if (!strncmp(device, DEVICE_MTD_NAME, strlen(DEVICE_MTD_NAME)))
if (strchr(device, DEVNAME_SEPARATOR)) {
type = DEVICE_UBI;
} else {
type = DEVICE_MTD;
}
else if (!strncmp(device, DEVICE_UBI_NAME, strlen(DEVICE_UBI_NAME)))
type = DEVICE_UBI;
else if (strlen(device) > 0)
type = DEVICE_FILE;
return type;
}
static int normalize_device_path(char *path, struct uboot_flash_env *dev)
{
char *sep = NULL, *normalized = NULL;
size_t normalized_len = 0, volume_len = 0, output_len = 0;
/*
* if volume name is present, split into device path and volume
* since only the device path needs normalized
*/
sep = strchr(path, DEVNAME_SEPARATOR);
if (sep)
{
volume_len = strlen(sep);
*sep = '\0';
}
if ((normalized = realpath(path, NULL)) == NULL)
{
/* device file didn't exist */
return -EINVAL;
}
normalized_len = strlen(normalized);
output_len = sizeof(dev->devname) - 1; /* leave room for null */
if ((normalized_len + volume_len) > output_len)
{
/* full name is too long to fit */
free(normalized);
return -EINVAL;
}
/*
* save normalized path to device file,
* and possibly append separator char & volume name
*/
memset(dev->devname, 0, sizeof(dev->devname));
strncpy(dev->devname, normalized, output_len);
free(normalized);
if (sep)
{
*sep = DEVNAME_SEPARATOR;
strncpy(dev->devname + normalized_len, sep, output_len - normalized_len);
}
return 0;
}
static int check_env_device(struct uboot_flash_env *dev)
{
int fd, ret;
struct stat st;
dev->device_type = get_device_type(dev->devname);
if (dev->device_type == DEVICE_NONE)
return -EBADF;
if (dev->device_type == DEVICE_UBI) {
ret = libubootenv_ubi_update_name(dev);
if (ret)
return ret;
}
ret = stat(dev->devname, &st);
if (ret < 0)
return -EBADF;
fd = open(dev->devname, O_RDONLY);
if (fd < 0)
return -EBADF;
if (S_ISCHR(st.st_mode)) {
if (dev->device_type == DEVICE_MTD) {
ret = libubootenv_mtdgetinfo(fd, dev);
if (ret < 0 || (dev->mtdinfo.type != MTD_NORFLASH &&
dev->mtdinfo.type != MTD_NANDFLASH)) {
close(fd);
return -EBADF;
}
if (dev->sectorsize == 0) {
dev->sectorsize = dev->mtdinfo.erasesize;
}
}
}
switch (dev->device_type) {
case DEVICE_FILE:
dev->flagstype = FLAGS_INCREMENTAL;
break;
case DEVICE_MTD:
switch (dev->mtdinfo.type) {
case MTD_NORFLASH:
dev->flagstype = FLAGS_BOOLEAN;
break;
case MTD_NANDFLASH:
dev->flagstype = FLAGS_INCREMENTAL;
};
break;
case DEVICE_UBI:
dev->flagstype = FLAGS_INCREMENTAL;
break;
default:
close(fd);
return -EBADF;
};
/*
* Check for negative offsets, treat it as backwards offset
* from the end of the block device
*/
if (dev->offset < 0) {
uint64_t blkdevsize;
int rc;
rc = ioctl(fd, BLKGETSIZE64, &blkdevsize);
if (rc < 0) {
close(fd);
return -EINVAL;
}
dev->offset += blkdevsize;
}
close(fd);
return 0;
}
static bool check_compatible_devices(struct uboot_ctx *ctx)
{
if (!ctx->redundant)
return true;
if (ctx->envdevs[0].mtdinfo.type != ctx->envdevs[1].mtdinfo.type)
return false;
if (ctx->envdevs[0].flagstype != ctx->envdevs[1].flagstype)
return false;
if (ctx->envdevs[0].envsize != ctx->envdevs[1].envsize)
return false;
return true;
}
static int fileread(struct uboot_flash_env *dev, void *data)
{
int ret = 0;
if (dev->offset)
ret = lseek(dev->fd, dev->offset, SEEK_SET);
if (ret < 0)
return ret;
size_t remaining = dev->envsize;
while (1) {
ret = read(dev->fd, data, remaining);
if (ret == 0 && remaining > 0)
return -1;
if (ret < 0)
break;
remaining -= ret;
data += ret;
if (!remaining) {
ret = dev->envsize;
break;
}
}
return ret;
}
static int devread(struct uboot_ctx *ctx, unsigned int copy, void *data)
{
int ret;
struct uboot_flash_env *dev;
if (copy > 1)
return -EINVAL;
dev = &ctx->envdevs[copy];
dev->fd = open(dev->devname, O_RDONLY);
if (dev->fd < 0)
return -EBADF;
switch (dev->device_type) {
case DEVICE_FILE:
ret = fileread(dev, data);
break;
case DEVICE_MTD:
ret = libubootenv_mtdread(dev, data);
break;
case DEVICE_UBI:
ret = libubootenv_ubiread(dev, data);
break;
default:
ret = -1;
break;
};
close(dev->fd);
return ret;
}
static int fileprotect(struct uboot_flash_env *dev, bool on)
{
const char c_sys_path_1[] = "/sys/class/block/";
const char c_sys_path_2[] = "/force_ro";
const char c_dev_name_1[] = "mmcblk";
const char c_dev_name_2[] = "boot";
const char c_unprot_char = '0';
const char c_prot_char = '1';
const char *devfile = dev->devname;
int ret = 0; // 0 means OK, negative means error
int ret_int = 0;
char *sysfs_path = NULL;
int fd_force_ro;
// Devices without ro flag at /sys/class/block/mmcblk?boot?/force_ro are ignored
if (strncmp("/dev/block/", dev->devname, 11) == 0) {
devfile = dev->devname + 11;
} else if (strncmp("/dev/", dev->devname, 5) == 0) {
devfile = dev->devname + 5;
} else {
return ret;
}
ret_int = strncmp(devfile, c_dev_name_1, sizeof(c_dev_name_1) - 1);
if (ret_int != 0) {
return ret;
}
if (strncmp(devfile + sizeof(c_dev_name_1), c_dev_name_2, sizeof(c_dev_name_2) - 1) != 0) {
return ret;
}
if (*(devfile + sizeof(c_dev_name_1) - 1) < '0' ||
*(devfile + sizeof(c_dev_name_1) - 1) > '9') {
return ret;
}
if (*(devfile + sizeof(c_dev_name_1) + sizeof(c_dev_name_2) - 1) < '0' ||
*(devfile + sizeof(c_dev_name_1) + sizeof(c_dev_name_2) - 1) > '9') {
return ret;
}
// There is a ro flag, the device needs to be protected or unprotected
ret_int = asprintf(&sysfs_path, "%s%s%s", c_sys_path_1, devfile, c_sys_path_2);
if(ret_int < 0) {
ret = -ENOMEM;
goto fileprotect_out;
}
if (access(sysfs_path, W_OK) == -1) {
goto fileprotect_out;
}
fd_force_ro = open(sysfs_path, O_RDWR);
if (fd_force_ro == -1) {
ret = -EBADF;
goto fileprotect_out;
}
if(on == false){
ret_int = write(fd_force_ro, &c_unprot_char, 1);
} else {
ret_int = write(fd_force_ro, &c_prot_char, 1);
}
close(fd_force_ro);
fileprotect_out:
if(sysfs_path)
free(sysfs_path);
return ret;
}
static int filewrite(struct uboot_flash_env *dev, void *data)
{
int ret = 0;
ret = fileprotect(dev, false);
if (ret < 0)
return ret;
if (dev->offset)
ret = lseek(dev->fd, dev->offset, SEEK_SET);
if (ret < 0)
return ret;
size_t remaining = dev->envsize;
while (1) {
ret = write(dev->fd, data, remaining);
if (ret < 0)
break;
remaining -= ret;
data += ret;
if (!remaining) {
ret = dev->envsize;
break;
}
}
fsync(dev->fd);
fileprotect(dev, true); // no error handling, keep ret from write
return ret;
}
static int devwrite(struct uboot_ctx *ctx, unsigned int copy, void *data)
{
int ret;
struct uboot_flash_env *dev;
if (copy > 1)
return -EINVAL;
dev = &ctx->envdevs[copy];
dev->fd = open(dev->devname, O_RDWR);
if (dev->fd < 0)
return -EBADF;
switch (dev->device_type) {
case DEVICE_FILE:
ret = filewrite(dev, data);
break;
case DEVICE_MTD:
ret = libubootenv_mtdwrite(dev, data);
break;
case DEVICE_UBI:
ret = libubootenv_ubiwrite(dev, data);
break;
default:
ret = -1;
break;
};
close(dev->fd);
return ret;
}
const struct uboot_version_info *libuboot_version_info(void)
{
int i;
char *endptr = NULL;
char *start = VERSION;
long val = 0;
libinfo.version = VERSION;
for (i = 0; i < 3; i++) {
if (!*start)
break;
val = val + (strtol(start, &endptr, 10) << (8 * (2 - i)));
if (start == endptr)
break;
start = endptr + 1;
}
libinfo.version_num = val;
return &libinfo;
}
int libuboot_env_store(struct uboot_ctx *ctx)
{
struct var_entry *entry;
void *image;
char *data;
char *buf;
bool saveflags = false;
size_t size;
uint8_t offsetdata;
int ret;
int copy;
/*
* Allocate the bigger of the case
*/
image = malloc(sizeof(struct uboot_env_redund) + ctx->size);
if (!image)
return -ENOMEM;
if (ctx->redundant)
offsetdata = offsetof(struct uboot_env_redund, data);
else
offsetdata = offsetof(struct uboot_env_noredund, data);
data = (char *)(image + offsetdata);
buf = data;
LIST_FOREACH(entry, &ctx->varlist, next) {
size = (ctx->size - offsetdata) - (buf - data);
if ((strlen(entry->name) + strlen(entry->value) + 2) > size)
return -ENOMEM;
if (entry->type || entry->access)
saveflags = true;
buf += snprintf(buf, size, "%s=%s", entry->name, entry->value);
buf++;
}
/*
* Now save the .flags
*/
if (saveflags) {
bool first = true;
size = (ctx->size - offsetdata) - (buf - data);
buf += snprintf(buf, size, ".flags=");
LIST_FOREACH(entry, &ctx->varlist, next) {
size = (ctx->size - offsetdata) - (buf - data);
if (entry->type || entry->access) {
buf += snprintf(buf, size, "%s%s:%c%c",
first ? "" : ",",
entry->name,
attr_tostring(entry->type),
access_tostring(entry->access));
first = false;
}
}
buf++;
}
*buf++ = '\0';
if (ctx->redundant) {
unsigned char flags = ctx->envdevs[ctx->current].flags;
switch(ctx->envdevs[ctx->current].flagstype) {
case FLAGS_INCREMENTAL:
flags++;
break;
case FLAGS_BOOLEAN:
flags = 1;
break;
}
((struct uboot_env_redund *)image)->flags = flags;
}
*(uint32_t *)image = crc32(0, (uint8_t *)data, ctx->size - offsetdata);
copy = ctx->redundant ? (ctx->current ? 0 : 1) : 0;
ret = devwrite(ctx, copy, image);
free(image);
if (ret == ctx->size)
ret = 0;
if (ctx->redundant && !ret) {
if (ctx->envdevs[ctx->current].flagstype == FLAGS_BOOLEAN)
ret = libubootenv_set_obsolete_flag(&ctx->envdevs[ctx->current]);
}
if (!ret)
ctx->current = ctx->current ? 0 : 1;
return ret;
}
static int libuboot_load(struct uboot_ctx *ctx)
{
int ret, i;
int copies = 1;
void *buf[2];
size_t bufsize, usable_envsize;
struct uboot_flash_env *dev;
bool crcenv[2];
char *line, *next;
uint8_t offsetdata = offsetof(struct uboot_env_noredund, data);
uint8_t offsetcrc = offsetof(struct uboot_env_noredund, crc);
uint8_t offsetflags = offsetof(struct uboot_env_redund, flags);
char *data;
struct var_entry *entry;
ctx->valid = false;
bufsize = ctx->size;
if (ctx->redundant) {
copies++;
bufsize += ctx->size;
offsetdata = offsetof(struct uboot_env_redund, data);
offsetcrc = offsetof(struct uboot_env_redund, crc);
}
usable_envsize = ctx->size - offsetdata;
buf[0] = malloc(bufsize);
if (!buf[0])
return -ENOMEM;
if (copies > 1)
buf[1] = buf[0] + ctx->envdevs[0].envsize;
for (i = 0; i < copies; i++) {
data = (char *)(buf[i] + offsetdata);
uint32_t crc;
dev = &ctx->envdevs[i];
ret = devread(ctx, i, buf[i]);
if (ret != ctx->size) {
free(buf[0]);
return -EIO;
}
crc = *(uint32_t *)(buf[i] + offsetcrc);
dev->crc = crc32(0, (uint8_t *)data, usable_envsize);
crcenv[i] = dev->crc == crc;
if (ctx->redundant)
dev->flags = *(uint8_t *)(buf[i] + offsetflags);
}
if (!ctx->redundant) {
ctx->current = 0;
ctx->valid = crcenv[0];
} else {
if (crcenv[0] && !crcenv[1]) {
ctx->valid = true;
ctx->current = 0;
} else if (!crcenv[0] && crcenv[1]) {
ctx->valid = true;
ctx->current = 1;
} else if (!crcenv[0] && !crcenv[1]) {
ctx->valid = false;
ctx->current = 0;
} else { /* both valid, check flags */
ctx->valid = true;
if (ctx->envdevs[1].flags > ctx->envdevs[0].flags)
ctx->current = 1;
else
ctx->current = 0;
switch (ctx->envdevs[0].flagstype) {
case FLAGS_BOOLEAN:
if (ctx->envdevs[1].flags == 0xFF)
ctx->current = 1;
else if (ctx->envdevs[0].flags == 0xFF)
ctx->current = 0;
break;
case FLAGS_INCREMENTAL:
/* check overflow */
if (ctx->envdevs[0].flags == 0xFF &&
ctx->envdevs[1].flags == 0)
ctx->current = 1;
else if (ctx->envdevs[1].flags == 0xFF &&
ctx->envdevs[0].flags == 0)
ctx->current = 0;
break;
}
}
}
#if !defined(NDEBUG)
fprintf(stdout, "Environment %s, copy %d\n",
ctx->valid ? "OK" : "WRONG", ctx->current);
#endif
data = (char *)(buf[ctx->current] + offsetdata);
char *flagsvar = NULL;
if (ctx->valid) {
for (line = data; *line; line = next + 1) {
char *value;
/*
* Search the end of the string pointed by line
*/
for (next = line; *next; ++next) {
if ((next - (char *)data) > usable_envsize) {
free(buf[0]);
return -EIO;
}
}
value = strchr(line, '=');
if (!value)
continue;
*value++ = '\0';
if (!strcmp(line, ".flags"))
flagsvar = strdup(value);
else
libuboot_set_env(ctx, line, value);
}
}
/*
* Parse .flags and set the attributes for a variable
*/
char *pvar;
char *pval;
if (flagsvar) {
#if !defined(NDEBUG)
fprintf(stdout, "Environment FLAGS %s\n", flagsvar);
#endif
unsigned int flagslen = strlen(flagsvar);
pvar = flagsvar;
while (*pvar && (pvar - flagsvar) < flagslen) {
char *pnext;
pval = strchr(pvar, ':');
if (!pval)
break;
*pval++ = '\0';
pnext = strchr(pval, ',');
if (!pnext)
pnext = flagsvar + strlen(flagsvar);
else
*pnext++ = '\0';
entry = __libuboot_get_env(&ctx->varlist, pvar);
if (entry) {
for (int i = 0; i < strlen(pval); i++) {
switch (pval[i]) {
case 's':
entry->type = TYPE_ATTR_STRING;
break;
case 'd':
entry->type = TYPE_ATTR_DECIMAL;
break;
case 'x':
entry->type = TYPE_ATTR_HEX;
break;
case 'b':
entry->type = TYPE_ATTR_BOOL;
break;
case 'i':
entry->type = TYPE_ATTR_IP;
break;
case 'm':
entry->type = TYPE_ATTR_MAC;
break;
case 'a':
entry->access = ACCESS_ATTR_ANY;
break;
case 'r':
entry->access = ACCESS_ATTR_READ_ONLY;
break;
case 'o':
entry->access = ACCESS_ATTR_WRITE_ONCE;
break;
case 'c':
entry->access = ACCESS_ATTR_CHANGE_DEFAULT;
break;
default: /* ignore it */
break;
}
}
}
pvar = pnext;
}
}
free(flagsvar);
free(buf[0]);
return ctx->valid ? 0 : -ENODATA;
}
static int consume_event(struct parser_state *s, yaml_event_t *event)
{
char *value;
struct uboot_flash_env *dev;
struct uboot_ctx *newctx;
int cdev;
switch (s->state) {
case STATE_START:
switch (event->type) {
case YAML_STREAM_START_EVENT:
s->state = STATE_STREAM;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_STREAM:
switch (event->type) {
case YAML_DOCUMENT_START_EVENT:
s->state = STATE_DOCUMENT;
break;
case YAML_STREAM_END_EVENT:
s->state = STATE_STOP;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_DOCUMENT:
switch (event->type) {
case YAML_MAPPING_START_EVENT:
s->state = STATE_SECTION;
break;
case YAML_DOCUMENT_END_EVENT:
s->state = STATE_STREAM;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_SECTION:
switch (event->type) {
case YAML_SCALAR_EVENT:
value = (char *)event->data.scalar.value;
newctx = calloc (s->nelem + 1, sizeof(*newctx));
for (int i = 0; i < s->nelem; i++) {
newctx[i] = s->ctxsets[i];
}
if (s->ctxsets) free(s->ctxsets);
s->ctxsets = newctx;
s->ctx = &newctx[s->nelem];
s->ctx->name = strdup(value);
s->nelem++;
s->state = STATE_NAMESPACE;
break;
case YAML_MAPPING_END_EVENT:
s->state = STATE_DOCUMENT;
break;
case YAML_DOCUMENT_END_EVENT:
s->state = STATE_STREAM;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_NAMESPACE:
switch (event->type) {
case YAML_MAPPING_START_EVENT:
s->state = STATE_NAMESPACE_FIELDS;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_NAMESPACE_FIELDS:
switch (event->type) {
case YAML_SCALAR_EVENT:
value = (char *)event->data.scalar.value;
if (!strcmp(value, "size")) {
s->state = STATE_NSIZE;
} else if (!strcmp(value, "lockfile")) {
s->state = STATE_NLOCKFILE;
} else if (!strcmp(value, "devices")) {
s->state = STATE_DEVVALUES;
s->cdev = 0;
} else {
s->error = YAML_UNEXPECTED_KEY;
s->event_type = event->type;
return FAILURE;
}
break;
case YAML_MAPPING_END_EVENT:
s->state = STATE_SECTION;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_NSIZE:
switch (event->type) {
case YAML_SCALAR_EVENT:
value = (char *)event->data.scalar.value;
errno = 0;
s->ctx->size = strtoull(value, NULL, 0);
s->state = STATE_NAMESPACE_FIELDS;
break;
default:
s->error = YAML_UNEXPECTED_STATE;
s->event_type = event->type;
return FAILURE;
}
break;
case STATE_NLOCKFILE:
switch (event->type) {
case YAML_SCALAR_EVENT:
value = (char *)event->data.scalar.value;
s->ctx->lockfile = strdup(value);
s->state = STATE_NAMESPACE_FIELDS;