File: linux-scsi.c

package info (click to toggle)
efivar 37-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,648 kB
  • sloc: ansic: 10,605; makefile: 234; asm: 34
file content (304 lines) | stat: -rw-r--r-- 11,622 bytes parent folder | download | duplicates (2)
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
/*
 * libefiboot - library for the manipulation of EFI boot variables
 * Copyright 2012-2018 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include "fix_coverity.h"

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdint.h>
#include <unistd.h>

#include "efiboot.h"

/*
 * support for Old-school SCSI devices
 */

/*
 * helper for scsi formats...
 */
ssize_t HIDDEN
parse_scsi_link(const char *current, uint32_t *scsi_host,
                uint32_t *scsi_bus, uint32_t *scsi_device,
                uint32_t *scsi_target, uint64_t *scsi_lun,
                uint32_t *local_port_id, uint32_t *remote_port_id,
                uint32_t *remote_target_id)
{
        int rc;
        int sz = 0;
        int pos0 = 0, pos1 = 0;

        debug("entry");
        /*
         * This structure is completely ridiculous.
         *
         * /dev/sdc as SAS looks like:
         * /sys/dev/block/8:32 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host4/port-4:0/end_device-4:0/target4:0:0/4:0:0:0/block/sdc
         * /dev/sdc1 looks like:
         * /sys/dev/block/8:33 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host4/port-4:0/end_device-4:0/target4:0:0/4:0:0:0/block/sdc/sdc1
         *
         * OR
         *
         * /dev/sdc as SAS looks like:
         * /sys/dev/block/8:32 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host4/port-4:2:0/end_device-4:2:0/target4:2:0/4:2:0:0/block/sdc
         * /dev/sdc1 looks like:
         * /sys/dev/block/8:33 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host4/port-4:2:0/end_device-4:2:0/target4:2:0/4:2:0:0/block/sdc/sdc1
         *
         * /sys/block/sdc/device looks like:
         * device-> ../../../4:2:0:0
         *
         * OR
         *
         * 8:0 -> ../../devices/pci0000:74/0000:74:02.0/host2/port-2:0/expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda
         * 8:1 -> ../../devices/pci0000:74/0000:74:02.0/host2/port-2:0/expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
         *
         * /sys/block/sda/device looks like:
         * device -> ../../../2:0:0:0 *
         *
         * sas_address exists, but it's hard to find:
         * /sys/devices/pci0000:74/0000:74:02.0/host2/port-2:0/expander-2:0/sas_device/expander-2:0/sas_address
         * but sas_host_address is nowhere to be found, and sas_address
         * doesn't directly exist under /sys/class/ anywhere.  So you actually
         * have to go to
         * /sys/devices/pci0000:74/0000:74:02.0/host2/port-2:0/expander-2:0/sas_device/expander-2:0/sas_address
         * and chop that off to
         * /sys/devices/pci0000:74/0000:74:02.0/host2/port-2:0/expander-2:0/
         * and then add a bunch of port and end device crap to it to get:
         * /sys/devices/pci0000:74/0000:74:02.0/host2/port-2:0/expander-2:0/port-2:0:2/end_device-2:0:2/sas_device/end_device-2:0:2/sas_address

         */

        /*
         * So we start when current is:
         * host4/port-4:0/end_device-4:0/target4:0:0/4:0:0:0/block/sdc/sdc1
         * or
         * host2/port-2:0/expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
         */
        uint32_t tosser0, tosser1, tosser2;

        /* ignore a bunch of stuff
         *    host4/port-4:0
         * or host4/port-4:0:0
         */
        debug("searching for host4/");
        rc = sscanf(current, "host%d/%n", scsi_host, &pos0);
        debug("current:\"%s\" rc:%d pos0:%d\n", current+sz, rc, pos0);
        if (rc != 1)
                return -1;
        sz += pos0;
        pos0 = 0;

        /*
         * We might have this next:
         * port-2:0/expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
         * or:
         * port-2:0/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
         * or maybe (not sure):
         * port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
         */
        debug("searching for port-4:0 or port-4:0:0");
        rc = sscanf(current+sz, "port-%d:%d%n:%d%n", &tosser0,
                    &tosser1, &pos0, &tosser2, &pos1);
        debug("current:\"%s\" rc:%d pos0:%d pos1:%d\n", current+sz, rc, pos0, pos1);
        if (rc == 2 || rc == 3) {
                sz += pos0;
                pos0 = 0;
                if (local_port_id && rc == 2)
                        *local_port_id = tosser1;
                if (remote_port_id && rc == 3)
                        *remote_port_id = tosser2;

                if (current[sz] == '/')
                        sz += 1;

                /*
                 * We might have this next:
                 * expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
                 *                       ^ port id
                 *                     ^ scsi target id
                 *                   ^ host number
                 *          ^ host number
                 * We don't actually care about either number in expander-.../,
                 * because they're replicated in all the other places.  We just need
                 * to get past it.
                 */
                debug("searching for expander-4:0/");
                rc = sscanf(current+sz, "expander-%d:%d/%n", &tosser0, &tosser1, &pos0);
                debug("current:\"%s\" rc:%d pos0:%d\n", current+sz, rc, pos0);
                if (rc == 2) {
                        if (!remote_target_id) {
                                efi_error("Device is PHY is a remote target, but remote_target_id is NULL");
                                return -1;
                        }
                        *remote_target_id = tosser1;
                        sz += pos0;
                        pos0 = 0;

                        /*
                         * if we have that, we should have a 3-part port next
                         */
                        debug("searching for port-2:0:2/");
                        rc = sscanf(current+sz, "port-%d:%d:%d/%n", &tosser0, &tosser1, &tosser2, &pos0);
                        debug("current:\"%s\" rc:%d pos0:%d\n", current+sz, rc, pos0);
                        if (rc != 3) {
                                efi_error("Couldn't parse port expander port string");
                                return -1;
                        }
                        sz += pos0;
                }
                pos0 = 0;

                /* next:
                 *    /end_device-4:0
                 * or /end_device-4:0:0
                 * awesomely these are the exact same fields that go into port-blah,
                 * but we don't care for now about any of them anyway.
                 */
                debug("searching for end_device-4:0/ or end_device-4:0:0/");
                rc = sscanf(current + sz, "end_device-%d:%d%n", &tosser0, &tosser1, &pos0);
                debug("current:\"%s\" rc:%d pos0:%d\n", current+sz, rc, pos0);
                if (rc != 2)
                        return -1;

                pos1 = 0;
                rc = sscanf(current + sz + pos0, ":%d%n", &tosser2, &pos1);
                if (rc != 0 && rc != 1)
                        return -1;
                if (remote_port_id && rc == 1)
                        *remote_port_id = tosser2;
                if (local_port_id && rc == 0)
                        *local_port_id = tosser1;
                sz += pos0 + pos1;
                pos0 = pos1 = 0;

                if (current[sz] == '/')
                        sz += 1;
        } else if (rc != 0) {
                return -1;
        }

        /* now:
         * /target4:0:0/
         */
        uint64_t tosser3;
        debug("searching for target4:0:0/");
        rc = sscanf(current + sz, "target%d:%d:%"PRIu64"/%n", &tosser0, &tosser1,
                    &tosser3, &pos0);
        debug("current:\"%s\" rc:%d pos0:%d\n", current+sz, rc, pos0);
        if (rc != 3)
                return -1;
        sz += pos0;
        pos0 = 0;

        /* now:
         * %d:%d:%d:%llu/
         */
        debug("searching for 4:0:0:0/");
        rc = sscanf(current + sz, "%d:%d:%d:%"PRIu64"/%n",
                    scsi_bus, scsi_device, scsi_target, scsi_lun, &pos0);
        debug("current:\"%s\" rc:%d pos0:%d\n", current+sz, rc, pos0);
        if (rc != 4)
                return -1;
        sz += pos0;

        debug("returning %d", sz);
        return sz;
}

