-
Notifications
You must be signed in to change notification settings - Fork 139
/
scripting.c
223 lines (192 loc) · 4.45 KB
/
scripting.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
/*
* scripting.c
*
* 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 "commands.h"
#include "nvram.h"
#include "hfs/common.h"
#include "hfs/bdev.h"
#include "hfs/fs.h"
#include "hfs/hfsplus.h"
#include "printf.h"
#include "util.h"
uint8_t *script_load_hfs_file(int disk, int part, char *path, uint32_t *size)
{
bdevfs_device_t *dev = bdevfs_open(disk, part);
if(!dev)
{
bufferPrintf("fs: Cannot open partition hd%d,%d.\n", disk, part);
return NULL;
}
HFSPlusCatalogRecord* record;
char *name;
uint8_t *address;
uint32_t sz = 0;
record = getRecordFromPath(path, dev->volume, &name, NULL);
if(record != NULL)
{
if(record->recordType == kHFSPlusFolderRecord)
bufferPrintf("scripting: That path leads to a directory!\n");
else
sz = readHFSFile((HFSPlusCatalogFile*)record, &address, dev->volume);
}
else
bufferPrintf("scripting: No such file or directory %s.\n", path);
bdevfs_close(dev);
if(address)
{
*size = sz;
return address;
}
return NULL;
}
uint8_t *script_load_file(char *id, uint32_t *size)
{
if(*id == '(') // In format (hdX,Y)/some/path
{
char *dupe = strdup(id+1);
char *ptr = dupe;
if(ptr[0] == 'h' && ptr[1] == 'd')
ptr += 2;
char *devEnd = strchr(ptr, ')');
if(devEnd)
{
*devEnd = 0;
devEnd++;
int part = 0;
char *partStart = strchr(ptr, ',');
if(partStart)
{
*partStart = 0;
partStart++;
part = parseNumber(partStart);
}
int device = parseNumber(ptr);
uint8_t *ret = script_load_hfs_file(device, part, devEnd, size); // TODO: Hahahah! I should really fix this. -- Ricky26
free(dupe);
return ret;
}
}
else if(*id == '/') // In format /some/path of the system partition
{
return script_load_hfs_file(0, 0, id, size);
}
else if(*id >= '0' && *id <= '9')
{
char *ptr = strdup(id);
char *extent = strchr(ptr, '+');
uint8_t *ret = NULL;
if(extent)
{
*extent = 0;
extent++;
uint32_t sz = parseNumber(extent);
uint32_t addr = parseNumber(id);
*size = sz;
ret = (uint8_t*)addr;
}
else
bufferPrintf("scripting: Invalid memory location, must be in the format location+size.\n");
free(ptr);
return ret;
}
bufferPrintf("scripting: Badly formatted URI, %s\n", id);
return NULL;
}
char **script_split_file(char *_data, uint32_t _sz, uint32_t *_count)
{
int count = 0;
char *ptr = _data;
char *end = ptr + _sz;
while(ptr < end)
{
switch(*ptr)
{
case '\n':
*ptr = '\0';
// Fall through
case '\0':
count++;
break;
}
ptr++;
}
char **ret = malloc(sizeof(char**)*count);
ptr = _data;
ret[0] = ptr;
int i = 1;
while(ptr < end)
{
if(*ptr == 0)
{
ret[i] = ptr+1;
i++;
}
ptr++;
}
*_count = count;
return ret;
}
int script_run_command(char* command)
{
int argc;
char** argv = command_parse(command, &argc);
int success = command_run(argc, argv);
free(argv);
return success;
}
int script_run_commands(char** cmds, uint32_t count)
{
int i;
for(i = 0; i < count; i++)
{
bufferPrintf("scripting: %s\n", cmds[i]);
int ret = script_run_command(cmds[i]);
if(ret != 0)
{
bufferPrintf("scripting: Failed to run command '%s'.\n", cmds[i]);
return ret;
}
}
return 0;
}
int script_run_file(char *path)
{
uint32_t size, count;
char *data = (char*)script_load_file(path, &size);
if(!data)
{
bufferPrintf("scripting: Failed to load file '%s'.\n", path);
return -1;
}
char **cmds = script_split_file(data, size, &count);
if(!cmds)
{
bufferPrintf("scripting: Failed to parse script '%s'.\n", path);
free(data);
return -1;
}
int ret = script_run_commands(cmds, count);
free(cmds);
free(data);
return ret;
}