added port of dosfsck. Does not work yet as I cannot figure how to open the device. Win32 file access code is included ported from mkdosfs.c
Added: trunk/rosapps/sysutils/dosfsck/
Added: trunk/rosapps/sysutils/dosfsck/.cvsignore
Added: trunk/rosapps/sysutils/dosfsck/CHANGES
Added: trunk/rosapps/sysutils/dosfsck/COPYING
Added: trunk/rosapps/sysutils/dosfsck/Makefile
Added: trunk/rosapps/sysutils/dosfsck/README
Added: trunk/rosapps/sysutils/dosfsck/boot.c
Added: trunk/rosapps/sysutils/dosfsck/boot.h
Added: trunk/rosapps/sysutils/dosfsck/byteorder.h
Added: trunk/rosapps/sysutils/dosfsck/byteswap.h
Added: trunk/rosapps/sysutils/dosfsck/byteswap1.h
Added: trunk/rosapps/sysutils/dosfsck/check.c
Added: trunk/rosapps/sysutils/dosfsck/check.h
Added: trunk/rosapps/sysutils/dosfsck/common.c
Added: trunk/rosapps/sysutils/dosfsck/common.h
Added: trunk/rosapps/sysutils/dosfsck/compiler.h
Added: trunk/rosapps/sysutils/dosfsck/dosfsck.8
Added: trunk/rosapps/sysutils/dosfsck/dosfsck.c
Added: trunk/rosapps/sysutils/dosfsck/dosfsck.h
Added: trunk/rosapps/sysutils/dosfsck/fat.c
Added: trunk/rosapps/sysutils/dosfsck/fat.h
Added: trunk/rosapps/sysutils/dosfsck/file.c
Added: trunk/rosapps/sysutils/dosfsck/file.h
Added: trunk/rosapps/sysutils/dosfsck/generic.h
Added: trunk/rosapps/sysutils/dosfsck/io.c
Added: trunk/rosapps/sysutils/dosfsck/io.h
Added: trunk/rosapps/sysutils/dosfsck/lfn.c
Added: trunk/rosapps/sysutils/dosfsck/lfn.h
Added: trunk/rosapps/sysutils/dosfsck/little_endian.h
Added: trunk/rosapps/sysutils/dosfsck/mkdosfs.c
Added: trunk/rosapps/sysutils/dosfsck/msdos_fs.h
Added: trunk/rosapps/sysutils/dosfsck/swab.h
Added: trunk/rosapps/sysutils/dosfsck/types.h.bak
Added: trunk/rosapps/sysutils/dosfsck/version.h
Added: trunk/rosapps/sysutils/dosfsck/vfat.h

Added: trunk/rosapps/sysutils/dosfsck/.cvsignore
--- trunk/rosapps/sysutils/dosfsck/.cvsignore	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/.cvsignore	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1 @@
+dosfsck

Added: trunk/rosapps/sysutils/dosfsck/CHANGES
--- trunk/rosapps/sysutils/dosfsck/CHANGES	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/CHANGES	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,10 @@
+Changes from version 0 to 1
+===========================
+
+  - fixed an off-by-two error in check.c:check_file
+  - fixed marking clusters bad in fat.c:set_fat
+  - fat.c:reclaim_free was also reclaiming bad clusters.
+  - fixed many incorrect byte sex conversions in check.c and fat.c
+  - -t and -w now require -a or -r
+  - added option -d to drop files.
+  - added option -u to try to "undelete" non-directory files.

Added: trunk/rosapps/sysutils/dosfsck/COPYING
--- trunk/rosapps/sysutils/dosfsck/COPYING	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/COPYING	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,345 @@
+The license below applies to dosfsck, which is copyrighted by
+Werner Almesberger <almesber@lrc.di.epfl.ch> and Roman Hodek
+<Roman.Hodek@informatik.uni-erlangen.de>.
+
+
+		    GNU GENERAL PUBLIC LICENSE
+		       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+                       59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+		    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+			    NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+	    How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) 19yy  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) 19yy name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.

