aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/device/galaxys2/galaxys2_loader.c
blob: 03dca939495acc850dc56a0d7fbba75ac63e9f11 (plain)
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*
 * Firmware loader for Samsung I9100 (galaxys2)
 * Copyright (C) 2012 Alexander Tarasikov <alexander.tarasikov@gmail.com>
 * Copyright (C) 2012 Paul Kocialkowski <contact@paulk.fr>
 *
 * based on the incomplete C++ implementation which is
 * Copyright (C) 2012 Sergey Gridasov <grindars@gmail.com>
 *
 * 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 2 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

//for timeval
#include <sys/time.h>

//for mmap
#include <sys/mman.h>
#include <sys/stat.h>

#include "ipc.h"

#include "galaxys2_loader.h"
#include "xmm6260_loader.h"
#include "xmm6260_modemctl.h"
#include "modem_prj.h"

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

/*
 * Locations of the firmware components in the Samsung firmware
 */

struct xmm6260_radio_part galaxys2_radio_parts[] = {
    [PSI] = {
        .offset = 0,
        .length = 0xf000,
    },
    [EBL] = {
        .offset = 0xf000,
        .length = 0x19000,
    },
    [SECURE_IMAGE] = {
        .offset = 0x9ff800,
        .length = 0x800,
    },
    [FIRMWARE] = {
        .offset = 0x28000,
        .length = 0x9d8000,
    },
    [NVDATA] = {
        .offset = 0xa00000,
        .length = 2 << 20,
    }
};

struct galaxys2_boot_cmd_desc galaxys2_boot_cmd_desc[] = {
    [SetPortConf] = {
        .code = 0x86,
        .data_size = 0x800,
        .need_ack = 1,
    },
    [ReqSecStart] = {
        .code = 0x204,
        .data_size = 0x4000,
        .need_ack = 1,
    },
    [ReqSecEnd] = {
        .code = 0x205,
        .data_size = 0x4000,
        .need_ack = 1,
    },
    [ReqForceHwReset] = {
        .code = 0x208,
        .data_size = 0x4000,
        .need_ack = 0,
    },
    [ReqFlashSetAddress] = {
        .code = 0x802,
        .data_size = 0x4000,
        .need_ack = 1,
    },
    [ReqFlashWriteBlock] = {
        .code = 0x804,
        .data_size = 0x4000,
        .need_ack = 0,
    }
};

static int galaxys2_send_image(struct ipc_client *client,
    struct modemctl_io_data *io_data, enum xmm6260_image type)
{
    int ret = -1;

    if (type >= ARRAY_SIZE(galaxys2_radio_parts)) {
        ipc_client_log(client, "Error: bad image type %x", type);
        goto fail;
    }
    size_t length = galaxys2_radio_parts[type].length;
    size_t offset = galaxys2_radio_parts[type].offset;

    size_t start = offset;
    size_t end = length + start;

    //dump some image bytes
    ipc_client_log(client, "image start");

    while (start < end) {
        ret = write(io_data->boot_fd, io_data->radio_data + start, end - start);
        if (ret < 0) {
            ipc_client_log(client, "failed to write image chunk");
            goto fail;
        }
        start += ret;
    }

    unsigned char crc = xmm6260_crc_calculate(io_data->radio_data, offset, length);

    if ((ret = write(io_data->boot_fd, &crc, 1)) < 1) {
        ipc_client_log(client, "failed to write CRC");
        goto fail;
    }
    else {
        ipc_client_log(client, "wrote CRC %x", crc);
    }

    return 0;

fail:
    return ret;
}

static int galaxys2_send_psi(struct ipc_client *client, struct modemctl_io_data *io_data)
{
    size_t length = galaxys2_radio_parts[PSI].length;

    struct galaxys2_psi_header hdr = {
        .magic = XMM_PSI_MAGIC,
        .length = length,
        .padding = 0xff,
    };
    int ret = -1;

    if ((ret = write(io_data->boot_fd, &hdr, sizeof(hdr))) != sizeof(hdr)) {
        ipc_client_log(client, "%s: failed to write header, ret %d", __func__, ret);
        goto fail;
    }

    if ((ret = galaxys2_send_image(client, io_data, PSI)) < 0) {
        ipc_client_log(client, "Error: failed to send PSI image");
        goto fail;
    }

    int i;
    for (i = 0; i < 22; i++) {
        char ack;
        if (expect_read(io_data->boot_fd, &ack, 1) < 1) {
            ipc_client_log(client, "failed to read ACK byte %d", i);
            goto fail;
        }
    }

    if ((ret = expect_data(io_data->boot_fd, "\x1", 1)) < 0) {
        ipc_client_log(client, "failed to wait for first ACK");
        goto fail;
    }

    if ((ret = expect_data(io_data->boot_fd, "\x1", 1)) < 0) {
        ipc_client_log(client, "failed to wait for second ACK");
        goto fail;
    }

    if ((ret = expect_data(io_data->boot_fd, PSI_ACK_MAGIC, 2)) < 0) {
        ipc_client_log(client, "Error: failed to receive PSI ACK");
        goto fail;
    }
    else {
        ipc_client_log(client, "received PSI ACK");
    }

    return 0;

fail:
    return ret;
}

