blob: 027f7aa461e294f5ba35939ac03bb15f51a19dbe [file] [log] [blame]
Zane Shelley352293d2023-04-06 17:38:15 -05001#!/usr/bin/env python3
2
3import argparse
4import glob
5import os
6
7from pyprd.chip_data.binary import binary_encode
8from pyprd.chip_data.json import json_decode
9from pyprd.chip_data.peltool import peltool_encode
10
11
12def _import_chip_data(indir: str) -> dict:
13 """
14 Iterates each JSON file in the given directory (all of which should be
15 properly formated Chip Data JSON). Then, combines the data into a singular
16 chip_data.Base object for each model/EC.
17 """
18
19 bases = dict()
20
21 for file in glob.glob(os.path.join(indir, "*.json")):
22 with open(file, "r") as fp:
23 for base in json_decode(fp).split():
24 # Should be only one model/EC
25 assert 1 == len(base.model_ec)
26 model_ec = base.model_ec[0]
27
28 if model_ec not in bases:
29 bases[model_ec] = base
30 else:
31 bases[model_ec].merge(base)
32
33 return bases
34
35
36def gen_chip_data_binary(indir: str, outdir: str) -> None:
37 for model_ec, base in _import_chip_data(indir).items():
38 file = f"chip_data_{model_ec.lower()}.cdb"
Caleb Palmera1c5fa52025-04-21 08:41:32 -050039 if not os.path.exists(outdir):
40 os.makedirs(outdir)
Zane Shelley352293d2023-04-06 17:38:15 -050041 with open(os.path.join(outdir, file), "wb") as fp:
42 binary_encode(model_ec, base, fp)
43
44
Caleb Palmera85f4432024-12-02 15:15:05 -060045def gen_peltool_json(cdIndir: str, outdir: str, exSigPath=None) -> None:
46 for model_ec, base in _import_chip_data(cdIndir).items():
Zane Shelley352293d2023-04-06 17:38:15 -050047 file = f"pel_parser_data_{model_ec.lower()}.json"
Caleb Palmera1c5fa52025-04-21 08:41:32 -050048 if not os.path.exists(outdir):
49 os.makedirs(outdir)
Zane Shelley352293d2023-04-06 17:38:15 -050050 with open(os.path.join(outdir, file), "w") as fp:
Caleb Palmera85f4432024-12-02 15:15:05 -060051 peltool_encode(model_ec, base, fp, exSigPath)
Zane Shelley352293d2023-04-06 17:38:15 -050052
53
54if __name__ == "__main__":
55 parser = argparse.ArgumentParser(
56 description="""
57 Parses the target Chip Data files and generates specified data files.
58 """
59 )
60
61 parser.add_argument(
62 "format",
63 type=str,
64 metavar="<format>",
65 choices=["bin", "pel"],
66 help="""
67 Output data type: 'bin' => Chip Data binary, 'pel' => BMC peltool JSON.
68 """,
69 )
70
71 parser.add_argument(
72 "indir",
73 type=str,
74 metavar="<input_path>",
75 help="""
76 Input directory containing the Chip Data files.
77 """,
78 )
79
80 parser.add_argument(
81 "outdir",
82 type=str,
83 metavar="<output_path>",
84 help="""
85 Output directory for the generated data.
86 """,
87 )
88
89 args = parser.parse_args()
90
91 funcs = {
92 "bin": "gen_chip_data_binary",
93 "pel": "gen_peltool_json",
94 }
95
96 globals()[funcs[args.format]](args.indir, args.outdir)