4 * Copyright (C) 2018 Paweł Dembicki <paweldembicki@gmail.com>
6 * This tool is based on mktplinkfw.
7 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
8 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
20 #include <unistd.h> /* for unlink() */
22 #include <getopt.h> /* for getopt() */
29 #include <zlib.h> /*for crc32 */
31 #include "mkdlinkfw-lib.h"
33 extern char *progname
;
35 uint32_t jboot_timestamp(void)
37 char *env
= getenv("SOURCE_DATE_EPOCH");
39 time_t fixed_timestamp
= -1;
43 fixed_timestamp
= strtoull(env
, &endptr
, 10);
45 if (errno
|| (endptr
&& *endptr
!= '\0')) {
46 fprintf(stderr
, "Invalid SOURCE_DATE_EPOCH");
51 if (fixed_timestamp
== -1)
52 time(&fixed_timestamp
);
54 return (((uint32_t) fixed_timestamp
) - TIMESTAMP_MAGIC
) >> 2;
57 uint16_t jboot_checksum(uint16_t start_val
, uint16_t *data
, int size
)
59 uint32_t counter
= start_val
;
66 counter
= (uint16_t) counter
+ (counter
>> 16);
70 counter
+= *(uint8_t *) ptr
;
74 counter
= (uint16_t) counter
+ (counter
>> 16);
78 int get_file_stat(struct file_info
*fdata
)
83 if (fdata
->file_name
== NULL
)
86 res
= stat(fdata
->file_name
, &st
);
88 ERRS("stat failed on %s", fdata
->file_name
);
92 fdata
->file_size
= st
.st_size
;
96 int read_to_buf(const struct file_info
*fdata
, char *buf
)
99 int ret
= EXIT_FAILURE
;
102 f
= fopen(fdata
->file_name
, "r");
104 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
108 read
= fread(buf
, fdata
->file_size
, 1, f
);
109 if (ferror(f
) || read
!= 1) {
110 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
122 int write_fw(const char *ofname
, const char *data
, int len
)
125 int ret
= EXIT_FAILURE
;
127 f
= fopen(ofname
, "w");
129 ERRS("could not open \"%s\" for writing", ofname
);
134 fwrite(data
, len
, 1, f
);
136 ERRS("unable to write output file");
140 DBG("firmware file \"%s\" completed", ofname
);
147 if (ret
!= EXIT_SUCCESS
)