firmware-utils: replace GPL 2.0 boilerplate/reference with SPDX
[openwrt/staging/ldir.git] / tools / firmware-utils / src / mktplinkfw-lib.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
4 *
5 * This tool was based on:
6 * TP-Link WR941 V2 firmware checksum fixing tool.
7 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
8 */
9
10
11 #ifndef mktplinkfw_lib_h
12 #define mktplinkfw_lib_h
13
14 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
15 #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
16
17 #define MD5SUM_LEN 16
18
19 /*
20 * Message macros
21 */
22 #define ERR(fmt, ...) do { \
23 fflush(0); \
24 fprintf(stderr, "[%s] *** error: " fmt "\n", \
25 progname, ## __VA_ARGS__ ); \
26 } while (0)
27
28 #define ERRS(fmt, ...) do { \
29 int save = errno; \
30 fflush(0); \
31 fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
32 progname, ## __VA_ARGS__, strerror(save)); \
33 } while (0)
34
35 #define DBG(fmt, ...) do { \
36 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
37 } while (0)
38
39
40 struct file_info {
41 char *file_name; /* name of the file */
42 uint32_t file_size; /* length of the file */
43 };
44
45 struct flash_layout {
46 char *id;
47 uint32_t fw_max_len;
48 uint32_t kernel_la;
49 uint32_t kernel_ep;
50 uint32_t rootfs_ofs;
51 };
52
53 struct flash_layout *find_layout(struct flash_layout *layouts, const char *id);
54 void get_md5(const char *data, int size, uint8_t *md5);
55 int get_file_stat(struct file_info *fdata);
56 int read_to_buf(const struct file_info *fdata, char *buf);
57 int write_fw(const char *ofname, const char *data, int len);
58 inline void inspect_fw_pstr(const char *label, const char *str);
59 inline void inspect_fw_phex(const char *label, uint32_t val);
60 inline void inspect_fw_phexdec(const char *label, uint32_t val);
61 inline void inspect_fw_pmd5sum(const char *label, const uint8_t *val, const char *text);
62 int build_fw(size_t header_size);
63
64 #endif /* mktplinkfw_lib_h */