xref: /openbmc/openbmc-test-automation/lib/vpd_utils.py (revision 21e78943c05463ac235a62708bad4dc42130ff0b)
1#!/usr/bin/env python3
2
3r"""
4VPD functions.
5"""
6
7import json
8
9import bmc_ssh_utils as bsu
10import func_args as fa
11
12
13class VpdtoolException(Exception):
14    r"""
15    Base class for vpdtool related exceptions.
16    """
17
18    def __init__(self, message):
19        self.message = message
20        super().__init__(self.message)
21
22
23def vpdtool(option_string, **bsu_options):
24    r"""
25    Run vpdtool on the BMC with the caller's option string and return the result.
26
27    Example:
28
29    ${vpd_results}=  vpd-tool -i
30    Rprint Vars  vpd_results
31
32    vpd_results:
33      [/system/chassis/motherboard]:
34        [PN]:                PN12345
35        [SN]:                YL2E2D010000
36        [LocationCode]:      U78DA.ND1.       -P0
37        [CC]:                2E2D
38        [DR]:                SYSTEM BACKPLANE
39        [FN]:                F191014
40        [type]:              xyz.openbmc_project.Inventory.Item.Board.Motherboard
41      [/system/chassis/motherboard/ebmc_card_bmc]:
42        [PN]:                PN12345
43        [SN]:                YL6B58010000
44        [LocationCode]:      U78DA.ND1.       -P0-C5
45        [CC]:                6B58
46        [DR]:                EBMC
47        [FN]:                F191014
48        [type]:              xyz.openbmc_project.Inventory.Item.Bmc
49
50    Description of argument(s):
51    option_string            A string of options which are to be processed by
52                             the vpd-tool command.
53    bsu_options              Options to be passed directly to bmc_execute_command.
54                             See its prolog for details.
55    """
56
57    bsu_options = fa.args_to_objects(bsu_options)
58    out_buf, stderr, rc = bsu.bmc_execute_command(
59        "vpd-tool " + option_string, **bsu_options
60    )
61
62    # Only return output if its not a VPD write command.
63    try:
64        if "-w" not in option_string:
65            out_buf = json.loads(out_buf)
66            if "-r" in option_string:
67                return out_buf
68            else:
69                return out_buf[0]
70    except Exception as exception:
71        raise VpdtoolException(
72            "Failed to get VPD data from BMC : " + str(exception)
73        ) from exception
74