Added: trunk/rosapps/sysutils/dosfsck/Makefile
--- trunk/rosapps/sysutils/dosfsck/Makefile	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/Makefile	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,44 @@
+CC=mingw32-gcc
+OBJECTS = boot.o check.o common.o dosfsck.o fat.o file.o io.o lfn.o
+
+all: dosfsck.exe
+
+dosfsck.exe: $(OBJECTS)
+	$(CC) -o $@ $(LDFLAGS) $^
+
+.c.o:
+	$(CC) -c $(CFLAGS) $*.c
+
+install: dosfsck
+	mkdir -p $(SBINDIR) $(MANDIR)
+	install -m 755 dosfsck $(SBINDIR)
+	install -m 644 dosfsck.8 $(MANDIR)
+	rm -f $(SBINDIR)/fsck.msdos
+	rm -f $(SBINDIR)/fsck.vfat
+	ln -s dosfsck $(SBINDIR)/fsck.msdos
+	ln -s dosfsck $(SBINDIR)/fsck.vfat
+	rm -f $(MANDIR)/fsck.msdos.8
+	ln -s dosfsck.8 $(MANDIR)/fsck.msdos.8
+	ln -s dosfsck.8 $(MANDIR)/fsck.vfat.8
+
+clean:
+	rm -f *.o *.s *.i *~ \#*# tmp_make .#* .new*
+
+distclean: clean
+	rm -f *.a dosfsck
+
+dep:
+	sed '/\#\#\# Dependencies/q' <Makefile >tmp_make
+	$(CPP) $(CFLAGS) -MM *.c >>tmp_make
+	mv tmp_make Makefile
+
+### Dependencies
+boot.o: boot.c common.h dosfsck.h io.h boot.h
+check.o: check.c common.h dosfsck.h io.h fat.h file.h lfn.h check.h
+common.o: common.c common.h
+dosfsck.o: dosfsck.c common.h dosfsck.h io.h boot.h fat.h file.h \
+ check.h
+fat.o: fat.c common.h dosfsck.h io.h check.h fat.h
+file.o: file.c common.h file.h
+io.o: io.c dosfsck.h common.h io.h
+lfn.o: lfn.c common.h io.h dosfsck.h lfn.h file.h

Added: trunk/rosapps/sysutils/dosfsck/README
--- trunk/rosapps/sysutils/dosfsck/README	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/README	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,60 @@
+dosfsck, version 1
+==================
+
+WARNING: This is ALPHA test software. Use at your own risk.
+
+dosfsck is the Linux equivalent of PC/MS-DOS' CHKDSK. It checks the
+consistency of PC/MS-DOS file systems and optionally tries to repair
+them. The tests dosfsck performs are described in the man page.
+
+dosfsck needs header files from dosfs.9 (or later) to compile.
+
+Before using dosfsck to repair a file system that contains data of any
+value, you should verify that dosfsck is able to correct all reported
+errors. (Except fatal errors and those reported as unfixable, of
+course.) In order to do this, run it with the -V option, e.g.
+
+   dosfsck -V /dev/sda1		(automatic check)
+or dosfsck -V -r /dev/sda1	(interactive check and repair)
+
+dosfsck will perform two passes: in the first pass, inconsistencies are
+detected and a list of changes to correct the problems is generated. In
+the second pass, those changes are applied whenever dosfsck reads data
+from disk. Hence no fixable errors should be reported in the second
+pass if the first pass was successful.
+
+Please notify the author if fixable errors are reported in the second
+pass.
+
+After verifying that dosfsck appears to be able to perform the desired
+operations, either confirm that you want the changes to be performed
+(if dosfsck was started with -r) or re-run dosfsck with the -a option
+(if it was started without -r).
+
+Please send bug reports, comments, flames, etc. to
+almesber@nessie.cs.id.ethz.ch  or  almesber@bernina.ethz.ch
+
+- Werner
+
+FAT32 and LFN support
+=====================
+
+I've finally implemented some of the new features of MS-DOS
+filesystems: FAT32 and long filenames.
+
+FAT32 is automatically detected and of course the different FAT
+structure is handled. (Internally many changes were needed, so 32 bit
+variables for all cluster numbers and 64 bit vars for offsets inside
+the filesystem.) New checks for FAT32 are most notably on the backup
+boot sector and the new info sector. Also the possibility that the
+root directory resides in a cluster chain (instead of in a static
+area) on FAT32 is handled.
+
+dosfscheck also knows about VFAT long filenames now. It parses those
+names and uses them in listings etc. when available. There are also
+some checks on the (cruel) structure of how LFNs are stored and some
+attempts to fix problems.
+
+- Roman <roman@hodek.net>
+
+BTW, version 2 isn't ALPHA anymore :-)

