-
Notifications
You must be signed in to change notification settings - Fork 16
/
ota.go
176 lines (136 loc) · 4 KB
/
ota.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package ipsw
import (
"errors"
"io/ioutil"
"strconv"
"strings"
"howett.net/plist"
)
const OTABuildManifestFilename = "AssetData/boot/BuildManifest.plist"
type OTAXML struct {
Assets []*OTAFirmware
}
// Firmware release
type OTAFirmware struct {
BuildID string `plist:"Build"`
InstallationSize string `plist:"InstallationSize"`
Version string `plist:"OSVersion"`
PrerequisiteBuild string `plist:"PrerequisiteBuild"`
PrerequisiteOSVersion string `plist:"PrerequisiteOSVersion"`
ReleaseType string `plist:"ReleaseType"`
DocumentationID string `plist:"SUDocumentationID"`
SupportedDevices []Identifier `plist:"SupportedDevices"`
DownloadSize int `plist:"_DownloadSize"`
UnarchivedSize int `plist:"_UnarchivedSize"`
BaseURL string `plist:"__BaseURL"`
RelativePath string `plist:"__RelativePath"`
MarketingVersion string `plist:"MarketingVersion"` // for watches
}
func (o *OTAFirmware) GetURL() string {
return o.BaseURL + o.RelativePath
}
// NewOTAXML generates a new OTA XML struct
func NewOTAXML(src string) (*OTAXML, error) {
resp, err := DefaultClient.Get(src)
if err != nil {
return nil, err
}
defer resp.Body.Close()
document, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var ota OTAXML
_, err = plist.Unmarshal(document, &ota)
if err != nil {
return nil, err
}
return &ota, err
}
type OTABuildManifest struct {
BuildIdentities []*OTABuildIdentity `plist:"BuildIdentities"`
SupportedProductTypes []Identifier `plist:"SupportedProductTypes"`
ProductVersion string `plist:"ProductVersion"`
ProductBuildVersion string `plist:"ProductBuildVersion"`
}
type OTABuildIdentity struct {
ApChipID string `plist:"ApChipID"`
ApBoardID string `plist:"ApBoardID"`
ApSecurityDomain string `plist:"ApSecurityDomain"`
UniqueBuildID []byte `plist:"UniqueBuildID"`
Manifest map[string]*Image `plist:"Manifest"`
Info *OTABuildIdentityInfo `plist:"Info"`
}
type Image struct {
Info struct {
Path string `plist:"Path"`
} `plist:"Info"`
}
type OTABuildIdentityInfo struct {
DeviceClass string `plist:"DeviceClass"`
}
type OTAZip struct {
*IPSW
manifest *OTABuildManifest
}
func NewOTAZip(identifier, build, resource string) *OTAZip {
return &OTAZip{
IPSW: &IPSW{
Identifier: identifier,
BuildID: build,
Resource: resource,
},
}
}
func (z *OTAZip) BuildManifest() (*OTABuildManifest, error) {
if z.manifest != nil {
return z.manifest, nil
}
var manifest OTABuildManifest
err := z.PlistFromZip(OTABuildManifestFilename, &manifest)
z.manifest = &manifest
return &manifest, err
}
func (m *OTABuildManifest) DeviceByIdentifier(identifier Identifier) (*Device, error) {
var identity *OTABuildIdentity
if len(m.BuildIdentities) < 1 {
return nil, errors.New("invalid build identities")
} else if len(m.SupportedProductTypes) != len(m.BuildIdentities) {
// just use 0
identity = m.BuildIdentities[0]
} else {
for index, device := range m.SupportedProductTypes {
if device != identifier {
continue
}
identity = m.BuildIdentities[index]
}
}
chipID, err := strconv.ParseInt(identity.ApChipID, 0, 0)
if err != nil {
return nil, err
}
boardID, err := strconv.ParseInt(identity.ApBoardID, 0, 0)
if err != nil {
return nil, err
}
device := &Device{
Identifier: identifier,
BoardConfig: identity.Info.DeviceClass,
CPID: int(chipID),
BDID: int(boardID),
}
// @TODO this is a hack and should be removed...
if val, ok := identity.Manifest["AppleLogo"]; ok {
device.Platform = findPlatformFromPath(val.Info.Path)
}
return device, err
}
func findPlatformFromPath(path string) string {
parts := strings.Split(path, ".")
num := len(parts)
if num > 2 {
return parts[num-2]
}
return ""
}