@@ -2402,26 +2402,29 @@ static void set_signature_sizes(int secondary)
24022402}
24032403
24042404static int process_args (int argc , char * * argv ) {
2405- int ret = 0 ;
2406- int i ;
2405+ uint8_t buf [PATH_MAX - 32 ]; /* leave room to avoid "directive output may be truncated" */
2406+ static const int REQUIRED_PARAM_CT = 2 ; /* image name, key name */
2407+ static const int OPTIONAL_PARAM_CT = 2 ; /* version = 1, output = "[image name]_v1_signed.bin" */
24072408 char * tmpstr ;
24082409 const char * sign_str = "AUTO" ;
24092410 const char * hash_str = "SHA256" ;
24102411 const char * secondary_sign_str = "NONE" ;
2411- uint8_t buf [PATH_MAX - 32 ]; /* leave room to avoid "directive output may be truncated" */
2412+ int param_ct = 0 ; /* params are all non-setting values (image name, key, version, output) */
2413+ int ret = 0 ;
2414+ int i ;
24122415
2413- /* Usage requires at least 4 params:
2414- * sign [OPTIONS] IMAGE.BIN KEY.DER VERSION */
2415- if (argc < 4 || argc > 14 ) {
2416- printf ("Usage: %s [options] image key version\n" , argv [0 ]);
2416+ /* Usage requires at least 2 params:
2417+ * sign [OPTIONS] IMAGE.BIN KEY.DER */
2418+ if (argc < 3 || argc > 14 ) {
2419+ printf ("Usage: %s [options] image key [ version] [output name] \n" , argv [0 ]);
24172420 printf ("For full usage manual, see 'docs/Signing.md'\n" );
24182421 exit (1 );
24192422 }
24202423
24212424 /* Parse Arguments */
24222425#ifdef DEBUG_SIGNTOOL
24232426 printf (" Debug Sign Tool Enabled:\n" );
2424- printf (" Looking at %d args\n" , argc );
2427+ printf (" Looking at %d args\n" , argc - 1 ); /* arg[0] is the exe name */
24252428#endif
24262429 for (i = 1 ; i < argc ; i ++ ) {
24272430#ifdef DEBUG_SIGNTOOL
@@ -2772,23 +2775,40 @@ static int process_args(int argc, char** argv) {
27722775 }
27732776 else {
27742777#ifdef DEBUG_SIGNTOOL
2775- printf (" (#%d is not a setting, abort processing of remaining %d args as settings)\n" , i , argc - i );
2778+ printf (" (#%d is not a setting, stop processing of remaining %d args as settings)\n" , i , argc - i );
27762779#endif
2777- if ((i == (argc - 3 )) || (i == (argc - 4 )))
2780+ if ((i >= (argc - ( REQUIRED_PARAM_CT + OPTIONAL_PARAM_CT ))) && (i <= (argc - REQUIRED_PARAM_CT )))
27782781 {
27792782 /* Looks like we have good parameters */
27802783#ifdef DEBUG_SIGNTOOL
2781- printf ("Detected positional arguments.\n" );
2782- printf ("Using:\n" );
2783- printf (" Image: %s\n" , argv [i + 0 ]);
2784- printf (" Key: %s\n" , argv [i + 1 ]);
2785- printf (" Version: %s\n" , argv [i + 2 ]);
2786- printf (" Output: %s\n" , argv [i + 3 ]);
2784+ if (param_ct == 0 ) {
2785+ param_ct = argc - i ;
2786+ printf ("Detected %d positional arguments.\n" , param_ct );
2787+ printf ("Using:\n" );
2788+ }
2789+ else {
2790+ if ((i == argc - param_ct )) {
2791+ /* param #1 is essential */
2792+ printf (" Image: %s\n" , argv [i + 0 ]);
2793+ }
2794+ if ((i == argc - param_ct + 1 )) {
2795+ /* param #2 is essential */
2796+ printf (" Key: %s\n" , argv [i + 1 ]);
2797+ }
2798+ if ((i == argc - param_ct + 2 )) {
2799+ /* param #3 is optional, version number */
2800+ printf (" Version: %s\n" , argv [i + 2 ]);
2801+ }
2802+ if ((i == argc - param_ct + 3 )) {
2803+ /* param #4 is optional, output name*/
2804+ printf (" Output: %s\n" , argv [i + 3 ]);
2805+ }
2806+ }
27872807#endif
27882808 }
27892809 else {
2790- printf ("Error: expected exactly 3 or 4 positional arguments after options, got %d.\n" , argc - i );
2791- printf ("Usage: %s [OPTIONS] IMAGE.BIN KEY.DER VERSION [output]\n" , argv [0 ]);
2810+ printf ("Error: expected exactly 2, 3 or 4 positional arguments after options, got %d.\n" , argc - i );
2811+ printf ("Usage: %s [OPTIONS] IMAGE.BIN KEY.DER [ VERSION] [output]\n" , argv [0 ]);
27922812
27932813 exit (1 );
27942814 }
0 commit comments