Debian Codegraph @

util/intelp2m/parser/parser.go

raw symbols SCIP
1 package parser
2
3 import (
4 "bufio"
5 "fmt"
6 "os"
7 "strings"
8
9 "review.coreboot.org/coreboot.git/util/intelp2m/config/p2m"
10 "review.coreboot.org/coreboot.git/util/intelp2m/logs"
11 "review.coreboot.org/coreboot.git/util/intelp2m/parser/template"
12 "review.coreboot.org/coreboot.git/util/intelp2m/platforms"
13 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common"
14 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/fields"
15 "review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/register/bits"
16 )
17
18 type EntryType int
19
20 const (
21 EntryEmpty EntryType = iota
22 EntryPad
23 EntryGroup
24 EntryReserved
25 )
26
27 // Parser entry
28 // ID : pad id string
29 // Function : the string that means the pad function
30 // DW0 : DW0 register struct
31 // DW1 : DW1 register struct
32 // 0wnership : host software ownership
33 type Entry struct {
34 EType EntryType
35 ID string
36 Function string
37 DW0 uint32
38 DW1 uint32
39 Ownership bool
40 }
41
42 func (e *Entry) ToMacro() []string {
43 constructor, err := platforms.GetConstructor()
44 if err != nil {
45 panic(err)
46 }
47 macro := common.CreateFrom(e.ID, e.Ownership, constructor(e.DW0, e.DW1), fields.Get())
48 slices := strings.Split(macro.Generate(), "\n")
49 return slices
50 }
51
52 // extractPad() extracts pad information from a string
53 func extractPad(line string) (Entry, error) {
54 function, id, dw0, dw1, err := template.Apply(line)
55 if err != nil {
56 logs.Errorf("extraction error: %v", err)
57 return Entry{EType: EntryEmpty}, err
58 }
59
60 pad := Entry{
61 EType: EntryPad,
62 Function: function,
63 ID: id,
64 DW0: dw0,
65 DW1: dw1,
66 Ownership: common.Acpi,
67 }
68
69 if dw0 == bits.All32 {
70 pad.EType = EntryReserved
71 }
72
73 return pad, nil
74 }
75
76 // extractGroup() extracts information about the pad group from the string
77 func extractGroup(line string) Entry {
78 group := Entry{
79 EType: EntryGroup,
80 Function: line,
81 }
82 return group
83 }
84
85 // checkGPPG() checks whether the desired group is present in the string
86 func checkKeywords(line string, slice []string) bool {
87 for _, key := range slice {
88 if strings.Contains(line, key) {
89 return true
90 }
91 }
92 return false
93 }
94
95 // Extract() extracts pad information from a string
96 func Extract(line string) Entry {
97 if checkKeywords(line, []string{"GPIO Community", "GPIO Group"}) {
98 return extractGroup(line)
99 }
100 if gppg, err := platforms.GetGPPGroups(); err != nil {
101 logs.Errorf("extract error: %v: skip line <%s>", err, line)
102 return Entry{EType: EntryEmpty}
103 } else if checkKeywords(line, gppg) {
104 pad, err := extractPad(line)
105 if err != nil {
106 logs.Errorf("extract pad info from %s: %v", line, err)
107 return Entry{EType: EntryEmpty}
108 }
109 return pad
110 }
111 logs.Infof("skip line <%s>", line)
112 return Entry{EType: EntryEmpty}
113 }
114
115 // Run() starts the file parsing process
116 func Run() ([]Entry, error) {
117 entries := make([]Entry, 0)
118
119 file, err := os.Open(p2m.Config.InputPath)
120 if err != nil {
121 err = fmt.Errorf("input file error: %v", err)
122 logs.Errorf("%v", err)
123 return nil, err
124 }
125 defer file.Close()
126
127 logs.Infof("parse %s file", p2m.Config.InputPath)
128 scanner := bufio.NewScanner(file)
129 for scanner.Scan() {
130 line := scanner.Text()
131 entry := Extract(line)
132 if entry.EType != EntryEmpty {
133 entries = append(entries, entry)
134 }
135 }
136
137 logs.Infof("successfully completed: %d entries", len(entries))
138 return entries, nil
139 }