2007-07-05 16:02:19 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Managing environment and environment variables
|
|
|
|
*/
|
|
|
|
|
2007-07-05 16:01:53 +00:00
|
|
|
#ifdef __U_BOOT__
|
2007-07-05 16:01:44 +00:00
|
|
|
#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>
|
2007-09-25 10:58:52 +00:00
|
|
|
#include <libbb.h>
|
|
|
|
#include <libgen.h>
|
2007-10-18 16:32:02 +00:00
|
|
|
#else
|
|
|
|
# define errno_str(x) ("void")
|
2007-07-05 16:01:52 +00:00
|
|
|
#endif
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-25 10:58:52 +00:00
|
|
|
struct action_data {
|
|
|
|
int fd;
|
|
|
|
const char *base;
|
2007-09-27 10:04:17 +00:00
|
|
|
void *writep;
|
2007-09-25 10:58:52 +00:00
|
|
|
};
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
#define PAD4(x) ((x + 3) & ~3)
|
|
|
|
|
|
|
|
static int file_size_action(const char *filename, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
|
|
|
{
|
|
|
|
struct action_data *data = userdata;
|
|
|
|
|
|
|
|
data->writep += sizeof(struct envfs_inode);
|
|
|
|
data->writep += PAD4(strlen(filename) + 1 - strlen(data->base));
|
|
|
|
data->writep += PAD4(statbuf->st_size);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-25 10:58:52 +00:00
|
|
|
static int file_save_action(const char *filename, struct stat *statbuf,
|
|
|
|
void *userdata, int depth)
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
2007-09-25 10:58:52 +00:00
|
|
|
struct action_data *data = userdata;
|
2007-09-27 10:04:17 +00:00
|
|
|
struct envfs_inode *inode;
|
2007-07-05 16:01:44 +00:00
|
|
|
int fd;
|
2007-09-25 10:58:52 +00:00
|
|
|
int namelen = strlen(filename) + 1 - strlen(data->base);
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
debug("handling file %s size %ld namelen %d\n", filename + strlen(data->base), statbuf->st_size, namelen);
|
|
|
|
|
|
|
|
inode = (struct envfs_inode*)data->writep;
|
|
|
|
inode->magic = ENVFS_INODE_MAGIC;
|
|
|
|
inode->namelen = namelen;
|
|
|
|
inode->size = statbuf->st_size;
|
|
|
|
data->writep += sizeof(struct envfs_inode);
|
|
|
|
|
|
|
|
strcpy(data->writep, filename + strlen(data->base));
|
|
|
|
data->writep += PAD4(namelen);
|
2007-09-25 10:58:52 +00:00
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2007-10-18 16:32:02 +00:00
|
|
|
printf("Open %s %s\n", filename, errno_str());
|
2007-09-25 10:58:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
if (read(fd, data->writep, statbuf->st_size) < statbuf->st_size) {
|
2007-09-25 10:58:52 +00:00
|
|
|
perror("read");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
data->writep += PAD4(statbuf->st_size);
|
2007-09-25 10:58:52 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int envfs_save(char *filename, char *dirname)
|
|
|
|
{
|
2007-09-27 10:04:17 +00:00
|
|
|
struct envfs_super *super;
|
|
|
|
int envfd, size, ret;
|
2007-09-25 10:58:52 +00:00
|
|
|
struct action_data data;
|
2007-09-27 10:04:17 +00:00
|
|
|
void *buf = NULL;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
data.writep = 0;
|
|
|
|
data.base = dirname;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
/* first pass: calculate size */
|
|
|
|
recursive_action(dirname, ACTION_RECURSE, file_size_action,
|
|
|
|
NULL, &data, 0);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
size = (unsigned long)data.writep;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
buf = xzalloc(size + sizeof(struct envfs_super));
|
|
|
|
data.writep = buf + sizeof(struct envfs_super);
|
|
|
|
|
|
|
|
super = (struct envfs_super *)buf;
|
|
|
|
super->magic = ENVFS_MAGIC;
|
|
|
|
super->size = size;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
/* second pass: copy files to buffer */
|
2007-09-25 10:58:52 +00:00
|
|
|
recursive_action(dirname, ACTION_RECURSE, file_save_action,
|
|
|
|
NULL, &data, 0);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
super->crc = crc32(0, buf + sizeof(struct envfs_super), size);
|
|
|
|
super->sb_crc = crc32(0, buf, sizeof(struct envfs_super) - 4);
|
|
|
|
|
|
|
|
envfd = open(filename, O_WRONLY | O_CREAT);
|
|
|
|
if (envfd < 0) {
|
2007-10-18 16:32:02 +00:00
|
|
|
printf("Open %s %s\n", filename, errno_str());
|
2007-09-27 10:04:17 +00:00
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(envfd, buf, size + sizeof(struct envfs_super)) < sizeof(struct envfs_super)) {
|
2007-07-05 16:01:44 +00:00
|
|
|
perror("write");
|
|
|
|
goto out;
|
|
|
|
}
|
2007-09-27 10:04:17 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
close(envfd);
|
2007-09-27 10:04:17 +00:00
|
|
|
out1:
|
|
|
|
free(buf);
|
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:53 +00:00
|
|
|
#ifdef __U_BOOT__
|
2007-09-28 08:07:26 +00:00
|
|
|
static int do_saveenv(cmd_tbl_t *cmdtp, int argc, char *argv[])
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
2007-07-05 16:02:14 +00:00
|
|
|
int ret, fd;
|
2007-07-05 16:01:52 +00:00
|
|
|
char *filename, *dirname;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
|
|
|
printf("saving environment\n");
|
|
|
|
if (argc < 3)
|
2007-07-05 16:01:52 +00:00
|
|
|
dirname = "/env";
|
2007-07-05 16:01:44 +00:00
|
|
|
else
|
2007-07-05 16:01:52 +00:00
|
|
|
dirname = argv[2];
|
2007-07-05 16:01:44 +00:00
|
|
|
if (argc < 2)
|
2007-07-05 16:01:52 +00:00
|
|
|
filename = "/dev/env0";
|
2007-07-05 16:01:44 +00:00
|
|
|
else
|
2007-07-05 16:01:52 +00:00
|
|
|
filename = argv[1];
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-25 10:58:52 +00:00
|
|
|
fd = open(filename, O_WRONLY | O_CREAT);
|
2007-07-05 16:02:14 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
printf("could not open %s: %s", filename, errno_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-12 15:12:16 +00:00
|
|
|
ret = protect(fd, ~0, 0, 0);
|
2007-09-25 10:58:52 +00:00
|
|
|
|
2007-09-12 15:12:16 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:02:14 +00:00
|
|
|
ret = erase(fd, ~0, 0);
|
2007-07-05 19:50:14 +00:00
|
|
|
|
|
|
|
/* ENOSYS is no error here, many devices do not need it */
|
|
|
|
if (ret && errno != -ENOSYS) {
|
2007-07-05 16:02:14 +00:00
|
|
|
printf("could not erase %s: %s\n", filename, errno_str());
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:55 +00:00
|
|
|
ret = envfs_save(filename, dirname);
|
2007-09-25 10:58:52 +00:00
|
|
|
if (ret) {
|
2007-07-05 16:01:55 +00:00
|
|
|
printf("saveenv failed\n");
|
2007-09-25 10:58:52 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2007-07-05 16:02:14 +00:00
|
|
|
|
2007-09-12 15:12:16 +00:00
|
|
|
ret = protect(fd, ~0, 0, 1);
|
2007-09-25 10:58:52 +00:00
|
|
|
|
2007-09-12 15:12:16 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2007-09-25 10:58:52 +00:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2007-07-05 16:02:14 +00:00
|
|
|
close(fd);
|
2007-07-05 16:01:55 +00:00
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:59 +00:00
|
|
|
static __maybe_unused char cmd_saveenv_help[] =
|
2007-10-19 12:56:45 +00:00
|
|
|
"Usage: saveenv [<envfs>] [<directory>]\n"
|
2007-07-05 16:01:55 +00:00
|
|
|
"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";
|
|
|
|
|
2007-07-05 16:01:52 +00:00
|
|
|
U_BOOT_CMD_START(saveenv)
|
|
|
|
.maxargs = 3,
|
|
|
|
.cmd = do_saveenv,
|
2007-07-05 16:01:55 +00:00
|
|
|
.usage = "save environment to persistent storage",
|
|
|
|
U_BOOT_CMD_HELP(cmd_saveenv_help)
|
2007-07-05 16:01:52 +00:00
|
|
|
U_BOOT_CMD_END
|
2007-07-05 16:01:53 +00:00
|
|
|
#endif /* __U_BOOT__ */
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-10-19 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* @page saveenv_command saveenv
|
|
|
|
*
|
2007-11-08 11:01:52 +00:00
|
|
|
* Usage: saveenv [\<envfs>] [\<directory>]
|
2007-10-19 12:56:45 +00:00
|
|
|
*
|
2007-11-08 11:01:52 +00:00
|
|
|
* Save the files in \<directory> to the persistent storage device \<envfs>.
|
|
|
|
* \<envfs> is normally a block in flash, but could be any other file.
|
2007-10-19 12:56:45 +00:00
|
|
|
*
|
2007-11-08 11:01:52 +00:00
|
|
|
* If ommitted \<directory> defaults to \b /env and \<envfs> defaults to
|
|
|
|
* \b /dev/env0.
|
2007-10-19 12:56:45 +00:00
|
|
|
*
|
|
|
|
* @note envfs can only handle files. Directories are skipped silently.
|
|
|
|
*/
|
|
|
|
|
2007-09-25 10:58:52 +00:00
|
|
|
int envfs_load(char *filename, char *dir)
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
|
|
|
struct envfs_super super;
|
2007-09-27 10:04:17 +00:00
|
|
|
void *buf = NULL, *buf_free = NULL;
|
2007-07-05 16:01:44 +00:00
|
|
|
int envfd;
|
2007-09-13 13:24:32 +00:00
|
|
|
int fd, ret = 0;
|
2007-09-25 10:58:52 +00:00
|
|
|
char *str, *tmp;
|
|
|
|
int namelen_full;
|
2007-09-27 10:04:17 +00:00
|
|
|
unsigned long size;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-07-05 16:01:52 +00:00
|
|
|
envfd = open(filename, O_RDONLY);
|
2007-07-05 16:01:44 +00:00
|
|
|
if (envfd < 0) {
|
2007-10-18 16:32:02 +00:00
|
|
|
printf("Open %s %s\n", filename, errno_str());
|
2007-07-05 16:01:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
/* read superblock */
|
2007-09-13 13:24:32 +00:00
|
|
|
ret = read(envfd, &super, sizeof(struct envfs_super));
|
|
|
|
if ( ret < sizeof(struct envfs_super)) {
|
2007-07-05 16:01:44 +00:00
|
|
|
perror("read");
|
2007-09-16 09:16:58 +00:00
|
|
|
ret = errno;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-09-13 13:24:32 +00:00
|
|
|
if (super.magic != ENVFS_MAGIC) {
|
|
|
|
printf("envfs: wrong magic on %s\n", filename);
|
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
if (crc32(0, (unsigned char *)&super, sizeof(struct envfs_super) - 4)
|
|
|
|
!= super.sb_crc) {
|
|
|
|
printf("wrong crc on env superblock\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = xmalloc(super.size);
|
|
|
|
buf_free = buf;
|
|
|
|
ret = read(envfd, buf, super.size);
|
|
|
|
if (ret < super.size) {
|
|
|
|
perror("read");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crc32(0, (unsigned char *)buf, super.size)
|
|
|
|
!= super.crc) {
|
|
|
|
printf("wrong crc on env\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = super.size;
|
|
|
|
|
|
|
|
while (size) {
|
|
|
|
struct envfs_inode *inode;
|
|
|
|
|
|
|
|
inode = (struct envfs_inode *)buf;
|
|
|
|
|
|
|
|
if (inode->magic != ENVFS_INODE_MAGIC) {
|
2007-07-05 16:01:52 +00:00
|
|
|
printf("envfs: wrong magic on %s\n", filename);
|
2007-09-13 13:24:32 +00:00
|
|
|
ret = -EIO;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2007-09-13 13:24:32 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
debug("loading %s size %d namelen %d\n", inode->data, inode->size, inode->namelen);
|
2007-07-05 16:01:44 +00:00
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
str = concat_path_file(dir, inode->data);
|
2007-09-25 10:58:52 +00:00
|
|
|
tmp = strdup(str);
|
2007-09-27 10:04:17 +00:00
|
|
|
make_directory(dirname(tmp));
|
2007-09-25 10:58:52 +00:00
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
fd = open(str, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
|
|
free(str);
|
2007-07-05 16:01:44 +00:00
|
|
|
if (fd < 0) {
|
2007-10-18 16:32:02 +00:00
|
|
|
printf("Open %s\n", errno_str());
|
2007-09-13 13:24:32 +00:00
|
|
|
ret = fd;
|
2007-07-05 16:01:44 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
namelen_full = PAD4(inode->namelen);
|
|
|
|
ret = write(fd, buf + namelen_full + sizeof(struct envfs_inode), inode->size);
|
|
|
|
if (ret < inode->size) {
|
2007-07-05 16:01:44 +00:00
|
|
|
perror("write");
|
|
|
|
close(fd);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
buf += PAD4(inode->namelen) + PAD4(inode->size) + sizeof(struct envfs_inode);
|
|
|
|
size -= PAD4(inode->namelen) + PAD4(inode->size) + sizeof(struct envfs_inode);
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2007-09-27 10:04:17 +00:00
|
|
|
ret = 0;
|
2007-07-05 16:01:44 +00:00
|
|
|
out:
|
|
|
|
close(envfd);
|
2007-09-27 10:04:17 +00:00
|
|
|
if (buf_free)
|
|
|
|
free(buf_free);
|
2007-09-13 13:24:32 +00:00
|
|
|
return ret;
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:53 +00:00
|
|
|
#ifdef __U_BOOT__
|
2007-09-28 08:07:26 +00:00
|
|
|
static int do_loadenv(cmd_tbl_t *cmdtp, int argc, char *argv[])
|
2007-07-05 16:01:44 +00:00
|
|
|
{
|
2007-07-05 16:01:52 +00:00
|
|
|
char *filename, *dirname;
|
2007-07-05 16:01:44 +00:00
|
|
|
|
|
|
|
if (argc < 3)
|
2007-07-05 16:01:52 +00:00
|
|
|
dirname = "/env";
|
2007-07-05 16:01:44 +00:00
|
|
|
else
|
2007-07-05 16:01:52 +00:00
|
|
|
dirname = argv[2];
|
2007-07-05 16:01:44 +00:00
|
|
|
if (argc < 2)
|
2007-07-05 16:01:52 +00:00
|
|
|
filename = "/dev/env0";
|
2007-07-05 16:01:44 +00:00
|
|
|
else
|
2007-07-05 16:01:52 +00:00
|
|
|
filename = argv[1];
|
|
|
|
printf("loading environment from %s\n", filename);
|
|
|
|
return envfs_load(filename, dirname);
|
2007-07-05 16:01:44 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:59 +00:00
|
|
|
static __maybe_unused char cmd_loadenv_help[] =
|
2007-09-13 13:24:32 +00:00
|
|
|
"Usage: loadenv [ENVFS] [DIRECTORY]\n"
|
2007-07-05 16:01:55 +00:00
|
|
|
"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";
|
|
|
|
|
2007-07-05 16:01:52 +00:00
|
|
|
U_BOOT_CMD_START(loadenv)
|
|
|
|
.maxargs = 3,
|
|
|
|
.cmd = do_loadenv,
|
2007-07-05 16:01:55 +00:00
|
|
|
.usage = "load environment from persistent storage",
|
|
|
|
U_BOOT_CMD_HELP(cmd_loadenv_help)
|
2007-07-05 16:01:52 +00:00
|
|
|
U_BOOT_CMD_END
|
2007-07-05 16:01:53 +00:00
|
|
|
#endif /* __U_BOOT__ */
|
2007-10-19 12:56:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @page loadenv_command loadenv
|
|
|
|
*
|
2007-11-08 11:01:52 +00:00
|
|
|
* Usage: loadenv [\<directory>] [\<envfs>]
|
2007-10-19 12:56:45 +00:00
|
|
|
*
|
2007-11-08 11:01:52 +00:00
|
|
|
* Load the persistent storage contained in \<envfs> to the directory \<directory>.
|
2007-10-19 12:56:45 +00:00
|
|
|
*
|
2007-11-08 11:01:52 +00:00
|
|
|
* If ommitted \<directory> defaults to /env and \<envfs> defaults to
|
|
|
|
* \b /dev/env0.
|
2007-10-19 12:56:45 +00:00
|
|
|
*
|
|
|
|
* @note envfs can only handle files. Directories are skipped silently.
|
|
|
|
*/
|