-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands_console.go
148 lines (119 loc) · 3.65 KB
/
commands_console.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
package goxbdm
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"strings"
"github.com/0xdeafcafe/go-xbdm/models"
)
// rebootType is a custom type for storing different types of reboot.
type rebootType uint8
// freezeState is a custom type for storing the different states of console freezing.
type freezeState uint8
const (
screenshotHeaderFormat = "pitch=0x%x width=0x%x height=0x%x format=0x%x offsetx=0x00000000 offsety=0x00000000, framebuffersize=0x%x"
rebootTitleToActiveTitleFormat = `magicboot title=%s directory=%s`
// RebootTitle defines the enum for rebooting to the Developer Dashboard.
RebootTitle rebootType = iota
// RebootTitleToActiveTitle defines the enum for rebooting to the currently active
// title.
RebootTitleToActiveTitle
// RebootCold defines the enum for turning the kit off and then back on.
RebootCold
// FrozenState is when all the threads are resumed as normal.
FrozenState freezeState = iota
// UnfrozenState is when all the threads are paused.
UnfrozenState
)
// Reboot the Xbox console.
func (client *Client) Reboot(rebootType rebootType) error {
switch {
case rebootType == RebootTitle:
_, err := client.SendCommand("magicboot")
return err
case rebootType == RebootTitleToActiveTitle:
info, err := client.RunningXBEInfo()
if err != nil {
return err
}
// Get directory of the currently running title
name := info.Name
titleDirectory := fmt.Sprintf(`%s"`, name[0:strings.LastIndex(name, "\\")])
// Tell Xbox which title to launch
_, err = client.SendCommand(fmt.Sprintf(rebootTitleToActiveTitleFormat, name, titleDirectory))
return err
case rebootType == RebootCold:
_, err := client.SendCommand("magicboot COLD")
return err
default:
panic("invalid reboot type")
}
}
// RunningXBEInfo gets the xbeinfo of the currently running title.
func (client *Client) RunningXBEInfo() (*models.XBEInfo, error) {
_, err := client.SendCommand("xbeinfo running")
if err != nil {
return nil, err
}
body, err := client.ReadMultilineResponse()
if err != nil {
return nil, err
}
return models.NewXBEInfo(body), nil
}
// Screenshot dumps the frame buffer of the Xbox.
func (client *Client) Screenshot() ([]byte, error) {
resp, err := client.SendCommand("screenshot")
if err != nil {
return nil, err
}
if resp != "203- binary response follows" {
return nil, errors.New(resp)
}
// Read header values
header, _ := client.tcpClient.ReadString()
var pitch, width, height, format, frameBufferSize int
fmt.Sscanf(header, screenshotHeaderFormat, &pitch, &width, &height, &format, &frameBufferSize)
buf := bytes.NewBuffer(nil)
buf.Grow(frameBufferSize)
_, err = io.CopyN(buf, client.tcpClient.Reader, int64(frameBufferSize))
if err != nil {
log.Fatal("Reading image data failed: ", err)
}
// Deswizzle this thing
data := buf.Bytes()
for i := 0; i < pitch*height; i += pitch / width {
data[i], data[i+2] = data[i+2], data[i]
}
return data, nil
}
// ChangeFreezeState set's the freeze state of the Xbox.
func (client *Client) ChangeFreezeState(freezeState freezeState) (err error) {
switch freezeState {
case FrozenState:
_, err = client.SendCommand("stop")
break
case UnfrozenState:
_, err = client.SendCommand("go")
break
}
return
}
// ConsoleFeatures gets a list of features available on the console.
func (client *Client) ConsoleFeatures() ([]string, error) {
resp, err := client.SendCommand("consolefeatures")
if err != nil {
return nil, err
}
return strings.Split(resp, " "), nil
}
// ConsoleType gets the type of console.
func (client *Client) ConsoleType() (string, error) {
resp, err := client.SendCommand("consoletype")
if err != nil {
return "", err
}
return resp, nil
}