mb/google/skyrim: Enable ACPI tables
[coreboot.git] / util / autoport / log_maker.go
blob2a524d387a791902180dfc922ead846eaecda326
1 package main
3 import (
4 "errors"
5 "fmt"
6 "io"
7 "io/ioutil"
8 "log"
9 "os"
10 "os/exec"
11 "strings"
12 "bytes"
15 func TryRunAndSave(output string, name string, arg []string) error {
16 cmd := exec.Command(name, arg...)
18 f, err := os.Create(output)
19 if err != nil {
20 log.Fatal(err)
23 cmd.Stdout = f
24 cmd.Stderr = f
26 err = cmd.Start()
27 if err != nil {
28 return err
30 cmd.Wait()
31 return nil
34 func RunAndSave(output string, name string, arg ...string) {
35 err := TryRunAndSave(output, name, arg)
36 if err == nil {
37 return
39 idx := strings.LastIndex(name, "/")
40 relname := name
41 if idx >= 0 {
42 relname = name[idx+1:]
44 relname = "./" + relname
45 err = TryRunAndSave(output, relname, arg)
46 if err != nil {
47 log.Fatal(err)
51 const MAXPROMPTRETRY = 3
53 func PromptUser(prompt string, opts []string) (match string, err error) {
54 for i := 1; i < MAXPROMPTRETRY; i++ {
55 fmt.Printf("%s. (%s) Default:%s\n", prompt,
56 strings.Join(opts, "/"), opts[0])
57 var usrInput string
58 fmt.Scanln(&usrInput)
60 // Check for default entry
61 if usrInput == "" {
62 match = opts[0]
63 return
66 for _, opt := range opts {
67 if opt == usrInput {
68 match = opt
69 return
73 err = errors.New("max retries exceeded")
74 fmt.Fprintln(os.Stderr, "ERROR: max retries exceeded")
75 return
78 func 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)
88 out, err := os.Create(outDir + "/pin_" + strings.Replace(f.Name(), "hdaudio", "hw", -1))
89 if err != nil {
90 log.Fatal(err)
92 defer out.Close()
93 io.Copy(out, in)
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)
106 out, err := os.Create(outDir + "/" + f.Name())
107 if err != nil {
108 log.Fatal(err)
110 defer out.Close()
111 io.Copy(out, in)
116 func 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")
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"})
128 // Continue even if there is an error
130 switch opt {
131 case "y", "yes":
132 inteltoolArgs += "f"
135 RunAndSave(outDir+"/inteltool.log", "../inteltool/inteltool", inteltoolArgs)
136 RunAndSave(outDir+"/ectool.log", "../ectool/ectool", "-pd")
137 RunAndSave(outDir+"/superiotool.log", "../superiotool/superiotool", "-ade")
139 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()
152 if card != "" {
153 MakeHDALogs(outDir, card)
154 } else {
155 fmt.Fprintln(os.Stderr, "HDAudio not found on PCH.")
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)
164 out, err := os.Create(outDir + "/" + fname + ".log")
165 if err != nil {
166 log.Fatal(err)
168 defer out.Close()
169 io.Copy(out, in)
172 out, err := os.Create(outDir + "/input_bustypes.log")
173 if err != nil {
174 log.Fatal(err)
176 defer out.Close()
178 ClassInputDir := "/sys/class/input/"
179 files, _ := ioutil.ReadDir(ClassInputDir)
180 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)
187 io.Copy(out, in)