Rizin
unix-like reverse engineering framework and cli tools
kd.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kd.h"
#include <rz_util/rz_log.h>

Go to the source code of this file.

Macros

#define KD_DBG   if (false)
 

Functions

ut32 kd_data_checksum (const ut8 *buf, const ut64 buf_len)
 
int kd_send_ctrl_packet (io_desc_t *desc, const ut32 type, const ut32 id)
 
int kd_send_data_packet (io_desc_t *desc, const ut32 type, const ut32 id, const ut8 *req, const int req_len, const ut8 *buf, const ut32 buf_len)
 
int kd_read_packet (io_desc_t *desc, kd_packet_t **p)
 
bool kd_packet_is_valid (const kd_packet_t *p)
 
int kd_packet_is_ack (const kd_packet_t *p)
 

Macro Definition Documentation

◆ KD_DBG

#define KD_DBG   if (false)

Definition at line 9 of file kd.c.

Function Documentation

◆ kd_data_checksum()

ut32 kd_data_checksum ( const ut8 buf,
const ut64  buf_len 
)

Definition at line 11 of file kd.c.

11  {
12  ut32 i, acc;
13 
14  if (!buf || !buf_len) {
15  return 0;
16  }
17 
18  for (i = acc = 0; i < buf_len; i++) {
19  acc += buf[i];
20  }
21 
22  return acc;
23 }
lzma_index ** i
Definition: index.h:629
static int buf_len
Definition: asm_arc.c:22
uint32_t ut32
voidpf void * buf
Definition: ioapi.h:138

References buf_len, and i.

Referenced by kd_read_packet(), and kd_send_data_packet().

◆ kd_packet_is_ack()

int kd_packet_is_ack ( const kd_packet_t *  p)

Definition at line 158 of file kd.c.

158  {
159  return p->leader == KD_PACKET_CTRL && p->type == KD_PACKET_TYPE_ACKNOWLEDGE;
160 }
@ KD_PACKET_TYPE_ACKNOWLEDGE
Definition: kd.h:23
#define KD_PACKET_CTRL
Definition: kd.h:98
void * p
Definition: libc.cpp:67

References KD_PACKET_CTRL, KD_PACKET_TYPE_ACKNOWLEDGE, and p.

◆ kd_packet_is_valid()

bool kd_packet_is_valid ( const kd_packet_t *  p)

Definition at line 154 of file kd.c.

154  {
155  return p->leader == KD_PACKET_CTRL || p->leader == KD_PACKET_DATA || p->leader == KD_PACKET_UNUSED;
156 }
#define KD_PACKET_UNUSED
Definition: kd.h:96
#define KD_PACKET_DATA
Definition: kd.h:97

References KD_PACKET_CTRL, KD_PACKET_DATA, KD_PACKET_UNUSED, and p.

Referenced by kd_read_packet().

◆ kd_read_packet()

int kd_read_packet ( io_desc_t desc,
kd_packet_t **  p 
)

Definition at line 80 of file kd.c.

80  {
81  kd_packet_t pkt;
82  ut8 *buf;
83 
84  *p = NULL;
85 
86  if (iob_read(desc, (ut8 *)&pkt, sizeof(kd_packet_t)) <= 0) {
87  return KD_E_IOERR;
88  }
89 
90  if (!kd_packet_is_valid(&pkt)) {
91  KD_DBG eprintf("invalid leader %08x, trying to recover\n", pkt.leader);
92  while (!kd_packet_is_valid(&pkt)) {
94  char sig[4];
95  // Read byte-by-byte searching for the start of a packet
96  int ret;
97  while ((ret = iob_read(desc, (ut8 *)&sig, 1)) > 0) {
98  if (sig[0] == '0' || sig[0] == 'i') {
99  if (iob_read(desc, (ut8 *)&sig + 1, 3) == 3) {
100  if (strncmp(sig, "000", 3) && strncmp(sig, "iii", 3)) {
101  continue;
102  }
103  memcpy(&pkt, sig, sizeof(sig));
104  if (iob_read(desc, (ut8 *)&pkt + 4, sizeof(kd_packet_t) - 4) <= 0) {
105  return KD_E_IOERR;
106  }
107  break;
108  } else {
109  return KD_E_IOERR;
110  }
111  }
112  }
113  if (!ret) {
114  return KD_E_IOERR;
115  }
116  }
117  }
118 
119  buf = malloc(sizeof(kd_packet_t) + pkt.length);
120  if (!buf) {
121  return KD_E_IOERR;
122  }
123  memcpy(buf, &pkt, sizeof(kd_packet_t));
124 
125  if (pkt.length) {
126  iob_read(desc, (ut8 *)buf + sizeof(kd_packet_t), pkt.length);
127  }
128 
129  if (pkt.checksum != kd_data_checksum(buf + sizeof(kd_packet_t), pkt.length)) {
130  KD_DBG eprintf("Checksum mismatch!\n");
131  free(buf);
132  return KD_E_MALFORMED;
133  }
134 
135  if (pkt.leader == KD_PACKET_DATA) {
136  if (desc->iob->type == KD_IO_PIPE) {
137  ut8 trailer;
138  iob_read(desc, (ut8 *)&trailer, 1);
139 
140  if (trailer != 0xAA) {
141  KD_DBG eprintf("Missing trailer 0xAA\n");
142  free(buf);
143  return KD_E_MALFORMED;
144  }
145  }
146  }
148 
149  *p = (kd_packet_t *)buf;
150 
151  return KD_E_OK;
152 }
const char * desc
Definition: bin_vsf.c:19
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ut32 kd_data_checksum(const ut8 *buf, const ut64 buf_len)
Definition: kd.c:11
int kd_send_ctrl_packet(io_desc_t *desc, const ut32 type, const ut32 id)
Definition: kd.c:25
#define KD_DBG
Definition: kd.c:9
bool kd_packet_is_valid(const kd_packet_t *p)
Definition: kd.c:154
@ KD_E_IOERR
Definition: kd.h:14
@ KD_E_OK
Definition: kd.h:10
@ KD_E_MALFORMED
Definition: kd.h:13
@ KD_PACKET_TYPE_RESEND
Definition: kd.h:24
uint8_t ut8
Definition: lh5801.h:11
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
#define eprintf(x, y...)
Definition: rlcc.c:7
int iob_read(io_desc_t *desc, uint8_t *buf, const uint32_t buf_len)
Definition: transport.c:37
#define KD_IO_PIPE
Definition: transport.h:17

