[source] firmware-utils: honor env SOURCE_DATE_EPOCH

LEDE Commits lede-commits at lists.infradead.org
Mon Jun 19 05:35:30 PDT 2017


lynxis pushed a commit to source.git, branch master:
https://git.lede-project.org/c47a1a3527d988b637c1daee573cbe0170ef73c6

commit c47a1a3527d988b637c1daee573cbe0170ef73c6
Author: Alexander Couzens <lynxis at fe80.eu>
AuthorDate: Sun Jun 18 02:21:21 2017 +0200

    firmware-utils: honor env SOURCE_DATE_EPOCH
    
    Use the timestamp from the enviroment SOURCE_DATE_EPOCH
    if set instead of the build time.
    Fixes reproducible builds for certain firmware images.
    
    Signed-off-by: Alexander Couzens <lynxis at fe80.eu>
---
 tools/firmware-utils/src/addpattern.c        | 20 ++++++++++++++++++-
 tools/firmware-utils/src/hcsmakeimage.c      | 30 ++++++++++++++++++++++++----
 tools/firmware-utils/src/imagetag.c          |  1 -
 tools/firmware-utils/src/jcgimage.c          | 24 +++++++++++++++++++++-
 tools/firmware-utils/src/mkrtn56uimg.c       |  1 -
 tools/firmware-utils/src/spw303v.c           |  1 -
 tools/firmware-utils/src/tplink-safeloader.c | 19 +++++++++++++++++-
 tools/firmware-utils/src/xorimage.c          |  1 -
 tools/firmware-utils/src/zyxbcm.c            |  1 -
 9 files changed, 86 insertions(+), 12 deletions(-)

diff --git a/tools/firmware-utils/src/addpattern.c b/tools/firmware-utils/src/addpattern.c
index 2ecc7ff..9bc4865 100644
--- a/tools/firmware-utils/src/addpattern.c
+++ b/tools/firmware-utils/src/addpattern.c
@@ -58,6 +58,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -141,6 +142,20 @@ void usage(void)
 	exit(EXIT_FAILURE);
 }
 
+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);
+		}
+        }
+}
+
 struct board_info *find_board(char *id)
 {
 	struct board_info *board;
@@ -273,7 +288,10 @@ int main(int argc, char **argv)
 		usage();
 	}
 
-	if (time(&t) == (time_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;
 	}
diff --git a/tools/firmware-utils/src/hcsmakeimage.c b/tools/firmware-utils/src/hcsmakeimage.c
index 603ea88..7baa7b5 100644
--- a/tools/firmware-utils/src/hcsmakeimage.c
+++ b/tools/firmware-utils/src/hcsmakeimage.c
@@ -5,7 +5,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 +43,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 +163,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");
diff --git a/tools/firmware-utils/src/imagetag.c b/tools/firmware-utils/src/imagetag.c
index 90fb7a4..bc70399 100644
--- a/tools/firmware-utils/src/imagetag.c
+++ b/tools/firmware-utils/src/imagetag.c
@@ -11,7 +11,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
-#include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
 #include <netinet/in.h>
diff --git a/tools/firmware-utils/src/jcgimage.c b/tools/firmware-utils/src/jcgimage.c
index 7755eab..354c26b 100644
--- a/tools/firmware-utils/src/jcgimage.c
+++ b/tools/firmware-utils/src/jcgimage.c
@@ -158,6 +158,20 @@ opensize(char *name, size_t *size)
 	return fd;
 }
 
+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);
+		}
+        }
+}
+
 /*
  * Write the JCG header
  */
@@ -167,6 +181,13 @@ mkjcgheader(struct jcg_header *h, size_t psize, char *version)
 	uLong crc;
 	uint16_t major = 0, minor = 0;
 	void *payload = (void *)h + sizeof(*h);
+	time_t t;
+
+	if (source_date_epoch != -1) {
+		t = source_date_epoch;
+	} else if ((time(&t) == (time_t)(-1))) {
+		err(1, "time call failed");
+	}
 
 	if (version != NULL) {
 		if (sscanf(version, "%hu.%hu", &major, &minor) != 2) {
@@ -177,7 +198,7 @@ mkjcgheader(struct jcg_header *h, size_t psize, char *version)
 	memset(h, 0, sizeof(*h));
 	h->jh_magic = htonl(JH_MAGIC);
 	h->jh_type  = htonl(1);
-	h->jh_time  = htonl(time(NULL));
+	h->jh_time  = htonl(t);
 	h->jh_major = htons(major);
 	h->jh_minor = htons(minor);
 
@@ -303,6 +324,7 @@ main(int argc, char **argv)
 	/* Make sure the headers have the right size */
 	assert(sizeof(struct jcg_header) == 512);
 	assert(sizeof(struct uimage_header) == 64);
+	set_source_date_epoch();
 
 	while ((c = getopt(argc, argv, "o:k:f:u:v:h")) != -1) {
 		switch (c) {
diff --git a/tools/firmware-utils/src/mkrtn56uimg.c b/tools/firmware-utils/src/mkrtn56uimg.c
index fe9ae2c..92aaf31 100644
--- a/tools/firmware-utils/src/mkrtn56uimg.c
+++ b/tools/firmware-utils/src/mkrtn56uimg.c
@@ -17,7 +17,6 @@
 #include <netinet/in.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
-#include <time.h>
 #include <unistd.h>
 #include <zlib.h>
 
diff --git a/tools/firmware-utils/src/spw303v.c b/tools/firmware-utils/src/spw303v.c
index 654d68d..684532d 100644
--- a/tools/firmware-utils/src/spw303v.c
+++ b/tools/firmware-utils/src/spw303v.c
@@ -23,7 +23,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
-#include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
diff --git a/tools/firmware-utils/src/tplink-safeloader.c b/tools/firmware-utils/src/tplink-safeloader.c
index fd4c2ab..d2a1cb6 100644
--- a/tools/firmware-utils/src/tplink-safeloader.c
+++ b/tools/firmware-utils/src/tplink-safeloader.c
@@ -709,6 +709,20 @@ static void free_image_partition(struct image_partition_entry entry) {
 	free(entry.data);
 }
 
+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);
+		}
+        }
+}
+
 /** Generates the partition-table partition */
 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
 	struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
@@ -752,7 +766,9 @@ static struct image_partition_entry make_soft_version(uint32_t rev) {
 
 	time_t t;
 
-	if (time(&t) == (time_t)(-1))
+	if (source_date_epoch != -1)
+		t = source_date_epoch;
+	else if (time(&t) == (time_t)(-1))
 		error(1, errno, "time");
 
 	struct tm *tm = localtime(&t);
@@ -1105,6 +1121,7 @@ int main(int argc, char *argv[]) {
 	bool add_jffs2_eof = false, sysupgrade = false;
 	unsigned rev = 0;
 	const struct device_info *info;
+	set_source_date_epoch();
 
 	while (true) {
 		int c;
diff --git a/tools/firmware-utils/src/xorimage.c b/tools/firmware-utils/src/xorimage.c
index b5ab83f..ca6e437 100644
--- a/tools/firmware-utils/src/xorimage.c
+++ b/tools/firmware-utils/src/xorimage.c
@@ -21,7 +21,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
-#include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
diff --git a/tools/firmware-utils/src/zyxbcm.c b/tools/firmware-utils/src/zyxbcm.c
index ffeb7cc..1a2926b 100644
--- a/tools/firmware-utils/src/zyxbcm.c
+++ b/tools/firmware-utils/src/zyxbcm.c
@@ -23,7 +23,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
-#include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
 



More information about the lede-commits mailing list