summaryrefslogtreecommitdiffstats
path: root/utils/imxtools/scsitools/stmp_scsi.c
blob: e1d0dc4c776a97985e3acf74d2346c6d9e8e51be (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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2012 Amaury Pouly
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#define _BSD_SOURCE
#include "stmp_scsi.h"

#define MIN(a, b) ((a) < (b) ? (a) : (b))

static int g_endian = -1;

struct stmp_device_t
{
    rb_scsi_device_t dev;
    unsigned flags;
    void *user;
    stmp_printf_t printf;
};

static inline int little_endian(void)
{
    if(g_endian == -1)
    {
        int i = 1;
        g_endian = (int)*((unsigned char *)&i) == 1;
    }
    return g_endian;
}

uint16_t stmp_fix_endian16be(uint16_t w)
{
    return little_endian() ? w << 8 | w >> 8 : w;
}

uint32_t stmp_fix_endian32be(uint32_t w)
{
    return !little_endian() ? w :
        (uint32_t)stmp_fix_endian16be(w) << 16 | stmp_fix_endian16be(w >> 16);
}

uint64_t stmp_fix_endian64be(uint64_t w)
{
    return !little_endian() ? w :
        (uint64_t)stmp_fix_endian32be(w) << 32 | stmp_fix_endian32be(w >> 32);
}

static void misc_std_printf(void *user, const char *fmt, ...)
{
    (void) user;
    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

#define stmp_printf(dev, ...) \
    dev->printf(dev->user, __VA_ARGS__)

#define stmp_debugf(dev, ...) \
    do{ if(dev->flags & STMP_DEBUG) stmp_printf(dev, __VA_ARGS__); }while(0)

stmp_device_t stmp_open(rb_scsi_device_t rdev, unsigned flags, void *user, stmp_printf_t printf)
{
    if(printf == NULL)
        printf = misc_std_printf;
    stmp_device_t sdev = malloc(sizeof(struct stmp_device_t));
    memset(sdev, 0, sizeof(struct stmp_device_t));
    sdev->dev = rdev;
    sdev->flags = flags;
    sdev->user = user;
    sdev->printf = printf;
    return sdev;
}

void stmp_close(stmp_device_t dev)
{
    free(dev);
}

/* returns <0 on error and status otherwise */
int stmp_scsi(stmp_device_t dev, uint8_t *cdb, int cdb_size, unsigned flags,
    void *sense, int *sense_size, void *buffer, int *buf_size)
{
    struct rb_scsi_raw_cmd_t cmd;
    memset(&cmd, 0, sizeof(cmd));
    cmd.dir = RB_SCSI_NONE;
    if(flags & STMP_READ)
        cmd.dir = RB_SCSI_READ;
    if(flags & STMP_WRITE)
        cmd.dir = RB_SCSI_WRITE;
    cmd.cdb = cdb;
    cmd.cdb_len = cdb_size;
    cmd.sense = sense;
    cmd.sense_len = *sense_size;
    cmd.buf = buffer;
    cmd.buf_len = *buf_size;
    cmd.tmo = 1;
    int ret = rb_scsi_raw_xfer(dev->dev, &cmd);
    *sense_size = cmd.sense_len;
    *buf_size = cmd.buf_len;
    return ret == RB_SCSI_OK || ret == RB_SCSI_SENSE ? cmd.status : -ret;
}

int stmp_sense_analysis(stmp_device_t dev, int status, uint8_t *sense, int sense_size)
{
    if(status != 0 && (dev->flags & STMP_DEBUG))
    {
        stmp_printf(dev, "Status: %d\n", status);
        stmp_printf(dev, "Sense:");
        for(int i = 0; i < sense_size; i++)
            stmp_printf(dev, " %02x", sense[i]);
        stmp_printf(dev, "\n");
        rb_scsi_decode_sense(dev->dev, sense, sense_size);
    }
    return status;
}

static int stmp_scsi_read_cmd(stmp_device_t dev, uint8_t cmd, uint8_t subcmd,
    uint8_t subsubcmd, void *buf, int *len)
{
    uint8_t cdb[16];
    memset(cdb, 0, sizeof(cdb));
    cdb[0] = SCSI_STMP_READ;
    cdb[1] = cmd;
    cdb[2] = subcmd;
    cdb[3] = subsubcmd;

    uint8_t sense[32];
    int sense_size = sizeof(sense);

    int ret = stmp_scsi(dev, cdb, sizeof(cdb), STMP_READ, sense, &sense_size, buf, len);
    if(ret < 0)
        return ret;
    ret = stmp_sense_analysis(dev, ret, sense, sense_size);
    if(ret)
        return ret;
    return 0;
}

int stmp_scsi_inquiry(stmp_device_t dev, uint8_t *dev_type, char vendor[9], char product[17])
{
    unsigned char buffer[36];
    uint8_t cdb[10];
    memset(cdb, 0, sizeof(cdb));
    cdb[0] = 0x12;
    cdb[4] = sizeof(buffer);

    uint8_t sense[32];
    int sense_size = sizeof(sense);

    int buf_sz = sizeof(buffer);
    int ret = stmp_scsi(dev, cdb, sizeof(cdb), STMP_READ, sense, &sense_size, buffer, &buf_sz);
    if(ret < 0)
        return ret;
    ret = stmp_sense_analysis(dev, ret, sense, sense_size);
    if(ret)
        return ret;
    if(buf_sz != sizeof(buffer))
        return -1;
    *dev_type = buffer[0];
    memcpy(vendor, buffer + 8, 8);
    vendor[8] = 0;
    memcpy(product, buffer + 16, 16);
    product[16] = 0;
    return 0;
}

int stmp_scsi_get_protocol_version(stmp_device_t dev, void *buf, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_PROTOCOL_VERSION, 0, 0, buf, len);
}

int stmp_get_protocol_version(stmp_device_t dev, struct scsi_stmp_protocol_version_t *ver)
{
    int len = sizeof(*ver);
    int ret = stmp_scsi_get_protocol_version(dev, ver, &len);
    if(ret || len != sizeof(*ver))
        return -1;
    return 0;
}

int stmp_scsi_get_chip_major_rev_id(stmp_device_t dev, void *buf, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_CHIP_MAJOR_REV_ID, 0, 0, buf, len);
}

