-
Notifications
You must be signed in to change notification settings - Fork 0
/
firehose.c
619 lines (503 loc) · 16 KB
/
firehose.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* Copyright (c) 2016-2017, Linaro Ltd.
* Copyright (c) 2018, The Linux Foundation. All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the copyright holder 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 BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "qdl.h"
#include "ufs.h"
#include "python_logging.h"
static void xml_setpropf(xmlNode *node, const char *attr, const char *fmt, ...)
{
xmlChar buf[128];
va_list ap;
va_start(ap, fmt);
vsnprintf((char*)buf, sizeof(buf), fmt, ap);
xmlSetProp(node, (xmlChar*)attr, buf);
va_end(ap);
}
static xmlNode *firehose_response_parse(const void *buf, size_t len, int *error)
{
xmlNode *node;
xmlNode *root;
xmlDoc *doc;
doc = xmlReadMemory(buf, len, NULL, NULL, 0);
if (!doc) {
log_msg(log_error, "failed to parse firehose packet\n");
*error = -EINVAL;
return NULL;
}
root = xmlDocGetRootElement(doc);
for (node = root; node; node = node->next) {
if (node->type != XML_ELEMENT_NODE)
continue;
if (xmlStrcmp(node->name, (xmlChar*)"data") == 0)
break;
}
if (!node) {
log_msg(log_error, "firehose packet without data tag\n");
*error = -EINVAL;
xmlFreeDoc(doc);
return NULL;
}
for (node = node->children; node && node->type != XML_ELEMENT_NODE; node = node->next)
;
return node;
}
static void firehose_response_log(xmlNode *node)
{
xmlChar *value;
value = xmlGetProp(node, (xmlChar*)"value");
log_msg(log_info, "LOG: %s\n", value);
}
static int firehose_read(struct qdl_device *qdl, int wait, int (*response_parser)(xmlNode *node))
{
char buf[4096];
xmlNode *nodes;
xmlNode *node;
int error;
char *msg;
char *end;
bool done = false;
int ret = -ENXIO;
int n;
int timeout = 1000;
if (wait > 0)
timeout = wait;
for (;;) {
n = qdl_read(qdl, buf, sizeof(buf), timeout);
if (n < 0) {
if (done)
break;
warn("failed to read");
return -ETIMEDOUT;
}
buf[n] = '\0';
if (qdl_debug)
log_msg(log_info, "FIREHOSE READ: %s\n", buf);
for (msg = buf; msg[0]; msg = end) {
end = strstr(msg, "</data>");
if (!end) {
log_msg(log_error, "firehose response truncated\n");
exit(1);
}
end += strlen("</data>");
nodes = firehose_response_parse(msg, end - msg, &error);
if (!nodes) {
log_msg(log_error, "unable to parse response\n");
return error;
}
for (node = nodes; node; node = node->next) {
if (xmlStrcmp(node->name, (xmlChar*)"log") == 0) {
firehose_response_log(node);
} else if (xmlStrcmp(node->name, (xmlChar*)"response") == 0) {
if (!response_parser)
log_msg(log_error, "received response with no parser\n");
else
ret = response_parser(node);
done = true;
timeout = 1;
}
}
xmlFreeDoc(nodes->doc);
}
if (wait > 0)
timeout = 100;
}
return ret;
}
static int firehose_write(struct qdl_device *qdl, xmlDoc *doc)
{
int saved_errno;
xmlChar *s;
int len;
int ret;
xmlDocDumpMemory(doc, &s, &len);
if (qdl_debug)
log_msg(log_info, "FIREHOSE WRITE: %s\n", s);
ret = qdl_write(qdl, s, len, true);
saved_errno = errno;
xmlFree(s);
return ret < 0 ? -saved_errno : 0;
}
static int firehose_nop_parser(xmlNode *node)
{
xmlChar *value;
value = xmlGetProp(node, (xmlChar*)"value");
return !!xmlStrcmp(value, (xmlChar*)"ACK");
}
static size_t max_payload_size = 1048576;
/**
* firehose_configure_response_parser() - parse a configure response
* @node: response xmlNode
*
* Return: max size supported by the remote, or negative errno on failure
*/
static int firehose_configure_response_parser(xmlNode *node)
{
xmlChar *payload;
xmlChar *value;
size_t max_size;
value = xmlGetProp(node, (xmlChar*)"value");
payload = xmlGetProp(node, (xmlChar*)"MaxPayloadSizeToTargetInBytes");
if (!value || !payload)
return -EINVAL;
max_size = strtoul((char*)payload, NULL, 10);
/*
* When receiving an ACK the remote may indicate that we should attempt
* a larger payload size
*/
if (!xmlStrcmp(value, (xmlChar*)"ACK")) {
payload = xmlGetProp(node, (xmlChar*)"MaxPayloadSizeToTargetInBytesSupported");
if (!payload)
return -EINVAL;
max_size = strtoul((char*)payload, NULL, 10);
}
return max_size;
}
static int firehose_send_configure(struct qdl_device *qdl, size_t payload_size, bool skip_storage_init, const char *storage)
{
xmlNode *root;
xmlNode *node;
xmlDoc *doc;
int ret;
doc = xmlNewDoc((xmlChar*)"1.0");
root = xmlNewNode(NULL, (xmlChar*)"data");
xmlDocSetRootElement(doc, root);
node = xmlNewChild(root, NULL, (xmlChar*)"configure", NULL);
xml_setpropf(node, "MemoryName", storage);
xml_setpropf(node, "MaxPayloadSizeToTargetInBytes", "%d", payload_size);
xml_setpropf(node, "verbose", "%d", 0);
xml_setpropf(node, "ZLPAwareHost", "%d", 1);
xml_setpropf(node, "SkipStorageInit", "%d", skip_storage_init);
ret = firehose_write(qdl, doc);
xmlFreeDoc(doc);
if (ret < 0)
return ret;
return firehose_read(qdl, -1, firehose_configure_response_parser);
}
static int firehose_configure(struct qdl_device *qdl, bool skip_storage_init, const char *storage)
{
int ret;
ret = firehose_send_configure(qdl, max_payload_size, skip_storage_init, storage);
if (ret < 0)
return ret;
/* Retry if remote proposed different size */
if (ret != max_payload_size) {
ret = firehose_send_configure(qdl, ret, skip_storage_init, storage);
if (ret < 0)
return ret;
max_payload_size = ret;
}
if (qdl_debug) {
log_msg(log_info, "[CONFIGURE] max payload size: %zu\n",
max_payload_size);
}
return 0;
}
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
static int firehose_program(struct qdl_device *qdl, struct program *program, int fd)
{
unsigned num_sectors;
struct stat sb;
size_t chunk_size;
xmlNode *root;
xmlNode *node;
xmlDoc *doc;
void *buf;
time_t t0;
time_t t;
int left;
int ret;
int n;
num_sectors = program->num_sectors;
ret = fstat(fd, &sb);
if (ret < 0)
err(1, "failed to stat \"%s\"\n", program->filename);
num_sectors = (sb.st_size + program->sector_size - 1) / program->sector_size;
if (program->num_sectors && num_sectors > program->num_sectors) {
log_msg(log_info, "[PROGRAM] %s truncated to %d\n",
program->label,
program->num_sectors * program->sector_size);
num_sectors = program->num_sectors;
}
buf = malloc(max_payload_size);
if (!buf)
err(1, "failed to allocate sector buffer");
doc = xmlNewDoc((xmlChar*)"1.0");
root = xmlNewNode(NULL, (xmlChar*)"data");
xmlDocSetRootElement(doc, root);
node = xmlNewChild(root, NULL, (xmlChar*)"program", NULL);
xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", program->sector_size);
xml_setpropf(node, "num_partition_sectors", "%d", num_sectors);
xml_setpropf(node, "physical_partition_number", "%d", program->partition);
xml_setpropf(node, "start_sector", "%s", program->start_sector);
if (program->filename)
xml_setpropf(node, "filename", "%s", program->filename);
ret = firehose_write(qdl, doc);
if (ret < 0) {
log_msg(log_error, "[PROGRAM] failed to write program command\n");
goto out;
}
ret = firehose_read(qdl, -1, firehose_nop_parser);
if (ret) {
log_msg(log_error, "[PROGRAM] failed to setup programming\n");
goto out;
}
t0 = time(NULL);
lseek(fd, program->file_offset * program->sector_size, SEEK_SET);
left = num_sectors;
while (left > 0) {
chunk_size = MIN(max_payload_size / program->sector_size, left);
n = read(fd, buf, chunk_size * program->sector_size);
if (n < 0)
err(1, "failed to read");
if (n < max_payload_size)
memset(buf + n, 0, max_payload_size - n);
n = qdl_write(qdl, buf, chunk_size * program->sector_size, true);
if (n < 0)
err(1, "failed to write");
if (n != chunk_size * program->sector_size)
err(1, "failed to write full sector");
left -= chunk_size;
}
t = time(NULL) - t0;
ret = firehose_read(qdl, -1, firehose_nop_parser);
if (ret) {
log_msg(log_error, "[PROGRAM] failed\n");
} else if (t) {
log_msg(log_info,
"[PROGRAM] flashed \"%s\" successfully at %ldkB/s\n",
program->label,
program->sector_size * num_sectors / t / 1024);
} else {
log_msg(log_info, "[PROGRAM] flashed \"%s\" successfully\n",
program->label);
}
out:
xmlFreeDoc(doc);
return ret;
}
static int firehose_apply_patch(struct qdl_device *qdl, struct patch *patch)
{
xmlNode *root;
xmlNode *node;
xmlDoc *doc;
int ret;
log_msg(log_info, "%s\n", patch->what);
doc = xmlNewDoc((xmlChar*)"1.0");
root = xmlNewNode(NULL, (xmlChar*)"data");
xmlDocSetRootElement(doc, root);
node = xmlNewChild(root, NULL, (xmlChar*)"patch", NULL);
xml_setpropf(node, "SECTOR_SIZE_IN_BYTES", "%d", patch->sector_size);
xml_setpropf(node, "byte_offset", "%d", patch->byte_offset);
xml_setpropf(node, "filename", "%s", patch->filename);
xml_setpropf(node, "physical_partition_number", "%d", patch->partition);
xml_setpropf(node, "size_in_bytes", "%d", patch->size_in_bytes);
xml_setpropf(node, "start_sector", "%s", patch->start_sector);
xml_setpropf(node, "value", "%s", patch->value);
ret = firehose_write(qdl, doc);
if (ret < 0)
goto out;
ret = firehose_read(qdl, -1, firehose_nop_parser);
if (ret)
log_msg(log_error, "[APPLY PATCH] %d\n", ret);
out:
xmlFreeDoc(doc);
return ret;
}
static int firehose_send_single_tag(struct qdl_device *qdl, xmlNode *node){
xmlNode *root;
xmlDoc *doc;
int ret;
doc = xmlNewDoc((xmlChar*)"1.0");
root = xmlNewNode(NULL, (xmlChar*)"data");
xmlDocSetRootElement(doc, root);
xmlAddChild(root, node);
ret = firehose_write(qdl, doc);
if (ret < 0)
goto out;
ret = firehose_read(qdl, -1, firehose_nop_parser);
if (ret) {
log_msg(log_error, "[UFS] %s err %d\n", __func__, ret);
ret = -EINVAL;
}
out:
xmlFreeDoc(doc);
return ret;
}
int firehose_apply_ufs_common(struct qdl_device *qdl, struct ufs_common *ufs)
{
xmlNode *node_to_send;
int ret;
node_to_send = xmlNewNode (NULL, (xmlChar*)"ufs");
xml_setpropf(node_to_send, "bNumberLU", "%d", ufs->bNumberLU);
xml_setpropf(node_to_send, "bBootEnable", "%d", ufs->bBootEnable);
xml_setpropf(node_to_send, "bDescrAccessEn", "%d", ufs->bDescrAccessEn);
xml_setpropf(node_to_send, "bInitPowerMode", "%d", ufs->bInitPowerMode);
xml_setpropf(node_to_send, "bHighPriorityLUN", "%d", ufs->bHighPriorityLUN);
xml_setpropf(node_to_send, "bSecureRemovalType", "%d", ufs->bSecureRemovalType);
xml_setpropf(node_to_send, "bInitActiveICCLevel", "%d", ufs->bInitActiveICCLevel);
xml_setpropf(node_to_send, "wPeriodicRTCUpdate", "%d", ufs->wPeriodicRTCUpdate);
xml_setpropf(node_to_send, "bConfigDescrLock", "%d", 0/*ufs->bConfigDescrLock*/); //Safety, remove before fly
ret = firehose_send_single_tag(qdl, node_to_send);
if (ret)
log_msg(log_error, "[APPLY UFS common] %d\n", ret);
return ret;
}
int firehose_apply_ufs_body(struct qdl_device *qdl, struct ufs_body *ufs)
{
xmlNode *node_to_send;
int ret;
node_to_send = xmlNewNode (NULL, (xmlChar*)"ufs");
xml_setpropf(node_to_send, "LUNum", "%d", ufs->LUNum);
xml_setpropf(node_to_send, "bLUEnable", "%d", ufs->bLUEnable);
xml_setpropf(node_to_send, "bBootLunID", "%d", ufs->bBootLunID);
xml_setpropf(node_to_send, "size_in_kb", "%d", ufs->size_in_kb);
xml_setpropf(node_to_send, "bDataReliability", "%d", ufs->bDataReliability);
xml_setpropf(node_to_send, "bLUWriteProtect", "%d", ufs->bLUWriteProtect);
xml_setpropf(node_to_send, "bMemoryType", "%d", ufs->bMemoryType);
xml_setpropf(node_to_send, "bLogicalBlockSize", "%d", ufs->bLogicalBlockSize);
xml_setpropf(node_to_send, "bProvisioningType", "%d", ufs->bProvisioningType);
xml_setpropf(node_to_send, "wContextCapabilities", "%d", ufs->wContextCapabilities);
if(ufs->desc)
xml_setpropf(node_to_send, "desc", "%s", ufs->desc);
ret = firehose_send_single_tag(qdl, node_to_send);
if (ret)
log_msg(log_error, "[APPLY UFS body] %d\n", ret);
return ret;
}
int firehose_apply_ufs_epilogue(struct qdl_device *qdl, struct ufs_epilogue *ufs,
bool commit)
{
xmlNode *node_to_send;
int ret;
node_to_send = xmlNewNode (NULL, (xmlChar*)"ufs");
xml_setpropf(node_to_send, "LUNtoGrow", "%d", ufs->LUNtoGrow);
xml_setpropf(node_to_send, "commit", "%d", commit);
ret = firehose_send_single_tag(qdl, node_to_send);
if (ret)
log_msg(log_error, "[APPLY UFS epilogue] %d\n", ret);
return ret;
}
static int firehose_set_bootable(struct qdl_device *qdl, int part)
{
xmlNode *root;
xmlNode *node;
xmlDoc *doc;
int ret;
doc = xmlNewDoc((xmlChar*)"1.0");
root = xmlNewNode(NULL, (xmlChar*)"data");
xmlDocSetRootElement(doc, root);
node = xmlNewChild(root, NULL, (xmlChar*)"setbootablestoragedrive", NULL);
xml_setpropf(node, "value", "%d", part);
ret = firehose_write(qdl, doc);
xmlFreeDoc(doc);
if (ret < 0)
return ret;
ret = firehose_read(qdl, -1, firehose_nop_parser);
if (ret) {
log_msg(log_error, "failed to mark partition %d as bootable\n", part);
return -1;
}
log_msg(log_info, "partition %d is now bootable\n", part);
return 0;
}
static int firehose_reset(struct qdl_device *qdl)
{
xmlNode *root;
xmlNode *node;
xmlDoc *doc;
int ret;
doc = xmlNewDoc((xmlChar*)"1.0");
root = xmlNewNode(NULL, (xmlChar*)"data");
xmlDocSetRootElement(doc, root);
node = xmlNewChild(root, NULL, (xmlChar*)"power", NULL);
xml_setpropf(node, "value", "reset");
ret = firehose_write(qdl, doc);
xmlFreeDoc(doc);
if (ret < 0)
return ret;
return firehose_read(qdl, -1, firehose_nop_parser);
}
int firehose_run(struct qdl_device *qdl, const char *incdir, const char *storage, void* progress_callback_context)
{
int bootable;
int ret;
/* Wait for the firehose payload to boot */
sleep(3);
firehose_read(qdl, 1000, NULL);
if(ufs_need_provisioning()) {
ret = firehose_configure(qdl, true, storage);
if (ret)
return ret;
ret = ufs_provisioning_execute(qdl, firehose_apply_ufs_common,
firehose_apply_ufs_body, firehose_apply_ufs_epilogue);
if (!ret)
log_msg(log_info, "UFS provisioning succeeded\n");
else
log_msg(log_error, "UFS provisioning failed\n");
return ret;
}
ret = firehose_configure(qdl, false, storage);
if (ret)
return ret;
ret = program_execute(qdl, firehose_program, incdir, progress_callback_context);
if (ret)
return ret;
ret = patch_execute(qdl, firehose_apply_patch);
if (ret)
return ret;
bootable = program_find_bootable_partition();
if (bootable < 0)
log_msg(log_error, "no boot partition found\n");
else
firehose_set_bootable(qdl, bootable);
firehose_reset(qdl);
return 0;
}