Added: trunk/rosapps/sysutils/dosfsck/boot.c
--- trunk/rosapps/sysutils/dosfsck/boot.c	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/boot.c	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,370 @@
+/* boot.c  -  Read and analyze ia PC/MS-DOS boot sector */
+
+/* Written 1993 by Werner Almesberger */
+
+/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
+ * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include "common.h"
+#include "dosfsck.h"
+#include "io.h"
+#include "boot.h"
+
+
+#define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
+    /* don't divide by zero */
+
+static struct {
+    __u8 media;
+    char *descr;
+} mediabytes[] = {
+    { 0xf0, "5.25\" or 3.5\" HD floppy" },
+    { 0xf8, "hard disk" },
+    { 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or "
+            "5.25\" 1.2M floppy 2s/80tr/15sec" },
+    { 0xfa, "5.25\" 320k floppy 1s/80tr/8sec" },
+    { 0xfb, "3.5\" 640k floppy 2s/80tr/8sec" },
+    { 0xfc, "5.25\" 180k floppy 1s/40tr/9sec" },
+    { 0xfd, "5.25\" 360k floppy 2s/40tr/9sec" },
+    { 0xfe, "5.25\" 160k floppy 1s/40tr/8sec" },
+    { 0xff, "5.25\" 320k floppy 2s/40tr/8sec" },
+};
+
+#if defined __alpha || defined __ia64__ || defined __s390x__ || defined __x86_64__ || defined __ppc64__
+/* Unaligned fields must first be copied byte-wise */
+#define GET_UNALIGNED_W(f)			\
+    ({						\
+	unsigned short __v;			\
+	memcpy( &__v, &f, sizeof(__v) );	\
+	CF_LE_W( *(unsigned short *)&f );	\
+    })
+#else
+#define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f )
+#endif
+
+
+static char *get_media_descr( unsigned char media )
+{
+    int i;
+
+    for( i = 0; i < sizeof(mediabytes)/sizeof(*mediabytes); ++i ) {
+	if (mediabytes[i].media == media)
+	    return( mediabytes[i].descr );
+    }
+    return( "undefined" );
+}
+
+static void dump_boot(DOS_FS *fs,struct boot_sector *b,unsigned lss)
+{
+    unsigned short sectors;
+    
+    printf("Boot sector contents:\n");
+    if (!atari_format) {
+	char id[9];
+	strncpy(id,b->system_id,8);
+	id[8] = 0;
+	printf("System ID \"%s\"\n",id);
+    }
+    else {
+	/* On Atari, a 24 bit serial number is stored at offset 8 of the boot
+	 * sector */
+	printf("Serial number 0x%x\n",
+	       b->system_id[5] | (b->system_id[6]<<8) | (b->system_id[7]<<16));
+    }
+    printf("Media byte 0x%02x (%s)\n",b->media,get_media_descr(b->media));
+    printf("%10d bytes per logical sector\n",GET_UNALIGNED_W(b->sector_size));
+    printf("%10d bytes per cluster\n",fs->cluster_size);
+    printf("%10d reserved sector%s\n",CF_LE_W(b->reserved),
+	   CF_LE_W(b->reserved) == 1 ? "" : "s");
+    printf("First FAT starts at byte %llu (sector %llu)\n",
+	   (unsigned long long)fs->fat_start,
+	   (unsigned long long)fs->fat_start/lss);
+    printf("%10d FATs, %d bit entries\n",b->fats,fs->fat_bits);
+    printf("%10d bytes per FAT (= %u sectors)\n",fs->fat_size,
+	   fs->fat_size/lss);
+    if (!fs->root_cluster) {
+	printf("Root directory starts at byte %llu (sector %llu)\n",
+	       (unsigned long long)fs->root_start,
+	       (unsigned long long)fs->root_start/lss);
+	printf("%10d root directory entries\n",fs->root_entries);
+    }
+    else {
+	printf( "Root directory start at cluster %lu (arbitrary size)\n",
+		fs->root_cluster);
+    }
+    printf("Data area starts at byte %llu (sector %llu)\n",
+	   (unsigned long long)fs->data_start,
+	   (unsigned long long)fs->data_start/lss);
+    printf("%10lu data clusters (%llu bytes)\n",fs->clusters,
+	   (unsigned long long)fs->clusters*fs->cluster_size);
+    printf("%u sectors/track, %u heads\n",CF_LE_W(b->secs_track),
+	   CF_LE_W(b->heads));
+    printf("%10u hidden sectors\n",
+	   atari_format ?
+	   /* On Atari, the hidden field is only 16 bit wide and unused */
+	   (((unsigned char *)&b->hidden)[0] |
+	    ((unsigned char *)&b->hidden)[1] << 8) :
+	   CF_LE_L(b->hidden));
+    sectors = GET_UNALIGNED_W( b->sectors );
+    printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect));
+}
+
+static void check_backup_boot(DOS_FS *fs, struct boot_sector *b, int lss)
+{
+    struct boot_sector b2;
+
+    if (!fs->backupboot_start) {
+	printf( "There is no backup boot sector.\n" );
+	if (CF_LE_W(b->reserved) < 3) {
+	    printf( "And there is no space for creating one!\n" );
+	    return;
+	}
+	if (interactive)
+	    printf( "1) Create one\n2) Do without a backup\n" );
+	else printf( "  Auto-creating backup boot block.\n" );
+	if (!interactive || get_key("12","?") == '1') {
+	    int bbs;
+	    /* The usual place for the backup boot sector is sector 6. Choose
+	     * that or the last reserved sector. */
+	    if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6)
+		bbs = 6;
+	    else {
+		bbs = CF_LE_W(b->reserved) - 1;
+		if (bbs == CF_LE_W(b->info_sector))
+		    --bbs; /* this is never 0, as we checked reserved >= 3! */
+	    }
+	    fs->backupboot_start = bbs*lss;
+	    b->backup_boot = CT_LE_W(bbs);
+	    fs_write(fs->backupboot_start,sizeof(*b),b);
+	    fs_write((off_t)offsetof(struct boot_sector,backup_boot),
+		     sizeof(b->backup_boot),&b->backup_boot);
+	    printf( "Created backup of boot sector in sector %d\n", bbs );
+	    return;
+	}
+	else return;
+    }
+    
+    fs_read(fs->backupboot_start,sizeof(b2),&b2);
+    if (memcmp(b,&b2,sizeof(b2)) != 0) {
+	/* there are any differences */
+	__u8 *p, *q;
+	int i, pos, first = 1;
+	char buf[20];
+
+	printf( "There are differences between boot sector and its backup.\n" );
+	printf( "Differences: (offset:original/backup)\n  " );
+	pos = 2;
+	for( p = (__u8 *)b, q = (__u8 *)&b2, i = 0; i < sizeof(b2);
+	     ++p, ++q, ++i ) {
+	    if (*p != *q) {
+		sprintf( buf, "%s%u:%02x/%02x", first ? "" : ", ",
+			 (unsigned)(p-(__u8 *)b), *p, *q );
+		if (pos + strlen(buf) > 78) printf( "\n  " ), pos = 2;
+		printf( "%s", buf );
+		pos += strlen(buf);
+		first = 0;
+	    }
+	}
+	printf( "\n" );
+
+	if (interactive)
+	    printf( "1) Copy original to backup\n"
+		    "2) Copy backup to original\n"
+		    "3) No action\n" );
+	else printf( "  Not automatically fixing this.\n" );
+	switch (interactive ? get_key("123","?") : '3') {
+	  case '1':
+	    fs_write(fs->backupboot_start,sizeof(*b),b);
+	    break;
+	  case '2':
+	    fs_write(0,sizeof(b2),&b2);
+	    break;
+	  default:
+	    break;
+	}
+    }
+}
+
+static void init_fsinfo(struct info_sector *i)
+{
+    i->magic = CT_LE_L(0x41615252);
+    i->signature = CT_LE_L(0x61417272);
+    i->free_clusters = CT_LE_L(-1);
+    i->next_cluster = CT_LE_L(2);
+    i->boot_sign = CT_LE_W(0xaa55);
+}
+
+static void read_fsinfo(DOS_FS *fs, struct boot_sector *b,int lss)
+{
+    struct info_sector i;
+
+    if (!b->info_sector) {
+	printf( "No FSINFO sector\n" );
+	if (interactive)
+	    printf( "1) Create one\n2) Do without FSINFO\n" );
+	else printf( "  Not automatically creating it.\n" );
+	if (interactive && get_key("12","?") == '1') {
+	    /* search for a free reserved sector (not boot sector and not
+	     * backup boot sector) */
+	    __u32 s;
+	    for( s = 1; s < CF_LE_W(b->reserved); ++s )
+		if (s != CF_LE_W(b->backup_boot)) break;
+	    if (s > 0 && s < CF_LE_W(b->reserved)) {
+		init_fsinfo(&i);
+		fs_write((off_t)s*lss,sizeof(i),&i);
+		b->info_sector = CT_LE_W(s);
+		fs_write((off_t)offsetof(struct boot_sector,info_sector),
+			 sizeof(b->info_sector),&b->info_sector);
+		if (fs->backupboot_start)
+		    fs_write(fs->backupboot_start+
+			     offsetof(struct boot_sector,info_sector),
+			     sizeof(b->info_sector),&b->info_sector);
+	    }
+	    else {
+		printf( "No free reserved sector found -- "
+			"no space for FSINFO sector!\n" );
+		return;
+	    }
+	}
+	else return;
+    }
+    
+    fs->fsinfo_start = CF_LE_W(b->info_sector)*lss;
+    fs_read(fs->fsinfo_start,sizeof(i),&i);
+    
+    if (i.magic != CT_LE_L(0x41615252) ||
+	i.signature != CT_LE_L(0x61417272) ||
+	i.boot_sign != CT_LE_W(0xaa55)) {
+	printf( "FSINFO sector has bad magic number(s):\n" );
+	if (i.magic != CT_LE_L(0x41615252))
+	    printf( "  Offset %llu: 0x%08x != expected 0x%08x\n",
+		    (unsigned long long)offsetof(struct info_sector,magic),
+		    CF_LE_L(i.magic),0x41615252);
+	if (i.signature != CT_LE_L(0x61417272))
+	    printf( "  Offset %llu: 0x%08x != expected 0x%08x\n",
+		    (unsigned long long)offsetof(struct info_sector,signature),
+		    CF_LE_L(i.signature),0x61417272);
+	if (i.boot_sign != CT_LE_W(0xaa55))
+	    printf( "  Offset %llu: 0x%04x != expected 0x%04x\n",
+		    (unsigned long long)offsetof(struct info_sector,boot_sign),
+		    CF_LE_W(i.boot_sign),0xaa55);
+	if (interactive)
+	    printf( "1) Correct\n2) Don't correct (FSINFO invalid then)\n" );
+	else printf( "  Auto-correcting it.\n" );
+	if (!interactive || get_key("12","?") == '1') {
+	    init_fsinfo(&i);
+	    fs_write(fs->fsinfo_start,sizeof(i),&i);
+	}
+	else fs->fsinfo_start = 0;
+    }
+
+    if (fs->fsinfo_start)
+	fs->free_clusters = CF_LE_L(i.free_clusters);
+}
+
+void read_boot(DOS_FS *fs)
+{
+    struct boot_sector b;
+    unsigned total_sectors;
+    unsigned short logical_sector_size, sectors;
+    unsigned fat_length;
+    off_t data_size;
+
+    fs_read(0,sizeof(b),&b);
+    logical_sector_size = GET_UNALIGNED_W(b.sector_size);
+    if (!logical_sector_size) die("Logical sector size is zero.");
+    fs->cluster_size = b.cluster_size*logical_sector_size;
+    if (!fs->cluster_size) die("Cluster size is zero.");
+    if (b.fats != 2 && b.fats != 1)
+	die("Currently, only 1 or 2 FATs are supported, not %d.\n",b.fats);
+    fs->nfats = b.fats;
+    sectors = GET_UNALIGNED_W(b.sectors);
+    total_sectors = sectors ? sectors : CF_LE_L(b.total_sect);
+    if (verbose) printf("Checking we can access the last sector of the filesystem\n");
+    /* Can't access last odd sector anyway, so round down */
+    fs_test((off_t)((total_sectors & ~1)-1)*(off_t)logical_sector_size,
+	    logical_sector_size);
+    fat_length = CF_LE_W(b.fat_length) ?
+		 CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length);
+    fs->fat_start = (off_t)CF_LE_W(b.reserved)*logical_sector_size;
+    fs->root_start = ((off_t)CF_LE_W(b.reserved)+b.fats*fat_length)*
+      logical_sector_size;
+    fs->root_entries = GET_UNALIGNED_W(b.dir_entries);
+    fs->data_start = fs->root_start+ROUND_TO_MULTIPLE(fs->root_entries <<
+      MSDOS_DIR_BITS,logical_sector_size);
+    data_size = (off_t)total_sectors*logical_sector_size-fs->data_start;
+    fs->clusters = data_size/fs->cluster_size;
+    fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */
+    fs->fsinfo_start = 0; /* no FSINFO structure */
+    fs->free_clusters = -1; /* unknown */
+    if (!b.fat_length && b.fat32_length) {
+	fs->fat_bits = 32;
+	fs->root_cluster = CF_LE_L(b.root_cluster);
+	if (!fs->root_cluster && fs->root_entries)
+	    /* M$ hasn't specified this, but it looks reasonable: If
+	     * root_cluster is 0 but there is a separate root dir
+	     * (root_entries != 0), we handle the root dir the old way. Give a
+	     * warning, but convertig to a root dir in a cluster chain seems
+	     * to complex for now... */
+	    printf( "Warning: FAT32 root dir not in cluster chain! "
+		    "Compability mode...\n" );
+	else if (!fs->root_cluster && !fs->root_entries)
+	    die("No root directory!");
+	else if (fs->root_cluster && fs->root_entries)
+	    printf( "Warning: FAT32 root dir is in a cluster chain, but "
+		    "a separate root dir\n"
+		    "  area is defined. Cannot fix this easily.\n" );
+
+	fs->backupboot_start = CF_LE_W(b.backup_boot)*logical_sector_size;
+	check_backup_boot(fs,&b,logical_sector_size);
+	
+	read_fsinfo(fs,&b,logical_sector_size);
+    }
+    else if (!atari_format) {
+	/* On real MS-DOS, a 16 bit FAT is used whenever there would be too
+	 * much clusers otherwise. */
+	fs->fat_bits = (fs->clusters > MSDOS_FAT12) ? 16 : 12;
+    }
+    else {
+	/* On Atari, things are more difficult: GEMDOS always uses 12bit FATs
+	 * on floppies, and always 16 bit on harddisks. */
+	fs->fat_bits = 16; /* assume 16 bit FAT for now */
+	/* If more clusters than fat entries in 16-bit fat, we assume
+	 * it's a real MSDOS FS with 12-bit fat. */
+	if (fs->clusters+2 > fat_length*logical_sector_size*8/16 ||
+	    /* if it's a floppy disk --> 12bit fat */
+	    device_no == 2 ||
+	    /* if it's a ramdisk or loopback device and has one of the usual
+	     * floppy sizes -> 12bit FAT  */
+	    ((device_no == 1 || device_no == 7) &&
+	     (total_sectors == 720 || total_sectors == 1440 ||
+	      total_sectors == 2880)))
+	    fs->fat_bits = 12;
+    }
+    /* On FAT32, the high 4 bits of a FAT entry are reserved */
+    fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits;
+    fs->fat_size = fat_length*logical_sector_size;
+    if (fs->clusters > ((unsigned long long)fs->fat_size*8/fs->fat_bits)-2)
+	die("File system has %d clusters but only space for %d FAT entries.",
+	  fs->clusters,((unsigned long long)fs->fat_size*8/fs->fat_bits)-2);
+    if (!fs->root_entries && !fs->root_cluster)
+	die("Root directory has zero size.");
+    if (fs->root_entries & (MSDOS_DPS-1))
+	die("Root directory (%d entries) doesn't span an integral number of "
+	  "sectors.",fs->root_entries);
+    if (logical_sector_size & (SECTOR_SIZE-1))
+	die("Logical sector size (%d bytes) is not a multiple of the physical "
+	  "sector size.",logical_sector_size);
+    /* ++roman: On Atari, these two fields are often left uninitialized */
+    if (!atari_format && (!b.secs_track || !b.heads))
+	die("Invalid disk format in boot sector.");
+    if (verbose) dump_boot(fs,&b,logical_sector_size);
+}
+
+/* Local Variables: */
+/* tab-width: 8     */
+/* End:             */

