blob: fdcb191a11e21be78b1df331f77e56191d41c0ce [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
Rahul Maheshwarief003062020-03-23 07:17:16 -05002
3r"""
4VPD functions.
5"""
6
George Keishing6087e462022-09-19 08:01:44 -05007import json
Patrick Williams20f38712022-12-08 06:18:26 -06008
George Keishinge635ddc2022-12-08 07:38:02 -06009import bmc_ssh_utils as bsu
Patrick Williams20f38712022-12-08 06:18:26 -060010import func_args as fa
Rahul Maheshwarief003062020-03-23 07:17:16 -050011
12
Sridevi Ramesh21e78942025-01-26 03:06:18 -060013class 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
Rahul Maheshwarief003062020-03-23 07:17:16 -050023def 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]:
George Keishing6087e462022-09-19 08:01:44 -050034 [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
Rahul Maheshwarief003062020-03-23 07:17:16 -050041 [/system/chassis/motherboard/ebmc_card_bmc]:
George Keishing6087e462022-09-19 08:01:44 -050042 [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
Rahul Maheshwarief003062020-03-23 07:17:16 -050049
50 Description of argument(s):
George Keishing6087e462022-09-19 08:01:44 -050051 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.
Rahul Maheshwarief003062020-03-23 07:17:16 -050055 """
56
57 bsu_options = fa.args_to_objects(bsu_options)
Patrick Williams20f38712022-12-08 06:18:26 -060058 out_buf, stderr, rc = bsu.bmc_execute_command(
59 "vpd-tool " + option_string, **bsu_options
60 )
Rahul Maheshwari0ef1e152020-11-02 23:10:55 -060061
Rahul Maheshwari7e9191b2020-12-08 23:55:04 -060062 # Only return output if its not a VPD write command.
Sridevi Ramesh21e78942025-01-26 03:06:18 -060063 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