mktplinkfw-lib.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifndef mktplinkfw_lib_h
  14. #define mktplinkfw_lib_h
  15. #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
  16. #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
  17. #define MD5SUM_LEN 16
  18. /*
  19. * Message macros
  20. */
  21. #define ERR(fmt, ...) do { \
  22. fflush(0); \
  23. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  24. progname, ## __VA_ARGS__ ); \
  25. } while (0)
  26. #define ERRS(fmt, ...) do { \
  27. int save = errno; \
  28. fflush(0); \
  29. fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
  30. progname, ## __VA_ARGS__, strerror(save)); \
  31. } while (0)
  32. #define DBG(fmt, ...) do { \
  33. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  34. } while (0)
  35. struct file_info {
  36. char *file_name; /* name of the file */
  37. uint32_t file_size; /* length of the file */
  38. };
  39. struct flash_layout {
  40. char *id;
  41. uint32_t fw_max_len;
  42. uint32_t kernel_la;
  43. uint32_t kernel_ep;
  44. uint32_t rootfs_ofs;
  45. };
  46. struct flash_layout *find_layout(struct flash_layout *layouts, const char *id);
  47. void get_md5(const char *data, int size, uint8_t *md5);
  48. int get_file_stat(struct file_info *fdata);
  49. int read_to_buf(const struct file_info *fdata, char *buf);
  50. int write_fw(const char *ofname, const char *data, int len);
  51. inline void inspect_fw_pstr(const char *label, const char *str);
  52. inline void inspect_fw_phex(const char *label, uint32_t val);
  53. inline void inspect_fw_phexdec(const char *label, uint32_t val);
  54. inline void inspect_fw_pmd5sum(const char *label, const uint8_t *val, const char *text);
  55. int build_fw(size_t header_size);
  56. #endif /* mktplinkfw_lib_h */