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