Dbgp protocol

From Vita Development Wiki
Revision as of 19:11, 1 November 2023 by CreepNT (talk | contribs) (Created page with "This page details the protocol used to communicate with SceDeci4pDbgp and SceDeci4pSDbgp from PSP2TMAPI. == PSP2TMAPI usage == PSP2TMAPI can be used from C++ or C#....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page details the protocol used to communicate with SceDeci4pDbgp and SceDeci4pSDbgp from PSP2TMAPI.

PSP2TMAPI usage

PSP2TMAPI can be used from C++ or C#. See SCE samples and documentation for more details about PSP2TMAPI in general.

The following is sample C# code for sending and receiving custom protocol data:
using PSP2TMAPILib; //Include the PSP2 TM API library

const uint PROTOCOL_ID = 0x10020000; //SDbgp0
bool received_packet = false;

/* Receiving packets requires a helper class */
class PacketReceiver : IEventCustomProtocol {
        uint protocolId;
        bool receivedPacket = false;

        public PacketReceiver(uint proto) {
            protocolId = proto;
        }

        //Called every time a packet is received from the target
        public void OnPacketReceived(uint uProtocol, object Packet, uint uFragmentSeq, uint uAttributes) {
            if (uProtocol != protocolId)
                return;

            byte[] packetData = (byte[])Packet;
            string dataForPrint = "";
            foreach (byte b in packetData) {
                dataForPrint += string.Format("{0:X2} ", b);
            }
            Console.WriteLine("Got packet from protocol 0x{0:X}:", uProtocol);
            Console.WriteLine(dataForPrint);
            receivedPacket = true;
        }

        //Dummy routines for the rest of IEventCustomProtocol
        public void OnErrorInvalidHeader(uint uProtocol, object Packet) { }
        public void OnErrorNoConnection(uint uProtocol, object Packet) { }
        public void OnErrorNoProtocol(uint uProtocol, object Packet) { }
        public void OnErrorNoSpace(uint uProtocol, object Packet) { }
        public void OnStatusProtocol(uint uProtocol, uint uProtocolVersion, uint uProtocolLimit, uint uTargetNode, uint uStatus) { }
        public void OnStatusSpace() { }
        public void OnStatusForceUnregistered(uint uProtocol, string bstrRequester) { }
}

static void Main() {
  PSP2TMAPI api = new PSP2TMAPI();
  api.CheckCompatibility((uint)eCompatibleVersion.BuildVersion);

  //Select a target to interact with - this assumes target is already connected to
  ITarget target = api.Targets.DefaultTarget;

  try {
    target.ProtocolAvailabilityInfo(PROTOCOL_ID, out uint version, out uint versionLimit, out uint nodeID);
  } catch {
     //Protocol not available - cannot interact with (S)Dbgp (module probably not loaded)
     return;
  }

  //Make a receiver for the protocol
  PacketReceiver rcv = new PacketReceiver();

  //Register the protocol, register our receiver and send a packet
  target.RegisterCustomProtocol(PROTOCOL_ID);
  target.AdviseCustomProtocol(rcv);

  byte[] packet = new byte[4];
  packet[1] = 0xF0;
  packet[2] = 0x02;
  target.SendCustomProtocolData(protocol_id, packet);

  while (!rcv.receivedPacket) { /* wait for reply from target */ }

  //Cleanup
  target.UnadviseCustomProtocol(rcv);
  target.UnregisterCustomProtocol(PROTOCOL_ID);
}

Protocol IDs

SceDeci4pDbgp registers a single protocol for the whole system. SceDeci4pSDbgp registers a protocol for each CPU of the system (SDBGPx = CPU x).

Protocol name ID
DBGP 0x00020000
SDBGP0 0x10020000
SDBGP1 0x10020001
SDBGP2 0x10020002
SDBGP3 0x10020003

Packets

Common

//Opaque header holding data for the transport protocol (?)
//This is only visible on Vita-side: on host side, PSP2TMAPI does not transmit this data
typedef struct _TransportHeader {
    uint8_t unknown[0x10];
} TransportHeader;

//Header for all packets
typedef struct _DbgpPacketHeader {
    TransportHeader OPAQUE;
    uint8_t direction; //<! 0 for Host->Target, 1 for Target->Host
    uint8_t group;     //<! Command group
    uint8_t type;      //<! Command type - group specific
    uint8_t unk13;     //<! Unknown. Maybe to serve as user-provided tag.
} DbgpPacketHeader;

//Header for Target->Host packets
typedef struct _DbgpT2HPacketHeader {
    DbgpPacketHeader hdr;
    uint8_t unk14;
    uint8_t unk15;
    uint16_t unk16;
    int32_t status; //<! Status/error code
} DbgpT2HPacket;

//Command groups
#define DBGP_GROUP_SYSTEM   (0x10u)
#define DBGP_GROUP_PROCESS  (0x20u)
#define DBGP_GROUP_THREAD   (0x30u)
#define DBGP_GROUP_MODULE   (0x40u)
#define DBGP_GROUP_unk_50   (0x50u)
#define DBGP_GROUP_FILE     (0x60u)
#define DBGP_GROUP_NET      (0x70u)
#define DBGP_GROUP_COREDUMP (0x80u)
#define DBGP_GROUP_unk_90   (0x90u) //DIP switch 0xD7 must be off
#define DBGP_GROUP_MISC     (0xF0u)

When a request is sent to target with type=X, the response is sent to host with type=X+1.

System Group

SYSTEM_GET_CONF

type 0x00
Dbgp availability: Since at least firmware 0.920.050
SDbgp availability: Since at least firmware 0.920.050

Obtain system configuration information.

/* Request packet: no data in addition to header */

/* Response packet for Dbgp: */
struct {
    DbgpT2HPacket hdr;
    uint32_t size;   //<! Size of the data starting from this field (0x1C)
    uint32_t unk20;  //<! Known values: 7 (0.920.050/4.00)
    uint32_t unk24;  //<! Known values: 0x100 (0.920.050/4.00)
    uint32_t unk28;  //<! Known values: 0 (0.920.050/4.00)
    uint32_t unk2C;  //<! Known values: 0 (0.920.050/4.00)
    uint32_t unk30;  //<! Known values: 0 (0.920.050), 3 (4.00)
    uint32_t fwVersion;
}

/* Response packet for SDbgp: */
struct {
    DbgpT2HPacket hdr;
    uint32_t size;  //<! Size of the data starting from this field (0x1C in 0.920.050, 0x30 in 4.00)
    uint32_t unk20; //<! Known values: 7 (4.00)
    uint32_t unk24; //<! Known values: 4 (0.920.050), 0x14 (4.00)
    uint32_t unk28; //<! Known values: 6 (0.920.050/4.00)
    uint32_t unk2C; //<! Known values: 0 (0.920.050/4.00)
    uint32_t unk30; //<! Known values: 1 (4.00), 4 (0.920.050)
    uint8_t unused_34[4];
    uint32_t fwVersion;
    uint8_t unused_3C[4];
    uint32_t aslrSeed;
    uintptr_t sysroot;    //<! Virtual address of the SceSysroot object
    uint32_t sysrootSize; //<! Size of the SceSysroot object
}

Process Group

Thread Group

Module Group

Group 0x50

This group is only valid for SDbgp, since at least firmware 0.920.050.

All packets of this group are silently ignored. No response is sent to the host.

File Group

Net Group

Coredump Group

Group 0x90

Misc Group