Merge tag 'linux-kselftest-5.2-rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / fmc / fru-parse.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2012 CERN (www.cern.ch)
4  * Author: Alessandro Rubini <rubini@gnudd.com>
5  *
6  * This work is part of the White Rabbit project, a research effort led
7  * by CERN, the European Institute for Nuclear Research.
8  */
9 #include <linux/ipmi-fru.h>
10
11 /* Some internal helpers */
12 static struct fru_type_length *
13 __fru_get_board_tl(struct fru_common_header *header, int nr)
14 {
15         struct fru_board_info_area *bia;
16         struct fru_type_length *tl;
17
18         bia = fru_get_board_area(header);
19         tl = bia->tl;
20         while (nr > 0 && !fru_is_eof(tl)) {
21                 tl = fru_next_tl(tl);
22                 nr--;
23         }
24         if (fru_is_eof(tl))
25                 return NULL;
26         return tl;
27 }
28
29 static char *__fru_alloc_get_tl(struct fru_common_header *header, int nr)
30 {
31         struct fru_type_length *tl;
32         char *res;
33
34         tl = __fru_get_board_tl(header, nr);
35         if (!tl)
36                 return NULL;
37
38         res = fru_alloc(fru_strlen(tl) + 1);
39         if (!res)
40                 return NULL;
41         return fru_strcpy(res, tl);
42 }
43
44 /* Public checksum verifiers */
45 int fru_header_cksum_ok(struct fru_common_header *header)
46 {
47         uint8_t *ptr = (void *)header;
48         int i, sum;
49
50         for (i = sum = 0; i < sizeof(*header); i++)
51                 sum += ptr[i];
52         return (sum & 0xff) == 0;
53 }
54 int fru_bia_cksum_ok(struct fru_board_info_area *bia)
55 {
56         uint8_t *ptr = (void *)bia;
57         int i, sum;
58
59         for (i = sum = 0; i < 8 * bia->area_len; i++)
60                 sum += ptr[i];
61         return (sum & 0xff) == 0;
62 }
63
64 /* Get various stuff, trivial */
65 char *fru_get_board_manufacturer(struct fru_common_header *header)
66 {
67         return __fru_alloc_get_tl(header, 0);
68 }
69 char *fru_get_product_name(struct fru_common_header *header)
70 {
71         return __fru_alloc_get_tl(header, 1);
72 }
73 char *fru_get_serial_number(struct fru_common_header *header)
74 {
75         return __fru_alloc_get_tl(header, 2);
76 }
77 char *fru_get_part_number(struct fru_common_header *header)
78 {
79         return __fru_alloc_get_tl(header, 3);
80 }