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