Rizin
unix-like reverse engineering framework and cli tools
kd.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2017 LemonBoy <thatlemon@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "kd.h"
7 #include <rz_util/rz_log.h>
8 
9 #define KD_DBG if (false)
10 
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 }
24 
25 int kd_send_ctrl_packet(io_desc_t *desc, const ut32 type, const ut32 id) {
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 }
40 
41 int kd_send_data_packet(io_desc_t *desc, const ut32 type, const ut32 id, const ut8 *req,
42  const int req_len, const ut8 *buf, const ut32 buf_len) {
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 }
79 
80 int kd_read_packet(io_desc_t *desc, kd_packet_t **p) {
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 }
153 
154 bool kd_packet_is_valid(const kd_packet_t *p) {
155  return p->leader == KD_PACKET_CTRL || p->leader == KD_PACKET_DATA || p->leader == KD_PACKET_UNUSED;
156 }
157 
158 int kd_packet_is_ack(const kd_packet_t *p) {
159  return p->leader == KD_PACKET_CTRL && p->type == KD_PACKET_TYPE_ACKNOWLEDGE;
160 }
#define PFMT32x
lzma_index ** i
Definition: index.h:629
static int buf_len
Definition: asm_arc.c:22
const char * desc
Definition: bin_vsf.c:19
#define NULL
Definition: cris-opc.c:27
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
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
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
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: kd.c:41
int kd_read_packet(io_desc_t *desc, kd_packet_t **p)
Definition: kd.c:80
bool kd_packet_is_valid(const kd_packet_t *p)
Definition: kd.c:154
int kd_packet_is_ack(const kd_packet_t *p)
Definition: kd.c:158
@ KD_E_IOERR
Definition: kd.h:14
@ KD_E_OK
Definition: kd.h:10
@ KD_E_MALFORMED
Definition: kd.h:13
#define KD_MAX_PAYLOAD
Definition: kd.h:102
#define KD_PACKET_UNUSED
Definition: kd.h:96
#define KD_PACKET_DATA
Definition: kd.h:97
@ KD_PACKET_TYPE_RESEND
Definition: kd.h:24
@ KD_PACKET_TYPE_ACKNOWLEDGE
Definition: kd.h:23
#define KD_PACKET_CTRL
Definition: kd.h:98
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
int type
Definition: mipsasm.c:17
int id
Definition: op.c:540
#define eprintf(x, y...)
Definition: rlcc.c:7
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49
int iob_read(io_desc_t *desc, uint8_t *buf, const uint32_t buf_len)
Definition: transport.c:37
int iob_write(io_desc_t *desc, const uint8_t *buf, const uint32_t buf_len)
Definition: transport.c:16
#define KD_IO_PIPE
Definition: transport.h:17
ut64(WINAPI *w32_GetEnabledXStateFeatures)()