chfs_mkfs.c revision 1.7 1 1.1 ttoth /*-
2 1.1 ttoth * Copyright (c) 2012 Department of Software Engineering,
3 1.1 ttoth * University of Szeged, Hungary
4 1.1 ttoth * Copyright (c) 2012 Tamas Toth <ttoth (at) inf.u-szeged.hu>
5 1.1 ttoth * All rights reserved.
6 1.1 ttoth *
7 1.1 ttoth * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ttoth * by the Department of Software Engineering, University of Szeged, Hungary
9 1.1 ttoth *
10 1.1 ttoth * Redistribution and use in source and binary forms, with or without
11 1.1 ttoth * modification, are permitted provided that the following conditions
12 1.1 ttoth * are met:
13 1.1 ttoth * 1. Redistributions of source code must retain the above copyright
14 1.1 ttoth * notice, this list of conditions and the following disclaimer.
15 1.1 ttoth * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ttoth * notice, this list of conditions and the following disclaimer in the
17 1.1 ttoth * documentation and/or other materials provided with the distribution.
18 1.1 ttoth *
19 1.1 ttoth * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 ttoth * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 ttoth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 ttoth * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 ttoth * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.1 ttoth * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.1 ttoth * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.1 ttoth * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.1 ttoth * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 ttoth * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 ttoth * SUCH DAMAGE.
30 1.1 ttoth */
31 1.1 ttoth
32 1.1 ttoth #if HAVE_NBTOOL_CONFIG_H
33 1.1 ttoth #include "nbtool_config.h"
34 1.1 ttoth #endif
35 1.1 ttoth
36 1.1 ttoth #include <sys/param.h>
37 1.1 ttoth #include <sys/stat.h>
38 1.1 ttoth
39 1.1 ttoth #include <assert.h>
40 1.1 ttoth #include <fcntl.h>
41 1.1 ttoth #include <stdio.h>
42 1.1 ttoth #include <stdlib.h>
43 1.1 ttoth #include <string.h>
44 1.1 ttoth #include <zlib.h>
45 1.6 christos #include <util.h>
46 1.1 ttoth
47 1.1 ttoth #include "makefs.h"
48 1.1 ttoth #include "chfs_makefs.h"
49 1.1 ttoth
50 1.1 ttoth #include "media.h"
51 1.1 ttoth #include "ebh.h"
52 1.1 ttoth
53 1.1 ttoth #include "chfs/chfs_mkfs.h"
54 1.1 ttoth
55 1.1 ttoth static uint32_t img_ofs = 0;
56 1.1 ttoth static uint64_t version = 0;
57 1.1 ttoth static uint64_t max_serial = 0;
58 1.1 ttoth static int lebnumber = 0;
59 1.1 ttoth
60 1.1 ttoth static const unsigned char ffbuf[16] = {
61 1.1 ttoth 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 1.1 ttoth 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
63 1.1 ttoth };
64 1.1 ttoth
65 1.1 ttoth static void
66 1.1 ttoth buf_write(fsinfo_t *fsopts, const void *buf, size_t len)
67 1.1 ttoth {
68 1.1 ttoth ssize_t retval;
69 1.1 ttoth const char *charbuf = buf;
70 1.1 ttoth
71 1.1 ttoth while (len > 0) {
72 1.1 ttoth retval = write(fsopts->fd, charbuf, len);
73 1.1 ttoth
74 1.1 ttoth if (retval == -1) {
75 1.1 ttoth err(EXIT_FAILURE, "ERROR while writing");
76 1.1 ttoth }
77 1.1 ttoth
78 1.1 ttoth len -= retval;
79 1.1 ttoth charbuf += retval;
80 1.1 ttoth img_ofs += retval;
81 1.1 ttoth }
82 1.1 ttoth }
83 1.1 ttoth
84 1.1 ttoth void
85 1.1 ttoth padblock(fsinfo_t *fsopts)
86 1.1 ttoth {
87 1.6 christos chfs_opt_t *chfs_opts = fsopts->fs_specific;
88 1.6 christos while (img_ofs % chfs_opts->eraseblock) {
89 1.1 ttoth buf_write(fsopts, ffbuf, MIN(sizeof(ffbuf),
90 1.6 christos chfs_opts->eraseblock - (img_ofs % chfs_opts->eraseblock)));
91 1.1 ttoth }
92 1.1 ttoth }
93 1.1 ttoth
94 1.1 ttoth static void
95 1.1 ttoth padword(fsinfo_t *fsopts)
96 1.1 ttoth {
97 1.1 ttoth if (img_ofs % 4) {
98 1.1 ttoth buf_write(fsopts, ffbuf, 4 - img_ofs % 4);
99 1.1 ttoth }
100 1.1 ttoth }
101 1.1 ttoth
102 1.1 ttoth static void
103 1.1 ttoth pad_block_if_less_than(fsinfo_t *fsopts, int req)
104 1.1 ttoth {
105 1.6 christos chfs_opt_t *chfs_opts = fsopts->fs_specific;
106 1.6 christos if ((img_ofs % chfs_opts->eraseblock) + req >
107 1.6 christos (uint32_t)chfs_opts->eraseblock) {
108 1.1 ttoth padblock(fsopts);
109 1.1 ttoth write_eb_header(fsopts);
110 1.1 ttoth }
111 1.1 ttoth }
112 1.1 ttoth
113 1.1 ttoth void
114 1.1 ttoth write_eb_header(fsinfo_t *fsopts)
115 1.1 ttoth {
116 1.2 christos chfs_opt_t *opts;
117 1.1 ttoth struct chfs_eb_hdr ebhdr;
118 1.2 christos char *buf;
119 1.1 ttoth
120 1.2 christos opts = fsopts->fs_specific;
121 1.1 ttoth
122 1.2 christos #define MINSIZE MAX(MAX(CHFS_EB_EC_HDR_SIZE, CHFS_EB_HDR_NOR_SIZE), \
123 1.2 christos CHFS_EB_HDR_NAND_SIZE)
124 1.3 christos if ((uint32_t)opts->pagesize < MINSIZE)
125 1.2 christos errx(EXIT_FAILURE, "pagesize cannot be less than %zu", MINSIZE);
126 1.6 christos buf = emalloc(opts->pagesize);
127 1.1 ttoth
128 1.1 ttoth ebhdr.ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
129 1.1 ttoth ebhdr.ec_hdr.erase_cnt = htole32(1);
130 1.2 christos ebhdr.ec_hdr.crc_ec = htole32(crc32(0,
131 1.2 christos (uint8_t *)&ebhdr.ec_hdr + 8, 4));
132 1.1 ttoth
133 1.2 christos memcpy(buf, &ebhdr.ec_hdr, CHFS_EB_EC_HDR_SIZE);
134 1.7 christos memset(buf + CHFS_EB_EC_HDR_SIZE, 0xFF,
135 1.7 christos opts->pagesize - CHFS_EB_EC_HDR_SIZE);
136 1.1 ttoth
137 1.2 christos buf_write(fsopts, buf, opts->pagesize);
138 1.1 ttoth
139 1.2 christos memset(buf, 0xFF, opts->pagesize);
140 1.1 ttoth
141 1.2 christos if (opts->mediatype == TYPE_NAND) {
142 1.1 ttoth ebhdr.u.nand_hdr.lid = htole32(lebnumber++);
143 1.1 ttoth ebhdr.u.nand_hdr.serial = htole64(++(max_serial));
144 1.1 ttoth ebhdr.u.nand_hdr.crc = htole32(crc32(0,
145 1.2 christos (uint8_t *)&ebhdr.u.nand_hdr + 4,
146 1.2 christos CHFS_EB_HDR_NAND_SIZE - 4));
147 1.2 christos memcpy(buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
148 1.1 ttoth } else {
149 1.1 ttoth ebhdr.u.nor_hdr.lid = htole32(lebnumber++);
150 1.2 christos ebhdr.u.nor_hdr.crc = htole32(crc32(0,
151 1.2 christos (uint8_t *)&ebhdr.u.nor_hdr + 4,
152 1.1 ttoth CHFS_EB_HDR_NOR_SIZE - 4));
153 1.2 christos memcpy(buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
154 1.1 ttoth }
155 1.1 ttoth
156 1.2 christos buf_write(fsopts, buf, opts->pagesize);
157 1.2 christos free(buf);
158 1.1 ttoth }
159 1.1 ttoth
160 1.1 ttoth void
161 1.1 ttoth write_vnode(fsinfo_t *fsopts, fsnode *node)
162 1.1 ttoth {
163 1.1 ttoth struct chfs_flash_vnode fvnode;
164 1.1 ttoth memset(&fvnode, 0, sizeof(fvnode));
165 1.1 ttoth
166 1.1 ttoth fvnode.magic = htole16(CHFS_FS_MAGIC_BITMASK);
167 1.1 ttoth fvnode.type = htole16(CHFS_NODETYPE_VNODE);
168 1.1 ttoth fvnode.length = htole32(CHFS_PAD(sizeof(fvnode)));
169 1.1 ttoth fvnode.hdr_crc = htole32(crc32(0, (uint8_t *)&fvnode,
170 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
171 1.1 ttoth fvnode.vno = htole64(node->inode->ino);
172 1.1 ttoth fvnode.version = htole64(version++);
173 1.1 ttoth fvnode.mode = htole32(node->inode->st.st_mode);
174 1.1 ttoth fvnode.dn_size = htole32(node->inode->st.st_size);
175 1.1 ttoth fvnode.atime = htole32(node->inode->st.st_atime);
176 1.1 ttoth fvnode.ctime = htole32(node->inode->st.st_ctime);
177 1.1 ttoth fvnode.mtime = htole32(node->inode->st.st_mtime);
178 1.1 ttoth fvnode.gid = htole32(node->inode->st.st_uid);
179 1.1 ttoth fvnode.uid = htole32(node->inode->st.st_gid);
180 1.1 ttoth fvnode.node_crc = htole32(crc32(0, (uint8_t *)&fvnode,
181 1.1 ttoth sizeof(fvnode) - 4));
182 1.1 ttoth
183 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fvnode));
184 1.1 ttoth buf_write(fsopts, &fvnode, sizeof(fvnode));
185 1.1 ttoth padword(fsopts);
186 1.1 ttoth }
187 1.1 ttoth
188 1.1 ttoth void
189 1.1 ttoth write_dirent(fsinfo_t *fsopts, fsnode *node)
190 1.1 ttoth {
191 1.1 ttoth struct chfs_flash_dirent_node fdirent;
192 1.6 christos char *name;
193 1.1 ttoth
194 1.6 christos name = emalloc(strlen(node->name));
195 1.1 ttoth memcpy(name, node->name, strlen(node->name));
196 1.1 ttoth
197 1.6 christos memset(&fdirent, 0, sizeof(fdirent));
198 1.1 ttoth fdirent.magic = htole16(CHFS_FS_MAGIC_BITMASK);
199 1.1 ttoth fdirent.type = htole16(CHFS_NODETYPE_DIRENT);
200 1.1 ttoth fdirent.length = htole32(CHFS_PAD(sizeof(fdirent) + strlen(name)));
201 1.1 ttoth fdirent.hdr_crc = htole32(crc32(0, (uint8_t *)&fdirent,
202 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
203 1.1 ttoth fdirent.vno = htole64(node->inode->ino);
204 1.1 ttoth if (node->parent != NULL) {
205 1.1 ttoth fdirent.pvno = htole64(node->parent->inode->ino);
206 1.1 ttoth } else {
207 1.1 ttoth fdirent.pvno = htole64(node->inode->ino);
208 1.1 ttoth }
209 1.1 ttoth
210 1.1 ttoth fdirent.version = htole64(version++);
211 1.5 dogcow fdirent.mctime = 0;
212 1.1 ttoth fdirent.nsize = htole32(strlen(name));
213 1.1 ttoth fdirent.dtype = htole32(IFTOCHT(node->type & S_IFMT));
214 1.1 ttoth fdirent.name_crc = htole32(crc32(0, (uint8_t *)name, fdirent.nsize));
215 1.1 ttoth fdirent.node_crc = htole32(crc32(0, (uint8_t *)&fdirent,
216 1.1 ttoth sizeof(fdirent) - 4));
217 1.1 ttoth
218 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdirent) + fdirent.nsize);
219 1.1 ttoth buf_write(fsopts, &fdirent, sizeof(fdirent));
220 1.1 ttoth buf_write(fsopts, name, fdirent.nsize);
221 1.1 ttoth padword(fsopts);
222 1.1 ttoth }
223 1.1 ttoth
224 1.1 ttoth void
225 1.1 ttoth write_file(fsinfo_t *fsopts, fsnode *node, const char *dir)
226 1.1 ttoth {
227 1.1 ttoth int fd;
228 1.1 ttoth ssize_t len;
229 1.1 ttoth char *name = node->name;
230 1.2 christos chfs_opt_t *opts;
231 1.1 ttoth unsigned char *buf;
232 1.1 ttoth uint32_t fileofs = 0;
233 1.1 ttoth
234 1.2 christos opts = fsopts->fs_specific;
235 1.6 christos buf = emalloc(opts->pagesize);
236 1.1 ttoth if (node->type == S_IFREG || node->type == S_IFSOCK) {
237 1.1 ttoth char *longname;
238 1.2 christos if (asprintf(&longname, "%s/%s", dir, name) == 1)
239 1.2 christos goto out;
240 1.1 ttoth
241 1.1 ttoth fd = open(longname, O_RDONLY, 0444);
242 1.2 christos if (fd == -1)
243 1.2 christos err(EXIT_FAILURE, "Cannot open `%s'", longname);
244 1.1 ttoth
245 1.2 christos while ((len = read(fd, buf, opts->pagesize))) {
246 1.1 ttoth if (len < 0) {
247 1.1 ttoth warn("ERROR while reading %s", longname);
248 1.2 christos free(longname);
249 1.1 ttoth free(buf);
250 1.1 ttoth close(fd);
251 1.1 ttoth return;
252 1.1 ttoth }
253 1.1 ttoth
254 1.1 ttoth write_data(fsopts, node, buf, len, fileofs);
255 1.1 ttoth fileofs += len;
256 1.1 ttoth }
257 1.2 christos free(longname);
258 1.1 ttoth close(fd);
259 1.1 ttoth } else if (node->type == S_IFLNK) {
260 1.1 ttoth len = strlen(node->symlink);
261 1.1 ttoth memcpy(buf, node->symlink, len);
262 1.1 ttoth write_data(fsopts, node, buf, len, 0);
263 1.1 ttoth } else if (node->type == S_IFCHR || node->type == S_IFBLK ||
264 1.1 ttoth node->type == S_IFIFO) {
265 1.1 ttoth len = sizeof(dev_t);
266 1.1 ttoth memcpy(buf, &(node->inode->st.st_rdev), len);
267 1.1 ttoth write_data(fsopts, node, buf, len, 0);
268 1.1 ttoth }
269 1.1 ttoth
270 1.1 ttoth free(buf);
271 1.2 christos return;
272 1.2 christos out:
273 1.2 christos err(EXIT_FAILURE, "Memory allocation failed");
274 1.1 ttoth }
275 1.1 ttoth
276 1.1 ttoth void
277 1.1 ttoth write_data(fsinfo_t *fsopts, fsnode *node, unsigned char *buf, size_t len,
278 1.1 ttoth uint32_t ofs)
279 1.1 ttoth {
280 1.1 ttoth struct chfs_flash_data_node fdata;
281 1.2 christos
282 1.1 ttoth memset(&fdata, 0, sizeof(fdata));
283 1.1 ttoth if (len == 0) {
284 1.1 ttoth return;
285 1.1 ttoth }
286 1.1 ttoth
287 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdata) + len);
288 1.1 ttoth
289 1.1 ttoth fdata.magic = htole16(CHFS_FS_MAGIC_BITMASK);
290 1.1 ttoth fdata.type = htole16(CHFS_NODETYPE_DATA);
291 1.1 ttoth fdata.length = htole32(CHFS_PAD(sizeof(fdata) + len));
292 1.1 ttoth fdata.hdr_crc = htole32(crc32(0, (uint8_t *)&fdata,
293 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
294 1.1 ttoth fdata.vno = htole64(node->inode->ino);
295 1.1 ttoth fdata.data_length = htole32(len);
296 1.1 ttoth fdata.offset = htole32(ofs);
297 1.1 ttoth fdata.data_crc = htole32(crc32(0, (uint8_t *)buf, len));
298 1.2 christos fdata.node_crc = htole32(crc32(0,
299 1.2 christos (uint8_t *)&fdata, sizeof(fdata) - 4));
300 1.1 ttoth
301 1.1 ttoth buf_write(fsopts, &fdata, sizeof(fdata));
302 1.1 ttoth buf_write(fsopts, buf, len);
303 1.1 ttoth padword(fsopts);
304 1.1 ttoth }
305