1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2018 Paweł Dembicki <paweldembicki@gmail.com>
7 * This tool is based on mktplinkfw.
8 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
9 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
16 #include <unistd.h> /* for unlink() */
18 #include <getopt.h> /* for getopt() */
25 #include <zlib.h> /*for crc32 */
27 #include "mkdlinkfw-lib.h"
29 extern char *progname
;
31 uint32_t jboot_timestamp(void)
33 char *env
= getenv("SOURCE_DATE_EPOCH");
35 time_t fixed_timestamp
= -1;
39 fixed_timestamp
= strtoull(env
, &endptr
, 10);
41 if (errno
|| (endptr
&& *endptr
!= '\0')) {
42 fprintf(stderr
, "Invalid SOURCE_DATE_EPOCH");
47 if (fixed_timestamp
== -1)
48 time(&fixed_timestamp
);
50 return (((uint32_t) fixed_timestamp
) - TIMESTAMP_MAGIC
) >> 2;
53 uint16_t jboot_checksum(uint16_t start_val
, uint16_t *data
, int size
)
55 uint32_t counter
= start_val
;
62 counter
= (uint16_t) counter
+ (counter
>> 16);
66 counter
+= *(uint8_t *) ptr
;
70 counter
= (uint16_t) counter
+ (counter
>> 16);
74 int get_file_stat(struct file_info
*fdata
)
79 if (fdata
->file_name
== NULL
)
82 res
= stat(fdata
->file_name
, &st
);
84 ERRS("stat failed on %s", fdata
->file_name
);
88 fdata
->file_size
= st
.st_size
;
92 int read_to_buf(const struct file_info
*fdata
, char *buf
)
95 int ret
= EXIT_FAILURE
;
98 f
= fopen(fdata
->file_name
, "r");
100 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
104 read
= fread(buf
, fdata
->file_size
, 1, f
);
105 if (ferror(f
) || read
!= 1) {
106 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
118 int write_fw(const char *ofname
, const char *data
, int len
)
121 int ret
= EXIT_FAILURE
;
123 f
= fopen(ofname
, "w");
125 ERRS("could not open \"%s\" for writing", ofname
);
130 fwrite(data
, len
, 1, f
);
132 ERRS("unable to write output file");
136 DBG("firmware file \"%s\" completed", ofname
);
143 if (ret
!= EXIT_SUCCESS
)