firmware-utils/mktplinkfw: add TL-WR741ND v4
[openwrt.git] / tools / firmware-utils / src / imagetag_cmdline.c
1 /*
2   File autogenerated by gengetopt version 2.22.4
3   generated with the following command:
4   gengetopt --file-name=imagetag_cmdline --file-name=imagetag_cmdline
5
6   The developers of gengetopt consider the fixed text that goes in all
7   gengetopt output files to be in the public domain:
8   we make no copyright claims on it.
9 */
10
11 /* If we use autoconf.  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #ifndef FIX_UNUSED
21 #define FIX_UNUSED(X) (void) (X) /* avoid warnings for unused params */
22 #endif
23
24 #include <getopt.h>
25
26 #include "imagetag_cmdline.h"
27
28 const char *gengetopt_args_info_purpose = "Generate image with CFE imagetag for Broadcom 63xx routers.";
29
30 const char *gengetopt_args_info_usage = "Usage: imagetag [OPTIONS]...";
31
32 const char *gengetopt_args_info_description = "Copyright (C) 2008 Axel Gembe\nCopyright (C) 2009-2010 Daniel Dickinson\nLicensed unter the terms of the Gnu General Public License.\n\nGiven a root filesystem, a linux kernel, and an optional CFE, generates an \nimage with an imagetag for a Broadcom 63xx-based router.  Additional parameters \nto be specified depend on the specfic brand and model of router.";
33
34 const char *gengetopt_args_info_help[] = {
35   "  -h, --help                    Print help and exit",
36   "  -V, --version                 Print version and exit",
37   "  -i, --kernel=filename         File with LZMA compressed kernel to include in \n                                  the image.",
38   "  -f, --rootfs=filename         File with RootFS to include in the image.",
39   "  -o, --output=filename         Name of output file.",
40   "      --cfe=filename            File with CFE to include in the image.",
41   "  -b, --boardid=STRING          Board ID to set in the image (must match what \n                                  router expects, e.g. \"96345GW2\").",
42   "  -c, --chipid=STRING           Chip ID to set in the image (must match the \n                                  actual hardware, e.g. \"6345\").",
43   "  -s, --flash-start=address     Flash start address.  (default=`0xBFC00000')",
44   "  -n, --image-offset=offset     Offset from start address for the first byte \n                                  after the CFE (in memory).  \n                                  (default=`0x10000')",
45   "  -v, --tag-version=STRING      Version number for imagetag format.  \n                                  (default=`6')",
46   "  -a, --signature=STRING        Magic string (signature), for boards that need \n                                  it.  (default=`Broadcom Corporatio')",
47   "  -m, --signature2=STRING       Second magic string (signature2).  \n                                  (default=`ver. 2.0')",
48   "  -k, --block-size=STRING       Flash erase block size.  (default=`0x10000')",
49   "  -l, --load-addr=address       Kernel load address.",
50   "  -e, --entry=address           Address where the kernel entry point will be \n                                  for booting.",
51   "  -y, --layoutver=STRING        Flash layout version (version 2.2x of the \n                                  Broadcom code requires this).",
52   "  -1, --info1=STRING            String for first vendor information section.",
53   "      --altinfo=STRING          String for vendor information section \n                                  (alternate/pirelli).",
54   "  -2, --info2=STRING            String for second vendor information section.",
55   "      --root-first              Put the rootfs before the kernel (only for \n                                  stock images, e.g. captured from the router's \n                                  flash memory).  (default=off)",
56   "  -r, --rsa-signature=STRING    String for RSA Signature section.",
57   "      --second-image-flag=flag-value\n                                Dual Image Flag (2=not-specified).  (possible \n                                  values=\"0\", \"1\", \"2\" default=`2')",
58   "      --inactive=flag-value     Inactive Flag (2=not-specified).  (possible \n                                  values=\"0\", \"1\", \"2\" default=`2')",
59   "      --reserved2=STRING        String for second reserved section.",
60   "      --kernel-file-has-header  Indicates that the kernel file includes the \n                                  kernel header with correct load address and \n                                  entry point, so no changes are needed  \n                                  (default=off)",
61     0
62 };
63
64 typedef enum {ARG_NO
65   , ARG_FLAG
66   , ARG_STRING
67 } cmdline_parser_arg_type;
68
69 static
70 void clear_given (struct gengetopt_args_info *args_info);
71 static
72 void clear_args (struct gengetopt_args_info *args_info);
73
74 static int
75 cmdline_parser_internal (int argc, char **argv, struct gengetopt_args_info *args_info,
76                         struct cmdline_parser_params *params, const char *additional_error);
77
78 static int
79 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
80
81 const char *cmdline_parser_second_image_flag_values[] = {"0", "1", "2", 0}; /*< Possible values for second-image-flag. */
82 const char *cmdline_parser_inactive_values[] = {"0", "1", "2", 0}; /*< Possible values for inactive. */
83
84 static char *
85 gengetopt_strdup (const char *s);
86
87 static
88 void clear_given (struct gengetopt_args_info *args_info)
89 {
90   args_info->help_given = 0 ;
91   args_info->version_given = 0 ;
92   args_info->kernel_given = 0 ;
93   args_info->rootfs_given = 0 ;
94   args_info->output_given = 0 ;
95   args_info->cfe_given = 0 ;
96   args_info->boardid_given = 0 ;
97   args_info->chipid_given = 0 ;
98   args_info->flash_start_given = 0 ;
99   args_info->image_offset_given = 0 ;
100   args_info->tag_version_given = 0 ;
101   args_info->signature_given = 0 ;
102   args_info->signature2_given = 0 ;
103   args_info->block_size_given = 0 ;
104   args_info->load_addr_given = 0 ;
105   args_info->entry_given = 0 ;
106   args_info->layoutver_given = 0 ;
107   args_info->info1_given = 0 ;
108   args_info->altinfo_given = 0 ;
109   args_info->info2_given = 0 ;
110   args_info->root_first_given = 0 ;
111   args_info->rsa_signature_given = 0 ;
112   args_info->second_image_flag_given = 0 ;
113   args_info->inactive_given = 0 ;
114   args_info->reserved2_given = 0 ;
115   args_info->kernel_file_has_header_given = 0 ;
116 }
117
118 static
119 void clear_args (struct gengetopt_args_info *args_info)
120 {
121   FIX_UNUSED (args_info);
122   args_info->kernel_arg = NULL;
123   args_info->kernel_orig = NULL;
124   args_info->rootfs_arg = NULL;
125   args_info->rootfs_orig = NULL;
126   args_info->output_arg = NULL;
127   args_info->output_orig = NULL;
128   args_info->cfe_arg = NULL;
129   args_info->cfe_orig = NULL;
130   args_info->boardid_arg = NULL;
131   args_info->boardid_orig = NULL;
132   args_info->chipid_arg = NULL;
133   args_info->chipid_orig = NULL;
134   args_info->flash_start_arg = gengetopt_strdup ("0xBFC00000");
135   args_info->flash_start_orig = NULL;
136   args_info->image_offset_arg = gengetopt_strdup ("0x10000");
137   args_info->image_offset_orig = NULL;
138   args_info->tag_version_arg = gengetopt_strdup ("6");
139   args_info->tag_version_orig = NULL;
140   args_info->signature_arg = gengetopt_strdup ("Broadcom Corporatio");
141   args_info->signature_orig = NULL;
142   args_info->signature2_arg = gengetopt_strdup ("ver. 2.0");
143   args_info->signature2_orig = NULL;
144   args_info->block_size_arg = gengetopt_strdup ("0x10000");
145   args_info->block_size_orig = NULL;
146   args_info->load_addr_arg = NULL;
147   args_info->load_addr_orig = NULL;
148   args_info->entry_arg = NULL;
149   args_info->entry_orig = NULL;
150   args_info->layoutver_arg = NULL;
151   args_info->layoutver_orig = NULL;
152   args_info->info1_arg = NULL;
153   args_info->info1_orig = NULL;
154   args_info->altinfo_arg = NULL;
155   args_info->altinfo_orig = NULL;
156   args_info->info2_arg = NULL;
157   args_info->info2_orig = NULL;
158   args_info->root_first_flag = 0;
159   args_info->rsa_signature_arg = NULL;
160   args_info->rsa_signature_orig = NULL;
161   args_info->second_image_flag_arg = gengetopt_strdup ("2");
162   args_info->second_image_flag_orig = NULL;
163   args_info->inactive_arg = gengetopt_strdup ("2");
164   args_info->inactive_orig = NULL;
165   args_info->reserved2_arg = NULL;
166   args_info->reserved2_orig = NULL;
167   args_info->kernel_file_has_header_flag = 0;
168   
169 }
170
171 static
172 void init_args_info(struct gengetopt_args_info *args_info)
173 {
174
175
176   args_info->help_help = gengetopt_args_info_help[0] ;
177   args_info->version_help = gengetopt_args_info_help[1] ;
178   args_info->kernel_help = gengetopt_args_info_help[2] ;
179   args_info->rootfs_help = gengetopt_args_info_help[3] ;
180   args_info->output_help = gengetopt_args_info_help[4] ;
181   args_info->cfe_help = gengetopt_args_info_help[5] ;
182   args_info->boardid_help = gengetopt_args_info_help[6] ;
183   args_info->chipid_help = gengetopt_args_info_help[7] ;
184   args_info->flash_start_help = gengetopt_args_info_help[8] ;
185   args_info->image_offset_help = gengetopt_args_info_help[9] ;
186   args_info->tag_version_help = gengetopt_args_info_help[10] ;
187   args_info->signature_help = gengetopt_args_info_help[11] ;
188   args_info->signature2_help = gengetopt_args_info_help[12] ;
189   args_info->block_size_help = gengetopt_args_info_help[13] ;
190   args_info->load_addr_help = gengetopt_args_info_help[14] ;
191   args_info->entry_help = gengetopt_args_info_help[15] ;
192   args_info->layoutver_help = gengetopt_args_info_help[16] ;
193   args_info->info1_help = gengetopt_args_info_help[17] ;
194   args_info->altinfo_help = gengetopt_args_info_help[18] ;
195   args_info->info2_help = gengetopt_args_info_help[19] ;
196   args_info->root_first_help = gengetopt_args_info_help[20] ;
197   args_info->rsa_signature_help = gengetopt_args_info_help[21] ;
198   args_info->second_image_flag_help = gengetopt_args_info_help[22] ;
199   args_info->inactive_help = gengetopt_args_info_help[23] ;
200   args_info->reserved2_help = gengetopt_args_info_help[24] ;
201   args_info->kernel_file_has_header_help = gengetopt_args_info_help[25] ;
202   
203 }
204
205 void
206 cmdline_parser_print_version (void)
207 {
208   printf ("%s %s\n",
209      (strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
210      CMDLINE_PARSER_VERSION);
211 }
212
213 static void print_help_common(void) {
214   cmdline_parser_print_version ();
215
216   if (strlen(gengetopt_args_info_purpose) > 0)
217     printf("\n%s\n", gengetopt_args_info_purpose);
218
219   if (strlen(gengetopt_args_info_usage) > 0)
220     printf("\n%s\n", gengetopt_args_info_usage);
221
222   printf("\n");
223
224   if (strlen(gengetopt_args_info_description) > 0)
225     printf("%s\n\n", gengetopt_args_info_description);
226 }
227
228 void
229 cmdline_parser_print_help (void)
230 {
231   int i = 0;
232   print_help_common();
233   while (gengetopt_args_info_help[i])
234     printf("%s\n", gengetopt_args_info_help[i++]);
235 }
236
237 void
238 cmdline_parser_init (struct gengetopt_args_info *args_info)
239 {
240   clear_given (args_info);
241   clear_args (args_info);
242   init_args_info (args_info);
243 }
244
245 void
246 cmdline_parser_params_init(struct cmdline_parser_params *params)
247 {
248   if (params)
249     { 
250       params->override = 0;
251       params->initialize = 1;
252       params->check_required = 1;
253       params->check_ambiguity = 0;
254       params->print_errors = 1;
255     }
256 }
257
258 struct cmdline_parser_params *
259 cmdline_parser_params_create(void)
260 {
261   struct cmdline_parser_params *params = 
262     (struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
263   cmdline_parser_params_init(params);  
264   return params;
265 }
266
267 static void
268 free_string_field (char **s)
269 {
270   if (*s)
271     {
272       free (*s);
273       *s = 0;
274     }
275 }
276
277
278 static void
279 cmdline_parser_release (struct gengetopt_args_info *args_info)
280 {
281
282   free_string_field (&(args_info->kernel_arg));
283   free_string_field (&(args_info->kernel_orig));
284   free_string_field (&(args_info->rootfs_arg));
285   free_string_field (&(args_info->rootfs_orig));
286   free_string_field (&(args_info->output_arg));
287   free_string_field (&(args_info->output_orig));
288   free_string_field (&(args_info->cfe_arg));
289   free_string_field (&(args_info->cfe_orig));
290   free_string_field (&(args_info->boardid_arg));
291   free_string_field (&(args_info->boardid_orig));
292   free_string_field (&(args_info->chipid_arg));
293   free_string_field (&(args_info->chipid_orig));
294   free_string_field (&(args_info->flash_start_arg));
295   free_string_field (&(args_info->flash_start_orig));
296   free_string_field (&(args_info->image_offset_arg));
297   free_string_field (&(args_info->image_offset_orig));
298   free_string_field (&(args_info->tag_version_arg));
299   free_string_field (&(args_info->tag_version_orig));
300   free_string_field (&(args_info->signature_arg));
301   free_string_field (&(args_info->signature_orig));
302   free_string_field (&(args_info->signature2_arg));
303   free_string_field (&(args_info->signature2_orig));
304   free_string_field (&(args_info->block_size_arg));
305   free_string_field (&(args_info->block_size_orig));
306   free_string_field (&(args_info->load_addr_arg));
307   free_string_field (&(args_info->load_addr_orig));
308   free_string_field (&(args_info->entry_arg));
309   free_string_field (&(args_info->entry_orig));
310   free_string_field (&(args_info->layoutver_arg));
311   free_string_field (&(args_info->layoutver_orig));
312   free_string_field (&(args_info->info1_arg));
313   free_string_field (&(args_info->info1_orig));
314   free_string_field (&(args_info->altinfo_arg));
315   free_string_field (&(args_info->altinfo_orig));
316   free_string_field (&(args_info->info2_arg));
317   free_string_field (&(args_info->info2_orig));
318   free_string_field (&(args_info->rsa_signature_arg));
319   free_string_field (&(args_info->rsa_signature_orig));
320   free_string_field (&(args_info->second_image_flag_arg));
321   free_string_field (&(args_info->second_image_flag_orig));
322   free_string_field (&(args_info->inactive_arg));
323   free_string_field (&(args_info->inactive_orig));
324   free_string_field (&(args_info->reserved2_arg));
325   free_string_field (&(args_info->reserved2_orig));
326   
327   
328
329   clear_given (args_info);
330 }
331
332 /**
333  * @param val the value to check
334  * @param values the possible values
335  * @return the index of the matched value:
336  * -1 if no value matched,
337  * -2 if more than one value has matched
338  */
339 static int
340 check_possible_values(const char *val, const char *values[])
341 {
342   int i, found, last;
343   size_t len;
344
345   if (!val)   /* otherwise strlen() crashes below */
346     return -1; /* -1 means no argument for the option */
347
348   found = last = 0;
349
350   for (i = 0, len = strlen(val); values[i]; ++i)
351     {
352       if (strncmp(val, values[i], len) == 0)
353         {
354           ++found;
355           last = i;
356           if (strlen(values[i]) == len)
357             return i; /* exact macth no need to check more */
358         }
359     }
360
361   if (found == 1) /* one match: OK */
362     return last;
363
364   return (found ? -2 : -1); /* return many values or none matched */
365 }
366
367
368 static void
369 write_into_file(FILE *outfile, const char *opt, const char *arg, const char *values[])
370 {
371   int found = -1;
372   if (arg) {
373     if (values) {
374       found = check_possible_values(arg, values);      
375     }
376     if (found >= 0)
377       fprintf(outfile, "%s=\"%s\" # %s\n", opt, arg, values[found]);
378     else
379       fprintf(outfile, "%s=\"%s\"\n", opt, arg);
380   } else {
381     fprintf(outfile, "%s\n", opt);
382   }
383 }
384
385
386 int
387 cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
388 {
389   int i = 0;
390
391   if (!outfile)
392     {
393       fprintf (stderr, "%s: cannot dump options to stream\n", CMDLINE_PARSER_PACKAGE);
394       return EXIT_FAILURE;
395     }
396
397   if (args_info->help_given)
398     write_into_file(outfile, "help", 0, 0 );
399   if (args_info->version_given)
400     write_into_file(outfile, "version", 0, 0 );
401   if (args_info->kernel_given)
402     write_into_file(outfile, "kernel", args_info->kernel_orig, 0);
403   if (args_info->rootfs_given)
404     write_into_file(outfile, "rootfs", args_info->rootfs_orig, 0);
405   if (args_info->output_given)
406     write_into_file(outfile, "output", args_info->output_orig, 0);
407   if (args_info->cfe_given)
408     write_into_file(outfile, "cfe", args_info->cfe_orig, 0);
409   if (args_info->boardid_given)
410     write_into_file(outfile, "boardid", args_info->boardid_orig, 0);
411   if (args_info->chipid_given)
412     write_into_file(outfile, "chipid", args_info->chipid_orig, 0);
413   if (args_info->flash_start_given)
414     write_into_file(outfile, "flash-start", args_info->flash_start_orig, 0);
415   if (args_info->image_offset_given)
416     write_into_file(outfile, "image-offset", args_info->image_offset_orig, 0);
417   if (args_info->tag_version_given)
418     write_into_file(outfile, "tag-version", args_info->tag_version_orig, 0);
419   if (args_info->signature_given)
420     write_into_file(outfile, "signature", args_info->signature_orig, 0);
421   if (args_info->signature2_given)
422     write_into_file(outfile, "signature2", args_info->signature2_orig, 0);
423   if (args_info->block_size_given)
424     write_into_file(outfile, "block-size", args_info->block_size_orig, 0);
425   if (args_info->load_addr_given)
426     write_into_file(outfile, "load-addr", args_info->load_addr_orig, 0);
427   if (args_info->entry_given)
428     write_into_file(outfile, "entry", args_info->entry_orig, 0);
429   if (args_info->layoutver_given)
430     write_into_file(outfile, "layoutver", args_info->layoutver_orig, 0);
431   if (args_info->info1_given)
432     write_into_file(outfile, "info1", args_info->info1_orig, 0);
433   if (args_info->altinfo_given)
434     write_into_file(outfile, "altinfo", args_info->altinfo_orig, 0);
435   if (args_info->info2_given)
436     write_into_file(outfile, "info2", args_info->info2_orig, 0);
437   if (args_info->root_first_given)
438     write_into_file(outfile, "root-first", 0, 0 );
439   if (args_info->rsa_signature_given)
440     write_into_file(outfile, "rsa-signature", args_info->rsa_signature_orig, 0);
441   if (args_info->second_image_flag_given)
442     write_into_file(outfile, "second-image-flag", args_info->second_image_flag_orig, cmdline_parser_second_image_flag_values);
443   if (args_info->inactive_given)
444     write_into_file(outfile, "inactive", args_info->inactive_orig, cmdline_parser_inactive_values);
445   if (args_info->reserved2_given)
446     write_into_file(outfile, "reserved2", args_info->reserved2_orig, 0);
447   if (args_info->kernel_file_has_header_given)
448     write_into_file(outfile, "kernel-file-has-header", 0, 0 );
449   
450
451   i = EXIT_SUCCESS;
452   return i;
453 }
454
455 int
456 cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
457 {
458   FILE *outfile;
459   int i = 0;
460
461   outfile = fopen(filename, "w");
462
463   if (!outfile)
464     {
465       fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
466       return EXIT_FAILURE;
467     }
468
469   i = cmdline_parser_dump(outfile, args_info);
470   fclose (outfile);
471
472   return i;
473 }
474
475 void
476 cmdline_parser_free (struct gengetopt_args_info *args_info)
477 {
478   cmdline_parser_release (args_info);
479 }
480
481 /** @brief replacement of strdup, which is not standard */
482 char *
483 gengetopt_strdup (const char *s)
484 {
485   char *result = 0;
486   if (!s)
487     return result;
488
489   result = (char*)malloc(strlen(s) + 1);
490   if (result == (char*)0)
491     return (char*)0;
492   strcpy(result, s);
493   return result;
494 }
495
496 int
497 cmdline_parser (int argc, char **argv, struct gengetopt_args_info *args_info)
498 {
499   return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
500 }
501
502 int
503 cmdline_parser_ext (int argc, char **argv, struct gengetopt_args_info *args_info,
504                    struct cmdline_parser_params *params)
505 {
506   int result;
507   result = cmdline_parser_internal (argc, argv, args_info, params, 0);
508
509   if (result == EXIT_FAILURE)
510     {
511       cmdline_parser_free (args_info);
512       exit (EXIT_FAILURE);
513     }
514   
515   return result;
516 }
517
518 int
519 cmdline_parser2 (int argc, char **argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
520 {
521   int result;
522   struct cmdline_parser_params params;
523   
524   params.override = override;
525   params.initialize = initialize;
526   params.check_required = check_required;
527   params.check_ambiguity = 0;
528   params.print_errors = 1;
529
530   result = cmdline_parser_internal (argc, argv, args_info, &params, 0);
531
532   if (result == EXIT_FAILURE)
533     {
534       cmdline_parser_free (args_info);
535       exit (EXIT_FAILURE);
536     }
537   
538   return result;
539 }
540
541 int
542 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
543 {
544   int result = EXIT_SUCCESS;
545
546   if (cmdline_parser_required2(args_info, prog_name, 0) > 0)
547     result = EXIT_FAILURE;
548
549   if (result == EXIT_FAILURE)
550     {
551       cmdline_parser_free (args_info);
552       exit (EXIT_FAILURE);
553     }
554   
555   return result;
556 }
557
558 int
559 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
560 {
561   int error = 0;
562   FIX_UNUSED (additional_error);
563
564   /* checks for required options */
565   if (! args_info->kernel_given)
566     {
567       fprintf (stderr, "%s: '--kernel' ('-i') option required%s\n", prog_name, (additional_error ? additional_error : ""));
568       error = 1;
569     }
570   
571   if (! args_info->rootfs_given)
572     {
573       fprintf (stderr, "%s: '--rootfs' ('-f') option required%s\n", prog_name, (additional_error ? additional_error : ""));
574       error = 1;
575     }
576   
577   if (! args_info->output_given)
578     {
579       fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
580       error = 1;
581     }
582   
583   if (! args_info->boardid_given)
584     {
585       fprintf (stderr, "%s: '--boardid' ('-b') option required%s\n", prog_name, (additional_error ? additional_error : ""));
586       error = 1;
587     }
588   
589   if (! args_info->chipid_given)
590     {
591       fprintf (stderr, "%s: '--chipid' ('-c') option required%s\n", prog_name, (additional_error ? additional_error : ""));
592       error = 1;
593     }
594   
595   if (! args_info->load_addr_given)
596     {
597       fprintf (stderr, "%s: '--load-addr' ('-l') option required%s\n", prog_name, (additional_error ? additional_error : ""));
598       error = 1;
599     }
600   
601   if (! args_info->entry_given)
602     {
603       fprintf (stderr, "%s: '--entry' ('-e') option required%s\n", prog_name, (additional_error ? additional_error : ""));
604       error = 1;
605     }
606   
607   
608   /* checks for dependences among options */
609
610   return error;
611 }
612
613
614 static char *package_name = 0;
615
616 /**
617  * @brief updates an option
618  * @param field the generic pointer to the field to update
619  * @param orig_field the pointer to the orig field
620  * @param field_given the pointer to the number of occurrence of this option
621  * @param prev_given the pointer to the number of occurrence already seen
622  * @param value the argument for this option (if null no arg was specified)
623  * @param possible_values the possible values for this option (if specified)
624  * @param default_value the default value (in case the option only accepts fixed values)
625  * @param arg_type the type of this option
626  * @param check_ambiguity @see cmdline_parser_params.check_ambiguity
627  * @param override @see cmdline_parser_params.override
628  * @param no_free whether to free a possible previous value
629  * @param multiple_option whether this is a multiple option
630  * @param long_opt the corresponding long option
631  * @param short_opt the corresponding short option (or '-' if none)
632  * @param additional_error possible further error specification
633  */
634 static
635 int update_arg(void *field, char **orig_field,
636                unsigned int *field_given, unsigned int *prev_given, 
637                char *value, const char *possible_values[],
638                const char *default_value,
639                cmdline_parser_arg_type arg_type,
640                int check_ambiguity, int override,
641                int no_free, int multiple_option,
642                const char *long_opt, char short_opt,
643                const char *additional_error)
644 {
645   char *stop_char = 0;
646   const char *val = value;
647   int found;
648   char **string_field;
649   FIX_UNUSED (field);
650
651   stop_char = 0;
652   found = 0;
653
654   if (!multiple_option && prev_given && (*prev_given || (check_ambiguity && *field_given)))
655     {
656       if (short_opt != '-')
657         fprintf (stderr, "%s: `--%s' (`-%c') option given more than once%s\n", 
658                package_name, long_opt, short_opt,
659                (additional_error ? additional_error : ""));
660       else
661         fprintf (stderr, "%s: `--%s' option given more than once%s\n", 
662                package_name, long_opt,
663                (additional_error ? additional_error : ""));
664       return 1; /* failure */
665     }
666
667   if (possible_values && (found = check_possible_values((value ? value : default_value), possible_values)) < 0)
668     {
669       if (short_opt != '-')
670         fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s' (`-%c')%s\n", 
671           package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt, short_opt,
672           (additional_error ? additional_error : ""));
673       else
674         fprintf (stderr, "%s: %s argument, \"%s\", for option `--%s'%s\n", 
675           package_name, (found == -2) ? "ambiguous" : "invalid", value, long_opt,
676           (additional_error ? additional_error : ""));
677       return 1; /* failure */
678     }
679     
680   if (field_given && *field_given && ! override)
681     return 0;
682   if (prev_given)
683     (*prev_given)++;
684   if (field_given)
685     (*field_given)++;
686   if (possible_values)
687     val = possible_values[found];
688
689   switch(arg_type) {
690   case ARG_FLAG:
691     *((int *)field) = !*((int *)field);
692     break;
693   case ARG_STRING:
694     if (val) {
695       string_field = (char **)field;
696       if (!no_free && *string_field)
697         free (*string_field); /* free previous string */
698       *string_field = gengetopt_strdup (val);
699     }
700     break;
701   default:
702     break;
703   };
704
705
706   /* store the original value */
707   switch(arg_type) {
708   case ARG_NO:
709   case ARG_FLAG:
710     break;
711   default:
712     if (value && orig_field) {
713       if (no_free) {
714         *orig_field = value;
715       } else {
716         if (*orig_field)
717           free (*orig_field); /* free previous string */
718         *orig_field = gengetopt_strdup (value);
719       }
720     }
721   };
722
723   return 0; /* OK */
724 }
725
726
727 int
728 cmdline_parser_internal (
729   int argc, char **argv, struct gengetopt_args_info *args_info,
730                         struct cmdline_parser_params *params, const char *additional_error)
731 {
732   int c;        /* Character of the parsed option.  */
733
734   int error = 0;
735   struct gengetopt_args_info local_args_info;
736   
737   int override;
738   int initialize;
739   int check_required;
740   int check_ambiguity;
741   
742   package_name = argv[0];
743   
744   override = params->override;
745   initialize = params->initialize;
746   check_required = params->check_required;
747   check_ambiguity = params->check_ambiguity;
748
749   if (initialize)
750     cmdline_parser_init (args_info);
751
752   cmdline_parser_init (&local_args_info);
753
754   optarg = 0;
755   optind = 0;
756   opterr = params->print_errors;
757   optopt = '?';
758
759   while (1)
760     {
761       int option_index = 0;
762
763       static struct option long_options[] = {
764         { "help",       0, NULL, 'h' },
765         { "version",    0, NULL, 'V' },
766         { "kernel",     1, NULL, 'i' },
767         { "rootfs",     1, NULL, 'f' },
768         { "output",     1, NULL, 'o' },
769         { "cfe",        1, NULL, 0 },
770         { "boardid",    1, NULL, 'b' },
771         { "chipid",     1, NULL, 'c' },
772         { "flash-start",        1, NULL, 's' },
773         { "image-offset",       1, NULL, 'n' },
774         { "tag-version",        1, NULL, 'v' },
775         { "signature",  1, NULL, 'a' },
776         { "signature2", 1, NULL, 'm' },
777         { "block-size", 1, NULL, 'k' },
778         { "load-addr",  1, NULL, 'l' },
779         { "entry",      1, NULL, 'e' },
780         { "layoutver",  1, NULL, 'y' },
781         { "info1",      1, NULL, '1' },
782         { "altinfo",    1, NULL, 0 },
783         { "info2",      1, NULL, '2' },
784         { "root-first", 0, NULL, 0 },
785         { "rsa-signature",      1, NULL, 'r' },
786         { "second-image-flag",  1, NULL, 0 },
787         { "inactive",   1, NULL, 0 },
788         { "reserved2",  1, NULL, 0 },
789         { "kernel-file-has-header",     0, NULL, 0 },
790         { 0,  0, 0, 0 }
791       };
792
793       c = getopt_long (argc, argv, "hVi:f:o:b:c:s:n:v:a:m:k:l:e:y:1:2:r:", long_options, &option_index);
794
795       if (c == -1) break;       /* Exit from `while (1)' loop.  */
796
797       switch (c)
798         {
799         case 'h':       /* Print help and exit.  */
800           cmdline_parser_print_help ();
801           cmdline_parser_free (&local_args_info);
802           exit (EXIT_SUCCESS);
803
804         case 'V':       /* Print version and exit.  */
805           cmdline_parser_print_version ();
806           cmdline_parser_free (&local_args_info);
807           exit (EXIT_SUCCESS);
808
809         case 'i':       /* File with LZMA compressed kernel to include in the image..  */
810         
811         
812           if (update_arg( (void *)&(args_info->kernel_arg), 
813                &(args_info->kernel_orig), &(args_info->kernel_given),
814               &(local_args_info.kernel_given), optarg, 0, 0, ARG_STRING,
815               check_ambiguity, override, 0, 0,
816               "kernel", 'i',
817               additional_error))
818             goto failure;
819         
820           break;
821         case 'f':       /* File with RootFS to include in the image..  */
822         
823         
824           if (update_arg( (void *)&(args_info->rootfs_arg), 
825                &(args_info->rootfs_orig), &(args_info->rootfs_given),
826               &(local_args_info.rootfs_given), optarg, 0, 0, ARG_STRING,
827               check_ambiguity, override, 0, 0,
828               "rootfs", 'f',
829               additional_error))
830             goto failure;
831         
832           break;
833         case 'o':       /* Name of output file..  */
834         
835         
836           if (update_arg( (void *)&(args_info->output_arg), 
837                &(args_info->output_orig), &(args_info->output_given),
838               &(local_args_info.output_given), optarg, 0, 0, ARG_STRING,
839               check_ambiguity, override, 0, 0,
840               "output", 'o',
841               additional_error))
842             goto failure;
843         
844           break;
845         case 'b':       /* Board ID to set in the image (must match what router expects, e.g. \"96345GW2\")..  */
846         
847         
848           if (update_arg( (void *)&(args_info->boardid_arg), 
849                &(args_info->boardid_orig), &(args_info->boardid_given),
850               &(local_args_info.boardid_given), optarg, 0, 0, ARG_STRING,
851               check_ambiguity, override, 0, 0,
852               "boardid", 'b',
853               additional_error))
854             goto failure;
855         
856           break;
857         case 'c':       /* Chip ID to set in the image (must match the actual hardware, e.g. \"6345\")..  */
858         
859         
860           if (update_arg( (void *)&(args_info->chipid_arg), 
861                &(args_info->chipid_orig), &(args_info->chipid_given),
862               &(local_args_info.chipid_given), optarg, 0, 0, ARG_STRING,
863               check_ambiguity, override, 0, 0,
864               "chipid", 'c',
865               additional_error))
866             goto failure;
867         
868           break;
869         case 's':       /* Flash start address..  */
870         
871         
872           if (update_arg( (void *)&(args_info->flash_start_arg), 
873                &(args_info->flash_start_orig), &(args_info->flash_start_given),
874               &(local_args_info.flash_start_given), optarg, 0, "0xBFC00000", ARG_STRING,
875               check_ambiguity, override, 0, 0,
876               "flash-start", 's',
877               additional_error))
878             goto failure;
879         
880           break;
881         case 'n':       /* Offset from start address for the first byte after the CFE (in memory)..  */
882         
883         
884           if (update_arg( (void *)&(args_info->image_offset_arg), 
885                &(args_info->image_offset_orig), &(args_info->image_offset_given),
886               &(local_args_info.image_offset_given), optarg, 0, "0x10000", ARG_STRING,
887               check_ambiguity, override, 0, 0,
888               "image-offset", 'n',
889               additional_error))
890             goto failure;
891         
892           break;
893         case 'v':       /* Version number for imagetag format..  */
894         
895         
896           if (update_arg( (void *)&(args_info->tag_version_arg), 
897                &(args_info->tag_version_orig), &(args_info->tag_version_given),
898               &(local_args_info.tag_version_given), optarg, 0, "6", ARG_STRING,
899               check_ambiguity, override, 0, 0,
900               "tag-version", 'v',
901               additional_error))
902             goto failure;
903         
904           break;
905         case 'a':       /* Magic string (signature), for boards that need it..  */
906         
907         
908           if (update_arg( (void *)&(args_info->signature_arg), 
909                &(args_info->signature_orig), &(args_info->signature_given),
910               &(local_args_info.signature_given), optarg, 0, "Broadcom Corporatio", ARG_STRING,
911               check_ambiguity, override, 0, 0,
912               "signature", 'a',
913               additional_error))
914             goto failure;
915         
916           break;
917         case 'm':       /* Second magic string (signature2)..  */
918         
919         
920           if (update_arg( (void *)&(args_info->signature2_arg), 
921                &(args_info->signature2_orig), &(args_info->signature2_given),
922               &(local_args_info.signature2_given), optarg, 0, "ver. 2.0", ARG_STRING,
923               check_ambiguity, override, 0, 0,
924               "signature2", 'm',
925               additional_error))
926             goto failure;
927         
928           break;
929         case 'k':       /* Flash erase block size..  */
930         
931         
932           if (update_arg( (void *)&(args_info->block_size_arg), 
933                &(args_info->block_size_orig), &(args_info->block_size_given),
934               &(local_args_info.block_size_given), optarg, 0, "0x10000", ARG_STRING,
935               check_ambiguity, override, 0, 0,
936               "block-size", 'k',
937               additional_error))
938             goto failure;
939         
940           break;
941         case 'l':       /* Kernel load address..  */
942         
943         
944           if (update_arg( (void *)&(args_info->load_addr_arg), 
945                &(args_info->load_addr_orig), &(args_info->load_addr_given),
946               &(local_args_info.load_addr_given), optarg, 0, 0, ARG_STRING,
947               check_ambiguity, override, 0, 0,
948               "load-addr", 'l',
949               additional_error))
950             goto failure;
951         
952           break;
953         case 'e':       /* Address where the kernel entry point will be for booting..  */
954         
955         
956           if (update_arg( (void *)&(args_info->entry_arg), 
957                &(args_info->entry_orig), &(args_info->entry_given),
958               &(local_args_info.entry_given), optarg, 0, 0, ARG_STRING,
959               check_ambiguity, override, 0, 0,
960               "entry", 'e',
961               additional_error))
962             goto failure;
963         
964           break;
965         case 'y':       /* Flash layout version (version 2.2x of the Broadcom code requires this)..  */
966         
967         
968           if (update_arg( (void *)&(args_info->layoutver_arg), 
969                &(args_info->layoutver_orig), &(args_info->layoutver_given),
970               &(local_args_info.layoutver_given), optarg, 0, 0, ARG_STRING,
971               check_ambiguity, override, 0, 0,
972               "layoutver", 'y',
973               additional_error))
974             goto failure;
975         
976           break;
977         case '1':       /* String for first vendor information section..  */
978         
979         
980           if (update_arg( (void *)&(args_info->info1_arg), 
981                &(args_info->info1_orig), &(args_info->info1_given),
982               &(local_args_info.info1_given), optarg, 0, 0, ARG_STRING,
983               check_ambiguity, override, 0, 0,
984               "info1", '1',
985               additional_error))
986             goto failure;
987         
988           break;
989         case '2':       /* String for second vendor information section..  */
990         
991         
992           if (update_arg( (void *)&(args_info->info2_arg), 
993                &(args_info->info2_orig), &(args_info->info2_given),
994               &(local_args_info.info2_given), optarg, 0, 0, ARG_STRING,
995               check_ambiguity, override, 0, 0,
996               "info2", '2',
997               additional_error))
998             goto failure;
999         
1000           break;
1001         case 'r':       /* String for RSA Signature section..  */
1002         
1003         
1004           if (update_arg( (void *)&(args_info->rsa_signature_arg), 
1005                &(args_info->rsa_signature_orig), &(args_info->rsa_signature_given),
1006               &(local_args_info.rsa_signature_given), optarg, 0, 0, ARG_STRING,
1007               check_ambiguity, override, 0, 0,
1008               "rsa-signature", 'r',
1009               additional_error))
1010             goto failure;
1011         
1012           break;
1013
1014         case 0: /* Long option with no short option */
1015           /* File with CFE to include in the image..  */
1016           if (strcmp (long_options[option_index].name, "cfe") == 0)
1017           {
1018           
1019           
1020             if (update_arg( (void *)&(args_info->cfe_arg), 
1021                  &(args_info->cfe_orig), &(args_info->cfe_given),
1022                 &(local_args_info.cfe_given), optarg, 0, 0, ARG_STRING,
1023                 check_ambiguity, override, 0, 0,
1024                 "cfe", '-',
1025                 additional_error))
1026               goto failure;
1027           
1028           }
1029           /* String for vendor information section (alternate/pirelli)..  */
1030           else if (strcmp (long_options[option_index].name, "altinfo") == 0)
1031           {
1032           
1033           
1034             if (update_arg( (void *)&(args_info->altinfo_arg), 
1035                  &(args_info->altinfo_orig), &(args_info->altinfo_given),
1036                 &(local_args_info.altinfo_given), optarg, 0, 0, ARG_STRING,
1037                 check_ambiguity, override, 0, 0,
1038                 "altinfo", '-',
1039                 additional_error))
1040               goto failure;
1041           
1042           }
1043           /* Put the rootfs before the kernel (only for stock images, e.g. captured from the router's flash memory)..  */
1044           else if (strcmp (long_options[option_index].name, "root-first") == 0)
1045           {
1046           
1047           
1048             if (update_arg((void *)&(args_info->root_first_flag), 0, &(args_info->root_first_given),
1049                 &(local_args_info.root_first_given), optarg, 0, 0, ARG_FLAG,
1050                 check_ambiguity, override, 1, 0, "root-first", '-',
1051                 additional_error))
1052               goto failure;
1053           
1054           }
1055           /* Dual Image Flag (2=not-specified)..  */
1056           else if (strcmp (long_options[option_index].name, "second-image-flag") == 0)
1057           {
1058           
1059           
1060             if (update_arg( (void *)&(args_info->second_image_flag_arg), 
1061                  &(args_info->second_image_flag_orig), &(args_info->second_image_flag_given),
1062                 &(local_args_info.second_image_flag_given), optarg, cmdline_parser_second_image_flag_values, "2", ARG_STRING,
1063                 check_ambiguity, override, 0, 0,
1064                 "second-image-flag", '-',
1065                 additional_error))
1066               goto failure;
1067           
1068           }
1069           /* Inactive Flag (2=not-specified)..  */
1070           else if (strcmp (long_options[option_index].name, "inactive") == 0)
1071           {
1072           
1073           
1074             if (update_arg( (void *)&(args_info->inactive_arg), 
1075                  &(args_info->inactive_orig), &(args_info->inactive_given),
1076                 &(local_args_info.inactive_given), optarg, cmdline_parser_inactive_values, "2", ARG_STRING,
1077                 check_ambiguity, override, 0, 0,
1078                 "inactive", '-',
1079                 additional_error))
1080               goto failure;
1081           
1082           }
1083           /* String for second reserved section..  */
1084           else if (strcmp (long_options[option_index].name, "reserved2") == 0)
1085           {
1086           
1087           
1088             if (update_arg( (void *)&(args_info->reserved2_arg), 
1089                  &(args_info->reserved2_orig), &(args_info->reserved2_given),
1090                 &(local_args_info.reserved2_given), optarg, 0, 0, ARG_STRING,
1091                 check_ambiguity, override, 0, 0,
1092                 "reserved2", '-',
1093                 additional_error))
1094               goto failure;
1095           
1096           }
1097           /* Indicates that the kernel file includes the kernel header with correct load address and entry point, so no changes are needed.  */
1098           else if (strcmp (long_options[option_index].name, "kernel-file-has-header") == 0)
1099           {
1100           
1101           
1102             if (update_arg((void *)&(args_info->kernel_file_has_header_flag), 0, &(args_info->kernel_file_has_header_given),
1103                 &(local_args_info.kernel_file_has_header_given), optarg, 0, 0, ARG_FLAG,
1104                 check_ambiguity, override, 1, 0, "kernel-file-has-header", '-',
1105                 additional_error))
1106               goto failure;
1107           
1108           }
1109           
1110           break;
1111         case '?':       /* Invalid option.  */
1112           /* `getopt_long' already printed an error message.  */
1113           goto failure;
1114
1115         default:        /* bug: option not considered.  */
1116           fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
1117           abort ();
1118         } /* switch */
1119     } /* while */
1120
1121
1122
1123   if (check_required)
1124     {
1125       error += cmdline_parser_required2 (args_info, argv[0], additional_error);
1126     }
1127
1128   cmdline_parser_release (&local_args_info);
1129
1130   if ( error )
1131     return (EXIT_FAILURE);
1132
1133   return 0;
1134
1135 failure:
1136   
1137   cmdline_parser_release (&local_args_info);
1138   return (EXIT_FAILURE);
1139 }