-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
219 lines (193 loc) · 6.25 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/user"
"strings"
manager "github.com/kercre123/wire-os/pkg/download-manager"
handler "github.com/kercre123/wire-os/pkg/ota-handler"
patcher "github.com/kercre123/wire-os/pkg/ota-patcher"
"github.com/kercre123/wire-os/pkg/vars"
)
func isRoot() bool {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("[isRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root"
}
func startWeb() {
fmt.Println("Starting OTA fileserver at port 8080")
http.Handle("/", http.FileServer(http.Dir("./out")))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalf("Failed to start web server: %v", err)
}
}
func main() {
if !isRoot() {
fmt.Println("This program must be run as root, as it mounts filesystems.")
os.Exit(1)
}
vars.InitDownloadManager()
downloadCmd := flag.NewFlagSet("download", flag.ExitOnError)
downloadURL := downloadCmd.String("url", "", "URL to download the OTA from")
mountCmd := flag.NewFlagSet("mount", flag.ExitOnError)
mountOTA := mountCmd.String("ota", "", "OTA version to mount")
unmountCmd := flag.NewFlagSet("unmount", flag.ExitOnError)
packCmd := flag.NewFlagSet("pack", flag.ExitOnError)
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
deleteOTA := deleteCmd.String("ota", "", "OTA version to delete")
patchCmd := flag.NewFlagSet("patch", flag.ExitOnError)
patchOutput := patchCmd.String("output", "", "Desired output version (e.g., 1.6.0.3331.ota)")
patchTarget := patchCmd.Int("target", -1, "Desired target (0-3)")
listCmd := flag.NewFlagSet("list", flag.ExitOnError)
startWebCmd := flag.NewFlagSet("startweb", flag.ExitOnError)
helpCmd := flag.NewFlagSet("help", flag.ExitOnError)
if len(os.Args) < 2 {
fmt.Println("Expected 'download', 'mount', 'unmount', 'pack', 'delete', 'patch', 'list', 'startweb' or 'help' subcommands.")
os.Exit(1)
}
switch os.Args[1] {
case "download":
downloadCmd.Parse(os.Args[2:])
if *downloadURL == "" {
fmt.Println("Usage: download -url=<URL>")
os.Exit(1)
}
manager.Download(*downloadURL)
case "mount":
mountCmd.Parse(os.Args[2:])
if *mountOTA == "" {
fmt.Println("Usage: mount -ota=<OTA version>")
os.Exit(1)
}
version, err := manager.SplitVersion(*mountOTA)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
err = handler.MountOTA(version)
if err != nil {
fmt.Printf("Error mounting OTA: %v\n", err)
os.Exit(1)
}
fmt.Println("OTA mounted successfully.")
case "unmount":
unmountCmd.Parse(os.Args[2:])
fmt.Println("This will delete the image as well.")
err := handler.UnmountImage("./work")
if err != nil {
fmt.Printf("Error unmounting OTA: %v\n", err)
os.Exit(1)
}
err = os.Remove("./tmp/apq8009-robot-sysfs.img")
if err != nil {
fmt.Printf("Error removing image: %v\n", err)
os.Exit(1)
}
fmt.Println("OTA unmounted and image deleted successfully.")
case "pack":
packCmd.Parse(os.Args[2:])
err := handler.PackOTA()
if err != nil {
fmt.Printf("Error packing OTA: %v\n", err)
os.Exit(1)
}
fmt.Println("OTA packed successfully.")
case "delete":
deleteCmd.Parse(os.Args[2:])
if *deleteOTA == "" {
fmt.Println("Usage: delete -ota=<OTA version>")
os.Exit(1)
}
version, err := manager.SplitVersion(*deleteOTA)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
err = manager.Delete(version)
if err != nil {
fmt.Printf("Error deleting OTA: %v\n", err)
os.Exit(1)
}
fmt.Println("OTA deleted successfully.")
case "patch":
patchCmd.Parse(os.Args[2:])
if *patchOutput == "" || *patchTarget == -1 {
fmt.Println("Usage: patch -output=<output OTA version> -target=<0-4>")
os.Exit(1)
}
version, err := manager.SplitVersion(*patchOutput)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
if *patchTarget < 0 || *patchTarget > 4 {
fmt.Println("Error: target must be between 0 and 4")
os.Exit(1)
}
fmt.Println("Running patches...")
err = patcher.RunPatches(version, *patchTarget)
if err != nil {
fmt.Printf("Error applying patches: %v\n", err)
os.Exit(1)
}
fmt.Println("Patches applied successfully.")
case "list":
listCmd.Parse(os.Args[2:])
var downloaded []string
var output []string
storeEntries, err := os.ReadDir("./store")
if err != nil {
fmt.Printf("Error reading store directory: %v\n", err)
os.Exit(1)
}
for _, entry := range storeEntries {
if strings.Contains(entry.Name(), ".ota") {
downloaded = append(downloaded, strings.TrimSuffix(entry.Name(), ".ota"))
}
}
outputEntries, err := os.ReadDir("./out")
if err != nil {
fmt.Printf("Error reading out directory: %v\n", err)
os.Exit(1)
}
for _, entry := range outputEntries {
if strings.Contains(entry.Name(), ".ota") {
output = append(output, entry.Name())
}
}
fmt.Println("Downloaded OTAs: " + fmt.Sprint(downloaded))
fmt.Println("Modified OTAs (accessible via webserver): " + fmt.Sprint(output))
case "startweb":
startWebCmd.Parse(os.Args[2:])
startWeb()
case "help":
helpCmd.Parse(os.Args[2:])
fmt.Println("Available subcommands:")
fmt.Println(" download -url=<URL> Download an OTA from the specified URL.")
fmt.Println(" mount -ota=<OTA version> Mount a downloaded OTA.")
fmt.Println(" unmount Unmount the currently mounted OTA and delete the image.")
fmt.Println(" pack Pack the currently mounted OTA.")
fmt.Println(" delete -ota=<OTA version> Delete a specified OTA version.")
fmt.Println(" patch -output=<OTA> -target=<0-3> Apply patches to the specified OTA with the given target.")
fmt.Println(" list List downloaded and modified OTAs.")
fmt.Println(" startweb Start the OTA fileserver on port 8080.")
fmt.Println(" help Show this help message.")
fmt.Println("\nOTA format example: 1.6.0.3331")
fmt.Println("Targets:")
fmt.Println(" 0 = dev")
fmt.Println(" 1 = whiskey")
fmt.Println(" 2 = oskr")
fmt.Println(" 3 = orange")
fmt.Println(" 4 = dvt3")
default:
fmt.Printf("Unknown subcommand: %s\n", os.Args[1])
fmt.Println("Available subcommands: download, mount, unmount, pack, delete, patch, list, startweb, help")
os.Exit(1)
}
}