Debian Codegraph @

util/intelp2m/cli/options.go

raw symbols SCIP
1 package cli
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "review.coreboot.org/coreboot.git/util/intelp2m/config/p2m"
10 )
11
12 var name = filepath.Base(os.Args[0])
13
14 const usagePlatform = `usage: -platform <type>
15 type: adl | apl | cnl | ebg | ehl | jsl | lbg | mtl | snr | tgl | ?
16 adl - Alder Lake PCH
17 apl - Apollo Lake SoC
18 cnl - CannonLake-LP or Whiskeylake/Coffeelake/Cometlake-U SoC
19 ebg - Emmitsburg PCH with Xeon SP
20 ehl - Elkhart Lake SoC
21 jsl - Jasper Lake SoC
22 lbg - Lewisburg PCH with Xeon SP
23 mtl - MeteorLake SoC
24 snr - Sunrise PCH or Skylake/Kaby Lake SoC
25 tgl - TigerLake-H SoC
26 ? - show details
27 `
28 const usageFields = `usage: -fields <type>
29 type: cb | fsp | raw | ?
30 cb - coreboot style
31 fsp - Intel FSP style
32 raw - raw register value (don't convert, print as is)
33 ? - show details
34 `
35 const usageExclude = `usage: -e | -exclude
36 Exclude fields that should be ignored to generate "short" macro.
37 Details:
38 The utility should ignore "PAD_TRIG(OFF)" field in the "_PAD_CFG_STRUCT(GPP_I12,
39 PAD_FUNC(GPIO) | PAD_RESET(PLTRST) | PAD_TRIG(OFF) | PAD_BUF(RX_DISABLE) | 1, 0)"
40 "long" macro to generate the "PAD_CFG_GPO(GPP_I12, 1, PLTRST)" macro.
41
42 The utility with this option generates the "long" macro with only those fields that are
43 required to create the "short" one (in thia case without the "PAD_TRIG(OFF)" field):
44 "_PAD_CFG_STRUCT(GPP_I12, PAD_FUNC(GPIO) | PAD_RESET(PLTRST) | PAD_BUF(RX_DISABLE) | 1, 0)"
45 `
46
47 const usageUnchecked = `usage: -u | -unchecked
48 Disable automatic bitfield checking before generating.
49 Details:
50 The utility automatically checks the bit fields of the DW registers before generating
51 the macro. If a bit is set in the register and it is not used in the "short" macro, the
52 utility will generate the "long" one instead of the first one.
53
54 This option is used to disable automatic verification. In this case, all macros are
55 generated in the "short" format:
56 PAD_NC(GPP_F18, NONE),
57 PAD_CFG_NF(GPP_F19, NONE, PLTRST, NF1),
58 ...
59 `
60
61 const usageInfo = `usage: -i | -ii | -iii | -iiii
62 Generate additional information about macros.
63 Level 1 information (-i):
64 _PAD_CFG_STRUCT(GPP_A4, PAD_FUNC(NF1) | PAD_RESET(PLTRST) | (1 << 1), 0), /* LAD3 */
65
66 Level 2 information (-ii):
67 /* GPP_A4 - LAD3 */
68 /* DW0: 0x80000402, DW1: 0x00000000 */
69 _PAD_CFG_STRUCT(GPP_A4, PAD_FUNC(NF1) | PAD_RESET(PLTRST) | (1 << 1), 0),
70
71 Level 3 information (-iii):
72 /* GPP_A3 - LAD2 */
73 /* DW0: 0x80000402, DW1: 0x00000000 */
74 /* DW0: (1 << 1) - IGNORED */
75 _PAD_CFG_STRUCT(GPP_A3, PAD_FUNC(NF1) | PAD_RESET(PLTRST) | (1 << 1), 0),
76
77 Level 4 information (-iiii):
78 /* GPP_A4 - LAD3 */
79 /* DW0: 0x80000402, DW1: 0x00000000 */
80 /* DW0: (1 << 1) - IGNORED */
81 /* PAD_CFG_NF(GPP_A4, NONE, PLTRST, NF1), */
82 _PAD_CFG_STRUCT(GPP_A4, PAD_FUNC(NF1) | PAD_RESET(PLTRST) | (1 << 1), 0),
83 `
84
85 const usage = `
86 basic functions:
87 -h | -help Print help
88 -v | -version Print version
89 -file <path> Set path to the inteltool file. <inteltool.log> by default
90 -out <path> Set path to the generated file. <generate/gpio.h> by default
91 -logs <path> Override the log file path. <logs.txt> by default
92 -p | -platform <type> Set the PCH platform type. <sunrise> by default
93 (enter ? to show datails)
94
95 generation of long composite macros with bit field definitions:
96 -f | -fields <type> Set the bitfield type for the generated long macro
97 (enter ? to show datails)
98
99 control flags:
100 -e | -exclude Exclude fields that should be ignored to generate short macro
101 -u | -unchecked Disable automatic bitfield checking before generating
102 (enter ? to show datails)
103
104 generate additional information:
105 -i Add function to the comments
106 -ii Add DW0/DW1 register value to the comments
107 -iii Add ignored bitfields to the comments
108 -iiii Add target PAD_CFG() macro to the comments
109 (enter ? to show datails)
110 `
111
112 func Usage() {
113 fmt.Printf(`usage: %s <switches> %s`, name, usage)
114 }
115
116 func cbOptionsPlatform(value string) error {
117 if value == "?" {
118 fmt.Printf("%s", usagePlatform)
119 os.Exit(0)
120 }
121
122 if err := p2m.SetPlatformType(value); err != nil {
123 fmt.Printf("error: %v\n%s", err, usagePlatform)
124 os.Exit(0)
125 }
126
127 return nil
128 }
129
130 func cbOptionsFields(value string) error {
131 if value == "?" {
132 fmt.Printf("%s", usageFields)
133 os.Exit(0)
134 }
135
136 if err := p2m.SetFieldType(value); err != nil {
137 fmt.Printf("error: %v\n%s", err, usageFields)
138 os.Exit(0)
139 }
140
141 return nil
142 }
143
144 func cbOptionsExclude(_ string) error {
145 if numArgs := flag.NArg(); numArgs == 1 {
146 if flag.Arg(0) == "?" {
147 fmt.Printf("%s", usageExclude)
148 }
149 Usage()
150 os.Exit(0)
151 }
152 p2m.Config.IgnoredFields = true
153 return nil
154 }
155
156 func cbOptionsUnchecked(_ string) error {
157 if numArgs := flag.NArg(); numArgs == 1 {
158 if flag.Arg(0) == "?" {
159 fmt.Printf("%s", usageUnchecked)
160 }
161 Usage()
162 os.Exit(0)
163 }
164 p2m.Config.AutoCheck = false
165 return nil
166 }
167
168 func cbOptionsInfo(_ string) error {
169 if numArgs := flag.NArg(); numArgs == 1 {
170 if flag.Arg(0) == "?" {
171 fmt.Printf("%s", usageInfo)
172 os.Exit(0)
173 }
174 Usage()
175 os.Exit(0)
176 }
177
178 table := map[string]int{
179 "-i": 1,
180 "-ii": 2,
181 "-iii": 3,
182 "-iiii": 4,
183 }
184 for _, arg := range os.Args {
185 if level, exist := table[arg]; exist {
186 p2m.Config.GenLevel = level
187 return nil
188 }
189 }
190 Usage()
191 return nil
192 }
193
194 func ParseOptions() {
195 flag.Usage = Usage
196
197 flag.StringVar(&p2m.Config.InputPath, "file", "inteltool.log", "")
198 flag.StringVar(&p2m.Config.OutputPath, "out", "output/gpio.h", "")
199 flag.StringVar(&p2m.Config.LogsPath, "logs", "logs.txt", "")
200
201 help := flag.Bool("help", false, "")
202
203 vers, v := flag.Bool("version", false, ""), flag.Bool("v", false, "")
204
205 flag.Func("platform", usagePlatform, cbOptionsPlatform)
206 flag.Func("p", usagePlatform, cbOptionsPlatform)
207
208 flag.Func("fields", usageFields, cbOptionsFields)
209 flag.Func("f", usageFields, cbOptionsFields)
210
211 flag.BoolFunc("exclude", usageExclude, cbOptionsExclude)
212 flag.BoolFunc("e", usageExclude, cbOptionsExclude)
213
214 flag.BoolFunc("unchecked", usageUnchecked, cbOptionsUnchecked)
215 flag.BoolFunc("u", usageUnchecked, cbOptionsUnchecked)
216
217 flag.BoolFunc("i", usageInfo, cbOptionsInfo)
218 flag.BoolFunc("ii", usageInfo, cbOptionsInfo)
219 flag.BoolFunc("iii", usageInfo, cbOptionsInfo)
220 flag.BoolFunc("iiii", usageInfo, cbOptionsInfo)
221
222 if UseComplete() {
223 os.Exit(0)
224 }
225
226 flag.Parse()
227
228 if *help {
229 Usage()
230 os.Exit(0)
231 }
232
233 if *vers || *v {
234 fmt.Println(p2m.Config.Version)
235 os.Exit(0)
236 }
237 }