[BACK]Return to mmc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cdio

Annotation of src/usr.bin/cdio/mmc.c, Revision 1.33

1.33    ! deraadt     1: /*     $OpenBSD: mmc.c,v 1.32 2020/09/01 17:20:02 krw Exp $    */
1.3       mjc         2: /*
                      3:  * Copyright (c) 2006 Michael Coulter <mjc@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
1.33    ! deraadt    18: #include <sys/types.h>
1.1       mjc        19: #include <sys/limits.h>
1.31      cheloha    20: #include <sys/time.h>
1.1       mjc        21: #include <sys/scsiio.h>
1.8       mjc        22: #include <scsi/cd.h>
                     23: #include <scsi/scsi_all.h>
                     24: #include <scsi/scsi_disk.h>
1.1       mjc        25: #include <err.h>
1.2       mjc        26: #include <errno.h>
1.1       mjc        27: #include <fcntl.h>
                     28: #include <stdio.h>
                     29: #include <string.h>
1.31      cheloha    30: #include <time.h>
1.1       mjc        31: #include <unistd.h>
                     32: #include "extern.h"
                     33:
                     34: extern int fd;
1.28      krw        35: extern u_int8_t mediacap[];
1.2       mjc        36: extern char *cdname;
1.28      krw        37: extern int verbose;
1.20      av         38:
                     39: #define SCSI_GET_CONFIGURATION         0x46
                     40:
1.26      fgsch      41: #define MMC_FEATURE_HDR_LEN            8
1.25      av         42:
1.28      krw        43: static const struct {
                     44:        u_int16_t id;
                     45:        char *name;
                     46: } mmc_feature[] = {
                     47:        { 0x0000, "Profile List" },
                     48:        { 0x0001, "Core" },
                     49:        { 0x0002, "Morphing" },
                     50:        { 0x0003, "Removable Medium" },
                     51:        { 0x0004, "Write Protect" },
                     52:        { 0x0010, "Random Readable" },
                     53:        { 0x001d, "Multi-Read" },
                     54:        { 0x001e, "CD Read" },
                     55:        { 0x001f, "DVD Read" },
                     56:        { 0x0020, "Random Writable" },
                     57:        { 0x0021, "Incremental Streaming Writable" },
                     58:        { 0x0022, "Sector Erasable" },
                     59:        { 0x0023, "Formattable" },
                     60:        { 0x0024, "Hardware Defect Management" },
                     61:        { 0x0025, "Write Once" },
                     62:        { 0x0026, "Restricted Overwrite" },
                     63:        { 0x0027, "CD-RW CAV Write" },
                     64:        { 0x0028, "MRW" },
                     65:        { 0x0029, "Enhanced Defect Reporting" },
                     66:        { 0x002a, "DVD+RW" },
                     67:        { 0x002b, "DVD+R" },
                     68:        { 0x002c, "Rigid Restricted Overwrite" },
                     69:        { 0x002d, "CD Track at Once (TAO)" },
                     70:        { 0x002e, "CD Mastering (Session at Once)" },
                     71:        { 0x002f, "DVD-RW Write" },
                     72:        { 0x0030, "DDCD-ROM (Legacy)" },
                     73:        { 0x0031, "DDCD-R (Legacy)" },
                     74:        { 0x0032, "DDCD-RW (Legacy)" },
                     75:        { 0x0033, "Layer Jump Recording" },
                     76:        { 0x0037, "CD-RW Media Write Support" },
                     77:        { 0x0038, "BD-R Pseudo-Overwrite (POW)" },
                     78:        { 0x003a, "DVD+RW Dual Layer" },
                     79:        { 0x003b, "DVD+R Dual Layer" },
                     80:        { 0x0040, "BD Read" },
                     81:        { 0x0041, "BD Write" },
                     82:        { 0x0042, "Timely Safe Recording (TSR)" },
                     83:        { 0x0050, "HD DVD Read" },
                     84:        { 0x0051, "HD DVD Write" },
                     85:        { 0x0080, "Hybrid Disc" },
                     86:        { 0x0100, "Power Management" },
                     87:        { 0x0101, "S.M.A.R.T." },
                     88:        { 0x0102, "Embedded Changer" },
                     89:        { 0x0103, "CD Audio External Play (Legacy)" },
                     90:        { 0x0104, "Microcode Upgrade" },
                     91:        { 0x0105, "Timeout" },
                     92:        { 0x0106, "DVD CSS" },
                     93:        { 0x0107, "Real Time Streaming" },
                     94:        { 0x0108, "Drive Serial Number" },
                     95:        { 0x0109, "Media Serial Number" },
                     96:        { 0x010a, "Disc Control Blocks (DCBs)" },
                     97:        { 0x010b, "DVD CPRM" },
                     98:        { 0x010c, "Firmware Information" },
                     99:        { 0x010d, "AACS" },
                    100:        { 0x0110, "VCPS" },
                    101:        { 0, NULL }
                    102: };
                    103:
                    104: static const struct {
                    105:        u_int16_t id;
                    106:        char *name;
                    107: } mmc_profile[] = {
                    108:        { 0x0001, "Re-writable disk, capable of changing behaviour" },
                    109:        { 0x0002, "Re-writable, with removable media" },
                    110:        { 0x0003, "Magneto-Optical disk with sector erase capability" },
                    111:        { 0x0004, "Optical write once" },
                    112:        { 0x0005, "Advance Storage -- Magneto-Optical" },
                    113:        { 0x0008, "Read only Compact Disc" },
                    114:        { 0x0009, "Write once Compact Disc" },
                    115:        { 0x000a, "Re-writable Compact Disc" },
                    116:        { 0x0010, "Read only DVD" },
                    117:        { 0x0011, "Write once DVD using Sequential recording" },
                    118:        { 0x0012, "Re-writable DVD" },
                    119:        { 0x0013, "Re-recordable DVD using Restricted Overwrite" },
                    120:        { 0x0014, "Re-recordable DVD using Sequential recording" },
                    121:        { 0x0015, "Dual Layer DVD-R using Sequential recording" },
                    122:        { 0x0016, "Dual Layer DVD-R using Layer Jump recording" },
                    123:        { 0x001a, "DVD+ReWritable" },
                    124:        { 0x001b, "DVD+Recordable" },
                    125:        { 0x0020, "DDCD-ROM" },
                    126:        { 0x0021, "DDCD-R" },
                    127:        { 0x0022, "DDCD-RW" },
                    128:        { 0x002a, "DVD+Rewritable Dual Layer" },
                    129:        { 0x002b, "DVD+Recordable Dual Layer" },
                    130:        { 0x003e, "Blu-ray Disc ROM" },
                    131:        { 0x003f, "Blu-ray Disc Recordable -- Sequential Recording Mode" },
                    132:        { 0x0040, "Blu-ray Disc Recordable -- Random Recording Mode" },
                    133:        { 0x0041, "Blu-ray Disc Rewritable" },
                    134:        { 0x004e, "Read-only HD DVD" },
                    135:        { 0x004f, "Write-once HD DVD" },
                    136:        { 0x0050, "Rewritable HD DVD" },
                    137:        { 0, NULL }
                    138: };
                    139:
1.25      av        140: int
                    141: get_media_type(void)
                    142: {
                    143:        scsireq_t scr;
                    144:        char buf[32];
                    145:        u_char disctype;
                    146:        int rv, error;
                    147:
                    148:        rv = MEDIATYPE_UNKNOWN;
                    149:        memset(buf, 0, sizeof(buf));
                    150:        memset(&scr, 0, sizeof(scr));
                    151:
                    152:        scr.cmd[0] = READ_TOC;
                    153:        scr.cmd[1] = 0x2;       /* MSF */
                    154:        scr.cmd[2] = 0x4;       /* ATIP */
                    155:        scr.cmd[8] = 0x20;
                    156:
                    157:        scr.flags = SCCMD_ESCAPE | SCCMD_READ;
                    158:        scr.databuf = buf;
                    159:        scr.datalen = sizeof(buf);
                    160:        scr.cmdlen = 10;
                    161:        scr.timeout = 120000;
                    162:        scr.senselen = SENSEBUFLEN;
                    163:
                    164:        error = ioctl(fd, SCIOCCOMMAND, &scr);
