Commit 44f9671b authored by Grzegorz Daniluk's avatar Grzegorz Daniluk

shell: bring back init command

parent 8720372d
......@@ -874,6 +874,117 @@ int set_persistent_mac(uint8_t portnum, uint8_t *mac)
return 0;
}
/*
* The init script area consist of 2-byte size field and a set of
* shell commands separated with '\n' character.
*
* -------------------
* | bytes used (2B) |
* ------------------------------------------------
* | shell commands separated with '\n'..... |
* | |
* | |
* ------------------------------------------------
*/
int storage_init_erase(void)
{
int ret;
if (sdbfs_open_id(&wrc_sdbfs, SDB_VENDOR, SDB_DEV_INIT) < 0)
return -1;
ret = sdbfs_ferase(&wrc_sdbfs, 0, wrc_sdbfs.f_len);
if (ret == wrc_sdbfs.f_len)
ret = 1;
sdbfs_close(&wrc_sdbfs);
return ret == 1 ? 0 : -1;
}
/*
* Appends a new shell command at the end of boot script
*/
int storage_init_add(const char *args[])
{
int len, i;
uint8_t separator = ' ';
uint16_t used, readback;
int ret = -1;
uint8_t byte;
if (sdbfs_open_id(&wrc_sdbfs, SDB_VENDOR, SDB_DEV_INIT) < 0)
return -1;
/* check how many bytes we already have there */
used = 0;
while (sdbfs_fread(&wrc_sdbfs, sizeof(used)+used, &byte, 1) == 1) {
if (byte == 0xff)
break;
used++;
}
if (used > 256 /* 0xffff or wrong */)
used = 0;
i = 1; /* args[0] is "add" */
while (args[i] != NULL) {
len = strlen(args[i]);
if (sdbfs_fwrite(&wrc_sdbfs, sizeof(used) + used,
(void *)args[i], len) != len)
goto out;
used += len;
if (args[i+1] != NULL) /* next one is another word of the same command */
separator = ' ';
else /* no more words, end command with '\n' */
separator = '\n';
if (sdbfs_fwrite(&wrc_sdbfs, sizeof(used) + used,
&separator, sizeof(separator))
!= sizeof(separator))
goto out;
++used;
++i;
}
/* and finally update the size of the script */
if (sdbfs_fwrite(&wrc_sdbfs, 0, &used, sizeof(used)) != sizeof(used))
goto out;
if (sdbfs_fread(&wrc_sdbfs, 0, &readback, sizeof(readback))
!= sizeof(readback))
goto out;
ret = 0;
out:
sdbfs_close(&wrc_sdbfs);
return ret;
}
int storage_init_show(void)
{
int ret = -1;
uint16_t used;
uint8_t byte;
if (sdbfs_open_id(&wrc_sdbfs, SDB_VENDOR, SDB_DEV_INIT) < 0)
return -1;
pp_printf("-- user-defined script --\n");
used = 0;
do {
if (sdbfs_fread(&wrc_sdbfs, sizeof(used) + used, &byte, 1) != 1)
goto out;
if (byte != 0xff) {
pp_printf("%c", byte);
used++;
}
} while (byte != 0xff);
if (used == 0)
pp_printf("(empty)\n");
ret = 0;
out:
sdbfs_close(&wrc_sdbfs);
return ret;
}
int storage_init_readcmd(uint8_t *buf, uint8_t bufsize, uint8_t next)
{
int i = 0, ret = -1;
......
......@@ -396,5 +396,6 @@ void shell_register_commands()
REGISTER_WRC_COMMAND(calibration);
REGISTER_WRC_COMMAND(help);
REGISTER_WRC_COMMAND(diag);
REGISTER_WRC_COMMAND(init);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment