forked from maruos/installer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
490 lines (417 loc) · 13.3 KB
/
main.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//
// Copyright 2017 The Maru OS Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path"
"runtime"
"time"
"./android"
"./remote"
"github.com/dixonwille/wmenu"
"github.com/pdsouza/toolbox.go/ui"
)
const (
// Success exit codes.
SuccessBase = 1<<5 + iota
SuccessUserAbort
SuccessBootloaderUnlocked
Success = 0
)
const (
// Error exit codes.
ErrorBase = 1<<6 + iota
ErrorPrereqs
ErrorUserInput
ErrorUsbPerms
ErrorAdb
ErrorFastboot
ErrorRemote
ErrorTWRP
)
var (
reader = bufio.NewReader(os.Stdin)
progressBar = ui.ProgressBar{0, 10, ""}
)
func iEcho(format string, a ...interface{}) {
fmt.Printf(format+"\n", a...)
}
func eEcho(msg string) {
iEcho(msg)
}
func verifyAdbStatusOrAbort(adb *android.AdbClient) {
status, err := adb.Status()
if err != nil {
eEcho("Failed to get adb status: " + err.Error())
exit(ErrorAdb)
}
if status == android.NoDeviceFound || status == android.DeviceUnauthorized {
eEcho(MsgAdbIssue)
exit(ErrorAdb)
} else if status == android.NoUsbPerms {
eEcho(MsgFixPerms)
exit(ErrorUsbPerms)
}
}
func verifyFastbootStatusOrAbort(fastboot *android.FastbootClient) {
status, err := fastboot.Status()
if err != nil {
eEcho("Failed to get fastboot status: " + err.Error())
exit(ErrorFastboot)
}
if status == android.NoDeviceFound {
eEcho(MsgFastbootNoDeviceFound)
exit(ErrorFastboot)
} else if status == android.NoUsbPerms {
eEcho(MsgFixPerms)
exit(ErrorUsbPerms)
}
}
func progressCallback(percent float64) {
progressBar.Progress = percent
fmt.Print("\r" + progressBar.Render())
if percent == 1.0 {
fmt.Println()
}
}
func waitForOpKey(msg string) {
fmt.Printf(msg)
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func exit(code int) {
// When run by double-clicking the executable on windows, the command
// prompt will immediately exit upon program completion, making it hard for
// users to see the last few messages. Let's explicitly wait for
// acknowledgement from the user.
if runtime.GOOS == "windows" {
fmt.Print("\nPress [Enter] to exit...")
reader.ReadLine() // pause until the user presses enter
}
os.Exit(code)
}
func main() {
nhDevices := readDevicesConfig()
/*
Step 1 - Set path to binaries
Step 2 - Verify ADB and Fastboot
Step 3 - Check USB permissions
Step 4 - Identify this is the correct device
Step 5 - Detect if device is unlocked, then unlock
Step 6 - Download: NethunterOS, TWRP Recovery, Kali Filesystem
Step 7 - Boot into TWRP
Step 8 - Install NH
*/
var versionFlag = flag.Bool("version", false, "print the program version")
flag.Parse()
if *versionFlag == true {
iEcho("Nethunter installer version %s %s/%s", Version, runtime.GOOS, runtime.GOARCH)
exit(Success)
}
myPath, err := os.Executable()
if err != nil {
panic(err)
}
// include any bundled binaries in PATH
err = os.Setenv("PATH", path.Dir(myPath)+":"+os.Getenv("PATH"))
if err != nil {
eEcho("Failed to set PATH to include installer tools: " + err.Error())
exit(ErrorPrereqs)
}
// try to use the installer dir as the workdir to make sure any temporary
// files or downloaded dependencies are isolated to the installer dir
if err = os.Chdir(path.Dir(myPath)); err != nil {
eEcho("Warning: failed to change working directory")
}
iEcho(MsgWelcome)
// (We can remove this later)
eEcho("The installer supports the following devices: ")
for _, d := range nhDevices.Device {
fmt.Printf(" - %s (%s)\n", d.Common_name, d.Product_name)
}
fmt.Print("\nAre you ready to install Nethunter? (yes/no): ")
responseBytes, _, err := reader.ReadLine()
if err != nil {
iEcho("Failed to read input: ", err.Error())
exit(ErrorUserInput)
}
if "yes" != string(responseBytes) {
iEcho("")
iEcho("Aborting installation.")
exit(SuccessUserAbort)
}
iEcho("")
iEcho("Verifying installer tools...")
adb := android.NewAdbClient()
if _, err := adb.Status(); err != nil {
eEcho("Failed to run adb: " + err.Error())
eEcho(MsgIncompleteZip)
exit(ErrorPrereqs)
}
fastboot := android.NewFastbootClient()
if _, err := fastboot.Status(); err != nil {
eEcho("Failed to run fastboot: " + err.Error())
eEcho(MsgIncompleteZip)
exit(ErrorPrereqs)
}
iEcho("Checking USB permissions...")
status, _ := fastboot.Status()
if status == android.NoDeviceFound {
// We are in ADB mode (normal boot or recovery).
verifyAdbStatusOrAbort(&adb)
iEcho("Rebooting your device into bootloader...")
err = adb.Reboot("bootloader")
if err != nil {
eEcho("Failed to reboot into bootloader: " + err.Error())
exit(ErrorAdb)
}
time.Sleep(7000 * time.Millisecond)
if status, err = fastboot.Status(); err != nil || status == android.NoDeviceFound {
eEcho("Failed to reboot device into bootloader!")
exit(ErrorAdb)
}
}
// We are in fastboot mode (the bootloader).
verifyFastbootStatusOrAbort(&fastboot)
iEcho("Identifying your device...")
productName, err := fastboot.GetProduct()
// OnePlus uses the same board name for every device. Need to let user select
if productName == "QC_Reference_Phone" {
menu := wmenu.NewMenu("Detected OnePlus device. Select which device: ")
menu.Action(func(opts []wmenu.Opt) error { productName = opts[0].Text; return nil })
menu.Option("OnePlus 5", nil, true, nil)
menu.Option("OnePlus 1", nil, false, nil)
err := menu.Run()
if err != nil {
log.Fatal(err)
}
}
if err != nil {
eEcho("Failed to get device product info: " + err.Error())
exit(ErrorFastboot)
}
currDevice := findDeviceConfig(nhDevices, productName)
// Check that we have the device config in the file
if currDevice.Common_name != "" {
fmt.Printf("Device and config found, using %s (%s) configuration and endpoints\n", currDevice.Common_name, currDevice.Product_name)
} else {
eEcho("Device config not found! Bye.")
exit(1)
}
waitForOpKey("Press enter to continue with bootloader unlock check. Unlocking will wipe device if first time and will require restart.") // not sure about the sentence here
unlocked, err := fastboot.Unlocked()
if err != nil {
iEcho("Warning: unable to determine bootloader lock state: " + err.Error())
}
if !unlocked {
iEcho("Unlocking bootloader, you will need to confirm this on your device...")
err = fastboot.Unlock()
if err != nil {
eEcho("Failed to unlock bootloader: " + err.Error())
exit(ErrorFastboot)
}
fastboot.Reboot()
iEcho(MsgUnlockSuccess)
exit(SuccessBootloaderUnlocked)
}
// Check if there is any other extra files we need to get
if currDevice.Extra_file != "" && currDevice.Extra_url != "" {
if _, err := os.Stat(currDevice.Extra_file); os.IsNotExist(err) { // If file missing, download
remote.DownloadURL(currDevice.Extra_url)
}
}
// Request nethunter OS
if _, err := os.Stat(currDevice.Nhos_file); os.IsNotExist(err) { // If file missing, download
remote.DownloadURL(currDevice.Nhos_url)
}
// Request nethunter generic fileysstem
if _, err := os.Stat(currDevice.Nhfs_file); os.IsNotExist(err) { // If file missing, download
remote.DownloadURL(currDevice.Nhfs_url)
}
// Request gapps
if _, err := os.Stat(currDevice.Gapps_file); os.IsNotExist(err) { // If file missing, download
remote.DownloadURL(currDevice.Gapps_url)
}
// Download TWRP
if _, err := os.Stat(currDevice.Twrp_file); os.IsNotExist(err) { // If file missing, download
remote.DownloadURL(currDevice.Twrp_url)
}
waitForOpKey("Press enter to start the installation")
// Flash TWRP recovery
iEcho("Starting TWRP flash")
err = fastboot.FlashRecovery(currDevice.Twrp_file)
if err != nil {
eEcho("Failed to flash TWRP Recovery: " + err.Error())
exit(ErrorTWRP)
}
// Boot into twrp
iEcho("Booting TWRP to flash Nethunter update zip.\n Swipe to allow system modification in TWRP and wait")
err = fastboot.Boot(currDevice.Twrp_file)
if err != nil {
eEcho("Failed to boot TWRP: " + err.Error())
exit(ErrorTWRP)
}
// Wait for TWRP
waitForOpKey("Press enter when TWRP is fully loaded & ready")
// Start fresh
iEcho("Removing previous installations")
time.Sleep(1000 * time.Millisecond)
err = adb.Shell("twrp wipe dalvik")
if err != nil {
eEcho("Failed to wipe dalvik: " + err.Error())
exit(ErrorTWRP)
}
iEcho("Removing previous /data")
time.Sleep(1000 * time.Millisecond)
err = adb.Shell("twrp wipe data")
if err != nil {
eEcho("Failed to wipe data: " + err.Error())
exit(ErrorTWRP)
}
iEcho("Removing previous /system")
time.Sleep(1000 * time.Millisecond)
err = adb.Shell("twrp wipe system")
if err != nil {
eEcho("Failed to wipe system: " + err.Error())
exit(ErrorTWRP)
}
// Transfer any extra files we need to flash
if currDevice.Extra_file != "" {
iEcho("Transferring extra zip (firmware/etc) to your device...")
if err = adb.PushFg(currDevice.Extra_file, "/sdcard"); err != nil {
eEcho("Failed to push extra update zip to device: " + err.Error())
exit(ErrorAdb)
}
}
// Transfer ROM to sdcard then install in TWRP
iEcho("Transferring the NethunterOS zip to your device...")
if err = adb.PushFg(currDevice.Nhos_file, "/sdcard"); err != nil {
eEcho("Failed to push NethunterOS update zip to device: " + err.Error())
exit(ErrorAdb)
}
// Transfer filesystem with app to sdcard then install
iEcho("Transferring the Nethunter filesystem zip to your device...")
if err = adb.PushFg(currDevice.Nhfs_file, "/sdcard"); err != nil {
eEcho("Failed to push Nethunter update zip to device: " + err.Error())
exit(ErrorAdb)
}
// Transfer filesystem with app to sdcard then install
iEcho("Transferring the Google Apps zip to your device...")
if err = adb.PushFg(currDevice.Gapps_file, "/sdcard"); err != nil {
eEcho("Failed to push Google Apps zip to device: " + err.Error())
exit(ErrorAdb)
}
// Extras should be installed first (like Device firmware or baseband)
// Otherwise NHOS will fail
if currDevice.Extra_file != "" {
iEcho("Installing extra zip (firmware/baseband/etc) please keep your device connected...")
err = adb.Shell("twrp install /sdcard/" + currDevice.Extra_file)
if err != nil {
eEcho("Failed to flash extra update zip: " + err.Error())
exit(ErrorTWRP)
}
}
// Start installer for ROM, Gapps, then Nethunter chroot & apps
iEcho("Installing NethunterOS please keep your device connected...")
err = adb.Shell("twrp install /sdcard/" + currDevice.Nhos_file)
if err != nil {
eEcho("Failed to flash Nethunter update zip: " + err.Error())
exit(ErrorTWRP)
}
// Install gapps?
actFunc := func(opts []wmenu.Opt) error {
if opts[0].ID == 0 {
iEcho("Installing Gapps...")
err = adb.Shell("twrp install /sdcard/" + currDevice.Gapps_file)
if err != nil {
eEcho("Failed to flash Google Apps: " + err.Error())
exit(ErrorTWRP)
}
}
if opts[0].ID == 1 {
fmt.Println("Skipping Gapps install")
}
return nil
}
menu := wmenu.NewMenu("Install Gapps?") // The yes or no question
menu.Action(actFunc)
menu.IsYesNo(0)
err = menu.Run()
if err != nil {
log.Fatal(err)
}
// Pause a bit after install or TWRP gets confused
// is this allways enought?
time.Sleep(10000 * time.Millisecond)
iEcho("Wiping your device without wiping /data/media...")
err = adb.Shell("twrp wipe cache")
if err != nil {
eEcho("Failed to wipe cache: " + err.Error())
exit(ErrorTWRP)
}
time.Sleep(1000 * time.Millisecond)
err = adb.Shell("twrp wipe dalvik")
if err != nil {
eEcho("Failed to wipe dalvik: " + err.Error())
exit(ErrorTWRP)
}
iEcho(MsgSuccess)
err = adb.Reboot("")
if err != nil {
eEcho("Failed to reboot: " + err.Error())
iEcho("\nPlease reboot your device manually by going to Reboot > System > Do Not Install")
exit(ErrorAdb)
}
// Wait for user to select install form usb option
iEcho(MsgReenable)
waitForOpKey("Press enter when ADB is reenabled")
verifyAdbStatusOrAbort(&adb)
iEcho("Rebooting your device into bootloader...")
err = adb.Reboot("bootloader")
if err != nil {
eEcho("Failed to reboot into bootloader: " + err.Error())
exit(ErrorAdb)
}
time.Sleep(30000 * time.Millisecond) // 30 seconds // maybe add waitForOpKey here also?
// Boot into twrp
iEcho("Booting TWRP to flash Nethunter update zip.\n Swipe to allow system modification in TWRP and wait")
err = fastboot.Boot(currDevice.Twrp_file)
if err != nil {
eEcho("Failed to boot TWRP: " + err.Error())
exit(ErrorTWRP)
}
// Wait for TWRP
waitForOpKey("Press enter when TWRP is fully loaded & ready")
time.Sleep(20000 * time.Millisecond) // maybe add waitForOpKey here also?
iEcho("Installing Nethunter filesystem, please keep your device connected...")
err = adb.Shell("twrp install /sdcard/" + currDevice.Nhfs_file)
if err != nil {
eEcho("Failed to flash Nethunter update zip: " + err.Error())
exit(ErrorTWRP)
}
time.Sleep(30000 * time.Millisecond) // 30 seconds // maybe add waitForOpKey here also?
iEcho(MsgFinished)
err = adb.Reboot("")
if err != nil {
eEcho("Failed to reboot: " + err.Error())
iEcho("\nPlease reboot your device manually by going to Reboot > System > Do Not Install")
exit(ErrorAdb)
}
exit(Success)
}