Rizin
unix-like reverse engineering framework and cli tools
io_winkd.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2017 LemonBoy
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 // Copyright (c) 2014-2017, LemonBoy, All rights reserved. LGPLv3
5 
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3.0 of the License, or (at your option) any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library.
18 
19 #include <rz_io.h>
20 #include <rz_lib.h>
21 #include <rz_socket.h>
22 #include <rz_util.h>
23 #include <transport.h>
24 #include <winkd.h>
25 
26 typedef struct {
27  RzIODesc *fd;
28 } ReadAtCtx;
29 
30 static int op_at_phys(void *user, ut64 address, const ut8 *in, ut8 *out, int len, bool write) {
31  ReadAtCtx *ctx = user;
32  int ret = write ? winkd_write_at_phys(ctx->fd->data, address, in, len) : winkd_read_at_phys(ctx->fd->data, address, out, len);
33  return ret;
34 }
35 
36 static int read_at_phys(void *user, ut64 address, ut8 *buf, int len) {
37  return op_at_phys(user, address, NULL, buf, len, false);
38 }
39 
40 static int write_at_phys(void *user, ut64 address, const ut8 *buf, int len) {
41  return op_at_phys(user, address, buf, NULL, len, true);
42 }
43 
44 static int read_at_kernel_virtual(void *user, ut64 address, ut8 *buf, int len) {
45  ReadAtCtx *ctx = user;
46  return winkd_read_at(ctx->fd->data, address, buf, len);
47 }
48 
49 static bool __plugin_open(RzIO *io, const char *file, bool many) {
50  return (!strncmp(file, "winkd://", 8));
51 }
52 
53 static RzIODesc *__open(RzIO *io, const char *file, int rw, int mode) {
54  if (!__plugin_open(io, file, 0)) {
55  return NULL;
56  }
57 
58  // net - host:ip:key
59  // pipe - \\.\pipe\com_1 /tmp/windbg.pipe
60  io_backend_t *iob = NULL;
61  if (strchr(file + 8, ':')) {
62  iob = &iob_net;
63  } else {
64  iob = &iob_pipe;
65  }
66 
67  if (!iob) {
68  eprintf("Error: Invalid WinDBG path\n");
69  return NULL;
70  }
71 
72  void *io_ctx = iob->open(file + 8);
73  if (!io_ctx) {
74  eprintf("Error: Could not open the %s\n", iob->name);
75  return NULL;
76  }
77  eprintf("Opened %s %s with fd %p\n", iob->name, file + 8, io_ctx);
78 
79  io_desc_t *desc = io_desc_new(iob, io_ctx);
80  if (!desc) {
81  eprintf("Error: Could not create io_desc_t\n");
82  return NULL;
83  }
84 
86  if (!ctx) {
87  eprintf("Failed to initialize winkd context\n");
88  return NULL;
89  }
90  ctx->windctx.read_at_physical = read_at_phys;
91  ctx->windctx.write_at_physical = write_at_phys;
92  ctx->windctx.read_at_kernel_virtual = read_at_kernel_virtual;
94  if (!c) {
95  free(ctx);
96  return NULL;
97  }
98  c->fd = rz_io_desc_new(io, &rz_io_plugin_winkd, file, rw, mode, ctx);
99  if (!c->fd) {
100  free(c);
101  free(ctx);
102  return NULL;
103  }
104  ctx->windctx.user = c;
105  return c->fd;
106 }
107 
108 static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
109  if (!fd) {
110  return -1;
111  }
112  if (winkd_get_target(fd->data)) {
113  return winkd_write_at_uva(fd->data, io->off, buf, count);
114  }
115  return winkd_write_at(fd->data, io->off, buf, count);
116 }
117 
118 static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
119  switch (whence) {
120  case RZ_IO_SEEK_SET:
121  return io->off = offset;
122  case RZ_IO_SEEK_CUR:
123  return io->off += offset;
124  case RZ_IO_SEEK_END:
125  return io->off = UT64_MAX;
126  default:
127  return offset;
128  }
129 }
130 
131 static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
132  if (!fd) {
133  return -1;
134  }
135 
136  if (winkd_get_target(fd->data)) {
137  return winkd_read_at_uva(fd->data, io->off, buf, count);
138  }
139 
140  return winkd_read_at(fd->data, io->off, buf, count);
141 }
142 
143 static int __close(RzIODesc *fd) {
144  winkd_kdctx_free((KdCtx **)&fd->data);
145  return true;
146 }
147 
149  .name = "winkd",
150  .desc = "Attach to a KD debugger",
151  .uris = "winkd://",
152  .license = "LGPL3",
153  .open = __open,
154  .close = __close,
155  .read = __read,
156  .check = __plugin_open,
157  .lseek = __lseek,
158  .write = __write,
159  .isdbg = true
160 };
161 
162 #ifndef RZ_PLUGIN_INCORE
164  .type = RZ_LIB_TYPE_IO,
165  .data = &rz_io_plugin_winkd,
167 };
168 #endif
size_t len
Definition: 6502dis.c:15
const char * desc
Definition: bin_vsf.c:19
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
#define NULL
Definition: cris-opc.c:27
static static fork write
Definition: sflib.h:33
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 count
Definition: sflib.h:98
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static int read_at_phys(void *user, ut64 address, ut8 *buf, int len)
Definition: io_winkd.c:36
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_winkd.c:131
static int op_at_phys(void *user, ut64 address, const ut8 *in, ut8 *out, int len, bool write)
Definition: io_winkd.c:30
static bool __plugin_open(RzIO *io, const char *file, bool many)
Definition: io_winkd.c:49
static int read_at_kernel_virtual(void *user, ut64 address, ut8 *buf, int len)
Definition: io_winkd.c:44
RZ_API RzLibStruct rizin_plugin
Definition: io_winkd.c:163
static int write_at_phys(void *user, ut64 address, const ut8 *buf, int len)
Definition: io_winkd.c:40
static RzIODesc * __open(RzIO *io, const char *file, int rw, int mode)
Definition: io_winkd.c:53
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_winkd.c:108
static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_winkd.c:118
static int __close(RzIODesc *fd)
Definition: io_winkd.c:143
RzIOPlugin rz_io_plugin_winkd
Definition: io_winkd.c:148
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
io_backend_t iob_net
Definition: iob_net.c:564
io_backend_t iob_pipe
Definition: iob_pipe.c:130
uint8_t ut8
Definition: lh5801.h:11
#define eprintf(x, y...)
Definition: rlcc.c:7
#define RZ_IO_SEEK_CUR
Definition: rz_io.h:16
RZ_API RzIODesc * rz_io_desc_new(RzIO *io, RzIOPlugin *plugin, const char *uri, int flags, int mode, void *data)
Definition: io_desc.c:11
#define RZ_IO_SEEK_SET
Definition: rz_io.h:15
#define RZ_IO_SEEK_END
Definition: rz_io.h:17
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_VERSION
Definition: rz_version.h:8
#define c(i)
Definition: sha256.c:43
Definition: winkd.h:95
Definition: gzappend.c:170
const char * name
Definition: transport.h:28
void *(* open)(const char *path)
Definition: transport.h:32
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
ut64 off
Definition: rz_io.h:61
io_desc_t * io_desc_new(io_backend_t *iob, void *fp)
Definition: transport.c:7
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
int winkd_write_at_uva(RZ_BORROW RZ_NONNULL WindCtx *ctx, ut64 address, RZ_BORROW RZ_NONNULL RZ_IN const ut8 *buf, int count)
Definition: winkd.c:600
int winkd_write_at(RZ_BORROW RZ_NONNULL KdCtx *ctx, const ut64 offset, RZ_BORROW RZ_NONNULL RZ_IN const ut8 *buf, const int count)
Definition: winkd.c:1384
ut32 winkd_get_target(RZ_BORROW RZ_NONNULL WindCtx *ctx)
Definition: winkd.c:158
int winkd_write_at_phys(RZ_BORROW RZ_NONNULL KdCtx *ctx, const ut64 offset, RZ_BORROW RZ_NONNULL RZ_IN const ut8 *buf, const int count)
Definition: winkd.c:1408
int winkd_read_at(RZ_BORROW RZ_NONNULL KdCtx *ctx, const ut64 offset, RZ_BORROW RZ_NONNULL RZ_OUT ut8 *buf, const int count)
Definition: winkd.c:1377
void winkd_kdctx_free(RZ_OWN KdCtx **ctx)
Definition: winkd.c:187
int winkd_read_at_uva(RZ_BORROW RZ_NONNULL WindCtx *ctx, ut64 address, RZ_BORROW RZ_NONNULL RZ_OUT ut8 *buf, int count)
Definition: winkd.c:596
int winkd_read_at_phys(RZ_BORROW RZ_NONNULL KdCtx *ctx, const ut64 offset, RZ_BORROW RZ_NONNULL RZ_OUT ut8 *buf, const int count)
Definition: winkd.c:1370
KdCtx * winkd_kdctx_new(RZ_BORROW RZ_NONNULL io_desc_t *desc)
Definition: winkd.c:177
static const z80_opcode fd[]
Definition: z80_tab.h:997