This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
forked from CIS-Mobile/device-flasher
-
Notifications
You must be signed in to change notification settings - Fork 7
/
factoryimage.go
199 lines (176 loc) · 5.17 KB
/
factoryimage.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package factoryimage
import (
"bufio"
"errors"
"fmt"
"github.com/mholt/archiver/v3"
"github.com/sirupsen/logrus"
"github.com/aospalliance/device-flasher/internal/device"
"github.com/aospalliance/device-flasher/internal/platformtools"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
const JASMINE_OREO = "jasmine_global_images_V9.6.17.0.ODIMIFE_20181108.0000.00_8.1_1c60295d1c.tgz"
var (
ErrorFailedToFlash = errors.New("failed to flash device")
ErrorValidation = errors.New("failed to validate image for device")
)
type Config struct {
HostOS string
ImagePath string
WorkingDirectory string
Logger *logrus.Logger
}
type FactoryImage struct {
hostOS string
extractedDirectory string
flashAllScript string
ImagePath string
workingDirectory string
logger *logrus.Logger
IsExtracted bool
}
func New(config *Config) *FactoryImage {
return &FactoryImage{
hostOS: config.HostOS,
workingDirectory: config.WorkingDirectory,
ImagePath: config.ImagePath,
logger: config.Logger,
}
}
func (f *FactoryImage) FlashAll(device *device.Device, platformToolsPath platformtools.PlatformToolsPath) error {
pathEnvironmentVariable := "PATH"
if f.hostOS == "windows" {
pathEnvironmentVariable = "Path"
}
path := os.Getenv(pathEnvironmentVariable)
pathWithPlatformTools := string(platformToolsPath) + string(os.PathListSeparator) + path
flashAll := fmt.Sprintf(".%v%v", string(os.PathSeparator), f.flashAllScript)
flashAllExtra := ""
if filepath.Base(f.ImagePath) == JASMINE_OREO && f.hostOS == "linux" {
flashAllExtra = "/bin/sh"
}
f.logger.WithFields(logrus.Fields{
"flashAll": flashAll,
}).Debug("running flash all script on device")
flashCmd := exec.Command(flashAll)
// XXX: JASMINE_OREO
if flashAllExtra != "" {
flashCmd = exec.Command(flashAllExtra, flashAll)
}
flashCmd.Dir = f.extractedDirectory
flashCmd.Env = os.Environ()
flashCmd.Env = append(flashCmd.Env, fmt.Sprintf("%v=%v", pathEnvironmentVariable, pathWithPlatformTools))
flashCmd.Env = append(flashCmd.Env, fmt.Sprintf("ANDROID_SERIAL=%v", device.ID))
cmdStdoutReader, err := flashCmd.StdoutPipe()
if err != nil {
return fmt.Errorf("%w: %v", ErrorFailedToFlash, err)
}
cmdStderrReader, err := flashCmd.StderrPipe()
if err != nil {
return fmt.Errorf("%w: %v", ErrorFailedToFlash, err)
}
scannerStdout := bufio.NewScanner(cmdStdoutReader)
go func() {
for scannerStdout.Scan() {
f.logger.Infof("%v | %s", device.String(), scannerStdout.Text())
}
}()
scannerStderr := bufio.NewScanner(cmdStderrReader)
go func() {
for scannerStderr.Scan() {
f.logger.Infof("%v | %s", device.String(), scannerStderr.Text())
}
}()
err = flashCmd.Start()
if err != nil {
return fmt.Errorf("%w: %v", ErrorFailedToFlash, err)
}
err = flashCmd.Wait()
if err != nil {
return fmt.Errorf("%w: %v", ErrorFailedToFlash, err)
}
return nil
}
func (f *FactoryImage) Validate(deviceCodename device.Codename) error {
f.logger.WithFields(logrus.Fields{
"deviceCodename": deviceCodename,
}).Debug("running factory image validation")
if _, err := os.Stat(f.ImagePath); os.IsNotExist(err) {
return fmt.Errorf("%w: %v", ErrorValidation, err)
}
if filepath.Base(f.ImagePath) == JASMINE_OREO {
return nil
} else {
if !strings.Contains(f.ImagePath, strings.ToLower(string(deviceCodename))) {
return fmt.Errorf("%w: image filename should contain device codename %v", ErrorValidation, deviceCodename)
}
if !strings.Contains(f.ImagePath, "factory") {
return fmt.Errorf("%w: image filename should contain 'factory'", ErrorValidation)
}
}
return nil
}
func (f *FactoryImage) Extract() error {
if f.IsExtracted {
f.logger.Debugf("already extracted %v to %v", f.ImagePath, f.workingDirectory)
return nil
}
err := f.extract()
if err != nil {
return err
}
err = f.postExtractValidations()
if err != nil {
return err
}
f.IsExtracted = true
return nil
}
func (f *FactoryImage) extract() error {
f.logger.Infof("Extracting factory image: %v", f.ImagePath)
err := archiver.Unarchive(f.ImagePath, f.workingDirectory)
if err != nil {
return err
}
return nil
}
func (f *FactoryImage) postExtractValidations() error {
if filepath.Base(f.ImagePath) == JASMINE_OREO {
if f.hostOS == "windows" {
f.flashAllScript = "flash_all.bat"
} else {
f.flashAllScript = "flash_all.sh"
}
} else {
if f.hostOS == "windows" {
f.flashAllScript = "flash-all.bat"
} else {
f.flashAllScript = "flash-all.sh"
}
}
// TODO this can probably be simplified
files, err := ioutil.ReadDir(f.workingDirectory)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
_, err := os.Stat(f.workingDirectory + file.Name() + string(os.PathSeparator) + f.flashAllScript)
if err != nil {
f.extractedDirectory = f.workingDirectory + string(os.PathSeparator) + file.Name()
}
}
}
if f.extractedDirectory == "" {
return fmt.Errorf("unable to find %v in directory %v", f.flashAllScript, f.workingDirectory)
}
f.logger.WithFields(logrus.Fields{
"flashAllScript": f.flashAllScript,
"extractedDirectory": f.extractedDirectory,
}).Debug("validated extracted factory image")
return nil
}