+// SPDX-License-Identifier: GPL-2.0-only
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#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"
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 )
{
{
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");
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));
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;
}