Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers into modules, and delete rossubsys.

This commit is contained in:
Colin Finck 2017-10-03 07:45:34 +00:00
parent b94e2d8ca0
commit c2c66aff7d
24198 changed files with 0 additions and 37285 deletions

View file

@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. 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
them 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 prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. 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.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey 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;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If 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 convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU 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 that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
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.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
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.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
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
state 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) <year> <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 3 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, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program 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, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU 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 Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,39 @@
dosfstools consists of the programs mkfs.fat, fsck.fat and fatlabel to create,
check and label file systems of the FAT family. The dosfstools are licensed
under the GNU GPL version 3 or later. See the file COPYING for details.
### Build Requirements
dosfstools recommends libudev. It is used in mkfs.fat to collect additional
information about the device to format in order to refuse potentially unsafe
operations without additional confirmation.
### Installing
dosfstools are built using an autoconf/automake system, so the standard method
applies:
./configure
make
make install
You need to have superuser privileges in order to install into the standard
system wide locations.
The ./configure script has an option --enable-compat-symlinks that will
configure the build to symlink older names of the tools to the current ones on
installation. These are dosfsck, fsck.msdos and fsck.vfat for fsck.fat, mkdosfs,
mkfs.msdos and mkfs.vfat for mkfs.fat and dosfslabel for fatlabel.
### Building from the VCS repository
If you are working directly from a git clone of the official dosfstools
repository, you will find that you can not run "./configure" straight away
because it, like other autogenerated files for the build system, is not included
in the repository.
First, autoconf and automake have to be installed. Then you can run
"autoreconf -i" to generate all the required files.

View file

