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