int stmp_get_chip_major_rev_id(stmp_device_t dev, uint16_t *ver)
{
    int len = sizeof(*ver);
    int ret = stmp_scsi_get_chip_major_rev_id(dev, ver, &len);
    if(ret || len != sizeof(*ver))
        return -1;
    *ver = stmp_fix_endian16be(*ver);
    return 0;
}

int stmp_scsi_get_rom_rev_id(stmp_device_t dev, void *buf, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_ROM_REV_ID, 0, 0, buf, len);
}

int stmp_get_rom_rev_id(stmp_device_t dev, uint16_t *ver)
{
    int len = sizeof(*ver);
    int ret = stmp_scsi_get_rom_rev_id(dev, ver, &len);
    if(ret || len != sizeof(*ver))
        return -1;
    *ver = stmp_fix_endian16be(*ver);
    return 0;
}

int stmp_scsi_get_logical_media_info(stmp_device_t dev, uint8_t info, void *data, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_LOGICAL_MEDIA_INFO, info, 0, data, len);
}

int stmp_scsi_get_logical_table(stmp_device_t dev, int entry_count, void *buf, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_LOGICAL_TABLE, entry_count, 0, buf, len);
}

int stmp_get_logical_media_table(stmp_device_t dev, struct stmp_logical_media_table_t **table)
{
    struct scsi_stmp_logical_table_header_t header;
    int len = sizeof(header);
    int ret = stmp_scsi_get_logical_table(dev, 0, &header, &len);
    if(ret || len != sizeof(header))
        return -1;
    header.count = stmp_fix_endian16be(header.count);
    int sz = sizeof(header) + header.count * sizeof(struct scsi_stmp_logical_table_entry_t);
    len = sz;
    *table = malloc(sz);
    ret = stmp_scsi_get_logical_table(dev, header.count, &(*table)->header, &len);
    if(ret || len != sz)
        return -1;
    (*table)->header.count = stmp_fix_endian16be((*table)->header.count);
    for(unsigned i = 0; i < (*table)->header.count; i++)
        (*table)->entry[i].size = stmp_fix_endian64be((*table)->entry[i].size);
    return 0;
}

int stmp_scsi_get_logical_drive_info(stmp_device_t dev, uint8_t drive, uint8_t info, void *data, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_LOGICAL_DRIVE_INFO, drive, info, data, len);
}