References desc, eprintf, free(), iob_read(), kd_data_checksum(), KD_DBG, KD_E_IOERR, KD_E_MALFORMED, KD_E_OK, KD_IO_PIPE, KD_PACKET_DATA, kd_packet_is_valid(), KD_PACKET_TYPE_ACKNOWLEDGE, KD_PACKET_TYPE_RESEND, kd_send_ctrl_packet(), malloc(), memcpy(), NULL, and p.

Referenced by winkd_sync(), and winkd_wait_packet().

◆ kd_send_ctrl_packet()

int kd_send_ctrl_packet ( io_desc_t desc,
const ut32  type,
const ut32  id 
)

Definition at line 25 of file kd.c.

25  {
26  kd_packet_t pkt;
27 
28  pkt.leader = KD_PACKET_CTRL;
29  pkt.length = 0;
30  pkt.checksum = 0;
31  pkt.id = id;
32  pkt.type = type;
33 
34  if (iob_write(desc, (ut8 *)&pkt, sizeof(kd_packet_t)) < 0) {
35  return KD_E_IOERR;
36  }
37 
38  return KD_E_OK;
39 }
int type
Definition: mipsasm.c:17
int id
Definition: op.c:540
int iob_write(io_desc_t *desc, const uint8_t *buf, const uint32_t buf_len)
Definition: transport.c:16

References desc, id, iob_write(), KD_E_IOERR, KD_E_OK, KD_PACKET_CTRL, and type.

Referenced by kd_read_packet(), and winkd_sync().

◆ kd_send_data_packet()

int kd_send_data_packet ( io_desc_t desc,
const ut32  type,
const ut32  id,
const ut8 req,
const int  req_len,
const ut8 buf,
const ut32  buf_len 
)

Definition at line 41 of file kd.c.

42  {
43  kd_packet_t pkt;
44 
45  if (req_len + buf_len > KD_MAX_PAYLOAD) {
46  return KD_E_MALFORMED;
47  }
48 
49  RZ_LOG_DEBUG("==== Send Data ====\n");
50  RZ_LOG_DEBUG("ID: 0x%" PFMT32x "\n", id);
51  RZ_LOG_DEBUG("Type: 0x%" PFMT32x "\n", type);
52 
53  pkt.leader = KD_PACKET_DATA;
54  pkt.length = req_len + buf_len;
55  pkt.checksum = kd_data_checksum(req, req_len) + kd_data_checksum(buf, buf_len);
56  pkt.id = id;
57  pkt.type = type;
58 
59  if (iob_write(desc, (ut8 *)&pkt, sizeof(kd_packet_t)) < 0) {
60  return KD_E_IOERR;
61  }
62 
63  if (iob_write(desc, (ut8 *)req, req_len) < 0) {
64  return KD_E_IOERR;
65  }
66 
67  if (buf && iob_write(desc, (ut8 *)buf, buf_len) < 0) {
68  return KD_E_IOERR;
69  }
70 
71  if (desc->iob->type == KD_IO_PIPE) {
72  if (iob_write(desc, (ut8 *)"\xAA", 1) < 0) {
73  return KD_E_IOERR;
74  }
75  }
76 
77  return KD_E_OK;
78 }
#define PFMT32x
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
#define KD_MAX_PAYLOAD
Definition: kd.h:102
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49

References buf_len, desc, id, iob_write(), kd_data_checksum(), KD_E_IOERR, KD_E_MALFORMED, KD_E_OK, KD_IO_PIPE, KD_MAX_PAYLOAD, KD_PACKET_DATA, PFMT32x, req, RZ_LOG_DEBUG, and type.

Referenced by do_io_reply(), winkd_continue(), and winkd_send_state_manipulate_req().