-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcmd_run.go
65 lines (56 loc) · 1.56 KB
/
cmd_run.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
package main
import (
"bytes"
"fmt"
"os"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/freemyipod/wInd3x/pkg/app"
"github.com/freemyipod/wInd3x/pkg/dfu"
"github.com/freemyipod/wInd3x/pkg/exploit/haxeddfu"
"github.com/freemyipod/wInd3x/pkg/image"
)
var runCmd = &cobra.Command{
Use: "run [dfu image path]",
Short: "Run a DFU image on a device",
Long: "Run a DFU image (signed/encrypted or unsigned) on a connected device, starting haxed dfu mode first if necessary.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := app.New()
if err != nil {
return err
}
defer app.Close()
if err := haxeddfu.Trigger(app.Usb, app.Ep, false); err != nil {
return fmt.Errorf("failed to run wInd3x exploit: %w", err)
}
path := args[0]
glog.Infof("Uploading %s...", path)
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("Failed to read image: %w", err)
}
_, err = image.Read(bytes.NewReader(data))
switch {
case err == nil:
case err == image.ErrNotImage1:
fallthrough
case len(data) < 0x400:
glog.Infof("Given firmware file is not IMG1, packing into one...")
data, err = image.MakeUnsigned(app.Desc.Kind, 0, data)
if err != nil {
return err
}
default:
return err
}
if err := dfu.Clean(app.Usb); err != nil {
return fmt.Errorf("Failed to clean: %w", err)
}
if err := dfu.SendImage(app.Usb, data, app.Desc.Kind.DFUVersion()); err != nil {
return fmt.Errorf("Failed to send image: %w", err)
}
glog.Infof("Image sent.")
return nil
},
}