chfs_mkfs.c revision 1.3 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.1 ttoth
46 1.1 ttoth #include "makefs.h"
47 1.1 ttoth #include "chfs_makefs.h"
48 1.1 ttoth
49 1.1 ttoth #include "media.h"
50 1.1 ttoth #include "ebh.h"
51 1.1 ttoth
52 1.1 ttoth #include "chfs/chfs_mkfs.h"
53 1.1 ttoth
54 1.1 ttoth static uint32_t img_ofs = 0;
55 1.1 ttoth static uint64_t version = 0;
56 1.1 ttoth static uint64_t max_serial = 0;
57 1.1 ttoth static int lebnumber = 0;
58 1.1 ttoth
59 1.1 ttoth static const unsigned char ffbuf[16] = {
60 1.1 ttoth 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 1.1 ttoth 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
62 1.1 ttoth };
63 1.1 ttoth
64 1.1 ttoth static void
65 1.1 ttoth buf_write(fsinfo_t *fsopts, const void *buf, size_t len)
66 1.1 ttoth {
67 1.1 ttoth ssize_t retval;
68 1.1 ttoth const char *charbuf = buf;
69 1.1 ttoth
70 1.1 ttoth while (len > 0) {
71 1.1 ttoth retval = write(fsopts->fd, charbuf, len);
72 1.1 ttoth
73 1.1 ttoth if (retval == -1) {
74 1.1 ttoth err(EXIT_FAILURE, "ERROR while writing");
75 1.1 ttoth }
76 1.1 ttoth
77 1.1 ttoth len -= retval;
78 1.1 ttoth charbuf += retval;
79 1.1 ttoth img_ofs += retval;
80 1.1 ttoth }
81 1.1 ttoth }
82 1.1 ttoth
83 1.1 ttoth void
84 1.1 ttoth padblock(fsinfo_t *fsopts)
85 1.1 ttoth {
86 1.1 ttoth while (img_ofs % chfs_opts.eraseblock) {
87 1.1 ttoth buf_write(fsopts, ffbuf, MIN(sizeof(ffbuf),
88 1.1 ttoth chfs_opts.eraseblock - (img_ofs % chfs_opts.eraseblock)));
89 1.1 ttoth }
90 1.1 ttoth }
91 1.1 ttoth
92 1.1 ttoth static void
93 1.1 ttoth padword(fsinfo_t *fsopts)
94 1.1 ttoth {
95 1.1 ttoth if (img_ofs % 4) {
96 1.1 ttoth buf_write(fsopts, ffbuf, 4 - img_ofs % 4);
97 1.1 ttoth }
98 1.1 ttoth }
99 1.1 ttoth
100 1.1 ttoth static void
101 1.1 ttoth pad_block_if_less_than(fsinfo_t *fsopts, int req)
102 1.1 ttoth {
103 1.3 christos if ((img_ofs % chfs_opts.eraseblock) + req >
104 1.3 christos (uint32_t)chfs_opts.eraseblock) {
105 1.1 ttoth padblock(fsopts);
106 1.1 ttoth write_eb_header(fsopts);
107 1.1 ttoth }
108 1.1 ttoth }
109 1.1 ttoth
110 1.1 ttoth void
111 1.1 ttoth write_eb_header(fsinfo_t *fsopts)
112 1.1 ttoth {
113 1.2 christos chfs_opt_t *opts;
114 1.1 ttoth struct chfs_eb_hdr ebhdr;
115 1.2 christos char *buf;
116 1.1 ttoth
117 1.2 christos opts = fsopts->fs_specific;
118 1.1 ttoth
119 1.2 christos #define MINSIZE MAX(MAX(CHFS_EB_EC_HDR_SIZE, CHFS_EB_HDR_NOR_SIZE), \
120 1.2 christos CHFS_EB_HDR_NAND_SIZE)
121 1.3 christos if ((uint32_t)opts->pagesize < MINSIZE)
122 1.2 christos errx(EXIT_FAILURE, "pagesize cannot be less than %zu", MINSIZE);
123 1.2 christos if ((buf = malloc(opts->pagesize)) == NULL)
124 1.2 christos err(EXIT_FAILURE, "Memory allocation failed");
125 1.1 ttoth
126 1.2 christos memset(buf, 0xFF, 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.1 ttoth
135 1.2 christos buf_write(fsopts, buf, opts->pagesize);
136 1.1 ttoth
137 1.2 christos memset(buf, 0xFF, opts->pagesize);
138 1.1 ttoth
139 1.2 christos if (opts->mediatype == TYPE_NAND) {
140 1.1 ttoth ebhdr.u.nand_hdr.lid = htole32(lebnumber++);
141 1.1 ttoth ebhdr.u.nand_hdr.serial = htole64(++(max_serial));
142 1.1 ttoth ebhdr.u.nand_hdr.crc = htole32(crc32(0,
143 1.2 christos (uint8_t *)&ebhdr.u.nand_hdr + 4,
144 1.2 christos CHFS_EB_HDR_NAND_SIZE - 4));
145 1.2 christos memcpy(buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
146 1.1 ttoth } else {
147 1.1 ttoth ebhdr.u.nor_hdr.lid = htole32(lebnumber++);
148 1.2 christos ebhdr.u.nor_hdr.crc = htole32(crc32(0,
149 1.2 christos (uint8_t *)&ebhdr.u.nor_hdr + 4,
150 1.1 ttoth CHFS_EB_HDR_NOR_SIZE - 4));
151 1.2 christos memcpy(buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
152 1.1 ttoth }
153 1.1 ttoth
154 1.2 christos buf_write(fsopts, buf, opts->pagesize);
155 1.2 christos free(buf);
156 1.1 ttoth }
157 1.1 ttoth
158 1.1 ttoth void
159 1.1 ttoth write_vnode(fsinfo_t *fsopts, fsnode *node)
160 1.1 ttoth {
161 1.1 ttoth struct chfs_flash_vnode fvnode;
162 1.1 ttoth memset(&fvnode, 0, sizeof(fvnode));
163 1.1 ttoth
164 1.1 ttoth fvnode.magic = htole16(CHFS_FS_MAGIC_BITMASK);
165 1.1 ttoth fvnode.type = htole16(CHFS_NODETYPE_VNODE);
166 1.1 ttoth fvnode.length = htole32(CHFS_PAD(sizeof(fvnode)));
167 1.1 ttoth fvnode.hdr_crc = htole32(crc32(0, (uint8_t *)&fvnode,
168 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
169 1.1 ttoth fvnode.vno = htole64(node->inode->ino);
170 1.1 ttoth fvnode.version = htole64(version++);
171 1.1 ttoth fvnode.mode = htole32(node->inode->st.st_mode);
172 1.1 ttoth fvnode.dn_size = htole32(node->inode->st.st_size);
173 1.1 ttoth fvnode.atime = htole32(node->inode->st.st_atime);
174 1.1 ttoth fvnode.ctime = htole32(node->inode->st.st_ctime);
175 1.1 ttoth fvnode.mtime = htole32(node->inode->st.st_mtime);
176 1.1 ttoth fvnode.gid = htole32(node->inode->st.st_uid);
177 1.1 ttoth fvnode.uid = htole32(node->inode->st.st_gid);
178 1.1 ttoth fvnode.node_crc = htole32(crc32(0, (uint8_t *)&fvnode,
179 1.1 ttoth sizeof(fvnode) - 4));
180 1.1 ttoth
181 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fvnode));
182 1.1 ttoth buf_write(fsopts, &fvnode, sizeof(fvnode));
183 1.1 ttoth padword(fsopts);
184 1.1 ttoth }
185 1.1 ttoth
186 1.1 ttoth void
187 1.1 ttoth write_dirent(fsinfo_t *fsopts, fsnode *node)
188 1.1 ttoth {
189 1.1 ttoth struct chfs_flash_dirent_node fdirent;
190 1.1 ttoth char *name = malloc(sizeof(char) * strlen(node->name));
191 1.1 ttoth
192 1.1 ttoth if (name == NULL) {
193 1.1 ttoth err(EXIT_FAILURE, "ERROR memory allocation failed");
194 1.1 ttoth }
195 1.1 ttoth
196 1.1 ttoth memset(&fdirent, 0, sizeof(fdirent));
197 1.1 ttoth memcpy(name, node->name, strlen(node->name));
198 1.1 ttoth
199 1.1 ttoth fdirent.magic = htole16(CHFS_FS_MAGIC_BITMASK);
200 1.1 ttoth fdirent.type = htole16(CHFS_NODETYPE_DIRENT);
201 1.1 ttoth fdirent.length = htole32(CHFS_PAD(sizeof(fdirent) + strlen(name)));
202 1.1 ttoth fdirent.hdr_crc = htole32(crc32(0, (uint8_t *)&fdirent,
203 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
204 1.1 ttoth fdirent.vno = htole64(node->inode->ino);
205 1.1 ttoth if (node->parent != NULL) {
206 1.1 ttoth fdirent.pvno = htole64(node->parent->inode->ino);
207 1.1 ttoth } else {
208 1.1 ttoth fdirent.pvno = htole64(node->inode->ino);
209 1.1 ttoth }
210 1.1 ttoth
211 1.1 ttoth fdirent.version = htole64(version++);
212 1.1 ttoth fdirent.mctime = htole32(node->inode->st.st_mtimensec);
213 1.1 ttoth fdirent.nsize = htole32(strlen(name));
214 1.1 ttoth fdirent.dtype = htole32(IFTOCHT(node->type & S_IFMT));
215 1.1 ttoth fdirent.name_crc = htole32(crc32(0, (uint8_t *)name, fdirent.nsize));
216 1.1 ttoth fdirent.node_crc = htole32(crc32(0, (uint8_t *)&fdirent,
217 1.1 ttoth sizeof(fdirent) - 4));
218 1.1 ttoth
219 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdirent) + fdirent.nsize);
220 1.1 ttoth buf_write(fsopts, &fdirent, sizeof(fdirent));
221 1.1 ttoth buf_write(fsopts, name, fdirent.nsize);
222 1.1 ttoth padword(fsopts);
223 1.1 ttoth }
224 1.1 ttoth
225 1.1 ttoth void
226 1.1 ttoth write_file(fsinfo_t *fsopts, fsnode *node, const char *dir)
227 1.1 ttoth {
228 1.1 ttoth int fd;
229 1.1 ttoth ssize_t len;
230 1.1 ttoth char *name = node->name;
231 1.2 christos chfs_opt_t *opts;
232 1.1 ttoth unsigned char *buf;
233 1.1 ttoth uint32_t fileofs = 0;
234 1.1 ttoth
235 1.2 christos opts = fsopts->fs_specific;
236 1.2 christos buf = malloc(opts->pagesize);
237 1.1 ttoth
238 1.2 christos if (buf == NULL)
239 1.2 christos goto out;
240 1.1 ttoth
241 1.1 ttoth if (node->type == S_IFREG || node->type == S_IFSOCK) {
242 1.1 ttoth char *longname;
243 1.2 christos if (asprintf(&longname, "%s/%s", dir, name) == 1)
244 1.2 christos goto out;
245 1.1 ttoth
246 1.1 ttoth fd = open(longname, O_RDONLY, 0444);
247 1.2 christos if (fd == -1)
248 1.2 christos err(EXIT_FAILURE, "Cannot open `%s'", longname);
249 1.1 ttoth
250 1.2 christos while ((len = read(fd, buf, opts->pagesize))) {
251 1.1 ttoth if (len < 0) {
252 1.1 ttoth warn("ERROR while reading %s", longname);
253 1.2 christos free(longname);
254 1.1 ttoth free(buf);
255 1.1 ttoth close(fd);
256 1.1 ttoth return;
257 1.1 ttoth }
258 1.1 ttoth
259 1.1 ttoth write_data(fsopts, node, buf, len, fileofs);
260 1.1 ttoth fileofs += len;
261 1.1 ttoth }
262 1.2 christos free(longname);
263 1.1 ttoth close(fd);
264 1.1 ttoth } else if (node->type == S_IFLNK) {
265 1.1 ttoth len = strlen(node->symlink);
266 1.1 ttoth memcpy(buf, node->symlink, len);
267 1.1 ttoth write_data(fsopts, node, buf, len, 0);
268 1.1 ttoth } else if (node->type == S_IFCHR || node->type == S_IFBLK ||
269 1.1 ttoth node->type == S_IFIFO) {
270 1.1 ttoth len = sizeof(dev_t);
271 1.1 ttoth memcpy(buf, &(node->inode->st.st_rdev), len);
272 1.1 ttoth write_data(fsopts, node, buf, len, 0);
273 1.1 ttoth }
274 1.1 ttoth
275 1.1 ttoth free(buf);
276 1.2 christos return;
277 1.2 christos out:
278 1.2 christos err(EXIT_FAILURE, "Memory allocation failed");
279 1.1 ttoth }
280 1.1 ttoth
281 1.1 ttoth void
282 1.1 ttoth write_data(fsinfo_t *fsopts, fsnode *node, unsigned char *buf, size_t len,
283 1.1 ttoth uint32_t ofs)
284 1.1 ttoth {
285 1.1 ttoth struct chfs_flash_data_node fdata;
286 1.2 christos
287 1.1 ttoth memset(&fdata, 0, sizeof(fdata));
288 1.1 ttoth if (len == 0) {
289 1.1 ttoth return;
290 1.1 ttoth }
291 1.1 ttoth
292 1.1 ttoth pad_block_if_less_than(fsopts, sizeof(fdata) + len);
293 1.1 ttoth
294 1.1 ttoth fdata.magic = htole16(CHFS_FS_MAGIC_BITMASK);
295 1.1 ttoth fdata.type = htole16(CHFS_NODETYPE_DATA);
296 1.1 ttoth fdata.length = htole32(CHFS_PAD(sizeof(fdata) + len));
297 1.1 ttoth fdata.hdr_crc = htole32(crc32(0, (uint8_t *)&fdata,
298 1.1 ttoth CHFS_NODE_HDR_SIZE - 4));
299 1.1 ttoth fdata.vno = htole64(node->inode->ino);
300 1.1 ttoth fdata.data_length = htole32(len);
301 1.1 ttoth fdata.offset = htole32(ofs);
302 1.1 ttoth fdata.data_crc = htole32(crc32(0, (uint8_t *)buf, len));
303 1.2 christos fdata.node_crc = htole32(crc32(0,
304 1.2 christos (uint8_t *)&fdata, sizeof(fdata) - 4));
305 1.1 ttoth
306 1.1 ttoth buf_write(fsopts, &fdata, sizeof(fdata));
307 1.1 ttoth buf_write(fsopts, buf, len);
308 1.1 ttoth padword(fsopts);
309 1.1 ttoth }
310