aboutsummaryrefslogtreecommitdiff
path: root/sg_start.c
blob: 1c617515c44807707fdd61a98f4222cd93ee24b6 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#include "sg_lib.h"
#include "sg_cmds_basic.h"

/*
 *  Copyright (C) 1999-2006 D. Gilbert
 *  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, or (at your option)
 *  any later version.

    Since this code has been used in the past to form the backbone of
    some Linux apps based on the "sg" device driver, it has been
    strengthened.

    Start/Stop parameter by Kurt Garloff <garloff at suse dot de>, 6/2000
    Sync cache parameter by Kurt Garloff <garloff at suse dot de>, 1/2001
    Guard block device answering sg's ioctls. 
                     <dgilbert at interlog dot com> 12/2002
    Convert to SG_IO ioctl so can use sg or block devices in 2.6.* 3/2003
 
*/

static char * version_str = "0.51 20061016";


void usage ()
{
        fprintf(stderr, "Usage:  sg_start [0] [1] [--eject] [--fl=<n>] "
                "[-i] [--imm=0|1]\n"
                "                 [--load] [--loej] [--pc=<n>] [--start] "
                "[--stop] [-v] [-V]\n"
                "<                device>\n"
                "  where:\n"
                "    0          stop unit (e.g. spin down a disk or a "
                "cd/dvd)\n"
                "    1          start unit (e.g. spin up a disk or a "
                "cd/dvd)\n"
                "    --eject    stop then eject the medium\n"
                "    --fl=<n>   format layer number (mmc5)\n"
                "    -i         return immediately (same as '--imm=1')\n"
                "    --imm=0|1  0->await completion(def), 1->return "
                "immediately\n"
                "    --load     load then start the medium\n"
                "    --loej     load the medium if '-start' option is "
                "also given\n"
                "               or stop unit and eject\n"
                "    --pc=<n>   power conditions (in hex, default 0 -> no "
                "power condition)\n"
                "               1 -> active, 2 -> idle, 3 -> standby, "
                "5 -> sleep (MMC)\n"
                "    --start    start unit (same as '1'), default "
                "action\n"
                "    --stop     stop unit (same as '0')\n"
                "    -v         verbose (print out SCSI commands)\n"
                "    -V         print version string then exit\n\n"
                "    Example: 'sg_start --stop /dev/sdb'    stops unit\n"
                "             'sg_start --eject /dev/scd0'  stops unit and "
                "ejects medium\n\n"
                "Performs a START STOP UNIT SCSI command\n"
                );
        exit (1);
}

