Skip to content

Commit

Permalink
sonixflasher: introduce versioning
Browse files Browse the repository at this point in the history
also guard against offset selection without arg
  • Loading branch information
dexter93 committed May 31, 2023
1 parent 8218070 commit 49ef943
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions sonixflasher.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

#define MAX_ATTEMPTS 5

#define PROJECT_NAME "sonixflasher"
#define PROJECT_VER "1.0.0"

long MAX_FIRMWARE = MAX_FIRMWARE_SN32F260;

static void print_usage(char *m_name)
Expand All @@ -39,6 +42,7 @@ static void print_usage(char *m_name)
" --offset -o Set flashing offset (default: 0)\n"
" --file -f Binary of the firmware to flash (*.bin extension) \n"
" --jumploader -j Define if we are flashing a jumploader \n"
" --version -V Print version information\n"
"\n"
"Examples: \n"
". Flash jumploader to device w/ vid/pid 0x0c45/0x7040 \n"
Expand All @@ -50,6 +54,12 @@ static void print_usage(char *m_name)
"", m_name);
}

//Display program version
static void display_version(char *m_name)
{
fprintf(stderr,"%s " PROJECT_VER "\n",m_name);
}

void clear_buffer(unsigned char *data, size_t lenght)
{
for(int i = 0; i < lenght; i++) data[i] = 0;
Expand Down Expand Up @@ -246,26 +256,30 @@ int main(int argc, char* argv[])

if(argc < 2)
{
print_usage("sonixflasher");
print_usage(PROJECT_NAME);
exit(1);
}

struct option longoptions[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"vidpid", required_argument, NULL, 'v'},
{"offset", optional_argument, NULL, 'o'},
{"file", required_argument, NULL, 'f'},
{"jumploader", required_argument, NULL, 'j'},
{NULL,0,0,0}
};

while ((opt = getopt_long(argc, argv, "hv:o:f:j", longoptions, &opt_index)) != -1)
while ((opt = getopt_long(argc, argv, "hVv:o:f:j", longoptions, &opt_index)) != -1)
{
switch (opt)
{
case 0: // Show help
print_usage("sonixflasher");
case 'h': // Show help
print_usage(PROJECT_NAME);
break;
case 'V': // version
display_version(PROJECT_NAME);
break;
case 'v': // Set vid/pid
if( sscanf(optarg, "%4hx/%4hx", &vid,&pid) !=2 ) { // match "23FE/AB12"
Expand All @@ -292,6 +306,7 @@ int main(int argc, char* argv[])
{
case 'f':
case 'v':
case 'o':
fprintf(stderr, "ERROR: option '-%c' requires a parameter.\n", optopt);
break;
case 0:
Expand All @@ -303,7 +318,8 @@ int main(int argc, char* argv[])
}
exit(1);
}

// exit clean after printing
if(opt == 'h' || opt == 'V') exit(1);
}

if (file_name == NULL)
Expand Down

0 comments on commit 49ef943

Please sign in to comment.