1.28      krw       165:        if (error != -1 && scr.retsts == SCCMD_OK && scr.datalen_used > 7) {
1.25      av        166:                disctype = (buf[6] >> 6) & 0x1;
                    167:                if (disctype == 0)
                    168:                        rv = MEDIATYPE_CDR;
                    169:                else if (disctype == 1)
                    170:                        rv = MEDIATYPE_CDRW;
                    171:        }
                    172:
                    173:        return (rv);
                    174: }
1.20      av        175:
                    176: int
1.28      krw       177: get_media_capabilities(u_int8_t *cap, int rt)
1.20      av        178: {
                    179:        scsireq_t scr;
1.23      av        180:        u_char buf[4096];
1.26      fgsch     181:        u_int32_t i, dlen;
1.28      krw       182:        u_int16_t feature, profile, tmp;
1.26      fgsch     183:        u_int8_t feature_len;
1.28      krw       184:        int current, error, j, k;
1.20      av        185:
1.28      krw       186:        memset(cap, 0, MMC_FEATURE_MAX / NBBY);
1.20      av        187:        memset(buf, 0, sizeof(buf));
                    188:        memset(&scr, 0, sizeof(scr));
                    189:
                    190:        scr.cmd[0] = SCSI_GET_CONFIGURATION;
1.26      fgsch     191:        scr.cmd[1] = rt;
1.27      claudio   192:        tmp = htobe16(sizeof(buf));
                    193:        memcpy(scr.cmd + 7, &tmp, sizeof(u_int16_t));
1.20      av        194:
                    195:        scr.flags = SCCMD_ESCAPE | SCCMD_READ;
                    196:        scr.databuf = buf;
                    197:        scr.datalen = sizeof(buf);
                    198:        scr.cmdlen = 10;
                    199:        scr.timeout = 120000;
                    200:        scr.senselen = SENSEBUFLEN;
                    201:
                    202:        error = ioctl(fd, SCIOCCOMMAND, &scr);
1.28      krw       203:        if (error == -1 || scr.retsts != SCCMD_OK)
1.20      av        204:                return (-1);
1.26      fgsch     205:        if (scr.datalen_used < MMC_FEATURE_HDR_LEN)
                    206:                return (-1);    /* Can't get the header. */
                    207:
                    208:        /* Include the whole header in the length. */
                    209:        dlen = betoh32(*(u_int32_t *)buf) + 4;
                    210:        if (dlen > scr.datalen_used)
                    211:                dlen = scr.datalen_used;
                    212:
1.28      krw       213:        if (verbose > 1)
                    214:                printf("Features:\n");
1.26      fgsch     215:        for (i = MMC_FEATURE_HDR_LEN; i + 3 < dlen; i += feature_len) {
                    216:                feature_len = buf[i + 3] + 4;
                    217:                if (feature_len + i > dlen)
                    218:                        break;
1.20      av        219:
                    220:                feature = betoh16(*(u_int16_t *)(buf + i));
1.26      fgsch     221:                if (feature >= MMC_FEATURE_MAX)
                    222:                        break;
1.20      av        223:
1.28      krw       224:                if (verbose > 1) {
                    225:                        printf("0x%04x", feature);
                    226:                        for (j = 0; mmc_feature[j].name != NULL; j++)
                    227:                                if (feature == mmc_feature[j].id)
                    228:                                        break;
                    229:                        if (mmc_feature[j].name == NULL)
                    230:                                printf(" <Undocumented>");
                    231:                        else
                    232:                                printf(" %s", mmc_feature[j].name);
                    233:                        if (feature_len > 4)
                    234:                                printf(" (%d bytes of data)", feature_len - 4);
                    235:                        printf("\n");
                    236:                        if (verbose > 2) {
                    237:                                printf("    ");
                    238:                                for (j = i; j < i + feature_len; j++) {
                    239:                                        printf("%02x", buf[j]);
                    240:                                        if ((j + 1) == (i + feature_len))
                    241:                                                printf("\n");
                    242:                                        else if ((j > i) && ((j - i + 1) % 16
                    243:                                            == 0))
                    244:                                                printf("\n    ");
                    245:                                        else if ((j - i) == 3)
                    246:                                                printf("|");
                    247:                                        else
                    248:                                                printf(" ");
                    249:                                }
                    250:                        }
                    251:                }
                    252:                if (feature == 0 && verbose > 1) {
                    253:                        if (verbose > 2)
                    254:                                printf("    Profiles:\n");
                    255:                        for (j = i + 4; j < i + feature_len; j += 4) {
                    256:                                profile = betoh16(*(u_int16_t *)(buf+j));
                    257:                                current = buf[j+2] == 1;
                    258:                                if (verbose < 3 && !current)
                    259:                                        continue;
                    260:                                if (current)
                    261:                                        printf("  * ");
                    262:                                else
                    263:                                        printf("    ");
                    264:                                printf("0x%04x", profile);
                    265:                                for (k = 0; mmc_profile[k].name != NULL; k++)
                    266:                                        if (profile == mmc_profile[k].id)
                    267:                                                break;
                    268:                                if (mmc_profile[k].name == NULL)
                    269:                                        printf(" <Undocumented>");
                    270:                                else
                    271:                                        printf(" %s", mmc_profile[k].name);
                    272:                                printf(" %s\n", current ? "[Current Profile]" :
                    273:                                    "" );
                    274:                        }
                    275:                }
1.33    ! deraadt   276:                cdio_setbit(cap, feature);
1.20      av        277:        }
                    278:
                    279:        return (0);
                    280: }
