forked from zhangzhichaolove/AndroidBootReplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplaceBoot.go
138 lines (129 loc) · 3.37 KB
/
replaceBoot.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
//go:generate go-bindata -fs -pkg=asset -prefix "tool/" tool/...
////go:generate go-bindata -o=./asset/asset.go -pkg=asset tool/...
////go:generate go-bindata -fs -prefix "static/" tool/...
//go:generate go-bindata -version
package main
import (
"bufio"
"fmt"
//asset "github.com/zhangzhichaolove/AndroidBootReplace/type"
"github.com/zhangzhichaolove/AndroidBootReplace/asset"
"io"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
)
var imgName = "new.img"
//go get github.com/zhangzhichaolove/go-bindata/v2/...
//go install github.com/zhangzhichaolove/go-bindata/v2/...@latest
//go install -a -v github.com/zhangzhichaolove/go-bindata/v2/...@latest
func main() {
restore()
if len(os.Args) == 2 {
imgName = os.Args[1]
} else {
fmt.Println("默认会将同级目录下new.img替换到手机,你也可以手动指定该镜像名称,例如:./replaceBoot magisk_patched.img")
}
_, e := os.Stat(imgName)
if e != nil {
fmt.Println("镜像文件不存在,刷机终止。")
return
}
isConnect := make(chan bool)
go func() {
http.Handle("/", http.FileServer(asset.AssetFile()))
http.ListenAndServe(":168", nil)
}()
go func() {
for {
select {
case <-isConnect:
return
default:
fmt.Println("请让手机进入fastboot模式,并连接数据线...")
}
time.Sleep(2 * time.Second)
}
}()
execRealTimeCommand("tool/fastboot --version")
execCommand("tool/fastboot wait-for-device", isConnect)
execRealTimeCommand(fmt.Sprintf("tool/fastboot flash init_boot %s", imgName))
execRealTimeCommand("tool/fastboot reboot")
//execRealTimeCommand("ping www.baidu.com")
}
func restore() {
if err := asset.RestoreAssets("tool", ""); err != nil {
fmt.Println("文件释放失败:", err.Error())
}
}
func execCommand(strCommand string, connect chan bool) string {
cmd := exec.Command("/bin/bash", "-c", strCommand)
output, err := cmd.CombinedOutput()
if err != nil {
if strings.Contains(string(output), "fastboot: usage: unknown command wait-for-device") {
fmt.Println("手机连接成功,进入刷机模式...")
connect <- true
} else {
fmt.Println("命令运行错误-->", string(output), err)
}
return ""
}
fmt.Println(string(output))
return string(output)
}
//func execCommand(strCommand string) string {
// cmd := exec.Command("/bin/bash", "-c", strCommand)
// fmt.Println("执行命令:", cmd.Args)
// stdout, err := cmd.StdoutPipe()
// if err != nil {
// fmt.Println("命令运行错误-->", err)
// return ""
// }
// cmd.Start()
// outText := ""
// reader := bufio.NewReader(stdout)
// //实时循环读取输出流中的一行内容
// for {
// line, err2 := reader.ReadString('\n')
// if err2 != nil || io.EOF == err2 {
// break
// }
// outText += line
// fmt.Println(line)
// }
// cmd.Wait()
// return outText
//}
func read(wg *sync.WaitGroup, std io.ReadCloser) {
defer wg.Done()
reader := bufio.NewReader(std)
for {
readString, err := reader.ReadString('\n')
if err != nil || err == io.EOF {
return
}
fmt.Print(readString)
}
}
func execRealTimeCommand(cmd string) error {
//c := exec.Command("cmd", "/C", cmd) // windows
c := exec.Command("bash", "-c", cmd) // mac or linux
stdout, err := c.StdoutPipe()
if err != nil {
return err
}
stderr, err := c.StderrPipe()
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(2)
go read(&wg, stdout)
go read(&wg, stderr)
err = c.Start()
wg.Wait()
return err
}