static int galaxys2_send_ebl(struct ipc_client *client, struct modemctl_io_data *io_data)
{
    int ret;
    int fd = io_data->boot_fd;
    unsigned length = galaxys2_radio_parts[EBL].length;

    if ((ret = write(fd, &length, sizeof(length))) < 0) {
        ipc_client_log(client, "Error: failed to write EBL length");
        goto fail;
    }

    if ((ret = expect_data(fd, EBL_HDR_ACK_MAGIC, 2)) < 0) {
        ipc_client_log(client, "Error: failed to wait for EBL header ACK");
        goto fail;
    }

    if ((ret = galaxys2_send_image(client, io_data, EBL)) < 0) {
        ipc_client_log(client, "Error: failed to send EBL image");
        goto fail;
    }

    if ((ret = expect_data(fd, EBL_IMG_ACK_MAGIC, 2)) < 0) {
        ipc_client_log(client, "Error: failed to wait for EBL image ACK");
        goto fail;
    }

    return 0;

fail:
    return ret;
}

static int galaxys2_boot_cmd(struct ipc_client *client,
    struct modemctl_io_data *io_data, enum xmm6260_boot_cmd cmd,
    void *data, size_t data_size)
{
    int ret = 0;
    char *cmd_data = 0;
    if (cmd >= ARRAY_SIZE(galaxys2_boot_cmd_desc)) {
        ipc_client_log(client, "Error: bad command %x\n", cmd);
        goto done_or_fail;
    }

    unsigned cmd_code = galaxys2_boot_cmd_desc[cmd].code;

    uint16_t magic = (data_size & 0xffff) + cmd_code;
    unsigned char *ptr = (unsigned char*)data;
    size_t i;
    for (i = 0; i < data_size; i++) {
        magic += ptr[i];
    }

    struct galaxys2_boot_cmd header = {
        .check = magic,
        .cmd = cmd_code,
        .data_size = data_size,
    };

    size_t cmd_size = galaxys2_boot_cmd_desc[cmd].data_size;
    size_t buf_size = cmd_size + sizeof(header);

    cmd_data = (char*)malloc(buf_size);
    if (!cmd_data) {
        ipc_client_log(client, "Error: failed to allocate command buffer");
        ret = -ENOMEM;
        goto done_or_fail;
    }
    memset(cmd_data, 0, buf_size);
    memcpy(cmd_data, &header, sizeof(header));
    memcpy(cmd_data + sizeof(header), data, data_size);

    if ((ret = write(io_data->boot_fd, cmd_data, buf_size)) < 0) {
        ipc_client_log(client, "Error: failed to write command to socket");
        goto done_or_fail;
    }

    if ((unsigned)ret != buf_size) {
        ipc_client_log(client, "Error: written %d bytes of %d", ret, buf_size);
        ret = -EINVAL;
        goto done_or_fail;
    }

    if (!galaxys2_boot_cmd_desc[cmd].need_ack) {
        ret = 0;
        goto done_or_fail;
    }

    struct galaxys2_boot_cmd ack = {
        .check = 0,
    };
    if ((ret = expect_read(io_data->boot_fd, &ack, sizeof(ack))) < 0) {
        ipc_client_log(client, "Error: failed to receive ack for cmd %x", header.cmd);
        goto done_or_fail;
    }

    if (ret != sizeof(ack)) {
        ipc_client_log(client, "Error: received %x bytes of %x for ack", ret, sizeof(ack));
        ret = -EINVAL;
        goto done_or_fail;
    }

    if (ack.cmd != header.cmd) {
        ipc_client_log(client, "Error: ack cmd %x does not match request %x", ack.cmd, header.cmd);
        ret = -EINVAL;
        goto done_or_fail;
    }

    if ((ret = expect_read(io_data->boot_fd, cmd_data, cmd_size)) < 0) {
        ipc_client_log(client, "Error: failed to receive reply data");
        goto done_or_fail;
    }

    if ((unsigned)ret != cmd_size) {
        ipc_client_log(client, "Error: received %x bytes of %x for reply data", ret, cmd_size);
        ret = -EINVAL;
        goto done_or_fail;
    }

done_or_fail:

    if (cmd_data) {
        free(cmd_data);
    }

    return ret;
}