int main(int argc, char * argv[])
{
        int startstop = -1;
        const char * file_name = 0;
        const char * cp;
        int k, fd, num, res, plen, jmp_out;
        unsigned int u;
        int ambigu = 0;
        int immed = 0;
        int loej = 0;
        int fl_num = -1;
        int power_conds = 0;
        int verbose = 0;
        int ret = 0;
        
        for (k = 1; k < argc; ++k) {
                cp = argv[k];
                plen = strlen(cp);
                if (plen <= 0)
                        continue;
                if ('-' == *cp) {
                        for (--plen, ++cp, jmp_out = 0; plen > 0;
                             --plen, ++cp) {
                                switch (*cp) {
                                case 'i':
                                        if ('\0' == *(cp + 1))
                                                immed = 1;
                                        else
                                                jmp_out = 1;
                                        break;
                                case 'v':
                                        ++verbose;
                                        break;
                                case 'V':
                                        fprintf(stderr, "Version string: "
                                                "%s\n", version_str);
                                        exit(0);
                                case '?':
                                        usage();
                                        return 0;
                                case '-':
                                        ++cp;
                                        --plen;
                                        jmp_out = 1;
                                        break;
                                default:
                                        jmp_out = 1;
                                        break;
                                }
                                if (jmp_out)
                                        break;
                        }
                        if (plen <= 0)
                                continue;

                        if (0 == strncmp(cp, "eject", 5)) {
                                loej = 1;
                                if (startstop == 1)
                                        ambigu = 1;
                                else
                                        startstop = 0;
                        } else if (0 == strncmp("fl=", cp, 3)) {
                                num = sscanf(cp + 3, "%x", &u);
                                if (1 != num) {
                                        fprintf(stderr, "Bad value after "
                                                "'fl=' option\n");
                                        usage();
                                        return SG_LIB_SYNTAX_ERROR;
                                }
                                fl_num = u;
                        } else if (0 == strncmp("imm=", cp, 4)) {
                                num = sscanf(cp + 4, "%x", &u);
                                if ((1 != num) || (u > 1)) {
                                        fprintf(stderr, "Bad value after "
                                                "'imm=' option\n");
                                        usage();
                                        return SG_LIB_SYNTAX_ERROR;
                                }
                                immed = u;
                        } else if (0 == strncmp(cp, "load", 4)) {
                                loej = 1;
                                if (startstop == 0)
                                        ambigu = 1;
                                else
                                        startstop = 1;
                        } else if (0 == strncmp(cp, "loej", 4))
                                loej = 1;
                        else if (0 == strncmp("pc=", cp, 3)) {
                                num = sscanf(cp + 3, "%x", &u);
                                if ((1 != num) || (u > 15)) {
                                        fprintf(stderr, "Bad value after "
                                                "after 'pc=' option\n");
                                        usage();
                                        return SG_LIB_SYNTAX_ERROR;
                                }
                                power_conds = u;
                        } else if (0 == strncmp(cp, "start", 5)) {
                                if (startstop == 0)
                                        ambigu = 1;
                                else
                                        startstop = 1;
                        } else if (0 == strncmp(cp, "stop", 4)) {
                                if (startstop == 1)
                                        ambigu = 1;
                                else
                                        startstop = 0;
                        } else if (jmp_out) {
                                fprintf(stderr, "Unrecognized option: %s\n",
                                        cp);
                                usage();
                                return SG_LIB_SYNTAX_ERROR;
                        }
                } else if (0 == strcmp("0", cp)) {
                        if (startstop >= 0)
                                ambigu = 1;
                        else
                                startstop = 0;
                } else if (0 == strcmp("1", cp)) {
                        if (startstop >= 0)
                                ambigu = 1;
                        else
                                startstop = 1;
                } else if (0 == file_name)
                        file_name = cp;
                else {
                        fprintf(stderr, "too many arguments, got: %s, not "
                                "expecting: %s\n", file_name, cp);
                        usage();
                        return SG_LIB_SYNTAX_ERROR;
                }
                if (ambigu) {
                        fprintf(stderr, "please, only one of 0, 1, --eject, "
                                "--load, --start or --stop\n");
                        usage();
                        return SG_LIB_SYNTAX_ERROR;
                }
        }
    
        if (0 == file_name) {
                fprintf(stderr, "No <scsi_device> argument given\n");
                usage();
                return SG_LIB_SYNTAX_ERROR;
        }

        if (fl_num >= 0) {
                if (startstop == 0) {
                        fprintf(stderr, "Giving '--fl=<n>' and '--stop' (or "
                                "'--eject') is invalid\n");
                        usage();
                        return SG_LIB_SYNTAX_ERROR;
                }
                if (power_conds > 0) {
                        fprintf(stderr, "Giving '--fl=<n>' and '--pc=<n>' "
                                "when <n> is non-zero is invalid\n");
                        usage();
                        return SG_LIB_SYNTAX_ERROR;
                }
        } else {
                if ((startstop == -1) && loej)
                        startstop = 0;
                if ((startstop == -1) && (0 == power_conds))
                        startstop = 1;
        }
                
        fd = sg_cmds_open_device(file_name, 0 /* rw */, verbose);
        if (fd < 0) {
                fprintf(stderr, "Error trying to open %s: %s\n",
                        file_name, safe_strerror(-fd));
                return SG_LIB_FILE_ERROR;
        }

        res = 0;
        if (fl_num >= 0)
                res = sg_ll_start_stop_unit(fd, immed, fl_num, power_conds,
                                            1 /* fl */, 1 /* loej */,
                                            1 /*start */, 1 /* noisy */,
                                            verbose);
        else if (power_conds > 0)
                res = sg_ll_start_stop_unit(fd, immed, 0, power_conds, 0, 0,
                                            0, 1, verbose);
        else if (startstop != -1)
                res = sg_ll_start_stop_unit(fd, immed, 0, 0, 0, loej,
                                            startstop, 1, verbose);
        ret = res;
        if (res) {
                if (verbose < 2) {
                        if (SG_LIB_CAT_INVALID_OP == res)
                                fprintf(stderr, "command not supported\n");
                        else if (SG_LIB_CAT_NOT_READY == res)
                                fprintf(stderr, "device not ready\n");
                        else if (SG_LIB_CAT_UNIT_ATTENTION == res)
                                fprintf(stderr, "unit attention\n");
                        else if (SG_LIB_CAT_ABORTED_COMMAND == res)
                                fprintf(stderr, "aborted command\n");
                        else if (SG_LIB_CAT_ILLEGAL_REQ == res)
                                fprintf(stderr, "invalid field in cdb\n");
                }
                fprintf(stderr, "START STOP UNIT command failed\n");
        }
        res = sg_cmds_close_device(fd);
        if ((res < 0) && (0 == ret))
                return SG_LIB_FILE_ERROR;
        return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
}