summaryrefslogtreecommitdiffstats
path: root/common/env.c
blob: 4bd0deee46c7eea54f5f49f7bfeb2da6dd140f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * env.c - environment variables storage
 *
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <common.h>
#include <command.h>
#include <driver.h>
#include <malloc.h>
#include <xfuncs.h>
#include <errno.h>

struct variable_d {
        struct variable_d *next;
        char data[0];
};

#define VARIABLE_D_SIZE(name, value) (sizeof(struct variable_d) + strlen(name) + strlen(value) + 2)

static struct variable_d _env_list;
static struct variable_d *env_list = &_env_list;

static char *var_val(struct variable_d *var)
{
        return &var->data[strlen(var->data) + 1];
}

static char *var_name(struct variable_d *var)
{
        return var->data;
}

const char *getenv (const char *name)
{
        struct variable_d *var;

	if (strchr(name, '.')) {
		const char *ret = 0;
		char *devstr = strdup(name);
		char *par = strchr(devstr, '.');
		struct device_d *dev;
		*par = 0;
		dev = get_device_by_id(devstr);
		if (dev) {
				par++;
				ret = dev_get_param(dev, par);
		}
		free(devstr);
		return ret;
	}

        var = env_list->next;

        while (var) {
                if (!strcmp(var_name(var), name))
                        return var_val(var);
		var = var->next;
	}
	return 0;
}

int setenv (const char *name, const char *value)
{
        struct variable_d *var = env_list;
        struct variable_d *newvar = NULL;
	char *par;

	if ((par = strchr(name, '.'))) {
		struct device_d *dev;
		*par++ = 0;
		dev = get_device_by_id(name);
		if (dev) {
			int ret = dev_set_param(dev, par, value);
			if (ret < 0)
				perror("set parameter");
			errno = 0;
		} else {
		    errno = -ENODEV;
		    perror("set parameter");
                }
		return errno;
	}

        if (value) {
                newvar = xzalloc(VARIABLE_D_SIZE(name, value));
                strcpy(&newvar->data[0], name);
                strcpy(&newvar->data[strlen(name) + 1], value);
        }

        while (var->next) {
                if (!strcmp(var->next->data, name)) {
                        if (value) {
                                newvar->next = var->next->next;
                                free(var->next);
                                var->next = newvar;
                                return 0;
                        } else {
                                struct variable_d *tmp;
                                tmp = var->next;
                                var->next = var->next->next;
                                free(var->next);
                                return 0;
                        }
                }
                var = var->next;
        }
        var->next = newvar;

	return 0;
}

int do_printenv (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
        struct variable_d *var = env_list->next;

	if (argc == 2) {
                const char *val = getenv(argv[1]);
                if (val) {
                        printf("%s=%s\n", argv[1], val);
                        return 0;
                }
                printf("## Error: \"%s\" not defined\n", argv[1]);
                return -EINVAL;
        }

        while (var) {
                printf("%s=%s\n", var_name(var), var_val(var));
                var = var->next;
        }

        return 0;
}

U_BOOT_CMD_START(printenv)
	.maxargs	= CONFIG_MAXARGS,
	.cmd		= do_printenv,
	.usage		= "print environment variables",
	U_BOOT_CMD_HELP(
	"\n    - print values of all environment variables\n"
	"printenv name ...\n"
	"    - print value of environment variable 'name'\n")
U_BOOT_CMD_END

#ifdef CONFIG_SIMPLE_PARSER
int do_setenv ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	if (argc < 2) {
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}

	setenv(argv[1], argv[2]);

        return 0;
}

U_BOOT_CMD_START(setenv)
	.maxargs	= CONFIG_MAXARGS,
	.cmd		= do_setenv,
	.usage		= "set environment variables",
	U_BOOT_CMD_HELP(
	"name value ...\n"
	"    - set environment variable 'name' to 'value ...'\n"
	"setenv name\n"
	"    - delete environment variable 'name'\n")
U_BOOT_CMD_END

#endif