summaryrefslogtreecommitdiffstats
path: root/commands/environment.c
blob: 2f211d1b0d7e1ae51ce2856fee0f1f0955ac3f78 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * environment.c - simple archive implementation
 *
 * 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
 */

#ifdef __U_BOOT__
#include <common.h>
#include <command.h>
#include <driver.h>
#include <malloc.h>
#include <errno.h>
#include <fs.h>
#include <fcntl.h>
#include <linux/stat.h>
#include <envfs.h>
#include <xfuncs.h>
#include <libbb.h>
#include <libgen.h>
#endif

struct action_data {
	int fd;
	const char *base;
};

static int file_save_action(const char *filename, struct stat *statbuf,
				void *userdata, int depth)
{
	int isize;
	struct action_data *data = userdata;
	struct envfs_inode *inode = NULL;
	int fd;
	int namelen = strlen(filename) + 1 - strlen(data->base);
	int namelen_full = (namelen + 3) & ~3;

	isize = namelen_full + ((statbuf->st_size + 3) & ~3) + sizeof(struct envfs_inode);
	inode = xzalloc(isize);

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		perror("open");
		goto out;
	}

	if (read(fd, inode->data + namelen_full, statbuf->st_size) < statbuf->st_size) {
		perror("read");
		goto out;
	}

	close(fd);
	inode->magic = ENVFS_INODE_MAGIC;
	inode->namelen = namelen;
	inode->size  = namelen_full + statbuf->st_size;

	strcpy(inode->data, filename + strlen(data->base));

	/* FIXME: calc crc */

	if (write(data->fd, inode, isize) < isize) {
		perror("write");
		goto out;
	}
out:
	free(inode);
	return 1;
}

int envfs_save(char *filename, char *dirname)
{
	struct envfs_super super;
	int envfd;
	struct envfs_inode inode;
	struct action_data data;

	envfd = open(filename, O_WRONLY | O_CREAT);
	if (envfd < 0) {
		perror("open");
		return -1;
	}

	super.magic = ENVFS_MAGIC;

	write(envfd, &super, sizeof(struct envfs_super));

	data.fd = envfd;
	data.base = dirname;

	recursive_action(dirname, ACTION_RECURSE, file_save_action,
			NULL, &data, 0);

	memset(&inode, 0, sizeof(struct envfs_inode));
	inode.magic = ENVFS_END_MAGIC;
	if (write(envfd, &inode, sizeof(struct envfs_inode)) < sizeof(struct envfs_inode)) {
		perror("write");
		goto out;
	}
out:
	close(envfd);
	return 0;
}

#ifdef __U_BOOT__
int do_saveenv(cmd_tbl_t *cmdtp, int argc, char *argv[])
{
	int ret, fd;
	char *filename, *dirname;

	printf("saving environment\n");
	if (argc < 3)
		dirname = "/env";
	else
		dirname = argv[2];
	if (argc < 2)
		filename = "/dev/env0";
	else
		filename = argv[1];

	fd = open(filename, O_WRONLY | O_CREAT);
	if (fd < 0) {
		printf("could not open %s: %s", filename, errno_str());
		return 1;
	}

	ret = protect(fd, ~0, 0, 0);

	/* ENOSYS is no error here, many devices do not need it */
	if (ret && errno != -ENOSYS) {
		printf("could not unprotect %s: %s\n", filename, errno_str());
		close(fd);
		return 1;
	}

	ret = erase(fd, ~0, 0);

	/* ENOSYS is no error here, many devices do not need it */
	if (ret && errno != -ENOSYS) {
		printf("could not erase %s: %s\n", filename, errno_str());
		close(fd);
		return 1;
	}

	ret = envfs_save(filename, dirname);
	if (ret) {
		printf("saveenv failed\n");
		goto out;
	}

	ret = protect(fd, ~0, 0, 1);

	/* ENOSYS is no error here, many devices do not need it */
	if (ret && errno != -ENOSYS) {
		printf("could not protect %s: %s\n", filename, errno_str());
		close(fd);
		return 1;
	}

	ret = 0;
out:
	close(fd);
	return ret;
}

static __maybe_unused char cmd_saveenv_help[] =
"Usage: saveenv [DIRECTORY] [ENVFS]\n"
"Save the files in <directory> to the persistent storage device <envfs>.\n"
"<envfs> is normally a block in flash, but could be any other file.\n"
"If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n"
"Note that envfs can only handle files. Directories are skipped silently.\n";

