-
Notifications
You must be signed in to change notification settings - Fork 277
/
compatibility.go
138 lines (123 loc) · 4.66 KB
/
compatibility.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright 2020-present Facebook. All Rights Reserved.
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
package validate
import (
"fmt"
"log"
"strings"
"syscall"
"github.com/facebook/openbmc/tools/flashy/lib/fileutils"
"github.com/facebook/openbmc/tools/flashy/lib/utils"
"github.com/pkg/errors"
)
// Deal with images that have changed names, but are otherwise compatible.
// The version strings are free form, so to come up with regexes that safely
// matches all possible formats would be tough. Instead, we use this to do
// substitutions before matching in areVersionsCompatible().
// NB: the values of this mapping CANNOT contain a dash!
var compatibleVersionMapping = map[string]string{
"fby2-gpv2": "fbgp2",
"fby3pvt": "fby3",
"fby3vboot2": "fby3",
"fbnd": "northdome",
"kodiak": "sandia",
}
var normalizeVersion = func(ver string) string {
for k, v := range compatibleVersionMapping {
ver = strings.Replace(ver, k, v, 1)
}
return ver
}
// CheckImageBuildNameCompatibility checks compatibility of the image file by comparing the "normalized" build name of
// (1) the /etc/issue file and (2) the image file
// return an error if they do not match
var CheckImageBuildNameCompatibility = func(imageFilePath string) error {
etcIssueVer, err := utils.GetOpenBMCVersionFromIssueFile()
if err != nil {
return err
}
log.Printf("OpenBMC Version from /etc/issue: '%v'", etcIssueVer)
imageFileVer, err := GetOpenBMCVersionFromImageFile(imageFilePath)
if err != nil {
return err
}
log.Printf("OpenBMC Version from image file '%v': '%v'",
imageFilePath, imageFileVer)
// the two build names below are normalized versions
etcIssueBuildName, err := getNormalizedBuildNameFromVersion(etcIssueVer)
if err != nil {
return err
}
log.Printf("OpenBMC (normalized) build name from /etc/issue: '%v'", etcIssueBuildName)
imageFileBuildName, err := getNormalizedBuildNameFromVersion(imageFileVer)
if err != nil {
return err
}
log.Printf("OpenBMC (normalized) name from image file '%v': '%v'",
imageFilePath, imageFileBuildName)
// these build names might not match for old versions, as either /etc/isssue
// or the image file might not be well-formed
if etcIssueBuildName != "unknown" && etcIssueBuildName != imageFileBuildName {
err := errors.Errorf("OpenBMC versions from /etc/issue ('%v') and image file ('%v')"+
" do not match!",
etcIssueBuildName, imageFileBuildName)
return err
}
return nil
}
// getNormalizedBuildNameFromVersion gets the normalized build name from the version using
// compatibleVersionMapping.
// fby2-gpv2-v2019.43.1 -> fbgp2
// yosemite-v1.2 -> yosemite
var getNormalizedBuildNameFromVersion = func(ver string) (string, error) {
const rBuildname = "buildname"
nVer := normalizeVersion(ver)
buildNameRegEx := fmt.Sprintf(`^(?P<%v>\w+)`, rBuildname)
verMap, err := utils.GetRegexSubexpMap(buildNameRegEx, nVer)
if err != nil {
return "", errors.Errorf("Unable to get build name from version '%v' (normalized: '%v'): %v",
ver, nVer, err)
}
return verMap[rBuildname], nil
}
// GetOpenBMCVersionFromImageFile gets OpenBMC version from the image file.
// examples: fbtp-v2020.09.1, wedge100-v2020.07.1
// WARNING: This relies on the U-Boot version string on the image
// there is no guarantee that this will succeed
var GetOpenBMCVersionFromImageFile = func(imageFilePath string) (string, error) {
// mmap the first 1MB of the image file
imageFileBuf, err := fileutils.MmapFileRange(
imageFilePath, 0, 1024*1024, syscall.PROT_READ, syscall.MAP_SHARED,
)
if err != nil {
return "", errors.Errorf("Unable to read and mmap image file '%v': %v",
imageFilePath, err)
}
// unmap
defer fileutils.Munmap(imageFileBuf)
const rVersion = "version"
ubootVersionRegEx := fmt.Sprintf(`U-Boot \d+\.\d+ (?P<%v>[^\s]+)`, rVersion)
imageFileVerMap, err := utils.GetBytesRegexSubexpMap(ubootVersionRegEx, imageFileBuf)
if err != nil {
return "", errors.Errorf("Unable to find OpenBMC version in image file '%v': %v",
imageFilePath, err)
}
version := imageFileVerMap[rVersion]
return version, nil
}