chfs_mkfs.c revision 1.6 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.2 christos memset(buf, 0xFF, opts->pagesize);
128 1.1 ttoth
129 1.1 ttoth ebhdr.ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
130 1.1 ttoth ebhdr.ec_hdr.erase_cnt = htole32(1);
131 1.2 christos ebhdr.ec_hdr.crc_ec = htole32(crc32(0,
132 1.2 christos (uint8_t *)&ebhdr.ec_hdr + 8, 4));
133 1.1 ttoth
134 1.2 christos memcpy(buf, &ebhdr.ec_hdr, CHFS_EB_EC_HDR_SIZE);
135 1.1 ttoth
136 1.2 christos buf_write(fsopts, buf, opts->pagesize);
137 1.1 ttoth
138 1.2 christos memset(buf, 0xFF, opts->pagesize);
139 1.1 ttoth
140 1.2 christos if (opts->mediatype == TYPE_NAND) {
141 1.1 ttoth ebhdr.u.nand_hdr.lid = htole32(lebnumber++);
142 1.1 ttoth ebhdr.u.nand_hdr.serial = htole64(++(max_serial));
143 1.1 ttoth ebhdr.u.nand_hdr.crc = htole32(crc32(0,
144 1.2 christos (uint8_t *)&ebhdr.u.nand_hdr + 4,
145 1.2 christos CHFS_EB_HDR_NAND_SIZE - 4));
146 1.2 christos memcpy(buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
147 1.1 ttoth } else {
148 1.1 ttoth ebhdr.u.nor_hdr.lid = htole32(lebnumber++);
149 1.2 christos ebhdr.u.nor_hdr.crc = htole32(crc32(0,
150 1.2 christos (uint8_t *)&ebhdr.u.nor_hdr + 4,
151 1.1 ttoth CHFS_EB_HDR_NOR_SIZE - 4));
152 1.2 christos memcpy(buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
153 1.1 ttoth }
154 1.1 ttoth
155 1.2 christos buf_write(fsopts, buf, opts->pagesize);
156 1.2 christos free(buf);
157 1.1 ttoth }
158 1.1 ttoth
159 1.1 ttoth void
160 1.1 ttoth write_vnode(fsinfo_t *fsopts, fsnode *node)
161 1.1 ttoth {
162 1.1 ttoth struct chfs_flash_vnode fvnode;
163 1.1 ttoth memset(&fvnode, 0, sizeof(fvnode));
164 1.1 ttoth
165 1.1 ttoth fvnode.magic = htole16(CHFS_FS_MAGIC_BITMASK);
166 1.1 ttoth fvnode.type = htole16(CHFS_NODETYPE_VNODE);
167 1.1 ttoth fvnode.length = htole32(CHFS_PAD(sizeof(fvnode)));
168 1.1 ttoth fvnode.hdr_crc = htole32(crc32(0, (uint8_t *)&fvnode,
169 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
170 1.1 ttoth fvnode.vno = htole64(node->inode->ino);
171 1.1 ttoth fvnode.version = htole64(version++);
172 1.1 ttoth fvnode.mode = htole32(node->inode->st.st_mode);
173 1.1 ttoth fvnode.dn_size = htole32(node->inode->st.st_size);
174 1.1 ttoth fvnode.atime = htole32(node->inode->st.st_atime);
175 1.1 ttoth fvnode.ctime = htole32(node->inode->st.st_ctime);
176 1.1 ttoth fvnode.mtime = htole32(node->inode->st.st_mtime);
177 1.1 ttoth fvnode.gid = htole32(node->inode->st.st_uid);
178 1.1 ttoth fvnode.uid = htole32(node->inode->st.st_gid);
179 1.1 ttoth fvnode.node_crc = htole32(crc32(0, (uint8_t *)&fvnode,
180 1.1 ttoth sizeof(fvnode) - 4));
181 1.1 ttoth
182 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fvnode));
183 1.1 ttoth buf_write(fsopts, &fvnode, sizeof(fvnode));
184 1.1 ttoth padword(fsopts);
185 1.1 ttoth }
186 1.1 ttoth
187 1.1 ttoth void
188 1.1 ttoth write_dirent(fsinfo_t *fsopts, fsnode *node)
189 1.1 ttoth {
190 1.1 ttoth struct chfs_flash_dirent_node fdirent;
191 1.6 christos char *name;
192 1.1 ttoth
193 1.6 christos name = emalloc(strlen(node->name));
194 1.1 ttoth memcpy(name, node->name, strlen(node->name));
195 1.1 ttoth
196 1.6 christos memset(&fdirent, 0, sizeof(fdirent));
197 1.1 ttoth fdirent.magic = htole16(CHFS_FS_MAGIC_BITMASK);
198 1.1 ttoth fdirent.type = htole16(CHFS_NODETYPE_DIRENT);
199 1.1 ttoth fdirent.length = htole32(CHFS_PAD(sizeof(fdirent) + strlen(name)));
200 1.1 ttoth fdirent.hdr_crc = htole32(crc32(0, (uint8_t *)&fdirent,
201 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
202 1.1 ttoth fdirent.vno = htole64(node->inode->ino);
203 1.1 ttoth if (node->parent != NULL) {
204 1.1 ttoth fdirent.pvno = htole64(node->parent->inode->ino);
205 1.1 ttoth } else {
206 1.1 ttoth fdirent.pvno = htole64(node->inode->ino);
207 1.1 ttoth }
208 1.1 ttoth
209 1.1 ttoth fdirent.version = htole64(version++);
210 1.5 dogcow fdirent.mctime = 0;
211 1.1 ttoth fdirent.nsize = htole32(strlen(name));
212 1.1 ttoth fdirent.dtype = htole32(IFTOCHT(node->type & S_IFMT));
213 1.1 ttoth fdirent.name_crc = htole32(crc32(0, (uint8_t *)name, fdirent.nsize));
214 1.1 ttoth fdirent.node_crc = htole32(crc32(0, (uint8_t *)&fdirent,
215 1.1 ttoth sizeof(fdirent) - 4));
216 1.1 ttoth
217 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdirent) + fdirent.nsize);
218 1.1 ttoth buf_write(fsopts, &fdirent, sizeof(fdirent));
219 1.1 ttoth buf_write(fsopts, name, fdirent.nsize);
220 1.1 ttoth padword(fsopts);
221 1.1 ttoth }
222 1.1 ttoth
223 1.1 ttoth void
224 1.1 ttoth write_file(fsinfo_t *fsopts, fsnode *node, const char *dir)
225 1.1 ttoth {
226 1.1 ttoth int fd;
227 1.1 ttoth ssize_t len;
228 1.1 ttoth char *name = node->name;
229 1.2 christos chfs_opt_t *opts;
230 1.1 ttoth unsigned char *buf;
231 1.1 ttoth uint32_t fileofs = 0;
232 1.1 ttoth
233 1.2 christos opts = fsopts->fs_specific;
234 1.6 christos buf = emalloc(opts->pagesize);
235 1.1 ttoth if (node->type == S_IFREG || node->type == S_IFSOCK) {
236 1.1 ttoth char *longname;
237 1.2 christos if (asprintf(&longname, "%s/%s", dir, name) == 1)
238 1.2 christos goto out;
239 1.1 ttoth
240 1.1 ttoth fd = open(longname, O_RDONLY, 0444);
241 1.2 christos if (fd == -1)
242 1.2 christos err(EXIT_FAILURE, "Cannot open `%s'", longname);
243 1.1 ttoth
244 1.2 christos while ((len = read(fd, buf, opts->pagesize))) {
245 1.1 ttoth if (len < 0) {
246 1.1 ttoth warn("ERROR while reading %s", longname);
247 1.2 christos free(longname);
248 1.1 ttoth free(buf);
249 1.1 ttoth close(fd);
250 1.1 ttoth return;
251 1.1 ttoth }
252 1.1 ttoth
253 1.1 ttoth write_data(fsopts, node, buf, len, fileofs);
254 1.1 ttoth fileofs += len;
255 1.1 ttoth }
256 1.2 christos free(longname);
257 1.1 ttoth close(fd);
258 1.1 ttoth } else if (node->type == S_IFLNK) {
259 1.1 ttoth len = strlen(node->symlink);
260 1.1 ttoth memcpy(buf, node->symlink, len);
261 1.1 ttoth write_data(fsopts, node, buf, len, 0);
262 1.1 ttoth } else if (node->type == S_IFCHR || node->type == S_IFBLK ||
263 1.1 ttoth node->type == S_IFIFO) {
264 1.1 ttoth len = sizeof(dev_t);
265 1.1 ttoth memcpy(buf, &(node->inode->st.st_rdev), len);
266 1.1 ttoth write_data(fsopts, node, buf, len, 0);
267 1.1 ttoth }
268 1.1 ttoth
269 1.1 ttoth free(buf);
270 1.2 christos return;
271 1.2 christos out:
272 1.2 christos err(EXIT_FAILURE, "Memory allocation failed");
273 1.1 ttoth }
274 1.1 ttoth
275 1.1 ttoth void
276 1.1 ttoth write_data(fsinfo_t *fsopts, fsnode *node, unsigned char *buf, size_t len,
277 1.1 ttoth uint32_t ofs)
278 1.1 ttoth {
279 1.1 ttoth struct chfs_flash_data_node fdata;
280 1.2 christos
281 1.1 ttoth memset(&fdata, 0, sizeof(fdata));
282 1.1 ttoth if (len == 0) {
283 1.1 ttoth return;
284 1.1 ttoth }
285 1.1 ttoth
286 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdata) + len);
287 1.1 ttoth
288 1.1 ttoth fdata.magic = htole16(CHFS_FS_MAGIC_BITMASK);
289 1.1 ttoth fdata.type = htole16(CHFS_NODETYPE_DATA);
290 1.1 ttoth fdata.length = htole32(CHFS_PAD(sizeof(fdata) + len));
291 1.1 ttoth fdata.hdr_crc = htole32(crc32(0, (uint8_t *)&fdata,
292 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
293 1.1 ttoth fdata.vno = htole64(node->inode->ino);
294 1.1 ttoth fdata.data_length = htole32(len);
295 1.1 ttoth fdata.offset = htole32(ofs);
296 1.1 ttoth fdata.data_crc = htole32(crc32(0, (uint8_t *)buf, len));
297 1.2 christos fdata.node_crc = htole32(crc32(0,
298 1.2 christos (uint8_t *)&fdata, sizeof(fdata) - 4));
299 1.1 ttoth
300 1.1 ttoth buf_write(fsopts, &fdata, sizeof(fdata));
301 1.1 ttoth buf_write(fsopts, buf, len);
302 1.1 ttoth padword(fsopts);
303 1.1 ttoth }
304