summaryrefslogtreecommitdiffstats
path: root/scripts/cfe-wfi-tag.py
blob: 5fac8ee4751974821bff4d4ea327d6a120c741d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3

"""
Whole Flash Image Tag

{
	u32 crc32;
	u32 version;
	u32 chipID;
	u32 flashType;
	u32 flags;
}

CRC32: Ethernet (Poly 0x04C11DB7)

Version:
	0x00005700: Any version
	0x00005731: NAND 1MB data partition
	0x00005732: Normal version

Chip ID:
	Broadcom Chip ID
	0x00006328: BCM6328
	0x00006362: BCM6362
	0x00006368: BCM6368
	0x00063268: BCM63268

Flash Type:
	1: NOR
	2: NAND 16k blocks
	3: NAND 128k blocks
	4: NAND 256k blocks
	5: NAND 512k blocks
	6: NAND 1MB blocks
	7: NAND 2MB blocks

Flags:
	0x00000001: PMC
	0x00000002: Secure BootROM

"""

import argparse
import os
import struct
import binascii


def auto_int(x):
    return int(x, 0)


def create_tag(args, in_bytes):
    # JAM CRC32 is bitwise not and unsigned
    crc = ~binascii.crc32(in_bytes) & 0xFFFFFFFF
    tag = struct.pack(
        ">IIIII",
        crc,
        args.tag_version,
        args.chip_id,
        args.flash_type,
        args.flags,
    )
    return tag


def create_output(args):
    in_st = os.stat(args.input_file)
    in_size = in_st.st_size

    in_f = open(args.input_file, "r+b")
    in_bytes = in_f.read(in_size)
    in_f.close()

    tag = create_tag(args, in_bytes)

    out_f = open(args.output_file, "w+b")
    out_f.write(in_bytes)
    out_f.write(tag)
    out_f.close()


def main():
    global args

    parser = argparse.ArgumentParser(description="")

    parser.add_argument(
        "--input-file",
        dest="input_file",
        action="store",
        type=str,
        help="Input file",
    )

    parser.add_argument(
        "--output-file",
        dest="output_file",
        action="store",
        type=str,
        help="Output file",
    )

    parser.add_argument(
        "--version",
        dest="tag_version",
        action="store",
        type=auto_int,
        help="WFI Tag Version",
    )

    parser.add_argument(
        "--chip-id",
        dest="chip_id",
        action="store",
        type=auto_int,
        help="WFI Chip ID",
    )

    parser.add_argument(
        "--flash-type",
        dest="flash_type",
        action="store",
        type=auto_int,
        help="WFI Flash Type",
    )

    parser.add_argument(
        "--flags", dest="flags", action="store", type=auto_int, help="WFI Flags"
    )

    args = parser.parse_args()

    if not args.flags:
        args.flags = 0

    if (
        (not args.input_file)
        or (not args.output_file)
        or (not args.tag_version)
        or (not args.chip_id)
        or (not args.flash_type)
    ):
        parser.print_help()
    else:
        create_output(args)


main()