1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

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

View File

@ -42,10 +42,6 @@ func execOutput(name string, arg ...string) (output string, err error) {
return
}
func nodeName() (string, error) {
return execOutput("uname", "-n")
}
func parseVersion(str string) (major, minor, patch int, err error) {
versionNumber := strings.Split(str, "-")
parts := strings.Split(versionNumber[0], ".")
@ -77,24 +73,33 @@ func kernelVersion() (major, minor, patch int, err error) {
return parseVersion(output)
}
func getPiRevision() (int, error) {
//default return code of a rev2 board
cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo")
func cpuInfo() (model, hardware string, revision int, err error) {
output, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
return 4, err
return "", "", 0, err
}
for _, line := range strings.Split(string(cpuinfo), "\n") {
fields := strings.Fields(line)
if len(fields) > 0 && fields[0] == "Revision" {
rev, err := strconv.ParseInt(fields[2], 16, 8)
return int(rev), err
for _, line := range strings.Split(string(output), "\n") {
fields := strings.Split(line, ":")
if len(fields) < 1 {
continue
}
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.
func DetectHost() (Host, int, error) {
func DetectHost() (host Host, rev int, err error) {
major, minor, patch, err := kernelVersion()
if err != nil {
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)
}
node, err := nodeName()
model, hardware, rev, err := cpuInfo()
if err != nil {
return HostNull, 0, err
}
var host Host
var rev int
switch node {
case "raspberrypi":
host = HostRPi
rev, _ = getPiRevision()
case "beaglebone":
host = HostBBB
switch {
case strings.Contains(model, "ARMv7") && (strings.Contains(hardware, "AM33XX") || strings.Contains(hardware, "AM335X")):
return HostBBB, rev, nil
case strings.Contains(hardware, "BCM2708"):
return HostRPi, rev, nil
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
}