2018-12-20 21:24:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/cfgdata/cfgdata_main.c
|
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-12-20 21:24:51 +01:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-12-20 21:24:51 +01:00
|
|
|
*
|
2021-06-10 13:17:16 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-12-20 21:24:51 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2022-03-28 19:15:31 +02:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2018-12-20 21:24:51 +01:00
|
|
|
#include <nuttx/mtd/configdata.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <fcntl.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2022-03-21 22:51:00 +01:00
|
|
|
#include <inttypes.h>
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char *g_config_dev = "/dev/config";
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Print usage information
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_help(void)
|
|
|
|
{
|
2020-02-23 05:51:44 +01:00
|
|
|
printf("\nUsage: cfgdata <cmd> [arguments]\n");
|
2018-12-20 21:24:51 +01:00
|
|
|
printf("Where <cmd> is one of:\n\n");
|
2022-08-04 18:34:59 +02:00
|
|
|
printf(" all: show all config entries\n");
|
|
|
|
printf(" print: display a specific config entry\n");
|
|
|
|
printf(" set: set or change a config entry\n");
|
|
|
|
printf(" unset: delete a config entry\n");
|
|
|
|
printf(" format: delete all config entries\n\n");
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
printf("Syntax for 'set' cmd:\n");
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
printf(" set name [hex_byte,hex_byte,etc.]\n");
|
|
|
|
printf(" set name \"string\"\n\n");
|
|
|
|
#else
|
|
|
|
printf(" set id,instance [bytes]\n");
|
|
|
|
printf(" set id,instance \"string\"\n\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("Syntax for 'print' cmd:\n");
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
printf(" print name\n");
|
|
|
|
#else
|
|
|
|
printf(" print id,instance\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("Syntax for 'unset' cmd:\n");
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
printf(" unset name\n");
|
|
|
|
#else
|
|
|
|
printf(" unset id,instance\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Parse out the id,inst,name tokens
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
static int cfgdatacmd_idtok(int startpos, char *token)
|
|
|
|
{
|
|
|
|
while (token[startpos] != ',' && token[startpos] != '\0')
|
2022-03-21 22:51:00 +01:00
|
|
|
{
|
|
|
|
startpos++;
|
|
|
|
}
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
if (token[startpos] != ',')
|
|
|
|
{
|
|
|
|
/* Error in format */
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
printf("Expected config identifier in 'id,instance' format\n");
|
2018-12-20 21:24:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return, skipping the ',' */
|
|
|
|
|
|
|
|
return startpos + 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Set a config item value
|
|
|
|
*
|
|
|
|
* config set 1,0,wr_width_cs0 [0x3]
|
|
|
|
* config set 1,1,wr_width_cs1 [0x2]
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_parse_byte_array(struct config_data_s *cfg,
|
2022-03-21 22:51:00 +01:00
|
|
|
int argc, char *argv[])
|
2018-12-20 21:24:51 +01:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int c;
|
|
|
|
int count;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
/* Start with arg3 */
|
|
|
|
|
|
|
|
x = 3;
|
|
|
|
c = 1;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* Loop for all remaining arguments until ']' found */
|
|
|
|
|
|
|
|
while (x < argc && argv[x][c] != ']' && argv[x][c] != '\0')
|
|
|
|
{
|
|
|
|
/* Count this item */
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* Skip to next item */
|
|
|
|
|
|
|
|
while (argv[x][c] != ',' && argv[x][c] != ']' && argv[x][c] != 0)
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for comma separator */
|
|
|
|
|
|
|
|
if (argv[x][c] == ',')
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for space separated items */
|
|
|
|
|
|
|
|
if (argv[x][c] == 0)
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test if static stack space allocation is big enough for data */
|
|
|
|
|
|
|
|
if (count > cfg->len)
|
|
|
|
{
|
|
|
|
/* Perform dynamic memory allocation */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
cfg->configdata = (FAR uint8_t *)malloc(count);
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg->len = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count determined. Start with arg3 again and parse the bytes */
|
|
|
|
|
|
|
|
x = 3;
|
|
|
|
c = 1;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* Loop for all remaining arguments until ']' found */
|
|
|
|
|
|
|
|
while (x < argc && argv[x][c] != ']' && argv[x][c] != '\0')
|
|
|
|
{
|
|
|
|
/* Parse this item */
|
|
|
|
|
|
|
|
if (strncmp(&argv[x][c], "0x", 2) == 0)
|
|
|
|
{
|
|
|
|
/* Hex byte */
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
sscanf(&argv[x][c + 2], "%x", &val);
|
2022-03-21 22:51:00 +01:00
|
|
|
cfg->configdata[count] = (uint8_t)val;
|
2018-12-20 21:24:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Decimal value */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
cfg->configdata[count] = (uint8_t)atoi(&argv[x][c]);
|
2018-12-20 21:24:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the count */
|
|
|
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* Skip to next item */
|
|
|
|
|
|
|
|
while (argv[x][c] != ',' && argv[x][c] != ']' && argv[x][c] != 0)
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for comma separator */
|
|
|
|
|
|
|
|
if (argv[x][c] == ',')
|
|
|
|
{
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for space separated items */
|
|
|
|
|
|
|
|
if (argv[x][c] == 0)
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->len = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Set a config item value
|
|
|
|
*
|
|
|
|
* config set 1,0,wr_width_cs0 [0x3]
|
|
|
|
* config set 1,1,wr_width_cs1 [0x2]
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_set(int argc, char *argv[])
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
int x;
|
|
|
|
struct config_data_s cfg;
|
|
|
|
uint8_t data[32];
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
|
|
|
|
/* Copy the name to the cfg struct */
|
|
|
|
|
2023-03-05 18:26:18 +01:00
|
|
|
strlcpy(cfg.name, argv[2], CONFIG_MTD_CONFIG_NAME_LEN);
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#else
|
2020-01-02 13:09:50 +01:00
|
|
|
|
2018-12-20 21:24:51 +01:00
|
|
|
/* Parse the id and instance */
|
|
|
|
|
|
|
|
cfg.id = atoi(argv[2]);
|
|
|
|
|
|
|
|
/* Advance past ',' to instance number */
|
|
|
|
|
|
|
|
x = cfgdatacmd_idtok(0, argv[2]);
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert instance to integer */
|
|
|
|
|
|
|
|
cfg.instance = atoi(&argv[2][x]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Test if data is an array of bytes or simple string */
|
|
|
|
|
|
|
|
if (argv[3][0] == '[')
|
|
|
|
{
|
|
|
|
/* It is an array of bytes. Count the number of bytes */
|
|
|
|
|
|
|
|
cfg.configdata = data;
|
|
|
|
cfg.len = sizeof(data);
|
|
|
|
cfgdatacmd_parse_byte_array(&cfg, argc, argv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool isnumber = true;
|
|
|
|
|
|
|
|
/* It is a simple string. Test if it looks like a number */
|
|
|
|
|
|
|
|
cfg.configdata = data;
|
|
|
|
if (strncmp(argv[3], "0x", 2) == 0)
|
|
|
|
{
|
|
|
|
/* Test for all hex digit values */
|
|
|
|
|
|
|
|
for (x = 2; x < strlen(argv[3]); x++)
|
|
|
|
{
|
|
|
|
if (!isxdigit(argv[3][x]))
|
|
|
|
{
|
|
|
|
isnumber = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isnumber)
|
|
|
|
{
|
2022-03-21 22:51:00 +01:00
|
|
|
sscanf(&argv[3][2], "%" SCNx32, (int32_t *)&cfg.configdata);
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg.len = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Test for all hex digit values */
|
|
|
|
|
|
|
|
for (x = 0; x < strlen(argv[3]); x++)
|
|
|
|
{
|
|
|
|
if (!isdigit(argv[3][x]))
|
|
|
|
{
|
|
|
|
isnumber = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isnumber)
|
|
|
|
{
|
|
|
|
int32_t temp = atoi(argv[3]);
|
2022-03-21 22:51:00 +01:00
|
|
|
*((int32_t *)cfg.configdata) = temp;
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg.len = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isnumber)
|
|
|
|
{
|
|
|
|
/* Point to the string and calculate the length */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
cfg.configdata = (FAR uint8_t *)argv[3];
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg.len = strlen(argv[3]) + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now open the /dev/config file and set the config item */
|
|
|
|
|
2022-04-01 09:53:43 +02:00
|
|
|
if ((fd = open(g_config_dev, 0)) < 2)
|
2018-12-20 21:24:51 +01:00
|
|
|
{
|
|
|
|
/* Display error */
|
|
|
|
|
|
|
|
printf("error: unable to open %s\n", g_config_dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
ret = ioctl(fd, CFGDIOC_SETCONFIG, (unsigned long)(uintptr_t)&cfg);
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
/* Close the file and report error if any */
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
printf("Error %d setting config entry\n", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the cfg.configdata if needed */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
if (cfg.configdata != (FAR uint8_t *)argv[3] &&
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg.configdata != data)
|
|
|
|
{
|
|
|
|
free(cfg.configdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Unset a config item value
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_unset(int argc, char *argv[])
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
struct config_data_s cfg;
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
/* Copy the name to the cfg struct */
|
|
|
|
|
2023-03-05 18:26:18 +01:00
|
|
|
strlcpy(cfg.name, argv[2], CONFIG_MTD_CONFIG_NAME_LEN);
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#else
|
2020-01-02 13:09:50 +01:00
|
|
|
int x;
|
|
|
|
|
2018-12-20 21:24:51 +01:00
|
|
|
/* Parse the id and instance */
|
|
|
|
|
|
|
|
cfg.id = atoi(argv[2]);
|
|
|
|
|
|
|
|
/* Advance past ',' to instance number */
|
|
|
|
|
|
|
|
x = cfgdatacmd_idtok(0, argv[2]);
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert instance to integer */
|
|
|
|
|
|
|
|
cfg.instance = atoi(&argv[2][x]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cfg.configdata = NULL;
|
|
|
|
cfg.len = 0;
|
|
|
|
|
|
|
|
/* Try to open the /dev/config file */
|
|
|
|
|
2022-04-01 09:53:43 +02:00
|
|
|
if ((fd = open(g_config_dev, 0)) < 2)
|
2018-12-20 21:24:51 +01:00
|
|
|
{
|
|
|
|
/* Display error */
|
|
|
|
|
|
|
|
printf("error: unable to open %s\n", g_config_dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the config item */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
ret = ioctl(fd, CFGDIOC_DELCONFIG, (unsigned long)(uintptr_t)&cfg);
|
2018-12-20 21:24:51 +01:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
printf("Error deletign config entry '%s'\n", argv[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Print a config item value
|
|
|
|
*
|
|
|
|
* config print 1,1
|
|
|
|
* config print wr_width_cs0
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_print(int argc, char *argv[])
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
int x;
|
|
|
|
struct config_data_s cfg;
|
|
|
|
bool isstring;
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
|
|
|
|
/* Copy the name to the cfg struct */
|
|
|
|
|
2023-03-05 18:26:18 +01:00
|
|
|
strlcpy(cfg.name, argv[2], CONFIG_MTD_CONFIG_NAME_LEN);
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
#else
|
2020-01-02 13:09:50 +01:00
|
|
|
|
2018-12-20 21:24:51 +01:00
|
|
|
/* Parse the id and instance */
|
|
|
|
|
|
|
|
cfg.id = atoi(argv[2]);
|
|
|
|
|
|
|
|
/* Advance past ',' to instance number */
|
2020-01-31 15:13:45 +01:00
|
|
|
|
2018-12-20 21:24:51 +01:00
|
|
|
x = cfgdatacmd_idtok(0, argv[2]);
|
|
|
|
if (x == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert instance to integer */
|
|
|
|
|
|
|
|
cfg.instance = atoi(&argv[2][x]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Try to open the /dev/config file */
|
|
|
|
|
|
|
|
if ((fd = open(g_config_dev, O_RDONLY)) < 2)
|
|
|
|
{
|
|
|
|
/* Display error */
|
|
|
|
|
|
|
|
printf("error: unable to open %s\n", g_config_dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
cfg.configdata = (FAR uint8_t *)malloc(256);
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg.len = 256;
|
|
|
|
if (cfg.configdata == NULL)
|
|
|
|
{
|
|
|
|
printf("Error allocating buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the config item */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
ret = ioctl(fd, CFGDIOC_GETCONFIG, (unsigned long)(uintptr_t)&cfg);
|
2018-12-20 21:24:51 +01:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
printf("Error reading config entry '%s'\n", argv[2]);
|
|
|
|
free(cfg.configdata);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display the data */
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
isstring = cfg.configdata[cfg.len - 1] == 0;
|
2018-12-20 21:24:51 +01:00
|
|
|
for (x = 0; x < cfg.len - 1; x++)
|
|
|
|
{
|
|
|
|
/* Test for all ascii characters */
|
|
|
|
|
|
|
|
if (cfg.configdata[x] < ' ' || cfg.configdata[x] > '~')
|
|
|
|
{
|
|
|
|
isstring = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display the data */
|
|
|
|
|
|
|
|
if (isstring)
|
|
|
|
{
|
|
|
|
printf("%s\n", cfg.configdata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Loop though all bytes and display them */
|
|
|
|
|
|
|
|
for (x = 0; x < cfg.len; x++)
|
|
|
|
{
|
|
|
|
/* Print the next byte */
|
|
|
|
|
|
|
|
printf("0x%02X ", cfg.configdata[x]);
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
if (((x + 1) & 7) == 0 && x + 1 != cfg.len)
|
2018-12-20 21:24:51 +01:00
|
|
|
{
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cfg.configdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Enumerate and display all config items
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_show_all_config_items(void)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
int x;
|
|
|
|
struct config_data_s cfg;
|
|
|
|
char fmtstr[24];
|
|
|
|
bool isstring;
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
/* Try to open the /dev/config file */
|
|
|
|
|
2022-04-01 09:53:43 +02:00
|
|
|
if ((fd = open(g_config_dev, 0)) < 2)
|
2018-12-20 21:24:51 +01:00
|
|
|
{
|
|
|
|
/* Display error */
|
|
|
|
|
|
|
|
printf("error: unable to open %s\n", g_config_dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print header */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2023-03-05 16:55:15 +01:00
|
|
|
snprintf(fmtstr, sizeof(fmtstr),
|
|
|
|
"%%-%ds%%-6sData\n", CONFIG_MTD_CONFIG_NAME_LEN);
|
2018-12-20 21:24:51 +01:00
|
|
|
printf(fmtstr, "Name", "Len");
|
2023-03-05 16:55:15 +01:00
|
|
|
snprintf(fmtstr, sizeof(fmtstr),
|
|
|
|
"%%-%ds%%-6d", CONFIG_MTD_CONFIG_NAME_LEN);
|
2018-12-20 21:24:51 +01:00
|
|
|
#else
|
2023-02-09 12:01:32 +01:00
|
|
|
strlcpy(fmtstr, "%-6s%-6s%-6sData\n", sizeof(fmtstr));
|
2018-12-20 21:24:51 +01:00
|
|
|
printf(fmtstr, "ID", "Inst", "Len");
|
2023-02-09 12:01:32 +01:00
|
|
|
strlcpy(fmtstr, "%-6d%-6d%-6d", sizeof(fmtstr));
|
2018-12-20 21:24:51 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the first config item */
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
cfg.configdata = (FAR uint8_t *)malloc(256);
|
2018-12-20 21:24:51 +01:00
|
|
|
cfg.len = 256;
|
|
|
|
if (cfg.configdata == NULL)
|
|
|
|
{
|
|
|
|
printf("Error allocating buffer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
ret = ioctl(fd, CFGDIOC_FIRSTCONFIG, (unsigned long)(uintptr_t)&cfg);
|
2018-12-20 21:24:51 +01:00
|
|
|
|
|
|
|
while (ret != -1)
|
|
|
|
{
|
|
|
|
/* Print this entry */
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
|
|
|
printf(fmtstr, cfg.name, cfg.len);
|
|
|
|
#else
|
|
|
|
printf(fmtstr, cfg.id, cfg.instance, cfg.len);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Test if data is a string */
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
isstring = cfg.configdata[cfg.len - 1] == 0;
|
2018-12-20 21:24:51 +01:00
|
|
|
for (x = 0; x < cfg.len - 1; x++)
|
|
|
|
{
|
|
|
|
/* Test for all ascii characters */
|
|
|
|
|
|
|
|
if (cfg.configdata[x] < ' ' || cfg.configdata[x] > '~')
|
|
|
|
{
|
|
|
|
isstring = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Display the data */
|
|
|
|
|
|
|
|
if (isstring)
|
|
|
|
{
|
|
|
|
printf("%s\n", cfg.configdata);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char fmtstr2[10];
|
|
|
|
|
|
|
|
#ifdef CONFIG_MTD_CONFIG_NAMED
|
2023-03-05 16:55:15 +01:00
|
|
|
snprintf(fmtstr2, sizeof(fmtstr2),
|
|
|
|
"\n%ds", CONFIG_MTD_CONFIG_NAME_LEN + 6);
|
2018-12-20 21:24:51 +01:00
|
|
|
#else
|
2023-02-09 12:01:32 +01:00
|
|
|
strlcpy(fmtstr2, "\n%18s", sizeof(fmtstr2));
|
2018-12-20 21:24:51 +01:00
|
|
|
#endif
|
|
|
|
/* Loop though all bytes and display them */
|
|
|
|
|
|
|
|
for (x = 0; x < cfg.len; x++)
|
|
|
|
{
|
|
|
|
/* Print the next byte */
|
|
|
|
|
|
|
|
printf("0x%02X ", cfg.configdata[x]);
|
|
|
|
|
2021-06-10 18:40:35 +02:00
|
|
|
if (((x + 1) & 7) == 0 && x + 1 != cfg.len)
|
2018-12-20 21:24:51 +01:00
|
|
|
{
|
|
|
|
printf(fmtstr2, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next config item */
|
|
|
|
|
|
|
|
cfg.len = 256;
|
2022-03-21 22:51:00 +01:00
|
|
|
ret = ioctl(fd, CFGDIOC_NEXTCONFIG, (unsigned long)(uintptr_t)&cfg);
|
2018-12-20 21:24:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
free(cfg.configdata);
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Erase all config items
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void cfgdatacmd_format(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Try to open the /dev/config file */
|
|
|
|
|
2022-04-01 09:53:43 +02:00
|
|
|
if ((fd = open(g_config_dev, 0)) < 2)
|
2022-03-21 22:51:00 +01:00
|
|
|
{
|
|
|
|
/* Display error */
|
|
|
|
|
|
|
|
printf("error: unable to open %s\n", g_config_dev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(fd, MTDIOC_BULKERASE, 0);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
printf("Error %d config format\n", errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:24:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
/* Argument given? */
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
/* Show usage info and exit */
|
|
|
|
|
|
|
|
cfgdatacmd_help();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for "all" cmd */
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "all") == 0)
|
|
|
|
{
|
|
|
|
/* Print the existing config items */
|
|
|
|
|
|
|
|
cfgdatacmd_show_all_config_items();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for "set" cmd */
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "set") == 0)
|
|
|
|
{
|
|
|
|
if (argc < 4)
|
|
|
|
{
|
|
|
|
printf("At least 2 arguments needed for 'set' command\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the routine to set a config item */
|
|
|
|
|
|
|
|
cfgdatacmd_set(argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for "print" cmd */
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "print") == 0)
|
|
|
|
{
|
|
|
|
/* Test for print all */
|
|
|
|
|
|
|
|
if (strcmp(argv[2], "all") == 0)
|
|
|
|
{
|
|
|
|
cfgdatacmd_show_all_config_items();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Call the routine to print a config item */
|
|
|
|
|
|
|
|
cfgdatacmd_print(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test for "unset" cmd */
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "unset") == 0)
|
|
|
|
{
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
printf("Need 1 argument for 'unset' command\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call the routine to set a config item */
|
|
|
|
|
|
|
|
cfgdatacmd_unset(argc, argv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:51:00 +01:00
|
|
|
/* Test for "format" cmd */
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "format") == 0)
|
|
|
|
{
|
|
|
|
/* Call the routine to erase all config items */
|
|
|
|
|
|
|
|
cfgdatacmd_format();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-20 21:24:51 +01:00
|
|
|
/* Unknown cmd */
|
|
|
|
|
|
|
|
printf("Unknown config command: %s\n", argv[1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|