1*** Settings ***
2Documentation  Test IPMI FRU data.
3
4Resource               ../lib/ipmi_client.robot
5Resource               ../lib/bmc_dbus.robot
6Variables              ../data/ipmi_raw_cmd_table.py
7Library                ../lib/ipmi_utils.py
8
9*** Variables ***
10${FRU_NAME}       dimm01 dimm02 cpu0 cpu1  motherboard
11${BUSCTL_FRU}                xyz.openbmc_project.FruDevice
12${FRU_DBUS_URL}              /xyz/openbmc_project/FruDevice
13${fru_device_id}             0x00
14${fru_device_id_invalid}     0xff
15${read_write_offset}         0x00 0x00
16&{dbus_dict}
17&{ipmi_dbus_name_mapping}    Chassis Part Number=.CHASSIS_PART_NUMBER
18...  Board Mfg Date=.BOARD_MANUFACTURE_DATE  Board Mfg=.BOARD_MANUFACTURER
19...  Board Product=.BOARD_PRODUCT_NAME  Board Serial=.BOARD_SERIAL_NUMBER
20...  Board Part Number=.BOARD_PART_NUMBER  Product Manufacturer=.PRODUCT_MANUFACTURER
21...  Product Name=.PRODUCT_PRODUCT_NAME  Product Part Number=.PRODUCT_PART_NUMBER
22...  Product Version=.PRODUCT_VERSION  Product Serial=.PRODUCT_SERIAL_NUMBER
23
24*** Test Cases ***
25Test FRU Device Name
26    [Documentation]  Search FRU for device name
27    [Tags]  Test_FRU_Device_Name
28
29    ${output}=  Run External IPMI Standard Command  fru
30    Should Contain  ${output}  ${FRU_NAME}  msg=Fail: Given FRU device ${FRU_NAME} not found
31
32
33Verify Fru Device Configuration
34    [Documentation]  Read the FRU device configuration of each device
35    ...  and compare with DBUS data.
36    [Tags]  Verify_Fru_Device_Configuration
37
38    # IPMI FRU print.
39    ${ipmi_output}=  Run External IPMI Standard Command  fru
40
41    # Create dictionary with FRU device serial number as key and details as value from IPMI.
42    ${ipmi_fru}=  Get IPMI FRU Devices Data  ${ipmi_output}
43
44    # Returns all the available FRU dbus uri.
45    ${dbus_fru_uri}=  Get DBUS URI List From BMC  ${BUSCTL_FRU}  ${FRU_DBUS_URL}
46
47    # Returns all the FRU device uri with special characters removed.
48    ${dbus_fru_uri_list}=  Fetch DBUS URI List Without Unicode  ${dbus_fru_uri}
49
50    # Creates dictionary with serial number as key, and corresponding FRU device uri as value from dbus.
51    Get DBUS Dictionary For FRU Devices  ${dbus_fru_uri_list}  ${ipmi_fru}
52
53    # Compare dbus dictionary each field, with IPMI FRU device fields for each FRU device.
54    Compare IPMI FRU with DBUS  ${ipmi_fru}
55
56
57Verify Get FRU Inventory Area Info
58    [Documentation]  Verify IPMI get FRU inventory area info command.
59    [Tags]  Verify_Get_FRU_Inventory_Area_Info
60
61    # IPMI read FRU data command.
62    ${resp}=  Read FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}
63    ${bytes_read}=  Set Variable  ${resp.split()[0]}
64
65    # IPMI get FRU inventory area info command.
66    ${bytes_inventory}=  Get FRU Inventory Area Info
67
68    # Compare read FRU data Count returned -- count is ‘1’ based, with inventory area count.
69    Should Be Equal  ${bytes_inventory}  ${bytes_read}
70
71
72Verify Get FRU Inventory Area Info For Invalid Device Data
73    [Documentation]  Verify IPMI get FRU inventory area info command for Invalid Device Data.
74    [Tags]  Verify_Get_FRU_Inventory_Area_Info_For_Invalid_Device_Data
75
76    # Verify response for invalid FRU device id.
77    Run Keyword and Expect Error  *${IPMI_RAW_CMD['FRU']['Inventory_Area_Info'][1]}*
78    ...  Run IPMI Standard Command  raw ${IPMI_RAW_CMD['FRU']['Inventory_Area_Info'][0]} ${fru_device_id_invalid}
79
80
81Verify Get FRU Inventory Area Info For Invalid Data Request
82    [Documentation]  Verify IPMI get FRU inventory area info command for Invalid Data Request.
83    [Tags]  Verify_Get_FRU_Inventory_Area_Info_For_Invalid_Data_Request
84
85    # Verify response for invalid response data - extra bytes.
86    Run Keyword and Expect Error  *${IPMI_RAW_CMD['FRU']['Inventory_Area_Info'][2]}*
87    ...  Run IPMI Standard Command  raw ${IPMI_RAW_CMD['FRU']['Inventory_Area_Info'][0]} ${fru_device_id} 0x00
88
89
90Verify IPMI Write FRU Data
91    [Documentation]  Verify write data in FRU and compare data from read FRU data command via IPMI.
92    [Tags]  Verify_IPMI_Write_FRU_Data
93    [Setup]  Get Default FRU Data
94    [Teardown]  Restore Default FRU Data
95
96    # Generate random data to write in FRU device.
97    ${write_data_prefixed}  ${write_data}=  Generate Random Data For FRU
98
99    # Get the length of the data generated and convert to hex.
100    ${write_data_length}=  Get Length  ${write_data}
101    ${write_data_length}=  Convert To Hex  ${write_data_length}  lowercase=yes
102
103    # Write the data to FRU device.
104    Write FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}  ${write_data_prefixed}  ${write_data_length}
105
106    # Read the FRU data.
107    ${resp}=  Read FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}
108    ${resp_data}=  Set Variable  ${resp.split()[1:]}
109
110    # Verify if the data written and read are same.
111    Should Be Equal  ${write_data}  ${resp_data}
112
113
114Verify IPMI Write FRU Data With BMC Reboot
115    [Documentation]  Verify IPMI write data in FRU and compare data from read FRU data command after BMC reboot.
116    [Tags]  Verify_IPMI_Write_FRU_Data_With_BMC_Reboot
117    [Setup]  Get Default FRU Data
118    [Teardown]  Restore Default FRU Data
119
120    # Generate random data to write in FRU device.
121    ${write_data_prefixed}  ${write_data}=  Generate Random Data For FRU
122
123    # Get the length of the data generated and convert to hex.
124    ${write_data_length}=  Get Length  ${write_data}
125    ${write_data_length}=  Convert To Hex  ${write_data_length}  lowercase=yes
126
127    # Write the data to FRU device.
128    Write FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}  ${write_data_prefixed}  ${write_data_length}
129
130    # Read the FRU data.
131    ${resp}=  Read FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}
132    ${resp_data}=  Set Variable  ${resp.split()[1:]}
133
134    # Verify if the data written and read are same.
135    Should Be Equal  ${write_data}  ${resp_data}
136
137    # Reboot BMC and verify if the data written and read are same.
138    IPMI MC Reset Cold (run)
139    ${resp}=  Read FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}
140    Should Not Be Equal  ${resp}  ${initial_fru_data}
141    Should Be Equal  ${resp[1:]}  ${write_data}
142
143
144*** Keywords ***
145
146Get IPMI FRU Devices Data
147    [Documentation]  Get response from IPMI FRU command and format data
148    ...  with Board or Product serial as key and corresponding data as value.
149    [Arguments]  ${ipmi_output}
150
151    # Description of Argument(s):
152    # ipmi_output        All the FRU devices listed in IPMI FRU command.
153
154    # Get the FRU list and return as a dictionary with serial number as key.
155    # Example:
156    # fru_data = {
157    # "123456789012345XYZ":
158    #  {
159    #     FRU Device Description : Builtin FRU Device (ID 0),
160    #     Chassis Type          : Rack Mount Chassis,
161    #     Chassis Part Number   : xxx-xxxxx-xxxx-xxx,
162    #     Board Mfg Date        : Fri Oct 16 06:34:00 2020 UTC,
163    #     Board Mfg             : XXXXX,
164    #     Board Product         : XXXXX,
165    #     Board Serial          : 123456789012345XYZ,
166    #     Board Part Number     : xxx.xxxxx.xxxx
167    #     Board Extra           : 01
168    #     Product Manufacturer  : XXXXX
169    #     Product Name          : XXXXX
170    #     Product Part Number   : xxx-xxxx-xxxx-xxx
171    #     Product Version       : v1.0
172    #     Product Serial        : 1234567890XYZ
173    #  },....}
174
175    # Gets response from FRU data and split each device.
176    ${output}=  Set Variable  ${ipmi_output.strip("\n")}
177    ${output}=  Split String  ${output}  \n\n
178    &{fru}=  Create Dictionary
179    ${num}=  Set Variable  0
180
181    # For each device, identify either Board Serial/Product Serial (whichever is available).
182    FOR  ${devices}  IN  @{output}
183        &{tmp}=  Create Dictionary
184        ${dev}=  Split String  ${devices}  \n
185        FOR  ${device}  IN  @{dev}
186            ${ipmi_fru_board_serial_status}=  Run Keyword And Return Status  Should Contain  ${device}  Board Serial
187            Exit For Loop If  '${ipmi_fru_board_serial_status}' == 'True'
188        END
189        ${frudata}=  Get From List  ${output}  ${num}
190        ${serial_no}=  Run Keyword If  '${ipmi_fru_board_serial_status}' == 'True'
191        ...    Get Lines Containing String  ${frudata}  Board Serial
192        ...  ELSE
193        ...    Get Lines Containing String  ${frudata}  Product Serial
194
195        # Get each device and split field as key and value and append to a dictionary.
196        ${serial_nos}=  Set Variable  ${serial_no.strip()}
197        ${data}=  Split String  ${serial_nos}  :
198        ${serial_number}=  Get From List  ${data}  1
199        ${num}=  Evaluate  int(${num}) + 1
200        FOR  ${entry}  IN  @{dev}
201            ${entry}=  Split String  ${entry}  ${SPACE}:${SPACE}
202            ${entry1}=  Set Variable  ${entry[0].strip()}
203            ${entry2}=  Set Variable  ${entry[1].strip()}
204            Set To Dictionary  ${tmp}  ${entry1}  ${entry2}
205        END
206        ${serial_number}=  Set Variable  ${serial_number.strip()}
207        # Assign serial number as key for main dictionary and a each device detail as value.
208        Set To Dictionary  ${fru}  ${serial_number}  ${tmp}
209    END
210
211    [Return]  ${fru}
212
213
214Get DBUS Dictionary For FRU Devices
215    [Documentation]  Provides the dictionary of DBUS FRU devices from DBUS FRU.
216    [Arguments]  ${dbus_fru}  ${ipmi_fru}
217
218    # Description of Argument(s):
219    # dbus_fru    FRU dbus uri list.
220    # ipmi_fru    IPMI FRU details.
221
222    # Execute DBUS Introspect Command for each device,
223    # Appends dictionary with serial number as key and FRU dbus uri as value,
224    # if the IPMI FRU key matches the serial number of each device dbus response.
225    # Example :
226    #    ${dbus_output} = { "123456789012345XYZ" : "xyz.openbmc_project.FruDevice/xyz/openbmc_project/FruDevice/Device_0" }
227    FOR  ${fru}  IN  @{dbus_fru}
228        ${cmd}=  Catenate  ${BUSCTL_FRU} ${fru}
229        ${dbus_output}=  Execute DBUS Introspect Command  ${cmd}
230        ${dbus_fru_board_serial_status}=  Run Keyword And Return Status  Should Contain  ${dbus_output}  .BOARD_SERIAL
231        ${dbus_fru_product_serial_status}=  Run Keyword And Return Status  Should Contain  ${dbus_output}  .PRODUCT_SERIAL
232        Run Keyword If  '${dbus_fru_board_serial_status}' == 'True' or '${dbus_fru_product_serial_status}' == 'True'
233        ...    Create Dictionary For DBUS URI  ${dbus_output}  ${ipmi_fru}  ${dbus_fru_board_serial_status}  ${cmd}
234    END
235
236
237Create Dictionary For DBUS URI
238    [Documentation]  Create Dictionary For DBUS URI
239    [Arguments]  ${dbus_output}  ${ipmi_fru}  ${dbus_fru_board_serial_status}  ${fru_command}
240
241    # Description of Argument(s):
242    # dbus_output                             Dbus response got from BMC console.
243    # ipmi_fru                                IPMI FRU details.
244    # dbus_fru_board_serial_status            FRU devices may have either BOARD_SERIAL or PRODUCT_SERIAL
245    # ...                                     if status was true value of BOARD_SERIAL will be taken for dictionary as an key
246    # ...                                     otherwise value of PRODUCT_SERIAL will be taken as an key for dictionary.
247    # fru_command                             FRU command to map into dictionary as value.
248
249    # Validates the IPMI FRU dictionary key with dbus uri response serial number.
250    # If matches then, sets the serial number as key and FRU uri as value.
251    # ${dbus_dict} defined under variable section.
252    FOR  ${ipmi_fru_serial_no}  IN  @{ipmi_fru.keys()}
253        ${serial_no}=  Run Keyword If  '${dbus_fru_board_serial_status}' == 'True'
254        ...    Get Lines Containing String  ${dbus_output}  .BOARD_SERIAL
255        ...  ELSE
256        ...    Get Lines Containing String  ${dbus_output}  .PRODUCT_SERIAL
257        ${serial_no}=  Split String  ${serial_no}  "
258        ${dbus_serial_no}=  Set Variable  ${serial_no[1].strip()}
259        ${serial_no_status}=  Run Keyword And Return Status  Should Be Equal As Strings  ${ipmi_fru_serial_no}  ${dbus_serial_no}
260        Run Keyword If  '${serial_no_status}' == 'True'
261        ...    Run Keywords  Set To Dictionary  ${dbus_dict}  ${dbus_serial_no}  ${fru_command}  AND
262        ...    Exit For Loop
263    END
264
265
266Compare IPMI FRU with DBUS
267    [Documentation]  Compare the IPMI FRU dictionary values with DBUS dictionary values,
268    ...  if the serial number is present in both FRU and dbus dictionaries.
269    [Arguments]  ${ipmi_fru}
270
271   # Description of Argument(s):
272   # ipmi_fru    IPMI FRU device details.
273
274    # With each IPMI FRU key, get the corresponding valid from dbus dictionary,
275    # Execute the value which is dbus uri and,
276    # validate each dbus field value with IPMI FRU field value.
277    # Values are validated for keys present in the ipmi_dbus_name_mapping dictionary.
278    # Example :
279    #    IPMI FRU field :
280    #             Board Part Number     : 111.22222.0000
281    #    DBUS FRU field :
282    #            .BOARD_PART_NUMBER      property  s     "111.22222.0000"        emits-change
283    FOR  ${key}  ${ipmi_fru_value}  IN  &{ipmi_fru}
284        ${dbus_resp}=  Execute DBUS Introspect Command  ${dbus_dict}[${key}]
285        ${ipmi_fru_subkeys}=  Get Dictionary Keys  ${ipmi_fru_value}
286        FOR  ${subkeys}  IN  @{ipmi_fru_subkeys}
287            ${key_status}=  Run Keyword And Return Status  Dictionary Should Contain Key
288            ...  ${ipmi_dbus_name_mapping}  ${subkeys}
289            Continue For Loop If  '${key_status}' == 'False'
290            ${property_name}=  Get From Dictionary  ${ipmi_dbus_name_mapping}  ${subkeys}
291            ${dbus_data}=  Get Lines Containing String  ${dbus_resp}  ${property_name}
292            ${dbus_value}=  Set Variable  ${dbus_data.split('"')[1].strip()}
293            ${ipmi_response}=  Get From Dictionary  ${ipmi_fru_value}  ${subkeys}
294            ${status}=  Run Keyword And Return Status  Should Contain  ${property_name}  DATE
295            IF  ${status}
296                # If the required IPMI field has date field, the IPMI FRU value is converted to
297                # format = %Y-%m-%d - %H:%M:%S and validated against dbus FRU data.
298                ${ipmi_date}=  Convert Date  ${ipmi_response}  date_format=%a %b %d %H:%M:%S %Y
299                ...  result_format=%Y-%m-%d - %H:%M:%S
300                Run Keyword And Continue On Failure  Should Be Equal As Strings  ${ipmi_date}  ${dbus_value}
301                ...  message=${property_name} Property value mismatch with IPMI and DBUS
302            ELSE
303                Run Keyword And Continue On Failure  Should Be Equal As Strings  ${ipmi_response}  ${dbus_value}
304                ...  message=${property_name} Property value mismatch with IPMI and DBUS
305            END
306        END
307    END
308
309
310Get FRU Inventory Area Info
311    [Documentation]  IPMI Get FRU Inventory Area Info and returns FRU Inventory area size in bytes.
312
313    ${resp}=  Run IPMI Standard Command  raw ${IPMI_RAW_CMD['FRU']['Inventory_Area_Info'][0]} ${fru_device_id}
314    ${resp}=  Split String  ${resp}
315
316    [Return]  ${resp[0]}
317
318
319Read FRU Data Via IPMI
320    [Documentation]  Read FRU data using IPMI raw command.
321    [Arguments]  ${fru_id}  ${offset}
322
323   # Description of Argument(s):
324   # fru_id        FRU id.
325   # offset        Offset byte for read FRU command.
326
327    # IPMI Read FRU Data Command.
328    # 0xff - Count to read --- count is ‘1’ based
329    ${resp}=  Run IPMI Standard Command
330    ...  raw ${IPMI_RAW_CMD['FRU']['Read'][0]} ${fru_id} ${offset} 0xff
331
332    [Return]  ${resp}
333
334
335Write FRU Data Via IPMI
336    [Documentation]  Write FRU data using IPMI raw command.
337    [Arguments]  ${fru_id}  ${offset}  ${data}  ${length}
338
339   # Description of Argument(s):
340   # fru_id        FRU id.
341   # offset        Offset byte for read FRU command.
342   # data          Data to write for write FRU command.
343   # length        Count of bytes that gets written in write FRU command.
344
345    # IPMI Write FRU Data Command.
346    ${resp}=  Run IPMI Standard Command
347    ...  raw ${IPMI_RAW_CMD['FRU']['Write'][0]} ${fru_id} ${offset} ${data}
348
349    Should Be Equal As Strings  ${resp}  ${length}
350
351
352Generate Random Data For FRU
353    [Documentation]  Generate random data for write in FRU.
354
355    # Description:
356    #  Generates string of bytes and convert to hexadecimal data.
357    #  Gets the length of initial FRU data read with IPMI read FRU device command.
358
359    # ${frudata_prefixed}    string with bytes prefixed 0x by default
360    # ${fru_data}            string with only hexadecimal bytes without prefix
361
362    ${string}=  Generate Random String  ${initial_fru_length}  [LETTERS]
363    ${frudata_prefixed}  ${fru_data}=  Identify Request Data  ${string}
364
365    [Return]  ${frudata_prefixed}  ${fru_data}
366
367
368Get Default FRU Data
369    [Documentation]  Get default data via read FRU data IPMI command.
370
371    # Read the default FRU device data.
372    # split the response and identify length of Requested data.
373    ${initial_fru_data}=  Read FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}
374    ${initial_fru_list}=  Split String  ${initial_fru_data}
375    ${initial_fru_list}=  Set Variable  ${initial_fru_list[1:]}
376    ${initial_fru_length}=  Get Length  ${initial_fru_list}
377    Set Test Variable  ${initial_fru_data}
378    Set Test Variable  ${initial_fru_list}
379    Set Test Variable  ${initial_fru_length}
380
381
382Restore Default FRU Data
383    [Documentation]  Restore default FRU data.
384
385    # Prefix 0x to initial request data.
386    ${fru_list}=  Prefix Bytes  ${initial_fru_list}
387    ${fru_byte}=  Evaluate  " ".join(${fru_list})
388    ${initial_frulength_hex}=  Convert To Hex  ${initial_fru_length}  lowercase=yes
389    # Write the initial FRU data to restore.
390    Write FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}  ${fru_byte}  ${initial_frulength_hex}
391    # Verify whether initial FRU data is restored.
392    ${fru_data}=  Read FRU Data Via IPMI  ${fru_device_id}  ${read_write_offset}
393    Should Be Equal  ${fru_data}  ${initial_fru_data}