gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/pci/parse.go (about)

     1  // Copyright 2017-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pci
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  )
    11  
    12  func isHex(b byte) bool {
    13  	return ('a' <= b && b <= 'f') || ('A' <= b && b <= 'F') || ('0' <= b && b <= '9')
    14  }
    15  
    16  // scan searches for Vendor and Device lines from the input *bufio.Scanner based
    17  // on pci.ids format. Found Vendors and Devices are added to the input ids map.
    18  func scan(s *bufio.Scanner, ids map[string]Vendor) {
    19  	var currentVendor string
    20  	var line string
    21  
    22  	for s.Scan() {
    23  		line = s.Text()
    24  
    25  		switch {
    26  		case isHex(line[0]) && isHex(line[1]):
    27  			currentVendor = line[:4]
    28  			ids[currentVendor] = Vendor{Name: line[6:], Devices: make(map[string]Device)}
    29  		case currentVendor != "" && line[0] == '\t' && isHex(line[1]) && isHex(line[3]):
    30  			ids[currentVendor].Devices[line[1:5]] = Device(line[7:])
    31  		}
    32  	}
    33  }
    34  
    35  func parse(input []byte) idMap {
    36  	ids := make(map[string]Vendor)
    37  	s := bufio.NewScanner(bytes.NewReader(input))
    38  	scan(s, ids)
    39  	return ids
    40  }