U_BOOT_CMD_START(saveenv)
	.maxargs	= 3,
	.cmd		= do_saveenv,
	.usage		= "save environment to persistent storage",
	U_BOOT_CMD_HELP(cmd_saveenv_help)
U_BOOT_CMD_END
#endif /* __U_BOOT__ */

int mkdirp(const char *dir)
{
	char *s = strdup(dir);
	char *path = s;
	char c;

	do {
		c = 0;

		/* Bypass leading non-'/'s and then subsequent '/'s. */
		while (*s) {
			if (*s == '/') {
				do {
					++s;
				} while (*s == '/');
				c = *s;		/* Save the current char */
				*s = 0;		/* and replace it with nul. */
				break;
			}
			++s;
		}

		if (mkdir(path, 0777) < 0) {

			/* If we failed for any other reason than the directory
			 * already exists, output a diagnostic and return -1.*/
#ifdef __U_BOOT__
			if (errno != -EEXIST)
#else
			if (errno != EEXIST)
#endif
				break;
		}
		if (!c)
			goto out;

		/* Remove any inserted nul from the path (recursive mode). */
		*s = c;

	} while (1);

out:
	free(path);
	return errno;
}

int envfs_load(char *filename, char *dir)
{
	int malloc_size = 0;
	struct envfs_super super;
	void *buf = NULL;
	int envfd;
	struct envfs_inode inode;
	int fd, ret = 0;
	char *str, *tmp;
	int namelen_full;

	envfd = open(filename, O_RDONLY);
	if (envfd < 0) {
		perror("open");
		return -1;
	}

	ret = read(envfd, &super, sizeof(struct envfs_super));
	if ( ret < sizeof(struct envfs_super)) {
		perror("read");
		ret = errno;
		goto out;
	}

	if (super.magic != ENVFS_MAGIC) {
		printf("envfs: wrong magic on %s\n", filename);
		ret = -EIO;
		goto out;
	}

	while (1) {
		ret = read(envfd, &inode, sizeof(struct envfs_inode));
		if (ret < sizeof(struct envfs_inode)) {
			perror("read");
			goto out;
		}
		if (inode.magic == ENVFS_END_MAGIC) {
			ret = 0;
			break;
		}
		if (inode.magic != ENVFS_INODE_MAGIC) {
			printf("envfs: wrong magic on %s\n", filename);
			ret = -EIO;
			goto out;
		}
		if (inode.size > malloc_size) {
			if (buf)
				free(buf);
			buf = xmalloc(inode.size);
			malloc_size = inode.size;
		}

		ret = read(envfd, buf, inode.size);
		if (ret < inode.size) {
			perror("read");
			goto out;
		}

		str = concat_path_file(dir, buf);
		tmp = strdup(str);
		mkdirp(dirname(tmp));
		free(tmp);

		fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
		free(str);
		if (fd < 0) {
			perror("open");
			ret = fd;
			goto out;
		}

		namelen_full = ((inode.namelen + 3) & ~3);
		ret = write(fd, buf + namelen_full, inode.size - namelen_full);
		if (ret < inode.size - namelen_full) {
			perror("write");
			close(fd);
			goto out;
		}
		close(fd);

		if (inode.size & 0x3) {
			ret = read(envfd, buf, 4 - (inode.size & 0x3));
			if (ret < 4 - (inode.size & 0x3)) {
				perror("read");
				ret = errno;
				goto out;
			}
		}
	}

out:
	close(envfd);
	if (buf)
		free(buf);
	return ret;
}

#ifdef __U_BOOT__
int do_loadenv(cmd_tbl_t *cmdtp, int argc, char *argv[])
{
	char *filename, *dirname;

	if (argc < 3)
		dirname = "/env";
	else
		dirname = argv[2];
	if (argc < 2)
		filename = "/dev/env0";
	else
		filename = argv[1];
	printf("loading environment from %s\n", filename);
	return envfs_load(filename, dirname);
}

static __maybe_unused char cmd_loadenv_help[] =
"Usage: loadenv [ENVFS] [DIRECTORY]\n"
"Load the persistent storage contained in <envfs> to the directory\n"
"<directory>.\n"
"If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n"
"Note that envfs can only handle files. Directories are skipped silently.\n";

U_BOOT_CMD_START(loadenv)
	.maxargs	= 3,
	.cmd		= do_loadenv,
	.usage		= "load environment from persistent storage",
	U_BOOT_CMD_HELP(cmd_loadenv_help)
U_BOOT_CMD_END
#endif /* __U_BOOT__ */