ramips: fix USW-Flex reversed switch-port order
[openwrt/staging/hauke.git] / tools / firmware-utils / src / hcsmakeimage.c
index 603ea882b11b9e83b79fd8123b8fe1991ffc7db5..ce954cfcc9380b7871cbe3f7c8deef1477528b6f 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0-only
 #include <stdlib.h>
 #include <sys/types.h>
 #include <stdio.h>
@@ -5,7 +6,8 @@
 #include <string.h>
 #include <getopt.h>
 #include <unistd.h>
-#include <sys/time.h>
+#include <errno.h>
+#include <time.h>
 #include <sys/stat.h>
 #include <libgen.h>
 #include "bcmalgo.h"
@@ -42,6 +44,19 @@ static void print_help ( const char* ename )
        printf ( "\n" );
 }
 
+static time_t source_date_epoch = -1;
+static void set_source_date_epoch() {
+       char *env = getenv("SOURCE_DATE_EPOCH");
+       char *endptr = env;
+       errno = 0;
+        if (env && *env) {
+               source_date_epoch = strtoull(env, &endptr, 10);
+               if (errno || (endptr && *endptr != '\0')) {
+                       fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
+                       exit(1);
+               }
+        }
+}
 
 int main ( int argc, char** argv )
 {
@@ -149,11 +164,19 @@ int main ( int argc, char** argv )
        {
                fname = filename;
        }
-       struct timeval tm;
-       gettimeofday ( &tm,NULL );
+
+       time_t t = -1;
+       set_source_date_epoch();
+       if (source_date_epoch != -1) {
+               t = source_date_epoch;
+       } else if ((time(&t) == (time_t)(-1))) {
+               fprintf(stderr, "time call failed\n");
+               return EXIT_FAILURE;
+       }
+
        struct stat buf;
        stat ( input,&buf );
-       ldr_header_t* head = construct_header ( magicnum, (uint16_t) majrev, (uint16_t) minrev, ( uint32_t ) tm.tv_sec, ( uint32_t ) buf.st_size, ldaddress, fname, get_file_crc ( input ) );
+       ldr_header_t* head = construct_header ( magicnum, (uint16_t) majrev, (uint16_t) minrev, ( uint32_t ) t, ( uint32_t ) buf.st_size, ldaddress, fname, get_file_crc ( input ) );
        free(dupe);
        //uint32_t magic, uint16_t rev_maj,uint16_t rev_min, uint32_t build_date, uint32_t filelen, uint32_t ldaddress, const char* filename, uint32_t crc
        //FILE* fd = fopen ("/tftpboot/haxorware11rev32.bin","r");
@@ -161,6 +184,7 @@ int main ( int argc, char** argv )
        char* filebuffer = malloc ( buf.st_size+10 );
        FILE* fd = fopen ( input,"r" );
        fread ( filebuffer, 1, buf.st_size,fd );
+       fclose (fd);
        if (!output)
                {
                output = malloc(strlen(input+5));
@@ -172,10 +196,13 @@ int main ( int argc, char** argv )
        if (!fd_out)
                {
                fprintf(stderr, "Failed to open output file: %s\n", output);
+               free(filebuffer);
                exit(1);
                }
        fwrite ( head,1,sizeof ( ldr_header_t ),fd_out );
        fwrite ( filebuffer,1,buf.st_size,fd_out );
        printf("Firmware image %s is ready\n", output);
+       free(filebuffer);
+       fclose(fd_out);
        return 0;
 }