imagetag_cmdline.c 41 KB

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