aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Firmware/dfuprog/dfuprog.cpp
blob: 62ad3abc319338cd127bfdf824062e9aec7739ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: StubMotorEncoderRequest.proto

#ifndef PROTOBUF_StubMotorEncoderRequest_2eproto__INCLUDED
#define PROTOBUF_StubMotorEncoderRequest_2eproto__INCLUDED

#include <string>

#include <google/protobuf/stubs/common.h>

#if GOOGLE_PROTOBUF_VERSION < 3004000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please update
#error your headers.
#endif
#if 3004000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please
#error regenerate this file with a newer version of protoc.
#endif

#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_table_driven.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h>  // IWYU pragma: export
#include <google/protobuf/extension_set.h>  // IWYU pragma: export
#include <google/protobuf/unknown_field_set.h>
// @@protoc_insertion_point(includes)
namespace Tango {
namespace PMR {
namespace Stubs {
class StubMotorEncoderRequest;
class StubMotorEncoderRequestDefaultTypeInternal;
extern StubMotorEncoderRequestDefaultTypeInternal _StubMotorEncoderRequest_default_instance_;
}  // namespace Stubs
}  // namespace PMR
}  // namespace Tango

namespace Tango {
namespace PMR {
namespace Stubs {

namespace protobuf_StubMotorEncoderRequest_2eproto {
// Internal implementation detail -- do not call these.
struct TableStruct {
  static const ::google::protobuf::internal::ParseTableField entries[];
  static const ::google::protobuf::internal::AuxillaryParseTableField aux[];
  static const ::google::protobuf::internal::ParseTable schema[];
  static const ::google::protobuf::uint32 offsets[];
  static const ::google::protobuf::internal::FieldMetadata field_metadata[];
  static const ::google::protobuf::internal::SerializationTable serialization_table[];
  static void InitDefaultsImpl();
};
void AddDescriptors();
void InitDefaults();
}  // namespace protobuf_StubMotorEncoderRequest_2eproto

// ===================================================================

class StubMotorEncoderRequest : public ::google::protobuf::Message /* @@protoc_insertion_point(class_definition:Tango.PMR.Stubs.StubMotorEncoderRequest) */ {
 public:
  StubMotorEncoderRequest();
  virtual ~StubMotorEncoderRequest();

  StubMotorEncoderRequest(const StubMotorEncoderRequest& from);

  inline StubMotorEncoderRequest& operator=(const StubMotorEncoderRequest& from) {
    CopyFrom(from);
    return *this;
  }
  #if LANG_CXX11
  StubMotorEncoderRequest(StubMotorEncoderRequest&& from) noexcept
    : StubMotorEncoderRequest() {
    *this = ::std::move(from);
  }

  inline StubMotorEncoderRequest& operator=(StubMotorEncoderRequest&& from) noexcept {
    if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) {
      if (this != &from) InternalSwap(&from);
    } else {
      CopyFrom(from);
    }
    return *this;
  }
  #endif
  static const ::google::protobuf::Descriptor* descriptor();
  static const StubMotorEncoderRequest& default_instance();

  static inline const StubMotorEncoderRequest* internal_default_instance() {
    return reinterpret_cast<const StubMotorEncoderRequest*>(
               &_StubMotorEncoderRequest_default_instance_);
  }
  static PROTOBUF_CONSTEXPR int const kIndexInFileMessages =
    0;

  void Swap(StubMotorEncoderRequest* other);
  friend void swap(StubMotorEncoderRequest& a, StubMotorEncoderRequest& b) {
    a.Swap(&b);
  }

  // implements Message ----------------------------------------------

  inline StubMotorEncoderRequest* New() const PROTOBUF_FINAL { return New(NULL); }

  StubMotorEncoderRequest* New(::google::protobuf::Arena* arena) const PROTOBUF_FINAL;
  void CopyFrom(const ::google::protobuf::Message& from) PROTOBUF_FINAL;
  void MergeFrom(const ::google::protobuf::Message& from) PROTOBUF_FINAL;
  void CopyFrom(const StubMotorEncoderRequest& from);
  void MergeFrom(const StubMotorEncoderRequest& from);
  void Clear() PROTOBUF_FINAL;
  bool IsInitialized() const PROTOBUF_FINAL;