1.1       mjc       281:
1.29      deraadt   282: static int
1.22      av        283: set_speed(int wspeed)
                    284: {
                    285:        scsireq_t scr;
                    286:        int r;
                    287:
                    288:        memset(&scr, 0, sizeof(scr));
1.24      fgsch     289:        scr.cmd[0] = SET_CD_SPEED;
1.33    ! deraadt   290:        scr.cmd[1] = (cdio_isset(mediacap, MMC_FEATURE_CDRW_CAV)) != 0;
1.22      av        291:        *(u_int16_t *)(scr.cmd + 2) = htobe16(DRIVE_SPEED_OPTIMAL);
                    292:        *(u_int16_t *)(scr.cmd + 4) = htobe16(wspeed);
                    293:
                    294:        scr.cmdlen = 12;
                    295:        scr.datalen = 0;
                    296:        scr.timeout = 120000;
                    297:        scr.flags = SCCMD_ESCAPE;
                    298:        scr.senselen = SENSEBUFLEN;
                    299:
                    300:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    301:        return (r == 0 ? scr.retsts : -1);
                    302: }
                    303:
                    304: int
1.1       mjc       305: blank(void)
                    306: {
1.8       mjc       307:        struct scsi_blank *scb;
1.1       mjc       308:        scsireq_t scr;
                    309:        int r;
                    310:
                    311:        bzero(&scr, sizeof(scr));
1.8       mjc       312:        scb = (struct scsi_blank *)scr.cmd;
                    313:        scb->opcode = BLANK;
                    314:        scb->byte2 |= BLANK_MINIMAL;
                    315:        scr.cmdlen = sizeof(*scb);
1.1       mjc       316:        scr.datalen = 0;
                    317:        scr.timeout = 120000;
                    318:        scr.flags = SCCMD_ESCAPE;
                    319:        scr.senselen = SENSEBUFLEN;
                    320:
1.2       mjc       321:        r = ioctl(fd, SCIOCCOMMAND, &scr);
1.1       mjc       322:        return (r == 0 ? scr.retsts : -1);
                    323: }
                    324:
                    325: int
                    326: unit_ready(void)
                    327: {
1.8       mjc       328:        struct scsi_test_unit_ready *scb;
1.1       mjc       329:        scsireq_t scr;
                    330:        int r;
                    331:
                    332:        bzero(&scr, sizeof(scr));
1.8       mjc       333:        scb = (struct scsi_test_unit_ready *)scr.cmd;
                    334:        scb->opcode = TEST_UNIT_READY;
                    335:        scr.cmdlen = sizeof(*scb);
1.1       mjc       336:        scr.datalen = 0;
                    337:        scr.timeout = 120000;
                    338:        scr.flags = SCCMD_ESCAPE;
                    339:        scr.senselen = SENSEBUFLEN;
                    340:
                    341:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    342:        return (r == 0 ? scr.retsts : -1);
                    343: }
                    344:
                    345: int
                    346: synchronize_cache(void)
                    347: {
1.8       mjc       348:        struct scsi_synchronize_cache *scb;
1.1       mjc       349:        scsireq_t scr;
                    350:        int r;
                    351:
                    352:        bzero(&scr, sizeof(scr));
1.8       mjc       353:        scb = (struct scsi_synchronize_cache *)scr.cmd;
                    354:        scb->opcode = SYNCHRONIZE_CACHE;
                    355:        scr.cmdlen = sizeof(*scb);
1.1       mjc       356:        scr.datalen = 0;
                    357:        scr.timeout = 120000;
                    358:        scr.flags = SCCMD_ESCAPE;
                    359:        scr.senselen = SENSEBUFLEN;
                    360:
                    361:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    362:        return (r == 0 ? scr.retsts : -1);
                    363: }
                    364:
                    365: int
                    366: close_session(void)
                    367: {
1.8       mjc       368:        struct scsi_close_track *scb;
1.1       mjc       369:        scsireq_t scr;
                    370:        int r;
                    371:
                    372:        bzero(&scr, sizeof(scr));
1.8       mjc       373:        scb = (struct scsi_close_track *)scr.cmd;
                    374:        scb->opcode = CLOSE_TRACK;
                    375:        scb->closefunc = CT_CLOSE_SESS;
                    376:        scr.cmdlen = sizeof(*scb);
1.1       mjc       377:        scr.datalen = 0;
                    378:        scr.timeout = 120000;
                    379:        scr.flags = SCCMD_ESCAPE;
                    380:        scr.senselen = SENSEBUFLEN;
                    381:
                    382:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    383:        return (r == 0 ? scr.retsts : -1);
                    384: }
                    385:
                    386: int
