blob: 2a524d387a791902180dfc922ead846eaecda326 [file] [log] [blame]
Vladimir Serbinenko3129f792014-10-15 21:51:47 +02001package main
2
3import (
Chris Ching5343b1f2016-05-17 11:56:03 -06004 "errors"
5 "fmt"
Vladimir Serbinenko3129f792014-10-15 21:51:47 +02006 "io"
7 "io/ioutil"
8 "log"
9 "os"
10 "os/exec"
11 "strings"
Iru Cai112e9ba2019-06-29 14:06:30 +080012 "bytes"
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020013)
14
Vladimir Serbinenko39e25ec2015-05-29 20:49:09 +020015func TryRunAndSave(output string, name string, arg []string) error {
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020016 cmd := exec.Command(name, arg...)
17
18 f, err := os.Create(output)
19 if err != nil {
20 log.Fatal(err)
21 }
22
23 cmd.Stdout = f
24 cmd.Stderr = f
25
26 err = cmd.Start()
27 if err != nil {
Vladimir Serbinenko39e25ec2015-05-29 20:49:09 +020028 return err
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020029 }
30 cmd.Wait()
Vladimir Serbinenko39e25ec2015-05-29 20:49:09 +020031 return nil
32}
33
34func RunAndSave(output string, name string, arg ...string) {
35 err := TryRunAndSave(output, name, arg)
36 if err == nil {
37 return
38 }
39 idx := strings.LastIndex(name, "/")
40 relname := name
41 if idx >= 0 {
42 relname = name[idx+1:]
43 }
44 relname = "./" + relname
45 err = TryRunAndSave(output, relname, arg)
46 if err != nil {
47 log.Fatal(err)
48 }
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020049}
50
Chris Ching5343b1f2016-05-17 11:56:03 -060051const MAXPROMPTRETRY = 3
52
53func PromptUser(prompt string, opts []string) (match string, err error) {
54 for i := 1; i < MAXPROMPTRETRY; i++ {
Maximilian Schanderbf942922017-11-05 20:25:42 +010055 fmt.Printf("%s. (%s) Default:%s\n", prompt,
56 strings.Join(opts, "/"), opts[0])
Chris Ching5343b1f2016-05-17 11:56:03 -060057 var usrInput string
58 fmt.Scanln(&usrInput)
59
60 // Check for default entry
61 if usrInput == "" {
62 match = opts[0]
63 return
64 }
65
66 for _, opt := range opts {
67 if opt == usrInput {
68 match = opt
69 return
70 }
71 }
72 }
73 err = errors.New("max retries exceeded")
74 fmt.Fprintln(os.Stderr, "ERROR: max retries exceeded")
75 return
76}
77
Iru Cai112e9ba2019-06-29 14:06:30 +080078func MakeHDALogs(outDir string, cardName string) {
79 SysDir := "/sys/class/sound/" + cardName + "/"
80 files, _ := ioutil.ReadDir(SysDir)
81 for _, f := range files {
82 if (strings.HasPrefix(f.Name(), "hw") || strings.HasPrefix(f.Name(), "hdaudio")) && f.IsDir() {
83 in, err := os.Open(SysDir + f.Name() + "/init_pin_configs")
84 defer in.Close()
85 if err != nil {
86 log.Fatal(err)
87 }
88 out, err := os.Create(outDir + "/pin_" + strings.Replace(f.Name(), "hdaudio", "hw", -1))
89 if err != nil {
90 log.Fatal(err)
91 }
92 defer out.Close()
93 io.Copy(out, in)
94 }
95 }
96
97 ProcDir := "/proc/asound/" + cardName + "/"
98 files, _ = ioutil.ReadDir(ProcDir)
99 for _, f := range files {
100 if strings.HasPrefix(f.Name(), "codec#") && !f.IsDir() {
101 in, err := os.Open(ProcDir + f.Name())
102 defer in.Close()
103 if err != nil {
104 log.Fatal(err)
105 }
106 out, err := os.Create(outDir + "/" + f.Name())
107 if err != nil {
108 log.Fatal(err)
109 }
110 defer out.Close()
111 io.Copy(out, in)
112 }
113 }
114}
115
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200116func MakeLogs(outDir string) {
117 os.MkdirAll(outDir, 0700)
118 RunAndSave(outDir+"/lspci.log", "lspci", "-nnvvvxxxx")
119 RunAndSave(outDir+"/dmidecode.log", "dmidecode")
120 RunAndSave(outDir+"/acpidump.log", "acpidump")
Chris Ching5343b1f2016-05-17 11:56:03 -0600121
122 inteltoolArgs := "-a"
123 opt, err := PromptUser("WARNING: The following tool MAY cause your system to hang when it attempts "+
124 "to probe for graphics registers. Having the graphics registers will help create a better port. "+
125 "Should autoport probe these registers?",
126 []string{"y", "yes", "n", "no"})
127
128 // Continue even if there is an error
129
130 switch opt {
131 case "y", "yes":
Sebastian 'Swift Geek' Grzywnae6bd18f2017-01-08 03:42:30 +0100132 inteltoolArgs += "f"
Chris Ching5343b1f2016-05-17 11:56:03 -0600133 }
134
135 RunAndSave(outDir+"/inteltool.log", "../inteltool/inteltool", inteltoolArgs)
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800136 RunAndSave(outDir+"/ectool.log", "../ectool/ectool", "-pd")
Arthur Heymans98e77c72017-02-24 13:18:14 +0100137 RunAndSave(outDir+"/superiotool.log", "../superiotool/superiotool", "-ade")
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200138
Iru Cai112e9ba2019-06-29 14:06:30 +0800139 SysSound := "/sys/class/sound/"
140 card := ""
141 cards, _ := ioutil.ReadDir(SysSound)
142 for _, f := range cards {
143 if strings.HasPrefix(f.Name(), "card") {
144 cid, err := ioutil.ReadFile(SysSound + f.Name() + "/id")
145 if err == nil && bytes.Equal(cid, []byte("PCH\n")) {
146 fmt.Fprintln(os.Stderr, "PCH sound card is", f.Name())
147 card = f.Name()
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200148 }
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200149 }
150 }
151
Iru Cai112e9ba2019-06-29 14:06:30 +0800152 if card != "" {
153 MakeHDALogs(outDir, card)
154 } else {
155 fmt.Fprintln(os.Stderr, "HDAudio not found on PCH.")
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200156 }
157
158 for _, fname := range []string{"cpuinfo", "ioports"} {
159 in, err := os.Open("/proc/" + fname)
160 defer in.Close()
161 if err != nil {
162 log.Fatal(err)
163 }
164 out, err := os.Create(outDir + "/" + fname + ".log")
165 if err != nil {
166 log.Fatal(err)
167 }
168 defer out.Close()
169 io.Copy(out, in)
170 }
Vladimir Serbinenko91195c62015-05-29 23:53:37 +0200171
172 out, err := os.Create(outDir + "/input_bustypes.log")
173 if err != nil {
174 log.Fatal(err)
175 }
176 defer out.Close()
177
178 ClassInputDir := "/sys/class/input/"
Iru Cai112e9ba2019-06-29 14:06:30 +0800179 files, _ := ioutil.ReadDir(ClassInputDir)
Vladimir Serbinenko91195c62015-05-29 23:53:37 +0200180 for _, f := range files {
181 if strings.HasPrefix(f.Name(), "input") && !f.Mode().IsRegular() { /* Allow both dirs and symlinks. */
182 in, err := os.Open(ClassInputDir + f.Name() + "/id/bustype")
183 defer in.Close()
184 if err != nil {
185 log.Fatal(err)
186 }
187 io.Copy(out, in)
188 }
189 }
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200190}