-
Notifications
You must be signed in to change notification settings - Fork 10
/
cmd_cfw.go
240 lines (210 loc) · 5.99 KB
/
cmd_cfw.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
package main
import (
"bytes"
"context"
"fmt"
"os"
"slices"
"time"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/freemyipod/wInd3x/pkg/app"
"github.com/freemyipod/wInd3x/pkg/cache"
"github.com/freemyipod/wInd3x/pkg/cfw"
"github.com/freemyipod/wInd3x/pkg/dfu"
"github.com/freemyipod/wInd3x/pkg/efi"
"github.com/freemyipod/wInd3x/pkg/exploit/haxeddfu"
"github.com/freemyipod/wInd3x/pkg/image"
)
var cfwCmd = &cobra.Command{
Use: "cfw",
Short: "Custom firmware generation (EXPERIMENTAL)",
Long: "Build custom firmware bits. Very new, very undocumented. Mostly useful for devs.",
}
type findVisitor struct {
want []string
found []*efi.FirmwareFile
}
func (v *findVisitor) Done() error {
return nil
}
func (v *findVisitor) VisitFile(file *efi.FirmwareFile) error {
for _, section := range file.Sections {
if section.Header().Type == efi.SectionTypeUserInterface {
name := string(bytes.ReplaceAll(section.Raw(), []byte{0}, []byte{}))
if slices.Contains(v.want, name) {
glog.Infof("Found %s", name)
v.found = append(v.found, file)
}
}
}
return nil
}
func (v *findVisitor) VisitSection(section efi.Section) error {
return nil
}
func superdiags(app *app.App) ([]byte, error) {
diagb, err := cache.Get(app, cache.PayloadKindDiagsDecrypted)
if err != nil {
return nil, fmt.Errorf("when getting diags: %w", err)
}
diagi, err := image.Read(bytes.NewReader(diagb))
if err != nil {
return nil, fmt.Errorf("when reading diags: %w", err)
}
diag, err := efi.ReadVolume(efi.NewNestedReader(diagi.Body))
if err != nil {
return nil, fmt.Errorf("when reading diags fv: %w", err)
}
bootb, err := cache.Get(app, cache.PayloadKindBootloaderDecrypted)
if err != nil {
return nil, fmt.Errorf("when getting bootloader: %w", err)
}
booti, err := image.Read(bytes.NewReader(bootb))
if err != nil {
return nil, fmt.Errorf("when reading bootloader: %w", err)
}
boot, err := efi.ReadVolume(efi.NewNestedReader(booti.Body))
if err != nil {
return nil, fmt.Errorf("when reading bootloader fv: %w", err)
}
fv := &findVisitor{
want: []string{
"DiskIoDxe",
"Partition",
"Image1FSReadOnly",
"Nand",
},
}
if err := cfw.VisitVolume(boot, fv); err != nil {
return nil, fmt.Errorf("when visiting bootloader: %w", err)
}
if want, got := len(fv.want), len(fv.found); want != got {
return nil, fmt.Errorf("did not find all requested modules (wanted %v, got %d)", fv.want, len(fv.found))
}
glog.Infof("Before: %d files", len(diag.Files))
diag.Files = append(diag.Files, fv.found...)
glog.Infof("After: %d files", len(diag.Files))
diagb, err = diag.Serialize()
if err != nil {
return nil, fmt.Errorf("could not serialize superdiags fv: %w", err)
}
diagbi, err := image.MakeUnsigned(diagi.DeviceKind, diagi.Header.Entrypoint, diagb)
if err != nil {
return nil, fmt.Errorf("could not make superdiags image: %w", err)
}
return diagbi, nil
}
var cfwSuperdiagsCmd = &cobra.Command{
Use: "superdiags",
Short: "Run superdiags",
Long: "Run superdiags (diag with extra Nand driver). If your iPod has a connected DCSD cable, you'll be able to access a console over it.",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := app.New()
if err != nil {
return err
}
defer app.Close()
diags, err := superdiags(app)
if err != nil {
return err
}
wtf, err := cache.Get(app, cache.PayloadKindWTFDefanged)
if err != nil {
return err
}
if err := haxeddfu.Trigger(app.Usb, app.Ep, false); err != nil {
return fmt.Errorf("failed to run wInd3x exploit: %w", err)
}
glog.Infof("Sending defanged WTF...")
if err := dfu.SendImage(app.Usb, wtf, app.Desc.Kind.DFUVersion()); err != nil {
return fmt.Errorf("failed to send image: %w", err)
}
glog.Infof("Waiting 10s for device to switch to WTF mode...")
ctx, ctxC := context.WithTimeout(cmd.Context(), 10*time.Second)
defer ctxC()
if err := app.WaitWTF(ctx); err != nil {
return fmt.Errorf("device did not switch to WTF mode: %w", err)
}
time.Sleep(time.Second)
glog.Infof("Sending diags...")
for i := 0; i < 10; i++ {
err = dfu.SendImage(app.Usb, diags, app.Desc.Kind.DFUVersion())
if err == nil {
break
} else {
glog.Errorf("%v", err)
time.Sleep(time.Second)
}
}
if err != nil {
return err
}
glog.Infof("Done.")
return nil
},
}
var cfwRunCmd = &cobra.Command{
Use: "run [firmware]",
Short: "Run CFW",
Long: "Run CFW based on modified WTF and firmware (eg. modified OSOS or u-boot)",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
app, err := app.New()
if err != nil {
return err
}
defer app.Close()
fwb, err := os.ReadFile(args[0])
if err != nil {
return err
}
wtf, err := cache.Get(app, cache.PayloadKindWTFDefanged)
if err != nil {
return err
}
if err := haxeddfu.Trigger(app.Usb, app.Ep, false); err != nil {
return fmt.Errorf("failed to run wInd3x exploit: %w", err)
}
glog.Infof("Sending defanged WTF...")
if err := dfu.SendImage(app.Usb, wtf, app.Desc.Kind.DFUVersion()); err != nil {
return fmt.Errorf("failed to send image: %w", err)
}
_, err = image.Read(bytes.NewReader(fwb))
switch {
case err == nil:
case err == image.ErrNotImage1:
fallthrough
case len(fwb) < 0x400:
glog.Infof("Given firmware file is not IMG1, packing into one...")
fwb, err = image.MakeUnsigned(app.Desc.Kind, 0, fwb)
if err != nil {
return err
}
default:
return err
}
glog.Infof("Waiting 10s for device to switch to WTF mode...")
ctx, ctxC := context.WithTimeout(cmd.Context(), 10*time.Second)
defer ctxC()
if err := app.WaitWTF(ctx); err != nil {
return fmt.Errorf("device did not switch to WTF mode: %w", err)
}
time.Sleep(time.Second)
glog.Infof("Sending firmware...")
for i := 0; i < 10; i++ {
err = dfu.SendImage(app.Usb, fwb, app.Desc.Kind.DFUVersion())
if err == nil {
break
} else {
glog.Errorf("%v", err)
time.Sleep(time.Second)
}
}
if err != nil {
return err
}
glog.Infof("Done.")
return nil
},
}