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