c98301d91ad3376c14cac1b5254caf51a5ac2873
[openwrt/staging/hauke.git] / scripts / cfe-wfi-tag.py
1 #!/usr/bin/env python3
2
3 """
4 Whole Flash Image Tag
5
6 {
7 u32 crc32;
8 u32 version;
9 u32 chipID;
10 u32 flashType;
11 u32 flags;
12 }
13
14 CRC32: Ethernet (Poly 0x04C11DB7)
15
16 Version:
17 0x00005700: Any version
18 0x00005731: NAND 1MB data partition
19 0x00005732: Normal version
20
21 Chip ID:
22 Broadcom Chip ID
23 0x00006328: BCM6328
24 0x00006362: BCM6362
25 0x00006368: BCM6368
26 0x00063268: BCM63268
27
28 Flash Type:
29 1: NOR
30 2: NAND 16k blocks
31 3: NAND 128k blocks
32 4: NAND 256k blocks
33 5: NAND 512k blocks
34 6: NAND 1MB blocks
35 7: NAND 2MB blocks
36
37 Flags:
38 0x00000001: PMC
39 0x00000002: Secure BootROM
40
41 """
42
43 import argparse
44 import os
45 import struct
46 import binascii
47
48
49 def auto_int(x):
50 return int(x, 0)
51
52 def create_tag(args, in_bytes):
53 # JAM CRC32 is bitwise not and unsigned
54 crc = (~binascii.crc32(in_bytes) & 0xFFFFFFFF)
55 tag = struct.pack('>IIIII', crc, args.tag_version, args.chip_id, args.flash_type, args.flags)
56 return tag
57
58 def create_output(args):
59 in_st = os.stat(args.input_file)
60 in_size = in_st.st_size
61
62 in_f = open(args.input_file, 'r+b')
63 in_bytes = in_f.read(in_size)
64 in_f.close()
65
66 tag = create_tag(args, in_bytes)
67
68 out_f = open(args.output_file, 'w+b')
69 out_f.write(in_bytes)
70 out_f.write(tag)
71 out_f.close()
72
73 def main():
74 global args
75
76 parser = argparse.ArgumentParser(description='')
77
78 parser.add_argument('--input-file',
79 dest='input_file',
80 action='store',
81 type=str,
82 help='Input file')
83
84 parser.add_argument('--output-file',
85 dest='output_file',
86 action='store',
87 type=str,
88 help='Output file')
89
90 parser.add_argument('--version',
91 dest='tag_version',
92 action='store',
93 type=auto_int,
94 help='WFI Tag Version')
95
96 parser.add_argument('--chip-id',
97 dest='chip_id',
98 action='store',
99 type=auto_int,
100 help='WFI Chip ID')
101
102 parser.add_argument('--flash-type',
103 dest='flash_type',
104 action='store',
105 type=auto_int,
106 help='WFI Flash Type')
107
108 parser.add_argument('--flags',
109 dest='flags',
110 action='store',
111 type=auto_int,
112 help='WFI Flags')
113
114 args = parser.parse_args()
115
116 if not args.flags:
117 args.flags = 0
118
119 if ((not args.input_file) or
120 (not args.output_file) or
121 (not args.tag_version) or
122 (not args.chip_id) or
123 (not args.flash_type)):
124 parser.print_help()
125 else:
126 create_output(args)
127
128 main()