int stmp_scsi_get_device_info(stmp_device_t dev, uint8_t info, void *data, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_DEVICE_INFO, info, 0, data, len);
}

int stmp_scsi_get_serial_number(stmp_device_t dev, uint8_t info, void *data, int *len)
{
    return stmp_scsi_read_cmd(dev, SCSI_STMP_CMD_GET_CHIP_SERIAL_NUMBER, info, 0, data, len);
}

int stmp_scsi_read_logical_drive_sectors(stmp_device_t dev, uint8_t drive, uint64_t address,
    uint32_t count, void *buffer, int *buffer_size)
{
    uint8_t cdb[16];
    memset(cdb, 0, sizeof(cdb));
    cdb[0] = SCSI_STMP_READ;
    cdb[1] = SCSI_STMP_CMD_READ_LOGICAL_DRIVE_SECTOR;
    cdb[2] = drive;
    address = stmp_fix_endian64be(address);
    memcpy(&cdb[3], &address, sizeof(address));
    count = stmp_fix_endian32be(count);
    memcpy(&cdb[11], &count, sizeof(count));

    uint8_t sense[32];
    int sense_size = sizeof(sense);

    int ret = stmp_scsi(dev, cdb, sizeof(cdb), STMP_READ, sense, &sense_size, buffer, buffer_size);
    if(ret < 0)
        return ret;
    return stmp_sense_analysis(dev, ret, sense, sense_size);
}

int stmp_scsi_write_logical_drive_sectors(stmp_device_t dev, uint8_t drive, uint64_t address,
    uint32_t count, void *buffer, int *buffer_size)
{
    uint8_t cdb[16];
    memset(cdb, 0, sizeof(cdb));
    cdb[0] = SCSI_STMP_WRITE;
    cdb[1] = SCSI_STMP_CMD_WRITE_LOGICAL_DRIVE_SECTOR;
    cdb[2] = drive;
    address = stmp_fix_endian64be(address);
    memcpy(&cdb[3], &address, sizeof(address));
    count = stmp_fix_endian32be(count);
    memcpy(&cdb[11], &count, sizeof(count));

    uint8_t sense[32];
    int sense_size = sizeof(sense);

    int ret = stmp_scsi(dev, cdb, sizeof(cdb), STMP_WRITE, sense, &sense_size, buffer, buffer_size);
    if(ret < 0)
        return ret;
    return stmp_sense_analysis(dev, ret, sense, sense_size);
}

int stmp_read_logical_drive_sectors(stmp_device_t dev, uint8_t drive, uint64_t address,
    uint32_t count, void *buffer, int buffer_size)
{
    int len = buffer_size;
    int ret = stmp_scsi_read_logical_drive_sectors(dev, drive, address, count, buffer, &len);
    if(ret || len != buffer_size)
        return -1;
    return 0;
}

int stmp_write_logical_drive_sectors(stmp_device_t dev, uint8_t drive, uint64_t address,
    uint32_t count, void *buffer, int buffer_size)
{
    int len = buffer_size;
    int ret = stmp_scsi_write_logical_drive_sectors(dev, drive, address, count, buffer, &len);
    if(ret || len != buffer_size)
        return -1;
    return 0;
}

#define check_len(l) if(len != l) return -1;
#define fixn(n, p) *(uint##n##_t *)(p) = stmp_fix_endian##n##be(*(uint##n##_t *)(p))
#define fix16(p) fixn(16, p)
#define fix32(p) fixn(32, p)
#define fix64(p) fixn(64, p)

