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
|
/*
* RouterBOOT configuration utility
*
* Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <linux/limits.h>
#include "rbcfg.h"
#include "cyg_crc.h"
#define RBCFG_TMP_FILE "/tmp/.rbcfg"
#define RBCFG_MTD_NAME "soft_config"
#define RB_ERR_NOTFOUND 1
#define RB_ERR_INVALID 2
#define RB_ERR_NOMEM 3
#define RB_ERR_IO 4
#define ARRAY_SIZE(_a) (sizeof((_a)) / sizeof((_a)[0]))
struct rbcfg_ctx {
char *mtd_device;
char *tmp_file;
char *buf;
unsigned buflen;
};
struct rbcfg_value {
const char *name;
const char *desc;
union {
uint32_t u32;
const char *raw;
} val;
};
#define RBCFG_ENV_TYPE_U32 0
struct rbcfg_env {
const char *name;
int type;
uint16_t id;
const struct rbcfg_value *values;
int num_values;
};
#define CMD_FLAG_USES_CFG 0x01
struct rbcfg_command {
const char *command;
const char *usage;
int flags;
int (*exec)(int argc, const char *argv[]);
};
static void usage(void);
/* Globals */
static struct rbcfg_ctx *rbcfg_ctx;
static char *rbcfg_name;
#define CFG_U32(_name, _desc, _val) { \
.name = (_name), \
.desc = (_desc), \
.val.u32 = (_val), \
}
static const struct rbcfg_value rbcfg_boot_delay[] = {
CFG_U32("1", "1 second", RB_BOOT_DELAY_1SEC),
CFG_U32("2", "2 seconds", RB_BOOT_DELAY_2SEC),
CFG_U32("3", "3 seconds", RB_BOOT_DELAY_3SEC),
CFG_U32("4", "4 seconds", RB_BOOT_DELAY_4SEC),
CFG_U32("5", "5 seconds", RB_BOOT_DELAY_5SEC),
CFG_U32("6", "6 seconds", RB_BOOT_DELAY_6SEC),
CFG_U32("7", "7 seconds", RB_BOOT_DELAY_7SEC),
CFG_U32("8", "8 seconds", RB_BOOT_DELAY_8SEC),
CFG_U32("9", "9 seconds", RB_BOOT_DELAY_9SEC),
};
static const struct rbcfg_value rbcfg_boot_device[] = {
CFG_U32("eth", "boot over Ethernet",
RB_BOOT_DEVICE_ETHER),
CFG_U32("nandeth", "boot from NAND, if fail then Ethernet",
RB_BOOT_DEVICE_NANDETH),
CFG_U32("ethnand", "boot Ethernet once, then NAND",
RB_BOOT_DEVICE_ETHONCE),
CFG_U32("nand", "boot from NAND only",
RB_BOOT_DEVICE_NANDONLY),
CFG_U32("flash", "boot in flash configuration mode",
RB_BOOT_DEVICE_FLASHCFG),
CFG_U32("flashnand", "boot in flash configuration mode once, then NAND",
RB_BOOT_DEVICE_FLSHONCE),
};
static const struct rbcfg_value rbcfg_boot_key[] = {
CFG_U32("any", "any key", RB_BOOT_KEY_ANY),
CFG_U32("del", "<Delete> key only", RB_BOOT_KEY_DEL),
};
static const struct rbcfg_value rbcfg_boot_protocol[] = {
CFG_U32("bootp", "BOOTP protocol", RB_BOOT_PROTOCOL_BOOTP),
CFG_U32("dhcp", "DHCP protocol", RB_BOOT_PROTOCOL_DHCP),
};
static const struct rbcfg_value rbcfg_uart_speed[] = {
CFG_U32("115200", "", RB_UART_SPEED_115200),
CFG_U32("57600", "", RB_UART_SPEED_57600),
CFG_U32("38400", "", RB_UART_SPEED_38400),
CFG_U32("19200", "", RB_UART_SPEED_19200),
CFG_U32("9600", "", RB_UART_SPEED_9600),
CFG_U32("4800", "", RB_UART_SPEED_4800),
CFG_U32("2400", "", RB_UART_SPEED_2400),
CFG_U32("1200", "", RB_UART_SPEED_1200),
CFG_U32("off", "disable console output", RB_UART_SPEED_OFF),
};
static const struct rbcfg_value rbcfg_cpu_mode[] = {
CFG_U32("powersave", "power save", RB_CPU_MODE_POWERSAVE),
CFG_U32("regular", "regular (better for -0c environment)",
RB_CPU_MODE_REGULAR),
};
static const struct rbcfg_value rbcfg_booter[] = {
CFG_U32("regular", "load regular booter", RB_BOOTER_REGULAR),
CFG_U32("backup", "force backup-booter loading", RB_BOOTER_BACKUP),
};
static const struct rbcfg_env rbcfg_envs[] = {
{
.name = "boot_delay",
.id = RB_ID_BOOT_DELAY,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_boot_delay,
.num_values = ARRAY_SIZE(rbcfg_boot_delay),
}, {
.name = "boot_device",
.id = RB_ID_BOOT_DEVICE,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_boot_device,
.num_values = ARRAY_SIZE(rbcfg_boot_device),
}, {
.name = "boot_key",
.id = RB_ID_BOOT_KEY,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_boot_key,
.num_values = ARRAY_SIZE(rbcfg_boot_key),
}, {
.name = "boot_protocol",
.id = RB_ID_BOOT_PROTOCOL,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_boot_protocol,
.num_values = ARRAY_SIZE(rbcfg_boot_protocol),
}, {
.name = "booter",
.id = RB_ID_BOOTER,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_booter,
.num_values = ARRAY_SIZE(rbcfg_booter),
}, {
.name = "cpu_mode",
.id = RB_ID_CPU_MODE,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_cpu_mode,
.num_values = ARRAY_SIZE(rbcfg_cpu_mode),
}, {
.name = "uart_speed",
.id = RB_ID_UART_SPEED,
.type = RBCFG_ENV_TYPE_U32,
.values = rbcfg_uart_speed,
.num_values = ARRAY_SIZE(rbcfg_uart_speed),
}
};
static inline uint16_t
get_u16(const void *buf)
{
const uint8_t *p = buf;
return ((uint16_t) p[1] + ((uint16_t) p[0] << 8));
}
static inline uint32_t
get_u32(const void *buf)
{
const uint8_t *p = buf;
return ((uint32_t) p[3] + ((uint32_t) p[2] << 8) +
((uint32_t) p[1] << 16) + ((uint32_t) p[0] << 24));
}
static inline void
put_u32(void *buf, uint32_t val)
{
uint8_t *p = buf;
p[3] = val & 0xff;
p[2] = (val >> 8) & 0xff;
p[1] = (val >> 16) & 0xff;
p[0] = (val >> 24) & 0xff;
}
static int
rbcfg_find_tag(struct rbcfg_ctx *ctx, uint16_t tag_id, uint16_t *tag_len,
void **tag_data)
{
uint16_t id;
uint16_t len;
char *buf = ctx->buf;
unsigned int buflen = ctx->buflen;
int ret = RB_ERR_NOTFOUND;
/* skip magic and CRC value */
buf += 8;
buflen -= 8;
while (buflen > 2) {
len = get_u16(buf);
buf += 2;
buflen -= 2;
if (buflen < 2)
break;
id = get_u16(buf);
buf += 2;
buflen -= 2;
if (id == RB_ID_TERMINATOR)
break;
if (buflen < len)
break;
if (id == tag_id) {
*tag_len = len;
*tag_data = buf;
ret = 0;
break;
}
buf += len;
buflen -= len;
}
if (ret)
fprintf(stderr, "no tag found with id=%u\n", tag_id);
return ret;
}
static int
rbcfg_get_u32(struct rbcfg_ctx *ctx, uint16_t id, uint32_t *val)
{
void *tag_data;
uint16_t tag_len;
int err;
err = rbcfg_find_tag(ctx, id, &tag_len, &tag_data);
if (err)
return err;
*val = get_u32(tag_data);
return 0;
}
static int
rbcfg_set_u32(struct rbcfg_ctx *ctx, uint16_t id, uint32_t val)
{
void *tag_data;
uint16_t tag_len;
int err;
err = rbcfg_find_tag(ctx, id, &tag_len, &tag_data);
if (err)
return err;
put_u32(tag_data, val);
return 0;
}
char *rbcfg_find_mtd(const char *name, int *erase_size)
{
FILE *f;
int mtd_num;
char dev[PATH_MAX];
char *ret = NULL;
struct stat s;
int err;
f = fopen("/proc/mtd", "r");
if (!f)
return NULL;
while (1) {
char *p;
p = fgets(dev, sizeof(dev), f);
if (!p)
break;
if (!strstr(dev, name))
continue;
err = sscanf(dev, "mtd%d: %08x", &mtd_num, erase_size);
if (err != 2)
break;
sprintf(dev, "/dev/mtdblock%d", mtd_num);
err = stat(dev, &s);
if (err < 0)
break;
if ((s.st_mode & S_IFBLK) == 0)
break;
ret = malloc(strlen(dev) + 1);
if (ret == NULL)
break;
strncpy(ret, dev, strlen(dev) + 1);
break;
}
fclose(f);
return ret;
}
static int
rbcfg_check_tmp(struct rbcfg_ctx *ctx)
{
struct stat s;
int err;
err = stat(ctx->tmp_file, &s);
if (err < 0)
return 0;
if ((s.st_mode & S_IFREG) == 0)
return 0;
if (s.st_size != ctx->buflen)
return 0;
return 1;
}
static int
rbcfg_load(struct rbcfg_ctx *ctx)
{
uint32_t magic;
uint32_t crc_orig, crc;
char *name;
int tmp;
int fd;
int err;
tmp = rbcfg_check_tmp(ctx);
name = (tmp) ? ctx->tmp_file : ctx->mtd_device;
fd = open(name, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "unable to open %s\n", name);
err = RB_ERR_IO;
goto err;
}
err = read(fd, ctx->buf, ctx->buflen);
if (err != ctx->buflen) {
fprintf(stderr, "unable to read from %s\n", name);
err = RB_ERR_IO;
goto err_close;
}
magic = get_u32(ctx->buf);
if (magic != RB_MAGIC_SOFT) {
fprintf(stderr, "invalid configuration\n");
err = RB_ERR_INVALID;
goto err_close;
}
crc_orig = get_u32(ctx->buf + 4);
put_u32(ctx->buf + 4, 0);
crc = cyg_ether_crc32((unsigned char *) ctx->buf, ctx->buflen);
if (crc != crc_orig) {
fprintf(stderr, "configuration has CRC error\n");
err = RB_ERR_INVALID;
goto err_close;
}
err = 0;
err_close:
close(fd);
err:
return err;
}
static int
rbcfg_open()
{
char *mtd_device;
struct rbcfg_ctx *ctx;
int buflen;
int err;
mtd_device = rbcfg_find_mtd(RBCFG_MTD_NAME, &buflen);
if (!mtd_device) {
fprintf(stderr, "unable to find configuration\n");
return RB_ERR_NOTFOUND;
}
ctx = malloc(sizeof(struct rbcfg_ctx) + buflen);
if (ctx == NULL) {
err = RB_ERR_NOMEM;
goto err_free_mtd;
}
ctx->mtd_device = mtd_device;
ctx->tmp_file = RBCFG_TMP_FILE;
ctx->buflen = buflen;
ctx->buf = (char *) &ctx[1];
err = rbcfg_load(ctx);
if (err)
goto err_free_ctx;
rbcfg_ctx = ctx;
return 0;
err_free_ctx:
free(ctx);
err_free_mtd:
free(mtd_device);
return err;
}
static int
rbcfg_update(int tmp)
{
struct rbcfg_ctx *ctx = rbcfg_ctx;
char *name;
uint32_t crc;
int fd;
int err;
put_u32(ctx->buf, RB_MAGIC_SOFT);
put_u32(ctx->buf + 4, 0);
crc = cyg_ether_crc32((unsigned char *) ctx->buf, ctx->buflen);
put_u32(ctx->buf + 4, crc);
name = (tmp) ? ctx->tmp_file : ctx->mtd_device;
fd = open(name, O_WRONLY | O_CREAT);
if (fd < 0) {
fprintf(stderr, "unable to open %s for writing\n", name);
err = RB_ERR_IO;
goto out;
}
err = write(fd, ctx->buf, ctx->buflen);
if (err != ctx->buflen) {
err = RB_ERR_IO;
goto out_close;
}
fsync(fd);
err = 0;
out_close:
close(fd);
out:
return err;
}
static void
rbcfg_close(void)
{
struct rbcfg_ctx *ctx;
ctx = rbcfg_ctx;
free(ctx->mtd_device);
free(ctx);
}
static const struct rbcfg_value *
rbcfg_env_find(const struct rbcfg_env *env, const char *name)
{
unsigned i;
for (i = 0; i < env->num_values; i++) {
const struct rbcfg_value *v = &env->values[i];
if (strcmp(v->name, name) == 0)
return v;
}
return NULL;
}
static const struct rbcfg_value *
rbcfg_env_find_u32(const struct rbcfg_env *env, uint32_t val)
{
unsigned i;
for (i = 0; i < env->num_values; i++) {
const struct rbcfg_value *v = &env->values[i];
if (v->val.u32 == val)
return v;
}
return NULL;
}
static const char *
rbcfg_env_get_u32(const struct rbcfg_env *env)
{
const struct rbcfg_value *v;
uint32_t val;
int err;
err = rbcfg_get_u32(rbcfg_ctx, env->id, &val);
if (err)
return NULL;
v = rbcfg_env_find_u32(env, val);
if (v == NULL) {
fprintf(stderr, "unknown value %08x found for %s\n",
val, env->name);
return NULL;
}
return v->name;
}
static int
rbcfg_env_set_u32(const struct rbcfg_env *env, const char *data)
{
const struct rbcfg_value *v;
int err;
v = rbcfg_env_find(env, data);
if (v == NULL) {
fprintf(stderr, "invalid value '%s'\n", data);
return RB_ERR_INVALID;
}
err = rbcfg_set_u32(rbcfg_ctx, env->id, v->val.u32);
return err;
}
static const char *
rbcfg_env_get(const struct rbcfg_env *env)
{
const char *ret = NULL;
switch (env->type) {
case RBCFG_ENV_TYPE_U32:
ret = rbcfg_env_get_u32(env);
break;
}
return ret;
}
static int
rbcfg_env_set(const struct rbcfg_env *env, const char *data)
{
int ret = 0;
switch (env->type) {
case RBCFG_ENV_TYPE_U32:
ret = rbcfg_env_set_u32(env, data);
break;
}
return ret;
}
static int
rbcfg_cmd_apply(int argc, const char *argv[])
{
return rbcfg_update(0);
}
static int
rbcfg_cmd_help(int argc, const char *argv[])
{
usage();
return 0;
}
static int
rbcfg_cmd_get(int argc, const char *argv[])
{
int err = RB_ERR_NOTFOUND;
int i;
if (argc != 1) {
usage();
return RB_ERR_INVALID;
}
for (i = 0; i < ARRAY_SIZE(rbcfg_envs); i++) {
const struct rbcfg_env *env = &rbcfg_envs[i];
const char *value;
if (strcmp(env->name, argv[0]))
continue;
value = rbcfg_env_get(env);
if (value) {
fprintf(stdout, "%s\n", value);
err = 0;
}
break;
}
return err;
}
static int
rbcfg_cmd_set(int argc, const char *argv[])
{
int err = RB_ERR_INVALID;
int i;
if (argc != 2) {
/* not enough parameters */
usage();
return RB_ERR_INVALID;
}
for (i = 0; i < ARRAY_SIZE(rbcfg_envs); i++) {
const struct rbcfg_env *env = &rbcfg_envs[i];
if (strcmp(env->name, argv[0]))
continue;
err = rbcfg_env_set(env, argv[1]);
if (err == 0)
err = rbcfg_update(1);
break;
}
return err;
}
static int
rbcfg_cmd_show(int argc, const char *argv[])
{
int i;
if (argc != 0) {
usage();
return RB_ERR_INVALID;
}
for (i = 0; i < ARRAY_SIZE(rbcfg_envs); i++) {
const struct rbcfg_env *env = &rbcfg_envs[i];
const char *value;
value = rbcfg_env_get(env);
if (value)
fprintf(stdout, "%s=%s\n", env->name, value);
}
return 0;
}
static const struct rbcfg_command rbcfg_commands[] = {
{
.command = "apply",
.usage = "apply\n"
"\t- write configuration to the mtd device",
.flags = CMD_FLAG_USES_CFG,
.exec = rbcfg_cmd_apply,
}, {
.command = "help",
.usage = "help\n"
"\t- show this screen",
.exec = rbcfg_cmd_help,
}, {
.command = "get",
.usage = "get <name>\n"
"\t- get value of the configuration option <name>",
.flags = CMD_FLAG_USES_CFG,
.exec = rbcfg_cmd_get,
}, {
.command = "set",
.usage = "set <name> <value>\n"
"\t- set value of the configuration option <name> to <value>",
.flags = CMD_FLAG_USES_CFG,
.exec = rbcfg_cmd_set,
}, {
.command = "show",
.usage = "show\n"
"\t- show value of all configuration options",
.flags = CMD_FLAG_USES_CFG,
.exec = rbcfg_cmd_show,
}
};
static void
usage(void)
{
char buf[255];
int len;
int i;
fprintf(stderr, "Usage: %s <command>\n", rbcfg_name);
fprintf(stderr, "\nCommands:\n");
for (i = 0; i < ARRAY_SIZE(rbcfg_commands); i++) {
const struct rbcfg_command *cmd;
cmd = &rbcfg_commands[i];
len = snprintf(buf, sizeof(buf), "%s", cmd->usage);
buf[len] = '\0';
fprintf(stderr, "%s\n", buf);
}
fprintf(stderr, "\nConfiguration options:\n");
for (i = 0; i < ARRAY_SIZE(rbcfg_envs); i++) {
const struct rbcfg_env *env;
int j;
env = &rbcfg_envs[i];
fprintf(stderr, "\n%s:\n", env->name);
for (j = 0; j < env->num_values; j++) {
const struct rbcfg_value *v = &env->values[j];
fprintf(stderr, "\t%-12s %s\n", v->name, v->desc);
}
}
fprintf(stderr, "\n");
}
int main(int argc, const char *argv[])
{
const struct rbcfg_command *cmd = NULL;
int ret;
int i;
rbcfg_name = (char *) argv[0];
if (argc < 2) {
usage();
return EXIT_FAILURE;
}
for (i = 0; i < ARRAY_SIZE(rbcfg_commands); i++) {
if (strcmp(rbcfg_commands[i].command, argv[1]) == 0) {
cmd = &rbcfg_commands[i];
break;
}
}
if (cmd == NULL) {
fprintf(stderr, "unknown command '%s'\n", argv[1]);
usage();
return EXIT_FAILURE;
}
argc -= 2;
argv += 2;
if (cmd->flags & CMD_FLAG_USES_CFG) {
ret = rbcfg_open();
if (ret)
return EXIT_FAILURE;
}
ret = cmd->exec(argc, argv);
if (cmd->flags & CMD_FLAG_USES_CFG)
rbcfg_close();
if (ret)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
|