]>
Commit | Line | Data |
---|---|---|
6aff3115 | 1 | /* |
bc11756d | 2 | * (C) Copyright 2000-2008 |
6aff3115 WD |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
6aff3115 WD |
6 | */ |
7 | ||
8 | /* | |
3bac3513 | 9 | * Command line user interface to firmware (=U-Boot) environment. |
6aff3115 WD |
10 | * |
11 | * Implements: | |
bc11756d GE |
12 | * fw_printenv [[ -n name ] | [ name ... ]] |
13 | * - prints the value of a single environment variable | |
14 | * "name", the ``name=value'' pairs of one or more | |
15 | * environment variables "name", or the whole | |
16 | * environment if no names are specified. | |
6aff3115 WD |
17 | * fw_setenv name [ value ... ] |
18 | * - If a name without any values is given, the variable | |
19 | * with this name is deleted from the environment; | |
20 | * otherwise, all "value" arguments are concatenated, | |
bc11756d | 21 | * separated by single blank characters, and the |
6aff3115 WD |
22 | * resulting string is assigned to the environment |
23 | * variable "name" | |
24 | */ | |
25 | ||
e4a223f0 JH |
26 | #include <fcntl.h> |
27 | #include <getopt.h> | |
6aff3115 WD |
28 | #include <stdio.h> |
29 | #include <string.h> | |
30 | #include <stdlib.h> | |
e4a223f0 JH |
31 | #include <sys/file.h> |
32 | #include <unistd.h> | |
6aff3115 WD |
33 | #include "fw_env.h" |
34 | ||
35 | #define CMD_PRINTENV "fw_printenv" | |
36 | #define CMD_SETENV "fw_setenv" | |
37 | ||
bd7b26f8 SB |
38 | static struct option long_options[] = { |
39 | {"script", required_argument, NULL, 's'}, | |
40 | {"help", no_argument, NULL, 'h'}, | |
41 | {NULL, 0, NULL, 0} | |
42 | }; | |
43 | ||
44 | void usage(void) | |
45 | { | |
46 | ||
47 | fprintf(stderr, "fw_printenv/fw_setenv, " | |
48 | "a command line interface to U-Boot environment\n\n" | |
122bc088 | 49 | "usage:\tfw_printenv [-n] [variable name]\n" |
bd7b26f8 SB |
50 | "\tfw_setenv [variable name] [variable value]\n" |
51 | "\tfw_setenv -s [ file ]\n" | |
52 | "\tfw_setenv -s - < [ file ]\n\n" | |
53 | "The file passed as argument contains only pairs " | |
54 | "name / value\n" | |
55 | "Example:\n" | |
56 | "# Any line starting with # is treated as comment\n" | |
57 | "\n" | |
58 | "\t netdev eth0\n" | |
59 | "\t kernel_addr 400000\n" | |
60 | "\t var1\n" | |
61 | "\t var2 The quick brown fox jumps over the " | |
62 | "lazy dog\n" | |
63 | "\n" | |
64 | "A variable without value will be dropped. It is possible\n" | |
65 | "to put any number of spaces between the fields, but any\n" | |
66 | "space inside the value is treated as part of the value " | |
67 | "itself.\n\n" | |
68 | ); | |
69 | } | |
70 | ||
e4a223f0 | 71 | int main(int argc, char *argv[]) |
6aff3115 WD |
72 | { |
73 | char *p; | |
74 | char *cmdname = *argv; | |
bd7b26f8 SB |
75 | char *script_file = NULL; |
76 | int c; | |
e4a223f0 JH |
77 | const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; |
78 | int lockfd = -1; | |
79 | int retval = EXIT_SUCCESS; | |
80 | ||
7a546db2 | 81 | lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
e4a223f0 JH |
82 | if (-1 == lockfd) { |
83 | fprintf(stderr, "Error opening lock file %s\n", lockname); | |
84 | return EXIT_FAILURE; | |
85 | } | |
86 | ||
87 | if (-1 == flock(lockfd, LOCK_EX)) { | |
88 | fprintf(stderr, "Error locking file %s\n", lockname); | |
89 | close(lockfd); | |
90 | return EXIT_FAILURE; | |
91 | } | |
6aff3115 WD |
92 | |
93 | if ((p = strrchr (cmdname, '/')) != NULL) { | |
94 | cmdname = p + 1; | |
95 | } | |
96 | ||
122bc088 | 97 | while ((c = getopt_long (argc, argv, "ns:h", |
bd7b26f8 SB |
98 | long_options, NULL)) != EOF) { |
99 | switch (c) { | |
122bc088 DH |
100 | case 'n': |
101 | /* handled in fw_printenv */ | |
102 | break; | |
bd7b26f8 SB |
103 | case 's': |
104 | script_file = optarg; | |
105 | break; | |
106 | case 'h': | |
107 | usage(); | |
e4a223f0 | 108 | goto exit; |
29ccd7c3 DH |
109 | default: /* '?' */ |
110 | fprintf(stderr, "Try `%s --help' for more information." | |
111 | "\n", cmdname); | |
e4a223f0 JH |
112 | retval = EXIT_FAILURE; |
113 | goto exit; | |
bd7b26f8 SB |
114 | } |
115 | } | |
116 | ||
6aff3115 | 117 | if (strcmp(cmdname, CMD_PRINTENV) == 0) { |
e4a223f0 JH |
118 | if (fw_printenv(argc, argv) != 0) |
119 | retval = EXIT_FAILURE; | |
6aff3115 | 120 | } else if (strcmp(cmdname, CMD_SETENV) == 0) { |
bd7b26f8 SB |
121 | if (!script_file) { |
122 | if (fw_setenv(argc, argv) != 0) | |
e4a223f0 | 123 | retval = EXIT_FAILURE; |
bd7b26f8 SB |
124 | } else { |
125 | if (fw_parse_script(script_file) != 0) | |
e4a223f0 | 126 | retval = EXIT_FAILURE; |
bd7b26f8 | 127 | } |
e4a223f0 JH |
128 | } else { |
129 | fprintf(stderr, | |
130 | "Identity crisis - may be called as `" CMD_PRINTENV | |
131 | "' or as `" CMD_SETENV "' but not as `%s'\n", | |
132 | cmdname); | |
133 | retval = EXIT_FAILURE; | |
6aff3115 WD |
134 | } |
135 | ||
e4a223f0 JH |
136 | exit: |
137 | flock(lockfd, LOCK_UN); | |
138 | close(lockfd); | |
139 | return retval; | |
6aff3115 | 140 | } |