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 static unsigned char jffs2_eof_mark[4] = { 0xde, 0xad, 0xc0, 0xde };
37 uint32_t jboot_timestamp(void)
41 return (((uint32_t) rawtime) - TIMESTAMP_MAGIC) >> 2;
44 uint16_t jboot_checksum(uint16_t start_val, uint16_t *data, int size)
46 uint32_t counter = start_val;
53 counter = (uint16_t) counter + (counter >> 16);
57 counter += *(uint8_t *) ptr;
61 counter = (uint16_t) counter + (counter >> 16);
65 int get_file_stat(struct file_info *fdata)
70 if (fdata->file_name == NULL)
73 res = stat(fdata->file_name, &st);
75 ERRS("stat failed on %s", fdata->file_name);
79 fdata->file_size = st.st_size;
83 int read_to_buf(const struct file_info *fdata, char *buf)
86 int ret = EXIT_FAILURE;
88 f = fopen(fdata->file_name, "r");
90 ERRS("could not open \"%s\" for reading", fdata->file_name);
95 fread(buf, fdata->file_size, 1, f);
97 ERRS("unable to read from file \"%s\"", fdata->file_name);
109 int pad_jffs2(char *buf, int currlen, int maxlen)
115 pad_mask = (4 * 1024) | (64 * 1024); /* EOF at 4KB and at 64KB */
116 while ((len < maxlen) && (pad_mask != 0)) {
120 for (i = 10; i < 32; i++) {
126 len = ALIGN(len, mask);
128 for (i = 10; i < 32; i++) {
130 if ((len & (mask - 1)) == 0)
134 for (i = 0; i < sizeof(jffs2_eof_mark); i++)
135 buf[len + i] = jffs2_eof_mark[i];
137 len += sizeof(jffs2_eof_mark);
143 int write_fw(const char *ofname, const char *data, int len)
146 int ret = EXIT_FAILURE;
148 f = fopen(ofname, "w");
150 ERRS("could not open \"%s\" for writing", ofname);
155 fwrite(data, len, 1, f);
157 ERRS("unable to write output file");
161 DBG("firmware file \"%s\" completed", ofname);
168 if (ret != EXIT_SUCCESS)