summaryrefslogtreecommitdiff
path: root/lib/device/parse_vpd.c
blob: 4bafa7b9eff66aa9d58088cf15723dc2fc69ebe9 (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
/*
 * Copyright (C) 2022 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "base/memory/zalloc.h"
#include "lib/misc/lib.h"
#include "lib/device/device.h"

#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>
#include <stdbool.h>
#include <assert.h>

/*
 * Replace series of spaces with a single _.
 */
int format_t10_id(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes)
{
	int in_space = 0;
	int retlen = 0;
	int j = 0;
	int i;

	for (i = 0; i < in_bytes; i++) {
		if (!in[i])
			break;
		if (j >= (out_bytes - 2))
			break;
		/* skip leading spaces */
		if (!retlen && (in[i] == ' '))
			continue;
		/* replace one or more spaces with _ */
		if (in[i] == ' ') {
			in_space = 1;
			continue;
		}
		/* spaces are finished so insert _ */
		if (in_space) {
			out[j++] = '_';
			in_space = 0;
			retlen++;
		}
		out[j++] = in[i];
		retlen++;
	}
	return retlen;
}

static int _to_hex(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes)
{
	int off = 0;
	int num;
	int i;

	for (i = 0; i < in_bytes; i++) {
		num = sprintf((char *)out + off, "%02x", in[i]);
		if (num < 0)
			break;
		off += num;
		if (off + 2 >= out_bytes)
			break;
	}
	return off;
}

#define ID_BUFSIZE 1024

/*
 * based on linux kernel function
 */
int parse_vpd_ids(const unsigned char *vpd_data, int vpd_datalen, struct dm_list *ids)
{
	char id[ID_BUFSIZE];
	unsigned char tmp_str[ID_BUFSIZE];
	const unsigned char *d, *cur_id_str;
	size_t id_len = ID_BUFSIZE;
	int id_size = -1;
	uint8_t cur_id_size = 0;

	memset(id, 0, ID_BUFSIZE);
	for (d = vpd_data + 4;
	     d < vpd_data + vpd_datalen;
	     d += d[3] + 4) {
		memset(tmp_str, 0, sizeof(tmp_str));

		switch (d[1] & 0xf) {
		case 0x1:
			/* T10 Vendor ID */
			cur_id_size = d[3];
			if (cur_id_size + 4 > id_len)
				cur_id_size = id_len - 4;
			cur_id_str = d + 4;
			format_t10_id(cur_id_str, cur_id_size, tmp_str, sizeof(tmp_str));
			id_size = snprintf(id, ID_BUFSIZE, "t10.%s", tmp_str);
			if (id_size < 0)
				break;
			if (id_size >= ID_BUFSIZE)
				id_size = ID_BUFSIZE - 1;
			add_wwid(id, 1, ids);
			break;
		case 0x2:
			/* EUI-64 */
			cur_id_size = d[3];
			cur_id_str = d + 4;
			switch (cur_id_size) {
			case 8:
				_to_hex(cur_id_str, 8, tmp_str, sizeof(tmp_str));
				id_size = snprintf(id, ID_BUFSIZE, "eui.%s", tmp_str);
				break;
			case 12:
				_to_hex(cur_id_str, 12, tmp_str, sizeof(tmp_str));
				id_size = snprintf(id, ID_BUFSIZE, "eui.%s", tmp_str);
				break;
			case 16:
				_to_hex(cur_id_str, 16, tmp_str, sizeof(tmp_str));
				id_size = snprintf(id, ID_BUFSIZE, "eui.%s", tmp_str);
				break;
			default:
				break;
			}
			if (id_size < 0)
				break;
			if (id_size >= ID_BUFSIZE)
				id_size = ID_BUFSIZE - 1;
			add_wwid(id, 2, ids);
			break;
		case 0x3:
			/* NAA */
			cur_id_size = d[3];
			cur_id_str = d + 4;
			switch (cur_id_size) {
			case 8:
				_to_hex(cur_id_str, 8, tmp_str, sizeof(tmp_str));
				id_size = snprintf(id, ID_BUFSIZE, "naa.%s", tmp_str);
				break;
			case 16:
				_to_hex(cur_id_str, 16, tmp_str, sizeof(tmp_str));
				id_size = snprintf(id, ID_BUFSIZE, "naa.%s", tmp_str);
				break;
			default:
				break;
			}
			if (id_size < 0)
				break;
			if (id_size >= ID_BUFSIZE)
				id_size = ID_BUFSIZE - 1;
			add_wwid(id, 3, ids);
			break;
		case 0x8:
			/* SCSI name string */
			cur_id_size = d[3];
			cur_id_str = d + 4;
			if (cur_id_size >= id_len)
				cur_id_size = id_len - 1;
			memcpy(id, cur_id_str, cur_id_size);
			id_size = cur_id_size;

			/*
			 * Not in the kernel version, copying multipath code,
			 * which checks if this string begins with naa or eui
			 * and if so does tolower() on the chars.
			 */
			if (!strncmp(id, "naa.", 4) || !strncmp(id, "eui.", 4)) {
				int i;
				for (i = 0; i < id_size; i++)
					id[i] = tolower(id[i]);
			}
			add_wwid(id, 8, ids);
			break;
		default:
			break;
		}
	}

	return id_size;
}