dfu_file: Add function to generate and add a DFU suffix to a file

As counterpart to the parse function it expects the needed values in the
dfu_file struct, calculates the CRC and adds the complete suffix to the file.
This function will be used by an external dfu-suffix manipulation utility added
in a later commit.

Signed-off-by: Stefan Schmidt <stefan@datenfreihafen.org>
[Tormod: Free firmware image buffer if read fails]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
dfu_file: Verify that we could read the whole file

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
dfu_file: Return failure if CRC does not match

This also means that dfu-suffix will refuse to remove a suffix
if the checksum is wrong. Which makes sense, because if the
checksum is wrong, this does not seem to be a DFU suffix.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
dfu-suffix: New DFU suffix manipulation tool

It supports verifying, removing and adding a DFU suffix to a given file.

Signed-off-by: Stefan Schmidt <stefan@datenfreihafen.org>
[Tormod: Always interpret -v -p -d options as hex]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
dfu-suffix: Print version before banner, and do it every time

To be useful for some utilities, the -V option should print
the version before the copyright banner. Simplify this by
always printing the version before the copyright banner.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Port file handling to stdio streams

Thanks go to Satz Klauer, some of this is inspired by his Windows
patch.

Note that the "delete DFU suffix" operation of the dfu-suffix tool still
uses the POSIX ftruncate() to truncate a file, so this part is less
portable. We use autotools to check for availability of ftruncate().
This might not be available in all win32 environments, but we recommend
using MinGW for Windows, where this works fine.

In main.c, for opening the uploading file, the previously used open()
with O_CREATE|O_EXCL flags does not have a straight equivalent in stdio.
glibc has an fopen() "wx" mode equivalent to the posix open()
O_CREAT|O_EXCL mode, but to make this more portable we will instead open
for appending, then check if we are at the beginning of the file.

So the only difference is that this will check for a non-empty
file whereas the old open() would check for an existing file.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Check for availability of getpagesize()

The function is only needed for Linux hosts, if at all.

autotools gets fooled when cross-building for Windows
on Linux (false positive when trying to build and run a test
program on the build machine) so check for mingw32/64.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Replace usleep() and sleep() by milli_sleep() macro

Use usleep when available, otherwise try Sleep if this looks
like a Windows build.

Put this compatibility stuff into a new header file "portable.h".

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
configure.ac: Remove unnecessary tests

Some of these checks are not necessary and we do not use
their corresponding HAVE_XXX in the code.

The AC_FUNC_MALLOC compliance test is not needed on our
supported(?) targets, and it also does the wrong thing
when cross-compiling for Windows using mingw32.

Also add -i (install missing auxiliary files) to autoreconf
in autogen.sh to generate e.g. m4 files.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Remove some obsolete, unused code

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Use standard library (C99) types instead of OS types

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Only conditionally include unistd.h

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
usb_dfu.h: Pack struct properly for Microsoft compilers

The original "packed" attribute is a gcc extension.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Various "pedantic" code compliance fixes

Make gcc -pedantic happy, mostly C90 compliance.
- do not specify subobject when initializing array
- usage string too long in help()
- no comma after last item in list
- no declaration inside code

The remaining warnings come from libusb.h.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Const'ify some strings and add some explicit casts

In particular, add explicit cast from pointers to void.

Also define __FUNCTION__ if the compiler doesn't.

Thanks to Satz Klauer for pointing out many of these.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Do not use leading underscores in #include guards

Such identifiers are reserved for the "implementation".

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
dfuse: Add progress indication on download

It was a bit too quiet when run without verbose options.

Also add a helpful hint if the user tries to download a
non-dfuse file to a dfuse device without specifying an
address.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
Detect DfuSe device correctly on big-endian architectures

TODO: Populate our own struct instead of using the packed
struct as-is from the wire.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
main: Stop guessing transfer size, error out instead

We really should be able to read out the functional descriptor
on all devices now. If we can't, or the reported transfer size is
bogus, we should add a quirk for the device.

The user can still specify the transfer size using the -t option.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
main: Update copyright banner

Stefan and I have contributed substantially to the whole
code base, so add us both as authors.

Spell out "Copyright" since "(C)" has no legal meaning.
IANAL, YMMV :)

The warning about DFU version 1.0 only is not necessary,
we support 1.1 as well as 1.0 (and 1.1a). And there are no
other versions of the standard.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>