@ -0,0 +1,465 @@
/* boot.c - Read and analyze ia PC/MS-DOS boot sector
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
Copyright (C) 2015 Andreas Bombe <aeb@debian.org>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#include "vfatlib.h"
#define NDEBUG
#include <debug.h>
#define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0)
/* don't divide by zero */
/* cut-over cluster counts for FAT12 and FAT16 */
#define FAT12_THRESHOLD 4085
#define FAT16_THRESHOLD 65525
static struct {
uint8_t media;
const 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 __x86_64__ || defined __ppc64__
/* Unaligned fields must first be copied byte-wise (little endian) */
#define GET_UNALIGNED_W(u) \
(((unsigned char*)(&u))[0] | (((unsigned char*)&(u))[1] << 8))
#elif defined __s390x__
/* Unaligned fields must first be copied byte-wise (big endian) */
#define GET_UNALIGNED_W(pu) \
(((unsigned char*)&(u))[1] | (((unsigned char*)&(u))[0] << 8))
#else
#define GET_UNALIGNED_W(f) le16toh( *(unsigned short *)&f )
#endif
static const 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, (const char *)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", le16toh(b->reserved),
le16toh(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",
(unsigned long)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",
(unsigned long)fs->data_clusters,
(unsigned long long)fs->data_clusters * fs->cluster_size);
printf("%u sectors/track, %u heads\n", le16toh(b->secs_track),
le16toh(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) : le32toh(b->hidden));
sectors = GET_UNALIGNED_W(b->sectors);
printf("%10u sectors total\n", sectors ? sectors : le32toh(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 (le16toh(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 (le16toh(b->reserved) >= 7 && le16toh(b->info_sector) != 6)
bbs = 6;
else {
bbs = le16toh(b->reserved) - 1;
if (bbs == le16toh(b->info_sector))
--bbs; /* this is never 0, as we checked reserved >= 3! */
}
fs->backupboot_start = bbs * lss;
b->backup_boot = htole16(bbs);
fs_write(fs->backupboot_start, sizeof(*b), b);
fs_write(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 */
uint8_t *p, *q;
int i, pos, first = 1;
char buf[20];
printf("There are differences between boot sector and its backup.\n");
printf("This is mostly harmless. Differences: (offset:original/backup)\n ");
pos = 2;
for (p = (uint8_t *) b, q = (uint8_t *) & b2, i = 0; i < sizeof(b2);
++p, ++q, ++i) {
if (*p != *q) {
sprintf(buf, "%s%u:%02x/%02x", first ? "" : ", ",
(unsigned)(p - (uint8_t *) 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 = htole32(0x41615252);
i->signature = htole32(0x61417272);
i->free_clusters = htole32(-1);
i->next_cluster = htole32(2);
i->boot_sign = htole16(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) */
uint32_t s;
for (s = 1; s < le16toh(b->reserved); ++s)
if (s != le16toh(b->backup_boot))
break;
if (s > 0 && s < le16toh(b->reserved)) {
init_fsinfo(&i);
fs_write((off_t)s * lss, sizeof(i), &i);
b->info_sector = htole16(s);
fs_write(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 = le16toh(b->info_sector) * lss;
fs_read(fs->fsinfo_start, sizeof(i), &i);
if (i.magic != htole32(0x41615252) ||
i.signature != htole32(0x61417272) || i.boot_sign != htole16(0xaa55)) {
printf("FSINFO sector has bad magic number(s):\n");
if (i.magic != htole32(0x41615252))
printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
(unsigned long long)offsetof(struct info_sector, magic),
le32toh(i.magic), 0x41615252);
if (i.signature != htole32(0x61417272))
printf(" Offset %llu: 0x%08x != expected 0x%08x\n",
(unsigned long long)offsetof(struct info_sector, signature),
le32toh(i.signature), 0x61417272);
if (i.boot_sign != htole16(0xaa55))
printf(" Offset %llu: 0x%04x != expected 0x%04x\n",
(unsigned long long)offsetof(struct info_sector, boot_sign),
le16toh(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 = le32toh(i.free_clusters);
}
static char print_fat_dirty_state(void)
{
printf("Dirty bit is set. Fs was not properly unmounted and"
" some data may be corrupt.\n");
if (interactive) {
printf("1) Remove dirty bit\n" "2) No action\n");
return get_key("12", "?");
} else
printf(" Automatically removing dirty bit.\n");
return '1';
}
static void check_fat_state_bit(DOS_FS * fs, void *b)
{
if (fs->fat_bits == 32) {
struct boot_sector *b32 = b;
if (b32->reserved3 & FAT_STATE_DIRTY) {
printf("0x41: ");
if (print_fat_dirty_state() == '1') {
b32->reserved3 &= ~FAT_STATE_DIRTY;
fs_write(0, sizeof(*b32), b32);
}
}
} else {
struct boot_sector_16 *b16 = b;
if (b16->reserved2 & FAT_STATE_DIRTY) {
printf("0x25: ");
if (print_fat_dirty_state() == '1') {
b16->reserved2 &= ~FAT_STATE_DIRTY;
fs_write(0, sizeof(*b16), b16);
}
}
}
}
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.");
/* This was moved up because it's the first thing that will fail */
/* if the platform needs special handling of unaligned multibyte accesses */
/* but such handling isn't being provided. See GET_UNALIGNED_W() above. */
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);
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 : le32toh(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) * logical_sector_size,
logical_sector_size);
fat_length = le16toh(b.fat_length) ?
le16toh(b.fat_length) : le32toh(b.fat32_length);
fs->fat_start = (off_t)le16toh(b.reserved) * logical_sector_size;
fs->root_start = ((off_t)le16toh(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->data_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 = le32toh(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! "
"Compatibility 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");
if (fs->data_clusters < FAT16_THRESHOLD)
printf("Warning: Filesystem is FAT32 according to fat_length "
"and fat32_length fields,\n"
" but has only %lu clusters, less than the required "
"minimum of %d.\n"
" This may lead to problems on some systems.\n",
(unsigned long)fs->data_clusters, FAT16_THRESHOLD);
check_fat_state_bit(fs, &b);
fs->backupboot_start = le16toh(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->data_clusters >= FAT12_THRESHOLD) ? 16 : 12;
if (fs->data_clusters >= FAT16_THRESHOLD)
die("Too many clusters (%lu) for FAT16 filesystem.", fs->data_clusters);
check_fat_state_bit(fs, &b);
} 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->data_clusters + 2 > fat_length * logical_sector_size * 8 / 16 ||
/* if it has one of the usual floppy sizes -> 12bit FAT */
(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;
fs->label = calloc(12, sizeof(uint8_t));
if (fs->fat_bits == 12 || fs->fat_bits == 16) {
struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b;
if (b16->extended_sig == 0x29)
memmove(fs->label, b16->label, 11);
else {
free(fs->label);
fs->label = NULL;
}
} else if (fs->fat_bits == 32) {
if (b.extended_sig == 0x29)
memmove(fs->label, &b.label, 11);
else {
free(fs->label);
fs->label = NULL;
}
}
if (fs->data_clusters >
((uint64_t)fs->fat_size * 8 / fs->fat_bits) - 2)
die("Filesystem has %d clusters but only space for %d FAT entries.",
fs->data_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);
#if 0 /* linux kernel doesn't check that either */
/* ++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.");
#endif
if (verbose)
dump_boot(fs, &b, logical_sector_size);
}

View file

@ -0,0 +1,30 @@
/* boot.h - Read and analyze ia PC/MS-DOS boot sector
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#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

View file

@ -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 */

View file

@ -0,0 +1,41 @@
/* 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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, 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 */

View file

@ -0,0 +1,134 @@
/* 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.
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., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#if !defined _BYTESWAP_H && !defined _NETINET_IN_H
# error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
#endif
#ifndef _BITS_BYTESWAP_H
#define _BITS_BYTESWAP_H 1
/* Swap bytes in 16 bit value. */
#define __bswap_constant_16(x) \
((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
#ifdef __GNUC__
# if __GNUC__ >= 2
# define __bswap_16(x) \
(__extension__ \
({ register unsigned short int __v, __x = (x); \
if (__builtin_constant_p (__x)) \
__v = __bswap_constant_16 (__x); \
else \
__asm__ ("rorw $8, %w0" \
: "=r" (__v) \
: "0" (__x) \
: "cc"); \
__v; }))
# else
/* This is better than nothing. */
# define __bswap_16(x) \
(__extension__ \
({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
# endif
#else
static __inline unsigned short int
__bswap_16 (unsigned short int __bsx)
{
return __bswap_constant_16 (__bsx);
}
#endif
/* Swap bytes in 32 bit value. */
#define __bswap_constant_32(x) \
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
#ifdef __GNUC__
# if __GNUC__ >= 2
/* To swap the bytes in a word the i486 processors and up provide the
`bswap' opcode. On i386 we have to use three instructions. */
# if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ \
&& !defined __pentium4__
# define __bswap_32(x) \
(__extension__ \
({ register unsigned int __v, __x = (x); \
if (__builtin_constant_p (__x)) \
__v = __bswap_constant_32 (__x); \
else \
__asm__ ("rorw $8, %w0;" \
"rorl $16, %0;" \
"rorw $8, %w0" \
: "=r" (__v) \
: "0" (__x) \
: "cc"); \
__v; }))
# else
# define __bswap_32(x) \
(__extension__ \
({ register unsigned int __v, __x = (x); \
if (__builtin_constant_p (__x)) \
__v = __bswap_constant_32 (__x); \
else \
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \
__v; }))
# endif
# else
# define __bswap_32(x) \
(__extension__ \
({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
# endif
#else
static __inline unsigned int
__bswap_32 (unsigned int __bsx)
{
return __bswap_constant_32 (__bsx);
}
#endif
#if defined __GNUC__ && __GNUC__ >= 2
/* Swap bytes in 64 bit value. */
#define __bswap_constant_64(x) \
((((x) & 0xff00000000000000ull) >> 56) \
| (((x) & 0x00ff000000000000ull) >> 40) \
| (((x) & 0x0000ff0000000000ull) >> 24) \
| (((x) & 0x000000ff00000000ull) >> 8) \
| (((x) & 0x00000000ff000000ull) << 8) \
| (((x) & 0x0000000000ff0000ull) << 24) \
| (((x) & 0x000000000000ff00ull) << 40) \
| (((x) & 0x00000000000000ffull) << 56))
# define __bswap_64(x) \
(__extension__ \
({ union { __extension__ unsigned long long int __ll; \
unsigned long int __l[2]; } __w, __r; \
if (__builtin_constant_p (x)) \
__r.__ll = __bswap_constant_64 (x); \
else \
{ \
__w.__ll = (x); \
__r.__l[0] = __bswap_32 (__w.__l[1]); \
__r.__l[1] = __bswap_32 (__w.__l[0]); \
} \
__r.__ll; }))
#endif
#endif /* _BITS_BYTESWAP_H */

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,40 @@
/* check.h - Check and repair a PC/MS-DOS filesystem
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#ifndef _CHECK_H
#define _CHECK_H
off_t alloc_rootdir_entry(DOS_FS * fs, DIR_ENT * de, const char *pattern);
/* Allocate a free slot in the root directory for a new file. The file name is
constructed after 'pattern', which must include a %d type format for printf
and expand to exactly 11 characters. The name actually used is written into
the 'de' structure, the rest of *de is cleared. The offset returned is to
where in the filesystem the entry belongs. */
int scan_root(DOS_FS * fs);
/* Scans the root directory and recurses into all subdirectories. See check.c
for all the details. Returns a non-zero integer if the filesystem has to
be checked again. */
#endif

View file

@ -0,0 +1,175 @@
/****
** Platform-dependent file
****/
/* common.c - Common functions
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#include "vfatlib.h"
#define NDEBUG
#include <debug.h>
typedef struct _link {
void *data;
struct _link *next;
} LINK;
DECLSPEC_NORETURN // __attribute((noreturn))
void exit(int exitcode)
{
// DbgBreakPoint();
NtTerminateProcess(NtCurrentProcess(), exitcode);
/* Should never get here */
while(1);
}
DECLSPEC_NORETURN // __attribute((noreturn))
void die_func(const char *msg, ...) // die
{
va_list args;
va_start(args, msg);
// vfprintf(stderr, msg, args);
DPRINT1("Unrecoverable problem!\n");
VfatPrintV((char*)msg, args);
va_end(args);
// // fprintf(stderr, "\n");
// VfatPrint("\n");
exit(1);
}
DECLSPEC_NORETURN // __attribute((noreturn))
void pdie_func(const char *msg, ...) // pdie
{
va_list args;
va_start(args, msg);
// vfprintf(stderr, msg, args);
DPRINT1("Unrecoverable problem!\n");
VfatPrintV((char*)msg, args);
va_end(args);
// // fprintf(stderr, ":%s\n", strerror(errno));
// // VfatPrint(":%s\n", strerror(errno));
// VfatPrint("\n");
exit(1);
}
void *vfalloc(size_t size)
{
void *ptr;
ptr = RtlAllocateHeap(RtlGetProcessHeap(), 0, size);
if (ptr == NULL)
{
DPRINT1("Allocation failed!\n");
pdie("malloc");
return NULL;
}
return ptr;
}
void *vfcalloc(size_t size, size_t count)
{
void *ptr;
ptr = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, size * count);
if (ptr == NULL)
{
DPRINT1("Allocation failed!\n");
return NULL;
}
return ptr;
}
void vffree(void *ptr)
{
RtlFreeHeap(RtlGetProcessHeap(), 0, ptr);
}
void *qalloc(void **root, int size)
{
LINK *link;
link = vfalloc(sizeof(LINK));
link->next = *root;
*root = link;
return link->data = vfalloc(size);
}
void qfree(void **root)
{
LINK *this;
while (*root) {
this = (LINK *) * root;
*root = this->next;
vffree(this->data);
vffree(this);
}
}
#ifdef min
#undef min
#endif
int min(int a,int b)
{
return a < b ? a : b;
}
char get_key(const char *valid, const char *prompt)
{
#if 0
int ch, okay;
while (1) {
if (prompt)
printf("%s ", prompt);
fflush(stdout);
while (ch = getchar(), ch == ' ' || ch == '\t') ;
if (ch == EOF)
exit(1);
if (!strchr(valid, okay = ch))
okay = 0;
while (ch = getchar(), ch != '\n' && ch != EOF) ;
if (ch == EOF)
exit(1);
if (okay)
return okay;
printf("Invalid input.\n");
}
#else
return 0;
#endif
}

View file

@ -0,0 +1,73 @@
/****
** Platform-dependent file
****/
/* common.h - Common functions
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#ifndef _COMMON_H
#define _COMMON_H
DECLSPEC_NORETURN // __attribute((noreturn))
// void die(const char *msg, ...);
void die_func(const char *msg, ...);
#define die(msg, ...) \
do { \
die_func("DIE! (%s:%d) " msg "\n", __RELFILE__, __LINE__, ##__VA_ARGS__); \
} while (0)
/* Displays a prinf-style message and terminates the program. */
DECLSPEC_NORETURN // __attribute((noreturn))
// void pdie(const char *msg, ...);
void pdie_func(const char *msg, ...);
#define pdie(msg, ...) \
do { \
pdie_func("P-DIE! (%s:%d) " msg "\n", __RELFILE__, __LINE__, ##__VA_ARGS__); \
} while (0)
/* Like die, but appends an error message according to the state of errno. */
void *vfalloc(size_t size);
void *vfcalloc(size_t size, size_t count);
void vffree(void *ptr);
/* mallocs SIZE bytes and returns a pointer to the data. Terminates the program
if malloc fails. */
void *qalloc(void **root, int size);
/* Like alloc, but registers the data area in a list described by ROOT. */
void qfree(void **root);
/* Deallocates all qalloc'ed data areas described by ROOT. */
//int min(int a,int b);
/* Returns the smaller integer value of a and b. */
char get_key(const char *valid, const char *prompt);
/* Displays PROMPT and waits for user input. Only characters in VALID are
accepted. Terminates the program on EOF. Returns the character. */
#endif

View file

@ -0,0 +1,158 @@
#ifndef __LINUX_COMPILER_H
#define __LINUX_COMPILER_H
#ifndef __ASSEMBLY__
#ifdef __CHECKER__
# define __user __attribute__((noderef, address_space(1)))
# define __kernel /* default address space */
# define __safe __attribute__((safe))
# define __force __attribute__((force))
# define __nocast __attribute__((nocast))
# define __iomem __attribute__((noderef, address_space(2)))
# define __acquires(x) __attribute__((context(0,1)))
# define __releases(x) __attribute__((context(1,0)))
# define __acquire(x) __context__(1)
# define __release(x) __context__(-1)
# define __cond_lock(x) ((x) ? ({ __context__(1); 1; }) : 0)
extern void __chk_user_ptr(void __user *);
extern void __chk_io_ptr(void __iomem *);
#else
# define __user
# define __kernel
# define __safe
# define __force
# define __nocast
# define __iomem
# define __chk_user_ptr(x) (void)0
# define __chk_io_ptr(x) (void)0
//# define __builtin_warning(x, y...) (1)
# define __acquires(x)
# define __releases(x)
# define __acquire(x) (void)0
# define __release(x) (void)0
# define __cond_lock(x) (x)
#endif
#ifdef __KERNEL__
#if __GNUC__ > 4
#error no compiler-gcc.h file for this gcc version
#elif __GNUC__ == 4
# include <linux/compiler-gcc4.h>
#elif __GNUC__ == 3
# include <linux/compiler-gcc3.h>
#elif __GNUC__ == 2
# include <linux/compiler-gcc2.h>
#else
# error Sorry, your compiler is too old/not recognized.
#endif
/* Intel compiler defines __GNUC__. So we will overwrite implementations
* coming from above header files here
*/
#ifdef __INTEL_COMPILER
# include <linux/compiler-intel.h>
#endif
/*
* Generic compiler-dependent macros required for kernel
* build go below this comment. Actual compiler/compiler version
* specific implementations come from the above header files
*/
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
/* Optimization barrier */
#ifndef barrier
# define barrier() __memory_barrier()
#endif
#ifndef RELOC_HIDE
# define RELOC_HIDE(ptr, off) \
({ unsigned long __ptr; \
__ptr = (unsigned long) (ptr); \
(typeof(ptr)) (__ptr + (off)); })
#endif
#endif /* __KERNEL__ */
#endif /* __ASSEMBLY__ */
/*
* Allow us to mark functions as 'deprecated' and have gcc emit a nice
* warning for each use, in hopes of speeding the functions removal.
* Usage is:
* int __deprecated foo(void)
*/
#ifndef __deprecated
# define __deprecated /* unimplemented */
#endif
#ifdef MODULE
#define __deprecated_for_modules __deprecated
#else
#define __deprecated_for_modules
#endif
#ifndef __must_check
#define __must_check
#endif
/*
* Allow us to avoid 'defined but not used' warnings on functions and data,
* as well as force them to be emitted to the assembly file.
*
* As of gcc 3.3, static functions that are not marked with attribute((used))
* may be elided from the assembly file. As of gcc 3.3, static data not so
* marked will not be elided, but this may change in a future gcc version.
*
* In prior versions of gcc, such functions and data would be emitted, but
* would be warned about except with attribute((unused)).
*/
#ifndef __attribute_used__
# define __attribute_used__ /* unimplemented */
#endif
/*
* From the GCC manual:
*
* Many functions have no effects except the return value and their
* return value depends only on the parameters and/or global
* variables. Such a function can be subject to common subexpression
* elimination and loop optimization just as an arithmetic operator
* would be.
* [...]
*/
#ifndef __attribute_pure__
# define __attribute_pure__ /* unimplemented */
#endif
/*
* From the GCC manual:
*
* Many functions do not examine any values except their arguments,
* and have no effects except the return value. Basically this is
* just slightly more strict class than the `pure' attribute above,
* since function is not allowed to read global memory.
*
* Note that a function that has pointer arguments and examines the
* data pointed to must _not_ be declared `const'. Likewise, a
* function that calls a non-`const' function usually must not be
* `const'. It does not make sense for a `const' function to return
* `void'.
*/
#ifndef __attribute_const__
# define __attribute_const__ /* unimplemented */
#endif
#ifndef noinline
#define noinline
#endif
#ifndef __always_inline
#define __always_inline inline
#endif
#endif /* __LINUX_COMPILER_H */

View file

@ -0,0 +1,14 @@
/* dosfsck.h - Interface with vfatlib.h */
/* Redefine off_t as a 'long long' */
// typedef __int64 off_t;
#define off_t __int64
#include "check/common.h"
#include "check/fsck.fat.h"
#include "check/io.h"
#include "check/boot.h"
#include "check/check.h"
#include "check/fat.h"
#include "check/file.h"
#include "check/lfn.h"

View file

@ -0,0 +1,571 @@
/* fat.c - Read/write access to the FAT
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#include "vfatlib.h"
#define NDEBUG
#include <debug.h>
/**
* Fetch the FAT entry for a specified cluster.
*
* @param[out] entry Cluster to which cluster of interest is linked
* @param[in] fat FAT table for the partition
* @param[in] cluster Cluster of interest
* @param[in] fs Information from the FAT boot sectors (bits per FAT entry)
*/
void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs)
{
unsigned char *ptr;
if (cluster > fs->data_clusters + 1) {
die("Internal error: cluster out of range in get_fat() (%lu > %lu).",
(unsigned long)cluster, (unsigned long)(fs->data_clusters + 1));
}
switch (fs->fat_bits) {
case 12:
ptr = &((unsigned char *)fat)[cluster * 3 / 2];
entry->value = 0xfff & (cluster & 1 ? (ptr[0] >> 4) | (ptr[1] << 4) :
(ptr[0] | ptr[1] << 8));
break;
case 16:
entry->value = le16toh(((unsigned short *)fat)[cluster]);
break;
case 32:
/* According to M$, the high 4 bits of a FAT32 entry are reserved and
* are not part of the cluster number. So we cut them off. */
{
uint32_t e = le32toh(((unsigned int *)fat)[cluster]);
entry->value = e & 0xfffffff;
entry->reserved = e >> 28;
}
break;
default:
die("Bad FAT entry size: %d bits.", fs->fat_bits);
}
}
/**
* Build a bookkeeping structure from the partition's FAT table.
* If the partition has multiple FATs and they don't agree, try to pick a winner,
* and queue a command to overwrite the loser.
* One error that is fixed here is a cluster that links to something out of range.
*
* @param[inout] fs Information about the filesystem
*/
void read_fat(DOS_FS * fs)
{
int eff_size, alloc_size;
uint32_t i;
void *first, *second = NULL;
int first_ok, second_ok;
uint32_t total_num_clusters;
/* Clean up from previous pass */
if (fs->fat)
free(fs->fat);
if (fs->cluster_owner)
free(fs->cluster_owner);
fs->fat = NULL;
fs->cluster_owner = NULL;
total_num_clusters = fs->data_clusters + 2;
eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL;
if (fs->fat_bits != 12)
alloc_size = eff_size;
else
/* round up to an even number of FAT entries to avoid special
* casing the last entry in get_fat() */
alloc_size = (total_num_clusters * 12 + 23) / 24 * 3;
first = alloc(alloc_size);
fs_read(fs->fat_start, eff_size, first);
if (fs->nfats > 1) {
second = alloc(alloc_size);
fs_read(fs->fat_start + fs->fat_size, eff_size, second);
}
if (second && memcmp(first, second, eff_size) != 0) {
FAT_ENTRY first_media, second_media;
get_fat(&first_media, first, 0, fs);
get_fat(&second_media, second, 0, fs);
first_ok = (first_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
second_ok = (second_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs);
if (first_ok && !second_ok) {
printf("FATs differ - using first FAT.\n");
fs_write(fs->fat_start + fs->fat_size, eff_size, first);
}
if (!first_ok && second_ok) {
printf("FATs differ - using second FAT.\n");
fs_write(fs->fat_start, eff_size, second);
memcpy(first, second, eff_size);
}
if (first_ok && second_ok) {
if (interactive) {
printf("FATs differ but appear to be intact. Use which FAT ?\n"
"1) Use first FAT\n2) Use second FAT\n");
if (get_key("12", "?") == '1') {
fs_write(fs->fat_start + fs->fat_size, eff_size, first);
} else {
fs_write(fs->fat_start, eff_size, second);
memcpy(first, second, eff_size);
}
} else {
printf("FATs differ but appear to be intact. Using first "
"FAT.\n");
fs_write(fs->fat_start + fs->fat_size, eff_size, first);
}
}
if (!first_ok && !second_ok) {
printf("Both FATs appear to be corrupt. Giving up.\n");
exit(1);
}
}
if (second) {
free(second);
}
fs->fat = (unsigned char *)first;
fs->cluster_owner = alloc(total_num_clusters * sizeof(DOS_FILE *));
memset(fs->cluster_owner, 0, (total_num_clusters * sizeof(DOS_FILE *)));
/* Truncate any cluster chains that link to something out of range */
for (i = 2; i < fs->data_clusters + 2; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
if (curEntry.value == 1) {
printf("Cluster %ld out of range (1). Setting to EOF.\n",
(long)(i - 2));
set_fat(fs, i, -1);
}
if (curEntry.value >= fs->data_clusters + 2 &&
(curEntry.value < FAT_MIN_BAD(fs))) {
printf("Cluster %ld out of range (%ld > %ld). Setting to EOF.\n",
(long)(i - 2), (long)curEntry.value,
(long)(fs->data_clusters + 2 - 1));
set_fat(fs, i, -1);
}
}
}
/**
* Update the FAT entry for a specified cluster
* (i.e., change the cluster it links to).
* Queue a command to write out this change.
*
* @param[in,out] fs Information about the filesystem
* @param[in] cluster Cluster to change
* @param[in] new Cluster to link to
* Special values:
* 0 == free cluster
* -1 == end-of-chain
* -2 == bad cluster
*/
void set_fat(DOS_FS * fs, uint32_t cluster, int32_t new)
{
unsigned char *data = NULL;
int size;
off_t offs;
if (cluster > fs->data_clusters + 1) {
die("Internal error: cluster out of range in set_fat() (%lu > %lu).",
(unsigned long)cluster, (unsigned long)(fs->data_clusters + 1));
}
if (new == -1)
new = FAT_EOF(fs);
else if ((long)new == -2)
new = FAT_BAD(fs);
else if (new > fs->data_clusters + 1) {
die("Internal error: new cluster out of range in set_fat() (%lu > %lu).",
(unsigned long)new, (unsigned long)(fs->data_clusters + 1));
}
switch (fs->fat_bits) {
case 12:
data = fs->fat + cluster * 3 / 2;
offs = fs->fat_start + cluster * 3 / 2;
if (cluster & 1) {
FAT_ENTRY prevEntry;
get_fat(&prevEntry, fs->fat, cluster - 1, fs);
data[0] = ((new & 0xf) << 4) | (prevEntry.value >> 8);
data[1] = new >> 4;
} else {
FAT_ENTRY subseqEntry;
if (cluster != fs->data_clusters + 1)
get_fat(&subseqEntry, fs->fat, cluster + 1, fs);
else
subseqEntry.value = 0;
data[0] = new & 0xff;
data[1] = (new >> 8) | ((0xff & subseqEntry.value) << 4);
}
size = 2;
break;
case 16:
data = fs->fat + cluster * 2;
offs = fs->fat_start + cluster * 2;
*(unsigned short *)data = htole16(new);
size = 2;
break;
case 32:
{
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, cluster, fs);
data = fs->fat + cluster * 4;
offs = fs->fat_start + cluster * 4;
/* According to M$, the high 4 bits of a FAT32 entry are reserved and
* are not part of the cluster number. So we never touch them. */
*(uint32_t *)data = htole32((new & 0xfffffff) |
(curEntry.reserved << 28));
size = 4;
}
break;
default:
die("Bad FAT entry size: %d bits.", fs->fat_bits);
}
fs_write(offs, size, data);
if (fs->nfats > 1) {
fs_write(offs + fs->fat_size, size, data);
}
}
int bad_cluster(DOS_FS * fs, uint32_t cluster)
{
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, cluster, fs);
return FAT_IS_BAD(fs, curEntry.value);
}
/**
* Get the cluster to which the specified cluster is linked.
* If the linked cluster is marked bad, abort.
*
* @param[in] fs Information about the filesystem
* @param[in] cluster Cluster to follow
*
* @return -1 'cluster' is at the end of the chain
* @return Other values Next cluster in this chain
*/
uint32_t next_cluster(DOS_FS * fs, uint32_t cluster)
{
uint32_t value;
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, cluster, fs);
value = curEntry.value;
if (FAT_IS_BAD(fs, value))
die("Internal error: next_cluster on bad cluster");
return FAT_IS_EOF(fs, value) ? -1 : value;
}
off_t cluster_start(DOS_FS * fs, uint32_t cluster)
{
return fs->data_start + ((off_t)cluster - 2) * (uint64_t)fs->cluster_size;
}
/**
* Update internal bookkeeping to show that the specified cluster belongs
* to the specified dentry.
*
* @param[in,out] fs Information about the filesystem
* @param[in] cluster Cluster being assigned
* @param[in] owner Information on dentry that owns this cluster
* (may be NULL)
*/
void set_owner(DOS_FS * fs, uint32_t cluster, DOS_FILE * owner)
{
if (fs->cluster_owner == NULL)
die("Internal error: attempt to set owner in non-existent table");
if (owner && fs->cluster_owner[cluster]
&& (fs->cluster_owner[cluster] != owner))
die("Internal error: attempt to change file owner");
fs->cluster_owner[cluster] = owner;
}
DOS_FILE *get_owner(DOS_FS * fs, uint32_t cluster)
{
if (fs->cluster_owner == NULL)
return NULL;
else
return fs->cluster_owner[cluster];
}
void fix_bad(DOS_FS * fs)
{
uint32_t i;
if (verbose)
printf("Checking for bad clusters.\n");
for (i = 2; i < fs->data_clusters + 2; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value))
if (!fs_test(cluster_start(fs, i), fs->cluster_size)) {
printf("Cluster %lu is unreadable.\n", (unsigned long)i);
set_fat(fs, i, -2);
}
}
}
void reclaim_free(DOS_FS * fs)
{
int reclaimed;
uint32_t i;
if (verbose)
printf("Checking for unused clusters.\n");
reclaimed = 0;
for (i = 2; i < fs->data_clusters + 2; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
if (!get_owner(fs, i) && curEntry.value &&
!FAT_IS_BAD(fs, curEntry.value)) {
set_fat(fs, i, 0);
reclaimed++;
}
}
if (reclaimed)
printf("Reclaimed %d unused cluster%s (%llu bytes).\n", (int)reclaimed,
reclaimed == 1 ? "" : "s",
(unsigned long long)reclaimed * fs->cluster_size);
}
/**
* Assign the specified owner to all orphan chains (except cycles).
* Break cross-links between orphan chains.
*
* @param[in,out] fs Information about the filesystem
* @param[in] owner dentry to be assigned ownership of orphans
* @param[in,out] num_refs For each orphan cluster [index], how many
* clusters link to it.
* @param[in] start_cluster Where to start scanning for orphans
*/
static void tag_free(DOS_FS * fs, DOS_FILE * owner, uint32_t *num_refs,
uint32_t start_cluster)
{
int prev;
uint32_t i, walk;
if (start_cluster == 0)
start_cluster = 2;
for (i = start_cluster; i < fs->data_clusters + 2; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
/* If the current entry is the head of an un-owned chain... */
if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) &&
!get_owner(fs, i) && !num_refs[i]) {
prev = 0;
/* Walk the chain, claiming ownership as we go */
for (walk = i; walk != -1; walk = next_cluster(fs, walk)) {
if (!get_owner(fs, walk)) {
set_owner(fs, walk, owner);
} else {
/* We've run into cross-links between orphaned chains,
* or a cycle with a tail.
* Terminate this orphan chain (break the link)
*/
set_fat(fs, prev, -1);
/* This is not necessary because 'walk' is owned and thus
* will never become the head of a chain (the only case
* that would matter during reclaim to files).
* It's easier to decrement than to prove that it's
* unnecessary.
*/
num_refs[walk]--;
break;
}
prev = walk;
}
}
}
}
/**
* Recover orphan chains to files, handling any cycles or cross-links.
*
* @param[in,out] fs Information about the filesystem
*/
void reclaim_file(DOS_FS * fs)
{
DOS_FILE orphan;
int reclaimed, files;
int changed = 0;
uint32_t i, next, walk;
uint32_t *num_refs = NULL; /* Only for orphaned clusters */
uint32_t total_num_clusters;
if (verbose)
printf("Reclaiming unconnected clusters.\n");
total_num_clusters = fs->data_clusters + 2;
num_refs = alloc(total_num_clusters * sizeof(uint32_t));
memset(num_refs, 0, (total_num_clusters * sizeof(uint32_t)));
/* Guarantee that all orphan chains (except cycles) end cleanly
* with an end-of-chain mark.
*/
for (i = 2; i < total_num_clusters; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
next = curEntry.value;
if (!get_owner(fs, i) && next && next < fs->data_clusters + 2) {
/* Cluster is linked, but not owned (orphan) */
FAT_ENTRY nextEntry;
get_fat(&nextEntry, fs->fat, next, fs);
/* Mark it end-of-chain if it links into an owned cluster,
* a free cluster, or a bad cluster.
*/
if (get_owner(fs, next) || !nextEntry.value ||
FAT_IS_BAD(fs, nextEntry.value))
set_fat(fs, i, -1);
else
num_refs[next]++;
}
}
/* Scan until all the orphans are accounted for,
* and all cycles and cross-links are broken
*/
do {
tag_free(fs, &orphan, num_refs, changed);
changed = 0;
/* Any unaccounted-for orphans must be part of a cycle */
for (i = 2; i < total_num_clusters; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) &&
!get_owner(fs, i)) {
if (!num_refs[curEntry.value]--)
die("Internal error: num_refs going below zero");
set_fat(fs, i, -1);
changed = curEntry.value;
printf("Broke cycle at cluster %lu in free chain.\n", (unsigned long)i);
/* If we've created a new chain head,
* tag_free() can claim it
*/
if (num_refs[curEntry.value] == 0)
break;
}
}
}
while (changed);
/* Now we can start recovery */
files = reclaimed = 0;
for (i = 2; i < total_num_clusters; i++)
/* If this cluster is the head of an orphan chain... */
if (get_owner(fs, i) == &orphan && !num_refs[i]) {
DIR_ENT de;
off_t offset;
files++;
offset = alloc_rootdir_entry(fs, &de, "FSCK%04dREC");
de.start = htole16(i & 0xffff);
if (fs->fat_bits == 32)
de.starthi = htole16(i >> 16);
for (walk = i; walk > 0 && walk != -1;
walk = next_cluster(fs, walk)) {
de.size = htole32(le32toh(de.size) + fs->cluster_size);
reclaimed++;
}
fs_write(offset, sizeof(DIR_ENT), &de);
}
if (reclaimed)
printf("Reclaimed %d unused cluster%s (%llu bytes) in %d chain%s.\n",
reclaimed, reclaimed == 1 ? "" : "s",
(unsigned long long)reclaimed * fs->cluster_size, files,
files == 1 ? "" : "s");
free(num_refs);
}
uint32_t update_free(DOS_FS * fs)
{
uint32_t i;
uint32_t free = 0;
int do_set = 0;
for (i = 2; i < fs->data_clusters + 2; i++) {
FAT_ENTRY curEntry;
get_fat(&curEntry, fs->fat, i, fs);
if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value))
++free;
}
if (!fs->fsinfo_start)
return free;
if (verbose)
printf("Checking free cluster summary.\n");
if (fs->free_clusters != 0xFFFFFFFF) {
if (free != fs->free_clusters) {
printf("Free cluster summary wrong (%ld vs. really %ld)\n",
(long)fs->free_clusters, (long)free);
if (interactive)
printf("1) Correct\n2) Don't correct\n");
else
printf(" Auto-correcting.\n");
if (!interactive || get_key("12", "?") == '1')
do_set = 1;
}
} else {
printf("Free cluster summary uninitialized (should be %ld)\n", (long)free);
if (rw) {
if (interactive)
printf("1) Set it\n2) Leave it uninitialized\n");
else
printf(" Auto-setting.\n");
if (!interactive || get_key("12", "?") == '1')
do_set = 1;
}
}
if (do_set) {
uint32_t le_free = htole32(free);
fs->free_clusters = free;
fs_write(fs->fsinfo_start + offsetof(struct info_sector, free_clusters),
sizeof(le_free), &le_free);
}
return free;
}

View file

@ -0,0 +1,85 @@
/* fat.h - Read/write access to the FAT
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
THe complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#ifndef _FAT_H
#define _FAT_H
void read_fat(DOS_FS * fs);
/* Loads the FAT of the filesystem described by FS. Initializes the FAT,
replaces broken FATs and rejects invalid cluster entries. */
void get_fat(FAT_ENTRY * entry, void *fat, uint32_t cluster, DOS_FS * fs);
/* Retrieve the FAT entry (next chained cluster) for CLUSTER. */
void set_fat(DOS_FS * fs, uint32_t cluster, int32_t new);
/* Changes the value of the CLUSTERth cluster of the FAT of FS to NEW. Special
values of NEW are -1 (EOF, 0xff8 or 0xfff8) and -2 (bad sector, 0xff7 or
0xfff7) */
int bad_cluster(DOS_FS * fs, uint32_t cluster);
/* Returns a non-zero integer if the CLUSTERth cluster is marked as bad or zero
otherwise. */
uint32_t next_cluster(DOS_FS * fs, uint32_t cluster);
/* Returns the number of the cluster following CLUSTER, or -1 if this is the
last cluster of the respective cluster chain. CLUSTER must not be a bad
cluster. */
off_t cluster_start(DOS_FS * fs, uint32_t cluster);
/* Returns the byte offset of CLUSTER, relative to the respective device. */
void set_owner(DOS_FS * fs, uint32_t cluster, DOS_FILE * owner);
/* Sets the owner pointer of the respective cluster to OWNER. If OWNER was NULL
before, it can be set to NULL or any non-NULL value. Otherwise, only NULL is
accepted as the new value. */
DOS_FILE *get_owner(DOS_FS * fs, uint32_t cluster);
/* Returns the owner of the repective cluster or NULL if the cluster has no
owner. */
void fix_bad(DOS_FS * fs);
/* Scans the disk for currently unused bad clusters and marks them as bad. */
void reclaim_free(DOS_FS * fs);
/* Marks all allocated, but unused clusters as free. */
void reclaim_file(DOS_FS * fs);
/* Scans the FAT for chains of allocated, but unused clusters and creates files
for them in the root directory. Also tries to fix all inconsistencies (e.g.
loops, shared clusters, etc.) in the process. */
uint32_t update_free(DOS_FS * fs);
/* Updates free cluster count in FSINFO sector. */
#endif

View file

@ -0,0 +1,267 @@
/* file.c - Additional file attributes
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#include "vfatlib.h"
#define NDEBUG
#include <debug.h>
FDSC *fp_root = NULL;
static void put_char(char **p, unsigned char c)
{
// if (dos_char_to_printable(p, c))
// return;
if ((c >= ' ' && c < 0x7f) || c >= 0xa0)
*(*p)++ = c;
else {
*(*p)++ = '\\';
*(*p)++ = '0' + (c >> 6);
*(*p)++ = '0' + ((c >> 3) & 7);
*(*p)++ = '0' + (c & 7);
}
}
/**
* Construct the "pretty-printed" representation of the name in a short directory entry.
*
* @param[in] fixed Pointer to name[0] of a DIR_ENT
*
* @return Pointer to static string containing pretty "8.3" equivalent of the
* name in the directory entry.
*/
char *file_name(unsigned char *fixed)
{
static char path[MSDOS_NAME * 4 + 2];
char *p;
int i, j;
p = path;
for (i = j = 0; i < 8; i++)
if (fixed[i] != ' ') {
while (j++ < i)
*p++ = ' ';
put_char(&p, fixed[i]);
}
if (strncmp((const char *)(fixed + 8), " ", 3)) {
*p++ = '.';
for (i = j = 0; i < 3; i++)
if (fixed[i + 8] != ' ') {
while (j++ < i)
*p++ = ' ';
put_char(&p, fixed[i + 8]);
}
}
*p = 0;
return path;
}
int file_cvt(unsigned char *name, unsigned char *fixed)
{
unsigned char c;
int size, ext, cnt;
size = 8;
ext = 0;
while (*name) {
c = *name;
if (c < ' ' || c > 0x7e || strchr("*?<>|\"/", c)) {
printf("Invalid character in name. Use \\ooo for special "
"characters.\n");
return 0;
}
if (c == '.') {
if (ext) {
printf("Duplicate dots in name.\n");
return 0;
}
while (size--)
*fixed++ = ' ';
size = 3;
ext = 1;
name++;
continue;
}
if (c == '\\') {
c = 0;
for (cnt = 3; cnt; cnt--) {
if (*name < '0' || *name > '7') {
printf("Invalid octal character.\n");
return 0;
}
c = c * 8 + *name++ - '0';
}
if (cnt < 4) {
printf("Expected three octal digits.\n");
return 0;
}
name += 3;
}
if (islower(c))
c = toupper(c);
if (size) {
*fixed++ = c;
size--;
}
name++;
}
if (*name || size == 8)
return 0;
if (!ext) {
while (size--)
*fixed++ = ' ';
size = 3;
}
while (size--)
*fixed++ = ' ';
return 1;
}
void file_add(char *path, FD_TYPE type)
{
FDSC **current, *walk;
char name[MSDOS_NAME];
char *here;
current = &fp_root;
if (*path != '/')
die("%s: Absolute path required.", path);
path++;
while (1) {
if ((here = strchr(path, '/')))
*here = 0;
if (!file_cvt((unsigned char *)path, (unsigned char *)name))
exit(2);
for (walk = *current; walk; walk = walk->next)
if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type ==
fdt_undelete && !strncmp(name + 1, walk->name + 1, MSDOS_NAME - 1))))
die("Ambiguous name: \"%s\"", path);
else if (here && !strncmp(name, walk->name, MSDOS_NAME))
break;
if (!walk) {
walk = alloc(sizeof(FDSC));
strncpy(walk->name, name, MSDOS_NAME);
walk->type = here ? fdt_none : type;
walk->first = NULL;
walk->next = *current;
*current = walk;
}
current = &walk->first;
if (!here)
break;
*here = '/';
path = here + 1;
}
}
FDSC **file_cd(FDSC ** curr, char *fixed)
{
FDSC **walk;
if (!curr || !*curr)
return NULL;
for (walk = curr; *walk; walk = &(*walk)->next)
if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first)
return &(*walk)->first;
return NULL;
}
static FDSC **file_find(FDSC ** dir, char *fixed)
{
if (!dir || !*dir)
return NULL;
if (*(unsigned char *)fixed == DELETED_FLAG) {
while (*dir) {
if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1)
&& !(*dir)->first)
return dir;
dir = &(*dir)->next;
}
return NULL;
}
while (*dir) {
if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first)
return dir;
dir = &(*dir)->next;
}
return NULL;
}
/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
such file exists or if CURR is NULL. */
FD_TYPE file_type(FDSC ** curr, char *fixed)
{
FDSC **this;
if ((this = file_find(curr, fixed)))
return (*this)->type;
return fdt_none;
}
void file_modify(FDSC ** curr, char *fixed)
{
FDSC **this, *next;
if (!(this = file_find(curr, fixed)))
die("Internal error: file_find failed");
switch ((*this)->type) {
case fdt_drop:
printf("Dropping %s\n", file_name((unsigned char *)fixed));
*(unsigned char *)fixed = DELETED_FLAG;
break;
case fdt_undelete:
*fixed = *(*this)->name;
printf("Undeleting %s\n", file_name((unsigned char *)fixed));
break;
default:
die("Internal error: file_modify");
}
next = (*this)->next;
free(*this);
*this = next;
}
static void report_unused(FDSC * this)
{
FDSC *next;
while (this) {
next = this->next;
if (this->first)
report_unused(this->first);
else if (this->type != fdt_none)
printf("Warning: did not %s file %s\n", this->type == fdt_drop ?
"drop" : "undelete", file_name((unsigned char *)this->name));
free(this);
this = next;
}
}
void file_unused(void)
{
report_unused(fp_root);
}

View file

@ -0,0 +1,72 @@
/* file.h - Additional file attributes
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#ifndef _FILE_H
#define _FILE_H
#include "msdos_fs.h"
typedef enum { fdt_none, fdt_drop, fdt_undelete } FD_TYPE;
typedef struct _fptr {
char name[MSDOS_NAME];
FD_TYPE type;
struct _fptr *first; /* first entry */
struct _fptr *next; /* next file in directory */
} FDSC;
extern FDSC *fp_root;
char *file_name(unsigned char *fixed);
/* Returns a pointer to a pretty-printed representation of a fixed MS-DOS file
name. */
int file_cvt(unsigned char *name, unsigned char *fixed);
/* Converts a pretty-printed file name to the fixed MS-DOS format. Returns a
non-zero integer on success, zero on failure. */
void file_add(char *path, FD_TYPE type);
/* Define special attributes for a path. TYPE can be either FDT_DROP or
FDT_UNDELETE. */
FDSC **file_cd(FDSC ** curr, char *fixed);
/* Returns a pointer to the directory descriptor of the subdirectory FIXED of
CURR, or NULL if no such subdirectory exists. */
FD_TYPE file_type(FDSC ** curr, char *fixed);
/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
such file exists or if CURR is NULL. */
void file_modify(FDSC ** curr, char *fixed);
/* Performs the necessary operation on the entry of CURR that is named FIXED. */
void file_unused(void);
/* Displays warnings for all unused file attributes. */
#endif

View file

@ -0,0 +1,251 @@
/* fsck.fat.h - Common data structures and global variables
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
Copyright (C) 2015 Andreas Bombe <aeb@debian.org>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#ifndef _DOSFSCK_H
#define _DOSFSCK_H
//#include "types.h"
#include <stdint.h>
#ifdef _WIN32
typedef unsigned int __u32;
typedef unsigned __int64 __u64;
#define le16toh(v) (v)
#define le32toh(v) (v)
#define htole16(v) (v)
#define htole32(v) (v)
#endif
#ifdef _M_IX86
#include "byteorder.h"
#endif
#if 0
#undef le16toh
#undef le32toh
#undef htole16
#undef htole32
#endif
#if __BYTE_ORDER == __BIG_ENDIAN
#include "byteswap.h"
#if 0
#define le16toh(v) bswap_16(v)
#define le32toh(v) bswap_32(v)
#define htole16(v) le16toh(v)
#define htole32(v) le32toh(v)
#endif
#if 0
#define le16toh(v) le16_to_cpu(v)
#define le32toh(v) le32_to_cpu(v)
#define htole16(v) cpu_to_le16(v)
#define htole32(v) cpu_to_le32(v)
#endif
#else
#define le16toh(v) (v)
#define le32toh(v) (v)
#define htole16(v) (v)
#define htole32(v) (v)
#endif /* __BIG_ENDIAN */
// #include "endian_compat.h"
#ifndef offsetof
#define offsetof(t,e) ((int)&(((t *)0)->e))
#endif
// extern int interactive, rw, list, verbose, test, write_immed;
// extern int atari_format;
// extern unsigned n_files;
// extern void *mem_queue;
#include "rosglue.h"
#include "msdos_fs.h"
#define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
#define FAT_STATE_DIRTY 0x01
#include <pshpack1.h>
/* ++roman: Use own definition of boot sector structure -- the kernel headers'
* name for it is msdos_boot_sector in 2.0 and fat_boot_sector in 2.1 ... */
struct boot_sector {
uint8_t ignored[3]; /* Boot strap short or near jump */
uint8_t system_id[8]; /* Name - can be used to special case
partition manager volumes */
uint8_t sector_size[2]; /* bytes per logical sector */
uint8_t cluster_size; /* sectors/cluster */
uint16_t reserved; /* reserved sectors */
uint8_t fats; /* number of FATs */
uint8_t dir_entries[2]; /* root directory entries */
uint8_t sectors[2]; /* number of sectors */
uint8_t media; /* media code (unused) */
uint16_t fat_length; /* sectors/FAT */
uint16_t secs_track; /* sectors per track */
uint16_t heads; /* number of heads */
uint32_t hidden; /* hidden sectors (unused) */
uint32_t total_sect; /* number of sectors (if sectors == 0) */
/* The following fields are only used by FAT32 */
uint32_t fat32_length; /* sectors/FAT */
uint16_t flags; /* bit 8: fat mirroring, low 4: active fat */
uint8_t version[2]; /* major, minor filesystem version */
uint32_t root_cluster; /* first cluster in root directory */
uint16_t info_sector; /* filesystem info sector */
uint16_t backup_boot; /* backup boot sector */
uint8_t reserved2[12]; /* Unused */
uint8_t drive_number; /* Logical Drive Number */
uint8_t reserved3; /* Unused */
uint8_t extended_sig; /* Extended Signature (0x29) */
uint32_t serial; /* Serial number */
uint8_t label[11]; /* FS label */
uint8_t fs_type[8]; /* FS Type */
/* fill up to 512 bytes */
uint8_t junk[422];
} __attribute__ ((packed));
struct boot_sector_16 {
uint8_t ignored[3]; /* Boot strap short or near jump */
uint8_t system_id[8]; /* Name - can be used to special case
partition manager volumes */
uint8_t sector_size[2]; /* bytes per logical sector */
uint8_t cluster_size; /* sectors/cluster */
uint16_t reserved; /* reserved sectors */
uint8_t fats; /* number of FATs */
uint8_t dir_entries[2]; /* root directory entries */
uint8_t sectors[2]; /* number of sectors */
uint8_t media; /* media code (unused) */
uint16_t fat_length; /* sectors/FAT */
uint16_t secs_track; /* sectors per track */
uint16_t heads; /* number of heads */
uint32_t hidden; /* hidden sectors (unused) */
uint32_t total_sect; /* number of sectors (if sectors == 0) */
uint8_t drive_number; /* Logical Drive Number */
uint8_t reserved2; /* Unused */
uint8_t extended_sig; /* Extended Signature (0x29) */
uint32_t serial; /* Serial number */
uint8_t label[11]; /* FS label */
uint8_t fs_type[8]; /* FS Type */
/* fill up to 512 bytes */
uint8_t junk[450];
} __attribute__ ((packed));
struct info_sector {
uint32_t magic; /* Magic for info sector ('RRaA') */
uint8_t junk[0x1dc];
uint32_t reserved1; /* Nothing as far as I can tell */
uint32_t signature; /* 0x61417272 ('rrAa') */
uint32_t free_clusters; /* Free cluster count. -1 if unknown */
uint32_t next_cluster; /* Most recently allocated cluster. */
uint32_t reserved2[3];
uint16_t reserved3;
uint16_t boot_sign;
};
typedef struct {
uint8_t name[MSDOS_NAME]; /* name including extension */
uint8_t attr; /* attribute bits */
uint8_t lcase; /* Case for base and extension */
uint8_t ctime_ms; /* Creation time, milliseconds */
uint16_t ctime; /* Creation time */
uint16_t cdate; /* Creation date */
uint16_t adate; /* Last access date */
uint16_t starthi; /* High 16 bits of cluster in FAT32 */
uint16_t time, date, start; /* time, date and first cluster */
uint32_t size; /* file size (in bytes) */
} __attribute__ ((packed)) DIR_ENT;
#include <poppack.h>
typedef struct _dos_file {
DIR_ENT dir_ent;
char *lfn;
off_t offset;
off_t lfn_offset;
struct _dos_file *parent; /* parent directory */
struct _dos_file *next; /* next entry */
struct _dos_file *first; /* first entry (directory only) */
} DOS_FILE;
typedef struct {
uint32_t value;
uint32_t reserved;
} FAT_ENTRY;
typedef struct {
int nfats;
off_t fat_start;
unsigned int fat_size; /* unit is bytes */
unsigned int fat_bits; /* size of a FAT entry */
unsigned int eff_fat_bits; /* # of used bits in a FAT entry */
uint32_t root_cluster; /* 0 for old-style root dir */
off_t root_start;
unsigned int root_entries;
off_t data_start;
unsigned int cluster_size;
uint32_t data_clusters; /* not including two reserved cluster numbers */
off_t fsinfo_start; /* 0 if not present */
long free_clusters;
off_t backupboot_start; /* 0 if not present */
unsigned char *fat;
DOS_FILE **cluster_owner;
char *label;
} DOS_FS;
/* value to use as end-of-file marker */
#define FAT_EOF(fs) ((atari_format ? 0xfff : 0xff8) | FAT_EXTD(fs))
#define FAT_IS_EOF(fs,v) ((uint32_t)(v) >= (0xff8|FAT_EXTD(fs)))
/* value to mark bad clusters */
#define FAT_BAD(fs) (0xff7 | FAT_EXTD(fs))
/* range of values used for bad clusters */
#define FAT_MIN_BAD(fs) ((atari_format ? 0xff0 : 0xff7) | FAT_EXTD(fs))
#define FAT_MAX_BAD(fs) ((atari_format ? 0xff7 : 0xff7) | FAT_EXTD(fs))
#define FAT_IS_BAD(fs,v) ((v) >= FAT_MIN_BAD(fs) && (v) <= FAT_MAX_BAD(fs))
/* return -16 as a number with fs->fat_bits bits */
#define FAT_EXTD(fs) (((1 << fs->eff_fat_bits)-1) & ~0xf)
/* marker for files with no 8.3 name */
#define FAT_NO_83NAME 32
#endif

View file

@ -0,0 +1,485 @@
/****
** Platform-dependent file
****/
/* io.c - Virtual disk input/output
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
Copyright (C) 2015 Andreas Bombe <aeb@debian.org>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/*
* Thu Feb 26 01:15:36 CET 1998: Martin Schulze <joey@infodrom.north.de>
* Fixed nasty bug that caused every file with a name like
* xxxxxxxx.xxx to be treated as bad name that needed to be fixed.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#include "vfatlib.h"
#define NDEBUG
#include <debug.h>
#define FSCTL_IS_VOLUME_DIRTY CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 30, METHOD_BUFFERED, FILE_ANY_ACCESS)
typedef struct _change {
void *data;
off_t pos;
int size;
struct _change *next;
} CHANGE;
static CHANGE *changes, *last;
static int did_change = 0;
static HANDLE fd;
static LARGE_INTEGER CurrentOffset;
/**** Win32 / NT support ******************************************************/
static int WIN32close(HANDLE FileHandle)
{
if (!NT_SUCCESS(NtClose(FileHandle)))
return -1;
return 0;
}
#define close WIN32close
static int WIN32read(HANDLE FileHandle, void *buf, unsigned int len)
{
NTSTATUS Status;
IO_STATUS_BLOCK IoStatusBlock;
Status = NtReadFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
buf,
len,
&CurrentOffset,
NULL);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtReadFile() failed (Status %lx)\n", Status);
return -1;
}
CurrentOffset.QuadPart += len;
return (int)len;
}
#define read WIN32read
static int WIN32write(HANDLE FileHandle, void *buf, unsigned int len)
{
NTSTATUS Status;
IO_STATUS_BLOCK IoStatusBlock;
Status = NtWriteFile(FileHandle,
NULL,
NULL,
NULL,
&IoStatusBlock,
buf,
len,
&CurrentOffset,
NULL);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
return -1;
}
CurrentOffset.QuadPart += len;
return (int)len;
}
#define write WIN32write
static off_t WIN32lseek(HANDLE fd, off_t offset, int whence)
{
LARGE_INTEGER Offset;
Offset.QuadPart = (LONGLONG)offset;
switch (whence)
{
case SEEK_SET:
break;
case SEEK_CUR:
Offset.QuadPart += CurrentOffset.QuadPart;
break;
// case SEEK_END:
// Offset.QuadPart += FileSize.QuadPart;
// break;
default:
// errno = EINVAL;
return (off_t)-1;
}
if (Offset.QuadPart < 0LL)
{
// errno = EINVAL;
return (off_t)-1;
}
// if (Offset.QuadPart > FileSize.QuadPart)
// {
// // errno = EINVAL;
// return (off_t)-1;
// }
CurrentOffset = Offset;
return CurrentOffset.QuadPart;
}
#define lseek WIN32lseek
/******************************************************************************/
void fs_open(PUNICODE_STRING DriveRoot, int read_write)
{
NTSTATUS Status;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK Iosb;
InitializeObjectAttributes(&ObjectAttributes,
DriveRoot,
0,
NULL,
NULL);
Status = NtOpenFile(&fd,
FILE_GENERIC_READ | (read_write ? FILE_GENERIC_WRITE : 0),
&ObjectAttributes,
&Iosb,
read_write ? 0 : FILE_SHARE_READ,
FILE_SYNCHRONOUS_IO_ALERT);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtOpenFile() failed with status 0x%.08x\n", Status);
return;
}
// If read_write is specified, then the volume should be exclusively locked
if (read_write) fs_lock(TRUE);
// Query geometry and partition info, to have bytes per sector, etc
CurrentOffset.QuadPart = 0LL;
changes = last = NULL;
did_change = 0;
}
BOOLEAN fs_isdirty(void)
{
NTSTATUS Status;
ULONG DirtyMask = 0;
IO_STATUS_BLOCK IoSb;
/* Check if volume is dirty */
Status = NtFsControlFile(fd,
NULL, NULL, NULL, &IoSb,
FSCTL_IS_VOLUME_DIRTY,
NULL, 0, &DirtyMask, sizeof(DirtyMask));
if (!NT_SUCCESS(Status))
{
DPRINT1("NtFsControlFile() failed with Status 0x%08x\n", Status);
return FALSE;
}
/* Convert Dirty mask to a boolean value */
return (DirtyMask & 1);
}
NTSTATUS fs_lock(BOOLEAN LockVolume)
{
NTSTATUS Status;
IO_STATUS_BLOCK IoSb;
/* Check if volume is dirty */
Status = NtFsControlFile(fd,
NULL, NULL, NULL, &IoSb,
LockVolume ? FSCTL_LOCK_VOLUME
: FSCTL_UNLOCK_VOLUME,
NULL, 0, NULL, 0);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtFsControlFile() failed with Status 0x%08x\n", Status);
}
return Status;
}
void fs_dismount(void)
{
NTSTATUS Status;
IO_STATUS_BLOCK IoSb;
/* Check if volume is dirty */
Status = NtFsControlFile(fd,
NULL, NULL, NULL, &IoSb,
FSCTL_DISMOUNT_VOLUME,
NULL, 0, NULL, 0);
if (!NT_SUCCESS(Status))
{
DPRINT1("NtFsControlFile() failed with Status 0x%08x\n", Status);
}
}
/**
* Read data from the partition, accounting for any pending updates that are
* queued for writing.
*
* @param[in] pos Byte offset, relative to the beginning of the partition,
* at which to read
* @param[in] size Number of bytes to read
* @param[out] data Where to put the data read
*/
void fs_read(off_t pos, int size, void *data)
{
CHANGE *walk;
int got;
#if 1 // TMN
const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size;
const off_t seekpos_aligned = pos - (pos % 512);
const size_t seek_delta = (size_t)(pos - seekpos_aligned);
#if DBG
const size_t readsize = (size_t)(pos - seekpos_aligned) + readsize_aligned;
#endif
char* tmpBuf = alloc(readsize_aligned);
if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",pos);
if ((got = read(fd, tmpBuf, readsize_aligned)) < 0) pdie("Read %d bytes at %lld",size,pos);
assert(got >= size);
got = size;
assert(seek_delta + size <= readsize);
memcpy(data, tmpBuf+seek_delta, size);
free(tmpBuf);
#else // TMN:
if (lseek(fd, pos, 0) != pos)
pdie("Seek to %lld", pos);
if ((got = read(fd, data, size)) < 0)
pdie("Read %d bytes at %lld", size, pos);
#endif // TMN:
if (got != size)
die("Got %d bytes instead of %d at %lld", got, size, pos);
for (walk = changes; walk; walk = walk->next) {
if (walk->pos < pos + size && walk->pos + walk->size > pos) {
if (walk->pos < pos)
memcpy(data, (char *)walk->data + pos - walk->pos,
min(size, walk->size - pos + walk->pos));
else
memcpy((char *)data + walk->pos - pos, walk->data,
min(walk->size, size + pos - walk->pos));
}
}
}
int fs_test(off_t pos, int size)
{
void *scratch;
int okay;
#if 1 // TMN
const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size; // TMN:
const off_t seekpos_aligned = pos - (pos % 512); // TMN:
scratch = alloc(readsize_aligned);
if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",pos);
okay = read(fd, scratch, readsize_aligned) == (int)readsize_aligned;
free(scratch);
#else // TMN:
if (lseek(fd, pos, 0) != pos)
pdie("Seek to %lld", pos);
scratch = alloc(size);
okay = read(fd, scratch, size) == size;
free(scratch);
#endif // TMN:
return okay;
}
void fs_write(off_t pos, int size, void *data)
{
CHANGE *new;
int did;
#if 1 //SAE
if (FsCheckFlags & FSCHECK_IMMEDIATE_WRITE) {
void *scratch;
const size_t readsize_aligned = (size % 512) ? (size + (512 - (size % 512))) : size;
const off_t seekpos_aligned = pos - (pos % 512);
const size_t seek_delta = (size_t)(pos - seekpos_aligned);
BOOLEAN use_read = (seek_delta != 0) || ((readsize_aligned-size) != 0);
/* Aloc temp buffer if write is not aligned */
if (use_read)
scratch = alloc(readsize_aligned);
else
scratch = data;
did_change = 1;
if (lseek(fd, seekpos_aligned, 0) != seekpos_aligned) pdie("Seek to %lld",seekpos_aligned);
if (use_read)
{
/* Read aligned data */
if (read(fd, scratch, readsize_aligned) < 0) pdie("Read %d bytes at %lld",size,pos);
/* Patch data in memory */
memcpy((char *)scratch + seek_delta, data, size);
}
/* Write it back */
if ((did = write(fd, scratch, readsize_aligned)) == (int)readsize_aligned)
{
if (use_read) free(scratch);
return;
}
if (did < 0) pdie("Write %d bytes at %lld", size, pos);
die("Wrote %d bytes instead of %d at %lld", did, size, pos);
}
#else //SAE
if (write_immed) {
did_change = 1;
if (lseek(fd, pos, 0) != pos)
pdie("Seek to %lld", pos);
if ((did = write(fd, data, size)) == size)
return;
if (did < 0)
pdie("Write %d bytes at %lld", size, pos);
die("Wrote %d bytes instead of %d at %lld", did, size, pos);
}
#endif //SAE
new = alloc(sizeof(CHANGE));
new->pos = pos;
memcpy(new->data = alloc(new->size = size), data, size);
new->next = NULL;
if (last)
last->next = new;
else
changes = new;
last = new;
}
static void fs_flush(void)
{
#if 1
CHANGE *this;
int old_write_immed = (FsCheckFlags & FSCHECK_IMMEDIATE_WRITE);
/* Disable writes to the list now */
FsCheckFlags |= FSCHECK_IMMEDIATE_WRITE;
while (changes) {
this = changes;
changes = changes->next;
fs_write(this->pos, this->size, this->data);
free(this->data);
free(this);
}
/* Restore values */
if (!old_write_immed) FsCheckFlags ^= FSCHECK_IMMEDIATE_WRITE;
#else
CHANGE *this;
int size;
while (changes) {
this = changes;
changes = changes->next;
if (lseek(fd, this->pos, 0) != this->pos)
{
// printf("Seek to %lld failed: %s\n Did not write %d bytes.\n",
// (long long)this->pos, strerror(errno), this->size);
printf("Seek to %lld failed\n Did not write %d bytes.\n",
(long long)this->pos, this->size);
}
else if ((size = write(fd, this->data, this->size)) < 0)
{
// printf("Writing %d bytes at %lld failed: %s\n", this->size,
// (long long)this->pos, strerror(errno));
printf("Writing %d bytes at %lld failed\n",
this->size, (long long)this->pos);
}
else if (size != this->size)
printf("Wrote %d bytes instead of %d bytes at %lld.\n",
size, this->size, (long long)this->pos);
free(this->data);
free(this);
}
#endif
}
int fs_close(int write)
{
CHANGE *next;
int changed;
changed = !!changes;
if (write)
fs_flush();
else
while (changes) {
next = changes->next;
free(changes->data);
free(changes);
changes = next;
}
if (close(fd) < 0)
pdie("closing filesystem");
return changed || did_change;
}
int fs_changed(void)
{
return !!changes || did_change;
}