1.5       mjc       387: writetao(struct track_head *thp)
1.1       mjc       388: {
1.15      deraadt   389:        u_char modebuf[70], bdlen;
1.5       mjc       390:        struct track_info *tr;
1.15      deraadt   391:        int r, track = 0;
1.5       mjc       392:
1.1       mjc       393:        if ((r = mode_sense_write(modebuf)) != SCCMD_OK) {
                    394:                warnx("mode sense failed: %d", r);
                    395:                return (r);
                    396:        }
                    397:        bdlen = modebuf[7];
                    398:        modebuf[2+8+bdlen] |= 0x40; /* Buffer Underrun Free Enable */
                    399:        modebuf[2+8+bdlen] |= 0x01; /* change write type to TAO */
                    400:
1.5       mjc       401:        SLIST_FOREACH(tr, thp, track_list) {
1.15      deraadt   402:                track++;
1.7       deraadt   403:                switch (tr->type) {
1.5       mjc       404:                case 'd':
1.1       mjc       405:                        modebuf[3+8+bdlen] = 0x04; /* track mode = data */
                    406:                        modebuf[4+8+bdlen] = 0x08; /* 2048 block track mode */
                    407:                        modebuf[8+8+bdlen] = 0x00; /* turn off XA */
1.5       mjc       408:                        break;
                    409:                case 'a':
1.1       mjc       410:                        modebuf[3+8+bdlen] = 0x00; /* track mode = audio */
                    411:                        modebuf[4+8+bdlen] = 0x00; /* 2352 block track mode */
                    412:                        modebuf[8+8+bdlen] = 0x00; /* turn off XA */
1.5       mjc       413:                        break;
                    414:                default:
                    415:                        warn("impossible tracktype detected");
                    416:                        break;
1.1       mjc       417:                }
                    418:                while (unit_ready() != SCCMD_OK)
                    419:                        continue;
                    420:                if ((r = mode_select_write(modebuf)) != SCCMD_OK) {
1.15      deraadt   421:                        warnx("mode select failed: %d", r);
1.1       mjc       422:                        return (r);
                    423:                }
1.22      av        424:
                    425:                set_speed(tr->speed);
1.15      deraadt   426:                writetrack(tr, track);
1.1       mjc       427:                synchronize_cache();
                    428:        }
1.15      deraadt   429:        fprintf(stderr, "Closing session.\n");
1.1       mjc       430:        close_session();
                    431:        return (0);
                    432: }
                    433:
                    434: int
