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)
39 return (((uint32_t) rawtime
) - TIMESTAMP_MAGIC
) >> 2;
42 uint16_t jboot_checksum(uint16_t start_val
, uint16_t *data
, int size
)
44 uint32_t counter
= start_val
;
51 counter
= (uint16_t) counter
+ (counter
>> 16);
55 counter
+= *(uint8_t *) ptr
;
59 counter
= (uint16_t) counter
+ (counter
>> 16);
63 int get_file_stat(struct file_info
*fdata
)
68 if (fdata
->file_name
== NULL
)
71 res
= stat(fdata
->file_name
, &st
);
73 ERRS("stat failed on %s", fdata
->file_name
);
77 fdata
->file_size
= st
.st_size
;
81 int read_to_buf(const struct file_info
*fdata
, char *buf
)
84 int ret
= EXIT_FAILURE
;
86 f
= fopen(fdata
->file_name
, "r");
88 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
93 fread(buf
, fdata
->file_size
, 1, f
);
95 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
107 int write_fw(const char *ofname
, const char *data
, int len
)
110 int ret
= EXIT_FAILURE
;
112 f
= fopen(ofname
, "w");
114 ERRS("could not open \"%s\" for writing", ofname
);
119 fwrite(data
, len
, 1, f
);
121 ERRS("unable to write output file");
125 DBG("firmware file \"%s\" completed", ofname
);
132 if (ret
!= EXIT_SUCCESS
)