$NetBSD$

--- mkfs.jffs.c.orig	2006-04-30 22:59:15.000000000 +0200
+++ mkfs.jffs.c	2007-10-20 16:29:50.000000000 +0200
@@ -16,34 +16,15 @@
 #include <sys/stat.h>
 #include <dirent.h>
 #include <unistd.h>
+#if __linux__
 #include <linux/types.h>
 #include <asm/byteorder.h>
-#include <ctype.h>
-
-#define swab16(x) \
-        ((__u16)( \
-                (((__u16)(x) & (__u16)0x00ffU) << 8) | \
-                (((__u16)(x) & (__u16)0xff00U) >> 8) ))
-#define swab32(x) \
-        ((__u32)( \
-                (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
-                (((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
-                (((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
-                (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
-
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define cpu_to_le16(x) ({ __u16 _x = x; swab16(_x); })
-#define cpu_to_le32(x) ({ __u32 _x = x; swab32(_x); })
-#define cpu_to_be16(x) (x)
-#define cpu_to_be32(x) (x)
-#else
-#define cpu_to_le16(x) (x)
-#define cpu_to_le32(x) (x)
-#define cpu_to_be16(x) ({ __u16 _x = x; swab16(_x); })
-#define cpu_to_be32(x) ({ __u32 _x = x; swab32(_x); })
 #endif
-#define le32_to_cpu(x) cpu_to_le32(x)
-#define be32_to_cpu(x) cpu_to_be32(x)
+#if __NetBSD__
+#include <stdint.h>
+#include <mtd_swab.h>
+#endif
+#include <ctype.h>
 
 #define BLOCK_SIZE 1024
 #define JFFS_MAGIC 0x34383931 /* "1984" */
@@ -55,35 +36,44 @@
 
 /* How many padding bytes should be inserted between two chunks of data
    on the flash?  */
+#ifdef __NetBSD__
+#define JFFS_GET_PAD_BYTES(size) ((JFFS_ALIGN_SIZE                     \
+				  - ((uint32_t)(size) % JFFS_ALIGN_SIZE)) \
+				  % JFFS_ALIGN_SIZE)
+#else
 #define JFFS_GET_PAD_BYTES(size) ((JFFS_ALIGN_SIZE                     \
 				  - ((__u32)(size) % JFFS_ALIGN_SIZE)) \
 				  % JFFS_ALIGN_SIZE)
+#endif
 
 
 struct jffs_raw_inode
 {
-  __u32 magic;    /* A constant magic number.  */
-  __u32 ino;      /* Inode number.  */
-  __u32 pino;     /* Parent's inode number.  */
-  __u32 version;  /* Version number.  */
-  __u32 mode;     /* file_type, mode  */
-  __u16 uid;
-  __u16 gid;
-  __u32 atime;
-  __u32 mtime;
-  __u32 ctime;
-  __u32 offset;     /* Where to begin to write.  */
-  __u32 dsize;      /* Size of the file data.  */
-  __u32 rsize;      /* How much are going to be replaced?  */
-  __u8 nsize;       /* Name length.  */
-  __u8 nlink;       /* Number of links.  */
-  __u8 spare : 6;   /* For future use.  */
-  __u8 rename : 1;  /* Is this a special rename?  */
-  __u8 deleted : 1; /* Has this file been deleted?  */
-  __u8 accurate;    /* The inode is obsolete if accurate == 0.  */
-  __u32 dchksum;    /* Checksum for the data.  */
-  __u16 nchksum;    /* Checksum for the name.  */
-  __u16 chksum;     /* Checksum for the raw_inode.  */
+#ifdef __NetBSD__
+  uint32_t magic;    /* A constant magic number.  */
+  uint32_t ino;      /* Inode number.  */
+  uint32_t pino;     /* Parent's inode number.  */
+  uint32_t version;  /* Version number.  */
+  uint32_t mode;     /* file_type, mode  */
+  uint16_t uid;
+  uint16_t gid;
+  uint32_t atime;
+  uint32_t mtime;
+  uint32_t ctime;
+  uint32_t offset;     /* Where to begin to write.  */
+  uint32_t dsize;      /* Size of the file data.  */
+  uint32_t rsize;      /* How much are going to be replaced?  */
+  uint8_t nsize;       /* Name length.  */
+  uint8_t nlink;       /* Number of links.  */
+  uint8_t spare : 6;   /* For future use.  */
+  uint8_t rename : 1;  /* Is this a special rename?  */
+  uint8_t deleted : 1; /* Has this file been deleted?  */
+  uint8_t accurate;    /* The inode is obsolete if accurate == 0.  */
+  uint32_t dchksum;    /* Checksum for the data.  */
+  uint16_t nchksum;    /* Checksum for the name.  */
+  uint16_t chksum;     /* Checksum for the raw_inode.  */
+#else
+#endif
 };
 
 
@@ -104,7 +94,11 @@
 #define ENDIAN_LITTLE 2
 int endian = ENDIAN_HOST;
 
+#ifdef __NetBSD__
+static uint32_t jffs_checksum(void *data, int size);
+#else
 static __u32 jffs_checksum(void *data, int size);
+#endif
 void jffs_print_trace(const char *path, int depth);
 int make_root_dir(FILE *fs, int first_ino, const char *root_dir_path,
 		  int depth);
@@ -113,11 +107,20 @@
 int mkfs(FILE *fs, const char *path, int ino, int parent, int depth);
 
 
+#ifdef __NetBSD__
+static uint32_t
+#else
 static __u32
+#endif
 jffs_checksum(void *data, int size)
 {
+#ifdef __NetBSD__
+  uint32_t sum = 0;
+  uint8_t *ptr = (uint8_t *)data;
+#else
   __u32 sum = 0;
   __u8 *ptr = (__u8 *)data;
+#endif
 
   while (size-- > 0)
   {
@@ -202,7 +205,11 @@
 	fprintf(stderr, "}\n");
 }
 
+#ifdef __NetBSD__
+static void write_val32(uint32_t *adr, uint32_t val)
+#else
 static void write_val32(__u32 *adr, __u32 val)
+#endif
 {
   switch(endian) {
   case ENDIAN_HOST:
@@ -217,7 +224,11 @@
   }
 }
 
+#ifdef __NetBSD__
+static void write_val16(uint16_t *adr, uint16_t val)
+#else
 static void write_val16(__u16 *adr, __u16 val)
+#endif
 {
   switch(endian) {
   case ENDIAN_HOST:
@@ -232,9 +243,17 @@
   }
 }
 
+#ifdef __NetBSD__
+static uint32_t read_val32(uint32_t *adr)
+#else
 static __u32 read_val32(__u32 *adr)
+#endif
 {
+#ifdef __NetBSD__
+  uint32_t val = 0;
+#else
   __u32 val = 0;
+#endif
 
   switch(endian) {
   case ENDIAN_HOST:
@@ -311,6 +330,9 @@
 void
 write_file(struct jffs_file *f, FILE *fs, struct stat st)
 {
+#ifdef __NetBSD__
+#else
+#endif
   int npad = JFFS_GET_PAD_BYTES(f->inode.nsize);
   int dpad = JFFS_GET_PAD_BYTES(read_val32(&f->inode.dsize));
   int size = sizeof(struct jffs_raw_inode) + f->inode.nsize + npad
@@ -340,7 +362,11 @@
   {
     if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
     {
+#ifdef __NetBSD__
+      uint16_t tmp;
+#else
       __u16 tmp;
+#endif
 
       switch(endian) {
         case ENDIAN_HOST:
