2 #------------------------------------------------------------------------------
3 # $File: tplink,v 1.5 2020/03/28 23:14:26 christos Exp $
4 # tplink: File magic for openwrt firmware files
6 # URL: https://wiki.openwrt.org/doc/techref/header
7 # Reference: https://git.openwrt.org/?p=openwrt.git;a=blob;f=tools/firmware-utils/src/mktplinkfw.c
9 # check for valid header version 1 or 2
12 # test for header padding with nulls
14 # skip Norton Commander Cleanup Utility NCCLEAN.INI by looking for valid vendor
15 >>>4 ubelong >0x1F000000
16 # skip user.dbt by looking for positive hardware id
18 >>>>>0 use firmware-tplink
20 0 name firmware-tplink
22 !:mime application/x-tplink-bin
24 # hardware id like 10430001 07410001 09410004 09410006
27 # hardware revision like 1
28 >0x44 ubelong !1 (revision %u)
29 # vendor_name[24] like OpenWrt or TP-LINK Technologies
31 # fw_version[36] like r49389 or ver. 1.0
33 # header version 1 or 2
35 # ver_hi.ver_mid.ver_lo
36 >0x98 long !0 \b, version
38 >>0x9A ubeshort x \b.%u
39 >>0x9C ubeshort x \b.%u
40 # region code 0~universal 1~US
42 #>>0x48 ubelong 0 (universal)
44 >>0x48 ubelong >1 (region %u)
45 # total length of the firmware. not always true
46 >0x7C ubelong x \b, %u bytes or less
48 >0x48 ubelong !0 \b, UNKNOWN1 0x%x
50 #>0x4c ubequad x \b, MD5 %llx
51 #>>0x54 ubequad x \b%llx
53 >0x5c ubelong !0 \b, UNKNOWN2 0x%x
55 #>0x60 ubequad !0 \b, 2nd MD5 %llx
56 #>>0x68 ubequad x \b%llx
58 >0x70 ubelong !0 \b, UNKNOWN3 0x%x
60 #>0x74 ubelong x \b, 0x%x load
62 #>0x78 ubelong x \b, 0x%x entry
63 # kernel data offset. 200h means direct after header
64 >0x80 ubelong x \b, at 0x%x
65 # kernel data length and 1 space
66 >0x84 ubelong x %u bytes
67 # look for kernel type (gzip compressed vmlinux.bin by ./compress)
69 # root file system data offset
70 # WRONG in 5.35 with above indirect expression
71 >0x88 ubelong x \b, at 0x%x
72 # rootfs data length and 1 space
73 >0x8C ubelong x %u bytes
74 # in 5.32 only true for offset ~< FILE_BYTES_MAX=9 MB defined in ../../src/file.h
76 # 'qshs' for wr940nv1_en_3_13_7_up(111228).bin
77 #>(0x88.L) string x \b, file system '%.4s'
78 #>(0x88.L) ubequad x \b, file system 0x%llx
79 # bootloader data offset
80 >0x90 ubelong !0 \b, at 0x%x
81 # bootloader data length only resonable if bootloader offset not null
82 >>0x94 ubelong !0 %u bytes
83 # pad[354] should be 354 null bytes.
84 #>0x9E ubequad !0 \b, padding 0x%llx
85 # But at 0x120 18 non null bytes in examples like
86 # wr940nv4_eu_3_16_9_up_boot(160620).bin
87 # wr940nv6_us_3_18_1_up_boot(171030).bin
88 #>0x120 ubequad !0 \b, other padding 0x%llx