arm start.c: Make runtime function address calculation tolerant for more compilers
[barebox-mini2440.git] / commands / saveenv.c
blob42ea58f4dacdc82246b3408c25dcf35bb85befcc
1 /*
2 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4 * See file CREDITS for list of people who contributed to this
5 * project.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /**
22 * @file
23 * @brief saveenv: Make the environment persistent
26 #include <common.h>
27 #include <command.h>
28 #include <errno.h>
29 #include <fs.h>
30 #include <fcntl.h>
31 #include <environment.h>
33 static int do_saveenv(struct command *cmdtp, int argc, char *argv[])
35 int ret, fd;
36 char *filename, *dirname;
38 printf("saving environment\n");
39 if (argc < 3)
40 dirname = "/env";
41 else
42 dirname = argv[2];
43 if (argc < 2)
44 filename = "/dev/env0";
45 else
46 filename = argv[1];
48 fd = open(filename, O_WRONLY | O_CREAT);
49 if (fd < 0) {
50 printf("could not open %s: %s\n", filename, errno_str());
51 return 1;
54 ret = protect(fd, ~0, 0, 0);
56 /* ENOSYS is no error here, many devices do not need it */
57 if (ret && errno != -ENOSYS) {
58 printf("could not unprotect %s: %s\n", filename, errno_str());
59 close(fd);
60 return 1;
63 ret = erase(fd, ~0, 0);
65 /* ENOSYS is no error here, many devices do not need it */
66 if (ret && errno != -ENOSYS) {
67 printf("could not erase %s: %s\n", filename, errno_str());
68 close(fd);
69 return 1;
72 close(fd);
74 ret = envfs_save(filename, dirname);
75 if (ret) {
76 printf("saveenv failed\n");
77 goto out;
80 fd = open(filename, O_WRONLY | O_CREAT);
82 ret = protect(fd, ~0, 0, 1);
84 /* ENOSYS is no error here, many devices do not need it */
85 if (ret && errno != -ENOSYS) {
86 printf("could not protect %s: %s\n", filename, errno_str());
87 close(fd);
88 return 1;
91 ret = 0;
92 out:
93 close(fd);
94 return ret;
97 static const __maybe_unused char cmd_saveenv_help[] =
98 "Usage: saveenv [<envfs>] [<directory>]\n"
99 "Save the files in <directory> to the persistent storage device <envfs>.\n"
100 "<envfs> is normally a block in flash, but could be any other file.\n"
101 "If ommitted <directory> defaults to /env and <envfs> defaults to /dev/env0.\n"
102 "Note that envfs can only handle files. Directories are skipped silently.\n";
104 BAREBOX_CMD_START(saveenv)
105 .cmd = do_saveenv,
106 .usage = "save environment to persistent storage",
107 BAREBOX_CMD_HELP(cmd_saveenv_help)
108 BAREBOX_CMD_END
111 * @page saveenv_command saveenv
113 * Usage: saveenv [\<envfs>] [\<directory>]
115 * Save the files in \<directory> to the persistent storage device \<envfs>.
116 * \<envfs> is normally a block in flash, but could be any other file.
118 * If ommitted \<directory> defaults to \c /env and \<envfs> defaults to
119 * \c /dev/env0.
121 * @note envfs can only handle files. Directories are skipped silently.