forked from linux-msm/qdl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ufs.c
297 lines (261 loc) · 11.1 KB
/
ufs.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
292
293
294
295
296
297
/*
* Copyright (c) 2018, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "ufs.h"
#include "qdl.h"
struct ufs_common *ufs_common_p;
struct ufs_epilogue *ufs_epilogue_p;
struct ufs_body *ufs_body_p;
struct ufs_body *ufs_body_last;
static const char notice_b_config_descr_lock[] = "\n"
"Please pay attention that UFS provisioning is irreversible (OTP) operation unless parameter bConfigDescrLock = 0.\n"
"In order to prevent unintentional device locking the tool has the following safety:\n\n"
" if you REALLY intend to perform OTP, please ensure that your XML includes property\n"
" bConfigDescrLock = 1 AND provide command line parameter --finalize-provisioning.\n\n"
" Unless you intend to lock your device, please set bConfigDescrLock = 0 in your XML\n"
" and don't use command line parameter --finalize-provisioning.\n\n"
"In case of mismatch between CL and XML provisioning is not performed.\n\n";
bool ufs_need_provisioning(void) {
return ufs_epilogue_p != NULL;
}
struct ufs_common *ufs_parse_common_params(xmlNode *node, bool finalize_provisioning) {
(void) finalize_provisioning;
struct ufs_common *result;
int errors;
result = calloc(1, sizeof(struct ufs_common));
errors = 0;
result->bNumberLU = attr_as_unsigned(node, "bNumberLU", &errors);
result->bBootEnable = attr_as_unsigned(node, "bBootEnable", &errors) != 0;
result->bDescrAccessEn = attr_as_unsigned(node, "bDescrAccessEn", &errors) != 0;
result->bInitPowerMode = attr_as_unsigned(node, "bInitPowerMode", &errors);
result->bHighPriorityLUN = attr_as_unsigned(node, "bHighPriorityLUN", &errors);
result->bSecureRemovalType = attr_as_unsigned(node, "bSecureRemovalType", &errors);
result->bInitActiveICCLevel = attr_as_unsigned(node, "bInitActiveICCLevel", &errors);
result->wPeriodicRTCUpdate = attr_as_unsigned(node, "wPeriodicRTCUpdate", &errors);
result->bConfigDescrLock = attr_as_unsigned(node, "bConfigDescrLock", &errors) != 0;
if (errors) {
fprintf(stderr, "[UFS] errors while parsing common\n");
free(result);
return NULL;
}
return result;
}
struct ufs_body *ufs_parse_body(xmlNode *node) {
struct ufs_body *result;
int errors;
result = calloc(1, sizeof(struct ufs_body));
errors = 0;
result->LUNum = attr_as_unsigned(node, "LUNum", &errors);
result->bLUEnable = attr_as_unsigned(node, "bLUEnable", &errors) != 0;
result->bBootLunID = attr_as_unsigned(node, "bBootLunID", &errors);
result->size_in_kb = attr_as_unsigned(node, "size_in_kb", &errors);
result->bDataReliability = attr_as_unsigned(node, "bDataReliability", &errors);
result->bLUWriteProtect = attr_as_unsigned(node, "bLUWriteProtect", &errors);
result->bMemoryType = attr_as_unsigned(node, "bMemoryType", &errors);
result->bLogicalBlockSize = attr_as_unsigned(node, "bLogicalBlockSize", &errors);
result->bProvisioningType = attr_as_unsigned(node, "bProvisioningType", &errors);
result->wContextCapabilities = attr_as_unsigned(node, "wContextCapabilities", &errors);
result->desc = attr_as_string(node, "desc", &errors);
if (errors) {
fprintf(stderr, "[UFS] errors while parsing body\n");
free(result);
return NULL;
}
return result;
}
struct ufs_epilogue *ufs_parse_epilogue(xmlNode *node) {
struct ufs_epilogue *result;
int errors = 0;
result = calloc(1, sizeof(struct ufs_epilogue));
result->LUNtoGrow = attr_as_unsigned(node, "LUNtoGrow", &errors);
if (errors) {
fprintf(stderr, "[UFS] errors while parsing epilogue\n");
free(result);
return NULL;
}
return result;
}
int ufs_load(const char *ufs_file, bool finalize_provisioning) {
xmlNode *node;
xmlNode *root;
xmlDoc *doc;
int retval = 0;
struct ufs_body *ufs_body_tmp;
if (ufs_common_p) {
fprintf(stderr,
"Only one UFS provisioning XML allowed, %s ignored\n",
ufs_file);
return -EEXIST;
}
doc = xmlReadFile(ufs_file, NULL, 0);
if (!doc) {
fprintf(stderr, "[UFS] failed to parse %s\n", ufs_file);
return -EINVAL;
}
root = xmlDocGetRootElement(doc);
for (node = root->children; node; node = node->next) {
if (node->type != XML_ELEMENT_NODE)
continue;
if (xmlStrcmp(node->name, (xmlChar *) "ufs")) {
fprintf(stderr, "[UFS] unrecognized tag \"%s\", ignoring\n",
node->name);
continue;
}
if (xmlGetProp(node, (xmlChar *) "bNumberLU")) {
if (!ufs_common_p) {
ufs_common_p = ufs_parse_common_params(node,
finalize_provisioning);
} else {
fprintf(stderr, "[UFS] Only one common tag is allowed\n"
"[UFS] provisioning aborted\n");
retval = -EINVAL;
break;
}
if (!ufs_common_p) {
fprintf(stderr, "[UFS] Common tag corrupted\n"
"[UFS] provisioning aborted\n");
retval = -EINVAL;
break;
}
} else if (xmlGetProp(node, (xmlChar *) "LUNum")) {
ufs_body_tmp = ufs_parse_body(node);
if (ufs_body_tmp) {
if (ufs_body_p) {
ufs_body_last->next = ufs_body_tmp;
ufs_body_last = ufs_body_tmp;
} else {
ufs_body_p = ufs_body_tmp;
ufs_body_last = ufs_body_tmp;
}
} else {
fprintf(stderr, "[UFS] LU tag corrupted\n"
"[UFS] provisioning aborted\n");
retval = -EINVAL;
break;
}
} else if (xmlGetProp(node, (xmlChar *) "commit")) {
if (!ufs_epilogue_p) {
ufs_epilogue_p = ufs_parse_epilogue(node);
if (ufs_epilogue_p)
continue;
} else {
fprintf(stderr, "[UFS] Only one finalizing tag is allowed\n"
"[UFS] provisioning aborted\n");
retval = -EINVAL;
break;
}
if (!ufs_epilogue_p) {
fprintf(stderr, "[UFS] Finalizing tag corrupted\n"
"[UFS] provisioning aborted\n");
retval = -EINVAL;
break;
}
} else {
fprintf(stderr, "[UFS] Unknown tag or %s corrupted\n"
"[UFS] provisioning aborted\n", ufs_file);
retval = -EINVAL;
break;
}
}
xmlFreeDoc(doc);
if (!retval && (!ufs_common_p || !ufs_body_p || !ufs_epilogue_p)) {
fprintf(stderr, "[UFS] %s seems to be incomplete\n"
"[UFS] provisioning aborted\n", ufs_file);
retval = -EINVAL;
}
if (retval) {
if (ufs_common_p) {
free(ufs_common_p);
}
if (ufs_body_p) {
free(ufs_body_p);
}
if (ufs_epilogue_p) {
free(ufs_epilogue_p);
}
fprintf(stderr, "[UFS] %s seems to be corrupted, ignore\n", ufs_file);
return retval;
}
if (!finalize_provisioning != !ufs_common_p->bConfigDescrLock) {
fprintf(stderr,
"[UFS] Value bConfigDescrLock %d in file %s don't match command line parameter --finalize-provisioning %d\n"
"[UFS] provisioning aborted\n",
ufs_common_p->bConfigDescrLock, ufs_file, finalize_provisioning);
fprintf(stderr, notice_b_config_descr_lock);
return -EINVAL;
}
return 0;
}
int ufs_provisioning_execute(struct qdl_device *ctx,
int (*apply_ufs_common)(struct qdl_device *, struct ufs_common *),
int (*apply_ufs_body)(struct qdl_device *, struct ufs_body *),
int (*apply_ufs_epilogue)(struct qdl_device *, struct ufs_epilogue *, bool)) {
int ret;
struct ufs_body *body;
if (ufs_common_p->bConfigDescrLock) {
int i;
printf("Attention!\nIrreversible provisioning will start in 5 s\n");
for (i = 5; i > 0; i--) {
printf(".\a");
sleep(1);
}
printf("\n");
}
// Just ask a target to check the XML w/o real provisioning
ret = apply_ufs_common(ctx, ufs_common_p);
if (ret)
return ret;
for (body = ufs_body_p; body; body = body->next) {
ret = apply_ufs_body(ctx, body);
if (ret)
return ret;
}
ret = apply_ufs_epilogue(ctx, ufs_epilogue_p, false);
if (ret) {
fprintf(stderr,
"UFS provisioning impossible, provisioning XML may be corrupted\n");
return ret;
}
// Real provisioning -- target didn't refuse a given XML
ret = apply_ufs_common(ctx, ufs_common_p);
if (ret)
return ret;
for (body = ufs_body_p; body; body = body->next) {
ret = apply_ufs_body(ctx, body);
if (ret)
return ret;
}
return apply_ufs_epilogue(ctx, ufs_epilogue_p, true);
}