From 459cbb46e8155fd242159f73fb1d61c9b2828c70 Mon Sep 17 00:00:00 2001 From: martinSusz <119517241+martinSusz@users.noreply.github.com> Date: Wed, 30 Nov 2022 21:58:00 +0200 Subject: [PATCH 1/5] Making creation of loader file reproducible by setting fixed rkTime (in my case the birthday of my daugther), more info https://reproducible-builds.org/ --- main.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 72bd94b..074c6c9 100644 --- a/main.cpp +++ b/main.cpp @@ -1393,15 +1393,20 @@ static inline bool getFileSize(const char *path, uint32_t* size) { static inline rk_time getTime(void) { rk_time rkTime; + /*** Making creation of rkxx_loader_vx.xx.xxx.bin file reproducible + by setting fixed rkTime (in my case the birthday of my daugther), + more info https://reproducible-builds.org/ + struct tm *tm; time_t tt = time(NULL); - tm = localtime(&tt); - rkTime.year = tm->tm_year + 1900; - rkTime.month = tm->tm_mon + 1; - rkTime.day = tm->tm_mday; - rkTime.hour = tm->tm_hour; - rkTime.minute = tm->tm_min; - rkTime.second = tm->tm_sec; + tm = localtime(&tt); + ***/ + rkTime.year = 2021; // tm->tm_year + 1900; + rkTime.month = 12; // tm->tm_mon + 1; + rkTime.day = 15; // tm->tm_mday; + rkTime.hour = 13; // tm->tm_hour; + rkTime.minute = 00; // tm->tm_min; + rkTime.second = 00; // tm->tm_sec; printf("%d-%d-%d %02d:%02d:%02d\n", rkTime.year, rkTime.month, rkTime.day, rkTime.hour, rkTime.minute, rkTime.second); From 8aaf80cd1321a852107d974eb431f2a4857ed5b5 Mon Sep 17 00:00:00 2001 From: martinSusz <119517241+martinSusz@users.noreply.github.com> Date: Thu, 1 Dec 2022 22:56:58 +0200 Subject: [PATCH 2/5] Optional reproducibility feature for PACK arg Adding new parameter for PACK argument that will trig the reproducibility feature --- main.cpp | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/main.cpp b/main.cpp index 074c6c9..e1c1810 100644 --- a/main.cpp +++ b/main.cpp @@ -60,7 +60,7 @@ void usage() printf("ReadFlashInfo:\t\trfi\r\n"); printf("ReadChipInfo:\t\trci\r\n"); printf("ReadCapability:\t\trcb\r\n"); - printf("PackBootLoader:\t\tpack\r\n"); + printf("PackBootLoader:\t\tpack \r\n"); printf("UnpackBootLoader:\tunpack \r\n"); printf("TagSPL:\t\t\ttagspl \r\n"); printf("-------------------------------------------------------\r\n\r\n"); @@ -1390,23 +1390,29 @@ static inline bool getFileSize(const char *path, uint32_t* size) { return true; } -static inline rk_time getTime(void) { +static inline rk_time getTime(bool reproducible) { rk_time rkTime; - - /*** Making creation of rkxx_loader_vx.xx.xxx.bin file reproducible - by setting fixed rkTime (in my case the birthday of my daugther), - more info https://reproducible-builds.org/ - - struct tm *tm; - time_t tt = time(NULL); - tm = localtime(&tt); - ***/ - rkTime.year = 2021; // tm->tm_year + 1900; - rkTime.month = 12; // tm->tm_mon + 1; - rkTime.day = 15; // tm->tm_mday; - rkTime.hour = 13; // tm->tm_hour; - rkTime.minute = 00; // tm->tm_min; - rkTime.second = 00; // tm->tm_sec; + if (reproducible) { + /*** Making creation of rkxx_loader_vx.xx.xxx.bin file reproducible + by setting fixed rkTime (in this case the birthday of my daugther), + more info https://reproducible-builds.org/ ***/ + rkTime.year = 2021; + rkTime.month = 12; + rkTime.day = 15; + rkTime.hour = 13; + rkTime.minute = 00; + rkTime.second = 00; + } else { + struct tm *tm; + time_t tt = time(NULL); + tm = localtime(&tt); + rkTime.year = tm->tm_year + 1900; + rkTime.month = tm->tm_mon + 1; + rkTime.day = tm->tm_mday; + rkTime.hour = tm->tm_hour; + rkTime.minute = tm->tm_min; + rkTime.second = tm->tm_sec; + } printf("%d-%d-%d %02d:%02d:%02d\n", rkTime.year, rkTime.month, rkTime.day, rkTime.hour, rkTime.minute, rkTime.second); @@ -1547,13 +1553,13 @@ static inline uint32_t getChipType(const char* chip) { return chipType; } -static inline void getBoothdr(rk_boot_header* hdr) { +static inline void getBoothdr(rk_boot_header* hdr, bool reproducible) { memset(hdr, 0, sizeof(rk_boot_header)); hdr->tag = TAG; hdr->size = sizeof(rk_boot_header); hdr->version = (getBCD(gOpts.major) << 8) | getBCD(gOpts.minor); hdr->mergerVersion = MERGER_VERSION; - hdr->releaseTime = getTime(); + hdr->releaseTime = getTime(reproducible); hdr->chipType = getChipType(gOpts.chip); hdr->code471Num = gOpts.code471Num; @@ -1590,7 +1596,7 @@ static inline uint32_t getCrc(const char* path) { return crc; } -bool mergeBoot(void) { +bool mergeBoot(bool reproducible) { uint32_t dataOffset; bool ret = false; int i; @@ -1622,7 +1628,7 @@ bool mergeBoot(void) { goto end; } - getBoothdr(&hdr); + getBoothdr(&hdr, reproducible); printf("Writing header...\n"); fwrite(&hdr, 1, sizeof(rk_boot_header), outFile); @@ -3075,7 +3081,7 @@ bool handle_command(int argc, char* argv[], CRKScan *pScan) printf("rkdeveloptool ver %s\r\n", PACKAGE_VERSION); return true; } else if (strcmp(strCmd.c_str(), "PACK") == 0) {//pack boot loader - mergeBoot(); + mergeBoot(argc > 2); return true; } else if (strcmp(strCmd.c_str(), "UNPACK") == 0) {//unpack boot loader string strLoader = argv[2]; From 21cebf476e780ec1d13d57740cb53a8c9b0a0b79 Mon Sep 17 00:00:00 2001 From: martinSusz <119517241+martinSusz@users.noreply.github.com> Date: Fri, 2 Dec 2022 00:37:40 +0200 Subject: [PATCH 3/5] Supporting SOURCE_DATE_EPOCH environment variable Credits https://reproducible-builds.org/docs/source-date-epoch/ --- main.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index e1c1810..c44d3d8 100644 --- a/main.cpp +++ b/main.cpp @@ -1404,7 +1404,17 @@ static inline rk_time getTime(bool reproducible) { rkTime.second = 00; } else { struct tm *tm; - time_t tt = time(NULL); + time_t tt; + time_t now; + char *source_date_epoch; + /* This assumes that the SOURCE_DATE_EPOCH environment variable will contain + a correct, positive integer in the time_t range */ + if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) == NULL || + (now = (time_t)strtoll(source_date_epoch, NULL, 10)) <= 0) + tt = time(&now); + else + tt = time(NULL); + tm = localtime(&tt); rkTime.year = tm->tm_year + 1900; rkTime.month = tm->tm_mon + 1; From 1e6322cafb6e7d5b08c0427e890fa6bc89a535e2 Mon Sep 17 00:00:00 2001 From: martinSusz <119517241+martinSusz@users.noreply.github.com> Date: Sat, 3 Dec 2022 01:40:02 +0200 Subject: [PATCH 4/5] fixing truncation error Alternative solution to https://github.com/rockchip-linux/rkdeveloptool/pull/67 --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index c44d3d8..604de7f 100644 --- a/main.cpp +++ b/main.cpp @@ -1511,7 +1511,7 @@ static bool saveEntry(FILE* outFile, char* path, rk_entry_type type, static inline uint32_t convertChipType(const char* chip) { char buffer[5]; memset(buffer, 0, sizeof(buffer)); - snprintf(buffer, sizeof(buffer), "%s", chip); + memcpy(buffer, chip, sizeof(buffer)); return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; } From a8b95c4902a3c6d62aecfedeffad02ce915cbe17 Mon Sep 17 00:00:00 2001 From: martinSusz <119517241+martinSusz@users.noreply.github.com> Date: Sat, 3 Dec 2022 02:18:44 +0200 Subject: [PATCH 5/5] Replacing memcpy with safer memccpy Credits to: https://github.com/DorianRudolph/rkdeveloptool/commit/df4656eedc5fc536f55b77d6b126330755d53fad --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 604de7f..a2dab22 100644 --- a/main.cpp +++ b/main.cpp @@ -1511,7 +1511,7 @@ static bool saveEntry(FILE* outFile, char* path, rk_entry_type type, static inline uint32_t convertChipType(const char* chip) { char buffer[5]; memset(buffer, 0, sizeof(buffer)); - memcpy(buffer, chip, sizeof(buffer)); + memccpy(buffer, chip, '\0', sizeof(buffer)); return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; }