Module Name:    src
Committed By:   ttoth
Date:           Thu Apr 19 15:36:06 UTC 2012

Modified Files:
        src/usr.sbin/makefs: Makefile makefs.8 makefs.c makefs.h
Added Files:
        src/usr.sbin/makefs: chfs.c chfs_makefs.h
        src/usr.sbin/makefs/chfs: Makefile.inc chfs_mkfs.c chfs_mkfs.h

Log Message:
added chfs support for makefs


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/usr.sbin/makefs/Makefile
cvs rdiff -u -r0 -r1.1 src/usr.sbin/makefs/chfs.c \
    src/usr.sbin/makefs/chfs_makefs.h
cvs rdiff -u -r1.38 -r1.39 src/usr.sbin/makefs/makefs.8
cvs rdiff -u -r1.31 -r1.32 src/usr.sbin/makefs/makefs.c
cvs rdiff -u -r1.24 -r1.25 src/usr.sbin/makefs/makefs.h
cvs rdiff -u -r0 -r1.1 src/usr.sbin/makefs/chfs/Makefile.inc \
    src/usr.sbin/makefs/chfs/chfs_mkfs.c src/usr.sbin/makefs/chfs/chfs_mkfs.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/makefs/Makefile
diff -u src/usr.sbin/makefs/Makefile:1.29 src/usr.sbin/makefs/Makefile:1.30
--- src/usr.sbin/makefs/Makefile:1.29	Mon Jul 18 22:52:37 2011
+++ src/usr.sbin/makefs/Makefile	Thu Apr 19 15:36:06 2012
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.29 2011/07/18 22:52:37 tron Exp $
+#	$NetBSD: Makefile,v 1.30 2012/04/19 15:36:06 ttoth Exp $
 #
 
 WARNS?=	3	# XXX -Wsign-compare
@@ -6,7 +6,7 @@ WARNS?=	3	# XXX -Wsign-compare
 .include <bsd.own.mk>
 
 PROG=	makefs
-SRCS=	cd9660.c ffs.c v7fs.c \
+SRCS=	cd9660.c chfs.c ffs.c v7fs.c \
 	getid.c \
 	makefs.c misc.c \
 	pack_dev.c \
@@ -21,6 +21,7 @@ CPPFLAGS+=	-I${.CURDIR} -I${MKNODSRC} -I
 .PATH:		${MKNODSRC} ${MTREESRC}
 
 .include "${.CURDIR}/cd9660/Makefile.inc"
+.include "${.CURDIR}/chfs/Makefile.inc"
 .include "${.CURDIR}/ffs/Makefile.inc"
 .include "${.CURDIR}/v7fs/Makefile.inc"
 

Index: src/usr.sbin/makefs/makefs.8
diff -u src/usr.sbin/makefs/makefs.8:1.38 src/usr.sbin/makefs/makefs.8:1.39
--- src/usr.sbin/makefs/makefs.8:1.38	Sat Jan 28 02:35:46 2012
+++ src/usr.sbin/makefs/makefs.8	Thu Apr 19 15:36:06 2012
@@ -1,4 +1,4 @@
-.\"	$NetBSD: makefs.8,v 1.38 2012/01/28 02:35:46 christos Exp $
+.\"	$NetBSD: makefs.8,v 1.39 2012/04/19 15:36:06 ttoth Exp $
 .\"
 .\" Copyright (c) 2001-2003 Wasabi Systems, Inc.
 .\" All rights reserved.
@@ -33,7 +33,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd January 27, 2012
+.Dd April 5, 2012
 .Dt MAKEFS 8
 .Os
 .Sh NAME
@@ -199,6 +199,8 @@ The following file system types are supp
 BSD fast file system (default).
 .It Sy cd9660
 ISO 9660 file system.
+.It Sy chfs
+Chip flash file system.
 .It Sy v7fs
 7th Edition(V7) file system.
 .El
