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