static int galaxys2_boot_info_ack(struct ipc_client *client,
    struct modemctl_io_data *io_data)
{
    int ret;
    struct galaxys2_boot_info info;

    if ((ret = expect_read(io_data->boot_fd, &info, sizeof(info))) != sizeof(info)) {
        ipc_client_log(client, "Error: failed to receive Boot Info ret=%d", ret);
        ret = -1;
        goto fail;
    }
    else {
        ipc_client_log(client, "received Boot Info");
    }

    if ((ret = galaxys2_boot_cmd(client, io_data, SetPortConf, &info, sizeof(info))) < 0) {
        ipc_client_log(client, "Error: failed to send SetPortConf command");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent SetPortConf command");
    }

    return 0;

fail:
    return ret;
}

static int galaxys2_send_image_data(struct ipc_client *client,
    struct modemctl_io_data *io_data, uint32_t addr,
    void *data, int data_len)
{
    int ret = 0;
    int count = 0;
    char *data_p = (char *) data;

    if ((ret = galaxys2_boot_cmd(client, io_data, ReqFlashSetAddress, &addr, 4)) < 0) {
        ipc_client_log(client, "Error: failed to send ReqFlashSetAddress");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent ReqFlashSetAddress");
    }

    while (count < data_len) {
        int rest = data_len - count;
        int chunk = rest < SEC_DOWNLOAD_CHUNK ? rest : SEC_DOWNLOAD_CHUNK;

        ret = galaxys2_boot_cmd(client, io_data, ReqFlashWriteBlock, data_p, chunk);
        if (ret < 0) {
            ipc_client_log(client, "Error: failed to send data chunk");
            goto fail;
        }

        data_p += chunk;
        count += chunk;
    }

    usleep(SEC_DOWNLOAD_DELAY_US);

fail:
    return ret;
}

static int galaxys2_send_image_addr(struct ipc_client *client,
    struct modemctl_io_data *io_data, uint32_t addr, enum xmm6260_image type)
{
    uint32_t offset = galaxys2_radio_parts[type].offset;
    uint32_t length = galaxys2_radio_parts[type].length;
    char *start = io_data->radio_data + offset;
    int ret = 0;

    ret = galaxys2_send_image_data(client, io_data, addr, start, length);

    return ret;
}

static int galaxys2_send_secure_images(struct ipc_client *client,
    struct modemctl_io_data *io_data)
{
    int ret = 0;

    uint32_t sec_off = galaxys2_radio_parts[SECURE_IMAGE].offset;
    uint32_t sec_len = galaxys2_radio_parts[SECURE_IMAGE].length;
    void *sec_img = io_data->radio_data + sec_off;
    void *nv_data = NULL;

    if ((ret = galaxys2_boot_cmd(client, io_data, ReqSecStart, sec_img, sec_len)) < 0) {
        ipc_client_log(client, "Error: failed to write ReqSecStart");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent ReqSecStart");
    }

    if ((ret = galaxys2_send_image_addr(client, io_data, FW_LOAD_ADDR, FIRMWARE)) < 0) {
        ipc_client_log(client, "Error: failed to send FIRMWARE image");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent FIRMWARE image");
    }

    if (nv_data_check(client) < 0)
        goto fail;

    if (nv_data_md5_check(client) < 0)
        goto fail;

    nv_data = ipc_client_file_read(client, nv_data_path(client), 2 << 20, 1024);
    if (nv_data == NULL) {
        ipc_client_log(client, "Error: failed to read NVDATA image");
        goto fail;
    }

    if ((ret = galaxys2_send_image_data(client, io_data, NVDATA_LOAD_ADDR, nv_data, 2 << 20)) < 0) {
        ipc_client_log(client, "Error: failed to send NVDATA image");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent NVDATA image");
    }

    free(nv_data);

    if ((ret = galaxys2_boot_cmd(client, io_data, ReqSecEnd,
        BL_END_MAGIC, BL_END_MAGIC_LEN)) < 0)
    {
        ipc_client_log(client, "Error: failed to write ReqSecEnd");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent ReqSecEnd");
    }