View file

@ -0,0 +1,80 @@
/****
** Platform-dependent file
****/
/* io.h - Virtual disk input/output
Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
* by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
#ifndef _IO_H
#define _IO_H
//#include <sys/types.h> /* for loff_t */
// #include <fcntl.h> /* for off_t */
void fs_open(PUNICODE_STRING DriveRoot, int read_write);
/* Opens the file system PATH. If RW is zero, the file system is opened
read-only, otherwise, it is opened read-write. */
BOOLEAN fs_isdirty(void);
/* Checks if filesystem is dirty */
void fs_read(off_t pos, int size, void *data);
/* Reads SIZE bytes starting at POS into DATA. Performs all applicable
changes. */
int fs_test(off_t pos, int size);
/* Returns a non-zero integer if SIZE bytes starting at POS can be read without
errors. Otherwise, it returns zero. */
void fs_write(off_t pos, int size, void *data);
/* If write_immed is non-zero, SIZE bytes are written from DATA to the disk,
starting at POS. If write_immed is zero, the change is added to a list in
memory. */
int fs_close(int write);
/* Closes the filesystem, performs all pending changes if WRITE is non-zero
and removes the list of changes. Returns a non-zero integer if the file
system has been changed since the last fs_open, zero otherwise. */
int fs_changed(void);
/* Determines whether the filesystem has changed. See fs_close. */
NTSTATUS fs_lock(BOOLEAN LockVolume);
/* Lock or unlocks the volume */
void fs_dismount(void);
/* Dismounts the volume */
#endif

