make mtd-utils compile correctly on cygwin
[oweals/openwrt.git] / tools / mtd-utils / patches / 120-cygwin_fixes.patch
1 diff -urN mtd-20090505/Makefile mtd-20090505.new/Makefile
2 --- mtd-20090505/Makefile       2009-06-05 16:59:08.000000000 +0200
3 +++ mtd-20090505.new/Makefile   2010-01-05 11:11:24.000000000 +0100
4 @@ -7,6 +7,11 @@
5    CPPFLAGS += -DWITHOUT_XATTR
6  endif
7  
8 +ifeq ($(shell uname -o),Cygwin)
9 +CPPFLAGS += -I./include/cygwin
10 +endif
11 +
12 +ifneq ($(shell uname -o),Cygwin)
13  SUBDIRS = ubi-utils mkfs.ubifs
14  
15  TARGETS = ftl_format flash_erase flash_eraseall nanddump doc_loadbios \
16 @@ -17,6 +22,10 @@
17         rfddump rfdformat \
18         serve_image recv_image \
19         sumtool #jffs2reader
20 +else
21 +SUBDIRS =
22 +TARGETS = mkfs.jffs2
23 +endif
24  
25  SYMLINKS =
26  
27 diff -urN mtd-20090505/include/cygwin/bits-byteswap.h mtd-20090505.new/include/cygwin/bits-byteswap.h
28 --- mtd-20090505/include/cygwin/bits-byteswap.h 1970-01-01 01:00:00.000000000 +0100
29 +++ mtd-20090505.new/include/cygwin/bits-byteswap.h     2010-01-05 11:09:45.000000000 +0100
30 @@ -0,0 +1,132 @@
31 +/* Macros to swap the order of bytes in integer values.
32 +   Copyright (C) 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
33 +   This file is part of the GNU C Library.
34 +
35 +   The GNU C Library is free software; you can redistribute it and/or
36 +   modify it under the terms of the GNU Lesser General Public
37 +   License as published by the Free Software Foundation; either
38 +   version 2.1 of the License, or (at your option) any later version.
39 +
40 +   The GNU C Library is distributed in the hope that it will be useful,
41 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
42 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43 +   Lesser General Public License for more details.
44 +
45 +   You should have received a copy of the GNU Lesser General Public
46 +   License along with the GNU C Library; if not, write to the Free
47 +   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
48 +   02111-1307 USA.  */
49 +
50 +#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
51 +# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
52 +#endif
53 +
54 +#ifndef _BITS_BYTESWAP_H
55 +#define _BITS_BYTESWAP_H 1
56 +
57 +/* Swap bytes in 16 bit value.  */
58 +#define __bswap_constant_16(x) \
59 +     ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
60 +
61 +#ifdef __GNUC__
62 +# if __GNUC__ >= 2
63 +#  define __bswap_16(x) \
64 +     (__extension__                                                          \
65 +      ({ register unsigned short int __v, __x = (x);                         \
66 +        if (__builtin_constant_p (__x))                                      \
67 +          __v = __bswap_constant_16 (__x);                                   \
68 +        else                                                                 \
69 +          __asm__ ("rorw $8, %w0"                                            \
70 +                   : "=r" (__v)                                              \
71 +                   : "0" (__x)                                               \
72 +                   : "cc");                                                  \
73 +        __v; }))
74 +# else
75 +/* This is better than nothing.  */
76 +#  define __bswap_16(x) \
77 +     (__extension__                                                          \
78 +      ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
79 +# endif
80 +#else
81 +static __inline unsigned short int
82 +__bswap_16 (unsigned short int __bsx)
83 +{
84 +  return __bswap_constant_16 (__bsx);
85 +}
86 +#endif
87 +
88 +/* Swap bytes in 32 bit value.  */
89 +#define __bswap_constant_32(x) \
90 +     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |                      \
91 +      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
92 +
93 +#ifdef __GNUC__
94 +# if __GNUC__ >= 2
95 +/* To swap the bytes in a word the i486 processors and up provide the
96 +   `bswap' opcode.  On i386 we have to use three instructions.  */
97 +#  if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
98 +#   define __bswap_32(x)                                                     \
99 +     (__extension__                                                          \
100 +      ({ register unsigned int __v, __x = (x);                               \
101 +        if (__builtin_constant_p (__x))                                      \
102 +          __v = __bswap_constant_32 (__x);                                   \
103 +        else                                                                 \
104 +          __asm__ ("rorw $8, %w0;"                                           \
105 +                   "rorl $16, %0;"                                           \
106 +                   "rorw $8, %w0"                                            \
107 +                   : "=r" (__v)                                              \
108 +                   : "0" (__x)                                               \
109 +                   : "cc");                                                  \
110 +        __v; }))
111 +#  else
112 +#   define __bswap_32(x) \
113 +     (__extension__                                                          \
114 +      ({ register unsigned int __v, __x = (x);                               \
115 +        if (__builtin_constant_p (__x))                                      \
116 +          __v = __bswap_constant_32 (__x);                                   \
117 +        else                                                                 \
118 +          __asm__ ("bswap %0" : "=r" (__v) : "0" (__x));                     \
119 +        __v; }))
120 +#  endif
121 +# else
122 +#  define __bswap_32(x) \
123 +     (__extension__                                                          \
124 +      ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
125 +# endif
126 +#else
127 +static __inline unsigned int
128 +__bswap_32 (unsigned int __bsx)
129 +{
130 +  return __bswap_constant_32 (__bsx);
131 +}
132 +#endif
133 +
134 +
135 +#if defined __GNUC__ && __GNUC__ >= 2
136 +/* Swap bytes in 64 bit value.  */
137 +#define __bswap_constant_64(x) \
138 +     ((((x) & 0xff00000000000000ull) >> 56)                                  \
139 +      | (((x) & 0x00ff000000000000ull) >> 40)                                \
140 +      | (((x) & 0x0000ff0000000000ull) >> 24)                                \
141 +      | (((x) & 0x000000ff00000000ull) >> 8)                                 \
142 +      | (((x) & 0x00000000ff000000ull) << 8)                                 \
143 +      | (((x) & 0x0000000000ff0000ull) << 24)                                \
144 +      | (((x) & 0x000000000000ff00ull) << 40)                                \
145 +      | (((x) & 0x00000000000000ffull) << 56))
146 +
147 +# define __bswap_64(x) \
148 +     (__extension__                                                          \
149 +      ({ union { __extension__ unsigned long long int __ll;                  \
150 +                unsigned long int __l[2]; } __w, __r;                        \
151 +         if (__builtin_constant_p (x))                                       \
152 +          __r.__ll = __bswap_constant_64 (x);                                \
153 +        else                                                                 \
154 +          {                                                                  \
155 +            __w.__ll = (x);                                                  \
156 +            __r.__l[0] = __bswap_32 (__w.__l[1]);                            \
157 +            __r.__l[1] = __bswap_32 (__w.__l[0]);                            \
158 +          }                                                                  \
159 +        __r.__ll; }))
160 +#endif
161 +
162 +#endif /* _BITS_BYTESWAP_H */
163 diff -urN mtd-20090505/include/cygwin/byteswap.h mtd-20090505.new/include/cygwin/byteswap.h
164 --- mtd-20090505/include/cygwin/byteswap.h      1970-01-01 01:00:00.000000000 +0100
165 +++ mtd-20090505.new/include/cygwin/byteswap.h  2010-01-05 11:09:45.000000000 +0100
166 @@ -0,0 +1,40 @@
167 +/* Copyright (C) 1997 Free Software Foundation, Inc.
168 +   This file is part of the GNU C Library.
169 +
170 +   The GNU C Library is free software; you can redistribute it and/or
171 +   modify it under the terms of the GNU Lesser General Public
172 +   License as published by the Free Software Foundation; either
173 +   version 2.1 of the License, or (at your option) any later version.
174 +
175 +   The GNU C Library is distributed in the hope that it will be useful,
176 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
177 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
178 +   Lesser General Public License for more details.
179 +
180 +   You should have received a copy of the GNU Lesser General Public
181 +   License along with the GNU C Library; if not, write to the Free
182 +   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
183 +   02111-1307 USA.  */
184 +
185 +#ifndef _BYTESWAP_H
186 +#define _BYTESWAP_H     1
187 +
188 +/* Get the machine specific, optimized definitions.  */
189 +#include "bits-byteswap.h"
190 +
191 +
192 +/* The following definitions must all be macros since otherwise some
193 +   of the possible optimizations are not possible.  */
194 +
195 +/* Return a value with all bytes in the 16 bit argument swapped.  */
196 +#define bswap_16(x) __bswap_16 (x)
197 +
198 +/* Return a value with all bytes in the 32 bit argument swapped.  */
199 +#define bswap_32(x) __bswap_32 (x)
200 +
201 +#if defined __GNUC__ && __GNUC__ >= 2
202 +/* Return a value with all bytes in the 64 bit argument swapped.  */
203 +# define bswap_64(x) __bswap_64 (x)
204 +#endif
205 +
206 +#endif /* byteswap.h */
207 diff -urN mtd-20090505/include/cygwin/endian.h mtd-20090505.new/include/cygwin/endian.h
208 --- mtd-20090505/include/cygwin/endian.h        1970-01-01 01:00:00.000000000 +0100
209 +++ mtd-20090505.new/include/cygwin/endian.h    2010-01-05 11:43:10.000000000 +0100
210 @@ -0,0 +1,26 @@
211 +#ifndef _CYGENDIAN_H_
212 +#define _CYGENDIAN_H_
213 +
214 +#ifdef __CYGWIN__
215 +
216 +#include <sys/param.h>
217 +
218 +#ifndef __BIG_ENDIAN
219 +#define __BIG_ENDIAN 4321
220 +#endif
221 +
222 +#ifndef __LITTLE_ENDIAN
223 +#define __LITTLE_ENDIAN 1234
224 +#endif
225 +
226 +#ifndef __BYTE_ORDER
227 +#define __BYTE_ORDER   __LITTLE_ENDIAN
228 +#endif
229 +
230 +#ifndef BYTE_ORDER
231 +#define BYTE_ORDER     __LITTLE_ENDIAN
232 +#endif
233 +
234 +#endif /* __CYGWIN__ */
235 +
236 +#endif /* _CYGENDIAN_H_ */
237 diff -urN mtd-20090505/include/cygwin/ioctl.h mtd-20090505.new/include/cygwin/ioctl.h
238 --- mtd-20090505/include/cygwin/ioctl.h 1970-01-01 01:00:00.000000000 +0100
239 +++ mtd-20090505.new/include/cygwin/ioctl.h     2010-01-05 11:09:45.000000000 +0100
240 @@ -0,0 +1,38 @@
241 +#ifndef _CYGIOCTL_H_
242 +#define _CYGIOCTL_H_
243 +
244 +#ifdef __CYGWIN__
245 +
246 +#define _IOC_NRBITS     8
247 +#define _IOC_TYPEBITS   8
248 +#define _IOC_SIZEBITS   14
249 +#define _IOC_DIRBITS    2
250
251 +#define _IOC_NRMASK     ((1 << _IOC_NRBITS)-1)
252 +#define _IOC_TYPEMASK   ((1 << _IOC_TYPEBITS)-1)
253 +#define _IOC_SIZEMASK   ((1 << _IOC_SIZEBITS)-1)
254 +#define _IOC_DIRMASK    ((1 << _IOC_DIRBITS)-1)
255
256 +#define _IOC_NRSHIFT    0
257 +#define _IOC_TYPESHIFT  (_IOC_NRSHIFT+_IOC_NRBITS)
258 +#define _IOC_SIZESHIFT  (_IOC_TYPESHIFT+_IOC_TYPEBITS)
259 +#define _IOC_DIRSHIFT   (_IOC_SIZESHIFT+_IOC_SIZEBITS)
260
261 +#define _IOC_NONE       0U
262 +#define _IOC_WRITE      1U
263 +#define _IOC_READ       2U
264
265 +#define _IOC(dir,type,nr,size) \
266 +       (((dir)  << _IOC_DIRSHIFT) | \
267 +       ((type) << _IOC_TYPESHIFT) | \
268 +       ((nr)   << _IOC_NRSHIFT) | \
269 +       ((size) << _IOC_SIZESHIFT))
270 +
271 +#define _IO(type,nr)            _IOC(_IOC_NONE,(type),(nr),0)
272 +#define _IOR(type,nr,size)      _IOC(_IOC_READ,(type),(nr),sizeof(size))
273 +#define _IOW(type,nr,size)      _IOC(_IOC_WRITE,(type),(nr),sizeof(size))
274 +#define _IOWR(type,nr,size)     _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),sizeof(size))
275 +
276 +#endif /* __CYGWIN__ */
277 +
278 +#endif /* _CYGIOCTL_H_ */
279 diff -urN mtd-20090505/include/cygwin/pread.c mtd-20090505.new/include/cygwin/pread.c
280 --- mtd-20090505/include/cygwin/pread.c 1970-01-01 01:00:00.000000000 +0100
281 +++ mtd-20090505.new/include/cygwin/pread.c     2010-01-05 11:09:45.000000000 +0100
282 @@ -0,0 +1,41 @@
283 +#ifdef __CYGWIN__
284 +
285 +#include <errno.h>
286 +
287 +ssize_t
288 +pread(int fd, void *p, size_t n, off_t off)
289 +{
290 +        off_t ooff;
291 +        int oerrno;
292 +
293 +        if ((ooff  = lseek(fd, off, SEEK_SET)) == -1)
294 +                return -1;
295 +
296 +        n = read(fd, p, n);
297 +
298 +        oerrno = errno;
299 +        lseek(fd, ooff, SEEK_SET);
300 +        errno = oerrno;
301 +
302 +        return n;
303 +}
304 +
305 +ssize_t
306 +pwrite(int fd, const void *p, size_t n, off_t off)
307 +{
308 +        off_t ooff;
309 +        int oerrno;
310 +
311 +        if ((ooff  = lseek(fd, off, SEEK_SET)) == -1)
312 +                return -1;
313 +
314 +        n = write(fd, p, n);
315 +
316 +        oerrno = errno;
317 +        lseek(fd, ooff, SEEK_SET);
318 +        errno = oerrno;
319 +
320 +        return n;
321 +}
322 +
323 +#endif /* __CYGWIN__ */
324 diff -urN mtd-20090505/lnconf.sh mtd-20090505.new/lnconf.sh
325 --- mtd-20090505/lnconf.sh      1970-01-01 01:00:00.000000000 +0100
326 +++ mtd-20090505.new/lnconf.sh  2010-01-05 11:09:45.000000000 +0100
327 @@ -0,0 +1,53 @@
328 +#!/bin/sh
329 +#
330 +# Generic configure replacement.
331 +#
332 +# $Id: lnconf.sh,v 1.1 2004/04/05 21:55:59 igor Exp $ 
333 +#
334 +# Copies all files from the script directory to the current one.
335 +# Intended to replace 'configure' for packages that don't have one, to
336 +# allow building outside of the source tree.
337 +#
338 +# Note: this does not do any fancy things with detecting shells and
339 +# supporting other platforms.  But it should work on Cygwin.
340 +
341 +# find out where the script is located
342 +tdir=`echo "$0" | sed 's%[\\/][^\\/][^\\/]*$%%'`
343 +test "x$tdir" = "x$0" && tdir=.
344 +
345 +a_srcdir=`cd $tdir; pwd`
346 +a_destdir=`pwd`
347 +
348 +# sanity checks:
349 +# are we in the script directory?
350 +test "x$a_srcdir" = "x$a_destdir" && exit 0
351 +# is there any chance that this is the script directory?
352 +test "x`cd "$a_srcdir" && /bin/ls -id`" = "x`/bin/ls -id`" && exit 0
353 +
354 +# try to find lndir and use it if it's available
355 +LNDIR="`which lndir 2>/dev/null`"
356 +if [ "x$LNDIR" = "x" ]; then
357 +  lndir() {
358 +    test "x$1" = "x" && return 1
359 +    # be careful of the current directory
360 +    DINODE=`find . -maxdepth 0 -ls | sed 's/ .*$//'` 
361 +    case "`pwd`" in
362 +      "`cd "$1" && pwd`"/*) CUR="-type d -inum $DINODE -prune -o";;
363 +    esac
364 +    # duplicate the directory structure
365 +    (cd "$1" && find . $CUR -type d -mindepth 1 -print) | xargs -tr mkdir -p
366 +    # copy all symbolic links
367 +    (cd "$1" && find . $CUR -type l -mindepth 1 -print) | xargs -ri sh -c "ln -s \"\`readlink "$1/{}"\`\" \"{}\""
368 +    # or simply
369 +    #(cd "$1" && find . $CUR -type l -mindepth 1 -print) | xargs -ri ln -s "$1"/{} {}
370 +    # link all files
371 +    (cd "$1" && find . $CUR -type f -mindepth 1 -print) | xargs -ri ln -s "$1"/{} {}
372 +  }
373 +else
374 +  lndir() {
375 +    "$LNDIR" "$@"
376 +  }
377 +fi
378 +
379 +lndir "$tdir"
380 +
381 diff -urN mtd-20090505/mkfs.jffs2.c mtd-20090505.new/mkfs.jffs2.c
382 --- mtd-20090505/mkfs.jffs2.c   2009-06-05 16:59:08.000000000 +0200
383 +++ mtd-20090505.new/mkfs.jffs2.c       2010-01-05 11:25:52.000000000 +0100
384 @@ -75,6 +75,14 @@
385  #include "crc32.h"
386  #include "rbtree.h"
387  
388 +#ifdef __CYGWIN__
389 +#include <cygwin/ioctl.h>
390 +#include <cygwin/endian.h>
391 +#include <cygwin/pread.c>
392 +# define IFTODT(mode)          (((mode) & 0170000) >> 12)
393 +# define DTTOIF(dirtype)       ((dirtype) << 12)
394 +#endif /* __CYGWIN__ */
395 +
396  /* Do not use the weird XPG version of basename */
397  #undef basename
398  
399 @@ -474,7 +482,7 @@
400     the following macros use it if available or use a hacky workaround...
401   */
402  
403 -#ifdef __GNUC__
404 +#if defined __GNUC__ && !defined __CYGWIN__
405  #define SCANF_PREFIX "a"
406  #define SCANF_STRING(s) (&s)
407  #define GETCWD_SIZE 0
408 @@ -557,6 +565,14 @@
409         }
410         entry = find_filesystem_entry(root, name, mode);
411         if (entry) {
412 +               /* Check the type */
413 +               if ((mode & S_IFMT) != (entry->sb.st_mode & S_IFMT)) {
414 +                       error_msg ("skipping device_table entry '%s': type mismatch!", name);
415 +                       free(name);
416 +                       free(hostpath);
417 +                       return 1;
418 +               }
419 +
420                 /* Ok, we just need to fixup the existing entry
421                  * and we will be all done... */
422                 entry->sb.st_uid = uid;
423 @@ -566,11 +582,21 @@
424                         entry->sb.st_rdev = makedev(major, minor);
425                 }
426         } else {
427 +               if (type == 'f' || type == 'l') {
428 +                       error_msg ("skipping device_table entry '%s': file does not exist!", name);
429 +                       free(name);
430 +                       free(hostpath);
431 +                       return 1;
432 +               }
433                 /* If parent is NULL (happens with device table entries),
434                  * try and find our parent now) */
435                 tmp = strdup(name);
436                 dir = dirname(tmp);
437 -               parent = find_filesystem_entry(root, dir, S_IFDIR);
438 +               if (!strcmp(dir, "/")) {
439 +                       parent = root;
440 +               } else {
441 +                       parent = find_filesystem_entry(root, dir, S_IFDIR);
442 +               }
443                 free(tmp);
444                 if (parent == NULL) {
445                         error_msg ("skipping device_table entry '%s': no parent directory!", name);
446 @@ -584,6 +610,7 @@
447                                 add_host_filesystem_entry(name, hostpath, uid, gid, mode, 0, parent);
448                                 break;
449                         case 'f':
450 +                       case 'l':
451                                 add_host_filesystem_entry(name, hostpath, uid, gid, mode, 0, parent);
452                                 break;
453                         case 'p':
454 diff -urN mtd-20090505/ubi-utils/src/libubi.c mtd-20090505.new/ubi-utils/src/libubi.c
455 --- mtd-20090505/ubi-utils/src/libubi.c 2009-06-05 16:59:08.000000000 +0200
456 +++ mtd-20090505.new/ubi-utils/src/libubi.c     2010-01-05 11:46:24.000000000 +0100
457 @@ -30,6 +30,9 @@
458  #include <sys/ioctl.h>
459  #include <sys/stat.h>
460  #include <sys/types.h>
461 +#ifdef __CYGWIN__
462 +#include <cygwin/ioctl.h>
463 +#endif
464  #include <libubi.h>
465  #include "libubi_int.h"
466  #include "common.h"