-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandlers
523 lines (507 loc) · 14.3 KB
/
handlers
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// That means we have args in the usual places, and all is the same *save* that we will
// implement a function, not shuffle data around.
// We first need to see if it's a call that got us here.
// So if the inst() fails, we'll need to look at (rsp) and get the inst from there.
// For now, we're gonna hack it out. If the failing addr is in the range
// of funcs we assume function call.
/* if addr >= StartFuncs && addr < EndFuncs {
// Assume it's a call. We can switch on the addr. We're going to want to pop the
// stack when done.
op := addr & 0xffff
log.Printf("functions: %v(%#x), arg type %T, args %v", table.RuntimeServicesNames[op], op, inst.Args, inst.Args)
switch op {
case table.STOutputString:
args := args(p, &r, 6)
log.Printf("StOutputString args %#x", args)
r.Rax = EFI_SUCCESS
if err := p.SetRegs(r); err != nil {
return err
}
return nil
default:
log.Printf("conout op opcode %#x addr %v: unknonw opcode", op, addr)
r.Rax = EFI_SUCCESS
if err := p.SetRegs(r); err != nil {
return err
}
return nil
}
}
}*/
log.Printf("Segv: addr %#x: %s", addr, showone("\t", r))
if pc < 0x200000 {
var err error
// Just grab them all
args := args(p, r, 6)
l := fmt.Sprintf("%#x, %s[", addr, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%v,", a)
}
l += "]"
op := addr & 0xfff
// This is a mess: the eip is actually at the instruction
// after the call. dammit. variable length instructions.
// likely a 5 byte call but ... dammit.
switch addr & ^uint64(0xffff) {
case STOut:
switch op {
case table.STOutOutputString:
log.Printf("StOutputString args %#x", args)
ptr := args[1]
// it's an f'ing struct. The first element is This. Skip it.
str, err := p.ReadWord(uintptr(ptr + 8))
if err != nil {
return fmt.Errorf("STOutputString: Reading char * at %#x failed", ptr+8)
}
n, err := p.ReadStupidString(uintptr(str))
if err != nil {
err = fmt.Errorf("Can't read StupidString at #%x, err %v", str, err)
}
fmt.Printf("%s\n", n)
r.Rax = EFI_SUCCESS
if err := p.SetRegs(r); err != nil {
return err
}
}
default:
err = fmt.Errorf("Can't handle dest %v", inst.Args[0])
}
// pop the stack, then set the EIP, then setregs.
r.Rip, err = pop(p, r)
if err != nil {
return err
}
if err := p.SetRegs(r); err != nil {
return err
}
return err
}
if (addr >= ImageHandle) && (addr <= ImageHandleEnd) {
l := fmt.Sprintf("%#x, %s[", pc, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%s,", a.String())
}
l += "]"
return fmt.Errorf("ImageHandle error, %v", l)
}
if (addr >= SystemTable) && (addr <= SystemTableEnd) {
l := fmt.Sprintf("%#x, %s[", pc, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%v,", a)
}
l += "]"
op := addr & 0xffff
n, ok := table.SystemTableNames[op]
if !ok {
return fmt.Errorf("No system table entry for offset %#x: %s\n", op, l)
}
log.Printf("System table: %#x, %s", op, n.N)
// code expects to return a value of a thing, or call that thing.
// So consistent.
switch inst.Args[0] {
case x86asm.RCX:
r.Rcx = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RDX:
r.Rdx = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RAX:
r.Rax = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.R8:
r.R8 = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
default:
return fmt.Errorf("Can't handle dest %v", inst.Args[0])
}
}
if (addr >= LoadedImage) && (addr <= LoadedImage+0x10000) {
l := fmt.Sprintf("%#x, %s[", pc, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%v,", a)
}
l += "]"
op := addr & 0xffff
n, ok := table.LoadedImageTableNames[op]
if !ok {
return fmt.Errorf("No loaded image entry for offset %#x: %s\n", op, l)
}
log.Printf("loaded image table: %#x, %s", op, n.N)
switch inst.Args[0] {
case x86asm.EDX:
r.Rdx = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RDX:
r.Rdx = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RCX:
r.Rcx = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RAX:
r.Rax = n.Val
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
default:
return fmt.Errorf("Can't handle dest %v", inst.Args[0])
}
}
if (addr >= Boot) && (addr <= Boot+0x10000) {
// No matter what happpens, move to the next one.
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
op := addr & 0xffff
log.Printf("Boot services: %s(%#x), arg type %T, args %v", bootServicesNames[int(op)], op, inst.Args, inst.Args)
switch op {
case AllocatePool:
// Status = gBS->AllocatePool (EfiBootServicesData, sizeof (EXAMPLE_DEVICE), (VOID **)&Device);
args := args(p, r, 3)
// ignore arg 0 for now.
log.Printf("AllocatePool: %d bytes", args[1])
var bb [8]byte
binary.LittleEndian.PutUint64(bb[:], uint64(dat))
if err := p.Write(args[2], bb[:]); err != nil {
return fmt.Errorf("Can't write %d bytes to %#x: %v", len(bb), dat, err)
}
dat += args[1]
return nil
case FreePool:
// Status = gBS->FreePool (Device);
args := args(p, r, 1)
// Free? Forget it.
log.Printf("FreePool: %#x", args[0])
return nil
case LocateHandle:
// EFI_STATUS LocateHandle (IN EFI_LOCATE_SEARCH_TYPE SearchType, IN EFI_GUID *Protocol OPTIONAL, IN VOID *SearchKey OPTIONAL,IN OUT UINTN *NoHandles, OUT EFI_HANDLE **Buffer);
args := args(p, r, 5)
no := args[3]
var bb [8]byte
// just fail.
if err := p.Write(no, bb[:]); err != nil {
return fmt.Errorf("Can't write %d bytes to %#x: %v", len(bb), dat, err)
}
return nil
case HandleProtocol:
// There. All on one line. Not 7. So, UEFI, did that really hurt so much?
// typedef EFI_STATUS (EFIAPI *EFI_HANDLE_PROTOCOL) (IN EFI_HANDLE Handle, IN EFI_GUID *Protocol, OUT VOID **Interface);
// The arguments are rcx, rdx, r9
args := args(p, r, 3)
var g guid.GUID
if err := p.Read(args[1], g[:]); err != nil {
return fmt.Errorf("Can't read guid at #%x, err %v", args[1], err)
}
log.Printf("HandleProtocol: GUID %s", g)
if err := Srv(p, &g, args...); err != nil {
return fmt.Errorf("Can't handle HandleProtocol: %s: %v", callinfo(i, inst, r), err)
}
return nil
case PCHandleProtocol:
// There. All on one line. Not 7. So, UEFI, did that really hurt so much?
// typedef EFI_STATUS (EFIAPI *EFI_HANDLE_PROTOCOL) (IN EFI_HANDLE Handle, IN EFI_GUID *Protocol, OUT VOID **Interface);
// The arguments are rcx, rdx, r9
args := args(p, r, 3)
var g guid.GUID
if err := p.Read(args[1], g[:]); err != nil {
return fmt.Errorf("Can't read guid at #%x, err %v", args[1], err)
}
log.Printf("PCHandleProtocol: GUID %s", g)
if err := Srv(p, &g, args...); err != nil {
return fmt.Errorf("Can't handle HandleProtocol: %s: %v", callinfo(i, inst, r), err)
}
return nil
case ConnectController:
// The arguments are rcx, rdx, r9, r8
args := args(p, r, 4)
log.Printf("ConnectController: %#x", args)
// Just pretend it worked.
return nil
case WaitForEvent:
args := args(p, r, 3)
log.Printf("WaitForEvent: %#x", args)
// Just pretend it worked.
return nil
case 0xfffe:
arg0, err := GetReg(r, x86asm.RDX)
if err != nil {
return fmt.Errorf("Can't get RDX: %v", err)
}
any("go get it")
// idiot UEFI and idiot wchar_t
arg := uintptr(*arg0)
for {
var dat [2]byte
if err := p.Read(arg, dat[:]); err != nil {
return fmt.Errorf("Can't read data at #%x, err %v", addr, err)
}
if dat[0] == 0 && dat[1] == 0 {
break
}
fmt.Printf("%#02x %c,", dat, dat[0])
arg++
}
// This code is right for getting an arbitrary Mem out.
// It's wrong for 0xf8, whoops. Leave it here
// for reference.
case 0xffff:
m := inst.Args[0].(x86asm.Mem)
// A Mem is a memory reference.
// The general form is Segment:[Base+Scale*Index+Disp].
/*
type Mem struct {
Segment Reg
Base Reg
Scale uint8
Index Reg
Disp int64
}
*/
log.Printf("ARG[0] %q m is %#x", inst.Args[0], m)
b, err := GetReg(r, m.Base)
if err != nil {
any("FUCKED BASE")
return fmt.Errorf("Can't get Base reg %v in %v", m.Base, m)
}
addr := *b + uint64(m.Disp)
x, err := GetReg(r, m.Index)
if err == nil {
addr += uint64(m.Scale) * (*x)
}
//if v, ok := inst.Args[0].(*x86asm.Mem); ok {
log.Printf("computed addr is %#x", addr)
any("go get it")
var dat [16]byte
if err := p.Read(uintptr(addr), dat[:]); err != nil {
return fmt.Errorf("Can't read data at #%x, err %v", addr, err)
}
log.Printf("dat at %#x is %#x", addr, dat)
//return nil
//}
//return fmt.Errorf("Wrong type of 0xf8? %T but should be %T", inst.Args[0], x86asm.Mem)
return nil
default:
return fmt.Errorf("opcode %#x addr %v: unknonw opcode", op, addr)
}
}
if (addr >= Runtime) && (addr <= Runtime+0x10000) {
// No matter what happpens, move to the next one.
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
op := addr & 0xffff
log.Printf("Runtime services: %v(%#x), arg type %T, args %v", table.RuntimeServicesNames[op], op, inst.Args, inst.Args)
switch op {
case table.RTGetVariable:
args := args(p, r, 5)
log.Printf("table.RTGetVariable args %#x", args)
ptr := args[0]
n, err := p.ReadStupidString(ptr)
if err != nil {
return fmt.Errorf("Can't read StupidString at #%x, err %v", ptr, err)
}
var g guid.GUID
if err := p.Read(args[1], g[:]); err != nil {
return fmt.Errorf("Can't read guid at #%x, err %v", args[1], err)
}
log.Printf("PCHandleProtocol: find %s %s", n, g)
v, err := ReadVariable(n, g)
if err != nil {
r.Rax = EFI_NOT_FOUND
if err := p.SetRegs(r); err != nil {
return err
}
}
log.Printf("%s:%s: v is %v", n, g, v)
r.Rax = EFI_SUCCESS
return nil
default:
return fmt.Errorf("opcode %#x addr %v: unknonw opcode", op, addr)
}
}
// ConOut points to a simple text output interface. We got here from the
// System Table.
// We got here because table.SystemTableNames[table.ConOut].Val = ConOut
if (addr >= ConOut) && (addr <= ConOut+0x10000) {
l := fmt.Sprintf("%#x, %s[", pc, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%v,", a)
}
l += "]"
op := addr & 0xfff
// pretend it's a deref
var n uint64
// This should then point to a function, which we will call.
if op <= table.STOutMode {
n = STOut + op
}
log.Printf("ConOut table: %#x, %#x", op, n)
// code expects to return a value of a thing, or call that thing.
// So consistent.
switch inst.Args[0] {
case x86asm.RCX:
r.Rcx = n
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RAX:
r.Rax = n
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.R8:
r.R8 = n
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
default:
return fmt.Errorf("ConOut Can't handle dest %v", inst.Args[0])
}
}
if (addr >= ConIn) && (addr <= ConIn+0x10000) {
l := fmt.Sprintf("%#x, %s[", pc, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%v,", a)
}
l += "]"
op := addr & 0xfff
// pretend it's a deref
var n uint64
log.Printf("ConIn table: %#x, %#x", op, n)
n = STIn + op
// code expects to return a value of a thing, or call that thing.
// So consistent.
switch inst.Args[0] {
case x86asm.RCX:
r.Rcx = n
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RAX:
r.Rax = n
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.R8:
r.R8 = n
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
default:
return fmt.Errorf("ConOut Can't handle dest %v", inst.Args[0])
}
}
// like i give a shit about their stupid console
// This is setting variables 'n shit. Not functions.
if (addr >= STOut+table.STOutMode) && (addr < STOut+table.STOutMode+0x1000) {
l := fmt.Sprintf("%#x, %s[", pc, InfoString(i))
for _, a := range inst.Args {
l += fmt.Sprintf("%v,", a)
}
l += "]"
// code expects to return a value of a thing, or call that thing.
// So consistent.
switch inst.Args[0] {
case x86asm.RCX:
r.Rcx = 0
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RDX:
r.Rdx = 0
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.RAX:
r.Rax = 0
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
case x86asm.R8:
r.R8 = 0
r.Rip += uint64(inst.Len)
if err := p.SetRegs(r); err != nil {
return err
}
return nil
default:
return fmt.Errorf("ConOut Can't handle dest %v", inst.Args[0])
}
}
case protocol.LoadedImageProtocol:
if len(args) < 3 {
return fmt.Errorf("protocol.LoadedImageProtocol needs 3 args, got %d", len(args))
}
// For now, we're not putting this out there.
// We will handle access via segv.
odat := dat
if false {
i, err := protocol.NewLoadedImage()
if err != nil {
return err
}
b, err := i.Marshal()
if err != nil {
return fmt.Errorf("Can't serialize %T: %v", i, err)
}
if err := p.Write(dat, b); err != nil {
return fmt.Errorf("Can't write %d bytes to %#x: %v", len(b), dat, err)
}
dat += uintptr(len(b))
// Store the return pointer through arg3.
}
var bb [8]byte
binary.LittleEndian.PutUint64(bb[:], uint64(LoadedImage))
if err := p.Write(args[2], bb[:]); err != nil {
return fmt.Errorf("Can't write %d bytes to %#x: %v", len(bb), odat, err)
}
return nil