3 # SPDX-License-Identifier: GPL-2.0-only
10 with
open(path
, 'rb') as f
:
13 def write(path
, data
):
14 with
open(path
, 'wb') as f
:
17 def padding(data
, size
, pattern
=b
'\0'):
18 return data
+ pattern
* (size
- len(data
))
20 def align(data
, size
, pattern
=b
'\0'):
21 return padding(data
, (len(data
) + (size
- 1)) & ~
(size
- 1), pattern
)
23 def gen_gfh_info(chip
, data
):
31 gfh_size
= struct
.calcsize(gfh_format
)
32 load_addr
= entries
[chip
] - gfh_size
33 load_size
= gfh_size
+ len(data
) + hashlib
.sha256().digest_size
35 gfh
= struct
.pack(gfh_format
,
36 0x014d4d4d, 0x00000038, 0x454c4946, 0x464e495f,
37 0x0000004f, 0x00000001, 0x01050001, load_addr
,
38 load_size
, 0x00020000, 0x000000a8, 0x00000020,
39 0x000000B0, 0x00000001, 0x014d4d4d, 0x0001000c,
40 0x00000001, 0x034d4d4d, 0x00070064, 0x00001182,
41 0x00000000, 0x00000000, 0x00000000, 0x00000000,
42 0x00000000, 0x00000000, 0x00000000, 0x00000000,
43 0x00000000, 0x00000000, 0x00000000, 0x00000000,
44 0x00000000, 0x00000000, 0x00000000, 0x00000000,
45 0x00000000, 0x00000000, 0x00006400, 0x00001388,
46 0x00000000, 0x00000000, 0x00000000, 0x00000000)
50 def gen_emmc_header(data
):
51 header
= (padding(struct
.pack('<12sII', b
'EMMC_BOOT', 1, 512), 512, b
'\xff') +
52 padding(struct
.pack('<8sIIIIIIII', b
'BRLYT', 1, 2048, 2048 + len(data
),
53 0x42424242, 0x00010005, 2048, 2048 + len(data
), 1) + b
'\0' * 140, 512,
58 def gen_sf_header(data
):
59 header
= (padding(struct
.pack('<12sII', b
'SF_BOOT', 1, 512), 512, b
'\xff') +
60 padding(struct
.pack('<8sIIIIIIII', b
'BRLYT', 1, 2048, 2048 + len(data
),
61 0x42424242, 0x00010007, 2048, 2048 + len(data
), 1) + b
'\0' * 140, 512,
67 'emmc': gen_emmc_header
,
71 def gen_preloader(chip_ver
, flash_type
, data
):
72 gfh_info
= gen_gfh_info(chip_ver
, data
)
73 gfh_hash
= hashlib
.sha256(gfh_info
+ data
).digest()
75 data
= align(gfh_info
+ data
+ gfh_hash
, 512, b
'\xff')
76 header
= gen_dev_header
[flash_type
](data
)
81 print('Usage: %s <chip> <flash_type> <input_file> <output_file>' % argv
[0])
82 print('\t flash_type: emmc|sf')
83 print('\t chip : mt8173|mt8183')
86 write(argv
[4], gen_preloader(argv
[1], argv
[2], read(argv
[3])))
88 if __name__
== '__main__':