detect host based on cpuinfo

This commit is contained in:
Ben Delarre 2015-01-13 14:15:25 -08:00 committed by Karan Misra
parent 9aaadba2e3
commit bf6e647c1d
1 changed files with 27 additions and 28 deletions

View File

@ -42,10 +42,6 @@ func execOutput(name string, arg ...string) (output string, err error) {
return return
} }
func nodeName() (string, error) {
return execOutput("uname", "-n")
}
func parseVersion(str string) (major, minor, patch int, err error) { func parseVersion(str string) (major, minor, patch int, err error) {
versionNumber := strings.Split(str, "-") versionNumber := strings.Split(str, "-")
parts := strings.Split(versionNumber[0], ".") parts := strings.Split(versionNumber[0], ".")
@ -77,24 +73,33 @@ func kernelVersion() (major, minor, patch int, err error) {
return parseVersion(output) return parseVersion(output)
} }
func getPiRevision() (int, error) { func cpuInfo() (model, hardware string, revision int, err error) {
//default return code of a rev2 board output, err := ioutil.ReadFile("/proc/cpuinfo")
cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil { if err != nil {
return 4, err return "", "", 0, err
} }
for _, line := range strings.Split(string(cpuinfo), "\n") { for _, line := range strings.Split(string(output), "\n") {
fields := strings.Fields(line) fields := strings.Split(line, ":")
if len(fields) > 0 && fields[0] == "Revision" { if len(fields) < 1 {
rev, err := strconv.ParseInt(fields[2], 16, 8) continue
return int(rev), err }
if strings.HasPrefix(fields[0], "Revision") {
rev, err := strconv.ParseInt(fields[1], 16, 8)
if err != nil {
continue
}
revision = int(rev)
} else if strings.HasPrefix(fields[0], "Hardware") {
hardware = fields[1]
} else if strings.HasPrefix(fields[0], "model name") {
model = fields[1]
} }
} }
return 4, nil return model, hardware, revision, nil
} }
// DetectHost returns the detected host and its revision number. // DetectHost returns the detected host and its revision number.
func DetectHost() (Host, int, error) { func DetectHost() (host Host, rev int, err error) {
major, minor, patch, err := kernelVersion() major, minor, patch, err := kernelVersion()
if err != nil { if err != nil {
return HostNull, 0, err return HostNull, 0, err
@ -104,23 +109,17 @@ func DetectHost() (Host, int, error) {
return HostNull, 0, fmt.Errorf("embd: linux kernel versions lower than 3.8 are not supported. you have %v.%v.%v", major, minor, patch) return HostNull, 0, fmt.Errorf("embd: linux kernel versions lower than 3.8 are not supported. you have %v.%v.%v", major, minor, patch)
} }
node, err := nodeName() model, hardware, rev, err := cpuInfo()
if err != nil { if err != nil {
return HostNull, 0, err return HostNull, 0, err
} }
var host Host switch {
var rev int case strings.Contains(model, "ARMv7") && (strings.Contains(hardware, "AM33XX") || strings.Contains(hardware, "AM335X")):
return HostBBB, rev, nil
switch node { case strings.Contains(hardware, "BCM2708"):
case "raspberrypi": return HostRPi, rev, nil
host = HostRPi
rev, _ = getPiRevision()
case "beaglebone":
host = HostBBB
default: default:
return HostNull, 0, fmt.Errorf("embd: your host %q is not supported at this moment. please request support at https://github.com/kidoman/embd/issues", node) return HostNull, 0, fmt.Errorf("embd: your host \"%v:%v\" is not supported at this moment. request support at https://github.com/kidoman/embd/issues", host, model)
} }
return host, rev, nil
} }