-
Notifications
You must be signed in to change notification settings - Fork 4
/
cm-tool.c
231 lines (192 loc) · 5.36 KB
/
cm-tool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** @file
* cm-tool - Code module execution helper
*/
/*
* Copyright (C) 2020 Alexander Eichner <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "libpspproxy.h"
struct termios TermIosOld;
/**
* Loads the given file into memory.
*
* @returns Status code.
* @param pszFilename The filename to load.
* @param ppv Where to store the memory buffer pointer on success.
* @param pcb Where to store the size of the loaded file on success.
*/
int cmToolLoadFromFile(const char *pszFilename, void **ppv, size_t *pcb)
{
int rc = 0;
FILE *pFwFile = fopen(pszFilename, "rb");
if (pFwFile)
{
/* Determine file size. */
rc = fseek(pFwFile, 0, SEEK_END);
if (!rc)
{
long cbFw = ftell(pFwFile);
if (cbFw != -1)
{
rewind(pFwFile);
void *pvFw = malloc(cbFw);
if (pvFw)
{
size_t cbRead = fread(pvFw, cbFw, 1, pFwFile);
if (cbRead == 1)
{
*ppv = pvFw;
*pcb = cbFw;
return 0;
}
free(pvFw);
rc = -1;
}
else
rc = -1;
}
else
rc = errno;
}
else
rc = errno;
fclose(pFwFile);
}
else
rc = errno;
return rc;
}
/**
* @copydoc{PSPPROXYIOIF,pfnLogMsg}
*/
static void cmToolProxyIoIfLogMsg(PSPPROXYCTX hCtx, void *pvUser, const char *pszMsg)
{
printf("%s", pszMsg);
}
/**
* @copydoc{PSPPROXYIOIF,pfnOutBufWrite}
*/
static int cmToolProxyIoIfOutBufWrite(PSPPROXYCTX hCtx, void *pvUser, uint32_t idOutBuf, const void *pvBuf, size_t cbBuf)
{
if (idOutBuf == 0)
{
printf("%.*s", (int)cbBuf, (const char *)pvBuf);
fflush(stdout);
}
return 0;
}
/**
* @copydoc{PSPPROXYIOIF,pfnInBufPeek}
*/
static size_t cmToolProxyIoIfInBufPeek(PSPPROXYCTX hCtx, void *pvUser, uint32_t idInBuf)
{
size_t cbAvail = 0;
if (idInBuf == 0)
{
int iAvail = 0;
ioctl(0, FIONREAD, &iAvail);
cbAvail = iAvail;
}
return cbAvail;
}
/**
* @copydoc{PSPPROXYIOIF,pfnInBufRead}
*/
static int cmToolProxyIoIfInBufRead(PSPPROXYCTX hCtx, void *pvUser, uint32_t idInBuf, void *pvBuf, size_t cbRead, size_t *pcbRead)
{
if (idInBuf == 0)
{
uint8_t *pbBuf = (uint8_t *)pvBuf;
while (cbRead)
{
int iChr = getchar();
if (iChr == EOF)
return -1;
*pbBuf++ = (char)iChr;
cbRead--;
}
}
return 0;
}
/**
* Proxy I/O interface callbacks.
*/
static PSPPROXYIOIF g_ProxyIoIf =
{
/** pfnLogMsg */
cmToolProxyIoIfLogMsg,
/** pfnOutBufWrite */
cmToolProxyIoIfOutBufWrite,
/** pfnInBufPeek */
cmToolProxyIoIfInBufPeek,
/** pfnInBufRead */
cmToolProxyIoIfInBufRead,
};
static void cmToolTerminalCfg(void)
{
setvbuf(stdin, NULL, _IONBF ,0);
setvbuf(stdout, NULL, _IONBF ,0);
/* Disable character echoing and enable raw mode on stdin. */
int rcPsx = tcgetattr(0, &TermIosOld);
if (!rcPsx)
{
struct termios TermIos = TermIosOld;
cfmakeraw(&TermIos);
tcsetattr(0, TCSANOW, &TermIos);
}
}
static void cmToolTerminalRestore(void)
{
tcsetattr(0, TCSANOW, &TermIosOld);
}
int main(int argc, char *argv[])
{
void *pv = NULL;
size_t cb = 0;
int rc = cmToolLoadFromFile(argv[2], &pv, &cb);
if (!rc)
{
PSPPROXYCTX hCtx;
rc = PSPProxyCtxCreate(&hCtx, argv[1], &g_ProxyIoIf, NULL);
if (!rc)
{
rc = PSPProxyCtxCodeModLoad(hCtx, pv, cb);
if (!rc)
{
cmToolTerminalCfg();
uint32_t u32CmRet = 0;
rc = PSPProxyCtxCodeModExec(hCtx, 0 /*u32Arg0*/, 0 /*u32Arg1*/, 0 /*u32Arg2*/, 0 /*u32Arg3*/, &u32CmRet, UINT32_MAX);
cmToolTerminalRestore();
if (!rc)
printf("Code module executed successfully and returned %#x\n", u32CmRet);
else
printf("Code module execution failed with %d\n", rc);
}
else
printf("Loading the code module failed with %d\n", rc);
PSPProxyCtxDestroy(hCtx);
}
free(pv);
}
else
printf("Loading the file \"%s\" failed with %d\n", argv[2], rc);
return rc;
}