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