summaryrefslogtreecommitdiff
path: root/apps/system/hex2bin/hex2bin_main.c
blob: f3acc44cd1d2f4db0e4f8c8c779d4630a2ee26b2 (plain) (blame)
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
/****************************************************************************
 * apps/system/hex2bin_main.c
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include <nuttx/streams.h>
#include <apps/hex2bin.h>

#ifdef CONFIG_SYSTEM_HEX2BIN_BUILTIN

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: show_usage
 ****************************************************************************/

static void show_usage(FAR const char *progname, int exitcode)
{
#ifdef CONFIG_SYSTEM_HEX2BIN_USAGE
  fprintf(stderr, "Usage:\n");
  fprintf(stderr, "\t%s [OPTIONS] <hexfile> <binfile>\n", progname);
  fprintf(stderr, "\t%s -h\n", progname);
  fprintf(stderr, "Where:\n");
  fprintf(stderr, "\t<hexfile>:\n");
  fprintf(stderr, "\t\tThe file containing the Intel HEX data to be converted.\n");
  fprintf(stderr, "\t<binfile>:\n");
  fprintf(stderr, "\t\tThe output file to be created contained the converted\n");
  fprintf(stderr, "\t\tbinary data.\n");
  fprintf(stderr, "\t-h:\n");
  fprintf(stderr, "\t\tPrints this message and exits\n");
  fprintf(stderr, "And [OPTIONS] include:\n");
  fprintf(stderr, "\t-s <start address>\n");
  fprintf(stderr, "\t\tSets the start address of the binary output.  This value\n");
  fprintf(stderr, "\t\tis used to (1) calculate offsets into the output stream,\n");
  fprintf(stderr, "\t\tand (2) for error checking.  Default: 0x%08x\n",
          CONFIG_SYSTEM_HEX2BIN_BASEADDR);
  fprintf(stderr, "\t-e <end address>\n");
  fprintf(stderr, "\t\tSets the maximum address (plus 1) of the binary output.\n");
  fprintf(stderr, "\t\tThis value is used to only for error checking.  The value\n");
  fprintf(stderr, "\t\tzero disables error checking.  Default: 0x%08x\n",
          CONFIG_SYSTEM_HEX2BIN_ENDPADDR);
  fprintf(stderr, "\t\tno error checking\n");
  fprintf(stderr, "\t-w <swap code>\n");
  fprintf(stderr, "\t\t(0) No swap, (1) swap bytes in 16-bit values, or (3) swap\n");
  fprintf(stderr, "\t\tbytes in 32-bit values.  Default: %d\n",
          CONFIG_SYSTEM_HEX2BIN_SWAP);
#endif
  exit(exitcode);
}

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: hex2bin_main
 *
 * Description:
 *   Main entry point when hex2bin is built as an NSH built-in task.
 *
 * Input Parameters:
 *   Standard task inputs
 *
 * Returned Value
 *   EXIT_SUCESS on success; EXIT_FAILURE on failure
 *
 ****************************************************************************/

#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int hex2bin_main(int argc, char **argv)
#endif
{
  struct lib_stdinstream_s stdinstream;
  struct lib_stdsostream_s stdoutstream;
  FAR const char *hexfile;
  FAR const char *binfile;
  FAR char *endptr;
  FAR FILE *instream;
  FAR FILE *outstream;
  unsigned long baseaddr;
  unsigned long endpaddr;
  unsigned long swap;
  int option;
  int ret;

  /* Parse the command line options */

  baseaddr = CONFIG_SYSTEM_HEX2BIN_BASEADDR;
  endpaddr = CONFIG_SYSTEM_HEX2BIN_ENDPADDR;
  swap     = CONFIG_SYSTEM_HEX2BIN_SWAP;

  while ((option = getopt(argc, argv, ":hs:e:w:")) != ERROR)
    {
      switch (option)
        {
#ifdef CONFIG_SYSTEM_HEX2BIN_USAGE
        case 'h':
          show_usage(argv[0], EXIT_SUCCESS);
          break;
#endif

        case 's':
          baseaddr = strtoul(optarg, &endptr, 16);
          if (endptr == optarg)
            {
              fprintf(stderr, "ERROR: Invalid argument to the -s option\n");
              show_usage(argv[0], EXIT_FAILURE);
            }
          break;

        case 'e':
          endpaddr = strtoul(optarg, &endptr, 16);
          if (endptr == optarg)
            {
              fprintf(stderr, "ERROR: Invalid argument to the -e option\n");
              show_usage(argv[0], EXIT_FAILURE);
            }
          break;

        case 'w':
          swap = strtoul(optarg, &endptr, 16);
          if (endptr == optarg || swap > (unsigned long)HEX2BIN_SWAP32)
            {
              fprintf(stderr, "ERROR: Invalid argument to the -w option\n");
              show_usage(argv[0], EXIT_FAILURE);
            }
          break;

        case ':':
          fprintf(stderr, "ERROR: Missing required argument\n");
          show_usage(argv[0], EXIT_FAILURE);
          break;

        default:
        case '?':
          fprintf(stderr, "ERROR: Unrecognized option\n");
          show_usage(argv[0], EXIT_FAILURE);
          break;
        }
    }

  /* There should be two final parameters remaining on the command line */

  if (optind >= argc)
    {
      printf("ERROR: Missing required <hexfile> and <binfile> arguments\n");
      show_usage(argv[0], EXIT_FAILURE);
    }

  hexfile = argv[optind];
  optind++;

  if (optind >= argc)
    {
      printf("ERROR: Missing required <binfile> argument\n");
      show_usage(argv[0], EXIT_FAILURE);
    }

  binfile = argv[optind];
  optind++;

  if (optind < argc)
    {
      printf("ERROR: Garbage at end of command line\n");
      show_usage(argv[0], EXIT_FAILURE);
    }

  /* Open the HEX file for reading */

  instream = fopen(hexfile, "r");
  if (instream == NULL)
    {
      int errcode = errno;
      DEBUGASSERT(errcode > 0);

      fprintf(stderr, "ERROR: Failed to open \"%s\" for reading: %d\n",
              hexfile, errcode);
      return -errcode;
    }

  /* Open the BIN file for reading */

  outstream = fopen(binfile, "wb");
  if (outstream == NULL)
    {
      int errcode = errno;
      DEBUGASSERT(errcode > 0);

      fprintf(stderr, "ERROR: Failed to open \"%s\" for writing: %d\n",
              binfile, errcode);
      fclose(instream);
      return -errcode;
    }

  /* Wrap the FILE streams as standard streams */

  lib_stdinstream(&stdinstream, instream);
  lib_stdsostream(&stdoutstream, outstream);

  /* And do the deed */

  ret = hex2bin(&stdinstream.public, &stdoutstream.public,
                (uint32_t)baseaddr, (uint32_t)endpaddr,
                (enum hex2bin_swap_e)swap);
  if (ret < 0)
    {
      fprintf(stderr, "ERROR: Failed to convert \"%s\" to binary: %d\n",
              ret);
    }

  /* Clean up and return */

  fflush(outstream);
  fclose(instream);
  fclose(outstream);
  return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

#endif /* CONFIG_SYSTEM_HEX2BIN_BUILTIN */