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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
/*
* This file is part of libsamsung-ipc.
*
* Copyright (C) 2012 Alexander Tarasikov <alexander.tarasikov@gmail.com>
* Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr>
*
* Based on the incomplete C++ implementation which is:
* Copyright (C) 2012 Sergey Gridasov <grindars@gmail.com>
*
* libsamsung-ipc 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 2 of the License, or
* (at your option) any later version.
*
* libsamsung-ipc 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 libsamsung-ipc. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/select.h>
#include <samsung-ipc.h>
#include <utils.h>
#include "xmm6260.h"
#include "xmm6260_mipi.h"
int xmm6260_mipi_crc_calculate(void *buffer, int length)
{
unsigned char crc;
int mipi_crc;
crc = xmm6260_crc_calculate(buffer, length);
mipi_crc = (crc << 24) | 0xffffff;
return mipi_crc;
}
int xmm6260_mipi_ack_read(int device_fd, unsigned short ack)
{
struct timeval timeout;
fd_set fds;
unsigned int value;
int rc;
int i;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(device_fd, &fds);
for (i = 0; i < 50; i++) {
rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
if (rc <= 0)
return -1;
value = 0;
rc = read(device_fd, &value, sizeof(value));
if (rc < (int) sizeof(value))
continue;
if ((value & 0xffff) == ack)
return 0;
}
return -1;
}
int xmm6260_mipi_psi_send(struct ipc_client *client, int device_fd,
void *psi_data, unsigned short psi_size)
{
struct xmm6260_mipi_psi_header psi_header;
char at[] = XMM6260_AT;
int psi_crc;
struct timeval timeout;
fd_set fds;
int wc;
unsigned char *p;
int length;
int rc;
int i;
if (client == NULL || device_fd < 0 || psi_data == NULL || psi_size == 0)
return -1;
FD_ZERO(&fds);
i = 0;
length = strlen(at);
do {
FD_SET(device_fd, &fds);
timeout.tv_sec = 0;
timeout.tv_usec = 100000;
rc = write(device_fd, at, length);
if (rc < length) {
ipc_client_log(client, "Writing ATAT in ASCII failed");
goto error;
}
ipc_client_log(client, "Wrote ATAT in ASCII");
rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
if (rc < 0) {
ipc_client_log(client, "Waiting for bootup failed");
goto error;
}
if (i++ > 50) {
ipc_client_log(client, "Waiting for bootup failed");
goto error;
}
} while(rc == 0);
rc = xmm6260_mipi_ack_read(device_fd, XMM6260_MIPI_BOOT0_ACK);
if (rc < 0) {
ipc_client_log(client, "Reading boot ACK failed");
goto error;
}
psi_header.padding = XMM6260_PSI_PADDING;
psi_header.length = ((psi_size >> 8) & 0xff) | ((psi_size & 0xff) << 8);
psi_header.magic = XMM6260_PSI_MAGIC;
rc = write(device_fd, &psi_header, sizeof(psi_header));
if (rc < (int) sizeof(psi_header)) {
ipc_client_log(client, "Writing PSI header failed");
goto error;
}
ipc_client_log(client, "Wrote PSI header");
p = (unsigned char *) psi_data;
wc = 0;
while (wc < psi_size) {
rc = write(device_fd, (void *) p, psi_size - wc);
if (rc < 0) {
ipc_client_log(client, "Writing PSI failed");
goto error;
}
p += rc;
wc += rc;
}
psi_crc = xmm6260_mipi_crc_calculate(psi_data, psi_size);
ipc_client_log(client, "Wrote PSI, CRC is 0x%x", psi_crc);
rc = write(device_fd, &psi_crc, sizeof(psi_crc));
if (rc < (int) sizeof(psi_crc)) {
ipc_client_log(client, "Writing PSI CRC failed");
goto error;
}
ipc_client_log(client, "Wrote PSI CRC (0x%x)", psi_crc);
rc = xmm6260_mipi_ack_read(device_fd, XMM6260_MIPI_PSI_ACK);
if (rc < 0) {
ipc_client_log(client, "Reading PSI ACK failed");
goto error;
}
rc = 0;
goto complete;
error:
rc = -1;
complete:
return rc;
}
int xmm6260_mipi_ebl_send(struct ipc_client *client, int device_fd,
void *ebl_data, int ebl_size)
{
unsigned short boot_magic[4];
unsigned char ebl_crc;
int chunk;
int count;
int wc;
unsigned char *p;
int length;
int rc;
if (client == NULL || device_fd < 0 || ebl_data == NULL || ebl_size <= 0)
return -1;
boot_magic[0] = 0;
boot_magic[1] = 0;
boot_magic[2] = XMM6260_MIPI_BOOT1_MAGIC;
boot_magic[3] = XMM6260_MIPI_BOOT1_MAGIC;
length = sizeof(boot_magic);
rc = write(device_fd, &length, sizeof(length));
if (rc < (int) sizeof(length)) {
ipc_client_log(client, "Writing boot magic length failed");
goto error;
}
rc = write(device_fd, &boot_magic, length);
if (rc < length) {
ipc_client_log(client, "Writing boot magic failed");
goto error;
}
ipc_client_log(client, "Wrote boot magic");
rc = xmm6260_mipi_ack_read(device_fd, XMM6260_MIPI_BOOT1_ACK);
if (rc < 0) {
ipc_client_log(client, "Reading boot magic ACK failed");
goto error;
}
length = sizeof(ebl_size);
rc = write(device_fd, &length, sizeof(length));
if (rc < (int) sizeof(length)) {
ipc_client_log(client, "Writing EBL size length failed");
goto error;
}
rc = write(device_fd, &ebl_size, length);
if (rc < length) {
ipc_client_log(client, "Writing EBL size failed");
goto error;
}
ipc_client_log(client, "Wrote EBL size");
rc = xmm6260_mipi_ack_read(device_fd, XMM6260_MIPI_EBL_SIZE_ACK);
if (rc < 0) {
ipc_client_log(client, "Reading EBL size ACK failed");
goto error;
}
ebl_size++;
rc = write(device_fd, &ebl_size, length);
if (rc < length) {
ipc_client_log(client, "Writing EBL size failed");
goto error;
}
ebl_size--;
p = (unsigned char *) ebl_data;
chunk = XMM6260_MIPI_EBL_CHUNK;
wc = 0;
while (wc < ebl_size) {
count = chunk < ebl_size - wc ? chunk : ebl_size - wc;
rc = write(device_fd, (void *) p, count);
if (rc < 0) {
ipc_client_log(client, "Writing EBL failed");
goto error;
}
p += rc;
wc += rc;
}
ebl_crc = xmm6260_crc_calculate(ebl_data, ebl_size);
ipc_client_log(client, "Wrote EBL, CRC is 0x%x", ebl_crc);
rc = write(device_fd, &ebl_crc, sizeof(ebl_crc));
if (rc < (int) sizeof(ebl_crc)) {
ipc_client_log(client, "Writing EBL CRC failed");
goto error;
}
ipc_client_log(client, "Wrote EBL CRC (0x%x)", ebl_crc);
rc = xmm6260_mipi_ack_read(device_fd, XMM6260_MIPI_EBL_ACK);
if (rc < 0) {
ipc_client_log(client, "Reading EBL ACK failed");
goto error;
}
rc = 0;
goto complete;
error:
rc = -1;
complete:
return rc;
}
int xmm6260_mipi_command_send(int device_fd, unsigned short code,
void *data, int size, int ack, int short_footer)
{
struct xmm6260_mipi_command_header header;
struct xmm6260_mipi_command_footer footer;
int footer_size;
void *buffer = NULL;
int length;
struct timeval timeout;
fd_set fds;
int chunk;
int c;
unsigned char *p;
int rc;
int i;
if (device_fd < 0 || data == NULL || size <= 0)
return -1;
header.size = size + sizeof(header);
header.magic = XMM6260_MIPI_COMMAND_HEADER_MAGIC;
header.code = code;
header.data_size = size;
footer.checksum = (size & 0xffff) + code;
footer.magic = XMM6260_MIPI_COMMAND_FOOTER_MAGIC;
footer.unknown = XMM6260_MIPI_COMMAND_FOOTER_UNKNOWN;
p = (unsigned char *) data;
for (i = 0; i < size; i++)
footer.checksum += *p++;
footer_size = sizeof(footer);
if (short_footer)
footer_size -= sizeof(short);
length = sizeof(header) + size + footer_size;
buffer = malloc(length);
p = (unsigned char *) buffer;
memcpy(p, &header, sizeof(header));
p += sizeof(header);
memcpy(p, data, size);
p += size;
memcpy(p, &footer, footer_size);
rc = write(device_fd, buffer, length);
if (rc < length)
goto error;
free(buffer);
buffer = NULL;
if (!ack) {
rc = 0;
goto complete;
}
FD_ZERO(&fds);
FD_SET(device_fd, &fds);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
if (rc <= 0)
goto error;
rc = read(device_fd, &length, sizeof(length));
if (rc < (int) sizeof(length) || length <= 0)
goto error;
length += sizeof(unsigned int);
if (length % 4 != 0)
length += length % 4;
if (length < (int) sizeof(buffer))
goto error;
buffer = malloc(length);
p = (unsigned char *) buffer;
memcpy(p, &length, sizeof(length));
p += sizeof(length);
chunk = 4;
c = sizeof(length);
while (c < length) {
rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
if (rc <= 0)
goto error;
rc = read(device_fd, (void *) p, chunk);
if (rc < chunk)
goto error;
p += rc;
c += rc;
}
memcpy(&header, buffer, sizeof(header));
if (header.code != code)
goto error;
rc = 0;
goto complete;
error:
rc = -1;
complete:
if (buffer != NULL)
free(buffer);
return rc;
}
int xmm6260_mipi_modem_data_send(int device_fd, void *data, int size, int address)
{
int chunk;
int count;
int c;
unsigned char *p;
int rc;
if (device_fd < 0 || data == NULL || size <= 0)
return -1;
rc = xmm6260_mipi_command_send(device_fd, XMM6260_COMMAND_FLASH_SET_ADDRESS, &address, sizeof(address), 1, 0);
if (rc < 0)
goto error;
p = (unsigned char *) data;
chunk = XMM6260_MIPI_MODEM_DATA_CHUNK;
c = 0;
while (c < size) {
count = chunk < size - c ? chunk : size - c;
rc = xmm6260_mipi_command_send(device_fd, XMM6260_COMMAND_FLASH_WRITE_BLOCK, p, count, 1, 1);
if (rc < 0)
goto error;
p += count;
c += count;
}
rc = 0;
goto complete;
error:
rc = -1;
complete:
return rc;
}
int xmm6260_mipi_port_config_send(struct ipc_client *client, int device_fd)
{
void *buffer = NULL;
int length;
struct timeval timeout;
fd_set fds;
int chunk;
int count;
int c;
unsigned char *p;
int rc;
if (client == NULL || device_fd < 0)
return -1;
FD_ZERO(&fds);
FD_SET(device_fd, &fds);
timeout.tv_sec = 2;
timeout.tv_usec = 0;
rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
if (rc <= 0)
goto error;
rc = read(device_fd, &length, sizeof(length));
if (rc < (int) sizeof(length) || length <= 0) {
ipc_client_log(client, "Reading port config length failed");
goto error;
}
ipc_client_log(client, "Read port config length (0x%x)", length);
buffer = malloc(length);
p = (unsigned char *) buffer;
chunk = 4;
c = 0;
while (c < length) {
count = chunk < length - c ? chunk : length - c;
rc = select(device_fd + 1, &fds, NULL, NULL, &timeout);
if (rc <= 0)
goto error;
rc = read(device_fd, p, count);
if (rc < count) {
ipc_client_log(client, "Reading port config failed");
goto error;
}
p += count;
c += count;
}
ipc_client_log(client, "Read port config");
rc = xmm6260_mipi_command_send(device_fd, XMM6260_COMMAND_SET_PORT_CONFIG, buffer, length, 1, 0);
if (rc < 0) {
ipc_client_log(client, "Sending port config command failed");
goto error;
}
rc = 0;
goto complete;
error:
rc = -1;
complete:
if (buffer != NULL)
free(buffer);
return rc;
}
int xmm6260_mipi_sec_start_send(struct ipc_client *client, int device_fd,
void *sec_data, int sec_size)
{
int rc;
if (client == NULL || device_fd < 0 || sec_data == NULL || sec_size <= 0)
return -1;
rc = xmm6260_mipi_command_send(device_fd, XMM6260_COMMAND_SEC_START, sec_data, sec_size, 1, 0);
if (rc < 0)
return -1;
return 0;
}
int xmm6260_mipi_sec_end_send(struct ipc_client *client, int device_fd)
{
unsigned short sec_data;
int sec_size;
int rc;
if (client == NULL || device_fd < 0)
return -1;
sec_data = XMM6260_SEC_END_MAGIC;
sec_size = sizeof(sec_data);
rc = xmm6260_mipi_command_send(device_fd, XMM6260_COMMAND_SEC_END, &sec_data, sec_size, 1, 1);
if (rc < 0)
return -1;
return 0;
}
int xmm6260_mipi_firmware_send(struct ipc_client *client, int device_fd,
void *firmware_data, int firmware_size)
{
int rc;
if (client == NULL || device_fd < 0 || firmware_data == NULL || firmware_size <= 0)
return -1;
rc = xmm6260_mipi_modem_data_send(device_fd, firmware_data, firmware_size, XMM6260_FIRMWARE_ADDRESS);
if (rc < 0)
return -1;
return 0;
}
int xmm6260_mipi_nv_data_send(struct ipc_client *client, int device_fd)
{
void *nv_data = NULL;
int nv_size;
int rc;
if (client == NULL || device_fd < 0)
return -1;
nv_size = ipc_client_nv_data_size(client);
nv_data = ipc_nv_data_load(client);
if (nv_data == NULL) {
ipc_client_log(client, "Loading nv_data failed");
goto error;
}
ipc_client_log(client, "Loaded nv_data");
rc = xmm6260_mipi_modem_data_send(device_fd, nv_data, nv_size, XMM6260_NV_DATA_ADDRESS);
if (rc < 0)
goto error;
rc = 0;
goto complete;
error:
rc = -1;
complete:
if (nv_data != NULL)
free(nv_data);
return rc;
}
int xmm6260_mipi_mps_data_send(struct ipc_client *client, int device_fd,
void *mps_data, int mps_size)
{
int rc;
if (client == NULL || device_fd < 0 || mps_data == NULL || mps_size <= 0)
return -1;
rc = xmm6260_mipi_modem_data_send(device_fd, mps_data, mps_size, XMM6260_MPS_DATA_ADDRESS);
if (rc < 0)
return -1;
return 0;
}
int xmm6260_mipi_hw_reset_send(struct ipc_client *client, int device_fd)
{
unsigned int hw_reset_data;
int hw_reset_size;
int rc;
if (client == NULL || device_fd < 0)
return -1;
hw_reset_data = XMM6260_HW_RESET_MAGIC;
hw_reset_size = sizeof(hw_reset_data);
rc = xmm6260_mipi_command_send(device_fd, XMM6260_COMMAND_HW_RESET, &hw_reset_data, hw_reset_size, 0, 1);
if (rc < 0)
return -1;
return 0;
}
// vim:ts=4:sw=4:expandtab
|