Debian Codegraph @

src/nvme/nbft.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * This file is part of libnvme.
4 * Copyright (c) 2021-2022, Dell Inc. or its subsidiaries. All Rights Reserved.
5 *
6 * Authors: Stuart Hayes <[email protected]>
7 *
8 */
9
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include <arpa/inet.h>
15 #include <ccan/endian/endian.h>
16
17 #include "private.h"
18 #include "nbft.h"
19 #include "log.h"
20
21
22 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
23
24 static __u8 csum(const __u8 *buffer, ssize_t length)
25 {
26 int n;
27 __u8 sum = 0;
28
29 for (n = 0; n < length; n++)
30 sum = (__u8)(sum + ((__u8 *)buffer)[n]);
31 return sum;
32 }
33
34 static void format_ip_addr(char *buf, size_t buflen, __u8 *addr)
35 {
36 struct in6_addr addr_ipv6;
37
38 memcpy(&addr_ipv6, addr, sizeof(addr_ipv6));
39 if (IN6_IS_ADDR_V4MAPPED(&addr_ipv6))
40 /* ipv4 */
41 inet_ntop(AF_INET, &addr_ipv6.s6_addr32[3], buf, buflen);
42 else
43 /* ipv6 */
44 inet_ntop(AF_INET6, &addr_ipv6, buf, buflen);
45 }
46
47 static bool in_heap(struct nbft_header *header, struct nbft_heap_obj obj)
48 {
49 if (le16_to_cpu(obj.length) == 0)
50 return true;
51 if (le32_to_cpu(obj.offset) < le32_to_cpu(header->heap_offset))
52 return false;
53 if (le32_to_cpu(obj.offset) >
54 le32_to_cpu(header->heap_offset) + le32_to_cpu(header->heap_length))
55 return false;
56 if (le32_to_cpu(obj.offset) + le16_to_cpu(obj.length) >
57 le32_to_cpu(header->heap_offset) + le32_to_cpu(header->heap_length))
58 return false;
59 return true;
60 }
61
62 /*
63 * Return transport_type string (NBFT Table 2)
64 */
65 static char *trtype_to_string(__u8 transport_type)
66 {
67 switch (transport_type) {
68 case 3:
69 return "tcp";
70 default:
71 return "invalid";
72 }
73 }
74
75 #define verify(condition, message) \
76 do { \
77 if (!(condition)) { \
78 nvme_msg(NULL, LOG_DEBUG, "file %s: " message "\n", \
79 nbft->filename); \
80 return -EINVAL; \
81 } \
82 } while (0)
83
84 static int __get_heap_obj(struct nbft_header *header, const char *filename,
85 const char *descriptorname, const char *fieldname,
86 struct nbft_heap_obj obj, bool is_string,
87 char **output)
88 {
89 if (le16_to_cpu(obj.length) == 0)
90 return -ENOENT;
91
92 if (!in_heap(header, obj)) {
93 nvme_msg(NULL, LOG_DEBUG,
94 "file %s: field '%s' in descriptor '%s' has invalid offset or length\n",
95 filename, fieldname, descriptorname);
96 return -EINVAL;
97 }
98
99 /* check that string is zero terminated correctly */
100 *output = (char *)header + le32_to_cpu(obj.offset);
101
102 if (is_string) {
103 if (strnlen(*output, le16_to_cpu(obj.length) + 1) < le16_to_cpu(obj.length))
104 nvme_msg(NULL, LOG_DEBUG,
105 "file %s: string '%s' in descriptor '%s' is shorter (%zd) than specified length (%d)\n",
106 filename, fieldname, descriptorname,
107 strnlen(*output, le16_to_cpu(obj.length) + 1),
108 le16_to_cpu(obj.length));
109 else if (strnlen(*output, le16_to_cpu(obj.length) + 1) >
110 le16_to_cpu(obj.length)) {
111 nvme_msg(NULL, LOG_DEBUG,
112 "file %s: string '%s' in descriptor '%s' is not zero terminated\n",
113 filename, fieldname, descriptorname);
114 return -EINVAL;
115 }
116 }
117
118 return 0;
119 }
120
121 #define get_heap_obj(descriptor, obj, is_string, output) \
122 __get_heap_obj(header, nbft->filename, \
123 stringify(descriptor), stringify(obj), \
124 descriptor->obj, is_string, \
125 output)
126
127 static struct nbft_info_discovery *discovery_from_index(struct nbft_info *nbft, int i)
128 {
129 struct nbft_info_discovery **d;
130
131 for (d = nbft->discovery_list; d && *d; d++) {
132 if ((*d)->index == i)
133 return *d;
134 }
135 return NULL;
136 }
137
138 static struct nbft_info_hfi *hfi_from_index(struct nbft_info *nbft, int i)
139 {
140 struct nbft_info_hfi **h;
141
142 for (h = nbft->hfi_list; h && *h; h++) {
143 if ((*h)->index == i)
144 return *h;
145 }
146 return NULL;
147 }
148
149 static struct nbft_info_security *security_from_index(struct nbft_info *nbft, int i)
150 {
151 struct nbft_info_security **s;
152
153 for (s = nbft->security_list; s && *s; s++) {
154 if ((*s)->index == i)
155 return *s;
156 }
157 return NULL;
158 }
159
160 static int read_ssns_exended_info(struct nbft_info *nbft,
161 struct nbft_info_subsystem_ns *ssns,
162 struct nbft_ssns_ext_info *raw_ssns_ei)
163 {
164 struct nbft_header *header = (struct nbft_header *)nbft->raw_nbft;
165
166 verify(raw_ssns_ei->structure_id == NBFT_DESC_SSNS_EXT_INFO,
167 "invalid ID in SSNS extended info descriptor");
168 verify(raw_ssns_ei->version == 1,
169 "invalid version in SSNS extended info descriptor");
170 verify(le16_to_cpu(raw_ssns_ei->ssns_index) == ssns->index,
171 "SSNS index doesn't match extended info descriptor index");
172
173 if (!(le32_to_cpu(raw_ssns_ei->flags) & NBFT_SSNS_EXT_INFO_VALID))
174 return -EINVAL;
175
176 if (le32_to_cpu(raw_ssns_ei->flags) & NBFT_SSNS_EXT_INFO_ADMIN_ASQSZ)
177 ssns->asqsz = le16_to_cpu(raw_ssns_ei->asqsz);
178 ssns->controller_id = le16_to_cpu(raw_ssns_ei->cntlid);
179 get_heap_obj(raw_ssns_ei, dhcp_root_path_str_obj, 1, &ssns->dhcp_root_path_string);
180
181 return 0;
182 }
183
184 static int read_ssns(struct nbft_info *nbft,
185 struct nbft_ssns *raw_ssns,
186 struct nbft_info_subsystem_ns **s)
187 {
188 struct nbft_header *header = (struct nbft_header *)nbft->raw_nbft;
189 struct nbft_info_subsystem_ns *ssns;
190 __u8 *ss_hfi_indexes = NULL;
191 __u8 *tmp = NULL;
192 int i, ret;
193
194 if (!(le16_to_cpu(raw_ssns->flags) & NBFT_SSNS_VALID))
195 return -EINVAL;
196
197 verify(raw_ssns->structure_id == NBFT_DESC_SSNS,
198 "invalid ID in SSNS descriptor");
199
200 /* verify transport type */
201 verify(raw_ssns->trtype == NBFT_TRTYPE_TCP,
202 "invalid transport type in SSNS descriptor");
203
204 ssns = calloc(1, sizeof(*ssns));
205 if (!ssns)
206 return -ENOMEM;
207
208 ssns->index = le16_to_cpu(raw_ssns->index);
209 strncpy(ssns->transport, trtype_to_string(raw_ssns->trtype), sizeof(ssns->transport));
210
211 /* transport specific flags */
212 if (raw_ssns->trtype == NBFT_TRTYPE_TCP) {
213 if (le16_to_cpu(raw_ssns->trflags) & NBFT_SSNS_PDU_HEADER_DIGEST)
214 ssns->pdu_header_digest_required = true;
215 if (le16_to_cpu(raw_ssns->trflags) & NBFT_SSNS_DATA_DIGEST)
216 ssns->data_digest_required = true;
217 }
218
219 /* primary discovery controller */
220 if (raw_ssns->primary_discovery_ctrl_index) {
221 ssns->discovery = discovery_from_index(nbft,
222 raw_ssns->primary_discovery_ctrl_index);
223 if (!ssns->discovery)
224 nvme_msg(NULL, LOG_DEBUG,
225 "file %s: namespace %d discovery controller not found\n",
226 nbft->filename, ssns->index);
227 }
228
229 /* subsystem transport address */
230 ret = get_heap_obj(raw_ssns, subsys_traddr_obj, 0, (char **)&tmp);
231 if (ret)
232 goto fail;
233
234 format_ip_addr(ssns->traddr, sizeof(ssns->traddr), tmp);
235
236 /* subsystem transport service identifier */
237 ret = get_heap_obj(raw_ssns, subsys_trsvcid_obj, 1, &ssns->trsvcid);
238 if (ret)
239 goto fail;
240
241 /* subsystem port ID */
242 ssns->subsys_port_id = le16_to_cpu(raw_ssns->subsys_port_id);
243
244 /* NSID, NID type, & NID */
245 ssns->nsid = le32_to_cpu(raw_ssns->nsid);
246 ssns->nid_type = raw_ssns->nidt;
247 ssns->nid = raw_ssns->nid;
248
249 /* flags */
250 ssns->unavailable = !!(le16_to_cpu(raw_ssns->flags) &
251 NBFT_SSNS_UNAVAIL_NAMESPACE_UNAVAIL);
252 ssns->discovered = !!(le16_to_cpu(raw_ssns->flags) &
253 NBFT_SSNS_DISCOVERED_NAMESPACE);
254
255 /* security profile */
256 if (raw_ssns->security_desc_index) {
257 ssns->security = security_from_index(nbft, raw_ssns->security_desc_index);
258 if (!ssns->security)
259 nvme_msg(NULL, LOG_DEBUG,
260 "file %s: namespace %d security controller not found\n",
261 nbft->filename, ssns->index);
262 }
263
264 /* HFI descriptors */
265 ret = get_heap_obj(raw_ssns, secondary_hfi_assoc_obj, 0, (char **)&ss_hfi_indexes);
266 if (ret)
267 goto fail;
268
269 ssns->hfis = calloc(le16_to_cpu(raw_ssns->secondary_hfi_assoc_obj.length) + 2,
270 sizeof(*ssns->hfis));
271 if (!ssns->hfis) {
272 ret = -ENOMEM;
273 goto fail;
274 }
275 ssns->hfis[0] = hfi_from_index(nbft, raw_ssns->primary_hfi_desc_index);
276 if (!ssns->hfis[0]) {
277 nvme_msg(NULL, LOG_DEBUG,
278 "file %s: SSNS %d: HFI %d not found\n",
279 nbft->filename, ssns->index, raw_ssns->primary_hfi_desc_index);
280 ret = -EINVAL;
281 goto fail;
282 }
283 ssns->num_hfis = 1;
284 for (i = 0; i < le16_to_cpu(raw_ssns->secondary_hfi_assoc_obj.length); i++) {
285 bool duplicate = false;
286 int j;
287
288 for (j = 0; j < i; j++) {
289 if (ss_hfi_indexes[i] == ss_hfi_indexes[j]) {
290 duplicate = true;
291 break;
292 }
293 }
294
295 if (!duplicate &&
296 ss_hfi_indexes[i] == raw_ssns->primary_hfi_desc_index)
297 duplicate = true;
298
299 if (duplicate) {
300 nvme_msg(NULL, LOG_DEBUG,
301 "file %s: SSNS %d skipping duplicate HFI index %d\n",
302 nbft->filename, ssns->index, ss_hfi_indexes[i]);
303 continue;
304 }
305
306 ssns->hfis[i + 1] = hfi_from_index(nbft, ss_hfi_indexes[i]);
307 if (ss_hfi_indexes[i] && !ssns->hfis[i + 1])
308 nvme_msg(NULL, LOG_DEBUG,
309 "file %s: SSNS %d HFI %d not found\n",
310 nbft->filename, ssns->index, ss_hfi_indexes[i]);
311 else
312 ssns->num_hfis++;
313 }
314
315 /* SSNS NQN */
316 ret = get_heap_obj(raw_ssns, subsys_ns_nqn_obj, 1, &ssns->subsys_nqn);
317 if (ret)
318 goto fail;
319
320 /* SSNS extended info */
321 if (le16_to_cpu(raw_ssns->flags) & NBFT_SSNS_EXTENDED_INFO_IN_USE) {
322 struct nbft_ssns_ext_info *ssns_extended_info;
323
324 if (!get_heap_obj(raw_ssns, ssns_extended_info_desc_obj, 0,
325 (char **)&ssns_extended_info))
326 read_ssns_exended_info(nbft, ssns, ssns_extended_info);
327 }
328
329 *s = ssns;
330 return 0;
331
332 fail:
333 free(ssns);
334 return ret;
335 }
336
337 static int read_hfi_info_tcp(struct nbft_info *nbft,
338 struct nbft_hfi_info_tcp *raw_hfi_info_tcp,
339 struct nbft_info_hfi *hfi)
340 {
341 struct nbft_header *header = (struct nbft_header *)nbft->raw_nbft;
342
343 if ((raw_hfi_info_tcp->flags & NBFT_HFI_INFO_TCP_VALID) == 0)
344 return -EINVAL;
345
346 verify(raw_hfi_info_tcp->structure_id == NBFT_DESC_HFI_TRINFO,
347 "invalid ID in HFI transport descriptor");
348 verify(raw_hfi_info_tcp->version == 1,
349 "invalid version in HFI transport descriptor");
350 if (le16_to_cpu(raw_hfi_info_tcp->hfi_index) != hfi->index)
351 nvme_msg(NULL, LOG_DEBUG,
352 "file %s: HFI descriptor index %d does not match index in HFI transport descriptor\n",
353 nbft->filename, hfi->index);
354
355 hfi->tcp_info.pci_sbdf = le32_to_cpu(raw_hfi_info_tcp->pci_sbdf);
356 memcpy(hfi->tcp_info.mac_addr, raw_hfi_info_tcp->mac_addr,
357 sizeof(raw_hfi_info_tcp->mac_addr));
358 hfi->tcp_info.vlan = le16_to_cpu(raw_hfi_info_tcp->vlan);
359 hfi->tcp_info.ip_origin = raw_hfi_info_tcp->ip_origin;
360 format_ip_addr(hfi->tcp_info.ipaddr, sizeof(hfi->tcp_info.ipaddr),
361 raw_hfi_info_tcp->ip_address);
362 hfi->tcp_info.subnet_mask_prefix = raw_hfi_info_tcp->subnet_mask_prefix;
363 format_ip_addr(hfi->tcp_info.gateway_ipaddr, sizeof(hfi->tcp_info.ipaddr),
364 raw_hfi_info_tcp->ip_gateway);
365 hfi->tcp_info.route_metric = le16_to_cpu(raw_hfi_info_tcp->route_metric);
366 format_ip_addr(hfi->tcp_info.primary_dns_ipaddr,
367 sizeof(hfi->tcp_info.primary_dns_ipaddr),
368 raw_hfi_info_tcp->primary_dns);
369 format_ip_addr(hfi->tcp_info.secondary_dns_ipaddr,
370 sizeof(hfi->tcp_info.secondary_dns_ipaddr),
371 raw_hfi_info_tcp->secondary_dns);
372 if (raw_hfi_info_tcp->flags & NBFT_HFI_INFO_TCP_DHCP_OVERRIDE) {
373 hfi->tcp_info.dhcp_override = true;
374 format_ip_addr(hfi->tcp_info.dhcp_server_ipaddr,
375 sizeof(hfi->tcp_info.dhcp_server_ipaddr),
376 raw_hfi_info_tcp->dhcp_server);
377 }
378 get_heap_obj(raw_hfi_info_tcp, host_name_obj, 1, &hfi->tcp_info.host_name);
379 if (raw_hfi_info_tcp->flags & NBFT_HFI_INFO_TCP_GLOBAL_ROUTE)
380 hfi->tcp_info.this_hfi_is_default_route = true;
381
382 return 0;
383 }
384
385 static int read_hfi(struct nbft_info *nbft,
386 struct nbft_hfi *raw_hfi,
387 struct nbft_info_hfi **h)
388 {
389 int ret;
390 struct nbft_info_hfi *hfi;
391 struct nbft_header *header = (struct nbft_header *)nbft->raw_nbft;
392
393 if (!(raw_hfi->flags & NBFT_HFI_VALID))
394 return -EINVAL;
395
396 verify(raw_hfi->structure_id == NBFT_DESC_HFI,
397 "invalid ID in HFI descriptor");
398
399 hfi = calloc(1, sizeof(struct nbft_info_hfi));
400 if (!hfi)
401 return -ENOMEM;
402
403 hfi->index = raw_hfi->index;
404
405 /*
406 * read HFI transport descriptor for this HFI
407 */
408 if (raw_hfi->trtype == NBFT_TRTYPE_TCP) {
409 /* TCP */
410 struct nbft_hfi_info_tcp *raw_hfi_info_tcp;
411
412 strncpy(hfi->transport, trtype_to_string(raw_hfi->trtype),
413 sizeof(hfi->transport));
414
415 ret = get_heap_obj(raw_hfi, trinfo_obj, 0, (char **)&raw_hfi_info_tcp);
416 if (ret)
417 goto fail;
418
419 ret = read_hfi_info_tcp(nbft, raw_hfi_info_tcp, hfi);
420 if (ret)
421 goto fail;
422 } else {
423 nvme_msg(NULL, LOG_DEBUG,
424 "file %s: invalid transport type %d\n",
425 nbft->filename, raw_hfi->trtype);
426 ret = -EINVAL;
427 goto fail;
428 }
429
430 *h = hfi;
431 return 0;
432
433 fail:
434 free(hfi);
435 return ret;
436 }
437
438 static int read_discovery(struct nbft_info *nbft,
439 struct nbft_discovery *raw_discovery,
440 struct nbft_info_discovery **d)
441 {
442 struct nbft_info_discovery *discovery = NULL;
443 struct nbft_header *header = (struct nbft_header *)nbft->raw_nbft;
444 int r = -EINVAL;
445
446 if (!(raw_discovery->flags & NBFT_DISCOVERY_VALID))
447 goto error;
448
449 verify(raw_discovery->structure_id == NBFT_DESC_DISCOVERY,
450 "invalid ID in discovery descriptor");
451
452 discovery = calloc(1, sizeof(struct nbft_info_discovery));
453 if (!discovery) {
454 r = -ENOMEM;
455 goto error;
456 }
457
458 discovery->index = raw_discovery->index;
459
460 if (get_heap_obj(raw_discovery, discovery_ctrl_addr_obj, 1, &discovery->uri))
461 goto error;
462
463 if (get_heap_obj(raw_discovery, discovery_ctrl_nqn_obj, 1, &discovery->nqn))
464 goto error;
465
466 discovery->hfi = hfi_from_index(nbft, raw_discovery->hfi_index);
467 if (raw_discovery->hfi_index && !discovery->hfi)
468 nvme_msg(NULL, LOG_DEBUG,
469 "file %s: discovery %d HFI not found\n",
470 nbft->filename, discovery->index);
471
472 discovery->security = security_from_index(nbft, raw_discovery->sec_index);
473 if (raw_discovery->sec_index && !discovery->security)
474 nvme_msg(NULL, LOG_DEBUG,
475 "file %s: discovery %d security descriptor not found\n",
476 nbft->filename, discovery->index);
477
478 *d = discovery;
479 r = 0;
480
481 error:
482 if (r)
483 free(discovery);
484 return r;
485 }
486
487 static int read_security(struct nbft_info *nbft,
488 struct nbft_security *raw_security,
489 struct nbft_info_security **s)
490 {
491 return -EINVAL;
492 }
493
494 static void read_hfi_descriptors(struct nbft_info *nbft, int num_hfi,
495 struct nbft_hfi *raw_hfi_array, int hfi_len)
496 {
497 int i, cnt;
498
499 nbft->hfi_list = calloc(num_hfi + 1, sizeof(struct nbft_info_hfi));
500 for (i = 0, cnt = 0; i < num_hfi; i++) {
501 if (read_hfi(nbft, &raw_hfi_array[i], &nbft->hfi_list[cnt]) == 0)
502 cnt++;
503 }
504 }
505
506 static void read_security_descriptors(struct nbft_info *nbft, int num_sec,
507 struct nbft_security *raw_sec_array, int sec_len)
508 {
509 int i, cnt;
510
511 nbft->security_list = calloc(num_sec + 1, sizeof(struct nbft_info_security));
512 for (i = 0, cnt = 0; i < num_sec; i++) {
513 if (read_security(nbft, &raw_sec_array[i], &nbft->security_list[cnt]) == 0)
514 cnt++;
515 }
516 }
517
518 static void read_discovery_descriptors(struct nbft_info *nbft, int num_disc,
519 struct nbft_discovery *raw_disc_array, int disc_len)
520 {
521 int i, cnt;
522
523 nbft->discovery_list = calloc(num_disc + 1, sizeof(struct nbft_info_discovery));
524 for (i = 0, cnt = 0; i < num_disc; i++) {
525 if (read_discovery(nbft, &raw_disc_array[i], &nbft->discovery_list[cnt]) == 0)
526 cnt++;
527 }
528 }
529
530 static void read_ssns_descriptors(struct nbft_info *nbft, int num_ssns,
531 struct nbft_ssns *raw_ssns_array, int ssns_len)
532 {
533 int i, cnt;
534
535 nbft->subsystem_ns_list = calloc(num_ssns + 1, sizeof(struct nbft_info_subsystem_ns));
536 for (i = 0, cnt = 0; i < num_ssns; i++) {
537 if (read_ssns(nbft, &raw_ssns_array[i], &nbft->subsystem_ns_list[cnt]) == 0)
538 cnt++;
539 }
540 }
541
542 /**
543 * parse_raw_nbft - parses raw ACPI NBFT table and fill in abstracted nbft_info structure
544 * @nbft: nbft_info struct containing only raw_nbft and raw_nbft_size
545 *
546 * Returns 0 on success, errno otherwise.
547 */
548 static int parse_raw_nbft(struct nbft_info *nbft)
549 {
550 __u8 *raw_nbft = nbft->raw_nbft;
551 int raw_nbft_size = nbft->raw_nbft_size;
552
553 struct nbft_header *header;
554 struct nbft_control *control;
555 struct nbft_host *host;
556
557 verify(raw_nbft_size >= sizeof(struct nbft_header) + sizeof(struct nbft_control),
558 "table is too short");
559 verify(csum(raw_nbft, raw_nbft_size) == 0, "invalid checksum");
560
561 /*
562 * header
563 */
564 header = (struct nbft_header *)raw_nbft;
565
566 verify(strncmp(header->signature, NBFT_HEADER_SIG, 4) == 0, "invalid signature");
567 verify(le32_to_cpu(header->length) <= raw_nbft_size, "length in header exceeds table length");
568 verify(header->major_revision == 1, "unsupported major revision");
569 verify(header->minor_revision == 0, "unsupported minor revision");
570 verify(le32_to_cpu(header->heap_length) + le32_to_cpu(header->heap_offset) <=
571 le32_to_cpu(header->length), "heap exceeds table length");
572
573 /*
574 * control
575 */
576 control = (struct nbft_control *)(raw_nbft + sizeof(struct nbft_header));
577
578 if ((control->flags & NBFT_CONTROL_VALID) == 0)
579 return 0;
580 verify(control->structure_id == NBFT_DESC_CONTROL,
581 "invalid ID in control structure");
582
583 /*
584 * host
585 */
586 verify(le32_to_cpu(control->hdesc.offset) + sizeof(struct nbft_host) <=
587 le32_to_cpu(header->length) &&
588 le32_to_cpu(control->hdesc.offset) >= sizeof(struct nbft_host),
589 "host descriptor offset/length is invalid");
590 host = (struct nbft_host *)(raw_nbft + le32_to_cpu(control->hdesc.offset));
591
592 verify(host->flags & NBFT_HOST_VALID, "host descriptor valid flag not set");
593 verify(host->structure_id == NBFT_DESC_HOST, "invalid ID in HOST descriptor");
594 nbft->host.id = (unsigned char *) &(host->host_id);
595 if (get_heap_obj(host, host_nqn_obj, 1, &nbft->host.nqn) != 0)
596 return -EINVAL;
597 nbft->host.host_id_configured = host->flags & NBFT_HOST_HOSTID_CONFIGURED;
598 nbft->host.host_nqn_configured = host->flags & NBFT_HOST_HOSTNQN_CONFIGURED;
599
600 /*
601 * HFI
602 */
603 if (control->num_hfi > 0) {
604 struct nbft_hfi *raw_hfi_array;
605
606 verify(le32_to_cpu(control->hfio) + sizeof(struct nbft_hfi) *
607 control->num_hfi <= le32_to_cpu(header->length),
608 "invalid hfi descriptor list offset");
609 raw_hfi_array = (struct nbft_hfi *)(raw_nbft + le32_to_cpu(control->hfio));
610 read_hfi_descriptors(nbft, control->num_hfi, raw_hfi_array,
611 le16_to_cpu(control->hfil));
612 }
613
614 /*
615 * security
616 */
617 if (control->num_sec > 0) {
618 struct nbft_security *raw_security_array;
619
620 verify(le32_to_cpu(control->seco) + le16_to_cpu(control->secl) *
621 control->num_sec <= le32_to_cpu(header->length),
622 "invalid security profile desciptor list offset");
623 raw_security_array = (struct nbft_security *)(raw_nbft +
624 le32_to_cpu(control->seco));
625 read_security_descriptors(nbft, control->num_sec,
626 raw_security_array,
627 le16_to_cpu(control->secl));
628 }
629
630 /*
631 * discovery
632 */
633 if (control->num_disc > 0) {
634 struct nbft_discovery *raw_discovery_array;
635
636 verify(le32_to_cpu(control->disco) + le16_to_cpu(control->discl) *
637 control->num_disc <= le32_to_cpu(header->length),
638 "invalid discovery profile descriptor list offset");
639 raw_discovery_array = (struct nbft_discovery *)(raw_nbft +
640 le32_to_cpu(control->disco));
641 read_discovery_descriptors(nbft, control->num_disc, raw_discovery_array,
642 le16_to_cpu(control->discl));
643 }
644
645 /*
646 * subsystem namespace
647 */
648 if (control->num_ssns > 0) {
649 struct nbft_ssns *raw_ssns_array;
650
651 verify(le32_to_cpu(control->ssnso) + le16_to_cpu(control->ssnsl) *
652 control->num_ssns <= le32_to_cpu(header->length),
653 "invalid subsystem namespace descriptor list offset");
654 raw_ssns_array = (struct nbft_ssns *)(raw_nbft +
655 le32_to_cpu(control->ssnso));
656 read_ssns_descriptors(nbft, control->num_ssns, raw_ssns_array,
657 le16_to_cpu(control->ssnsl));
658 }
659
660 return 0;
661 }
662
663 void nvme_nbft_free(struct nbft_info *nbft)
664 {
665 struct nbft_info_hfi **hfi;
666 struct nbft_info_security **sec;
667 struct nbft_info_discovery **disc;
668 struct nbft_info_subsystem_ns **ns;
669
670 for (hfi = nbft->hfi_list; hfi && *hfi; hfi++)
671 free(*hfi);
672 free(nbft->hfi_list);
673 for (disc = nbft->discovery_list; disc && *disc; disc++)
674 free(*disc);
675 free(nbft->discovery_list);
676 for (sec = nbft->security_list; sec && *sec; sec++)
677 free(*sec);
678 free(nbft->security_list);
679 for (ns = nbft->subsystem_ns_list; ns && *ns; ns++) {
680 free((*ns)->hfis);
681 free(*ns);
682 }
683 free(nbft->subsystem_ns_list);
684 free(nbft->raw_nbft);
685 free(nbft->filename);
686 free(nbft);
687 }
688
689 int nvme_nbft_read(struct nbft_info **nbft, const char *filename)
690 {
691 __u8 *raw_nbft = NULL;
692 size_t raw_nbft_size;
693 FILE *raw_nbft_fp = NULL;
694 int i;
695
696 /*
697 * read in raw nbft file
698 */
699 raw_nbft_fp = fopen(filename, "rb");
700 if (raw_nbft_fp == NULL) {
701 nvme_msg(NULL, LOG_ERR, "Failed to open %s: %s\n",
702 filename, strerror(errno));
703 errno = EINVAL;
704 return 1;
705 }
706
707 i = fseek(raw_nbft_fp, 0L, SEEK_END);
708 if (i) {
709 nvme_msg(NULL, LOG_ERR, "Failed to read from %s: %s\n",
710 filename, strerror(errno));
711 fclose(raw_nbft_fp);
712 errno = EINVAL;
713 return 1;
714 }
715
716 raw_nbft_size = ftell(raw_nbft_fp);
717 rewind(raw_nbft_fp);
718
719 raw_nbft = malloc(raw_nbft_size);
720 if (!raw_nbft) {
721 nvme_msg(NULL, LOG_ERR, "Failed to allocate memory for NBFT table");
722 fclose(raw_nbft_fp);
723 errno = ENOMEM;
724 return 1;
725 }
726
727 i = fread(raw_nbft, sizeof(*raw_nbft), raw_nbft_size, raw_nbft_fp);
728 if (i != raw_nbft_size) {
729 nvme_msg(NULL, LOG_ERR, "Failed to read from %s: %s\n",
730 filename, strerror(errno));
731 fclose(raw_nbft_fp);
732 free(raw_nbft);
733 errno = EINVAL;
734 return 1;
735 }
736 fclose(raw_nbft_fp);
737
738 /*
739 * alloc new struct nbft_info, add raw nbft & filename to it, and add it to the list
740 */
741 *nbft = calloc(1, sizeof(struct nbft_info));
742 if (!*nbft) {
743 nvme_msg(NULL, LOG_ERR, "Could not allocate memory for NBFT\n");
744 free(raw_nbft);
745 errno = ENOMEM;
746 return 1;
747 }
748
749 (*nbft)->filename = strdup(filename);
750 (*nbft)->raw_nbft = raw_nbft;
751 (*nbft)->raw_nbft_size = raw_nbft_size;
752
753 if (parse_raw_nbft(*nbft)) {
754 nvme_msg(NULL, LOG_ERR, "Failed to parse %s\n", filename);
755 nvme_nbft_free(*nbft);
756 errno = EINVAL;
757 return 1;
758 }
759 return 0;
760 }