Added: trunk/rosapps/sysutils/dosfsck/boot.h
--- trunk/rosapps/sysutils/dosfsck/boot.h	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/boot.h	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,13 @@
+/* boot.h  -  Read and analyze ia PC/MS-DOS boot sector */
+
+/* Written 1993 by Werner Almesberger */
+
+
+#ifndef _BOOT_H
+#define _BOOT_H
+
+void read_boot(DOS_FS *fs);
+
+/* Reads the boot sector from the currently open device and initializes *FS */
+
+#endif

Added: trunk/rosapps/sysutils/dosfsck/byteorder.h
--- trunk/rosapps/sysutils/dosfsck/byteorder.h	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/byteorder.h	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,59 @@
+#ifndef _I386_BYTEORDER_H
+#define _I386_BYTEORDER_H
+
+//#include "types.h"
+#include "compiler.h"
+
+#ifdef __GNUC__
+
+/* For avoiding bswap on i386 */
+//#ifdef __KERNEL__
+//#include <linux/config.h>
+//#endif
+
+static __inline__ __attribute_const__ __u32 ___arch__swab32(__u32 x)
+{
+#ifdef CONFIG_X86_BSWAP
+	__asm__("bswap %0" : "=r" (x) : "0" (x));
+#else
+	__asm__("xchgb %b0,%h0\n\t"	/* swap lower bytes	*/
+		"rorl $16,%0\n\t"	/* swap words		*/
+		"xchgb %b0,%h0"		/* swap higher bytes	*/
+		:"=q" (x)
+		: "0" (x));
+#endif
+	return x;
+}
+
+static __inline__ __attribute_const__ __u64 ___arch__swab64(__u64 val)
+{ 
+	union { 
+		struct { __u32 a,b; } s;
+		__u64 u;
+	} v;
+	v.u = val;
+#ifdef CONFIG_X86_BSWAP
+	asm("bswapl %0 ; bswapl %1 ; xchgl %0,%1" 
+	    : "=r" (v.s.a), "=r" (v.s.b) 
+	    : "0" (v.s.a), "1" (v.s.b)); 
+#else
+   v.s.a = ___arch__swab32(v.s.a); 
+	v.s.b = ___arch__swab32(v.s.b); 
+	asm("xchgl %0,%1" : "=r" (v.s.a), "=r" (v.s.b) : "0" (v.s.a), "1" (v.s.b));
+#endif
+	return v.u;	
+} 
+
+/* Do not define swab16.  Gcc is smart enough to recognize "C" version and
+   convert it into rotation or exhange.  */
+
+#define __arch__swab64(x) ___arch__swab64(x)
+#define __arch__swab32(x) ___arch__swab32(x)
+
+#define __BYTEORDER_HAS_U64__
+
+#endif /* __GNUC__ */
+
+//#include "little_endian.h"
+
+#endif /* _I386_BYTEORDER_H */