View file

@ -0,0 +1,524 @@
/* lfn.c - Functions for handling VFAT long filenames
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
Copyright (C) 2015 Andreas Bombe <aeb@debian.org>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#include "vfatlib.h"
#define NDEBUG
#include <debug.h>
typedef struct {
uint8_t id; /* sequence number for slot */
uint8_t name0_4[10]; /* first 5 characters in name */
uint8_t attr; /* attribute byte */
uint8_t reserved; /* always 0 */
uint8_t alias_checksum; /* checksum for 8.3 alias */
uint8_t name5_10[12]; /* 6 more characters in name */
uint16_t start; /* starting cluster number, 0 in long slots */
uint8_t name11_12[4]; /* last 2 characters in name */
} LFN_ENT;
#define LFN_ID_START 0x40
#define LFN_ID_SLOTMASK 0x1f
#define CHARS_PER_LFN 13
/* These modul-global vars represent the state of the LFN parser */
unsigned char *lfn_unicode = NULL;
unsigned char lfn_checksum;
int lfn_slot = -1;
off_t *lfn_offsets = NULL;
int lfn_parts = 0;
static unsigned char fat_uni2esc[64] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '+', '-'
};
/* This defines which unicode chars are directly convertable to ISO-8859-1 */
#define UNICODE_CONVERTABLE(cl,ch) (ch == 0 && (cl < 0x80 || cl >= 0xa0))
/* for maxlen param */
#define UNTIL_0 INT_MAX
static void copy_lfn_part(unsigned char *dst, LFN_ENT * lfn);
static char *cnv_unicode(const unsigned char *uni, int maxlen, int use_q);
/* Convert name part in 'lfn' from unicode to ASCII */
static __inline char* CNV_THIS_PART(LFN_ENT * lfn)
{
unsigned char __part_uni[CHARS_PER_LFN*2];
copy_lfn_part(__part_uni, lfn);
return cnv_unicode(__part_uni, CHARS_PER_LFN, 0);
}
/* Convert name parts collected so far (from previous slots) from unicode to
* ASCII */
#define CNV_PARTS_SO_FAR() \
(cnv_unicode( lfn_unicode+(lfn_slot*CHARS_PER_LFN*2), \
lfn_parts*CHARS_PER_LFN, 0 ))
#define BYTES_TO_WCHAR(cl,ch) ((wchar_t)((unsigned)(cl) + ((unsigned)(ch) << 8)))
static size_t mbslen(wchar_t x)
{
wchar_t wstr[] = { x, 0 };
return wcstombs(NULL, wstr, 0);
}
static size_t wctombs(char *dest, wchar_t x)
{
wchar_t wstr[] = { x, 0 };
size_t size = wcstombs(NULL, wstr, 0);
if (size != (size_t) - 1)
size = wcstombs(dest, wstr, size + 1);
return size;
}
/* This function converts an unicode string to a normal ASCII string, assuming
* ISO-8859-1 charset. Characters not in 8859-1 are converted to the same
* escape notation as used by the kernel, i.e. the uuencode-like ":xxx" */
static char *cnv_unicode(const unsigned char *uni, int maxlen, int use_q)
{
const unsigned char *up;
unsigned char *out, *cp;
int len, val;
size_t x;
for (len = 0, up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]);
up += 2) {
if ((x = mbslen(BYTES_TO_WCHAR(up[0], up[1]))) != (size_t) - 1)
len += x;
else if (UNICODE_CONVERTABLE(up[0], up[1]))
++len;
else
len += 4;
}
cp = out = use_q ? qalloc(&mem_queue, len + 1) : alloc(len + 1);
for (up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]); up += 2) {
if ((x =
wctombs((char *)cp, BYTES_TO_WCHAR(up[0], up[1]))) != (size_t) - 1)
cp += x;
else if (UNICODE_CONVERTABLE(up[0], up[1]))
*cp++ = up[0];
else {
/* here the same escape notation is used as in the Linux kernel */
*cp++ = ':';
val = (up[1] << 8) + up[0];
cp[2] = fat_uni2esc[val & 0x3f];
val >>= 6;
cp[1] = fat_uni2esc[val & 0x3f];
val >>= 6;
cp[0] = fat_uni2esc[val & 0x3f];
cp += 3;
}
}
*cp = 0;
return (char *)out;
}
static void copy_lfn_part(unsigned char *dst, LFN_ENT * lfn)
{
memcpy(dst, lfn->name0_4, 10);
memcpy(dst + 10, lfn->name5_10, 12);
memcpy(dst + 22, lfn->name11_12, 4);
}
static void clear_lfn_slots(int start, int end)
{
int i;
LFN_ENT empty;
/* New dir entry is zeroed except first byte, which is set to 0xe5.
* This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading
* a directory at the first zero entry...
*/
memset(&empty, 0, sizeof(empty));
empty.id = DELETED_FLAG;
for (i = start; i <= end; ++i) {
fs_write(lfn_offsets[i], sizeof(LFN_ENT), &empty);
}
}
void lfn_fix_checksum(off_t from, off_t to, const char *short_name)
{
int i;
uint8_t sum;
for (sum = 0, i = 0; i < 11; i++)
sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + short_name[i];
for (; from < to; from += sizeof(LFN_ENT)) {
fs_write(from + offsetof(LFN_ENT, alias_checksum), sizeof(sum), &sum);
}
}
void lfn_reset(void)
{
if (lfn_unicode)
free(lfn_unicode);
lfn_unicode = NULL;
if (lfn_offsets)
free(lfn_offsets);
lfn_offsets = NULL;
lfn_slot = -1;
}
/* This function is only called with de->attr == VFAT_LN_ATTR. It stores part
* of the long name. */
void lfn_add_slot(DIR_ENT * de, off_t dir_offset)
{
LFN_ENT *lfn = (LFN_ENT *) de;
int slot = lfn->id & LFN_ID_SLOTMASK;
unsigned offset;
if (lfn_slot == 0)
lfn_check_orphaned();
if (de->attr != VFAT_LN_ATTR)
die("lfn_add_slot called with non-LFN directory entry");
if (lfn->id & LFN_ID_START && slot != 0) {
if (lfn_slot != -1) {
int can_clear = 0;
/* There is already a LFN "in progess", so it is an error that a
* new start entry is here. */
/* Causes: 1) if slot# == expected: start bit set mysteriously, 2)
* old LFN overwritten by new one */
/* Fixes: 1) delete previous LFN 2) if slot# == expected and
* checksum ok: clear start bit */
/* XXX: Should delay that until next LFN known (then can better
* display the name) */
printf("A new long file name starts within an old one.\n");
if (slot == lfn_slot && lfn->alias_checksum == lfn_checksum) {
char *part1 = CNV_THIS_PART(lfn);
char *part2 = CNV_PARTS_SO_FAR();
printf(" It could be that the LFN start bit is wrong here\n"
" if \"%s\" seems to match \"%s\".\n", part1, part2);
free(part1);
free(part2);
can_clear = 1;
}
if (interactive) {
printf("1: Delete previous LFN\n2: Leave it as it is.\n");
if (can_clear)
printf("3: Clear start bit and concatenate LFNs\n");
} else
printf(" Not auto-correcting this.\n");
if (interactive) {
switch (get_key(can_clear ? "123" : "12", "?")) {
case '1':
clear_lfn_slots(0, lfn_parts - 1);
lfn_reset();
break;
case '2':
break;
case '3':
lfn->id &= ~LFN_ID_START;
fs_write(dir_offset + offsetof(LFN_ENT, id),
sizeof(lfn->id), &lfn->id);
break;
}
}
}
lfn_slot = slot;
lfn_checksum = lfn->alias_checksum;
lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2);
lfn_offsets = alloc(lfn_slot * sizeof(off_t));
lfn_parts = 0;
} else if (lfn_slot == -1 && slot != 0) {
/* No LFN in progress, but slot found; start bit missing */
/* Causes: 1) start bit got lost, 2) Previous slot with start bit got
* lost */
/* Fixes: 1) delete LFN, 2) set start bit */
char *part = CNV_THIS_PART(lfn);
printf("Long filename fragment \"%s\" found outside a LFN "
"sequence.\n (Maybe the start bit is missing on the "
"last fragment)\n", part);
free(part);
if (interactive) {
printf("1: Delete fragment\n2: Leave it as it is.\n"
"3: Set start bit\n");
} else
printf(" Not auto-correcting this.\n");
switch (interactive ? get_key("123", "?") : '2') {
case '1':
if (!lfn_offsets)
lfn_offsets = alloc(sizeof(off_t));
lfn_offsets[0] = dir_offset;
clear_lfn_slots(0, 0);
lfn_reset();
return;
case '2':
lfn_reset();
return;
case '3':
lfn->id |= LFN_ID_START;
fs_write(dir_offset + offsetof(LFN_ENT, id),
sizeof(lfn->id), &lfn->id);
lfn_slot = slot;
lfn_checksum = lfn->alias_checksum;
lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2);
lfn_offsets = alloc(lfn_slot * sizeof(off_t));
lfn_parts = 0;
break;
}
} else if (slot != lfn_slot) {
/* wrong sequence number */
/* Causes: 1) seq-no destroyed */
/* Fixes: 1) delete LFN, 2) fix number (maybe only if following parts
* are ok?, maybe only if checksum is ok?) (Attention: space
* for name was allocated before!) */
int can_fix = 0;
printf("Unexpected long filename sequence number "
"(%d vs. expected %d).\n", slot, lfn_slot);
if (lfn->alias_checksum == lfn_checksum && lfn_slot > 0) {
char *part1 = CNV_THIS_PART(lfn);
char *part2 = CNV_PARTS_SO_FAR();
printf(" It could be that just the number is wrong\n"
" if \"%s\" seems to match \"%s\".\n", part1, part2);
free(part1);
free(part2);
can_fix = 1;
}
if (interactive) {
printf("1: Delete LFN\n2: Leave it as it is (and ignore LFN so far)\n");
if (can_fix)
printf("3: Correct sequence number\n");
} else
printf(" Not auto-correcting this.\n");
switch (interactive ? get_key(can_fix ? "123" : "12", "?") : '2') {
case '1':
if (!lfn_offsets) {
lfn_offsets = alloc(sizeof(off_t));
lfn_parts = 0;
}
lfn_offsets[lfn_parts++] = dir_offset;
clear_lfn_slots(0, lfn_parts - 1);
lfn_reset();
return;
case '2':
lfn_reset();
return;
case '3':
lfn->id = (lfn->id & ~LFN_ID_SLOTMASK) | lfn_slot;
fs_write(dir_offset + offsetof(LFN_ENT, id),
sizeof(lfn->id), &lfn->id);
break;
}
}
if (lfn->alias_checksum != lfn_checksum) {
/* checksum mismatch */
/* Causes: 1) checksum field here destroyed */
/* Fixes: 1) delete LFN, 2) fix checksum */
printf("Checksum in long filename part wrong "
"(%02x vs. expected %02x).\n",
lfn->alias_checksum, lfn_checksum);
if (interactive) {
printf("1: Delete LFN\n2: Leave it as it is.\n"
"3: Correct checksum\n");
} else
printf(" Not auto-correcting this.\n");
if (interactive) {
switch (get_key("123", "?")) {
case '1':
lfn_offsets[lfn_parts++] = dir_offset;
clear_lfn_slots(0, lfn_parts - 1);
lfn_reset();
return;
case '2':
break;
case '3':
lfn->alias_checksum = lfn_checksum;
fs_write(dir_offset + offsetof(LFN_ENT, alias_checksum),
sizeof(lfn->alias_checksum), &lfn->alias_checksum);
break;
}
}
}
if (lfn_slot != -1) {
lfn_slot--;
offset = lfn_slot * CHARS_PER_LFN * 2;
copy_lfn_part(lfn_unicode + offset, lfn);
if (lfn->id & LFN_ID_START)
lfn_unicode[offset + 26] = lfn_unicode[offset + 27] = 0;
lfn_offsets[lfn_parts++] = dir_offset;
}
if (lfn->reserved != 0) {
printf("Reserved field in VFAT long filename slot is not 0 "
"(but 0x%02x).\n", lfn->reserved);
if (interactive)
printf("1: Fix.\n2: Leave it.\n");
else
printf("Auto-setting to 0.\n");
if (!interactive || get_key("12", "?") == '1') {
lfn->reserved = 0;
fs_write(dir_offset + offsetof(LFN_ENT, reserved),
sizeof(lfn->reserved), &lfn->reserved);
}
}
if (lfn->start != htole16(0)) {
printf("Start cluster field in VFAT long filename slot is not 0 "
"(but 0x%04x).\n", lfn->start);
if (interactive)
printf("1: Fix.\n2: Leave it.\n");
else
printf("Auto-setting to 0.\n");
if (!interactive || get_key("12", "?") == '1') {
lfn->start = htole16(0);
fs_write(dir_offset + offsetof(LFN_ENT, start),
sizeof(lfn->start), &lfn->start);
}
}
}
/* This function is always called when de->attr != VFAT_LN_ATTR is found, to
* retrieve the previously constructed LFN. */
char *lfn_get(DIR_ENT * de, off_t * lfn_offset)
{
char *lfn;
uint8_t sum;
int i;
*lfn_offset = 0;
if (de->attr == VFAT_LN_ATTR)
die("lfn_get called with LFN directory entry");
#if 0
if (de->lcase)
printf("lcase=%02x\n", de->lcase);
#endif
if (lfn_slot == -1)
/* no long name for this file */
return NULL;
if (lfn_slot != 0) {
/* The long name isn't finished yet. */
/* Causes: 1) LFN slot overwritten by non-VFAT aware tool */
/* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else
* and let user enter missing part of LFN (hard to do :-()
* 3) renumber entries and truncate name */
char *long_name = CNV_PARTS_SO_FAR();
char *short_name = file_name(de->name);
printf("Unfinished long file name \"%s\".\n"
" (Start may have been overwritten by %s)\n",
long_name, short_name);
free(long_name);
if (interactive) {
printf("1: Delete LFN\n2: Leave it as it is.\n"
"3: Fix numbering (truncates long name and attaches "
"it to short name %s)\n", short_name);
} else
printf(" Not auto-correcting this.\n");
switch (interactive ? get_key("123", "?") : '2') {
case '1':
clear_lfn_slots(0, lfn_parts - 1);
lfn_reset();
return NULL;
case '2':
lfn_reset();
return NULL;
case '3':
for (i = 0; i < lfn_parts; ++i) {
uint8_t id = (lfn_parts - i) | (i == 0 ? LFN_ID_START : 0);
fs_write(lfn_offsets[i] + offsetof(LFN_ENT, id),
sizeof(id), &id);
}
memmove(lfn_unicode, lfn_unicode + lfn_slot * CHARS_PER_LFN * 2,
lfn_parts * CHARS_PER_LFN * 2);
break;
}
}
for (sum = 0, i = 0; i < MSDOS_NAME; i++)
sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->name[i];
if (sum != lfn_checksum) {
/* checksum doesn't match, long name doesn't apply to this alias */
/* Causes: 1) alias renamed */
/* Fixes: 1) Fix checksum in LFN entries */
char *long_name = CNV_PARTS_SO_FAR();
char *short_name = file_name(de->name);
printf("Wrong checksum for long file name \"%s\".\n"
" (Short name %s may have changed without updating the long name)\n",
long_name, short_name);
free(long_name);
if (interactive) {
printf("1: Delete LFN\n2: Leave it as it is.\n"
"3: Fix checksum (attaches to short name %s)\n", short_name);
} else
printf(" Not auto-correcting this.\n");
if (interactive) {
switch (get_key("123", "?")) {
case '1':
clear_lfn_slots(0, lfn_parts - 1);
lfn_reset();
return NULL;
case '2':
lfn_reset();
return NULL;
case '3':
for (i = 0; i < lfn_parts; ++i) {
fs_write(lfn_offsets[i] + offsetof(LFN_ENT, alias_checksum),
sizeof(sum), &sum);
}
break;
}
}
}
*lfn_offset = lfn_offsets[0];
lfn = cnv_unicode(lfn_unicode, UNTIL_0, 1);
lfn_reset();
return (lfn);
}
void lfn_check_orphaned(void)
{
char *long_name;
if (lfn_slot == -1)
return;
long_name = CNV_PARTS_SO_FAR();
printf("Orphaned long file name part \"%s\"\n", long_name);
free(long_name);
if (interactive)
printf("1: Delete.\n2: Leave it.\n");
else
printf(" Auto-deleting.\n");
if (!interactive || get_key("12", "?") == '1') {
clear_lfn_slots(0, lfn_parts - 1);
}
lfn_reset();
}

