forked from jonasblixt/punchboot-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_partition.c
291 lines (209 loc) · 7.8 KB
/
api_partition.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdlib.h>
#include <string.h>
#include <pb-tools/pb-tools.h>
#include <pb-tools/api.h>
#include <pb-tools/wire.h>
#include <bpak/bpak.h>
PB_EXPORT int pb_api_partition_read_table(struct pb_context *ctx,
struct pb_partition_table_entry *out,
int *entries)
{
int rc;
struct pb_command cmd;
struct pb_result result;
struct pb_result_part_table_read tbl_read_result;
ctx->d(ctx, 2, "%s: call\n", __func__);
pb_wire_init_command(&cmd, PB_CMD_PART_TBL_READ);
rc = ctx->write(ctx, &cmd, sizeof(cmd));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
if (result.result_code != PB_RESULT_OK)
return result.result_code;
memcpy(&tbl_read_result, result.response, sizeof(tbl_read_result));
size_t bytes_to_read = (tbl_read_result.no_of_entries * \
sizeof(struct pb_result_part_table_entry));
ctx->d(ctx, 2, "%s: %i partitions, %i bytes\n", __func__,
tbl_read_result.no_of_entries, bytes_to_read);
if (tbl_read_result.no_of_entries > (*entries))
return -PB_RESULT_NO_MEMORY;
ctx->d(ctx, 2, "%s: reading table %p\n", __func__, out);
struct pb_result_part_table_entry *tbl = malloc(bytes_to_read+1);
rc = ctx->read(ctx, tbl, bytes_to_read);
if (rc != PB_RESULT_OK)
{
free(tbl);
return rc;
}
*entries = tbl_read_result.no_of_entries;
ctx->d(ctx, 2, "%s: read table\n", __func__);
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
{
ctx->d(ctx, 0, "%s: read error (%i)\n", __func__, rc);
free(tbl);
return rc;
}
if (!pb_wire_valid_result(&result))
{
ctx->d(ctx, 0, "%s: result error\n", __func__);
free(tbl);
return -PB_RESULT_ERROR;
}
for (int i = 0; i < tbl_read_result.no_of_entries; i++)
{
memcpy(out[i].uuid, tbl[i].uuid, 16);
strncpy(out[i].description, tbl[i].description, 36);
out[i].first_block = tbl[i].first_block;
out[i].last_block = tbl[i].last_block;
out[i].flags = tbl[i].flags;
out[i].block_size = tbl[i].block_size;
}
free(tbl);
ctx->d(ctx, 2, "%s: return %i (%s)\n", __func__, result.result_code,
pb_error_string(result.result_code));
return result.result_code;
}
PB_EXPORT int pb_api_partition_install_table(struct pb_context *ctx,
const uint8_t *uu, uint8_t variant)
{
int rc;
struct pb_command cmd;
struct pb_command_install_part_table install_tbl;
struct pb_result result;
ctx->d(ctx, 2, "%s: call\n", __func__);
memset(&install_tbl, 0, sizeof(install_tbl));
memcpy(install_tbl.uu, uu, 16);
install_tbl.variant = variant;
pb_wire_init_command2(&cmd, PB_CMD_PART_TBL_INSTALL, &install_tbl, sizeof(install_tbl));
rc = ctx->write(ctx, &cmd, sizeof(cmd));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
ctx->d(ctx, 2, "%s: return %i (%s)\n", __func__, result.result_code,
pb_error_string(result.result_code));
return result.result_code;
}
PB_EXPORT int pb_api_partition_verify(struct pb_context *ctx,
uint8_t *uuid,
uint8_t *sha256,
uint32_t size,
bool bpak)
{
int rc;
struct pb_command cmd;
struct pb_command_verify_part verify;
struct pb_result result;
ctx->d(ctx, 2, "%s: call\n", __func__);
memset(&verify, 0, sizeof(verify));
memcpy(verify.uuid, uuid, 16);
memcpy(verify.sha256, sha256, 32);
verify.size = size;
if (bpak)
verify.bpak = 1;
else
verify.bpak = 0;
pb_wire_init_command2(&cmd, PB_CMD_PART_VERIFY, &verify, sizeof(verify));
rc = ctx->write(ctx, &cmd, sizeof(cmd));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
ctx->d(ctx, 2, "%s: return %i (%s)\n", __func__, result.result_code,
pb_error_string(result.result_code));
return result.result_code;
}
PB_EXPORT int pb_api_partition_read_bpak(struct pb_context *ctx,
uint8_t *uuid,
struct bpak_header *header)
{
int rc;
struct pb_command cmd;
struct pb_command_read_bpak read_command;
struct pb_result result;
ctx->d(ctx, 2, "%s: call\n", __func__);
memset(&read_command, 0, sizeof(read_command));
memcpy(read_command.uuid, uuid, 16);
pb_wire_init_command2(&cmd, PB_CMD_PART_BPAK_READ, &read_command,
sizeof(read_command));
rc = ctx->write(ctx, &cmd, sizeof(cmd));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
if (result.result_code != PB_RESULT_OK)
return result.result_code;
rc = ctx->read(ctx, header, sizeof(*header));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
ctx->d(ctx, 2, "%s: return %i (%s)\n", __func__, result.result_code,
pb_error_string(result.result_code));
return result.result_code;
}
PB_EXPORT int pb_api_partition_erase(struct pb_context *ctx, uint8_t *uuid)
{
int rc;
struct pb_command cmd;
struct pb_command_erase_part erase_command;
struct pb_result result;
ctx->d(ctx, 2, "%s: call\n", __func__);
memset(&erase_command, 0, sizeof(erase_command));
memcpy(erase_command.uuid, uuid, 16);
pb_wire_init_command2(&cmd, PB_CMD_PART_ERASE, &erase_command,
sizeof(erase_command));
rc = ctx->write(ctx, &cmd, sizeof(cmd));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
ctx->d(ctx, 2, "%s: return %i (%s)\n", __func__, result.result_code,
pb_error_string(result.result_code));
return result.result_code;
}
PB_EXPORT int pb_api_partition_resize(struct pb_context *ctx, uint8_t *uuid,
size_t blocks)
{
int rc;
struct pb_command cmd;
struct pb_command_resize_part resize_command;
struct pb_result result;
ctx->d(ctx, 2, "%s: call\n", __func__);
memset(&resize_command, 0, sizeof(resize_command));
memcpy(resize_command.uuid, uuid, 16);
resize_command.blocks = blocks;
pb_wire_init_command2(&cmd, PB_CMD_PART_RESIZE, &resize_command,
sizeof(resize_command));
rc = ctx->write(ctx, &cmd, sizeof(cmd));
if (rc != PB_RESULT_OK)
return rc;
rc = ctx->read(ctx, &result, sizeof(result));
if (rc != PB_RESULT_OK)
return rc;
if (!pb_wire_valid_result(&result))
return -PB_RESULT_ERROR;
ctx->d(ctx, 2, "%s: return %i (%s)\n", __func__, result.result_code,
pb_error_string(result.result_code));
return result.result_code;
}