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@fe80.eu>
This commit is contained in:
Alexander Couzens 2017-06-18 02:21:21 +02:00
parent 77dc6a2ae7
commit c47a1a3527
9 changed files with 86 additions and 12 deletions

View file

@ -58,6 +58,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -141,6 +142,20 @@ void usage(void)
exit(EXIT_FAILURE); 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 *find_board(char *id)
{ {
struct board_info *board; struct board_info *board;
@ -273,7 +288,10 @@ int main(int argc, char **argv)
usage(); 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"); fprintf(stderr, "time call failed\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View file

@ -5,7 +5,8 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <errno.h>
#include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <libgen.h> #include <libgen.h>
#include "bcmalgo.h" #include "bcmalgo.h"
@ -42,6 +43,19 @@ static void print_help ( const char* ename )
printf ( "\n" ); 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 ) int main ( int argc, char** argv )
{ {
@ -149,11 +163,19 @@ int main ( int argc, char** argv )
{ {
fname = filename; 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; struct stat buf;
stat ( input,&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); 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 //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"); //FILE* fd = fopen ("/tftpboot/haxorware11rev32.bin","r");

View file

@ -11,7 +11,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <netinet/in.h> #include <netinet/in.h>

View file

@ -158,6 +158,20 @@ opensize(char *name, size_t *size)
return fd; 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 * Write the JCG header
*/ */
@ -167,6 +181,13 @@ mkjcgheader(struct jcg_header *h, size_t psize, char *version)
uLong crc; uLong crc;
uint16_t major = 0, minor = 0; uint16_t major = 0, minor = 0;
void *payload = (void *)h + sizeof(*h); 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 (version != NULL) {
if (sscanf(version, "%hu.%hu", &major, &minor) != 2) { 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)); memset(h, 0, sizeof(*h));
h->jh_magic = htonl(JH_MAGIC); h->jh_magic = htonl(JH_MAGIC);
h->jh_type = htonl(1); h->jh_type = htonl(1);
h->jh_time = htonl(time(NULL)); h->jh_time = htonl(t);
h->jh_major = htons(major); h->jh_major = htons(major);
h->jh_minor = htons(minor); h->jh_minor = htons(minor);
@ -303,6 +324,7 @@ main(int argc, char **argv)
/* Make sure the headers have the right size */ /* Make sure the headers have the right size */
assert(sizeof(struct jcg_header) == 512); assert(sizeof(struct jcg_header) == 512);
assert(sizeof(struct uimage_header) == 64); assert(sizeof(struct uimage_header) == 64);
set_source_date_epoch();
while ((c = getopt(argc, argv, "o:k:f:u:v:h")) != -1) { while ((c = getopt(argc, argv, "o:k:f:u:v:h")) != -1) {
switch (c) { switch (c) {

View file

@ -17,7 +17,6 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <zlib.h> #include <zlib.h>

View file

@ -23,7 +23,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>

View file

@ -709,6 +709,20 @@ static void free_image_partition(struct image_partition_entry entry) {
free(entry.data); 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 */ /** Generates the partition-table partition */
static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) { 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); 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; 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"); error(1, errno, "time");
struct tm *tm = localtime(&t); struct tm *tm = localtime(&t);
@ -1105,6 +1121,7 @@ int main(int argc, char *argv[]) {
bool add_jffs2_eof = false, sysupgrade = false; bool add_jffs2_eof = false, sysupgrade = false;
unsigned rev = 0; unsigned rev = 0;
const struct device_info *info; const struct device_info *info;
set_source_date_epoch();
while (true) { while (true) {
int c; int c;

View file

@ -21,7 +21,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>

View file

@ -23,7 +23,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>