Added: trunk/rosapps/sysutils/dosfsck/byteswap.h
--- trunk/rosapps/sysutils/dosfsck/byteswap.h	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/byteswap.h	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,40 @@
+/* Copyright (C) 1997 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifndef _BYTESWAP_H
+#define _BYTESWAP_H	1
+
+/* Get the machine specific, optimized definitions.  */
+#include "byteswap1.h"
+
+
+/* The following definitions must all be macros since otherwise some
+   of the possible optimizations are not possible.  */
+
+/* Return a value with all bytes in the 16 bit argument swapped.  */
+#define bswap_16(x) __bswap_16 (x)
+
+/* Return a value with all bytes in the 32 bit argument swapped.  */
+#define bswap_32(x) __bswap_32 (x)
+
+#if defined __GNUC__ && __GNUC__ >= 2
+/* Return a value with all bytes in the 64 bit argument swapped.  */
+# define bswap_64(x) __bswap_64 (x)
+#endif
+
+#endif /* byteswap.h */

Added: trunk/rosapps/sysutils/dosfsck/byteswap1.h
--- trunk/rosapps/sysutils/dosfsck/byteswap1.h	2005-07-27 23:11:38 UTC (rev 16823)
+++ trunk/rosapps/sysutils/dosfsck/byteswap1.h	2005-07-28 02:37:42 UTC (rev 16824)
@@ -0,0 +1,133 @@
+/* Macros to swap the order of bytes in integer values.
+   Copyright (C) 1997, 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
[truncated at 1000 lines; 7350 more skipped]