-
Notifications
You must be signed in to change notification settings - Fork 139
/
commands.c
218 lines (181 loc) · 4.96 KB
/
commands.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
/*
* commands.c - OpeniBoot base commands
*
* Copyright 2010 iDroid Project
*
* This file is part of iDroid. An android distribution for Apple products.
* For more information, please visit http://www.idroidproject.org/.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "openiboot.h"
#include "arm/arm.h"
#include "commands.h"
#include "util.h"
int received_file_size; // Makes commands automatically use output from last command.
OPIBCommand *command_get_next(OIBCommandIterator *_it)
{
if(*_it == NULL)
*_it = &command_list_init;
(*_it)++;
return **_it;
}
char **command_parse(char *str, int *argc)
{
while((*str == '\t')
||(*str == ' '))
str++;
int len = strlen(str);
char *end = str + len;
char *ptr = str;
int quote = 0;
while(ptr < end)
{
if(*ptr == '"')
quote = !quote;
else if(*ptr == '#' && !quote)
{
*ptr = 0;
break;
}
ptr++;
}
return tokenize(str, argc);
}
error_t command_run(int argc, char **argv)
{
OIBCommandIterator cmdIt = NULL;
OPIBCommand *cmd;
if(argc < 1) // No command to run.
return SUCCESS;
while((cmd = command_get_next(&cmdIt)))
{
if(strcmp(argv[0], cmd->name) == 0)
return cmd->routine(argc, argv);
}
return ENOENT;
}
static error_t cmd_help(int argc, char** argv)
{
OIBCommandIterator it = NULL;
OPIBCommand *cmd;
while((cmd = command_get_next(&it)))
bufferPrintf("%-20s%s\r\n", cmd->name, cmd->description);
return SUCCESS;
}
COMMAND("help", "list the available commands", cmd_help);
static error_t cmd_reboot(int argc, char** argv)
{
Reboot();
return SUCCESS;
}
COMMAND("reboot", "reboot the device", cmd_reboot);
static error_t cmd_md(int argc, char** argv)
{
if(argc < 3)
{
bufferPrintf("Usage: %s <address> <len>\r\n", argv[0]);
return EINVAL;
}
uint32_t address = parseNumber(argv[1]);
uint32_t len = parseNumber(argv[2]);
bufferPrintf("dumping memory 0x%x - 0x%x\r\n", address, address + len);
buffer_dump_memory(address, len);
return SUCCESS;
}
COMMAND("md", "display a block of memory as 32-bit integers", cmd_md);
static error_t cmd_hexdump(int argc, char** argv)
{
if(argc < 3)
{
bufferPrintf("Usage: %s <address> <len>\r\n", argv[0]);
return EINVAL;
}
uint32_t address = parseNumber(argv[1]);
uint32_t len = parseNumber(argv[2]);
bufferPrintf("dumping memory 0x%x - 0x%x\r\n", address, address + len);
hexdump((void*)address, len);
return SUCCESS;
}
COMMAND("hexdump", "display a block of memory like 'hexdump -C'", cmd_hexdump);
static error_t cmd_cat(int argc, char** argv)
{
if(argc < 3)
{
bufferPrintf("Usage: %s <address> <len>\r\n", argv[0]);
return EINVAL;
}
uint32_t address = parseNumber(argv[1]);
uint32_t len = parseNumber(argv[2]);
addToBuffer((char*) address, len);
return SUCCESS;
}
COMMAND("cat", "dumps a block of memory", cmd_cat);
static error_t cmd_mwb(int argc, char** argv)
{
if(argc < 3)
{
bufferPrintf("Usage: %s <address> <data>\r\n", argv[0]);
return EINVAL;
}
uint8_t* address = (uint8_t*) parseNumber(argv[1]);
uint8_t data = parseNumber(argv[2]);
*address = data;
bufferPrintf("Written to 0x%x to 0x%x\r\n", (uint8_t)data, address);
return SUCCESS;
}
COMMAND("mwb", "write a byte into a memory address", cmd_mwb);
static error_t cmd_mws(int argc, char** argv)
{
if(argc < 3)
{
bufferPrintf("Usage: %s <address> <string>\r\n", argv[0]);
return EINVAL;
}
char* address = (char*) parseNumber(argv[1]);
strcpy(address, argv[2]);
bufferPrintf("Written %s to 0x%x\r\n", argv[2], address);
return SUCCESS;
}
COMMAND("mws", "write a string into a memory address", cmd_mws);
static error_t cmd_mw(int argc, char** argv)
{
if(argc < 3)
{
bufferPrintf("Usage: %s <address> <data>\r\n", argv[0]);
return EINVAL;
}
uint32_t* address = (uint32_t*) parseNumber(argv[1]);
uint32_t data = parseNumber(argv[2]);
*address = data;
bufferPrintf("Written to 0x%x to 0x%x\r\n", data, address);
return SUCCESS;
}
COMMAND("mw", "write a 32-bit dword into a memory address", cmd_mw);
static error_t cmd_echo(int argc, char** argv)
{
int i;
for(i = 1; i < argc; i++)
bufferPrintf("%s ", argv[i]);
bufferPrintf("\r\n");
return SUCCESS;
}
COMMAND("echo", "echo back a line", cmd_echo);
static error_t cmd_version(int argc, char** argv)
{
bufferPrintf("%s\r\n", OPENIBOOT_VERSION_STR);
return SUCCESS;
}
COMMAND("version", "display the version string", cmd_version);