-
Notifications
You must be signed in to change notification settings - Fork 139
/
syscfg.c
176 lines (148 loc) · 3.66 KB
/
syscfg.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
/*
* syscfg.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 "syscfg.h"
#include "mtd.h"
#include "util.h"
#include "commands.h"
#define SCFG_MAGIC 0x53436667
#define CNTB_MAGIC 0x434e5442
#define SCFG_LOCATION 0x4000
typedef struct SCfgHeader
{
uint32_t magic;
uint32_t bytes_used;
uint32_t bytes_total;
uint32_t version;
uint32_t unknown;
uint32_t entries;
} SCfgHeader;
typedef struct SCfgEntry
{
uint32_t magic;
union {
uint8_t data[16];
struct {
uint32_t type;
uint32_t size;
uint32_t offset;
} cntb;
};
} SCfgEntry;
typedef struct OIBSyscfgEntry
{
int type;
uint32_t size;
uint8_t* data;
} OIBSyscfgEntry;
static SCfgHeader header;
static OIBSyscfgEntry* entries;
static mtd_t *syscfg_dev = NULL;
static mtd_t *syscfg_device()
{
if(!syscfg_dev)
{
mtd_t *ptr = NULL;
while((ptr = mtd_find(ptr)))
{
if(ptr->usage == mtd_boot_images)
{
syscfg_dev = ptr;
break;
}
}
}
return syscfg_dev;
}
int syscfg_setup()
{
int i;
SCfgEntry curEntry;
uint32_t cursor;
mtd_t *dev = syscfg_device();
if(!dev)
return -1;
mtd_prepare(dev);
mtd_read(dev, &header, SCFG_LOCATION, sizeof(header));
if(header.magic != SCFG_MAGIC)
{
mtd_finish(dev);
bufferPrintf("syscfg: cannot find readable syscfg partition!\r\n");
return -1;
}
bufferPrintf("syscfg: found version 0x%08x with %d entries using %d of %d bytes\r\n", header.version, header.entries, header.bytes_used, header.bytes_total);
entries = (OIBSyscfgEntry*) malloc(sizeof(OIBSyscfgEntry) * header.entries);
cursor = SCFG_LOCATION + sizeof(header);
for(i = 0; i < header.entries; ++i)
{
mtd_read(dev, &curEntry, cursor, sizeof(curEntry));
if(curEntry.magic != CNTB_MAGIC)
{
entries[i].type = curEntry.magic;
entries[i].size = 16;
entries[i].data = (uint8_t*) malloc(16);
memcpy(entries[i].data, curEntry.data, 16);
} else
{
entries[i].type = curEntry.cntb.type;
entries[i].size = curEntry.cntb.size;
entries[i].data = (uint8_t*) malloc(curEntry.cntb.size);
mtd_read(dev, entries[i].data, SCFG_LOCATION + curEntry.cntb.offset, curEntry.cntb.size);
}
cursor += sizeof(curEntry);
}
mtd_finish(dev);
return 0;
}
uint8_t* syscfg_get_entry(uint32_t type, int* size)
{
int i;
for(i = 0; i < header.entries; ++i)
{
if(entries[i].type == type)
{
*size = entries[i].size;
return entries[i].data;
}
}
return NULL;
}
void syscfg_list()
{
int i;
for(i = 0; i < header.entries; ++i)
{
char *tp = (char*)&entries[i].type;
bufferPrintf("%c%c%c%c: ", tp[3], tp[2], tp[1], tp[0]);
int j;
for(j = 0; j < entries[i].size; j++)
bufferPrintf("%c", entries[i].data[j]);
bufferPrintf("\r\n");
}
}
static error_t cmd_syscfg_list(int argc, char **argv)
{
syscfg_list();
return SUCCESS;
}
COMMAND("syscfg_list", "List all syscfg entries.", cmd_syscfg_list);