int stmp_fix_logical_media_info(uint8_t info, void *data, int len)
{
    switch(info)
    {
        case SCSI_STMP_MEDIA_INFO_NR_DRIVES:
            check_len(2);
            fix16(data);
            return 0;
        case SCSI_STMP_MEDIA_INFO_TYPE:
        case SCSI_STMP_MEDIA_INFO_SERIAL_NUMBER_SIZE:
        case SCSI_STMP_MEDIA_INFO_VENDOR:
        case SCSI_STMP_MEDIA_INFO_ALLOC_UNIT_SIZE:
        case SCSI_STMP_MEDIA_INFO_PAGE_SIZE:
        case SCSI_STMP_MEDIA_INFO_NR_DEVICES:
            check_len(4);
            fix32(data);
            return 0;
        case SCSI_STMP_MEDIA_INFO_SIZE:
        case SCSI_STMP_MEDIA_INFO_NAND_ID:
            check_len(8);
            fix64(data);
            return 0;
        case SCSI_STMP_MEDIA_INFO_IS_INITIALISED:
        case SCSI_STMP_MEDIA_INFO_STATE:
        case SCSI_STMP_MEDIA_INFO_IS_WRITE_PROTECTED:
        case SCSI_STMP_MEDIA_INFO_IS_SYSTEM_MEDIA:
        case SCSI_STMP_MEDIA_INFO_IS_MEDIA_PRESENT:
            check_len(1);
            return 0;
        case SCSI_STMP_MEDIA_INFO_SERIAL_NUMBER:
            return 0;
        default:
            return -1;
    }
}

static void stmp_fix_version(struct scsi_stmp_logical_drive_info_version_t *w)
{
    w->major = stmp_fix_endian16be(w->major);
    w->minor = stmp_fix_endian16be(w->minor);
    w->revision = stmp_fix_endian16be(w->revision);
}

int stmp_fix_logical_drive_info(uint8_t info, void *data, int len)
{
    switch(info)
    {
        case SCSI_STMP_DRIVE_INFO_SERIAL_NUMBER_SIZE:
            check_len(2);
            fix16(data);
            return 0;
        case SCSI_STMP_DRIVE_INFO_SECTOR_SIZE:
        case SCSI_STMP_DRIVE_INFO_ERASE_SIZE:
        case SCSI_STMP_DRIVE_INFO_SIZE_MEGA:
        case SCSI_STMP_DRIVE_INFO_TYPE:
        case SCSI_STMP_DRIVE_INFO_SECTOR_ALLOCATION:
            check_len(4);
            fix32(data);
            return 0;
        case SCSI_STMP_DRIVE_INFO_SIZE:
        case SCSI_STMP_DRIVE_INFO_SECTOR_COUNT:
            check_len(8);
            fix64(data);
            return 0;
        case SCSI_STMP_DRIVE_INFO_TAG:
        case SCSI_STMP_DRIVE_INFO_IS_WRITE_PROTETED:
        case SCSI_STMP_DRIVE_INFO_MEDIA_PRESENT:
        case SCSI_STMP_DRIVE_INFO_MEDIA_CHANGE:
            check_len(1);
            return 0;
        case SCSI_STMP_DRIVE_INFO_COMPONENT_VERSION:
        case SCSI_STMP_DRIVE_INFO_PROJECT_VERSION:
            check_len(6)
            stmp_fix_version(data);
            return 0;
        case SCSI_STMP_DRIVE_INFO_SERIAL_NUMBER:
            return 0;
        default:
            return -1;
    }
}

int stmp_fix_device_info(uint8_t info, void *data, int len)
{
    switch(info)
    {
        case 0: case 1:
            check_len(4);
            fix32(data);
            return 0;
        default:
            return -1;
    }
}
#undef fix64
#undef fix32
#undef fix16
#undef fixn
#undef checl_len

const char *stmp_get_logical_media_type_string(uint32_t type)
{
    switch(type)
    {
        case SCSI_STMP_MEDIA_TYPE_NAND: return "NAND";
        case SCSI_STMP_MEDIA_TYPE_SDMMC: return "SD/MMC";
        case SCSI_STMP_MEDIA_TYPE_HDD: return "HDD";
        case SCSI_STMP_MEDIA_TYPE_RAM: return "RAM";
        case SCSI_STMP_MEDIA_TYPE_iNAND: return "iNAND";
        default: return "?";
    }
}

const char *stmp_get_logical_media_vendor_string(uint32_t type)
{
    switch(type)
    {
        case SCSI_STMP_MEDIA_VENDOR_SAMSUNG: return "Samsung";
        case SCSI_STMP_MEDIA_VENDOR_STMICRO: return "ST Micro";
        case SCSI_STMP_MEDIA_VENDOR_HYNIX: return "Hynix";
        case SCSI_STMP_MEDIA_VENDOR_MICRON: return "Micron";
        case SCSI_STMP_MEDIA_VENDOR_TOSHIBA: return "Toshiba";
        case SCSI_STMP_MEDIA_VENDOR_RENESAS: return "Renesas";
        case SCSI_STMP_MEDIA_VENDOR_INTEL: return "Intel";
        case SCSI_STMP_MEDIA_VENDOR_SANDISK: return "Sandisk";
        default: return "?";
    }
}