    ret = galaxys2_boot_cmd(client, io_data, ReqForceHwReset,
        BL_RESET_MAGIC, BL_RESET_MAGIC_LEN);
    if (ret < 0) {
        ipc_client_log(client, "Error: failed to write ReqForceHwReset");
        goto fail;
    }
    else {
        ipc_client_log(client, "sent ReqForceHwReset");
    }

fail:
    return ret;
}

/*
 * i9200 (Galaxy S2) board-specific code
 */

/*
 * Power management
 */
static int galaxys2_ehci_setpower(struct ipc_client *client, bool enabled) {
    int ret = -1;

    ipc_client_log(client, "%s: enabled=%d", __func__, enabled);

    int ehci_fd = open(I9100_EHCI_PATH, O_RDWR);
    if (ehci_fd < 0) {
        ipc_client_log(client, "Error: failed to open EHCI fd");
        ret = -ENODEV;
        goto fail;
    }
    else {
        ipc_client_log(client, "opened EHCI %s: fd=%d", I9100_EHCI_PATH, ehci_fd);
    }

    ret = write(ehci_fd, enabled ? "1" : "0", 1);

    //must write exactly one byte
    if (ret <= 0) {
        ipc_client_log(client, "Error: failed to set EHCI power");
    }
    else {
        ipc_client_log(client, "set EHCI power");
    }

fail:
    if (ehci_fd >= 0) {
        close(ehci_fd);
    }

    return ret;
}

static int galaxys2_modem_reboot(struct ipc_client *client,
    struct modemctl_io_data *io_data, bool hard) {
    int ret;

    //wait for link to become ready before redetection
    if (!hard) {
        if ((ret = modemctl_wait_link_ready(client, io_data)) < 0) {
            ipc_client_log(client, "Error: failed to wait for link to get ready for redetection");
            goto fail;
        }
        else {
            ipc_client_log(client, "link ready for redetection");
        }
    }

    /*
     * Disable the hardware to ensure consistent state
     */
    if (hard) {
        if ((ret = modemctl_modem_power(client, io_data, false)) < 0) {
            ipc_client_log(client, "Error: failed to disable xmm6260 power");
            goto fail;
        }
        else {
            ipc_client_log(client, "disabled xmm6260 power");
        }
    }

    if ((ret = modemctl_link_set_enabled(client, io_data, false)) < 0) {
        ipc_client_log(client, "Error: failed to disable I9100 HSIC link");
        goto fail;
    }
    else {
        ipc_client_log(client, "disabled I9100 HSIC link");
    }

    if ((ret = galaxys2_ehci_setpower(client, false)) < 0) {
        ipc_client_log(client, "Error: failed to disable I9100 EHCI");
        goto fail;
    }
    else {
        ipc_client_log(client, "disabled I9100 EHCI");
    }

    if ((ret = modemctl_link_set_active(client, io_data, false)) < 0) {
        ipc_client_log(client, "Error: failed to deactivate I9100 HSIC link");
        goto fail;
    }
    else {
        ipc_client_log(client, "deactivated I9100 HSIC link");
    }

    /*
     * Now, initialize the hardware
     */

    if ((ret = modemctl_link_set_enabled(client, io_data, true)) < 0) {
        ipc_client_log(client, "Error: failed to enable I9100 HSIC link");
        goto fail;
    }
    else {
        ipc_client_log(client, "enabled I9100 HSIC link");
    }

    if ((ret = galaxys2_ehci_setpower(client, true)) < 0) {
        ipc_client_log(client, "Error: failed to enable I9100 EHCI");
        goto fail;
    }
    else {
        ipc_client_log(client, "enabled I9100 EHCI");
    }

    if ((ret = modemctl_link_set_active(client, io_data, true)) < 0) {
        ipc_client_log(client, "Error: failed to activate I9100 HSIC link");
        goto fail;
    }
    else {
        ipc_client_log(client, "activated I9100 HSIC link");
    }

    if (hard) {
        if ((ret = modemctl_modem_power(client, io_data, true)) < 0) {
            ipc_client_log(client, "Error: failed to enable xmm6260 power");
            goto fail;
        }
        else {
            ipc_client_log(client, "enabled xmm6260 power");
        }
    }

    if ((ret = modemctl_wait_link_ready(client, io_data)) < 0) {
        ipc_client_log(client, "Error: failed to wait for link to get ready");
        goto fail;
    }
    else {
        ipc_client_log(client, "link ready");
    }

fail:
    return ret;
}

