forked from SonicCloudOrg/sonic-gidevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagemounter.go
141 lines (115 loc) · 3.23 KB
/
imagemounter.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
package giDevice
import (
"fmt"
"github.com/SonicCloudOrg/sonic-gidevice/pkg/libimobiledevice"
"os"
)
var _ ImageMounter = (*imageMounter)(nil)
func newImageMounter(client *libimobiledevice.ImageMounterClient) *imageMounter {
return &imageMounter{
client: client,
}
}
type imageMounter struct {
client *libimobiledevice.ImageMounterClient
}
func (m *imageMounter) Images(imgType string) (imageSignatures [][]byte, err error) {
var pkt libimobiledevice.Packet
if pkt, err = m.client.NewXmlPacket(
m.client.NewBasicRequest(libimobiledevice.CommandTypeLookupImage, imgType),
); err != nil {
return nil, err
}
if err = m.client.SendPacket(pkt); err != nil {
return nil, err
}
var respPkt libimobiledevice.Packet
if respPkt, err = m.client.ReceivePacket(); err != nil {
return nil, err
}
var reply libimobiledevice.ImageMounterLookupImageResponse
if err = respPkt.Unmarshal(&reply); err != nil {
return nil, err
}
imageSignatures = reply.ImageSignature
return
}
func (m *imageMounter) UploadImage(imgType, dmgPath string, signatureData []byte) (err error) {
var dmgFileInfo os.FileInfo
if dmgFileInfo, err = os.Stat(dmgPath); err != nil {
return err
}
var pkt libimobiledevice.Packet
if pkt, err = m.client.NewXmlPacket(
m.client.NewReceiveBytesRequest(imgType, uint32(dmgFileInfo.Size()), signatureData),
); err != nil {
return err
}
if err = m.client.SendPacket(pkt); err != nil {
return err
}
var respPkt libimobiledevice.Packet
if respPkt, err = m.client.ReceivePacket(); err != nil {
return err
}
var reply libimobiledevice.ImageMounterBasicResponse
if err = respPkt.Unmarshal(&reply); err != nil {
return err
}
if reply.Status != "ReceiveBytesAck" {
return fmt.Errorf("image mounter 'ReceiveBytes' status: %s", reply.Status)
}
var dmgData []byte
if dmgData, err = os.ReadFile(dmgPath); err != nil {
return err
}
if err = m.client.SendDmg(dmgData); err != nil {
return err
}
if respPkt, err = m.client.ReceivePacket(); err != nil {
return err
}
if err = respPkt.Unmarshal(&reply); err != nil {
return err
}
if reply.Status != "Complete" {
return fmt.Errorf("image mounter 'SendDmg' status: %s", reply.Status)
}
return
}
func (m *imageMounter) Mount(imgType, devImgPath string, signatureData []byte) (err error) {
var pkt libimobiledevice.Packet
if pkt, err = m.client.NewXmlPacket(
m.client.NewMountImageRequest(imgType, devImgPath, signatureData),
); err != nil {
return err
}
if err = m.client.SendPacket(pkt); err != nil {
return err
}
var respPkt libimobiledevice.Packet
if respPkt, err = m.client.ReceivePacket(); err != nil {
return err
}
var reply libimobiledevice.ImageMounterBasicResponse
if err = respPkt.Unmarshal(&reply); err != nil {
return err
}
if reply.Status != "Complete" {
return fmt.Errorf("image mounter 'MountImage' status: %s", reply.Status)
}
return
}
func (m *imageMounter) UploadImageAndMount(imgType, devImgPath, dmgPath, signaturePath string) (err error) {
var signatureData []byte
if signatureData, err = os.ReadFile(signaturePath); err != nil {
return err
}
if err = m.UploadImage(imgType, dmgPath, signatureData); err != nil {
return err
}
if err = m.Mount(imgType, devImgPath, signatureData); err != nil {
return err
}
return
}