add added files
[bcm963xx.git] / userapps / broadcom / nvram / main.c
1 /*
2  * Frontend command-line utility for Linux NVRAM layer
3  *
4  * Copyright 2005, Broadcom Corporation
5  * All Rights Reserved.
6  * 
7  * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8  * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9  * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10  * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
11  *
12  * $Id$
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <typedefs.h>
20 #include <bcmnvram.h>
21
22 static void
23 usage(void)
24 {
25         fprintf(stderr, "usage: nvram [get name] [set name=value] [unset name] [show]\n");
26         exit(0);
27 }
28
29 /* NVRAM utility */
30 int
31 main(int argc, char **argv)
32 {
33         char *name, *value, buf[NVRAM_SPACE];
34         int size;
35
36         /* Skip program name */
37         --argc;
38         ++argv;
39
40         if (!*argv) 
41                 usage();
42
43         /* Process the remaining arguments. */
44         for (; *argv; argv++) {
45                 if (!strncmp(*argv, "get", 3)) {
46                         if (*++argv) {
47                                 if ((value = nvram_get(*argv)))
48                                         puts(value);
49                         }
50                 }
51                 else if (!strncmp(*argv, "set", 3)) {
52                         if (*++argv) {
53                                 strncpy(value = buf, *argv, sizeof(buf));
54                                 name = strsep(&value, "=");
55                                 nvram_set(name, value);
56                                 nvram_commit();
57                         }
58                 }
59                 else if (!strncmp(*argv, "unset", 5)) {
60                         if (*++argv){
61                                 nvram_unset(*argv);
62                                 nvram_commit();
63                         }
64                 }
65                 else if (!strncmp(*argv, "commit", 5)) {
66                         nvram_commit();
67                 }
68                 else if (!strncmp(*argv, "show", 4) ||
69                            !strncmp(*argv, "getall", 6)) {
70                         nvram_getall(buf, sizeof(buf));
71                         for (name = buf; *name; name += strlen(name) + 1)
72                                 puts(name);
73                         size = sizeof(struct nvram_header) + (int) name - (int) buf;
74                         fprintf(stderr, "size: %d bytes (%d left)\n", size, NVRAM_SPACE - size);
75                 }
76                 
77                 if (!*argv)
78                         break;
79         }
80
81         return 0;
82 }