View file

@ -0,0 +1,39 @@
/* lfn.h - Functions for handling VFAT long filenames
Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#ifndef _LFN_H
#define _LFN_H
void lfn_reset(void);
/* Reset the state of the LFN parser. */
void lfn_add_slot(DIR_ENT * de, off_t dir_offset);
/* Process a dir slot that is a VFAT LFN entry. */
char *lfn_get(DIR_ENT * de, off_t * lfn_offset);
/* Retrieve the long name for the proper dir entry. */
void lfn_check_orphaned(void);
void lfn_fix_checksum(off_t from, off_t to, const char *short_name);
#endif

View file

@ -0,0 +1,63 @@
/* msdos_fs.h - MS-DOS filesystem constants/structures
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 3 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, see <http://www.gnu.org/licenses/>.
The complete text of the GNU General Public License
can be found in /usr/share/common-licenses/GPL-3 file.
*/
#ifndef _MSDOS_FS_H
#define _MSDOS_FS_H
#define SECTOR_SIZE 512 /* sector size (bytes) */
#define MSDOS_DPS (SECTOR_SIZE / sizeof(struct msdos_dir_entry))
#define MSDOS_DPS_BITS 4 /* log2(MSDOS_DPS) */
#define MSDOS_DIR_BITS 5 /* log2(sizeof(struct msdos_dir_entry)) */
#define ATTR_NONE 0 /* no attribute bits */
#define ATTR_RO 1 /* read-only */
#define ATTR_HIDDEN 2 /* hidden */
#define ATTR_SYS 4 /* system */
#define ATTR_VOLUME 8 /* volume label */
#define ATTR_DIR 16 /* directory */
#define ATTR_ARCH 32 /* archived */
/* attribute bits that are copied "as is" */
#define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
#define DELETED_FLAG 0xe5 /* marks file as deleted when in name[0] */
#define IS_FREE(n) (!*(n) || *(n) == DELETED_FLAG)
#define MSDOS_NAME 11 /* maximum name length */
#define MSDOS_DOT ". " /* ".", padded to MSDOS_NAME chars */
#define MSDOS_DOTDOT ".. " /* "..", padded to MSDOS_NAME chars */
#include <pshpack1.h>
struct msdos_dir_entry {
uint8_t name[MSDOS_NAME]; /* name including extension */
uint8_t attr; /* attribute bits */
uint8_t lcase; /* Case for base and extension */
uint8_t ctime_cs; /* Creation time, centiseconds (0-199) */
uint16_t ctime; /* Creation time */
uint16_t cdate; /* Creation date */
uint16_t adate; /* Last access date */
uint16_t starthi; /* High 16 bits of cluster in FAT32 */
uint16_t time, date, start; /* time, date and first cluster */
uint32_t size; /* file size (in bytes) */
} __attribute__ ((packed));
#include <poppack.h>
#endif /* _MSDOS_FS_H */

