123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791 |
- /*
- * 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),
- };
- 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;
- }
|