-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.c
768 lines (639 loc) · 14.8 KB
/
config.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
#include "config.h"
#include "video_low.h"
#include "atoi.h"
#ifdef GUMBOOT
#include "log.h"
#include "string.h"
#include "malloc.h"
#else
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include "lstrender/log_compat.h"
#endif
#define ERR_MISSING_TOKEN 0x10
#define ERR_INVALID_NUMBER 0x20
#define ERR_EXTRA_TOKEN 0x30
#define ERR_MISSING_TITLE 0x40
#define ERR_UNEXPECTED_TOKEN 0x50
#define ERR_NOTHING_TO_DO 0x60
#define ERR_TOO_MANY_ACTIONS 0x70
#define ERR_TOO_MANY_STANZAS 0x80
#define ERR_INVALID_COLOR 0x90
#define ERR_TOO_MANY_COLORS 0xA0
#define ERR_DOUBLE_DEFINITION 0xB0
#define ERR_NO_ROOT 0xC0
#define ERR_INVALID_ROOT 0xD0
#define ERR_INVALID_VIDEO_MODE 0xE0
#define ERR_UNKNOWN_TOKEN 0xF0
#define ERR_INVISIBLE_FONT 0xF1
#define ERR_INVALID_SPLASH 0xF2
// all configuration options
int config_timeout = 0,
config_default = 0,
config_nomenu = 0,
config_entries_count = 0;
char *config_splashimage = NULL;
int config_vmode = -1;
rgb color_default[2] = {
{.as_rgba = {0xAA, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0, 0, 0, 0xFF}}
};
rgb color_error[2] = {{.as_rgba = {255, 0, 0, 255}}, {.as_rgba = {0, 0, 0, 255}}};
// color configuration
rgb config_color_normal[2] = {
{.as_rgba = {0xAA, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0, 0, 0, 0xFF}}
},
config_color_highlight[2] = {
{.as_rgba = {0xAA, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0, 0, 0, 0xFF}}
},
config_color_helptext[2] = {
{.as_rgba = {0xAA, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0, 0, 0, 0xFF}}
},
config_color_heading[2] = {
{.as_rgba = {0xAA, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0, 0, 0, 0xFF}}
};
stanza config_entries[MAX_CONFIG_ENTRIES];
static stanza *wip_stanza = NULL;
int process_line(char *line);
int complete_stanza();
char *tokenize(char *line);
const char *config_strerr(int err) {
switch (err) {
case ERR_MISSING_TOKEN :
return "missing token";
case ERR_INVALID_NUMBER :
return "invalid number";
case ERR_EXTRA_TOKEN :
return "extra token";
case ERR_MISSING_TITLE :
return "missing title";
case ERR_UNEXPECTED_TOKEN :
return "unexpected token";
case ERR_NOTHING_TO_DO :
return "no action specified";
case ERR_TOO_MANY_ACTIONS:
return "too many actions";
case ERR_TOO_MANY_STANZAS :
return "too many stanzas";
case ERR_INVALID_COLOR :
return "invalid color";
case ERR_TOO_MANY_COLORS :
return "too many colors";
case ERR_DOUBLE_DEFINITION :
return "duplicate definition";
case ERR_NO_ROOT :
return "no root specified";
case ERR_INVALID_ROOT :
return "invalid root";
case ERR_INVALID_VIDEO_MODE :
return "invalid video mode";
case ERR_UNKNOWN_TOKEN :
return "unknown token";
case ERR_INVISIBLE_FONT :
return "font would be invisible";
case ERR_INVALID_SPLASH:
return "invalid splash screen path";
}
return "???";
}
int config_load_from_buffer(char *cfg_data, u32 read) {
char *start = cfg_data, *last_line = cfg_data;
int line_no = 0;
while (1) {
line_no++;
// grab one line from the input
while (*start != '\n' && *start != 0x0) {
start++;
}
*start = 0;
int err = process_line(last_line);
if (err) {
log_printf("error processing line %d: %s\n", line_no, config_strerr(err));
// in case of error, abort
return err;
}
if (start == cfg_data + read) {
// reached EOF
break;
}
// start looking for next line
start++;
last_line = start;
}
if (wip_stanza) {
int err = complete_stanza();
if (err) {
log_printf("invalid last menu entry: %d\n", err);
return err;
}
}
if (config_entries_count == 0) {
log_printf("no config entries defined\n");
return -1;
}
if (config_default >= config_entries_count) {
log_printf("invalid default selected\n");
config_timeout = 0;
config_default = 0;
return -2;
}
if (config_nomenu && config_timeout) {
log_printf("cannot use nomenu with a timeout\n");
config_nomenu = 0;
return -2;
}
return 0;
}
int is_whitespace(char c) {
return ((c == ' ') || (c == '\t'));
}
void trim_right(char *s, char *beginning) {
s--;
while (1) {
if (s == beginning)
return;
if (*s == 0x0)
return;
if (is_whitespace(*s)) {
*s = 0x0;
s--;
} else
return;
}
}
char * strchr2(const char *s, char c1, char c2)
{
size_t i;
for (i = 0; s[i]; i++) {
if (s[i] == c1) {
if (!s[i+1])
break;
if (s[i+1] == c2)
return (char *)s + i;
}
}
return NULL;
}
int parse_timeout(char *s) {
size_t parsed;
int val = atoi_base(s, 10, &parsed);
if (parsed != strlen(s))
return ERR_INVALID_NUMBER;
config_timeout = val;
return 0;
}
int parse_default(char *s) {
size_t parsed;
int val = atoi_base(s, 10, &parsed);
if (!parsed || (parsed != strlen(s)))
return ERR_INVALID_NUMBER;
// will be validated later on
config_default = val;
return 0;
}
int complete_stanza() {
if (!wip_stanza->title)
return ERR_MISSING_TITLE;
int actions = 0;
if (wip_stanza->kernel)
actions++;
if (wip_stanza->reboot)
actions++;
if (wip_stanza->poweroff)
actions++;
if (wip_stanza->browse)
actions++;
if (actions > 1) {
return ERR_TOO_MANY_ACTIONS;
}
if (!actions)
return ERR_NOTHING_TO_DO;
if (wip_stanza->kernel && !wip_stanza->root)
return ERR_NO_ROOT;
// all good, keep track of this stanza
config_entries_count++;
wip_stanza = NULL;
return 0;
}
int parse_title(char *s) {
if (wip_stanza) {
int err = complete_stanza();
if (err)
return err;
}
if (config_entries_count == MAX_CONFIG_ENTRIES)
return ERR_TOO_MANY_STANZAS;
// titles define new stanzas as well
wip_stanza = &config_entries[config_entries_count];
memset(wip_stanza, sizeof(stanza), 0);
// find first occurrency of '\n'
char *pos = strchr2(s, '\\', 'n');
if (pos) {
// copy first chunk as title
*pos = 0x0;
pos+=2;
if (*pos) {
wip_stanza->help_text = malloc(strlen(pos));
char *dest = wip_stanza->help_text;
while (1) {
char *nl = strchr2(pos, '\\', 'n');
if (!nl) {
int n = strlen(pos);
memcpy(dest, pos, n);
dest+=n;
break;
}
int n = nl-pos;
memcpy(dest, pos, n);
dest+=n;
// add a proper newline
*dest = '\n';
dest++;
pos = nl+2;
if (!*pos)
break;
}
*dest = 0x0;
}
}
wip_stanza->title = strdup(s);
return 0;
}
int parse_boot() {
if (!wip_stanza)
return ERR_UNEXPECTED_TOKEN;
return complete_stanza();
}
int parse_reboot() {
if (!wip_stanza)
return ERR_UNEXPECTED_TOKEN;
if (wip_stanza->reboot)
return ERR_DOUBLE_DEFINITION;
wip_stanza->reboot = 1;
return 0;
}
int parse_poweroff() {
if (!wip_stanza)
return ERR_UNEXPECTED_TOKEN;
if (wip_stanza->poweroff)
return ERR_DOUBLE_DEFINITION;
wip_stanza->poweroff = 1;
return 0;
}
int parse_kernel(char *s) {
if (!wip_stanza)
return ERR_UNEXPECTED_TOKEN;
if (wip_stanza->kernel)
return ERR_DOUBLE_DEFINITION;
if (!s[1] && (s[0] == '/'))
return ERR_MISSING_TOKEN;
// find arguments (if any)
char *pos = strchr(s, ' ');
if (pos) {
*pos = 0x0;
pos++;
wip_stanza->kernel_args = strdup(pos);
} else {
wip_stanza->kernel_args = "";
}
wip_stanza->kernel = strdup(s);
return 0;
}
int parse_root(char *s) {
if (!wip_stanza)
return ERR_UNEXPECTED_TOKEN;
if (wip_stanza->root)
return ERR_DOUBLE_DEFINITION;
// only one drive, SD, is supported
if (strncmp(s, "(sd0,", 5)) {
return ERR_INVALID_ROOT;
}
// get partition number
s+=5;
if (strlen(s) < 2)
return ERR_INVALID_ROOT;
// check string after the number
if (s[1] != ')')
return ERR_INVALID_ROOT;
u8 n = (u8)(*s-48);
if (n > 3)
// extended partitions are not supported due to a FatFS limitation
return ERR_INVALID_ROOT;
// let's patch & re-use the string
s[1] = ':';
// make sure there is no trailing slash
char *last = s + strlen(s) - 1;
if (*last == '/')
*last = 0;
wip_stanza->root = strdup(s);
return 0;
}
#ifndef NO_SPLASHIMAGE
int parse_splashimage(char *s) {
if (config_splashimage)
return ERR_DOUBLE_DEFINITION;
// can only read splash images from first disk, first partition
if (strncmp("(sd0,0)/", s, 8))
return ERR_INVALID_SPLASH;
config_splashimage = strdup(s+8);
return 0;
}
#endif
rgb color_to_rgb[16]={
{.as_rgba = {0, 0, 0, 0xFF}},
{.as_rgba = {0, 0, 0xAA, 0xFF}},
{.as_rgba = {0, 0xAA, 0, 0xFF}},
{.as_rgba = {0, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0xAA, 0, 0, 0xFF}},
{.as_rgba = {0xAA, 0, 0xAA, 0xFF}},
{.as_rgba = {0xAA, 0x55, 0, 0xFF}},
{.as_rgba = {0xAA, 0xAA, 0xAA, 0xFF}},
{.as_rgba = {0x55, 0x55, 0x55, 0xFF}},
{.as_rgba = {0x55, 0x55, 0xFF, 0xFF}},
{.as_rgba = {0x55, 0xFF, 0x55, 0xFF}},
{.as_rgba = {0x55, 0xFF, 0xFF, 0xFF}},
{.as_rgba = {0xFF, 0x55, 0x55, 0xFF}},
{.as_rgba = {0xFF, 0x55, 0xFF, 0xFF}},
{.as_rgba = {0xFF, 0xFF, 0x55, 0xFF}},
{.as_rgba = {0xFF, 0xFF, 0xFF, 0xFF}}
};
static char *color_list[16] =
{
"black",
"blue",
"green",
"cyan",
"red",
"magenta",
"brown",
"light-gray",
"dark-gray",
"light-blue",
"light-green",
"light-cyan",
"light-red",
"light-magenta",
"yellow",
"white"
};
static int atocolor_half(char *str, rgb *result) {
if (!strncmp(str, "rgb(", 4)) {
char *ptr;
str+=4;
// read red component
ptr = strchr(str, ',');
if (!ptr)
return ERR_INVALID_COLOR;
*ptr=0;
ptr++;
size_t parsed;
int val = atoi_base(str, 10, &parsed);
if (parsed != strlen(str) || (val > 255))
return ERR_INVALID_NUMBER;
result->as_rgba.r = val;
str = ptr;
ptr = strchr(ptr, ',');
if (!ptr)
return ERR_INVALID_COLOR;
*ptr=0;
ptr++;
val = atoi_base(str, 10, &parsed);
if (parsed != strlen(str) || (val > 255))
return ERR_INVALID_NUMBER;
result->as_rgba.g = val;
str = ptr;
ptr = strchr(ptr, ')');
if (!ptr)
return ERR_INVALID_COLOR;
*ptr=0;
val = atoi_base(str, 10, &parsed);
if (parsed != strlen(str) || (val > 255))
return ERR_INVALID_NUMBER;
result->as_rgba.b = val;
return 0;
}
int i;
/* Search for the color name. */
for (i = 0; i < 16; i++) {
if (strcmp (color_list[i], str) == 0)
{
*result = color_to_rgb[i];
return 0;
}
}
return ERR_INVALID_COLOR;
}
/* Convert the color name STR into the magical number. */
static int atocolor(char *str, rgb *result)
{
char *ptr;
/* Find the separator. */
for (ptr = str; *ptr && *ptr != '/'; ptr++){
;
}
/* If not found, return -1. */
if (! *ptr)
return ERR_INVALID_COLOR;
/* Terminate the string STR. */
*ptr = 0;
ptr++;
// detect first half
int err = atocolor_half(str, &result[0]);
if (err)
return err;
// detect second half
err = atocolor_half(ptr, &result[1]);
if (err)
return err;
// forbid invisible combinations
if (result[0].as_u32 == result[1].as_u32)
return ERR_INVISIBLE_FONT;
return 0;
}
// parse colors second:
// color NORMAL [HIGHLIGHT [HELPTEXT [HEADING]]]
// see http://diddy.boot-land.net/grub4dos/files/commands.htm#color
int parse_color(char *s) {
char *next_token = tokenize(s);
int err = atocolor(s, (rgb *)&config_color_normal);
if (err)
return err;
if (!next_token)
return 0;
s = next_token;
next_token = tokenize(s);
err = atocolor(s, (rgb *)&config_color_highlight);
if (err)
return err;
if (!next_token)
return 0;
s = next_token;
next_token = tokenize(s);
err = atocolor(s, (rgb *)&config_color_helptext);
if (err)
return err;
if (!next_token)
return 0;
s = next_token;
next_token = tokenize(s);
err = atocolor(s, (rgb *)&config_color_heading);
if (err)
return err;
// no border support, sorry
if (next_token)
return ERR_TOO_MANY_COLORS;
return 0;
}
int parse_nomenu() {
if (config_nomenu)
return ERR_DOUBLE_DEFINITION;
config_nomenu = 1;
return 0;
}
int parse_browse() {
if (!wip_stanza)
return ERR_UNEXPECTED_TOKEN;
if (wip_stanza->browse)
return ERR_DOUBLE_DEFINITION;
wip_stanza->browse = 1;
return 0;
}
int parse_video(char *s) {
if (wip_stanza)
return ERR_UNEXPECTED_TOKEN;
if (config_vmode != -1)
return ERR_DOUBLE_DEFINITION;
if (!strcmp(s, "PROGRESSIVE"))
config_vmode = VIDEO_640X480_NTSCp_YUV16;
else if (!strcmp(s, "NTSC"))
config_vmode = VIDEO_640X480_NTSCi_YUV16;
else if (!strcmp(s, "PAL50"))
config_vmode = VIDEO_640X480_PAL50_YUV16;
else if (!strcmp(s, "PAL60"))
config_vmode = VIDEO_640X480_PAL60_YUV16;
else
return ERR_INVALID_VIDEO_MODE;
return 0;
}
// tokenize will set 0x0 at first whitespace in 's',
// and return the address of the next token (skipping whitespaces)
// or NULL if no other token is available
char *tokenize(char *line) {
// find next space or end of line
char *eot = line;
while (1) {
if (!*eot) {
return NULL;
}
if (!is_whitespace(*eot))
eot++;
else {
*eot = 0x0;
eot++;
break;
}
}
// skip some more leading whitespaces for the next token
if (eot != NULL) {
while (1) {
if (!*eot) {
eot = NULL;
break;
}
if (is_whitespace(*eot))
eot++;
else
/* good token */
break;
}
}
return eot;
}
int process_line(char *line) {
// skip whitespace
while (is_whitespace(*line))
line++;
if (!*line)
return 0;
// is this a comment?
if (*line == '#')
return 0;
char *token = tokenize(line);
if (0 == strcmp(line, "timeout")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_timeout(token);
} else if (0 == strcmp(line, "default")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_default(token);
} else if (0 == strcmp(line, "title")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_title(token);
#ifndef NO_SPLASHIMAGE
} else if (0 == strcmp(line, "splashimage")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_splashimage(token);
#endif
} else if (0 == strcmp(line, "color")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_color(token);
} else if (0 == strcmp(line, "video")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_video(token);
} else if (0 == strcmp(line, "kernel")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_kernel(token);
} else if (0 == strcmp(line, "root")) {
if (token == NULL) {
return ERR_MISSING_TOKEN;
}
return parse_root(token);
} else if (0 == strcmp(line, "reboot")) {
if (token) {
return ERR_EXTRA_TOKEN;
}
return parse_reboot();
} else if (0 == strcmp(line, "boot")) {
if (token) {
return ERR_EXTRA_TOKEN;
}
return parse_boot();
} else if ((0 == strcmp(line, "poweroff")) || (0 == strcmp(line, "halt"))) {
if (token) {
return ERR_EXTRA_TOKEN;
}
return parse_poweroff();
} else if ((0 == strcmp(line, "hiddenmenu")) || (0 == strcmp(line, "nomenu"))) {
if (token) {
return ERR_EXTRA_TOKEN;
}
return parse_nomenu();
} else if (0 == strcmp(line, "browse")) {
if (token) {
return ERR_EXTRA_TOKEN;
}
return parse_browse();
} else {
log_printf("unknown token: %s\n", line);
return ERR_UNKNOWN_TOKEN;
}
return 0;
}