Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "log" |
| 8 | "os" |
Arthur Heymans | 8bd25ab | 2018-08-12 00:21:08 +0200 | [diff] [blame] | 9 | "regexp" |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 10 | "strconv" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | type LogDevReader struct { |
| 15 | InputDirectory string |
| 16 | ACPITables map[string][]byte |
| 17 | EC []byte |
| 18 | } |
| 19 | |
| 20 | func isXDigit(x uint8) bool { |
| 21 | if x >= '0' && x <= '9' { |
| 22 | return true |
| 23 | } |
| 24 | if x >= 'a' && x <= 'f' { |
| 25 | return true |
| 26 | } |
| 27 | if x >= 'A' && x <= 'F' { |
| 28 | return true |
| 29 | } |
| 30 | return false |
| 31 | } |
| 32 | |
| 33 | type HexLine struct { |
| 34 | length uint |
| 35 | values [16]byte |
| 36 | start uint |
| 37 | } |
| 38 | |
| 39 | func (l *LogDevReader) ReadHexLine(line string) (hex HexLine) { |
| 40 | hex.start = 0 |
| 41 | line = strings.Trim(line, " ") |
| 42 | fmt.Sscanf(line, "%x:", &hex.start) |
| 43 | ll, _ := fmt.Sscanf(line, "%x: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", &hex.start, |
| 44 | &hex.values[0], &hex.values[1], &hex.values[2], |
| 45 | &hex.values[3], &hex.values[4], &hex.values[5], |
| 46 | &hex.values[6], &hex.values[7], &hex.values[8], |
| 47 | &hex.values[9], &hex.values[10], &hex.values[11], |
| 48 | &hex.values[12], &hex.values[13], &hex.values[14], |
| 49 | &hex.values[15]) |
| 50 | hex.length = uint(ll - 1) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | func (l *LogDevReader) AssignHexLine(inp string, target []byte) []byte { |
| 55 | hex := l.ReadHexLine(inp) |
| 56 | if hex.start+hex.length > uint(len(target)) { |
| 57 | target = target[0 : hex.start+hex.length] |
| 58 | } |
| 59 | copy(target[hex.start:hex.start+hex.length], hex.values[0:hex.length]) |
| 60 | return target |
| 61 | } |
| 62 | |
| 63 | func (l *LogDevReader) GetEC() []byte { |
| 64 | if l.EC != nil { |
| 65 | return l.EC |
| 66 | } |
| 67 | l.EC = make([]byte, 0x100, 0x100) |
| 68 | |
| 69 | file, err := os.Open(l.InputDirectory + "/ectool.log") |
| 70 | if err != nil { |
| 71 | log.Fatal(err) |
| 72 | } |
| 73 | defer file.Close() |
| 74 | |
| 75 | scanner := bufio.NewScanner(file) |
| 76 | |
| 77 | for scanner.Scan() { |
| 78 | line := scanner.Text() |
| 79 | if len(line) > 7 && isXDigit(line[0]) && isXDigit(line[1]) && line[2] == ':' { |
| 80 | l.EC = l.AssignHexLine(line, l.EC) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if err := scanner.Err(); err != nil { |
| 85 | log.Fatal(err) |
| 86 | } |
| 87 | |
| 88 | return l.EC |
| 89 | } |
| 90 | |
| 91 | func (l *LogDevReader) GetACPI() (Tables map[string][]byte) { |
| 92 | if l.ACPITables != nil { |
| 93 | return l.ACPITables |
| 94 | } |
| 95 | l.ACPITables = Tables |
| 96 | |
| 97 | file, err := os.Open(l.InputDirectory + "/acpidump.log") |
| 98 | if err != nil { |
| 99 | log.Fatal(err) |
| 100 | } |
| 101 | defer file.Close() |
| 102 | |
| 103 | scanner := bufio.NewScanner(file) |
| 104 | |
| 105 | Tables = map[string][]byte{} |
| 106 | |
| 107 | curTable := "" |
| 108 | for scanner.Scan() { |
| 109 | line := scanner.Text() |
Iru Cai | b95a1a4 | 2019-03-05 15:56:39 +0800 | [diff] [blame] | 110 | /* Only supports ACPI tables up to 0x100000 in size, FIXME if needed */ |
| 111 | is_hexline, _ := regexp.MatchString(" *[0-9A-Fa-f]{4,5}: ", line) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 112 | switch { |
| 113 | case len(line) >= 6 && line[5] == '@': |
| 114 | curTable = line[0:4] |
Iru Cai | b95a1a4 | 2019-03-05 15:56:39 +0800 | [diff] [blame] | 115 | Tables[curTable] = make([]byte, 0, 0x100000) |
Arthur Heymans | 8bd25ab | 2018-08-12 00:21:08 +0200 | [diff] [blame] | 116 | case is_hexline: |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 117 | Tables[curTable] = l.AssignHexLine(line, Tables[curTable]) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if err := scanner.Err(); err != nil { |
| 122 | log.Fatal(err) |
| 123 | } |
| 124 | |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | func (l *LogDevReader) GetPCIList() (PCIList []PCIDevData) { |
| 129 | file, err := os.Open(l.InputDirectory + "/lspci.log") |
| 130 | if err != nil { |
| 131 | log.Fatal(err) |
| 132 | } |
| 133 | defer file.Close() |
| 134 | |
| 135 | scanner := bufio.NewScanner(file) |
| 136 | |
| 137 | PCIList = []PCIDevData{} |
| 138 | |
| 139 | for scanner.Scan() { |
| 140 | line := scanner.Text() |
| 141 | switch { |
| 142 | case !(len(line) < 7 || !isXDigit(line[0]) || !isXDigit(line[1]) || line[2] != ':' || !isXDigit(line[3]) || !isXDigit(line[4]) || line[5] != '.' || !isXDigit(line[6])): |
| 143 | cur := PCIDevData{} |
| 144 | fmt.Sscanf(line, "%x:%x.%x", &cur.Bus, &cur.Dev, &cur.Func) |
| 145 | lc := strings.LastIndex(line, ":") |
| 146 | li := strings.LastIndex(line[0:lc], "[") |
| 147 | if li < 0 { |
| 148 | continue |
| 149 | } |
| 150 | ven := 0 |
| 151 | dev := 0 |
| 152 | fmt.Sscanf(line[li+1:], "%x:%x", &ven, &dev) |
| 153 | cur.PCIDevID = uint16(dev) |
| 154 | cur.PCIVenID = uint16(ven) |
| 155 | cur.ConfigDump = make([]byte, 0x100, 0x1000) |
| 156 | PCIList = append(PCIList, cur) |
| 157 | case len(line) > 7 && isXDigit(line[0]) && line[1] == '0' && line[2] == ':': |
| 158 | start := 0 |
| 159 | fmt.Sscanf(line, "%x:", &start) |
| 160 | cur := &PCIList[len(PCIList)-1] |
| 161 | cur.ConfigDump = l.AssignHexLine(line, cur.ConfigDump) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if err := scanner.Err(); err != nil { |
| 166 | log.Fatal(err) |
| 167 | } |
| 168 | |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | func (l *LogDevReader) GetInteltool() (ret InteltoolData) { |
| 173 | file, err := os.Open(l.InputDirectory + "/inteltool.log") |
| 174 | if err != nil { |
| 175 | log.Fatal(err) |
| 176 | } |
| 177 | defer file.Close() |
| 178 | |
| 179 | scanner := bufio.NewScanner(file) |
| 180 | paragraph := "" |
| 181 | ret.GPIO = map[uint16]uint32{} |
| 182 | ret.RCBA = map[uint16]uint32{} |
| 183 | ret.IGD = map[uint32]uint32{} |
| 184 | for scanner.Scan() { |
| 185 | line := scanner.Text() |
| 186 | switch { |
| 187 | case len(line) > 7 && line[0] == '0' && line[1] == 'x' && line[6] == ':' && paragraph == "RCBA": |
| 188 | addr, value := 0, 0 |
| 189 | fmt.Sscanf(line, "0x%x: 0x%x", &addr, &value) |
| 190 | ret.RCBA[uint16(addr)] = uint32(value) |
| 191 | case len(line) > 9 && line[0] == '0' && line[1] == 'x' && line[8] == ':' && paragraph == "IGD": |
| 192 | addr, value := 0, 0 |
| 193 | fmt.Sscanf(line, "0x%x: 0x%x", &addr, &value) |
| 194 | ret.IGD[uint32(addr)] = uint32(value) |
| 195 | case strings.Contains(line, "DEFAULT"): |
| 196 | continue |
| 197 | case strings.Contains(line, "DIFF"): |
| 198 | continue |
| 199 | case strings.HasPrefix(line, "gpiobase"): |
| 200 | addr, value := 0, 0 |
| 201 | fmt.Sscanf(line, "gpiobase+0x%x: 0x%x", &addr, &value) |
| 202 | ret.GPIO[uint16(addr)] = uint32(value) |
| 203 | case strings.HasPrefix(line, "============="): |
| 204 | paragraph = strings.Trim(line, "= ") |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | if err := scanner.Err(); err != nil { |
| 209 | log.Fatal(err) |
| 210 | } |
| 211 | return |
| 212 | } |
| 213 | |
| 214 | func (l *LogDevReader) GetDMI() (ret DMIData) { |
| 215 | file, err := os.Open(l.InputDirectory + "/dmidecode.log") |
| 216 | if err != nil { |
| 217 | log.Fatal(err) |
| 218 | } |
| 219 | defer file.Close() |
| 220 | |
| 221 | scanner := bufio.NewScanner(file) |
| 222 | paragraph := "" |
| 223 | for scanner.Scan() { |
| 224 | line := scanner.Text() |
| 225 | if !strings.HasPrefix(line, "\t") { |
| 226 | paragraph = strings.TrimSpace(line) |
| 227 | continue |
| 228 | } |
| 229 | idx := strings.Index(line, ":") |
| 230 | if idx < 0 { |
| 231 | continue |
| 232 | } |
| 233 | name := strings.TrimSpace(line[0:idx]) |
| 234 | value := strings.TrimSpace(line[idx+1:]) |
| 235 | switch paragraph + ":" + name { |
| 236 | case "System Information:Manufacturer": |
| 237 | ret.Vendor = value |
| 238 | case "System Information:Product Name": |
| 239 | ret.Model = value |
| 240 | case "System Information:Version": |
| 241 | ret.Version = value |
| 242 | case "Chassis Information:Type": |
| 243 | ret.IsLaptop = (value == "Notebook" || value == "Laptop") |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if err := scanner.Err(); err != nil { |
| 248 | log.Fatal(err) |
| 249 | } |
| 250 | return |
| 251 | } |
| 252 | |
| 253 | func (l *LogDevReader) GetAzaliaCodecs() (ret []AzaliaCodec) { |
Iru Cai | 112e9ba | 2019-06-29 14:06:30 +0800 | [diff] [blame] | 254 | cardno := -1 |
| 255 | for i := 0; i < 10; i++ { |
| 256 | pin, err := os.Open(l.InputDirectory + "/pin_hwC" + strconv.Itoa(i) + "D0") |
| 257 | if err == nil { |
| 258 | pin.Close() |
| 259 | cardno = i |
| 260 | break |
| 261 | } |
| 262 | } |
| 263 | if cardno == -1 { |
| 264 | return |
| 265 | } |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 266 | for codecno := 0; codecno < 10; codecno++ { |
| 267 | cur := AzaliaCodec{CodecNo: codecno, PinConfig: map[int]uint32{}} |
| 268 | codec, err := os.Open(l.InputDirectory + "/codec#" + strconv.Itoa(codecno)) |
| 269 | if err != nil { |
| 270 | continue |
| 271 | } |
| 272 | defer codec.Close() |
Iru Cai | 112e9ba | 2019-06-29 14:06:30 +0800 | [diff] [blame] | 273 | pin, err := os.Open(l.InputDirectory + "/pin_hwC" + strconv.Itoa(cardno) + |
| 274 | "D" + strconv.Itoa(codecno)) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 275 | if err != nil { |
| 276 | continue |
| 277 | } |
| 278 | defer pin.Close() |
| 279 | |
| 280 | scanner := bufio.NewScanner(codec) |
| 281 | for scanner.Scan() { |
| 282 | line := scanner.Text() |
| 283 | if strings.HasPrefix(line, "Codec:") { |
| 284 | fmt.Sscanf(line, "Codec: %s", &cur.Name) |
| 285 | continue |
| 286 | } |
| 287 | if strings.HasPrefix(line, "Vendor Id:") { |
| 288 | fmt.Sscanf(line, "Vendor Id: 0x%x", &cur.VendorID) |
| 289 | continue |
| 290 | } |
| 291 | if strings.HasPrefix(line, "Subsystem Id:") { |
| 292 | fmt.Sscanf(line, "Subsystem Id: 0x%x", &cur.SubsystemID) |
| 293 | continue |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | scanner = bufio.NewScanner(pin) |
| 298 | for scanner.Scan() { |
| 299 | line := scanner.Text() |
| 300 | addr := 0 |
| 301 | val := uint32(0) |
| 302 | fmt.Sscanf(line, "0x%x 0x%x", &addr, &val) |
| 303 | cur.PinConfig[addr] = val |
| 304 | } |
| 305 | ret = append(ret, cur) |
| 306 | } |
| 307 | return |
| 308 | } |
| 309 | |
| 310 | func (l *LogDevReader) GetIOPorts() []IOPorts { |
| 311 | file, err := os.Open(l.InputDirectory + "/ioports.log") |
| 312 | if err != nil { |
| 313 | log.Fatal(err) |
| 314 | } |
| 315 | defer file.Close() |
| 316 | scanner := bufio.NewScanner(file) |
| 317 | ret := make([]IOPorts, 0, 100) |
| 318 | for scanner.Scan() { |
| 319 | line := scanner.Text() |
| 320 | el := IOPorts{} |
| 321 | fmt.Sscanf(line, " %x-%x : %s", &el.Start, &el.End, &el.Usage) |
| 322 | ret = append(ret, el) |
| 323 | } |
| 324 | |
| 325 | if err := scanner.Err(); err != nil { |
| 326 | log.Fatal(err) |
| 327 | } |
| 328 | return ret |
| 329 | |
| 330 | } |
| 331 | |
| 332 | func (l *LogDevReader) GetCPUModel() (ret []uint32) { |
| 333 | file, err := os.Open(l.InputDirectory + "/cpuinfo.log") |
| 334 | if err != nil { |
| 335 | log.Fatal(err) |
| 336 | } |
| 337 | defer file.Close() |
| 338 | |
| 339 | scanner := bufio.NewScanner(file) |
| 340 | ret = make([]uint32, 0, 100) |
| 341 | proc := 0 |
| 342 | for scanner.Scan() { |
| 343 | line := scanner.Text() |
| 344 | sep := strings.Index(line, ":") |
| 345 | if sep < 0 { |
| 346 | continue |
| 347 | } |
| 348 | key := strings.TrimSpace(line[0:sep]) |
| 349 | val := strings.TrimSpace(line[sep+1:]) |
| 350 | |
| 351 | if key == "processor" { |
| 352 | proc, _ := strconv.Atoi(val) |
| 353 | if len(ret) <= proc { |
| 354 | ret = ret[0 : proc+1] |
| 355 | } |
| 356 | continue |
| 357 | } |
| 358 | if key == "cpu family" { |
| 359 | family, _ := strconv.Atoi(val) |
| 360 | ret[proc] |= uint32(((family & 0xf) << 8) | ((family & 0xff0) << 16)) |
| 361 | } |
| 362 | if key == "model" { |
| 363 | model, _ := strconv.Atoi(val) |
| 364 | ret[proc] |= uint32(((model & 0xf) << 4) | ((model & 0xf0) << 12)) |
| 365 | } |
| 366 | if key == "stepping" { |
| 367 | stepping, _ := strconv.Atoi(val) |
| 368 | ret[proc] |= uint32(stepping & 0xf) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if err := scanner.Err(); err != nil { |
| 373 | log.Fatal(err) |
| 374 | } |
| 375 | return |
| 376 | } |
| 377 | |
Vladimir Serbinenko | 91195c6 | 2015-05-29 23:53:37 +0200 | [diff] [blame] | 378 | func (l *LogDevReader) HasPS2() bool { |
| 379 | file, err := os.Open(l.InputDirectory + "/input_bustypes.log") |
| 380 | if err != nil { |
| 381 | log.Fatal(err) |
| 382 | } |
| 383 | defer file.Close() |
| 384 | scanner := bufio.NewScanner(file) |
| 385 | for scanner.Scan() { |
| 386 | line := scanner.Text() |
| 387 | if strings.Index(line, "0011") >= 0 { |
| 388 | return true |
| 389 | } |
| 390 | } |
| 391 | return false |
| 392 | } |
| 393 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 394 | var FlagLogInput = flag.String("input_log", ".", "Input log directory") |
| 395 | var FlagLogMkLogs = flag.Bool("make_logs", false, "Dump logs") |
| 396 | |
| 397 | func MakeLogReader() *LogDevReader { |
| 398 | if *FlagLogMkLogs { |
| 399 | MakeLogs(*FlagLogInput) |
| 400 | } |
| 401 | return &LogDevReader{InputDirectory: *FlagLogInput} |
| 402 | } |