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