Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / scripts / bareboxenv.c
blob5c7f10e6b9eec6118310b0691aab8f8c88c288aa
1 /*
2 * bareboxenv.c - generate or read a barebox environment archive
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdint.h>
29 #include <limits.h>
30 #include <errno.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <getopt.h>
35 #include <libgen.h>
37 #define debug(...)
39 void *xmalloc(size_t size)
41 void *p = NULL;
43 if (!(p = malloc(size))) {
44 printf("ERROR: out of memory\n");
45 exit(1);
48 return p;
51 void *xzalloc(size_t size)
53 void *p = xmalloc(size);
54 memset(p, 0, size);
55 return p;
58 /* Find out if the last character of a string matches the one given.
59 * Don't underrun the buffer if the string length is 0.
61 char* last_char_is(const char *s, int c)
63 if (s && *s) {
64 size_t sz = strlen(s) - 1;
65 s += sz;
66 if ( (unsigned char)*s == c)
67 return (char*)s;
69 return NULL;
72 enum {
73 ACTION_RECURSE = (1 << 0),
74 /* ACTION_FOLLOWLINKS = (1 << 1), - unused */
75 ACTION_DEPTHFIRST = (1 << 2),
76 /*ACTION_REVERSE = (1 << 3), - unused */
79 int recursive_action(const char *fileName, unsigned flags,
80 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
81 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData, int depth),
82 void* userData, const unsigned depth);
84 #define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
86 /* concatenate path and file name to new allocation buffer,
87 * not adding '/' if path name already has '/'
89 char *concat_path_file(const char *path, const char *filename)
91 char *lc, *str;
93 if (!path)
94 path = "";
95 lc = last_char_is(path, '/');
96 while (*filename == '/')
97 filename++;
99 str = xmalloc(strlen(path) + (lc==0 ? 1 : 0) + strlen(filename) + 1);
100 sprintf(str, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
102 return str;
106 * This function make special for recursive actions with usage
107 * concat_path_file(path, filename)
108 * and skipping "." and ".." directory entries
111 char *concat_subpath_file(const char *path, const char *f)
113 if (f && DOT_OR_DOTDOT(f))
114 return NULL;
115 return concat_path_file(path, f);
118 #include "../lib/recursive_action.c"
119 #include "../include/envfs.h"
120 #include "../lib/crc32.c"
121 #include "../lib/make_directory.c"
122 #include "../include/environment.h"
123 #include "../common/environment.c"
125 void usage(char *prgname)
127 printf( "Usage : %s [OPTION] DIRECTORY FILE\n"
128 "Load an barebox environment sector into a directory or\n"
129 "save a directory into an barebox environment sector\n"
130 "\n"
131 "options:\n"
132 " -s save (directory -> environment sector)\n"
133 " -l load (environment sector -> directory)\n"
134 " -p <size> pad output file to given size\n",
135 prgname);
138 int main(int argc, char *argv[])
140 int opt;
141 int save = 0, load = 0, pad = 0, fd;
142 char *filename = NULL, *dirname = NULL;
144 while((opt = getopt(argc, argv, "slp:")) != -1) {
145 switch (opt) {
146 case 's':
147 save = 1;
148 break;
149 case 'l':
150 load = 1;
151 break;
152 case 'p':
153 pad = strtoul(optarg, NULL, 0);
154 break;
158 if (optind + 1 >= argc) {
159 usage(argv[0]);
160 exit(1);
163 dirname = argv[optind];
164 filename = argv[optind + 1];
166 if ((!load && !save) || (load && save) || !filename || !dirname) {
167 usage(argv[0]);
168 exit(1);
171 if (save) {
172 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0644);
173 if (fd < 0) {
174 perror("open");
175 exit(1);
177 close(fd);
180 if (save && pad) {
181 if (truncate(filename, pad)) {
182 perror("truncate");
183 exit(1);
187 if (load) {
188 printf("loading env from file %s to %s\n", filename, dirname);
189 envfs_load(filename, dirname);
191 if (save) {
192 printf("saving contents of %s to file %s\n", dirname, filename);
193 envfs_save(filename, dirname);
195 exit(0);