@@ -339,6 +341,22 @@ Use RockRidge extensions (for longer fil
 .It Sy volumeid
 Volume set identifier of the image.
 .El
+.Ss CHFS-specific options
+.Sy chfs
+images have chfs-specific optional parameters that may be provided.
+Each of the options consists of a keyword, an equal sign
+.Pq Ql = ,
+and a value.
+The following keywords are supported:
+.Pp
+.Bl -tag -width optimization -offset indent -compact
+.It Sy pagesize
+Pagesize.
+.It Sy erasesize
+Erase block size of the media.
+.It Sy mediatype
+Type of the media. NOR: 0 or NAND: 1.
+.El
 .Ss V7FS-specific options
 The following keywords are supported:
 .Pp
@@ -370,4 +388,6 @@ utility appeared in
 .An Ram Vedam
 (cd9660 support),
 .An UCHIYAMA Yasushi
-(v7fs support).
+(v7fs support),
+.An Tamas Toth
+(chfs support).

Index: src/usr.sbin/makefs/makefs.c
diff -u src/usr.sbin/makefs/makefs.c:1.31 src/usr.sbin/makefs/makefs.c:1.32
--- src/usr.sbin/makefs/makefs.c:1.31	Sat Jan 28 02:35:46 2012
+++ src/usr.sbin/makefs/makefs.c	Thu Apr 19 15:36:06 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: makefs.c,v 1.31 2012/01/28 02:35:46 christos Exp $	*/
+/*	$NetBSD: makefs.c,v 1.32 2012/04/19 15:36:06 ttoth Exp $	*/
 
 /*
  * Copyright (c) 2001-2003 Wasabi Systems, Inc.
@@ -41,7 +41,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(__lint)
-__RCSID("$NetBSD: makefs.c,v 1.31 2012/01/28 02:35:46 christos Exp $");
+__RCSID("$NetBSD: makefs.c,v 1.32 2012/04/19 15:36:06 ttoth Exp $");
 #endif	/* !__lint */
 
 #include <assert.h>
@@ -73,6 +73,8 @@ static fstype_t fstypes[] = {
 	{ "ffs", ffs_prep_opts,	ffs_parse_opts,	ffs_cleanup_opts, ffs_makefs },
 	{ "cd9660", cd9660_prep_opts, cd9660_parse_opts, cd9660_cleanup_opts,
 	  cd9660_makefs},
+	{ "chfs", chfs_prep_opts, chfs_parse_opts, chfs_cleanup_opts,
+	  chfs_makefs },
 	{ "v7fs", v7fs_prep_opts, v7fs_parse_opts, v7fs_cleanup_opts,
 	  v7fs_makefs },
 	{ .type = NULL	},

Index: src/usr.sbin/makefs/makefs.h
diff -u src/usr.sbin/makefs/makefs.h:1.24 src/usr.sbin/makefs/makefs.h:1.25
--- src/usr.sbin/makefs/makefs.h:1.24	Sat Jan 28 02:35:46 2012
+++ src/usr.sbin/makefs/makefs.h	Thu Apr 19 15:36:06 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: makefs.h,v 1.24 2012/01/28 02:35:46 christos Exp $	*/
+/*	$NetBSD: makefs.h,v 1.25 2012/04/19 15:36:06 ttoth Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -172,6 +172,11 @@ int		cd9660_parse_opts(const char *, fsi
 void		cd9660_cleanup_opts(fsinfo_t *);
 void		cd9660_makefs(const char *, const char *, fsnode *, fsinfo_t *);
 
+void		chfs_prep_opts(fsinfo_t *);
+int		chfs_parse_opts(const char *, fsinfo_t *);
+void		chfs_cleanup_opts(fsinfo_t *);
+void		chfs_makefs(const char *, const char *, fsnode *, fsinfo_t *);
+
 void		v7fs_prep_opts(fsinfo_t *);
 int		v7fs_parse_opts(const char *, fsinfo_t *);
 void		v7fs_cleanup_opts(fsinfo_t *);

Added files:

Index: src/usr.sbin/makefs/chfs.c
diff -u /dev/null src/usr.sbin/makefs/chfs.c:1.1
--- /dev/null	Thu Apr 19 15:36:06 2012
+++ src/usr.sbin/makefs/chfs.c	Thu Apr 19 15:36:06 2012
@@ -0,0 +1,230 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *		      University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <[email protected]>
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by the Department of Software Engineering, University of Szeged, Hungary
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/param.h>
+
+#include <assert.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "makefs.h"
+#include "chfs_makefs.h"
+
+#include "chfs/chfs_mkfs.h"
+
+static void chfs_validate(const char *, fsnode *, fsinfo_t *);
+static int chfs_create_image(const char *, fsinfo_t *);
+static int chfs_populate_dir(const char *, fsnode *, fsnode *, fsinfo_t *);
+
+chfs_opt_t chfs_opts;
+
+void
+chfs_prep_opts(fsinfo_t *fsopts)
+{
+	fsopts->size = 0;
+	fsopts->fs_specific = &chfs_opts;
+
+	chfs_opts.pagesize = -1;
+	chfs_opts.eraseblock = -1;
+	chfs_opts.mediatype = -1;
+}
+
+void
+chfs_cleanup_opts(fsinfo_t *fsopts)
+{
+
+}
+
+int
+chfs_parse_opts(const char *option, fsinfo_t *fsopts)
+{
+	static const option_t chfs_options[] = {
+		{ "pagesize", &chfs_opts.pagesize,	1,	INT_MAX, "page size" },
+		{ "eraseblock", &chfs_opts.eraseblock, 1, INT_MAX, "eraseblock size" },
+		{ "mediatype", &chfs_opts.mediatype, 0, 1, 
+		    "type of the media, 0 (nor) or 1 (nand)" },
+		{ .name = NULL }
+	};
+
+	char *var, *val;
+	int retval;
+
+	assert(option != NULL);
+	assert(fsopts != NULL);
+
+	if ((var = strdup(option)) == NULL) {
+		err(EXIT_FAILURE, "Allocating memory for copy of option string");
+	}
+	retval = 0;
+
+	if ((val = strchr(var, '=')) == NULL) {
+		warnx("Option `%s' doesn't contain a value", var);
+		goto leave_chfs_parse_opts;
+	}
+	*val++ = '\0';
+
+	retval = set_option(chfs_options, var, val);
+	
+leave_chfs_parse_opts:
+	free(var);
+	return retval;
+}
+
+void
+chfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
+{
+	struct timeval	start;
+
+	assert(image != NULL);
+	assert(dir != NULL);
+	assert(root != NULL);
+	assert(fsopts != NULL);	
+
+	TIMER_START(start);
+	chfs_validate(dir, root, fsopts);
+	TIMER_RESULTS(start, "chfs_validate");
+
+	printf("Creating `%s'\n", image);
+	TIMER_START(start);
+	if (chfs_create_image(image, fsopts) == -1) {
+		errx(EXIT_FAILURE, "Image file `%s' not created", image);
+	}
+	TIMER_RESULTS(start, "chfs_create_image");
+	
+	fsopts->curinode = CHFS_ROOTINO;
+	root->inode->ino = CHFS_ROOTINO;
+
+	printf("Populating `%s'\n", image);
+	TIMER_START(start);
+	write_eb_header(fsopts);
+	if (!chfs_populate_dir(dir, root, root, fsopts)) {
+		errx(EXIT_FAILURE, "Image file `%s' not populated", image);
+	}
+	TIMER_RESULTS(start, "chfs_populate_dir");
+
+	padblock(fsopts);
+
+	if (close(fsopts->fd) == -1) {
+		err(EXIT_FAILURE, "Closing `%s'", image);
+	}
+	fsopts->fd = -1;
+
+	printf("Image `%s' complete\n", image);
+}
+
+static void
+chfs_validate(const char* dir, fsnode *root, fsinfo_t *fsopts)
+{
+	assert(dir != NULL);
+	assert(root != NULL);
+	assert(fsopts != NULL);
+
+	if (chfs_opts.pagesize == -1) {
+		chfs_opts.pagesize = DEFAULT_PAGESIZE;
+	}
+	if (chfs_opts.eraseblock == -1) {
+		chfs_opts.eraseblock = DEFAULT_ERASEBLOCK;
+	}
+	if (chfs_opts.mediatype == -1) {
+		chfs_opts.mediatype = DEFAULT_MEDIATYPE;
+	}
+}
+
+static int
+chfs_create_image(const char *image, fsinfo_t *fsopts)
+{
+	assert(image != NULL);
+	assert(fsopts != NULL);
+	
+	if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
+		warn("Can't open `%s' for writing", image);
+		return -1;
+	}
+
+	return fsopts->fd;
+}
+
+static int
+chfs_populate_dir(const char *dir, fsnode *root, fsnode *parent,
+    fsinfo_t *fsopts)
+{
+	fsnode *cur;
+	char path[MAXPATHLEN + 1];
+
+	assert(dir != NULL);
+	assert(root != NULL);
+	assert(fsopts != NULL);	
+	
+	for (cur = root->next; cur != NULL; cur = cur->next) {
+		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
+			cur->inode->flags |= FI_ALLOCATED;
+			if (cur != root) {
+				fsopts->curinode++;
+				cur->inode->ino = fsopts->curinode;
+				cur->parent = parent;
+			}
+		}
+
+		if (cur->inode->flags & FI_WRITTEN) {
+			continue;	// hard link
+		}
+		cur->inode->flags |= FI_WRITTEN;
+
+		write_vnode(fsopts, cur);
+		write_dirent(fsopts, cur);
+		if (!S_ISDIR(cur->type & S_IFMT)) {
+			write_file(fsopts, cur, dir);
+		}
+	}
+	
+	for (cur = root; cur != NULL; cur = cur->next) {
+		if (cur->child == NULL) {
+			continue;
+		}
+		if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
+		    >= sizeof(path)) {
+			errx(EXIT_FAILURE, "Pathname too long");
+		}
+		if (!chfs_populate_dir(path, cur->child, cur, fsopts)) {
+			return 0;
+		}
+	}
+
+	return 1;
+}
+
Index: src/usr.sbin/makefs/chfs_makefs.h
diff -u /dev/null src/usr.sbin/makefs/chfs_makefs.h:1.1
--- /dev/null	Thu Apr 19 15:36:06 2012
+++ src/usr.sbin/makefs/chfs_makefs.h	Thu Apr 19 15:36:06 2012
@@ -0,0 +1,50 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *		      University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <[email protected]>
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by the Department of Software Engineering, University of Szeged, Hungary
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _CHFS_MAKEFS_H
+#define _CHFS_MAKEFS_H
+
+#define TYPE_NOR 0
+#define TYPE_NAND 1
+
+#define DEFAULT_PAGESIZE 2048
+#define DEFAULT_ERASEBLOCK 131072
+#define DEFAULT_MEDIATYPE TYPE_NAND
+
+typedef struct {
+	int pagesize;		/* page size */
+	int eraseblock;		/* eraseblock size */
+	int mediatype;		/* type of the media, 0 (nor) or 1 (nand) */
+} chfs_opt_t;
+
+extern chfs_opt_t chfs_opts;
+
+#endif

Index: src/usr.sbin/makefs/chfs/Makefile.inc
diff -u /dev/null src/usr.sbin/makefs/chfs/Makefile.inc:1.1
--- /dev/null	Thu Apr 19 15:36:06 2012
+++ src/usr.sbin/makefs/chfs/Makefile.inc	Thu Apr 19 15:36:06 2012
@@ -0,0 +1,9 @@
+CHFS=	${NETBSDSRCDIR}/sys/ufs/chfs
+
+.PATH:	${.CURDIR}/chfs ${NETBSDSRCDIR}/sys/ufs/chfs
+
+CPPFLAGS+= -I${CHFS}
+
+SRCS+=	chfs_mkfs.c
+LDADD+= -lz
+DPADD+= ${LIBZ}
Index: src/usr.sbin/makefs/chfs/chfs_mkfs.c
diff -u /dev/null src/usr.sbin/makefs/chfs/chfs_mkfs.c:1.1
--- /dev/null	Thu Apr 19 15:36:06 2012
+++ src/usr.sbin/makefs/chfs/chfs_mkfs.c	Thu Apr 19 15:36:06 2012
@@ -0,0 +1,290 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *		      University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <[email protected]>
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by the Department of Software Engineering, University of Szeged, Hungary
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#if HAVE_NBTOOL_CONFIG_H
+#include "nbtool_config.h"
+#endif
+
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <assert.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <zlib.h>
+
+#include "makefs.h"
+#include "chfs_makefs.h"
+
+#include "media.h"
+#include "ebh.h"
+
+#include "chfs/chfs_mkfs.h"
+
+static uint32_t img_ofs = 0;
+static uint64_t version = 0;
+static uint64_t max_serial = 0;
+static int lebnumber = 0;
+
+static const unsigned char ffbuf[16] = {
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
+};
+
+static void
+buf_write(fsinfo_t *fsopts, const void *buf, size_t len)
+{
+	ssize_t retval;
+	const char *charbuf = buf;
+
+	while (len > 0) {
+		retval = write(fsopts->fd, charbuf, len);
+
+		if (retval == -1) {
+			err(EXIT_FAILURE, "ERROR while writing");
+		}
+
+		len -= retval;
+		charbuf += retval;
+		img_ofs += retval;
+	}
+}
+
+void
+padblock(fsinfo_t *fsopts)
+{
+	while (img_ofs % chfs_opts.eraseblock) {
+		buf_write(fsopts, ffbuf, MIN(sizeof(ffbuf),
+		    chfs_opts.eraseblock - (img_ofs % chfs_opts.eraseblock)));
+	}
+}
+
+static void
+padword(fsinfo_t *fsopts)
+{
+	if (img_ofs % 4) {
+		buf_write(fsopts, ffbuf, 4 - img_ofs % 4);
+	}
+}
+
+static void
+pad_block_if_less_than(fsinfo_t *fsopts, int req)
+{
+	if ((img_ofs % chfs_opts.eraseblock) + req > chfs_opts.eraseblock) {
+		padblock(fsopts);
+		write_eb_header(fsopts);
+	}
+}
+
+void
+write_eb_header(fsinfo_t *fsopts)
+{
+	chfs_opt_t *chfs_opts;
+	struct chfs_eb_hdr ebhdr;
+
+	chfs_opts = fsopts->fs_specific;
+
+	char buf[chfs_opts->pagesize];
+
+	memset(buf, 0xFF, sizeof(buf));
+
+	ebhdr.ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
+	ebhdr.ec_hdr.erase_cnt = htole32(1);
+	ebhdr.ec_hdr.crc_ec = htole32(crc32(0, (uint8_t *)&ebhdr.ec_hdr + 8, 4));
+
+	memcpy(&buf, &ebhdr.ec_hdr, CHFS_EB_EC_HDR_SIZE);
+
+	buf_write(fsopts, &buf, chfs_opts->pagesize);
+
+	memset(buf, 0xFF, chfs_opts->pagesize);
+
+	if (chfs_opts->mediatype == TYPE_NAND) {
+		ebhdr.u.nand_hdr.lid = htole32(lebnumber++);
+		ebhdr.u.nand_hdr.serial = htole64(++(max_serial));
+		ebhdr.u.nand_hdr.crc = htole32(crc32(0,
+		    (uint8_t *)&ebhdr.u.nand_hdr + 4, CHFS_EB_HDR_NAND_SIZE - 4));
+		memcpy(&buf, &ebhdr.u.nand_hdr, CHFS_EB_HDR_NAND_SIZE);
+	} else {
+		ebhdr.u.nor_hdr.lid = htole32(lebnumber++);
+		ebhdr.u.nor_hdr.crc = htole32(crc32(0, (uint8_t *)&ebhdr.u.nor_hdr + 4,
+		    CHFS_EB_HDR_NOR_SIZE - 4));
+		memcpy(&buf, &ebhdr.u.nor_hdr, CHFS_EB_HDR_NOR_SIZE);
+	}
+	
+	buf_write(fsopts, &buf, chfs_opts->pagesize);
+}
+
+void
+write_vnode(fsinfo_t *fsopts, fsnode *node)
+{
+	struct chfs_flash_vnode fvnode;
+	memset(&fvnode, 0, sizeof(fvnode));
+	
+	fvnode.magic = htole16(CHFS_FS_MAGIC_BITMASK);
+	fvnode.type = htole16(CHFS_NODETYPE_VNODE);
+	fvnode.length = htole32(CHFS_PAD(sizeof(fvnode)));
+	fvnode.hdr_crc = htole32(crc32(0, (uint8_t *)&fvnode,
+	    CHFS_NODE_HDR_SIZE - 4));
+	fvnode.vno = htole64(node->inode->ino);
+	fvnode.version = htole64(version++);
+	fvnode.mode = htole32(node->inode->st.st_mode);
+	fvnode.dn_size = htole32(node->inode->st.st_size);
+	fvnode.atime = htole32(node->inode->st.st_atime);
+	fvnode.ctime = htole32(node->inode->st.st_ctime);
+	fvnode.mtime = htole32(node->inode->st.st_mtime);
+	fvnode.gid = htole32(node->inode->st.st_uid);
+	fvnode.uid = htole32(node->inode->st.st_gid);
+	fvnode.node_crc = htole32(crc32(0, (uint8_t *)&fvnode,
+	    sizeof(fvnode) - 4));
+
+	pad_block_if_less_than(fsopts, sizeof(fvnode));
+	buf_write(fsopts, &fvnode, sizeof(fvnode));
+	padword(fsopts);
+}
+
+void
+write_dirent(fsinfo_t *fsopts, fsnode *node)
+{
+	struct chfs_flash_dirent_node fdirent;
+	char *name = malloc(sizeof(char) * strlen(node->name));
+
+	if (name == NULL) {
+		err(EXIT_FAILURE, "ERROR memory allocation failed");
+	}
+	
+	memset(&fdirent, 0, sizeof(fdirent));
+	memcpy(name, node->name, strlen(node->name));
+
+	fdirent.magic = htole16(CHFS_FS_MAGIC_BITMASK);
+	fdirent.type = htole16(CHFS_NODETYPE_DIRENT);
+	fdirent.length = htole32(CHFS_PAD(sizeof(fdirent) + strlen(name)));
+	fdirent.hdr_crc = htole32(crc32(0, (uint8_t *)&fdirent,
+	    CHFS_NODE_HDR_SIZE - 4));
+	fdirent.vno = htole64(node->inode->ino);
+	if (node->parent != NULL) {
+		fdirent.pvno = htole64(node->parent->inode->ino);
+	} else {
+		fdirent.pvno = htole64(node->inode->ino);
+	}
+
+	fdirent.version = htole64(version++);
+	fdirent.mctime = htole32(node->inode->st.st_mtimensec);
+	fdirent.nsize = htole32(strlen(name));
+	fdirent.dtype = htole32(IFTOCHT(node->type & S_IFMT));
+	fdirent.name_crc = htole32(crc32(0, (uint8_t *)name, fdirent.nsize));
+	fdirent.node_crc = htole32(crc32(0, (uint8_t *)&fdirent,
+	    sizeof(fdirent) - 4));
+	
+	pad_block_if_less_than(fsopts, sizeof(fdirent) + fdirent.nsize);
+	buf_write(fsopts, &fdirent, sizeof(fdirent));
+	buf_write(fsopts, name, fdirent.nsize);
+	padword(fsopts);
+}
+
+void
+write_file(fsinfo_t *fsopts, fsnode *node, const char *dir)
+{
+	int fd;
+	ssize_t len;
+	char *name = node->name;
+	chfs_opt_t *chfs_opts;
+	unsigned char *buf;
+	uint32_t fileofs = 0;
+
+	chfs_opts = fsopts->fs_specific;
+	buf = malloc(chfs_opts->pagesize);
+
+	if (buf == NULL) {
+		err(EXIT_FAILURE, "ERROR memory allocation failed");
+	}
+	
+	if (node->type == S_IFREG || node->type == S_IFSOCK) {
+		char *longname;
+		asprintf(&longname, "%s/%s", dir, name);
+
+		fd = open(longname, O_RDONLY, 0444);
+
+		while ((len = read(fd, buf, chfs_opts->pagesize))) {
+			if (len < 0) {
+				warn("ERROR while reading %s", longname);
+				free(buf);
+				close(fd);
+				return;
+			}
+
+			write_data(fsopts, node, buf, len, fileofs);
+			fileofs += len;
+		}
+		close(fd);	
+	} else if (node->type == S_IFLNK) {
+		len = strlen(node->symlink);
+		memcpy(buf, node->symlink, len);
+		write_data(fsopts, node, buf, len, 0);
+	} else if (node->type == S_IFCHR || node->type == S_IFBLK ||
+		node->type == S_IFIFO) {
+		len = sizeof(dev_t);
+		memcpy(buf, &(node->inode->st.st_rdev), len);
+		write_data(fsopts, node, buf, len, 0);
+	}
+
+	free(buf);
+}
+
+void
+write_data(fsinfo_t *fsopts, fsnode *node, unsigned char *buf, size_t len,
+    uint32_t ofs)
+{
+	struct chfs_flash_data_node fdata;
+	memset(&fdata, 0, sizeof(fdata));
+
+	if (len == 0) {
+		return;
+	}
+	
+	pad_block_if_less_than(fsopts, sizeof(fdata) + len);
+
+	fdata.magic = htole16(CHFS_FS_MAGIC_BITMASK);
+	fdata.type = htole16(CHFS_NODETYPE_DATA);
+	fdata.length = htole32(CHFS_PAD(sizeof(fdata) + len));
+	fdata.hdr_crc = htole32(crc32(0, (uint8_t *)&fdata,
+	    CHFS_NODE_HDR_SIZE - 4));
+	fdata.vno = htole64(node->inode->ino);
+	fdata.data_length = htole32(len);
+	fdata.offset = htole32(ofs);
+	fdata.data_crc = htole32(crc32(0, (uint8_t *)buf, len));
+	fdata.node_crc = htole32(crc32(0, (uint8_t *)&fdata, sizeof(fdata) - 4));
+
+	buf_write(fsopts, &fdata, sizeof(fdata));
+	buf_write(fsopts, buf, len);
+	padword(fsopts);
+}
Index: src/usr.sbin/makefs/chfs/chfs_mkfs.h
diff -u /dev/null src/usr.sbin/makefs/chfs/chfs_mkfs.h:1.1
--- /dev/null	Thu Apr 19 15:36:06 2012
+++ src/usr.sbin/makefs/chfs/chfs_mkfs.h	Thu Apr 19 15:36:06 2012
@@ -0,0 +1,44 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *		      University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <[email protected]>
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by the Department of Software Engineering, University of Szeged, Hungary
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _CHFS_MKFS_H
+#define _CHFS_MKFS_H
+
+#include "chfs.h"
+
+void padblock(fsinfo_t *);
+void write_eb_header(fsinfo_t *);
+void write_vnode(fsinfo_t *, fsnode *);
+void write_dirent(fsinfo_t *, fsnode *);
+void write_file(fsinfo_t *, fsnode *, const char *);
+void write_data(fsinfo_t *, fsnode *, unsigned char *, size_t, uint32_t);
+#endif
+

Reply via email to