tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: chfs support in makefs



In article <op.wcplh6hzbcua1o@localhost.localdomain>,
Tamas Toth <ttoth%inf.u-szeged.hu@localhost> wrote:
>-=-=-=-=-=-
>
>> I would prefer not copying the header files. Can't the <sys/vnode.h> be
>> conditionally included #if _KERNEL, or not done at all there?
>
>I just commited the modified chfs, so there aren't header file copies in  
>chfs's makefs anymore.
>Anything else? :)

Inline :-)

christos

Index: Makefile
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/Makefile,v
retrieving revision 1.29
diff -u -r1.29 Makefile
--- Makefile    18 Jul 2011 22:52:37 -0000      1.29
+++ Makefile    13 Apr 2012 14:51:30 -0000
@@ -6,7 +6,7 @@
 .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 @@
 .PATH:         ${MKNODSRC} ${MTREESRC}
 
 .include "${.CURDIR}/cd9660/Makefile.inc"
+.include "${.CURDIR}/chfs/Makefile.inc"
 .include "${.CURDIR}/ffs/Makefile.inc"
 .include "${.CURDIR}/v7fs/Makefile.inc"
 
Index: chfs.c
===================================================================
RCS file: chfs.c
diff -N chfs.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ chfs.c      13 Apr 2012 14:51:30 -0000
@@ -0,0 +1,238 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *                   University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <ttoth%inf.u-szeged.hu@localhost>
+ * 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 *);
+
+void
+chfs_prep_opts(fsinfo_t *fsopts)
+{
+       chfs_opt_t *chfs_opts;
+
+       if ((chfs_opts = calloc(1, sizeof(chfs_opt_t))) == NULL)
+               err(1, "Allocating memory for chfs_options");

sizeof(*chfs_opts)

+       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)
+{
+       if (fsopts->fs_specific)
+               free(fsopts->fs_specific);

no need to check, call free directly.

+}
+
+int
+chfs_parse_opts(const char *option, fsinfo_t *fsopts)
+{
+       chfs_opt_t *chfs_opts = fsopts->fs_specific;
+
+       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 }
+       };

static const for the options.

+
+       char *var, *val;
+       int retval;
+
+       assert(option != NULL);
+       assert(fsopts != NULL);
+       assert(chfs_opts != NULL);
+
+       if ((var = strdup(option)) == NULL)
+               err(1, "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:
+       if (var)
+               free(var);

No need for if

+       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(1, "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(1, "Image file `%s' not populated.", image);
+       }
+       TIMER_RESULTS(start, "chfs_populate_dir");
+
+       padblock(fsopts);
+
+       if (close(fsopts->fd) == -1)
+               err(1, "Closing `%s'", image);
+       fsopts->fd = -1;
+
+       printf("Image `%s' complete\n", image);
+}
+
+static void
+chfs_validate(const char* dir, fsnode *root, fsinfo_t *fsopts)
+{
+       chfs_opt_t *chfs_opts = fsopts->fs_specific;
+
+       assert(dir != NULL);
+       assert(root != NULL);
+       assert(fsopts != NULL);
+       assert(chfs_opts != 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);

style no parens around return.

+}
+
+static int
+chfs_populate_dir(const char *dir, fsnode *root, fsnode *parent, fsinfo_t 
*fsopts)
+{
+       fsnode *cur;
+       chfs_opt_t *chfs_opts = fsopts->fs_specific;
+       char path[MAXPATHLEN + 1];
+
+       assert(dir != NULL);
+       assert(root != NULL);
+       assert(fsopts != NULL); 
+       assert(chfs_opts != 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(1, "Pathname too long.");
+               }
+               if (! chfs_populate_dir(path, cur->child, cur, fsopts)) {
+                       return 0;
+               }
+       }
+
+       return 1;
+}
+
Index: chfs_makefs.h
===================================================================
RCS file: chfs_makefs.h
diff -N chfs_makefs.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ chfs_makefs.h       13 Apr 2012 14:51:30 -0000
@@ -0,0 +1,50 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *                   University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <ttoth%inf.u-szeged.hu@localhost>
+ * 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
+
+#define CHFS_ROOTINO 2
+
+typedef struct {
+       int pagesize;           /* page size */
+       int eraseblock;         /* eraseblock size */
+       int mediatype;          /* type of the media, 0 (nor) or 1 (nand) */
+} chfs_opt_t;
+
+#endif
Index: makefs.8
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/makefs.8,v
retrieving revision 1.38
diff -u -r1.38 makefs.8
--- makefs.8    28 Jan 2012 02:35:46 -0000      1.38
+++ makefs.8    13 Apr 2012 14:51:31 -0000
@@ -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 @@
 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 @@
 .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 @@
 .An Ram Vedam
 (cd9660 support),
 .An UCHIYAMA Yasushi