const char *stmp_get_logical_drive_type_string(uint32_t type)
{
    switch(type)
    {
        case SCSI_STMP_DRIVE_TYPE_DATA: return "Data";
        case SCSI_STMP_DRIVE_TYPE_SYSTEM: return "System";
        case SCSI_STMP_DRIVE_TYPE_HIDDEN: return "Hidden";
        case SCSI_STMP_DRIVE_TYPE_UNKNOWN: return "Unknown";
        default: return "?";
    }
}

const char *stmp_get_logical_drive_tag_string(uint32_t type)
{
    switch(type)
    {
        case SCSI_STMP_DRIVE_TAG_STMPSYS_S: return "System";
        case SCSI_STMP_DRIVE_TAG_HOSTLINK_S: return "Hostlink";
        case SCSI_STMP_DRIVE_TAG_RESOURCE_BIN: return "Resource";
        case SCSI_STMP_DRIVE_TAG_EXTRA_S: return "Extra";
        case SCSI_STMP_DRIVE_TAG_RESOURCE1_BIN: return "Resource1";
        case SCSI_STMP_DRIVE_TAG_OTGHOST_S: return "OTG";
        case SCSI_STMP_DRIVE_TAG_HOSTRSC_BIN: return "Host Resource";
        case SCSI_STMP_DRIVE_TAG_DATA: return "Data";
        case SCSI_STMP_DRIVE_TAG_HIDDEN: return "Hidden";
        case SCSI_STMP_DRIVE_TAG_BOOTMANAGER_S: return "Boot";
        case SCSI_STMP_DRIVE_TAG_UPDATER_S: return "Updater";
        default: return "?";
    }
}

const char *stmp_get_logical_media_state_string(uint8_t state)
{
    switch(state)
    {
        case SCSI_STMP_MEDIA_STATE_UNKNOWN: return "Unknown";
        case SCSI_STMP_MEDIA_STATE_ERASED: return "Erased";
        case SCSI_STMP_MEDIA_STATE_ALLOCATED: return "Allocated";
        default: return "?";
    }
}

int stmp_get_device_serial(stmp_device_t dev, uint8_t **buffer, int *len)
{
    *len = 2;
    uint16_t len16;
    int ret = stmp_scsi_get_serial_number(dev, 0, &len16, len);
    if(!ret && *len == 2)
    {
        len16 = stmp_fix_endian16be(len16);
        *len = len16;
        *buffer = malloc(*len);
        ret = stmp_scsi_get_serial_number(dev, 1, *buffer, len);
    }
    else
        ret = -1;
    return ret;
}

#define ARRAYLEN(x) (int)(sizeof(x)/sizeof((x)[0]))

