File: linux-nvme.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 (214 lines) | stat: -rw-r--r-- 6,465 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
/*
 * 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 <sys/param.h>
#include <unistd.h>

#include "efiboot.h"

/*
 * support for NVMe devices
 *
 * /sys/dev/block/$major:$minor looks like:
 * 259:0 -> ../../devices/pci0000:00/0000:00:1d.0/0000:05:00.0/nvme/nvme0/nvme0n1
 * 259:1 -> ../../devices/pci0000:00/0000:00:1d.0/0000:05:00.0/nvme/nvme0/nvme0n1/nvme0n1p1
 * or:
 * 259:0 ->../../devices/virtual/nvme-fabrics/ctl/nvme0/nvme0n1
 * 259:1 ->../../devices/virtual/nvme-fabrics/ctl/nvme0/nvme0n1/nvme0n1p1
 * or:
 * 259:5 -> ../../devices/virtual/nvme-subsystem/nvme-subsys0/nvme0n1
 * 259:6 -> ../../devices/virtual/nvme-subsystem/nvme-subsys0/nvme0n1/nvme0n1p1
 *
 * /sys/dev/block/259:0/device looks like:
 * device -> ../../nvme0
 *
 * /sys/dev/block/259:1/partition looks like:
 * $ cat partition
 * 1
 *
 * /sys/class/block/nvme0n1/eui looks like:
 * $ cat /sys/class/block/nvme0n1/eui
 * 00 25 38 53 5a 16 1d a9
 */

static ssize_t
parse_nvme(struct device *dev, const char *path, const char *root UNUSED)
{
	const char *current = path;
	int i, rc;
        int32_t tosser0, tosser1, tosser2, ctrl_id, ns_id, partition;
        uint8_t *filebuf = NULL;
	int pos0 = -1, pos1 = -1, pos2 = -1;
	ssize_t sz = 0;
	struct subdir {
		const char * const name;
		const char * const fmt;
		int *pos0, *pos1;
	} subdirs[] = {
		{"nvme-subsysN/", "%nnvme-subsys%d/%n", &pos0, &pos2},
		{"ctl/", "%nctl/%n%n", &pos0, &pos1},
		{"nvme/", "%nnvme/%n%n", &pos0, &pos1},
		{NULL, }
	};

        debug("entry");

	/*
	 * in this case, *any* of these is okay.
	 */
	for (i = 0; subdirs[i].name; i++) {
		debug("searching for %s", subdirs[i].name);
		pos0 = tosser0 = pos1 = -1;
		rc = sscanf(current, subdirs[i].fmt, &pos0, &pos1, &pos2);
		debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc,
		      *subdirs[i].pos0, *subdirs[i].pos1);
		if (*subdirs[i].pos0 >= 0 && *subdirs[i].pos1 >= *subdirs[i].pos0) {
			sz += *subdirs[i].pos1;
			current += *subdirs[i].pos1;
			break;
		}
	}

	if (!subdirs[i].name)
		return 0;

	debug("searching for nvme-subsysN/");
	if (!strncmp("nvme-subsysN/", subdirs[i].name, 13)) {
		debug("searching for nvme0n1");
		rc = sscanf(current, "%nnvme%dn%d%n",
			    &pos0, &ctrl_id, &ns_id, &pos1);
		debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
		if (rc != 2)
			return 0;
	} else {
		debug("searching for nvme0/nvme0n1 or nvme0/nvme0n1/nvme0n1p1");
		rc = sscanf(current, "%nnvme%d/nvme%dn%d%n/nvme%dn%dp%d%n",
			    &pos0, &tosser0, &ctrl_id, &ns_id, &pos1,
			    &tosser1, &tosser2, &partition, &pos2);
		debug("current:'%s' rc:%d pos0:%d pos1:%d pos2:%d\n", current, rc, pos0, pos1, pos2);
		/*
		 * If it isn't of that form, it's not one of our nvme devices.
		 */
		if (rc != 3 && rc != 6)
			return 0;
		if (rc == 3)
			pos2 = pos1;
	}

        dev->nvme_info.ctrl_id = ctrl_id;
        dev->nvme_info.ns_id = ns_id;
        dev->nvme_info.has_eui = 0;
        dev->interface_type = nvme;

        if (rc == 6) {
                if (dev->part == -1)
                        dev->part = partition;

		pos1 = pos2;
        }

	current += pos1;

        /*
         * now fish the eui out of sysfs is there is one...
         */
	debug("looking for the eui");
	char *euipath = NULL;
	rc = read_sysfs_file(&filebuf, "class/block/nvme%dn%d/eui", ctrl_id, ns_id);
	if (rc < 0 && (errno == ENOENT || errno == ENOTDIR)) {
		rc = find_device_file(&euipath, "eui", "class/block/nvme%dn%d", ctrl_id, ns_id);
		if (rc >= 0 && euipath != NULL)
			rc = read_sysfs_file(&filebuf, "%s", euipath);
        }
        if (rc >= 0 && filebuf != NULL) {
                uint8_t eui[8];
                if (rc < 23) {
                        errno = EINVAL;
                        return -1;
                }
                rc = sscanf((char *)filebuf,
                            "%02hhx %02hhx %02hhx %02hhx "
                            "%02hhx %02hhx %02hhx %02hhx",
                            &eui[0], &eui[1], &eui[2], &eui[3],
                            &eui[4], &eui[5], &eui[6], &eui[7]);
                if (rc < 8) {
                        errno = EINVAL;
                        return -1;
                }
		debug("eui is %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
		      eui[0], eui[1], eui[2], eui[3],
		      eui[4], eui[5], eui[6], eui[7]);
                dev->nvme_info.has_eui = 1;
                memcpy(dev->nvme_info.eui, eui, sizeof(eui));
        }

	debug("current:'%s' sz:%zd", current, current - path);
	return current - path;
}

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

        debug("entry");

        sz = efidp_make_nvme(buf + off, size ? size - off : 0,
                             dev->nvme_info.ns_id,
                             dev->nvme_info.has_eui ? dev->nvme_info.eui
                                                        : NULL);
        return sz;
}

static char *
make_part_name(struct device *dev)
{
        char *ret = NULL;
        ssize_t rc;

        if (dev->part < 1)
                return NULL;

        rc = asprintf(&ret, "%sp%d", dev->disk_name, dev->part);
        if (rc < 0) {
                efi_error("could not allocate memory");
                return NULL;
        }

        return ret;
}

static enum interface_type nvme_iftypes[] = { nvme, unknown };

struct dev_probe HIDDEN nvme_parser = {
        .name = "nvme",
        .iftypes = nvme_iftypes,
        .flags = DEV_PROVIDES_HD,
        .parse = parse_nvme,
        .create = dp_create_nvme,
        .make_part_name = make_part_name,
};