int galaxys2_ipc_bootstrap(struct ipc_client *client) {
    int ret = 0;
    struct modemctl_io_data io_data;
    memset(&io_data, 0, sizeof(client, io_data));

    io_data.radio_fd = open(RADIO_IMAGE, O_RDONLY);
    if (io_data.radio_fd < 0) {
        ipc_client_log(client, "Error: failed to open radio firmware");
        goto fail;
    }
    else {
        ipc_client_log(client, "opened radio image %s, fd=%d", RADIO_IMAGE, io_data.radio_fd);
    }

    if (fstat(io_data.radio_fd, &io_data.radio_stat) < 0) {
        ipc_client_log(client, "Error: failed to stat radio image, error %s", strerror(errno));
        goto fail;
    }

    io_data.radio_data = mmap(0, RADIO_MAP_SIZE, PROT_READ, MAP_SHARED,
        io_data.radio_fd, 0);
    if (io_data.radio_data == MAP_FAILED) {
        ipc_client_log(client, "Error: failed to mmap radio image, error %s", strerror(errno));
        goto fail;
    }

    io_data.boot_fd = open(BOOT_DEV, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (io_data.boot_fd < 0) {
        ipc_client_log(client, "Error: failed to open boot device");
        goto fail;
    }
    else {
        ipc_client_log(client, "opened boot device %s, fd=%d", BOOT_DEV, io_data.boot_fd);
    }

    io_data.link_fd = open(LINK_PM, O_RDWR);
    if (io_data.link_fd < 0) {
        ipc_client_log(client, "Error: failed to open link device");
        goto fail;
    }
    else {
        ipc_client_log(client, "opened link device %s, fd=%d", LINK_PM, io_data.link_fd);
    }

    if (galaxys2_modem_reboot(client, &io_data, true)) {
        ipc_client_log(client, "Error: failed to hard reset modem");
        goto fail;
    }
    else {
        ipc_client_log(client, "modem hard reset done");
    }

    /*
     * Now, actually load the firmware
     */
    if (write(io_data.boot_fd, "ATAT", 4) != 4) {
        ipc_client_log(client, "Error: failed to write ATAT to boot socket");
        goto fail;
    }
    else {
        ipc_client_log(client, "written ATAT to boot socket, waiting for ACK");
    }

    char buf[2];
    if (expect_read(io_data.boot_fd, buf, 1) < 0) {
        ipc_client_log(client, "Error: failed to receive bootloader ACK");
        goto fail;
    }
    if (expect_read(io_data.boot_fd, buf + 1, 1) < 0) {
        ipc_client_log(client, "Error: failed to receive chip IP ACK");
        goto fail;
    }
    ipc_client_log(client, "receive ID: [%02x %02x]", buf[0], buf[1]);

    if ((ret = galaxys2_send_psi(client, &io_data)) < 0) {
        ipc_client_log(client, "Error: failed to upload PSI");
        goto fail;
    }
    else {
        ipc_client_log(client, "PSI download complete");
    }

    if ((ret = galaxys2_send_ebl(client, &io_data)) < 0) {
        ipc_client_log(client, "Error: failed to upload EBL");
        goto fail;
    }
    else {
        ipc_client_log(client, "EBL download complete");
    }

    if ((ret = galaxys2_boot_info_ack(client, &io_data)) < 0) {
        ipc_client_log(client, "Error: failed to receive Boot Info");
        goto fail;
    }
    else {
        ipc_client_log(client, "Boot Info ACK done");
    }

    if ((ret = galaxys2_send_secure_images(client, &io_data)) < 0) {
        ipc_client_log(client, "Error: failed to upload Secure Image");
        goto fail;
    }
    else {
        ipc_client_log(client, "Secure Image download complete");
    }

    usleep(POST_BOOT_TIMEOUT_US);

    if ((ret = galaxys2_modem_reboot(client, &io_data, false))) {
        ipc_client_log(client, "Error: failed to soft reset modem");
        goto fail;
    }
    else {
        ipc_client_log(client, "modem soft reset done");
    }

    ipc_client_log(client, "Modem is online!");
    ret = 0;

fail:
    if (io_data.radio_data != MAP_FAILED) {
        munmap(io_data.radio_data, RADIO_MAP_SIZE);
    }

    if (io_data.link_fd >= 0) {
        close(io_data.link_fd);
    }

    if (io_data.radio_fd >= 0) {
        close(io_data.radio_fd);
    }

    if (io_data.boot_fd >= 0) {
        close(io_data.boot_fd);
    }

    return ret;
}

// vim:ts=4:sw=4:expandtab