util/intelp2m: Print the current project version
[coreboot.git] / util / intelp2m / parser / parser.go
blobf6ed0478c2926114e94b2c9cab1bd6cbc07ccd8f
1 package parser
3 import (
4 "bufio"
5 "fmt"
6 "strings"
7 "strconv"
9 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common"
10 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/snr"
11 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/lbg"
12 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/apl"
13 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/cnl"
14 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/tgl"
15 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/adl"
16 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/jsl"
17 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/mtl"
18 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/ebg"
19 "review.coreboot.org/coreboot.git/util/intelp2m/config"
22 // PlatformSpecific - platform-specific interface
23 type PlatformSpecific interface {
24 GenMacro(id string, dw0 uint32, dw1 uint32, ownership uint8) string
25 GroupNameExtract(line string) (bool, string)
26 KeywordCheck(line string) bool
29 // padInfo - information about pad
30 // id : pad id string
31 // offset : the offset of the register address relative to the base
32 // function : the string that means the pad function
33 // dw0 : DW0 register value
34 // dw1 : DW1 register value
35 // ownership : host software ownership
36 type padInfo struct {
37 id string
38 offset uint16
39 function string
40 dw0 uint32
41 dw1 uint32
42 ownership uint8
45 // generate - wrapper for Fprintf(). Writes text to the file specified
46 // in config.OutputGenFile
47 func (info *padInfo) generate(lvl int, line string, a ...interface{}) {
48 if config.InfoLevelGet() >= lvl {
49 fmt.Fprintf(config.OutputGenFile, line, a...)
53 // titleFprint - print GPIO group title to file
54 // /* ------- GPIO Group GPP_L ------- */
55 func (info *padInfo) titleFprint() {
56 info.generate(0, "\n\t/* %s */\n", info.function)
59 // reservedFprint - print reserved GPIO to file as comment
60 // /* GPP_H17 - RESERVED */
61 func (info *padInfo) reservedFprint() {
62 info.generate(2, "\n")
63 // small comment about reserved port
64 info.generate(0, "\t/* %s - %s */\n", info.id, info.function)
67 // padInfoMacroFprint - print information about current pad to file using
68 // special macros:
69 // PAD_CFG_NF(GPP_F1, 20K_PU, PLTRST, NF1), /* SATAXPCIE4 */
70 // gpio : gpio.c file descriptor
71 // macro : string of the generated macro
72 func (info *padInfo) padInfoMacroFprint(macro string) {
73 info.generate(2,
74 "\n\t/* %s - %s */\n\t/* DW0: 0x%0.8x, DW1: 0x%0.8x */\n",
75 info.id,
76 info.function,
77 info.dw0,
78 info.dw1)
79 info.generate(0, "\t%s", macro)
80 if config.InfoLevelGet() == 1 {
81 info.generate(1, "\t/* %s */", info.function)
83 info.generate(0, "\n")
86 // ParserData - global data
87 // line : string from the configuration file
88 // padmap : pad info map
89 // RawFmt : flag for generating pads config file with DW0/1 reg raw values
90 // Template : structure template type of ConfigFile
91 type ParserData struct {
92 platform PlatformSpecific
93 line string
94 padmap []padInfo
95 ownership map[string]uint32
98 // hostOwnershipGet - get the host software ownership value for the corresponding
99 // pad ID
100 // id : pad ID string
101 // return the host software ownership form the parser struct
102 func (parser *ParserData) hostOwnershipGet(id string) uint8 {
103 var ownership uint8 = 0
104 status, group := parser.platform.GroupNameExtract(id)
105 if config.TemplateGet() == config.TempInteltool && status {
106 numder, _ := strconv.Atoi(strings.TrimLeft(id, group))
107 if (parser.ownership[group] & (1 << uint8(numder))) != 0 {
108 ownership = 1
111 return ownership
114 // padInfoExtract - adds a new entry to pad info map
115 // return error status
116 func (parser *ParserData) padInfoExtract() int {
117 var function, id string
118 var dw0, dw1 uint32
119 var template = map[int]template{
120 config.TempInteltool: UseInteltoolLogTemplate,
121 config.TempGpioh : useGpioHTemplate,
122 config.TempSpec : useYourTemplate,
124 if template[config.TemplateGet()](parser.line, &function, &id, &dw0, &dw1) == 0 {
125 pad := padInfo{id: id,
126 function: function,
127 dw0: dw0,
128 dw1: dw1,
129 ownership: parser.hostOwnershipGet(id)}
130 parser.padmap = append(parser.padmap, pad)
131 return 0
133 fmt.Printf("This template (%d) does not match!\n", config.TemplateGet())
134 return -1
137 // communityGroupExtract
138 func (parser *ParserData) communityGroupExtract() {
139 pad := padInfo{function: parser.line}
140 parser.padmap = append(parser.padmap, pad)
143 // PlatformSpecificInterfaceSet - specific interface for the platform selected
144 // in the configuration
145 func (parser *ParserData) PlatformSpecificInterfaceSet() {
146 var platform = map[uint8]PlatformSpecific {
147 config.SunriseType : snr.PlatformSpecific{},
148 // See platforms/lbg/macro.go
149 config.LewisburgType : lbg.PlatformSpecific{
150 InheritanceTemplate : snr.PlatformSpecific{},
152 config.ApolloType : apl.PlatformSpecific{},
153 config.CannonType : cnl.PlatformSpecific{
154 InheritanceTemplate : snr.PlatformSpecific{},
156 config.TigerType : tgl.PlatformSpecific{},
157 config.AlderType : adl.PlatformSpecific{},
158 config.JasperType : jsl.PlatformSpecific{},
159 config.MeteorType : mtl.PlatformSpecific{},
160 // See platforms/ebg/macro.go
161 config.EmmitsburgType : ebg.PlatformSpecific{
162 InheritanceTemplate : cnl.PlatformSpecific{
163 InheritanceTemplate : snr.PlatformSpecific{},
167 parser.platform = platform[config.PlatformGet()]
170 // PadMapFprint - print pad info map to file
171 func (parser *ParserData) PadMapFprint() {
172 for _, pad := range parser.padmap {
173 switch pad.dw0 {
174 case 0:
175 pad.titleFprint()
176 case 0xffffffff:
177 pad.reservedFprint()
178 default:
179 str := parser.platform.GenMacro(pad.id, pad.dw0, pad.dw1, pad.ownership)
180 pad.padInfoMacroFprint(str)
185 // Register - read specific platform registers (32 bits)
186 // line : string from file with pad config map
187 // nameTemplate : register name femplate to filter parsed lines
188 // return
189 // valid : true if the dump of the register in intertool.log is set in accordance
190 // with the template
191 // name : full register name
192 // offset : register offset relative to the base address
193 // value : register value
194 func (parser *ParserData) Register(nameTemplate string) (
195 valid bool, name string, offset uint32, value uint32) {
196 if strings.Contains(parser.line, nameTemplate) &&
197 config.TemplateGet() == config.TempInteltool {
198 if registerInfoTemplate(parser.line, &name, &offset, &value) == 0 {
199 fmt.Printf("\n\t/* %s : 0x%x : 0x%x */\n", name, offset, value)
200 return true, name, offset, value
203 return false, "ERROR", 0, 0
206 // padOwnershipExtract - extract Host Software Pad Ownership from inteltool dump
207 // return true if success
208 func (parser *ParserData) padOwnershipExtract() bool {
209 var group string
210 status, name, offset, value := parser.Register("HOSTSW_OWN_GPP_")
211 if status {
212 _, group = parser.platform.GroupNameExtract(parser.line)
213 parser.ownership[group] = value
214 fmt.Printf("\n\t/* padOwnershipExtract: [offset 0x%x] %s = 0x%x */\n",
215 offset, name, parser.ownership[group])
217 return status
220 // padConfigurationExtract - reads GPIO configuration registers and returns true if the
221 // information from the inteltool log was successfully parsed.
222 func (parser *ParserData) padConfigurationExtract() bool {
223 // Only for Sunrise or CannonLake, and only for inteltool.log file template
224 if config.TemplateGet() != config.TempInteltool || config.IsPlatformApollo() {
225 return false
227 return parser.padOwnershipExtract()
230 // Parse pads groupe information in the inteltool log file
231 // ConfigFile : name of inteltool log file
232 func (parser *ParserData) Parse() {
233 // Read all lines from inteltool log file
234 fmt.Println("Parse IntelTool Log File...")
236 // determine the platform type and set the interface for it
237 parser.PlatformSpecificInterfaceSet()
239 // map of thepad ownership registers for the GPIO controller
240 parser.ownership = make(map[string]uint32)
242 scanner := bufio.NewScanner(config.InputRegDumpFile)
243 for scanner.Scan() {
244 parser.line = scanner.Text()
245 isIncluded, _ := common.KeywordsCheck(parser.line, "GPIO Community", "GPIO Group");
246 if isIncluded {
247 parser.communityGroupExtract()
248 } else if !parser.padConfigurationExtract() && parser.platform.KeywordCheck(parser.line) {
249 if parser.padInfoExtract() != 0 {
250 fmt.Println("...error!")
254 fmt.Println("...done!")