15 type LogMakingProgram
struct {
21 func ExecCommand(sudo
bool, name
string, arg
[]string) *exec
.Cmd
{
23 return exec
.Command("sudo", append([]string{name
}, arg
...)...)
25 return exec
.Command(name
, arg
...)
29 func (prog LogMakingProgram
) TryRunAndSave(output
string, sudo
bool, prefix
string) error
{
30 f
, err
:= os
.Create(output
)
36 cmd
:= ExecCommand(sudo
, prefix
+prog
.name
, prog
.args
)
47 func (prog LogMakingProgram
) RunAndSave(outDir
string, sudo
bool) {
48 output
:= fmt
.Sprintf("%s/%s.log", outDir
, prog
.name
)
49 cmdline
:= strings
.Join(append([]string{prog
.name
}, prog
.args
...), " ")
51 fmt
.Println("Running: "+cmdline
)
53 var sb strings
.Builder
54 for _
, prefix
:= range prog
.prefixes
{
55 err
:= prog
.TryRunAndSave(output
, sudo
, prefix
)
59 sb
.WriteString("\nError running '"+prefix
+cmdline
+"': "+err
.Error()+"\n")
60 data
, ferr
:= os
.ReadFile(output
)
62 sb
.WriteString("<failed to open log>\n")
65 sb
.WriteString("Program output:\n\n")
66 sb
.WriteString(string(data
))
71 fmt
.Println("\nCould not run program: '"+cmdline
+"'")
72 log
.Fatal(sb
.String())
75 const MAXPROMPTRETRY
= 3
77 func PromptUser(prompt
string, opts
[]string) (match
string, err error
) {
78 for i
:= 1; i
< MAXPROMPTRETRY
; i
++ {
79 fmt
.Printf("%s. (%s) Default:%s\n", prompt
,
80 strings
.Join(opts
, "/"), opts
[0])
84 // Check for default entry
90 for _
, opt
:= range opts
{
97 err
= errors
.New("max retries exceeded")
98 fmt
.Fprintln(os
.Stderr
, "ERROR: max retries exceeded")
102 func AppendYesNo(yesFirst
bool, yeah
[]string, nope
[]string) []string {
104 return append(yeah
, nope
...)
106 return append(nope
, yeah
...)
110 func PromptUserBool(prompt
string, fallback
bool) bool {
111 yeah
:= []string{"y", "yes"}
112 nope
:= []string{"n", "no"}
114 opt
, err
:= PromptUser(prompt
, AppendYesNo(fallback
, yeah
, nope
))
116 // Continue even if there is an error
119 for _
, val
:= range yeah
{
127 func MakeHDALogs(outDir
string, cardName
string) {
128 SysDir
:= "/sys/class/sound/" + cardName
+ "/"
129 files
, _
:= ioutil
.ReadDir(SysDir
)
130 for _
, f
:= range files
{
131 if (strings
.HasPrefix(f
.Name(), "hw") || strings
.HasPrefix(f
.Name(), "hdaudio")) && f
.IsDir() {
132 in
, err
:= os
.Open(SysDir
+ f
.Name() + "/init_pin_configs")
137 out
, err
:= os
.Create(outDir
+ "/pin_" + strings
.Replace(f
.Name(), "hdaudio", "hw", -1))
146 ProcDir
:= "/proc/asound/" + cardName
+ "/"
147 files
, _
= ioutil
.ReadDir(ProcDir
)
148 for _
, f
:= range files
{
149 if strings
.HasPrefix(f
.Name(), "codec#") && !f
.IsDir() {
150 in
, err
:= os
.Open(ProcDir
+ f
.Name())
155 out
, err
:= os
.Create(outDir
+ "/" + f
.Name())
165 func MakeLogs(outDir
string) {
166 os
.MkdirAll(outDir
, 0700)
168 sudo
:= PromptUserBool("Should autoport use sudo to run the commands to make the logs? "+
169 "This is recommended over running autoport as root, since the generated files "+
170 "won't be owned by root. If running as root already because sudo isn't available, "+
171 "choose 'no'. Otherwise, run autoport as a regular (non-root) user and choose 'yes'.",
174 probeGFX
:= PromptUserBool("WARNING: Running inteltool MAY cause your system to hang when it attempts "+
175 "to probe for graphics registers. Having the graphics registers will help create a better port. "+
176 "Should autoport probe these registers?",
179 inteltoolArgs
:= "-a"
184 var programs
= []LogMakingProgram
{
187 prefixes
: []string{""},
188 args
: []string{"-nnvvvxxxx"},
192 prefixes
: []string{""},
197 prefixes
: []string{""},
202 prefixes
: []string{"../inteltool/", ""},
203 args
: []string{inteltoolArgs
},
207 prefixes
: []string{"../ectool/", ""},
208 args
: []string{"-pd"},
212 prefixes
: []string{"../superiotool/", ""},
213 args
: []string{"-ade"},
217 fmt
.Println("Making logs...")
218 for _
, prog
:= range programs
{
219 prog
.RunAndSave(outDir
, sudo
)
222 SysSound
:= "/sys/class/sound/"
224 cards
, _
:= ioutil
.ReadDir(SysSound
)
225 for _
, f
:= range cards
{
226 if strings
.HasPrefix(f
.Name(), "card") {
227 cid
, err
:= ioutil
.ReadFile(SysSound
+ f
.Name() + "/id")
228 if err
== nil && bytes
.Equal(cid
, []byte("PCH\n")) {
229 fmt
.Fprintln(os
.Stderr
, "PCH sound card is", f
.Name())
236 MakeHDALogs(outDir
, card
)
238 fmt
.Fprintln(os
.Stderr
, "HDAudio not found on PCH.")
241 for _
, fname
:= range []string{"cpuinfo", "ioports"} {
242 in
, err
:= os
.Open("/proc/" + fname
)
247 out
, err
:= os
.Create(outDir
+ "/" + fname
+ ".log")
255 out
, err
:= os
.Create(outDir
+ "/input_bustypes.log")
261 ClassInputDir
:= "/sys/class/input/"
262 files
, _
:= ioutil
.ReadDir(ClassInputDir
)
263 for _
, f
:= range files
{
264 if strings
.HasPrefix(f
.Name(), "input") && !f
.Mode().IsRegular() { /* Allow both dirs and symlinks. */
265 in
, err
:= os
.Open(ClassInputDir
+ f
.Name() + "/id/bustype")