-(v7fs support).
+(v7fs support),
+.An Tamas Toth
+(chfs support).
Index: makefs.c
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/makefs.c,v
retrieving revision 1.31
diff -u -r1.31 makefs.c
--- makefs.c    28 Jan 2012 02:35:46 -0000      1.31
+++ makefs.c    13 Apr 2012 14:51:31 -0000
@@ -73,6 +73,8 @@
        { "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: makefs.h
===================================================================
RCS file: /cvsroot/src/usr.sbin/makefs/makefs.h,v
retrieving revision 1.24
diff -u -r1.24 makefs.h
--- makefs.h    28 Jan 2012 02:35:46 -0000      1.24
+++ makefs.h    13 Apr 2012 14:51:31 -0000
@@ -172,6 +172,11 @@
 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 *);
Index: chfs/Makefile.inc
===================================================================
RCS file: chfs/Makefile.inc
diff -N chfs/Makefile.inc
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ chfs/Makefile.inc   13 Apr 2012 14:51:31 -0000
@@ -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: chfs/chfs_mkfs.c
===================================================================
RCS file: chfs/chfs_mkfs.c
diff -N chfs/chfs_mkfs.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ chfs/chfs_mkfs.c    13 Apr 2012 14:51:31 -0000
@@ -0,0 +1,279 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *                   University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <ttoth%inf.u-szeged.hu@localhost>
+ * 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 <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <zlib.h>
+
+#include "makefs.h"
+#include "chfs_makefs.h"
+
+#include "chfs.h"
+#include "media.h"
+#include "ebh.h"
+
+#include "chfs/chfs_mkfs.h"
+
+#define min(x,y) (x < y) ? x : y
+
+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, int len)

should be size_t

+{
+       int retval;

should be ssize_t

+       const char *charbuf = (const char*)buf;

No need to case.

+
+       while (len > 0) {
+               retval = write(fsopts->fd, charbuf, len);
+
+               if (retval <= 0) {
+                       printf("ERROR while writing\n");
+                       return;

should use err and check against -1, no point continuing if there is an error.

+               }
+
+               len -= retval;
+               charbuf += retval;
+               img_ofs += retval;
+       }
+}
+
+void
+padblock(fsinfo_t *fsopts)
+{
+       chfs_opt_t *chfs_opts = fsopts->fs_specific;
+
+       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)
+{
+       chfs_opt_t *chfs_opts = fsopts->fs_specific;
+       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, chfs_opts->pagesize);
no &, use sizeof()


+       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));
+       
+       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, len;

ssize_t for 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 (node->type == S_IFREG || node->type == S_IFSOCK) {
+               char *longname = malloc((strlen(dir) + strlen(name)) * 
sizeof(char));
+               sprintf(longname, "%s/%s", dir, name);

use asprintf()

+
+               fd = open(longname, O_RDONLY, 0444);
+
+               while ((len = read(fd, buf, chfs_opts->pagesize))) {
+                       if (len < 0) {
+                               printf("ERROR while reading %s\n", longname);

use warn()

+                               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, int len, 
uint32_t ofs)

size_t for len;
+{
+       struct chfs_flash_data_node fdata;
+       memset(&fdata, 0, sizeof(fdata));
+
+       if (len > 0) {

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: chfs/chfs_mkfs.h
===================================================================
RCS file: chfs/chfs_mkfs.h
diff -N chfs/chfs_mkfs.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ chfs/chfs_mkfs.h    13 Apr 2012 14:51:31 -0000
@@ -0,0 +1,42 @@
+/*-
+ * Copyright (c) 2012 Department of Software Engineering,
+ *                   University of Szeged, Hungary
+ * Copyright (c) 2012 Tamas Toth <ttoth%inf.u-szeged.hu@localhost>
+ * 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
+
+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 *, int, uint32_t);
+#endif
+



Home | Main Index | Thread Index | Old Index