  size_t ByteSizeLong() const PROTOBUF_FINAL;
  bool MergePartialFromCodedStream(
      ::google::protobuf::io::CodedInputStream* input) PROTOBUF_FINAL;
  void SerializeWithCachedSizes(
      ::google::protobuf::io::CodedOutputStream* output) const PROTOBUF_FINAL;
  ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
      bool deterministic, ::google::protobuf::uint8* target) const PROTOBUF_FINAL;
  int GetCachedSize() const PROTOBUF_FINAL { return _cached_size_; }
  private:
  void SharedCtor();
  void SharedDtor();
  void SetCachedSize(int size) const PROTOBUF_FINAL;
  void InternalSwap(StubMotorEncoderRequest* other);
  privatepre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//*****************************************************************************
//
// dfuprog.cpp : A simple command-line utility for programming a USB-connected
//               Tiva board running the USB DFU boot loader.
//
// Copyright (c) 2008-2017 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 2.1.4.178 of the Tiva Firmware Development Package.
//
//*****************************************************************************

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include "lmdfu.h"
#include "lmdfuwrap.h"

//*****************************************************************************
//
// Helpful macros for generating output depending upon verbose and quiet flags.
//
//*****************************************************************************
#define VERBOSEPRINT(...) if(g_bVerbose) { printf(__VA_ARGS__); }
#define QUIETPRINT(...) if(!g_bQuiet) { printf(__VA_ARGS__); }

//*****************************************************************************
//
// Globals whose values are set or overridden via command line parameters.
//
//*****************************************************************************
bool g_bVerbose      = false;
bool g_bQuiet        = false;
bool g_bOverwrite    = false;
bool g_bUpload       = false;
bool g_bClear        = false;
bool g_bBinary       = false;
bool g_bEnumOnly     = false;
bool g_bDisregardIDs = false;
bool g_bSkipVerify   = false;
bool g_bWaitOnExit   = false;
bool g_bReset        = false;
bool g_bSwitchMode   = false;
char *g_pszFile      = NULL;
unsigned long g_ulAddress  = 0;
unsigned long g_ulAddressOverride  = 0xFFFFFFFF;
unsigned long g_ulLength = 0;
int g_iDeviceIndex = 0;

//*****************************************************************************
//
// Exit the application, optionally pausing for a key press first.
//
//*****************************************************************************
void
ExitApp(int iRetcode)
{

    //
    // Has the caller asked us to pause before exiting?
    //
    if(g_bWaitOnExit)
    {
        printf("\nPress any key to exit...\n");
        while (!_kbhit())
        {
            //
            // Wait for a key press.
            //
        }
    }

    exit(iRetcode);
}

//*****************************************************************************
//
// Display the welcome banner when the program is started.
//
//*****************************************************************************
void
PrintWelcome(void)
{
    if(g_bQuiet)
    {
        return;
    }

    printf("\nUSB Device Firmware Upgrade Example\n");
    printf("Copyright (c) 2008-2017 Texas Instruments Incorporated.  All rights reserved.\n\n");
}

//*****************************************************************************
//
// Show help on the application command line parameters.
//
//*****************************************************************************
void
ShowHelp(void)
{
    //
    // Only print help if we are not in quiet mode.
    //
    if(g_bQuiet)
    {
        return;
    }

    printf("This application may be used to download images to a Texas Instruments\n");
    printf("Tiva microcontroller running the USB Device Firmware Upgrade\n");
    printf("boot loader.  Additionally, the application can read back the\n");
    printf("existing application image or a subsection of flash and store it\n");
    printf("either as raw data or wrapped as a DFU-downloadable image.\n\n");
    printf("Supported parameters are:\n\n");
    printf("-e        - Enumerate connected devices, show info then exit.\n");
    printf("-m        - Switch into DFU mode if device is currently in runtime mode.\n");
    printf("-u        - Upload an image from the board into the target DFU file.\n");
    printf("            If absent, the file will be downloaded to the board.\n");
    printf("-c          Clear a block of flash. The address and size of the\n");
    printf("            block are may be given using -a and -l.  If these are\n");
    printf("            absent, clears the entire writable area of flash. Trumps -u.\n");
    printf("-f <file> - The file name for upload or download use.\n");
    printf("-b        - Upload binary rather than a DFU-formatted file. (used with -u)\n");
    printf("-d        - Disregard VID and PID in DFU image to be downloaded.\n");
    printf("-s        - Skip verification after a download operation.\n");
    printf("-a <num>  - Set the address the binary will be flashed to or read from.\n");
    printf("            If absent, binary files are flashed the default application\n");
    printf("            start address for the target.  Ignored for DFU files.\n");
    printf("-l <num>  - Set the upload length (use with -u). If absent, the\n");
    printf("            entire writable flash area is uploaded.\n");
    printf("-i <num>  - Sets the index of the USB DFU device to access if more\n");
    printf("            than one is found. If absent, the first device found is used.\n");
    printf("-x        - Overwrite existing file without prompting. (used with -u)\n");
    printf("-r        - Reset the target on completion of operation.\n");
    printf("-? or -h  - Show this help.\n");
    printf("-q        - Quiet mode. Disable output to stdio.\n");
    printf("-w        - Wait for a key press before exiting.\n");
    printf("-v        - Enable verbose output\n\n");
    printf("Examples:\n\n");
    printf("   dfuprog -f program.bin -a 0x1800\n\n");
    printf("Writes binary file program.bin to the device at address 0x1800\n\n");
    printf("   dfuprog -i 1 -f program.dfu\n\n");
    printf("Writes DFU-formatted file program.dfu to the second connected\n");
    printf("device (index 1) at the address found in the DFU file prefix.\n\n");
    printf("   dfuprog -u -f appimage.dfu\n\n");
    printf("Reads the current board application image into DFU-formatted file\n");
    printf("appimage.dfu\n\n");
}

//*****************************************************************************
//
// Parse the command line, extracting all parameters.
//
// Returns 0 on success. On failure, calls ExitApp(1).
//
//*****************************************************************************
int
ParseCommandLine(int argc, char *argv[])
{
    int iParm;
    bool bShowHelp;
    char *pcOptArg;

    //
    // By default, don't show the help screen.
    //
    bShowHelp = false;

    //
    // Walk through each of the parameters in the list, skipping the first one
    // which is the executable name itself.
    //
    for(iParm = 1; iParm < argc; iParm++)
    {
        //
        // Does this look like a valid switch?
        //
        if(!argv || ((argv[iParm][0] != '-') && (argv[iParm][0] != '/')) ||
           (argv[iParm][1] == '\0'))
        {
            //
            // We found something on the command line that didn't look like a
            // switch so bomb out.
            //
            printf("Unrecognized or invalid argument: %s\n", argv[iParm]);
            ExitApp(1);
        }
        else
        {
            //
            // For convenience, get a pointer to the next argument since this
            // is often a parameter for a given switch (and since this code was
            // converted from a previous version which used getopt which is not
            // available in the Windows SDK).
            //
            pcOptArg = ((iParm + 1) < argc) ? argv[iParm + 1] : NULL;
        }

        switch(argv[iParm][1])
        {
            case 'w':
                g_bWaitOnExit = true;
                break;

            case 'c':
                g_bClear = true;
                break;

            case 's':
                g_bSkipVerify = true;
                break;

            case 'd':
                g_bDisregardIDs = true;
                break;

            case 'e':
                g_bEnumOnly = true;
                break;

            case 'u':
                g_bUpload = true;
                break;

            case 'm':
                g_bSwitchMode = true;
                break;

            case 'f':
                g_pszFile = pcOptArg;
                iParm++;
                break;

            case 'b':
                g_bBinary = true;
                break;

            case 'a':
                g_ulAddressOverride = (unsigned long)strtol(pcOptArg, NULL, 0);
                iParm++;
                break;

            case 'l':
                g_ulLength = (unsigned long)strtol(pcOptArg, NULL, 0);
                iParm++;
                break;

            case 'i':
                g_iDeviceIndex = (int)strtol(pcOptArg, NULL, 0);
                iParm++;
                break;

            case 'r':
                g_bReset = TRUE;
                break;

            case 'v':
                g_bVerbose = TRUE;
                break;

            case 'q':
                g_bQuiet = TRUE;
                break;

            case 'x':
                g_bOverwrite = TRUE;
                break;

            case '?':
            case 'h':
                bShowHelp = TRUE;
                break;

            default:
                printf("Unrecognized argument: %s\n", argv[iParm]);
                ExitApp(1);
        }
    }

    //
    // Show the welcome banner unless we have been told to be quiet.
    //
    PrintWelcome();

    //
    // Show the help screen if requested.
    //
    if(bShowHelp)
    {
        ShowHelp();
        ExitApp(0);
    }

    //
    // Catch various invalid or pointless parameter cases.
    //
    if(g_bEnumOnly)
    {
        //
        // The -e option causes pretty much all other command line options to
        // be ignored so let the user know if they specified something that is
        // not going to do anything.
        //
        if(g_iDeviceIndex || g_ulLength || g_ulAddress || g_bBinary ||
           g_pszFile || g_bUpload || g_bClear || g_bSwitchMode)
        {
            //
            // Tell the user what's going on but don't exit since this is not
            // a fatal error condition.
            //
            QUIETPRINT("Some options ignored - irrelevant when used "
                       "with -e.\n");
        }
    }
    else
    {
        if(!g_bClear && !g_bSwitchMode)
        {
            //
            // We are performing a download or upload operation.  In this case,
            // we definitely need a file name.
            //
            if(!g_pszFile)
            {
                //
                // No file name provided.  If we haven't displayed it already,
                // show command line help then display the error information.
                //
                ShowHelp();

                QUIETPRINT("ERROR: No file name was specified. Please use -f "
                           "to provide one.\n");
                ExitApp(1);
            }
        }
    }

    //
    // Tell the caller that everything is OK.
    //
    return(0);
}

//*****************************************************************************
//
// Dump the information contained within the passed device information structure
// to stdout assuming we are not in quiet mode.
//
//*****************************************************************************
void
DumpDeviceInformation(tLMDFUHandle hHandle, tLMDFUDeviceInfo *psDevInfo)
{
    char pcStringBuf[256];
    unsigned short usStringLen;
    tLMDFUErr eRetcode;

    //
    // Show the VID and PID for the device.
    //
    QUIETPRINT("VID: 0x%04x    PID: 0x%04x\n", psDevInfo->usVID,
               psDevInfo->usPID);

    //
    // Now query the string descriptors associated with the device and
    // display these.
    //
    // We chicken out here and get an ASCII version of the strings
    // in the default language (even though this may not be
    // something that can be represented in ASCII).  Handling the
    // real UNICODE returned by LMDFUDeviceStringGet() is left as
    // an exercise to the reader.
    //
    usStringLen = sizeof(pcStringBuf);
    eRetcode = _LMDFUDeviceASCIIStringGet(hHandle, psDevInfo->ucProductString,
                                         pcStringBuf, &usStringLen);
    QUIETPRINT("Device Name:   %s\n",
               (eRetcode == DFU_OK) ? pcStringBuf : "<<Unknown>>");


    usStringLen = sizeof(pcStringBuf);
    eRetcode = _LMDFUDeviceASCIIStringGet(hHandle,
                                         psDevInfo->ucManufacturerString,
                                         pcStringBuf, &usStringLen);
    QUIETPRINT("Manufacturer:  %s\n",
               (eRetcode == DFU_OK) ? pcStringBuf : "<<Unknown>>");

    usStringLen = sizeof(pcStringBuf);
    eRetcode = _LMDFUDeviceASCIIStringGet(hHandle,
                                         psDevInfo->ucDFUInterfaceString,
                                         pcStringBuf, &usStringLen);
    QUIETPRINT("DFU Interface: %s\n",
               (eRetcode == DFU_OK) ? pcStringBuf : "<<Unknown>>");

    usStringLen = sizeof(pcStringBuf);
    eRetcode = _LMDFUDeviceASCIIStringGet(hHandle, psDevInfo->ucSerialString,
                                         pcStringBuf, &usStringLen);
    QUIETPRINT("Serial Num:    %s\n",
               (eRetcode == DFU_OK) ? pcStringBuf : "<<Unknown>>");

    //
    // Display other relevant information.
    //
    QUIETPRINT("Max Transfer:  %d bytes\n", psDevInfo->usTransferSize);
    QUIETPRINT("Mode:          %s\n", psDevInfo->bDFUMode ? "DFU" : "Runtime");
    if(psDevInfo->bDFUMode)
    {
        QUIETPRINT("TI Extensions: %s\n", psDevInfo->bSupportsTivaExtensions ?
                        "Supported" : "Not Supported");
        if(psDevInfo->bSupportsTivaExtensions)
        {
            QUIETPRINT("Target:        %s revision %c%c\n",
                       psDevInfo->pcPartNumber,
                       psDevInfo->cRevisionMajor + 'A',
                       psDevInfo->cRevisionMinor + '0');
        }
    }
    QUIETPRINT("Attributes:\n");
    QUIETPRINT("   Will Detach:       %s\n",
               ((psDevInfo->ucDFUAttributes & DFU_ATTR_WILL_DETACH) ?
                "Yes" : "No"));
    QUIETPRINT("   Manifest Tolerant: %s\n",
               ((psDevInfo->ucDFUAttributes & DFU_ATTR_MANIFEST_TOLERANT) ?
                "Yes" : "No"));
    QUIETPRINT("   Upload Capable:    %s\n",
        ((psDevInfo->ucDFUAttributes & DFU_ATTR_CAN_UPLOAD) ? "Yes" : "No"));
    QUIETPRINT("   Download Capable:  %s\n",
        ((psDevInfo->ucDFUAttributes & DFU_ATTR_CAN_DOWNLOAD) ? "Yes" : "No"));
}

//*****************************************************************************
//
// Upload an image from the device identified by the passed handle.  The actual
// section of flash to upload and the format to save the upload in is controlled
// by command line parameters via global variables.
//
// Returns 0 on success or a positive error return code on failure.
//
//*****************************************************************************
int
UploadImage(tLMDFUHandle hHandle, tLMDFUParams *psDFU)
{
    FILE *fh;
    tLMDFUErr eRetcode;
    unsigned char *pcFileBuf;
    size_t iLen;
    int iRetcode;
    int iResponse;
    errno_t eErr;

    QUIETPRINT("Uploading from device to %s...\n", g_pszFile);

    //
    // We only support upload on devices supporting the Tiva DFU
    // protocol since, on other devices, we have no idea how large an
    // uploaded image to expect.
    //
    if(!psDFU)
    {
        QUIETPRINT("Target device does not support Tiva protocol.\n");
        return(40);
    }

    //
    // Check if the address has a override value.
    //
    if(g_ulAddressOverride != 0xFFFFFFFF)
    {
        g_ulAddress = g_ulAddressOverride;
    }
    else if(g_ulAddress == 0)
    {
        //
        // Where should we start uploading from?  If the start address is not
        // specified, assume the start of the writeable area. Upload from
        // the application start address.
        //
        g_ulAddress = psDFU->ulAppStartAddr;
    }

    //
    // How much data must we read?  If the length is not specified, set the
    // length to be the whole block between the start address and the top of
    // the writable area.
    //
    if(g_ulLength == 0)
    {
        g_ulLength = psDFU->ulFlashTop - g_ulAddress;
    }

    //
    // Now allocate a buffer large enough to hold the image.  We need to add
    // space for the 8 byte prefix and 16 byte suffix if we have been asked
    // for a DFU-format image.
    //
    pcFileBuf = (unsigned char *)malloc(g_ulLength + (g_bBinary ? 0 : 24));
    if(!pcFileBuf)
    {
        //
        // We can't allocate the memory!
        //
        QUIETPRINT("Error allocating %d bytes of memory!\n",
                   g_ulLength + (g_bBinary ? 0 : 24));
        return(41);
    }

    //
    // Upload the data into our buffer.
    //
    eRetcode = _LMDFUUpload(hHandle, pcFileBuf, g_ulAddress,
                           (g_ulLength + (g_bBinary ? 0 : 24)), g_bBinary, NULL);

    if(eRetcode == DFU_OK)
    {
        //
        // We got the image successfully.  Have we been given the go-ahead to
        // overwrite the output file without a warning?
        //
        if(!g_bOverwrite)
        {
            eErr = fopen_s(&fh, g_pszFile, "rb");
            if(eErr == 0)
            {
                //
                // We don't need this handle any more so close the file.
                //
                fclose(fh);

                //
                // The file exists and we are in quiet mode so fail the
                // operation since we can't prompt the user for permission to
                // overwrite.
                //
                if(g_bQuiet)
                {
                    free(pcFileBuf);
                    return(43);
                }
                else
                {
                    //
                    // Prompt the user for permission to overwrite the output
                    // file.
                    //
                    printf("File %s exists. Overwrite? (Y/N): ", g_pszFile);
                    iResponse = getc(stdin);
                    if((iResponse != 'y') && (iResponse != 'Y'))
                    {
                        //
                        // The user didn't respond with 'y' or 'Y' so return an
                        // error and don't overwrite the file.
                        //
                        VERBOSEPRINT("User chose not to overwrite output.\n");
                        free(pcFileBuf);
                        return(44);
                    }
                    printf("Overwriting existing output file.\n");
                }
            }
        }

        //
        // At this point it is fine for us to open the output file for writing.
        //
        eErr = fopen_s(&fh, g_pszFile, "wb");

        if(eErr != 0)
        {
            QUIETPRINT("Error opening output file for writing.\n");
            iRetcode = 45;
        }
        else
        {
            //
            // Write the uploaded image to the file.
            //
            iLen = fwrite(pcFileBuf, 1, (g_ulLength + (g_bBinary ? 0 : 24)), fh);

            //
            // Did we write the expected amount of data?
            //
            if(iLen != (g_ulLength + (g_bBinary ? 0 : 24)))
            {
                //
                // No - something went wrong.
                //
                QUIETPRINT("Error writing output. Write %d bytes, "
                           "expected %d.\n", iLen,
                           (g_ulLength + (g_bBinary ? 0 : 24)));
                iRetcode = 46;
            }
            else
            {
                //
                // Life is good.  Set an appropriate exit code.
                //
                iRetcode = 0;
            }

            //
            // Close the output file.
            //
            fclose(fh);
        }
    }
    else
    {
        //
        // There was an error attempting to upload the image from the device.
        //
        QUIETPRINT("Error uploading %d bytes from 0x%08x!\n",
                   g_ulLength + (g_bBinary ? 0 : 24), g_ulAddress);
        iRetcode = 42;
    }

    //
    // Free up our image buffer.
    //
    free(pcFileBuf);

    return(iRetcode);
}

//*****************************************************************************
//
// Download an image to the the device identified by the passed handle.  The
// image to be downloaded and other parameters related to the operation are
// controlled by command line parameters via global variables.
//
// Returns 0 on success or a positive error return code on failure.
//
//*****************************************************************************
int
DownloadImage(tLMDFUHandle hHandle, tLMDFUParams *psDFU)
{
    FILE *fh;
    tLMDFUErr eRetcode;
    unsigned char *pcFileBuf;
    size_t iLen;
    size_t iRead;
    bool bTivaFormat;

    QUIETPRINT("Downloading %s to device...\n", g_pszFile);

    //
    // Check if the address has a override value.
    //
    if(g_ulAddressOverride != 0xFFFFFFFF)
    {
        g_ulAddress = g_ulAddressOverride;
    }

    //
    // Does the input file exist?
    //
    fopen_s(&fh, g_pszFile, "rb");
    if(!fh)
    {
        QUIETPRINT("Unable to open file %s. Does it exist?\n", g_pszFile);
        return(10);
    }

    //
    // How big is the file?
    //
    fseek(fh, 0, SEEK_END);
    iLen = ftell(fh);
    fseek(fh, 0, SEEK_SET);

    //
    // Allocate a buffer large enough for the file.
    //
    pcFileBuf = (unsigned char *)malloc(iLen);
    if(!pcFileBuf)
    {
        QUIETPRINT("Error allocating %d bytes of memory!\n", iLen);
        fclose(fh);
        return(11);
    }

    //
    // Read the file into our buffer and check that it was correctly read.
    //
    iRead = fread(pcFileBuf, 1, iLen, fh);
    fclose(fh);

    if(iRead != iLen)
    {
        QUIETPRINT("Error reading input file!\n");
        free(pcFileBuf);
        return(12);
    }

    //
    // Check to see whether this is a binary or a DFU-wrapped file.
    //
    eRetcode = _LMDFUIsValidImage(hHandle, pcFileBuf, (unsigned long)iLen, &bTivaFormat);

    //
    // Is the image for the target we are currently talking to?
    //
    if((eRetcode == DFU_ERR_UNSUPPORTED) && !g_bDisregardIDs)
    {
        QUIETPRINT("This image does not appear to be valid for the target "
                   "device.\nUse -d to disregard embedded IDs\n");
        free(pcFileBuf);
        return(14);
    }

    if(((eRetcode == DFU_OK) ||
       ((eRetcode == DFU_ERR_UNSUPPORTED) && g_bDisregardIDs)) &&
       bTivaFormat)
    {
        //
        // This is a DFU formatted image and either it is intended for the
        // target device or the user wants us to ignore the IDs in the file.
        // Since it also contains a Tiva prefix, we can send it to the
        // main download function.
        //
        VERBOSEPRINT("Image contains valid DFU suffix and Tiva prefix.\n");
        VERBOSEPRINT("Downloading image to flash.... ");
        eRetcode = _LMDFUDownload(hHandle, pcFileBuf, (unsigned long)iLen, g_bSkipVerify,
                                 g_bDisregardIDs, NULL);
    }
    else
    {
        //
        // This is not a DFU-formatted file so we download it as a binary.  In
        // this case, we need to pass the flash address passed by the user or,
        // if this was not provided, the application start address that the
        // device told us to use when we queried its capabilities earlier.
        //

        //
        // Did the file contain a DFU suffix but no Tiva prefix? If so,
        // we remove the DFU suffix since this doesn't get downloaded.  The
        // length of the suffix is in the 5th last byte.
        //
        if((eRetcode == DFU_OK) || (eRetcode == DFU_ERR_UNSUPPORTED))
        {
            iLen -= pcFileBuf[iLen - 5];
        }

        VERBOSEPRINT("Image is not fully DFU-wrapped. Downloading as binary\n");
        VERBOSEPRINT("Downloading image to flash.... ");

        eRetcode = _LMDFUDownloadBin(hHandle, pcFileBuf, (unsigned long)iLen,
                                    g_ulAddress, g_bSkipVerify, NULL);
    }

    VERBOSEPRINT("Completed.\n");

    //
    // Free the file buffer memory.
    //
    free(pcFileBuf);

    if(eRetcode != DFU_OK)
    {
        QUIETPRINT("Error %s (%d) reported during file download\n",
                   _LMDFUErrorStringGet(eRetcode), eRetcode);
        return(13);
    }
    else
    {
        return(0);
    }
}

//*****************************************************************************
//
// Erase a section of the writeable region of flash.  If the "-a" parameter
// was not specified or was specified with address 0, the entire writeable
// region of the target device flash will be erased.  If a non-zero address
// is provided, the -l parameter must also specified to set the start address
// and length of the region that will be erased.  The start address and length
// must both be integer multiples of 1024.
//
// Returns 0 on success or a positive error return code on failure.
//
//*****************************************************************************
int
ClearFlash(tLMDFUHandle hHandle, tLMDFUParams *psDFU)
{
    tLMDFUErr eRetcode;

    if(g_ulAddress)
    {
        QUIETPRINT("Clearing %dKB flash block from address 0x%08x\n",
                   g_ulLength / 1024, g_ulAddress);
    }
    else
    {
        QUIETPRINT("Clearing entire writable region of flash.\n");
    }

    //
    // Erase the required block and verify that the erase completed
    // successfully.
    //
    eRetcode = _LMDFUErase(hHandle, g_ulAddress, g_ulLength, true, NULL);

    if(eRetcode != DFU_OK)
    {
        QUIETPRINT("Error %s (%d) erasing flash!\n",
                   _LMDFUErrorStringGet(eRetcode), eRetcode);
        return(20);
    }
    else
    {
        QUIETPRINT("Flash erased successfully.\n");
        return(0);
    }
}

//*****************************************************************************
//
// The main entry point of the DFU programmer example application.
//
//*****************************************************************************
int
main(int argc,char* argv[])
{
    tLMDFUDeviceInfo sDevInfo;
    tLMDFUHandle hHandle;
    tLMDFUErr eRetcode;
    tLMDFUParams sDFU;
    tLMDFUParams *psDFU;
    int iExitCode;
    int iDevIndex;
    bool bCompleted;
    bool bDeviceFound;

    //
    // Parse the command line parameters, print the welcome banner and
    // tell the user about any errors they made.
    //
    ParseCommandLine(argc, argv);

    //
    // Initialize the DFU library.
    //
    eRetcode = _LMDFUInit();

    //
    // Could we load the DLL and query all its entry points successfully?
    //
    if(eRetcode != DFU_OK)
    {
        //
        // Oops - something is wrong with the DFU DLL.
        //
        if(eRetcode == DFU_ERR_NOT_FOUND)
        {
            printf("The driver for the USB Device Firmware Upgrade device cannot be found.\n");
            printf("Before running this program, please connect the DFU device to this system\n");
            printf("and install the device driver when prompted by Windows.  The device driver\n");
            printf("can be found on the evaluation kit CD or can be found in the windows_drivers\n");
            printf("directory of your TivaWare installation.\n\n");
            iExitCode = 10;
        }
        else if(eRetcode == DFU_ERR_INVALID_ADDR)
        {
            printf("The driver for the USB Device Firmware Upgrade device was found but appears\n");
            printf("to be a version which this program does not support. Please download and\n");
            printf("install the latest device driver and example applications from the TI\n");
            printf("web site to ensure that you are using compatible versions. The drivers\n");
            printf("can be found in the windows_drivers directory of your TivaWare\n");
            printf("installation and the applications can be found in package \"Windows-side\n");
            printf("examples for USB kits\" which may be downloaded from the web site at\n");
            printf("http://www.ti.com/software_updates.\n\n");
            iExitCode = 11;
        }
        else
        {
            printf("An error was reported while attempting to load the device driver for the \n");
            printf("USB Device Firmware Upgrade device.  If this error persists, please download\n");
            printf("and reinstall the latest device driver and example applications from the TI\n");
            printf("web site. The drivers can be found in the windows_drivers directory of your\n");
            printf("TivaWare installation and the applications can be found in package\n");
            printf("\"Windows-side examples for USB kits\" which may be downloaded from\n");
            printf("http://www.ti.com/software_updates.\n\n");
            iExitCode = 12;
        }

        //
        // Exit now with the appropriate error code.
        //
        ExitApp(iExitCode);
    }

    //
    // Enumerate the available DFU devices until we find the one we have been
    // asked to access.
    //
    QUIETPRINT("Scanning USB buses for supported DFU devices...\n\n");

    iDevIndex = 0;
    bCompleted = false;
    iExitCode = 0;
    bDeviceFound = false;

    do
    {
        //
        // Try to open a device.
        //
        eRetcode = _LMDFUDeviceOpen(iDevIndex, &sDevInfo, &hHandle);

        //
        // Were we successful?
        //
        if(eRetcode == DFU_OK)
        {
            //
            // Yes - if we are enumerating, dump information on the device.
            //
            if(g_bEnumOnly)
            {
                QUIETPRINT("\n<<<< Device %d >>>>\n\n", iDevIndex);
                DumpDeviceInformation(hHandle, &sDevInfo);
            }
            else
            {
                //
                // Have we found the device we are looking for?
                //
                if(iDevIndex == g_iDeviceIndex)
                {
                    //
                    // We have found the required device
                    //
                    bDeviceFound = true;

                    //
                    // Is the device currently in runtime mode?
                    //
                    if(sDevInfo.bDFUMode == FALSE)
                    {
                        //
                        // Have we been asked to switch it into DFU mode?
                        //
                        if(g_bSwitchMode)
                        {
                            QUIETPRINT("\n<<<< Device %d >>>>\n\n", iDevIndex);
                            DumpDeviceInformation(hHandle, &sDevInfo);
                            QUIETPRINT("Switching device into DFU mode.\n");

                            //
                            // Yes - switch the device into DFU mode.
                            //
                            eRetcode = _LMDFUModeSwitch(hHandle);

                            //
                            // Set an appropriate return code.
                            //
                            iExitCode = (eRetcode != DFU_OK) ? 0 : 100;

                            //
                            // Drop out of the loop.
                            //
                            break;
                        }
                        else
                        {
                            QUIETPRINT("Device is in runtime mode. Switch to "
                                       "DFU mode using '-m' before\n"
                                       "attempting any other operation\n");
                        }
                    }
                    else
                    {
                        //
                        // If we were asked to switch modes and the device is already in
                        // DFU mode, check that we were passed some other operation to
                        // perform.
                        //
                        if(g_bSwitchMode)
                        {
                            QUIETPRINT("Device is already in DFU mode. No "
                                        "switch necessary.\n");
                        }

                        //
                        // If it supports the Tiva DFU protocol, read back
                        // some important parameters.
                        //
                        if(sDevInfo.bSupportsTivaExtensions)
                        {
                            //
                            // Read flash size parameters from the device.
                            //
                            eRetcode = _LMDFUParamsGet(hHandle, &sDFU);
                            if(eRetcode != DFU_OK)
                            {
                                QUIETPRINT("Error %s (%d) reading flash "
                                           "parameters.\n",
                                           _LMDFUErrorStringGet(eRetcode),
                                           eRetcode);
                            }
                            psDFU = &sDFU;
                        }
                        else
                        {
                            //
                            // The device doesn't support the Tiva
                            // protocol so set the DFU parameter structure
                            // pointer to NULL.
                            //
                            psDFU = (tLMDFUParams *)0;
                        }

                        //
                        // No errors so far?
                        //
                        if(eRetcode == DFU_OK)
                        {
                            //
                            // Have we been asked to clear the flash?
                            //
                            if(g_bClear)
                            {
                                //
                                // Yes - clear the required area of flash.
                                //
                                iExitCode = ClearFlash(hHandle, psDFU);
                            }
                            //
                            // Yes - have we been asked to upload the current
                            // image to a file on the host?
                            //
                            else if(g_bUpload)
                            {
                                //
                                // Yes - go ahead and perform the upload.
                                //
                                iExitCode = UploadImage(hHandle, psDFU);
                            }
                            else
                            {
                                //
                                // No - download an image to the device if a
                                // filename has been provided.
                                //
                                if(g_pszFile)
                                {
                                    iExitCode = DownloadImage(hHandle, psDFU);
                                }
                            }
                        }

                        //
                        // At this point, we've done whatever we have been asked
                        // to do so make sure we exit the loop next time we get
                        // to the end.
                        //
                        bCompleted = true;
                    }
                }
            }

            //
            // We are finished with this device for now.
            //
            eRetcode = _LMDFUDeviceClose(hHandle, g_bReset);

            //
            // Move on to look for another device.
            //
            iDevIndex++;
        }
    }
    while((eRetcode == DFU_OK) && !bCompleted);

    //
    // Print a final summary of what we found if we are merely enumerating the
    // devices.
    //
    if(g_bEnumOnly)
    {
        QUIETPRINT("\nFound %d device%s.\n", iDevIndex, (iDevIndex == 1) ? "" : "s");
    }
    else
    {
        //
        // Did we find the device that the user wanted to talk to?
        //
        if(!bDeviceFound)
        {
            //
            // No - we couldn't find the requested device.
            //
            QUIETPRINT("The requested device was not found on the bus.\n");
            iExitCode = 1;
        }
    }

    ExitApp(iExitCode);
}