int stmp_get_logical_media_info(stmp_device_t dev, struct stmp_logical_media_info_t *info)
{
    memset(info, 0, sizeof(struct stmp_logical_media_info_t));
    int len, ret;
#define entry(name, def) \
    len = sizeof(info->name); \
    ret = stmp_scsi_get_logical_media_info(dev, SCSI_STMP_MEDIA_INFO_##def, &info->name, &len); \
    if(!ret) \
        ret = stmp_fix_logical_media_info(SCSI_STMP_MEDIA_INFO_##def, &info->name, len); \
    if(!ret) \
        info->has.name = true;

    entry(nr_drives, NR_DRIVES);
    entry(size, SIZE);
    entry(alloc_size, ALLOC_UNIT_SIZE);
    entry(initialised, IS_INITIALISED);
    entry(state, STATE);
    entry(write_protected, IS_WRITE_PROTECTED);
    entry(type, TYPE);
    entry(serial_len, SERIAL_NUMBER_SIZE);
    entry(system, IS_SYSTEM_MEDIA);
    entry(present, IS_MEDIA_PRESENT);
    entry(page_size, PAGE_SIZE);
    entry(vendor, VENDOR);
    entry(nand_id, NAND_ID);
    entry(nr_devices, NR_DEVICES);
#undef entry
    if(info->has.serial_len)
    {
        info->serial = malloc(info->serial_len);
        int len = info->serial_len;
        ret = stmp_scsi_get_logical_media_info(dev, SCSI_STMP_MEDIA_INFO_SERIAL_NUMBER,
            info->serial, &len);
        if(ret || len != (int)info->serial_len)
            free(info->serial);
        else
            info->has.serial = true;
    }
    return 0;
}

int stmp_get_logical_drive_info(stmp_device_t dev, uint8_t drive, struct stmp_logical_drive_info_t *info)
{
    memset(info, 0, sizeof(struct stmp_logical_drive_info_t));
    int len, ret;
#define entry(name, def) \
    len = sizeof(info->name); \
    ret = stmp_scsi_get_logical_drive_info(dev, drive, SCSI_STMP_DRIVE_INFO_##def, &info->name, &len); \
    if(!ret) \
        ret = stmp_fix_logical_drive_info(SCSI_STMP_DRIVE_INFO_##def, &info->name, len); \
    if(!ret) \
        info->has.name = true;

    entry(sector_size, SECTOR_SIZE);
    entry(erase_size, ERASE_SIZE);
    entry(size, SIZE);
    entry(sector_count, SECTOR_COUNT);
    entry(type, TYPE);
    entry(tag, TAG);
    entry(component_version, COMPONENT_VERSION);
    entry(project_version, PROJECT_VERSION);
    entry(write_protected, IS_WRITE_PROTECTED);
    entry(serial_len, SERIAL_NUMBER_SIZE);
    entry(present, MEDIA_PRESENT);
    entry(change, MEDIA_CHANGE);
    entry(sector_alloc, SECTOR_ALLOCATION);
#undef entry
    if(info->has.serial_len)
    {
        info->serial = malloc(info->serial_len);
        int len = info->serial_len;
        ret = stmp_scsi_get_logical_media_info(dev, SCSI_STMP_DRIVE_INFO_SERIAL_NUMBER,
            info->serial, &len);
        if(ret || len != (int)info->serial_len)
            free(info->serial);
        else
            info->has.serial = true;
    }
    return 0;
}

int stmp_read_firmware(stmp_device_t dev, void *user, stmp_fw_rw_fn_t fn)
{
    /* read logicial table */
    uint8_t *sector = NULL;
    struct stmp_logical_media_table_t *table = NULL;
    int ret = stmp_get_logical_media_table(dev, &table);
    if(ret)
    {
        stmp_printf(dev, "Cannot get logical table: %d\n", ret);
        return -1;
    }
    /* locate firmware partition */
    int entry = 0;
    while(entry < table->header.count)
        if(table->entry[entry].type == SCSI_STMP_DRIVE_TYPE_SYSTEM &&
                table->entry[entry].tag == SCSI_STMP_DRIVE_TAG_SYSTEM_BOOT)
            break;
        else
            entry++;
    if(entry == table->header.count)
    {
        stmp_printf(dev, "Cannot find firmware partition\n");
        goto Lerr;
    }
    uint8_t drive_no = table->entry[entry].drive_no;
    uint64_t drive_sz = table->entry[entry].size;
    stmp_debugf(dev, "Firmware drive: %#x\n", drive_no);
    stmp_debugf(dev, "Firmware max size: %#llx\n", (unsigned long long)drive_sz);
    /* get drive info */
    struct stmp_logical_drive_info_t info;
    ret = stmp_get_logical_drive_info(dev, drive_no, &info);
    if(ret || !info.has.sector_size)
    {
        stmp_printf(dev, "Cannot get sector size\n");
        goto Lerr;
    }
    unsigned sector_size = info.sector_size;
    stmp_debugf(dev, "Firmware sector size: %lu\n", (unsigned long)sector_size);
    /* allocate a buffer for one sector */
    sector = malloc(sector_size);
    /* read the first sector to check it is correct and get the total size */
    ret = stmp_read_logical_drive_sectors(dev, drive_no, 0, 1, sector, sector_size);
    if(ret)
    {
        stmp_printf(dev, "Cannot read first sector: %d\n", ret);
        goto Lerr;
    }
    uint32_t sig = *(uint32_t *)(sector + 0x14);
    if(sig != 0x504d5453)
    {
        stmp_printf(dev, "There is something wrong: the first sector doesn't have the STMP signature.\n");
        goto Lerr;
    }
    uint32_t fw_size = *(uint32_t *)(sector + 0x1c) * 16; /* see SB file format */
    stmp_debugf(dev, "Firmware size: %#x\n", fw_size);
    /* if fn is NULL, just return the size immediately */
    if(fn != NULL)
    {
        /* read all sectors one by one */
        for(int sec = 0; sec * sector_size < fw_size; sec++)
        {
            ret = stmp_read_logical_drive_sectors(dev, drive_no, sec, 1, sector, sector_size);
            if(ret)
            {
                stmp_printf(dev, "Cannot read sector %d: %d\n", sec, ret);
                goto Lerr;
            }
            int xfer_len = MIN(sector_size, fw_size - sec * sector_size);
            ret = fn(user, sector, xfer_len);
            if(ret != xfer_len)
            {
                stmp_printf(dev, "User write failed: %d\n", ret);
                goto Lerr;
            }
        }
    }
    ret = fw_size;
Lend:
    free(table);
    if(sector)
        free(sector);
    return ret;
Lerr:
    ret = -1;
    goto Lend;
}

int stmp_write_firmware(stmp_device_t dev, void *user, stmp_fw_rw_fn_t fn)
{
    /* read logicial table */
    struct stmp_logical_media_table_t *table = NULL;
    int ret = stmp_get_logical_media_table(dev, &table);
    if(ret)
    {
        stmp_printf(dev, "Cannot get logical table: %d\n", ret);
        return -1;
    }
    /* locate firmware partition */
    int entry = 0;
    while(entry < table->header.count)
        if(table->entry[entry].type == SCSI_STMP_DRIVE_TYPE_SYSTEM &&
                table->entry[entry].tag == SCSI_STMP_DRIVE_TAG_SYSTEM_BOOT)
            break;
        else
            entry++;
    if(entry == table->header.count)
    {
        stmp_printf(dev, "Cannot find firmware partition\n");
        goto Lerr;
    }
    uint8_t drive_no = table->entry[entry].drive_no;
    uint64_t drive_sz = table->entry[entry].size;
    stmp_debugf(dev, "Firmware drive: %#x\n", drive_no);
    stmp_debugf(dev, "Firmware max size: %#llx\n", (unsigned long long)drive_sz);
    /* get drive info */
    struct stmp_logical_drive_info_t info;
    ret = stmp_get_logical_drive_info(dev, drive_no, &info);
    if(ret || !info.has.sector_size)
    {
        stmp_printf(dev, "Cannot get sector size\n");
        goto Lerr;
    }
    unsigned sector_size = info.sector_size;
    stmp_debugf(dev, "Firmware sector size: %lu\n", (unsigned long)sector_size);
    /* allocate a buffer for one sector */
    uint8_t *sector = malloc(sector_size);
    /* read the first sector to check it is correct and get the total size */
    ret = fn(user, sector, sector_size);
    /* the whole file could be smaller than one sector, but it must be greater
     * then the header size */
    if(ret < 0x20)
    {
        stmp_printf(dev, "User read failed: %d\n", ret);
        goto Lerr;
    }
    uint32_t sig = *(uint32_t *)(sector + 0x14);
    if(sig != 0x504d5453)
    {
        stmp_printf(dev, "There is something wrong: the first sector doesn't have the STMP signature.\n");
        goto Lerr;
    }
    uint32_t fw_size = *(uint32_t *)(sector + 0x1c) * 16; /* see SB file format */
    stmp_debugf(dev, "Firmware size: %#x\n", fw_size);
    /* write all sectors one by one */
    for(int sec = 0; sec * sector_size < fw_size; sec++)
    {
        int xfer_len = MIN(sector_size, fw_size - sec * sector_size);
        /* avoid rereading the first sector */
        if(sec != 0)
            ret = fn(user, sector, xfer_len);
        if(ret != xfer_len)
        {
            stmp_printf(dev, "User read failed: %d\n", ret);
            goto Lerr;
        }
        if(ret < (int)sector_size)
            memset(sector + ret, 0, sector_size - ret);
        ret = stmp_write_logical_drive_sectors(dev, drive_no, sec, 1, sector, sector_size);
        if(ret)
        {
            stmp_printf(dev, "Cannot write sector %d: %d\n", sec, ret);
            goto Lerr;
        }
    }
    ret = fw_size;
Lend:
    free(table);
    return ret;
Lerr:
    ret = -1;
    goto Lend;
}