View file

@ -0,0 +1,47 @@
#ifndef __ROSGLUE_H__
#define __ROSGLUE_H__
#ifndef __GNUC__
#define __attribute__(X)
// WARNING: When using __attribute__((packed)), also use psh/poppack.h !!
#endif
#define printf VfatPrint
#define alloc vfalloc
#define calloc vfcalloc
#define free vffree
// extern int interactive, rw, list, verbose, test, write_immed;
// extern int atari_format;
// extern unsigned n_files;
// extern void *mem_queue;
#define FSCHECK_INTERACTIVE 0x01
#define FSCHECK_LIST_FILES 0x02
#define FSCHECK_VERBOSE 0x04
#define FSCHECK_TEST_READ 0x08
#define FSCHECK_READ_WRITE 0x10
#define FSCHECK_IMMEDIATE_WRITE 0x20
extern ULONG FsCheckFlags;
extern ULONG FsCheckTotalFiles;
extern PVOID FsCheckMemQueue;
#define interactive (FsCheckFlags & FSCHECK_INTERACTIVE) // Enable interactive mode
#define list (FsCheckFlags & FSCHECK_LIST_FILES) // List path names
#define verbose (FsCheckFlags & FSCHECK_VERBOSE) // Verbose mode
#define test (FsCheckFlags & FSCHECK_TEST_READ) // Test for bad clusters
#define rw (FsCheckFlags & FSCHECK_READ_WRITE) // Read-Write (if TRUE) or Read-only mode (if FALSE)
#define write_immed (FsCheckFlags & FSCHECK_IMMEDIATE_WRITE) // Write changes to disk immediately
#define atari_format FALSE
#define n_files FsCheckTotalFiles
#define mem_queue FsCheckMemQueue
#endif /* __ROSGLUE_H__ */

