File: parser.go

package info (click to toggle)
coreboot 25.09%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 217,084 kB
  • sloc: ansic: 1,685,313; sh: 15,803; python: 11,200; perl: 10,186; asm: 8,519; makefile: 5,179; cpp: 4,724; pascal: 2,327; ada: 1,985; yacc: 1,264; lex: 731; sed: 75; ruby: 5; lisp: 5; awk: 4
file content (139 lines) | stat: -rw-r--r-- 3,346 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package parser

import (
	"bufio"
	"fmt"
	"os"
	"strings"

	"review.coreboot.org/coreboot.git/util/intelp2m/config/p2m"
	"review.coreboot.org/coreboot.git/util/intelp2m/logs"
	"review.coreboot.org/coreboot.git/util/intelp2m/parser/template"
	"review.coreboot.org/coreboot.git/util/intelp2m/platforms"
	"review.coreboot.org/coreboot.git/util/intelp2m/platforms/common"
	"review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/fields"
	"review.coreboot.org/coreboot.git/util/intelp2m/platforms/common/register/bits"
)

type EntryType int

const (
	EntryEmpty EntryType = iota
	EntryPad
	EntryGroup
	EntryReserved
)

// Parser entry
// ID        : pad id string
// Function  : the string that means the pad function
// DW0       : DW0 register struct
// DW1       : DW1 register struct
// 0wnership : host software ownership
type Entry struct {
	EType     EntryType
	ID        string
	Function  string
	DW0       uint32
	DW1       uint32
	Ownership bool
}

func (e *Entry) ToMacro() []string {
	constructor, err := platforms.GetConstructor()
	if err != nil {
		panic(err)
	}
	macro := common.CreateFrom(e.ID, e.Ownership, constructor(e.DW0, e.DW1), fields.Get())
	slices := strings.Split(macro.Generate(), "\n")
	return slices
}

// extractPad() extracts pad information from a string
func extractPad(line string) (Entry, error) {
	function, id, dw0, dw1, err := template.Apply(line)
	if err != nil {
		logs.Errorf("extraction error: %v", err)
		return Entry{EType: EntryEmpty}, err
	}

	pad := Entry{
		EType:     EntryPad,
		Function:  function,
		ID:        id,
		DW0:       dw0,
		DW1:       dw1,
		Ownership: common.Acpi,
	}

	if dw0 == bits.All32 {
		pad.EType = EntryReserved
	}

	return pad, nil
}

// extractGroup() extracts information about the pad group from the string
func extractGroup(line string) Entry {
	group := Entry{
		EType:    EntryGroup,
		Function: line,
	}
	return group
}

// checkGPPG() checks whether the desired group is present in the string
func checkKeywords(line string, slice []string) bool {
	for _, key := range slice {
		if strings.Contains(line, key) {
			return true
		}
	}
	return false
}

// Extract() extracts pad information from a string
func Extract(line string) Entry {
	if checkKeywords(line, []string{"GPIO Community", "GPIO Group"}) {
		return extractGroup(line)
	}
	if gppg, err := platforms.GetGPPGroups(); err != nil {
		logs.Errorf("extract error: %v: skip line <%s>", err, line)
		return Entry{EType: EntryEmpty}
	} else if checkKeywords(line, gppg) {
		pad, err := extractPad(line)
		if err != nil {
			logs.Errorf("extract pad info from %s: %v", line, err)
			return Entry{EType: EntryEmpty}
		}
		return pad
	}
	logs.Infof("skip line <%s>", line)
	return Entry{EType: EntryEmpty}
}

// Run() starts the file parsing process
func Run() ([]Entry, error) {
	entries := make([]Entry, 0)

	file, err := os.Open(p2m.Config.InputPath)
	if err != nil {
		err = fmt.Errorf("input file error: %v", err)
		logs.Errorf("%v", err)
		return nil, err
	}
	defer file.Close()

	logs.Infof("parse %s file", p2m.Config.InputPath)
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		entry := Extract(line)
		if entry.EType != EntryEmpty {
			entries = append(entries, entry)
		}
	}

	logs.Infof("successfully completed: %d entries", len(entries))
	return entries, nil
}