-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmd_nor_read.go
104 lines (93 loc) · 2.56 KB
/
cmd_nor_read.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
package main
import (
"fmt"
"io"
"os"
"github.com/freemyipod/wInd3x/pkg/app"
"github.com/freemyipod/wInd3x/pkg/dfu"
"github.com/freemyipod/wInd3x/pkg/exploit"
"github.com/freemyipod/wInd3x/pkg/uasm"
"github.com/golang/glog"
"github.com/spf13/cobra"
)
var norCmd = &cobra.Command{
Use: "nor",
Short: "NOR Flash access (EXPERIMENTAL)",
Long: "Manipulate SPI NOR Flash on the device. Currently this is EXPERIMENTAL, as the SPI NOR access methods are not well reverse engineered.",
}
func readNOR(app *app.App, w io.Writer, spino, offset, size uint32) error {
ep := app.Ep
usb := app.Usb
listing := ep.DisableICache()
payload, err := ep.NORInit(spino)
if err != nil {
return err
}
listing = append(listing, payload...)
listing = append(listing, ep.HandlerFooter(0x20000000)...)
init := uasm.Program{
Address: ep.ExecAddr(),
Listing: listing,
}
if err := dfu.Clean(app.Usb); err != nil {
return fmt.Errorf("clean failed: %w", err)
}
if _, err := exploit.RCE(usb, ep, init.Assemble(), nil); err != nil {
return fmt.Errorf("failed to execute init payload: %w", err)
}
for i := uint32(0); i < size; i += 0x40 {
listing, dataAddr := ep.NORRead(spino, offset+i)
listing = append(listing, ep.HandlerFooter(dataAddr)...)
read := uasm.Program{
Address: ep.ExecAddr(),
Listing: listing,
}
if err := dfu.Clean(app.Usb); err != nil {
return fmt.Errorf("clean failed: %w", err)
}
data, err := exploit.RCE(usb, ep, read.Assemble(), nil)
if err != nil {
return fmt.Errorf("failed to execute read payload: %w", err)
}
if _, err := w.Write(data); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
}
return nil
}
var norReadCmd = &cobra.Command{
Use: "read [spino] [address] [count] [file]",
Short: "Read NOR flash",
Long: "Read N bytes from an address from given SPI peripheral.",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := app.New()
if err != nil {
return err
}
defer app.Close()
spino, err := parseNumber(args[0])
if err != nil {
return fmt.Errorf("invalid spi peripheral number")
}
address, err := parseNumber(args[1])
if err != nil {
return fmt.Errorf("invalid address")
}
count, err := parseNumber(args[2])
if err != nil {
return fmt.Errorf("invalid count")
}
f, err := os.Create(args[3])
if err != nil {
return err
}
glog.Infof("Reading NOR address 0x%08x... (SPI %d, %d bytes)", address, spino, count)
err = readNOR(app, f, spino, address, count)
if err != nil {
return err
}
glog.Infof("Done")
return nil
},
}