1.15      deraadt   435: writetrack(struct track_info *tr, int track)
1.1       mjc       436: {
1.31      cheloha   437:        struct timespec ts, ots, ats;
1.15      deraadt   438:        u_char databuf[65536], nblk;
                    439:        u_int end_lba, lba, tmp;
1.1       mjc       440:        scsireq_t scr;
1.11      mjc       441:        int r;
1.1       mjc       442:
1.5       mjc       443:        nblk = 65535/tr->blklen;
1.1       mjc       444:        bzero(&scr, sizeof(scr));
                    445:        scr.timeout = 300000;
1.32      krw       446:        scr.cmd[0] = WRITE_10;
1.1       mjc       447:        scr.cmd[1] = 0x00;
                    448:        scr.cmd[8] = nblk; /* Transfer length in blocks (LSB) */
                    449:        scr.cmdlen = 10;
                    450:        scr.databuf = (caddr_t)databuf;
1.5       mjc       451:        scr.datalen = nblk * tr->blklen;
1.1       mjc       452:        scr.senselen = SENSEBUFLEN;
                    453:        scr.flags = SCCMD_ESCAPE|SCCMD_WRITE;
                    454:
1.31      cheloha   455:        timespecclear(&ots);
                    456:        ats.tv_sec = 1;
                    457:        ats.tv_nsec = 0;
1.15      deraadt   458:
1.1       mjc       459:        if (get_nwa(&lba) != SCCMD_OK) {
                    460:                warnx("cannot get next writable address");
                    461:                return (-1);
                    462:        }
                    463:        tmp = htobe32(lba); /* update lba in cdb */
                    464:        memcpy(&scr.cmd[2], &tmp, sizeof(tmp));
                    465:
1.5       mjc       466:        if (tr->sz / tr->blklen + 1 > UINT_MAX || tr->sz < tr->blklen) {
1.15      deraadt   467:                warnx("file %s has invalid size", tr->file);
1.1       mjc       468:                return (-1);
                    469:        }
1.5       mjc       470:        if (tr->sz % tr->blklen) {
1.7       deraadt   471:                warnx("file %s is not multiple of block length %d",
                    472:                    tr->file, tr->blklen);
1.5       mjc       473:                end_lba = tr->sz / tr->blklen + lba + 1;
1.1       mjc       474:        } else {
1.5       mjc       475:                end_lba = tr->sz / tr->blklen + lba;
1.1       mjc       476:        }
1.21      av        477:        if (lseek(tr->fd, tr->off, SEEK_SET) == -1)
                    478:                err(1, "seek failed for file %s", tr->file);
1.13      deraadt   479:        while (lba < end_lba && nblk != 0) {
1.1       mjc       480:                while (lba + nblk <= end_lba) {
1.11      mjc       481:                        read(tr->fd, databuf, nblk * tr->blklen);
1.1       mjc       482:                        scr.cmd[8] = nblk;
1.5       mjc       483:                        scr.datalen = nblk * tr->blklen;
1.12      mjc       484: again:
1.1       mjc       485:                        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    486:                        if (r != 0) {
1.17      deraadt   487:                                printf("\r%60s", "");
1.1       mjc       488:                                warn("ioctl failed while attempting to write");
                    489:                                return (-1);
1.12      mjc       490:                        }
                    491:                        if (scr.retsts == SCCMD_SENSE && scr.sense[2] == 0x2) {
                    492:                                usleep(1000);
                    493:                                goto again;
1.1       mjc       494:                        }
                    495:                        if (scr.retsts != SCCMD_OK) {
1.17      deraadt   496:                                printf("\r%60s", "");
1.7       deraadt   497:                                warnx("ioctl returned bad status while "
                    498:                                    "attempting to write: %d",
                    499:                                    scr.retsts);
1.1       mjc       500:                                return (r);
                    501:                        }
                    502:                        lba += nblk;
1.15      deraadt   503:
1.31      cheloha   504:                        clock_gettime(CLOCK_MONOTONIC, &ts);
                    505:                        if (lba == end_lba || timespeccmp(&ts, &ots, >)) {
1.15      deraadt   506:                                fprintf(stderr,
1.17      deraadt   507:                                    "\rtrack %02d '%c' %08u/%08u %3d%%",
1.15      deraadt   508:                                    track, tr->type,
                    509:                                    lba, end_lba, 100 * lba / end_lba);
1.31      cheloha   510:                                timespecadd(&ts, &ats, &ots);
1.15      deraadt   511:                        }
1.1       mjc       512:                        tmp = htobe32(lba); /* update lba in cdb */
                    513:                        memcpy(&scr.cmd[2], &tmp, sizeof(tmp));
                    514:                }
                    515:                nblk--;
                    516:        }
1.13      deraadt   517:        printf("\n");
1.11      mjc       518:        close(tr->fd);
1.1       mjc       519:        return (0);
                    520: }
                    521:
                    522: int
                    523: mode_sense_write(unsigned char buf[])
                    524: {
1.8       mjc       525:        struct scsi_mode_sense_big *scb;
1.1       mjc       526:        scsireq_t scr;
                    527:        int r;
                    528:
                    529:        bzero(&scr, sizeof(scr));
1.8       mjc       530:        scb = (struct scsi_mode_sense_big *)scr.cmd;
                    531:        scb->opcode = MODE_SENSE_BIG;
                    532:        /* XXX: need to set disable block descriptors and check SCSI drive */
                    533:        scb->page = WRITE_PARAM_PAGE;
                    534:        scb->length[1] = 0x46; /* 16 for the header + size from pg. 89 mmc-r10a.pdf */
                    535:        scr.cmdlen = sizeof(*scb);
1.1       mjc       536:        scr.timeout = 4000;
                    537:        scr.senselen = SENSEBUFLEN;
1.7       deraadt   538:        scr.datalen= 0x46;
1.1       mjc       539:        scr.flags = SCCMD_ESCAPE|SCCMD_READ;
                    540:        scr.databuf = (caddr_t)buf;
                    541:
                    542:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    543:        return (r == 0 ? scr.retsts : -1);
                    544: }
                    545:
                    546: int
                    547: mode_select_write(unsigned char buf[])
                    548: {
1.8       mjc       549:        struct scsi_mode_select_big *scb;
1.1       mjc       550:        scsireq_t scr;
                    551:        int r;
                    552:
                    553:        bzero(&scr, sizeof(scr));
1.8       mjc       554:        scb = (struct scsi_mode_select_big *)scr.cmd;
                    555:        scb->opcode = MODE_SELECT_BIG;
1.10      deraadt   556:
                    557:        /*
                    558:         * INF-8020 says bit 4 in byte 2 is '1'
                    559:         * INF-8090 refers to it as 'PF(1)' then doesn't
                    560:         * describe it.
                    561:         */
1.8       mjc       562:        scb->byte2 = 0x10;
                    563:        scb->length[1] = 2 + buf[1] + 256 * buf[0];
1.1       mjc       564:        scr.timeout = 4000;
                    565:        scr.senselen = SENSEBUFLEN;
1.8       mjc       566:        scr.cmdlen = sizeof(*scb);
1.1       mjc       567:        scr.datalen = 2 + buf[1] + 256 * buf[0];
                    568:        scr.flags = SCCMD_ESCAPE|SCCMD_WRITE;
                    569:        scr.databuf = (caddr_t)buf;
                    570:
                    571:        r = ioctl(fd, SCIOCCOMMAND, &scr);
1.6       mjc       572:        return (r == 0 ? scr.retsts : -1);
                    573: }
                    574:
                    575: int
                    576: get_disc_size(off_t *availblk)
                    577: {
                    578:        u_char databuf[28];
1.8       mjc       579:        struct scsi_read_track_info *scb;
1.6       mjc       580:        scsireq_t scr;
1.15      deraadt   581:        int r, tmp;
1.6       mjc       582:
                    583:        bzero(&scr, sizeof(scr));
1.8       mjc       584:        scb = (struct scsi_read_track_info *)scr.cmd;
1.6       mjc       585:        scr.timeout = 4000;
                    586:        scr.senselen = SENSEBUFLEN;
1.8       mjc       587:        scb->opcode = READ_TRACK_INFO;
                    588:        scb->addrtype = RTI_TRACK;
1.16      mjc       589:        scb->addr[3] = 1;
                    590:        scb->data_len[1] = 0x1c;
1.8       mjc       591:        scr.cmdlen = sizeof(*scb);
1.7       deraadt   592:        scr.datalen= 0x1c;
1.6       mjc       593:        scr.flags = SCCMD_ESCAPE|SCCMD_READ;
                    594:        scr.databuf = (caddr_t)databuf;
                    595:
                    596:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    597:        memcpy(&tmp, &databuf[16], sizeof(tmp));
                    598:        *availblk = betoh32(tmp);
1.1       mjc       599:        return (r == 0 ? scr.retsts : -1);
                    600: }
                    601:
                    602: int
                    603: get_nwa(int *nwa)
                    604: {
                    605:        u_char databuf[28];
                    606:        scsireq_t scr;
1.15      deraadt   607:        int r, tmp;
1.1       mjc       608:
                    609:        bzero(&scr, sizeof(scr));
                    610:        scr.timeout = 4000;
                    611:        scr.senselen = SENSEBUFLEN;
1.24      fgsch     612:        scr.cmd[0] = READ_TRACK_INFO;
1.1       mjc       613:        scr.cmd[1] = 0x01;
                    614:        scr.cmd[5] = 0xff; /* Invisible Track */
                    615:        scr.cmd[7] = 0x00;
                    616:        scr.cmd[8] = 0x1c;
                    617:        scr.cmdlen = 10;
1.7       deraadt   618:        scr.datalen= 0x1c;
1.1       mjc       619:        scr.flags = SCCMD_ESCAPE|SCCMD_READ;
                    620:        scr.databuf = (caddr_t)databuf;
                    621:
                    622:        r = ioctl(fd, SCIOCCOMMAND, &scr);
                    623:        memcpy(&tmp, &databuf[12], sizeof(tmp));
                    624:        *nwa = betoh32(tmp);
                    625:        return (r == 0 ? scr.retsts : -1);
                    626: }