1 /* $NetBSD: memswitch.c,v 1.9 2005/06/11 18:42:56 he Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/ioctl.h>
45 #include <machine/sram.h>
48 * DEBUG -- works on other (faster) platforms;
49 * store in a regular file instead of actual non-volatile static RAM.
51 #define PATH_RAMFILE "/tmp/sramfile"
54 #include "memswitch.h"
58 u_int8_t
*current_values
= 0;
59 u_int8_t
*modified_values
= 0;
61 int main
__P((int, char*[]));
66 fprintf (stderr
, "usage: %s -a\n", progname
);
67 fprintf (stderr
, " %s [-h] variable ...\n", progname
);
68 fprintf (stderr
, " %s -w variable=value ...\n", progname
);
69 fprintf (stderr
, " %s [-rs] filename\n", progname
);
80 MD_NONE
, MD_WRITE
, MD_HELP
, MD_SHOWALL
, MD_SAVE
, MD_RESTORE
85 while ((ch
= getopt(argc
, argv
, "whanrs")) != -1) {
115 show_single (argv
[0]);
128 modify_single (argv
[0]);
137 help_single (argv
[0]);
164 char valuestr
[MAXVALUELEN
];
166 for (i
= 0; i
< number_of_props
; i
++) {
167 snprintf(fullname
, sizeof(fullname
), "%s.%s",
168 properties
[i
].class, properties
[i
].node
);
169 if (strcmp(name
, fullname
) == 0 || strcmp(name
, properties
[i
].class) == 0) {
170 properties
[i
].print (&properties
[i
], valuestr
);
172 printf ("%s=%s\n", fullname
, valuestr
);
177 errx (1, "No such %s: %s", strstr(name
, ".")?"property":"class", name
);
187 char valuestr
[MAXVALUELEN
];
189 for (i
= 0; i
< number_of_props
; i
++) {
190 properties
[i
].print (&properties
[i
], valuestr
);
193 properties
[i
].class, properties
[i
].node
);
194 printf ("%s\n", valuestr
);
205 char *class = NULL
, *node
= NULL
;
207 char valuestr
[MAXVALUELEN
];
211 for (i
= 0; i
< n
; i
++) {
212 if (expr
[i
] == '.') {
217 strncpy (class, expr
, i
);
223 errx (1, "Invalid expression: %s", expr
);
225 for ( ; i
< n
; i
++) {
226 if (expr
[i
] == '=') {
227 node
= alloca(i
- l
+ 1);
230 strncpy (node
, &(expr
[l
]), i
- l
);
236 errx (1, "Invalid expression: %s", expr
);
238 value
= &(expr
[++i
]);
240 for (i
= 0; i
< number_of_props
; i
++) {
241 if (strcmp(properties
[i
].class, class) == 0 &&
242 strcmp(properties
[i
].node
, node
) == 0) {
243 if (properties
[i
].parse(&properties
[i
], value
) < 0) {
244 /* error: do nothing */
246 properties
[i
].print (&properties
[i
], valuestr
);
247 printf("%s.%s -> %s\n", class, node
, valuestr
);
252 if (i
>= number_of_props
) {
253 errx (1, "No such property: %s.%s", class, node
);
265 char valuestr
[MAXVALUELEN
];
267 for (i
= 0; i
< number_of_props
; i
++) {
268 snprintf(fullname
, sizeof(fullname
), "%s.%s",
269 properties
[i
].class, properties
[i
].node
);
270 if (strcmp(name
, fullname
) == 0) {
271 properties
[i
].print (&properties
[i
], valuestr
);
273 printf ("%s=", fullname
);
274 printf ("%s\n", valuestr
);
275 printf ("%s", properties
[i
].descr
);
279 if (i
>= number_of_props
) {
280 errx (1, "No such property: %s", name
);
287 alloc_modified_values(void)
289 if (current_values
== 0)
290 alloc_current_values();
291 modified_values
= malloc (256);
292 if (modified_values
== 0)
294 memcpy (modified_values
, current_values
, 256);
298 alloc_current_values(void)
303 struct sram_io buffer
;
305 current_values
= malloc (256);
306 if (current_values
== 0)
309 sramfd
= open (_PATH_DEVSRAM
, O_RDONLY
);
311 err (1, "Opening %s", _PATH_DEVSRAM
);
313 /* Assume SRAM_IO_SIZE = n * 16. */
314 for (i
= 0; i
< 256; i
+= SRAM_IO_SIZE
) {
316 if (ioctl (sramfd
, SIOGSRAM
, &buffer
) < 0)
318 memcpy (¤t_values
[i
], buffer
.sram
, SRAM_IO_SIZE
);
327 current_values
= malloc (256);
328 if (current_values
== 0)
331 fd
= open (PATH_RAMFILE
, O_RDONLY
);
332 if (fd
< 0 && errno
== ENOENT
) {
333 modified_values
= malloc (256);
334 if (modified_values
== 0)
336 for (i
= 0; i
< number_of_props
; i
++) {
337 properties
[i
].modified_value
338 = properties
[i
].default_value
;
339 properties
[i
].modified
= 1;
340 properties
[i
].flush (&properties
[i
]);
343 fd
= creat (PATH_RAMFILE
, 0666);
345 err (1, "Creating %s", PATH_RAMFILE
);
346 if (write (fd
, modified_values
, 256) != 256)
347 err (1, "Writing %s", PATH_RAMFILE
);
349 free (modified_values
);
352 fd
= open (PATH_RAMFILE
, O_RDONLY
);
355 err (1, "Opening %s", PATH_RAMFILE
);
356 if (fstat (fd
, &st
) < 0)
358 if (st
.st_size
!= 256)
359 errx (1, "PANIC! INVALID RAMFILE");
360 if (read (fd
, current_values
, 256) != 256)
361 err (1, "reading %s", PATH_RAMFILE
);
365 properties
[PROP_MAGIC1
].fill (&properties
[PROP_MAGIC1
]);
366 properties
[PROP_MAGIC2
].fill (&properties
[PROP_MAGIC2
]);
367 if ((properties
[PROP_MAGIC1
].current_value
.longword
!= MAGIC1
) ||
368 (properties
[PROP_MAGIC2
].current_value
.longword
!= MAGIC2
))
369 errx (1, "PANIC! INVALID MAGIC");
378 struct sram_io buffer
;
381 for (i
= 0; i
< number_of_props
; i
++) {
382 if (properties
[i
].modified
)
383 properties
[i
].flush(&properties
[i
]);
386 if (modified_values
== 0)
387 /* Not modified at all. */
391 /* Assume SRAM_IO_SIZE = n * 16. */
392 for (i
= 0; i
< 256; i
+= SRAM_IO_SIZE
) {
393 if (memcmp (¤t_values
[i
], &modified_values
[i
],
398 sramfd
= open (_PATH_DEVSRAM
, O_RDWR
);
400 err (1, "Opening %s", _PATH_DEVSRAM
);
403 memcpy (buffer
.sram
, &modified_values
[i
], SRAM_IO_SIZE
);
404 if (ioctl (sramfd
, SIOPSRAM
, &buffer
) < 0)
408 sramfd
= open (PATH_RAMFILE
, O_WRONLY
);
410 err (1, "Opening %s", PATH_RAMFILE
);
411 if (write (sramfd
, modified_values
, 256) != 256)
412 err (1, "Writing %s", PATH_RAMFILE
);
428 alloc_current_values ();
430 if (strcmp (name
, "-") == 0)
431 fd
= 1; /* standard output */
433 fd
= open (name
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
435 err (1, "Opening output file");
438 if (write (fd
, current_values
, 256) != 256)
439 err (1, "Writing output file");
444 fprintf (stderr
, "Skipping save...\n");
456 struct sram_io buffer
;
458 modified_values
= malloc (256);
459 if (modified_values
== 0)
460 err (1, "Opening %s", _PATH_DEVSRAM
);
462 if (strcmp (name
, "-") == 0)
463 fd
= 0; /* standard input */
465 fd
= open (name
, O_RDONLY
);
467 err (1, "Opening input file");
470 sramfd
= open (_PATH_DEVSRAM
, O_RDONLY
);
472 err (1, "Opening %s", _PATH_DEVSRAM
);
474 /* Assume SRAM_IO_SIZE = n * 16. */
475 for (i
= 0; i
< 256; i
+= SRAM_IO_SIZE
) {
477 memcpy (buffer
.sram
, &modified_values
[i
], SRAM_IO_SIZE
);
478 if (ioctl (sramfd
, SIOPSRAM
, &buffer
) < 0)
484 fprintf (stderr
, "Skipping restore...\n");