static ssize_t
parse_scsi(struct device *dev, const char *current, const char *root UNUSED)
{
        uint32_t scsi_host, scsi_bus, scsi_device, scsi_target;
        uint64_t scsi_lun;
        ssize_t sz;
        int pos;
        int rc;

        debug("entry");

        debug("searching for ../../../0:0:0:0");
        rc = sscanf(dev->device, "../../../%d:%d:%d:%"PRIu64"%n",
                    &dev->scsi_info.scsi_bus,
                    &dev->scsi_info.scsi_device,
                    &dev->scsi_info.scsi_target,
                    &dev->scsi_info.scsi_lun,
                    &pos);
        debug("current:\"%s\" rc:%d pos:%d\n", dev->device, rc, pos);
        if (rc != 4)
                return 0;

        sz = parse_scsi_link(current, &scsi_host,
                              &scsi_bus, &scsi_device,
                              &scsi_target, &scsi_lun,
                              NULL, NULL, NULL);
        if (sz < 0)
                return 0;

        /*
         * SCSI disks can have up to 16 partitions, or 4 bits worth
         * and have one bit for the disk number.
         */
        if (dev->major == 8) {
                dev->interface_type = scsi;
                dev->disknum = (dev->minor >> 4);
                set_part(dev, dev->minor & 0xF);
        } else if (dev->major >= 65 && dev->major <= 71) {
                dev->interface_type = scsi;
                dev->disknum = 16*(dev->major-64) + (dev->minor >> 4);
                set_part(dev, dev->minor & 0xF);
        } else if (dev->major >= 128 && dev->major <= 135) {
                dev->interface_type = scsi;
                dev->disknum = 16*(dev->major-128) + (dev->minor >> 4);
                set_part(dev, dev->minor & 0xF);
        } else {
                efi_error("couldn't parse scsi major/minor");
                return -1;
        }

        return sz;
}

static ssize_t
dp_create_scsi(struct device *dev,
               uint8_t *buf,  ssize_t size, ssize_t off)
{
        ssize_t sz = 0;

        debug("entry");

        sz = efidp_make_scsi(buf + off, size ? size - off : 0,
                             dev->scsi_info.scsi_target,
                             dev->scsi_info.scsi_lun);
        if (sz < 0)
                efi_error("efidp_make_scsi() failed");

        return sz;
}

enum interface_type scsi_iftypes[] = { scsi, unknown };

struct dev_probe HIDDEN scsi_parser = {
        .name = "scsi",
        .iftypes = scsi_iftypes,
        .flags = DEV_PROVIDES_HD,
        .parse = parse_scsi,
        .create = dp_create_scsi,
};