Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Chris Ching | 5343b1f | 2016-05-17 11:56:03 -0600 | [diff] [blame] | 4 | "errors" |
| 5 | "fmt" |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 6 | "io" |
| 7 | "io/ioutil" |
| 8 | "log" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
Vladimir Serbinenko | 39e25ec | 2015-05-29 20:49:09 +0200 | [diff] [blame] | 14 | func TryRunAndSave(output string, name string, arg []string) error { |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 15 | cmd := exec.Command(name, arg...) |
| 16 | |
| 17 | f, err := os.Create(output) |
| 18 | if err != nil { |
| 19 | log.Fatal(err) |
| 20 | } |
| 21 | |
| 22 | cmd.Stdout = f |
| 23 | cmd.Stderr = f |
| 24 | |
| 25 | err = cmd.Start() |
| 26 | if err != nil { |
Vladimir Serbinenko | 39e25ec | 2015-05-29 20:49:09 +0200 | [diff] [blame] | 27 | return err |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 28 | } |
| 29 | cmd.Wait() |
Vladimir Serbinenko | 39e25ec | 2015-05-29 20:49:09 +0200 | [diff] [blame] | 30 | return nil |
| 31 | } |
| 32 | |
| 33 | func RunAndSave(output string, name string, arg ...string) { |
| 34 | err := TryRunAndSave(output, name, arg) |
| 35 | if err == nil { |
| 36 | return |
| 37 | } |
| 38 | idx := strings.LastIndex(name, "/") |
| 39 | relname := name |
| 40 | if idx >= 0 { |
| 41 | relname = name[idx+1:] |
| 42 | } |
| 43 | relname = "./" + relname |
| 44 | err = TryRunAndSave(output, relname, arg) |
| 45 | if err != nil { |
| 46 | log.Fatal(err) |
| 47 | } |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Chris Ching | 5343b1f | 2016-05-17 11:56:03 -0600 | [diff] [blame] | 50 | const MAXPROMPTRETRY = 3 |
| 51 | |
| 52 | func PromptUser(prompt string, opts []string) (match string, err error) { |
| 53 | for i := 1; i < MAXPROMPTRETRY; i++ { |
| 54 | fmt.Println("%s. (%s) Default:%s", prompt, strings.Join(opts, "/"), opts[0]) |
| 55 | var usrInput string |
| 56 | fmt.Scanln(&usrInput) |
| 57 | |
| 58 | // Check for default entry |
| 59 | if usrInput == "" { |
| 60 | match = opts[0] |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | for _, opt := range opts { |
| 65 | if opt == usrInput { |
| 66 | match = opt |
| 67 | return |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | err = errors.New("max retries exceeded") |
| 72 | fmt.Fprintln(os.Stderr, "ERROR: max retries exceeded") |
| 73 | return |
| 74 | } |
| 75 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 76 | func MakeLogs(outDir string) { |
| 77 | os.MkdirAll(outDir, 0700) |
| 78 | RunAndSave(outDir+"/lspci.log", "lspci", "-nnvvvxxxx") |
| 79 | RunAndSave(outDir+"/dmidecode.log", "dmidecode") |
| 80 | RunAndSave(outDir+"/acpidump.log", "acpidump") |
Chris Ching | 5343b1f | 2016-05-17 11:56:03 -0600 | [diff] [blame] | 81 | |
| 82 | inteltoolArgs := "-a" |
| 83 | opt, err := PromptUser("WARNING: The following tool MAY cause your system to hang when it attempts "+ |
| 84 | "to probe for graphics registers. Having the graphics registers will help create a better port. "+ |
| 85 | "Should autoport probe these registers?", |
| 86 | []string{"y", "yes", "n", "no"}) |
| 87 | |
| 88 | // Continue even if there is an error |
| 89 | |
| 90 | switch opt { |
| 91 | case "y", "yes": |
| 92 | opt += " -f" |
| 93 | } |
| 94 | |
| 95 | RunAndSave(outDir+"/inteltool.log", "../inteltool/inteltool", inteltoolArgs) |
Vladimir Serbinenko | 6286e35 | 2015-10-10 23:17:03 +0200 | [diff] [blame] | 96 | RunAndSave(outDir+"/ectool.log", "../ectool/ectool") |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 97 | |
| 98 | SysDir := "/sys/class/sound/card0/" |
| 99 | files, _ := ioutil.ReadDir(SysDir) |
| 100 | for _, f := range files { |
| 101 | if (strings.HasPrefix(f.Name(), "hw") || strings.HasPrefix(f.Name(), "hdaudio")) && f.IsDir() { |
| 102 | in, err := os.Open(SysDir + f.Name() + "/init_pin_configs") |
| 103 | defer in.Close() |
| 104 | if err != nil { |
| 105 | log.Fatal(err) |
| 106 | } |
| 107 | out, err := os.Create(outDir + "/pin_" + strings.Replace(f.Name(), "hdaudio", "hw", -1)) |
| 108 | if err != nil { |
| 109 | log.Fatal(err) |
| 110 | } |
| 111 | defer out.Close() |
| 112 | io.Copy(out, in) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | ProcDir := "/proc/asound/card0/" |
| 117 | files, _ = ioutil.ReadDir(ProcDir) |
| 118 | for _, f := range files { |
| 119 | if strings.HasPrefix(f.Name(), "codec#") && !f.IsDir() { |
| 120 | in, err := os.Open(ProcDir + f.Name()) |
| 121 | defer in.Close() |
| 122 | if err != nil { |
| 123 | log.Fatal(err) |
| 124 | } |
| 125 | out, err := os.Create(outDir + "/" + f.Name()) |
| 126 | if err != nil { |
| 127 | log.Fatal(err) |
| 128 | } |
| 129 | defer out.Close() |
| 130 | io.Copy(out, in) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | for _, fname := range []string{"cpuinfo", "ioports"} { |
| 135 | in, err := os.Open("/proc/" + fname) |
| 136 | defer in.Close() |
| 137 | if err != nil { |
| 138 | log.Fatal(err) |
| 139 | } |
| 140 | out, err := os.Create(outDir + "/" + fname + ".log") |
| 141 | if err != nil { |
| 142 | log.Fatal(err) |
| 143 | } |
| 144 | defer out.Close() |
| 145 | io.Copy(out, in) |
| 146 | } |
Vladimir Serbinenko | 91195c6 | 2015-05-29 23:53:37 +0200 | [diff] [blame] | 147 | |
| 148 | out, err := os.Create(outDir + "/input_bustypes.log") |
| 149 | if err != nil { |
| 150 | log.Fatal(err) |
| 151 | } |
| 152 | defer out.Close() |
| 153 | |
| 154 | ClassInputDir := "/sys/class/input/" |
| 155 | files, _ = ioutil.ReadDir(ClassInputDir) |
| 156 | for _, f := range files { |
| 157 | if strings.HasPrefix(f.Name(), "input") && !f.Mode().IsRegular() { /* Allow both dirs and symlinks. */ |
| 158 | in, err := os.Open(ClassInputDir + f.Name() + "/id/bustype") |
| 159 | defer in.Close() |
| 160 | if err != nil { |
| 161 | log.Fatal(err) |
| 162 | } |
| 163 | io.Copy(out, in) |
| 164 | } |
| 165 | } |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 166 | } |