Skip to content

Commit

Permalink
Add new modes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYadro committed May 5, 2021
1 parent 4a8807c commit bcb4413
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 83 deletions.
4 changes: 2 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ func isVritualCommand(cmd string) bool {
return false
}

func (sc syscon) proccessCommand(cmd string) string {
func (sc syscon) proccessCommand(cmd string) (string, error) {
cmd = strings.TrimSpace(cmd)
if isVritualCommand(cmd) {
return sc.proccessVirtualCommand(cmd)
return sc.proccessVirtualCommand(cmd), nil // TODO: Error handling
} else {
sc.sendCommand(cmd)
time.Sleep(sc.getRespTime(cmd) * time.Second)
Expand Down
76 changes: 40 additions & 36 deletions modecxr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"errors"
"fmt"
"log"
"strconv"
"strings"
)

func (sc syscon) sendCXRCommand(cmd string) {
Expand All @@ -14,50 +17,51 @@ func (sc syscon) sendCXRCommand(cmd string) {
for i := 0; i < length; i += maxSize {
j += maxSize
if j > length {
// j = length
break // And don'r send extra
j = length
}
fmt.Println(fcmd[i:j])
sc.writeCommand(fcmd[i:j])
}
sc.writeCommand("\r\n")
}

func (sc syscon) receiveCXRCommand() string {
// buff := make([]byte, 1000)
// n, err := sc.port.Read(buff)
// fmt.Printf("Read %v bytes\n", n)
// if err != nil {
// log.Fatal(err)
// }

// fmt.Println(string(buff[:n]))

// // test := strings.SplitAfterN(string(buff[:n]), "\n", 2)
// reg, err := regexp.Compile("[^a-zA-Z0-9:]+")
// if err != nil {
// log.Fatal(err)
// }
// processedString := reg.ReplaceAllString(string(buff[:n]), "")
// fmt.Println(processedString)
// return processedString
func (sc syscon) receiveCXRCommand() (string, error) {
buff := make([]byte, 1000)
for {
n, err := sc.port.Read(buff)
fmt.Printf("Read %v bytes\n", n)
if err != nil {
log.Fatal(err)
}
if n == 0 {
fmt.Println("\nEOF")
break
n, err := sc.port.Read(buff)
fmt.Printf("Read %v bytes\n", n)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buff[:n]))
respRaw := strings.TrimSpace(string(buff[:n]))
resp := strings.Split(respRaw, ":")
if len(resp) != 3 {
fmt.Println("wrong response length")
return "", errors.New("wrong response length")
}
if resp[0] != "R" && resp[0] != "E" {
fmt.Println("magic?")
return "", errors.New("magic?")
}
if resp[1] != fmt.Sprintf("%02X", countChecksum(resp[2])) {
fmt.Println("wrong checksum")
return "", errors.New("wrong chechsum")
}
respData := strings.Split(resp[2], " ")
if resp[0] == "R" && len(respData) < 2 || resp[0] == "E" && len(respData) != 2 {
fmt.Println("wrong data length")
return "", errors.New("wrong data length")
}
if respData[0] != "OK" || len(respData) < 2 {
respCode, err := strconv.Atoi(respData[1])
if err == nil && respCode == 0 {
return respData[2], nil
}

// if strings.Contains(string(buff[:n]), "\r\n") {
// test := strings.SplitAfterN(string(buff[:n]), "\n", 2)
fmt.Println(string(buff[:n]))
// return strings.TrimSpace(test[1])
// }
return "", errors.New("wrong response code")
}
respCode, err := strconv.Atoi(respData[1])
if err == nil && respCode == 0 {
return respData[2], nil
}
return ""
return "", errors.New("wrong response code")
}
9 changes: 5 additions & 4 deletions modecxrf.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"log"
"strings"
Expand All @@ -10,7 +11,7 @@ func (sc syscon) sendCXRFCommand(cmd string) {
sc.writeCommand(cmd + "\r\n")
}

func (sc syscon) receiveCXRFCommand() string {
func (sc syscon) receiveCXRFCommand() (string, error) {
buff := make([]byte, 1000)
for {
n, err := sc.port.Read(buff)
Expand All @@ -24,9 +25,9 @@ func (sc syscon) receiveCXRFCommand() string {
}

if strings.Contains(string(buff[:n]), "\r\n") {
test := strings.SplitAfterN(string(buff[:n]), "\n", 2)
return strings.TrimSpace(test[1])
resp := strings.SplitAfterN(string(buff[:n]), "\n", 2)
return strings.TrimSpace(resp[1]), nil
}
}
return ""
return "", errors.New("wrong response")
}
9 changes: 6 additions & 3 deletions modesw.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "fmt"
import (
"errors"
"fmt"
)

func (sc syscon) sendSWCommand(cmd string) {
// SendCmdLong ??????
Expand All @@ -9,6 +12,6 @@ func (sc syscon) sendSWCommand(cmd string) {
sc.writeCommand(fcmd)
}

func (sc syscon) receiveSWCommand() string {
return "not impl"
func (sc syscon) receiveSWCommand() (string, error) {
return "not impl", errors.New("not impl")
}
11 changes: 5 additions & 6 deletions ps3syscon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ type syscon struct {
mode string
}

type test struct {
mode string
}

func init() {
flag.StringVar(&portName, "port", "/dev/tty.SLAB_USBtoUART", "port to use")
flag.StringVar(&portName, "port", "/dev/tty.usbserial-145410", "port to use")
flag.StringVar(&sysconMode, "mode", "CXRF", "syscon mode")
flag.Parse()
sysconMode = strings.ToLower(sysconMode)
Expand All @@ -42,7 +38,10 @@ func main() {
if err != nil {
log.Fatal(err)
}
resp := sc.proccessCommand(cmd)
resp, err := sc.proccessCommand(cmd)
if err != nil {
log.Print(err)
}
fmt.Println(resp)
}
}
6 changes: 3 additions & 3 deletions syscon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"log"

Expand All @@ -21,7 +22,6 @@ func newSyscon(pName, sMode string) syscon {
mode = &serial.Mode{
BaudRate: 57600,
}
fmt.Println(mode)
}
}
port, err := serial.Open(pName, mode)
Expand Down Expand Up @@ -50,7 +50,7 @@ func (sc syscon) writeCommand(cmd string) {
fmt.Printf("Sent %v bytes\n", n)
}

func (sc syscon) receiveCommand() string {
func (sc syscon) receiveCommand() (string, error) {
switch sc.mode {
case "cxr":
return sc.receiveCXRCommand()
Expand All @@ -59,5 +59,5 @@ func (sc syscon) receiveCommand() string {
case "sw":
return sc.receiveSWCommand()
}
return "wrong mode"
return "wrong mode", errors.New("wrong mode")
}
57 changes: 28 additions & 29 deletions virtualcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,16 @@ func encode(plaintext []byte) []byte {
func (sc syscon) virtualCommandAuth() string {
switch sc.mode {
case "cxrf":
res := sc.proccessCommand("scopen")
if res == "SC_READY" {
res, err := sc.proccessCommand("scopen")
if err == nil && res == "SC_READY" {
fmt.Printf("Successfully opened syscon\n%s\n", res)
res = sc.proccessCommand(auth)
if len(res) == 128 {
res, err = sc.proccessCommand(auth)
if err == nil && len(res) == 128 {
fmt.Println("Right response length")
resNew, _ := hex.DecodeString(res)
if bytes.Equal(resNew[0:0x10], auth1ResponseHeader) {
resHex, _ := hex.DecodeString(res)
if bytes.Equal(resHex[0:0x10], auth1ResponseHeader) {
fmt.Println("Right Auth1 response header")
data := decode(resNew[0x10:0x40])
data := decode(resHex[0x10:0x40])
if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) {
fmt.Println("Right Auth1 response body")
newData := append(data[0x8:0x10], data[0x0:0x8]...)
Expand All @@ -185,8 +185,8 @@ func (sc syscon) virtualCommandAuth() string {
auth2Body := encode(newData)
authBody := append(auth2RequestHeader, auth2Body...)
com := fmt.Sprintf("%02X", authBody)
res := sc.proccessCommand(com)
if strings.Contains(res, "SC_SUCCESS") {
res, err := sc.proccessCommand(com)
if err == nil && strings.Contains(res, "SC_SUCCESS") {
return "Auth successful"
}
} else {
Expand All @@ -202,30 +202,29 @@ func (sc syscon) virtualCommandAuth() string {
fmt.Println("Error opening syscon")
}
default:
res := sc.proccessCommand("AUTH1 " + auth)
if res[0] == 0 {
resNew, _ := hex.DecodeString(res)
if bytes.Equal(resNew[0:0x10], auth1ResponseHeader) {
data := decode(resNew[0x10:0x40])
if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) {
newData := append(data[0x8:0x10], data[0x0:0x8]...)
newData = append(newData, zero...)
newData = append(newData, zero...)
auth2Body := encode(newData)
authBody := append(auth2RequestHeader, auth2Body...)
com := fmt.Sprintf("%02X", authBody)
res := sc.proccessCommand(com)
if res[0] == 0 {
fmt.Println("Auth successful")
}
} else {
fmt.Println("Wrong Auth1 response body")
res, err := sc.proccessCommand("AUTH1 " + auth)
resHex, _ := hex.DecodeString(res)
if err == nil && bytes.Equal(resHex[0:0x10], auth1ResponseHeader) {
fmt.Println("Right Auth1 response header")
data := decode(resHex[0x10:0x40])
if bytes.Equal(data[0x8:0x10], zero[0x0:0x8]) && bytes.Equal(data[0x10:0x20], auth1Response) && bytes.Equal(data[0x20:0x30], zero) {
fmt.Println("Right Auth1 response body")
newData := append(data[0x8:0x10], data[0x0:0x8]...)
newData = append(newData, zero...)
newData = append(newData, zero...)
auth2Body := encode(newData)
authBody := append(auth2RequestHeader, auth2Body...)
com := fmt.Sprintf("AUTH2 %02X", authBody)
res, err := sc.proccessCommand(com)
fmt.Println(res)
if err == nil {
return "Auth successful"
}
} else {
fmt.Println("Wrong Auth1 response header")
fmt.Println("Wrong Auth1 response body")
}
} else {
return "Auth1 response invalid"
fmt.Println("Wrong Auth1 response header")
}
}
return "Auth failed"
Expand Down

0 comments on commit bcb4413

Please sign in to comment.