summaryrefslogtreecommitdiff
path: root/util/autoport/log_maker.go
blob: 6183fb4d8078ba0b0ca9e42dff81451461979aee (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strings"
	"bytes"
)

type LogMakingProgram struct {
	name string
	prefixes []string
	args []string
}

func ExecCommand(sudo bool, name string, arg []string) *exec.Cmd {
	if sudo {
		return exec.Command("sudo", append([]string{name}, arg...)...)
	} else {
		return exec.Command(name, arg...)
	}
}

func (prog LogMakingProgram) TryRunAndSave(output string, sudo bool, prefix string) error {
	f, err := os.Create(output)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	cmd := ExecCommand(sudo, prefix+prog.name, prog.args)
	cmd.Stdout = f
	cmd.Stderr = f

	err = cmd.Start()
	if err != nil {
		return err
	}
	return cmd.Wait()
}

func (prog LogMakingProgram) RunAndSave(outDir string, sudo bool) {
	output := fmt.Sprintf("%s/%s.log", outDir, prog.name)
	cmdline := strings.Join(append([]string{prog.name}, prog.args...), " ")

	fmt.Println("Running: "+cmdline)

	var sb strings.Builder
	for _, prefix := range prog.prefixes {
		err := prog.TryRunAndSave(output, sudo, prefix)
		if err == nil {
			return
		}
		sb.WriteString("\nError running '"+prefix+cmdline+"': "+err.Error()+"\n")
		data, ferr := os.ReadFile(output)
		if ferr != nil {
			sb.WriteString("<failed to open log>\n")
		} else {
			if len(data) > 0 {
				sb.WriteString("Program output:\n\n")
				sb.WriteString(string(data))
			}
		}
	}

	fmt.Println("\nCould not run program: '"+cmdline+"'")
	log.Fatal(sb.String())
}

const MAXPROMPTRETRY = 3

func PromptUser(prompt string, opts []string) (match string, err error) {
	for i := 1; i < MAXPROMPTRETRY; i++ {
		fmt.Printf("%s. (%s) Default:%s\n", prompt,
		           strings.Join(opts, "/"), opts[0])
		var usrInput string
		fmt.Scanln(&usrInput)

		// Check for default entry
		if usrInput == "" {
			match = opts[0]
			return
		}

		for _, opt := range opts {
			if opt == usrInput {
				match = opt
				return
			}
		}
	}
	err = errors.New("max retries exceeded")
	fmt.Fprintln(os.Stderr, "ERROR: max retries exceeded")
	return
}

func AppendYesNo(yesFirst bool, yeah []string, nope []string) []string {
	if yesFirst {
		return append(yeah, nope...)
	} else {
		return append(nope, yeah...)
	}
}

func PromptUserBool(prompt string, fallback bool) bool {
	yeah := []string{"y", "yes"}
	nope := []string{"n", "no"}

	opt, err := PromptUser(prompt, AppendYesNo(fallback, yeah, nope))
	if err != nil {
		// Continue even if there is an error
		return fallback
	}
	for _, val := range yeah {
		if opt == val {
			return true
		}
	}
	return false
}

func MakeHDALogs(outDir string, cardName string) {
	SysDir := "/sys/class/sound/" + cardName + "/"
	files, _ := ioutil.ReadDir(SysDir)
	for _, f := range files {
		if (strings.HasPrefix(f.Name(), "hw") || strings.HasPrefix(f.Name(), "hdaudio")) && f.IsDir() {
			in, err := os.Open(SysDir + f.Name() + "/init_pin_configs")
			defer in.Close()
			if err != nil {
				log.Fatal(err)
			}
			out, err := os.Create(outDir + "/pin_" + strings.Replace(f.Name(), "hdaudio", "hw", -1))
			if err != nil {
				log.Fatal(err)
			}
			defer out.Close()
			io.Copy(out, in)
		}
	}

	ProcDir := "/proc/asound/" + cardName + "/"
	files, _ = ioutil.ReadDir(ProcDir)
	for _, f := range files {
		if strings.HasPrefix(f.Name(), "codec#") && !f.IsDir() {
			in, err := os.Open(ProcDir + f.Name())
			defer in.Close()
			if err != nil {
				log.Fatal(err)
			}
			out, err := os.Create(outDir + "/" + f.Name())
			if err != nil {
				log.Fatal(err)
			}
			defer out.Close()
			io.Copy(out, in)
		}
	}
}

