2019-02-08 11:55:35 +03:00
|
|
|
/* $NetBSD: memswitch.c,v 1.17 2019/02/08 08:55:35 isaki Exp $ */
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Minoura Makoto.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* memswitch.c */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
1999-06-28 12:48:35 +04:00
|
|
|
#include <errno.h>
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2018-01-26 12:38:26 +03:00
|
|
|
#ifndef SRAMDEBUG
|
1999-06-21 19:56:03 +04:00
|
|
|
#include <machine/sram.h>
|
1999-06-28 12:48:35 +04:00
|
|
|
#else
|
|
|
|
/*
|
2018-01-26 12:38:26 +03:00
|
|
|
* SRAMDEBUG -- works on other (faster) platforms;
|
1999-06-28 12:48:35 +04:00
|
|
|
* store in a regular file instead of actual non-volatile static RAM.
|
|
|
|
*/
|
2019-02-08 11:41:11 +03:00
|
|
|
#include <sys/stat.h>
|
1999-06-28 12:48:35 +04:00
|
|
|
#define PATH_RAMFILE "/tmp/sramfile"
|
|
|
|
#endif
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
#include "memswitch.h"
|
|
|
|
|
|
|
|
char *progname;
|
|
|
|
int nflag = 0;
|
|
|
|
u_int8_t *current_values = 0;
|
|
|
|
u_int8_t *modified_values = 0;
|
1999-06-28 12:48:35 +04:00
|
|
|
|
2018-01-24 00:06:24 +03:00
|
|
|
static void
|
1999-06-21 19:56:03 +04:00
|
|
|
usage(void)
|
|
|
|
{
|
2011-01-14 16:31:47 +03:00
|
|
|
fprintf(stderr, "usage: %s -a\n", progname);
|
|
|
|
fprintf(stderr, " %s [-h] variable ...\n", progname);
|
|
|
|
fprintf(stderr, " %s -w variable=value ...\n", progname);
|
|
|
|
fprintf(stderr, " %s [-rs] filename\n", progname);
|
1999-06-21 19:56:03 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-01-24 00:06:24 +03:00
|
|
|
main(int argc, char *argv[])
|
1999-06-21 19:56:03 +04:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
enum md {
|
|
|
|
MD_NONE, MD_WRITE, MD_HELP, MD_SHOWALL, MD_SAVE, MD_RESTORE
|
|
|
|
} mode = MD_NONE;
|
|
|
|
|
|
|
|
progname = argv[0];
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "whanrs")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'w': /* write */
|
|
|
|
mode = MD_WRITE;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
mode = MD_HELP;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
mode = MD_SHOWALL;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
mode = MD_SAVE;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
mode = MD_RESTORE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case MD_NONE:
|
|
|
|
if (argc == 0)
|
|
|
|
usage();
|
|
|
|
while (argv[0]) {
|
2011-01-14 16:31:47 +03:00
|
|
|
show_single(argv[0]);
|
1999-06-21 19:56:03 +04:00
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MD_SHOWALL:
|
|
|
|
if (argc)
|
|
|
|
usage();
|
|
|
|
show_all();
|
|
|
|
break;
|
|
|
|
case MD_WRITE:
|
|
|
|
if (argc == 0)
|
|
|
|
usage();
|
|
|
|
while (argv[0]) {
|
|
|
|
modify_single (argv[0]);
|
|
|
|
argv++;
|
|
|
|
}
|
2011-01-14 16:31:47 +03:00
|
|
|
flush();
|
1999-06-21 19:56:03 +04:00
|
|
|
break;
|
|
|
|
case MD_HELP:
|
|
|
|
if (argc == 0)
|
|
|
|
usage();
|
|
|
|
while (argv[0]) {
|
2011-01-14 16:31:47 +03:00
|
|
|
help_single(argv[0]);
|
1999-06-21 19:56:03 +04:00
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MD_SAVE:
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
save(argv[0]);
|
|
|
|
break;
|
|
|
|
case MD_RESTORE:
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
restore(argv[0]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-24 00:06:24 +03:00
|
|
|
show_single(const char *name)
|
1999-06-21 19:56:03 +04:00
|
|
|
{
|
|
|
|
int i;
|
2001-02-04 11:37:33 +03:00
|
|
|
int n = 0;
|
1999-06-21 19:56:03 +04:00
|
|
|
char fullname[50];
|
|
|
|
char valuestr[MAXVALUELEN];
|
|
|
|
|
|
|
|
for (i = 0; i < number_of_props; i++) {
|
2003-07-13 16:27:14 +04:00
|
|
|
snprintf(fullname, sizeof(fullname), "%s.%s",
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[i].class, properties[i].node);
|
2001-02-04 11:37:33 +03:00
|
|
|
if (strcmp(name, fullname) == 0 || strcmp(name, properties[i].class) == 0) {
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[i].print(&properties[i], valuestr);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (!nflag)
|
2011-01-14 16:31:47 +03:00
|
|
|
printf("%s=%s\n", fullname, valuestr);
|
2001-02-04 11:37:33 +03:00
|
|
|
n++;
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
}
|
2001-02-04 11:37:33 +03:00
|
|
|
if (n == 0) {
|
2011-01-14 16:31:47 +03:00
|
|
|
errx(1, "No such %s: %s", strstr(name, ".")?"property":"class", name);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
show_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char valuestr[MAXVALUELEN];
|
|
|
|
|
|
|
|
for (i = 0; i < number_of_props; i++) {
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[i].print(&properties[i], valuestr);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (!nflag)
|
2011-01-14 16:31:47 +03:00
|
|
|
printf("%s.%s=",
|
|
|
|
properties[i].class, properties[i].node);
|
|
|
|
printf("%s\n", valuestr);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-24 00:06:24 +03:00
|
|
|
modify_single(const char *expr)
|
1999-06-21 19:56:03 +04:00
|
|
|
{
|
2019-02-08 11:55:35 +03:00
|
|
|
int i;
|
|
|
|
char *buf;
|
|
|
|
char *p;
|
|
|
|
const char *class;
|
|
|
|
const char *node;
|
1999-06-21 19:56:03 +04:00
|
|
|
const char *value;
|
|
|
|
char valuestr[MAXVALUELEN];
|
|
|
|
|
2019-02-08 11:55:35 +03:00
|
|
|
buf = strdup(expr);
|
|
|
|
if (buf == NULL)
|
|
|
|
err(EXIT_FAILURE, "strdup failed");
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
for (class = p; *p; p++) {
|
|
|
|
if (*p == '.') {
|
|
|
|
*p++ = '\0';
|
1999-06-21 19:56:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 11:55:35 +03:00
|
|
|
for (node = p; *p; p++) {
|
|
|
|
if (*p == '=') {
|
|
|
|
*p++ = '\0';
|
1999-06-21 19:56:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 11:55:35 +03:00
|
|
|
value = p;
|
|
|
|
|
|
|
|
if (class[0] == '\0' || node[0] == '\0' || value[0] == '\0')
|
|
|
|
errx(1, "Invalid expression: %s", expr);
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
for (i = 0; i < number_of_props; i++) {
|
|
|
|
if (strcmp(properties[i].class, class) == 0 &&
|
|
|
|
strcmp(properties[i].node, node) == 0) {
|
|
|
|
if (properties[i].parse(&properties[i], value) < 0) {
|
|
|
|
/* error: do nothing */
|
|
|
|
} else {
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[i].print(&properties[i], valuestr);
|
1999-06-21 19:56:03 +04:00
|
|
|
printf("%s.%s -> %s\n", class, node, valuestr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i >= number_of_props) {
|
2011-01-14 16:31:47 +03:00
|
|
|
errx(1, "No such property: %s.%s", class, node);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
2019-02-08 11:55:35 +03:00
|
|
|
free(buf);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-24 00:06:24 +03:00
|
|
|
help_single(const char *name)
|
1999-06-21 19:56:03 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char fullname[50];
|
|
|
|
char valuestr[MAXVALUELEN];
|
|
|
|
|
|
|
|
for (i = 0; i < number_of_props; i++) {
|
2003-07-13 16:27:14 +04:00
|
|
|
snprintf(fullname, sizeof(fullname), "%s.%s",
|
|
|
|
properties[i].class, properties[i].node);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (strcmp(name, fullname) == 0) {
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[i].print(&properties[i], valuestr);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (!nflag)
|
2011-01-14 16:31:47 +03:00
|
|
|
printf("%s=", fullname);
|
|
|
|
printf("%s\n", valuestr);
|
|
|
|
printf("%s", properties[i].descr);
|
1999-06-21 19:56:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i >= number_of_props) {
|
2011-01-14 16:31:47 +03:00
|
|
|
errx(1, "No such property: %s", name);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alloc_modified_values(void)
|
|
|
|
{
|
|
|
|
if (current_values == 0)
|
|
|
|
alloc_current_values();
|
2011-01-14 16:31:47 +03:00
|
|
|
modified_values = malloc(256);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (modified_values == 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "malloc");
|
|
|
|
memcpy(modified_values, current_values, 256);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alloc_current_values(void)
|
|
|
|
{
|
2018-01-26 12:38:26 +03:00
|
|
|
#ifndef SRAMDEBUG
|
1999-06-21 19:56:03 +04:00
|
|
|
int i;
|
|
|
|
int sramfd = 0;
|
|
|
|
struct sram_io buffer;
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
current_values = malloc(256);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (current_values == 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "malloc");
|
1999-06-21 19:56:03 +04:00
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
sramfd = open(_PATH_DEVSRAM, O_RDONLY);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (sramfd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening %s", _PATH_DEVSRAM);
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
/* Assume SRAM_IO_SIZE = n * 16. */
|
|
|
|
for (i = 0; i < 256; i += SRAM_IO_SIZE) {
|
|
|
|
buffer.offset = i;
|
2011-01-14 16:31:47 +03:00
|
|
|
if (ioctl(sramfd, SIOGSRAM, &buffer) < 0)
|
|
|
|
err(1, "ioctl");
|
|
|
|
memcpy(¤t_values[i], buffer.sram, SRAM_IO_SIZE);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
close(sramfd);
|
1999-06-28 12:48:35 +04:00
|
|
|
#else
|
|
|
|
int i;
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
current_values = malloc(256);
|
1999-06-28 12:48:35 +04:00
|
|
|
if (current_values == 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "malloc");
|
1999-06-28 12:48:35 +04:00
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
fd = open(PATH_RAMFILE, O_RDONLY);
|
1999-06-28 12:48:35 +04:00
|
|
|
if (fd < 0 && errno == ENOENT) {
|
2011-01-14 16:31:47 +03:00
|
|
|
modified_values = malloc(256);
|
1999-06-28 12:48:35 +04:00
|
|
|
if (modified_values == 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, NULL);
|
1999-06-28 12:48:35 +04:00
|
|
|
for (i = 0; i < number_of_props; i++) {
|
|
|
|
properties[i].modified_value
|
|
|
|
= properties[i].default_value;
|
|
|
|
properties[i].modified = 1;
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[i].flush(&properties[i]);
|
1999-06-28 12:48:35 +04:00
|
|
|
}
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
fd = creat(PATH_RAMFILE, 0666);
|
1999-06-28 12:48:35 +04:00
|
|
|
if (fd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Creating %s", PATH_RAMFILE);
|
|
|
|
if (write(fd, modified_values, 256) != 256)
|
|
|
|
err(1, "Writing %s", PATH_RAMFILE);
|
|
|
|
close(fd);
|
|
|
|
free(modified_values);
|
1999-06-28 12:48:35 +04:00
|
|
|
modified_values = 0;
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
fd = open(PATH_RAMFILE, O_RDONLY);
|
1999-06-28 12:48:35 +04:00
|
|
|
}
|
|
|
|
if (fd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening %s", PATH_RAMFILE);
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
err(1, "fstat");
|
1999-06-28 12:48:35 +04:00
|
|
|
if (st.st_size != 256)
|
2011-01-14 16:31:47 +03:00
|
|
|
errx(1, "PANIC! INVALID RAMFILE");
|
|
|
|
if (read(fd, current_values, 256) != 256)
|
|
|
|
err(1, "reading %s", PATH_RAMFILE);
|
|
|
|
close(fd);
|
1999-06-28 12:48:35 +04:00
|
|
|
#endif
|
1999-06-21 19:56:03 +04:00
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
properties[PROP_MAGIC1].fill(&properties[PROP_MAGIC1]);
|
|
|
|
properties[PROP_MAGIC2].fill(&properties[PROP_MAGIC2]);
|
1999-06-21 19:56:03 +04:00
|
|
|
if ((properties[PROP_MAGIC1].current_value.longword != MAGIC1) ||
|
|
|
|
(properties[PROP_MAGIC2].current_value.longword != MAGIC2))
|
2011-01-14 16:31:47 +03:00
|
|
|
errx(1, "PANIC! INVALID MAGIC");
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
flush(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int sramfd = 0;
|
2018-01-26 12:38:26 +03:00
|
|
|
#ifndef SRAMDEBUG
|
1999-06-21 19:56:03 +04:00
|
|
|
struct sram_io buffer;
|
1999-06-28 12:48:35 +04:00
|
|
|
#endif
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
for (i = 0; i < number_of_props; i++) {
|
|
|
|
if (properties[i].modified)
|
|
|
|
properties[i].flush(&properties[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modified_values == 0)
|
|
|
|
/* Not modified at all. */
|
|
|
|
return;
|
|
|
|
|
2018-01-26 12:38:26 +03:00
|
|
|
#ifndef SRAMDEBUG
|
1999-06-21 19:56:03 +04:00
|
|
|
/* Assume SRAM_IO_SIZE = n * 16. */
|
|
|
|
for (i = 0; i < 256; i += SRAM_IO_SIZE) {
|
2011-01-14 16:31:47 +03:00
|
|
|
if (memcmp(¤t_values[i], &modified_values[i],
|
|
|
|
SRAM_IO_SIZE) == 0)
|
1999-06-21 19:56:03 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sramfd == 0) {
|
2011-01-14 16:31:47 +03:00
|
|
|
sramfd = open(_PATH_DEVSRAM, O_RDWR);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (sramfd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening %s", _PATH_DEVSRAM);
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
buffer.offset = i;
|
2011-01-14 16:31:47 +03:00
|
|
|
memcpy(buffer.sram, &modified_values[i], SRAM_IO_SIZE);
|
|
|
|
if (ioctl(sramfd, SIOPSRAM, &buffer) < 0)
|
|
|
|
err(1, "ioctl");
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
1999-06-28 12:48:35 +04:00
|
|
|
#else
|
2011-01-14 16:31:47 +03:00
|
|
|
sramfd = open(PATH_RAMFILE, O_WRONLY);
|
1999-06-28 12:48:35 +04:00
|
|
|
if (sramfd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening %s", PATH_RAMFILE);
|
|
|
|
if (write(sramfd, modified_values, 256) != 256)
|
|
|
|
err(1, "Writing %s", PATH_RAMFILE);
|
1999-06-28 12:48:35 +04:00
|
|
|
#endif
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
if (sramfd != 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
close(sramfd);
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-01-24 00:06:24 +03:00
|
|
|
save(const char *name)
|
1999-06-21 19:56:03 +04:00
|
|
|
{
|
2018-01-26 12:38:26 +03:00
|
|
|
#ifndef SRAMDEBUG
|
1999-06-21 19:56:03 +04:00
|
|
|
int fd;
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
alloc_current_values();
|
1999-06-21 19:56:03 +04:00
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
if (strcmp(name, "-") == 0)
|
1999-06-21 19:56:03 +04:00
|
|
|
fd = 1; /* standard output */
|
|
|
|
else {
|
2011-01-14 16:31:47 +03:00
|
|
|
fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (fd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening output file");
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
if (write(fd, current_values, 256) != 256)
|
|
|
|
err(1, "Writing output file");
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
if (fd != 1)
|
2011-01-14 16:31:47 +03:00
|
|
|
close(fd);
|
1999-06-28 12:48:35 +04:00
|
|
|
#else
|
2011-01-14 16:31:47 +03:00
|
|
|
fprintf(stderr, "Skipping save...\n");
|
1999-06-28 12:48:35 +04:00
|
|
|
#endif
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-01-24 00:06:24 +03:00
|
|
|
restore(const char *name)
|
1999-06-21 19:56:03 +04:00
|
|
|
{
|
2018-01-26 12:38:26 +03:00
|
|
|
#ifndef SRAMDEBUG
|
1999-06-21 19:56:03 +04:00
|
|
|
int sramfd, fd, i;
|
|
|
|
struct sram_io buffer;
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
modified_values = malloc(256);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (modified_values == 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening %s", _PATH_DEVSRAM);
|
1999-06-21 19:56:03 +04:00
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
if (strcmp(name, "-") == 0)
|
1999-06-21 19:56:03 +04:00
|
|
|
fd = 0; /* standard input */
|
|
|
|
else {
|
2011-01-14 16:31:47 +03:00
|
|
|
fd = open(name, O_RDONLY);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (fd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening input file");
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
if (read(fd, modified_values, 256) != 256)
|
|
|
|
err(1, "Reading input file");
|
2011-01-14 16:25:16 +03:00
|
|
|
|
|
|
|
if (fd != 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
close(fd);
|
2011-01-14 16:25:16 +03:00
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
sramfd = open(_PATH_DEVSRAM, O_RDWR);
|
1999-06-21 19:56:03 +04:00
|
|
|
if (sramfd < 0)
|
2011-01-14 16:31:47 +03:00
|
|
|
err(1, "Opening %s", _PATH_DEVSRAM);
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
/* Assume SRAM_IO_SIZE = n * 16. */
|
|
|
|
for (i = 0; i < 256; i += SRAM_IO_SIZE) {
|
|
|
|
buffer.offset = i;
|
2011-01-14 16:31:47 +03:00
|
|
|
memcpy(buffer.sram, &modified_values[i], SRAM_IO_SIZE);
|
|
|
|
if (ioctl(sramfd, SIOPSRAM, &buffer) < 0)
|
|
|
|
err(1, "ioctl");
|
1999-06-21 19:56:03 +04:00
|
|
|
}
|
|
|
|
|
2011-01-14 16:31:47 +03:00
|
|
|
close(sramfd);
|
1999-06-28 12:48:35 +04:00
|
|
|
#else
|
2011-01-14 16:31:47 +03:00
|
|
|
fprintf(stderr, "Skipping restore...\n");
|
1999-06-28 12:48:35 +04:00
|
|
|
#endif
|
1999-06-21 19:56:03 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|