1*9f988b79SJean-Baptiste Boric /*-
2*9f988b79SJean-Baptiste Boric * Copyright (c) 2012 Department of Software Engineering,
3*9f988b79SJean-Baptiste Boric * University of Szeged, Hungary
4*9f988b79SJean-Baptiste Boric * Copyright (c) 2012 Tamas Toth <ttoth@inf.u-szeged.hu>
5*9f988b79SJean-Baptiste Boric * All rights reserved.
6*9f988b79SJean-Baptiste Boric *
7*9f988b79SJean-Baptiste Boric * This code is derived from software contributed to The NetBSD Foundation
8*9f988b79SJean-Baptiste Boric * by the Department of Software Engineering, University of Szeged, Hungary
9*9f988b79SJean-Baptiste Boric *
10*9f988b79SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
11*9f988b79SJean-Baptiste Boric * modification, are permitted provided that the following conditions
12*9f988b79SJean-Baptiste Boric * are met:
13*9f988b79SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
14*9f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
15*9f988b79SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
16*9f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
17*9f988b79SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
18*9f988b79SJean-Baptiste Boric *
19*9f988b79SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20*9f988b79SJean-Baptiste Boric * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21*9f988b79SJean-Baptiste Boric * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22*9f988b79SJean-Baptiste Boric * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23*9f988b79SJean-Baptiste Boric * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*9f988b79SJean-Baptiste Boric * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*9f988b79SJean-Baptiste Boric * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*9f988b79SJean-Baptiste Boric * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27*9f988b79SJean-Baptiste Boric * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*9f988b79SJean-Baptiste Boric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*9f988b79SJean-Baptiste Boric * SUCH DAMAGE.
30*9f988b79SJean-Baptiste Boric */
31*9f988b79SJean-Baptiste Boric
32*9f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
33*9f988b79SJean-Baptiste Boric #include "nbtool_config.h"
34*9f988b79SJean-Baptiste Boric #endif
35*9f988b79SJean-Baptiste Boric
36*9f988b79SJean-Baptiste Boric #include <sys/param.h>
37*9f988b79SJean-Baptiste Boric
38*9f988b79SJean-Baptiste Boric #include <assert.h>
39*9f988b79SJean-Baptiste Boric #include <fcntl.h>
40*9f988b79SJean-Baptiste Boric #include <stdio.h>
41*9f988b79SJean-Baptiste Boric #include <stdlib.h>
42*9f988b79SJean-Baptiste Boric #include <string.h>
43*9f988b79SJean-Baptiste Boric #include <unistd.h>
44*9f988b79SJean-Baptiste Boric #include <util.h>
45*9f988b79SJean-Baptiste Boric
46*9f988b79SJean-Baptiste Boric #include "makefs.h"
47*9f988b79SJean-Baptiste Boric #include "chfs_makefs.h"
48*9f988b79SJean-Baptiste Boric
49*9f988b79SJean-Baptiste Boric #include "chfs/chfs_mkfs.h"
50*9f988b79SJean-Baptiste Boric
51*9f988b79SJean-Baptiste Boric static void chfs_validate(const char *, fsnode *, fsinfo_t *);
52*9f988b79SJean-Baptiste Boric static int chfs_create_image(const char *, fsinfo_t *);
53*9f988b79SJean-Baptiste Boric static int chfs_populate_dir(const char *, fsnode *, fsnode *, fsinfo_t *);
54*9f988b79SJean-Baptiste Boric
55*9f988b79SJean-Baptiste Boric
56*9f988b79SJean-Baptiste Boric void
chfs_prep_opts(fsinfo_t * fsopts)57*9f988b79SJean-Baptiste Boric chfs_prep_opts(fsinfo_t *fsopts)
58*9f988b79SJean-Baptiste Boric {
59*9f988b79SJean-Baptiste Boric chfs_opt_t *chfs_opts = ecalloc(1, sizeof(*chfs_opts));
60*9f988b79SJean-Baptiste Boric
61*9f988b79SJean-Baptiste Boric const option_t chfs_options[] = {
62*9f988b79SJean-Baptiste Boric { 'p', "pagesize", &chfs_opts->pagesize, OPT_INT32,
63*9f988b79SJean-Baptiste Boric 1, INT_MAX, "page size" },
64*9f988b79SJean-Baptiste Boric { 'e', "eraseblock", &chfs_opts->eraseblock, OPT_INT32,
65*9f988b79SJean-Baptiste Boric 1, INT_MAX, "eraseblock size" },
66*9f988b79SJean-Baptiste Boric { 'm', "mediatype", &chfs_opts->mediatype, OPT_INT32,
67*9f988b79SJean-Baptiste Boric 0, 1, "type of the media, 0 (nor) or 1 (nand)" },
68*9f988b79SJean-Baptiste Boric { .name = NULL }
69*9f988b79SJean-Baptiste Boric };
70*9f988b79SJean-Baptiste Boric
71*9f988b79SJean-Baptiste Boric chfs_opts->pagesize = -1;
72*9f988b79SJean-Baptiste Boric chfs_opts->eraseblock = -1;
73*9f988b79SJean-Baptiste Boric chfs_opts->mediatype = -1;
74*9f988b79SJean-Baptiste Boric
75*9f988b79SJean-Baptiste Boric fsopts->size = 0;
76*9f988b79SJean-Baptiste Boric fsopts->fs_specific = chfs_opts;
77*9f988b79SJean-Baptiste Boric fsopts->fs_options = copy_opts(chfs_options);
78*9f988b79SJean-Baptiste Boric }
79*9f988b79SJean-Baptiste Boric
80*9f988b79SJean-Baptiste Boric void
chfs_cleanup_opts(fsinfo_t * fsopts)81*9f988b79SJean-Baptiste Boric chfs_cleanup_opts(fsinfo_t *fsopts)
82*9f988b79SJean-Baptiste Boric {
83*9f988b79SJean-Baptiste Boric free(fsopts->fs_specific);
84*9f988b79SJean-Baptiste Boric free(fsopts->fs_options);
85*9f988b79SJean-Baptiste Boric }
86*9f988b79SJean-Baptiste Boric
87*9f988b79SJean-Baptiste Boric int
chfs_parse_opts(const char * option,fsinfo_t * fsopts)88*9f988b79SJean-Baptiste Boric chfs_parse_opts(const char *option, fsinfo_t *fsopts)
89*9f988b79SJean-Baptiste Boric {
90*9f988b79SJean-Baptiste Boric
91*9f988b79SJean-Baptiste Boric assert(option != NULL);
92*9f988b79SJean-Baptiste Boric assert(fsopts != NULL);
93*9f988b79SJean-Baptiste Boric
94*9f988b79SJean-Baptiste Boric return set_option(fsopts->fs_options, option, NULL, 0) != -1;
95*9f988b79SJean-Baptiste Boric }
96*9f988b79SJean-Baptiste Boric
97*9f988b79SJean-Baptiste Boric void
chfs_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)98*9f988b79SJean-Baptiste Boric chfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
99*9f988b79SJean-Baptiste Boric {
100*9f988b79SJean-Baptiste Boric struct timeval start;
101*9f988b79SJean-Baptiste Boric
102*9f988b79SJean-Baptiste Boric assert(image != NULL);
103*9f988b79SJean-Baptiste Boric assert(dir != NULL);
104*9f988b79SJean-Baptiste Boric assert(root != NULL);
105*9f988b79SJean-Baptiste Boric assert(fsopts != NULL);
106*9f988b79SJean-Baptiste Boric
107*9f988b79SJean-Baptiste Boric TIMER_START(start);
108*9f988b79SJean-Baptiste Boric chfs_validate(dir, root, fsopts);
109*9f988b79SJean-Baptiste Boric TIMER_RESULTS(start, "chfs_validate");
110*9f988b79SJean-Baptiste Boric
111*9f988b79SJean-Baptiste Boric printf("Creating `%s'\n", image);
112*9f988b79SJean-Baptiste Boric TIMER_START(start);
113*9f988b79SJean-Baptiste Boric if (chfs_create_image(image, fsopts) == -1) {
114*9f988b79SJean-Baptiste Boric errx(EXIT_FAILURE, "Image file `%s' not created", image);
115*9f988b79SJean-Baptiste Boric }
116*9f988b79SJean-Baptiste Boric TIMER_RESULTS(start, "chfs_create_image");
117*9f988b79SJean-Baptiste Boric
118*9f988b79SJean-Baptiste Boric fsopts->curinode = CHFS_ROOTINO;
119*9f988b79SJean-Baptiste Boric root->inode->ino = CHFS_ROOTINO;
120*9f988b79SJean-Baptiste Boric
121*9f988b79SJean-Baptiste Boric printf("Populating `%s'\n", image);
122*9f988b79SJean-Baptiste Boric TIMER_START(start);
123*9f988b79SJean-Baptiste Boric write_eb_header(fsopts);
124*9f988b79SJean-Baptiste Boric if (!chfs_populate_dir(dir, root, root, fsopts)) {
125*9f988b79SJean-Baptiste Boric errx(EXIT_FAILURE, "Image file `%s' not populated", image);
126*9f988b79SJean-Baptiste Boric }
127*9f988b79SJean-Baptiste Boric TIMER_RESULTS(start, "chfs_populate_dir");
128*9f988b79SJean-Baptiste Boric
129*9f988b79SJean-Baptiste Boric padblock(fsopts);
130*9f988b79SJean-Baptiste Boric
131*9f988b79SJean-Baptiste Boric if (close(fsopts->fd) == -1) {
132*9f988b79SJean-Baptiste Boric err(EXIT_FAILURE, "Closing `%s'", image);
133*9f988b79SJean-Baptiste Boric }
134*9f988b79SJean-Baptiste Boric fsopts->fd = -1;
135*9f988b79SJean-Baptiste Boric
136*9f988b79SJean-Baptiste Boric printf("Image `%s' complete\n", image);
137*9f988b79SJean-Baptiste Boric }
138*9f988b79SJean-Baptiste Boric
139*9f988b79SJean-Baptiste Boric static void
chfs_validate(const char * dir,fsnode * root,fsinfo_t * fsopts)140*9f988b79SJean-Baptiste Boric chfs_validate(const char* dir, fsnode *root, fsinfo_t *fsopts)
141*9f988b79SJean-Baptiste Boric {
142*9f988b79SJean-Baptiste Boric chfs_opt_t *chfs_opts;
143*9f988b79SJean-Baptiste Boric assert(dir != NULL);
144*9f988b79SJean-Baptiste Boric assert(root != NULL);
145*9f988b79SJean-Baptiste Boric assert(fsopts != NULL);
146*9f988b79SJean-Baptiste Boric
147*9f988b79SJean-Baptiste Boric chfs_opts = fsopts->fs_specific;
148*9f988b79SJean-Baptiste Boric
149*9f988b79SJean-Baptiste Boric if (chfs_opts->pagesize == -1) {
150*9f988b79SJean-Baptiste Boric chfs_opts->pagesize = DEFAULT_PAGESIZE;
151*9f988b79SJean-Baptiste Boric }
152*9f988b79SJean-Baptiste Boric if (chfs_opts->eraseblock == -1) {
153*9f988b79SJean-Baptiste Boric chfs_opts->eraseblock = DEFAULT_ERASEBLOCK;
154*9f988b79SJean-Baptiste Boric }
155*9f988b79SJean-Baptiste Boric if (chfs_opts->mediatype == -1) {
156*9f988b79SJean-Baptiste Boric chfs_opts->mediatype = DEFAULT_MEDIATYPE;
157*9f988b79SJean-Baptiste Boric }
158*9f988b79SJean-Baptiste Boric }
159*9f988b79SJean-Baptiste Boric
160*9f988b79SJean-Baptiste Boric static int
chfs_create_image(const char * image,fsinfo_t * fsopts)161*9f988b79SJean-Baptiste Boric chfs_create_image(const char *image, fsinfo_t *fsopts)
162*9f988b79SJean-Baptiste Boric {
163*9f988b79SJean-Baptiste Boric assert(image != NULL);
164*9f988b79SJean-Baptiste Boric assert(fsopts != NULL);
165*9f988b79SJean-Baptiste Boric
166*9f988b79SJean-Baptiste Boric if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
167*9f988b79SJean-Baptiste Boric warn("Can't open `%s' for writing", image);
168*9f988b79SJean-Baptiste Boric return -1;
169*9f988b79SJean-Baptiste Boric }
170*9f988b79SJean-Baptiste Boric
171*9f988b79SJean-Baptiste Boric return fsopts->fd;
172*9f988b79SJean-Baptiste Boric }
173*9f988b79SJean-Baptiste Boric
174*9f988b79SJean-Baptiste Boric static int
chfs_populate_dir(const char * dir,fsnode * root,fsnode * parent,fsinfo_t * fsopts)175*9f988b79SJean-Baptiste Boric chfs_populate_dir(const char *dir, fsnode *root, fsnode *parent,
176*9f988b79SJean-Baptiste Boric fsinfo_t *fsopts)
177*9f988b79SJean-Baptiste Boric {
178*9f988b79SJean-Baptiste Boric fsnode *cur;
179*9f988b79SJean-Baptiste Boric char path[MAXPATHLEN + 1];
180*9f988b79SJean-Baptiste Boric
181*9f988b79SJean-Baptiste Boric assert(dir != NULL);
182*9f988b79SJean-Baptiste Boric assert(root != NULL);
183*9f988b79SJean-Baptiste Boric assert(fsopts != NULL);
184*9f988b79SJean-Baptiste Boric
185*9f988b79SJean-Baptiste Boric for (cur = root->next; cur != NULL; cur = cur->next) {
186*9f988b79SJean-Baptiste Boric if ((cur->inode->flags & FI_ALLOCATED) == 0) {
187*9f988b79SJean-Baptiste Boric cur->inode->flags |= FI_ALLOCATED;
188*9f988b79SJean-Baptiste Boric if (cur != root) {
189*9f988b79SJean-Baptiste Boric fsopts->curinode++;
190*9f988b79SJean-Baptiste Boric cur->inode->ino = fsopts->curinode;
191*9f988b79SJean-Baptiste Boric cur->parent = parent;
192*9f988b79SJean-Baptiste Boric }
193*9f988b79SJean-Baptiste Boric }
194*9f988b79SJean-Baptiste Boric
195*9f988b79SJean-Baptiste Boric if (cur->inode->flags & FI_WRITTEN) {
196*9f988b79SJean-Baptiste Boric continue; // hard link
197*9f988b79SJean-Baptiste Boric }
198*9f988b79SJean-Baptiste Boric cur->inode->flags |= FI_WRITTEN;
199*9f988b79SJean-Baptiste Boric
200*9f988b79SJean-Baptiste Boric write_vnode(fsopts, cur);
201*9f988b79SJean-Baptiste Boric write_dirent(fsopts, cur);
202*9f988b79SJean-Baptiste Boric if (!S_ISDIR(cur->type & S_IFMT)) {
203*9f988b79SJean-Baptiste Boric write_file(fsopts, cur, dir);
204*9f988b79SJean-Baptiste Boric }
205*9f988b79SJean-Baptiste Boric }
206*9f988b79SJean-Baptiste Boric
207*9f988b79SJean-Baptiste Boric for (cur = root; cur != NULL; cur = cur->next) {
208*9f988b79SJean-Baptiste Boric if (cur->child == NULL) {
209*9f988b79SJean-Baptiste Boric continue;
210*9f988b79SJean-Baptiste Boric }
211*9f988b79SJean-Baptiste Boric if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
212*9f988b79SJean-Baptiste Boric cur->name) >= sizeof(path)) {
213*9f988b79SJean-Baptiste Boric errx(EXIT_FAILURE, "Pathname too long");
214*9f988b79SJean-Baptiste Boric }
215*9f988b79SJean-Baptiste Boric if (!chfs_populate_dir(path, cur->child, cur, fsopts)) {
216*9f988b79SJean-Baptiste Boric return 0;
217*9f988b79SJean-Baptiste Boric }
218*9f988b79SJean-Baptiste Boric }
219*9f988b79SJean-Baptiste Boric
220*9f988b79SJean-Baptiste Boric return 1;
221*9f988b79SJean-Baptiste Boric }
222*9f988b79SJean-Baptiste Boric
223