func MakeLogs(outDir string) {
	os.MkdirAll(outDir, 0700)

	sudo := PromptUserBool("Should autoport use sudo to run the commands to make the logs? "+
		"This is recommended over running autoport as root, since the generated files "+
		"won't be owned by root. If running as root already because sudo isn't available, "+
		"choose 'no'. Otherwise, run autoport as a regular (non-root) user and choose 'yes'.",
		true)

	probeGFX := PromptUserBool("WARNING: Running inteltool MAY cause your system to hang when it attempts "+
		"to probe for graphics registers.  Having the graphics registers will help create a better port. "+
		"Should autoport probe these registers?",
		true)

	inteltoolArgs := "-a"
	if probeGFX {
		inteltoolArgs += "f"
	}

	var programs = []LogMakingProgram {
		LogMakingProgram {
			name: "lspci",
			prefixes: []string{""},
			args: []string{"-nnvvvxxxx"},
		},
		LogMakingProgram {
			name: "dmidecode",
			prefixes: []string{""},
			args: []string{},
		},
		LogMakingProgram {
			name: "acpidump",
			prefixes: []string{""},
			args: []string{},
		},
		LogMakingProgram {
			name: "inteltool",
			prefixes: []string{"../inteltool/", ""},
			args: []string{inteltoolArgs},
		},
		LogMakingProgram {
			name: "ectool",
			prefixes: []string{"../ectool/", ""},
			args: []string{"-pd"},
		},
		LogMakingProgram {
			name: "superiotool",
			prefixes: []string{"../superiotool/", ""},
			args: []string{"-ade"},
		},
	}

	fmt.Println("Making logs...")
	for _, prog := range programs {
		prog.RunAndSave(outDir, sudo)
	}

	SysSound := "/sys/class/sound/"
	card := ""
	cards, _ := ioutil.ReadDir(SysSound)
	for _, f := range cards {
		if strings.HasPrefix(f.Name(), "card") {
			cid, err := ioutil.ReadFile(SysSound + f.Name() + "/id")
			if err == nil && bytes.Equal(cid, []byte("PCH\n")) {
				fmt.Fprintln(os.Stderr, "PCH sound card is", f.Name())
				card = f.Name()
			}
		}
	}

	if card != "" {
		MakeHDALogs(outDir, card)
	} else {
		fmt.Fprintln(os.Stderr, "HDAudio not found on PCH.")
	}

	for _, fname := range []string{"cpuinfo", "ioports"} {
		in, err := os.Open("/proc/" + fname)
		defer in.Close()
		if err != nil {
			log.Fatal(err)
		}
		out, err := os.Create(outDir + "/" + fname + ".log")
		if err != nil {
			log.Fatal(err)
		}
		defer out.Close()
		io.Copy(out, in)
	}

	out, err := os.Create(outDir + "/input_bustypes.log")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	ClassInputDir := "/sys/class/input/"
	files, _ := ioutil.ReadDir(ClassInputDir)
	for _, f := range files {
		if strings.HasPrefix(f.Name(), "input") && !f.Mode().IsRegular() { /* Allow both dirs and symlinks.  */
			in, err := os.Open(ClassInputDir + f.Name() + "/id/bustype")
			defer in.Close()
			if err != nil {
				log.Fatal(err)
			}
			io.Copy(out, in)
		}
	}
}