View file

@ -0,0 +1,192 @@
#ifndef _LINUX_BYTEORDER_SWAB_H
#define _LINUX_BYTEORDER_SWAB_H
/*
* linux/byteorder/swab.h
* Byte-swapping, independently from CPU endianness
* swabXX[ps]?(foo)
*
* Francois-Rene Rideau <fare@tunes.org> 19971205
* separated swab functions from cpu_to_XX,
* to clean up support for bizarre-endian architectures.
*
* See asm-i386/byteorder.h and suches for examples of how to provide
* architecture-dependent optimized versions
*
*/
#include "compiler.h"
/* casts are necessary for constants, because we never know how for sure
* how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
*/
#define ___swab16(x) \
({ \
__u16 __x = (x); \
((__u16)( \
(((__u16)(__x) & (__u16)0x00ffU) << 8) | \
(((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
})
#define ___swab32(x) \
({ \
__u32 __x = (x); \
((__u32)( \
(((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
(((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \
(((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \
(((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
})
#define ___swab64(x) \
({ \
__u64 __x = (x); \
((__u64)( \
(__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
(__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
(__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
(__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) << 8) | \
(__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >> 8) | \
(__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
(__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
(__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
})
#define ___constant_swab16(x) \
((__u16)( \
(((__u16)(x) & (__u16)0x00ffU) << 8) | \
(((__u16)(x) & (__u16)0xff00U) >> 8) ))
#define ___constant_swab32(x) \
((__u32)( \
(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
(((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
(((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
#define ___constant_swab64(x) \
((__u64)( \
(__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
(__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
(__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
(__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
(__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
(__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
(__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
(__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
/*
* provide defaults when no architecture-specific optimization is detected
*/
#ifndef __arch__swab16
# define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); })
#endif
#ifndef __arch__swab32
# define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); })
#endif
#ifndef __arch__swab64
# define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); })
#endif
#ifndef __arch__swab16p
# define __arch__swab16p(x) __arch__swab16(*(x))
#endif
#ifndef __arch__swab32p
# define __arch__swab32p(x) __arch__swab32(*(x))
#endif
#ifndef __arch__swab64p
# define __arch__swab64p(x) __arch__swab64(*(x))
#endif
#ifndef __arch__swab16s
# define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0)
#endif
#ifndef __arch__swab32s
# define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0)
#endif
#ifndef __arch__swab64s
# define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0)
#endif
/*
* Allow constant folding
*/
#if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
# define __swab16(x) \
(__builtin_constant_p((__u16)(x)) ? \
___swab16((x)) : \
__fswab16((x)))
# define __swab32(x) \
(__builtin_constant_p((__u32)(x)) ? \
___swab32((x)) : \
__fswab32((x)))
# define __swab64(x) \
(__builtin_constant_p((__u64)(x)) ? \
___swab64((x)) : \
__fswab64((x)))
#else
# define __swab16(x) __fswab16(x)
# define __swab32(x) __fswab32(x)
# define __swab64(x) __fswab64(x)
#endif /* OPTIMIZE */
static __inline__ __attribute_const__ __u16 __fswab16(__u16 x)
{
return __arch__swab16(x);
}
static __inline__ __u16 __swab16p(const __u16 *x)
{
return __arch__swab16p(x);
}
static __inline__ void __swab16s(__u16 *addr)
{
__arch__swab16s(addr);
}
static __inline__ __attribute_const__ __u32 __fswab32(__u32 x)
{
return __arch__swab32(x);
}
static __inline__ __u32 __swab32p(const __u32 *x)
{
return __arch__swab32p(x);
}
static __inline__ void __swab32s(__u32 *addr)
{
__arch__swab32s(addr);
}
#ifdef __BYTEORDER_HAS_U64__
static __inline__ __attribute_const__ __u64 __fswab64(__u64 x)
{
# ifdef __SWAB_64_THRU_32__
__u32 h = x >> 32;
__u32 l = x & ((1ULL<<32)-1);
return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
# else
return __arch__swab64(x);
# endif
}
static __inline__ __u64 __swab64p(const __u64 *x)
{
return __arch__swab64p(x);
}
static __inline__ void __swab64s(__u64 *addr)
{
__arch__swab64s(addr);
}
#endif /* __BYTEORDER_HAS_U64__ */
#if defined(__KERNEL__)
#define swab16 __swab16
#define swab32 __swab32
#define swab64 __swab64
#define swab16p __swab16p
#define swab32p __swab32p
#define swab64p __swab64p
#define swab16s __swab16s
#define swab32s __swab32s
#define swab64s __swab64s
#endif
#endif /* _LINUX_BYTEORDER_SWAB_H */