1*** Settings ***
2Documentation            Update the BMC code on a target BMC via Redifsh.
3
4# Test Parameters:
5# IMAGE_FILE_PATH        The path to the BMC image file.
6#
7# Firmware update states:
8#     Enabled            Image is installed and either functional or active.
9#     Disabled           Image installation failed or ready for activation.
10#     Updating           Image installation currently in progress.
11
12Resource                 ../../lib/resource.robot
13Resource                 ../../lib/bmc_redfish_resource.robot
14Resource                 ../../lib/openbmc_ffdc.robot
15Resource                 ../../lib/common_utils.robot
16Resource                 ../../lib/code_update_utils.robot
17Resource                 ../../lib/redfish_code_update_utils.robot
18Resource                 ../../lib/utils.robot
19Library                  ../../lib/gen_robot_valid.py
20Library                  ../../lib/var_funcs.py
21Library                  ../../lib/gen_robot_keyword.py
22Library                  ../../lib/code_update_utils.py
23
24Suite Setup              Suite Setup Execution
25Suite Teardown           Redfish.Logout
26Test Setup               Printn
27Test Teardown            FFDC On Test Case Fail
28
29# Force the test to timedout to prevent test hanging.
30Test Timeout             30 minutes
31
32Force Tags               BMC_Code_Update
33
34*** Variables ***
35
36${FORCE_UPDATE}          ${0}
37${LOOP_COUNT}            20
38
39*** Test Cases ***
40
41Redfish BMC Code Update
42    [Documentation]  Update the firmware image.
43    [Tags]  Redfish_BMC_Code_Update
44
45    ${image_version}=  Get Version Tar  ${IMAGE_FILE_PATH}
46    Rprint Vars  image_version
47
48    ${bmc_release_info}=  Get BMC Release Info
49    ${functional_version}=  Set Variable  ${bmc_release_info['version_id']}
50    Rprint Vars  functional_version
51
52    ${post_code_update_actions}=  Get Post Boot Action
53    ${state}=  Get Pre Reboot State
54    Rprint Vars  state
55
56    # Check if the existing firmware is functional.
57    Pass Execution If  '${functional_version}' == '${image_version}'
58    ...  The existing ${image_version} firmware is already functional.
59
60    ${sw_inv}=  Get Functional Firmware  BMC image
61    ${nonfunctional_sw_inv}=  Get Non Functional Firmware  ${sw_inv}  False
62
63    # Redfish active software image API.
64    Run Keyword If  not ${FORCE_UPDATE}
65    ...  Run Keyword If  ${num_records} > 0
66    ...    Run Keyword If  '${nonfunctional_sw_inv['version']}' == '${image_version}'
67    ...      Run Keywords  Switch Backup Firmware Image To Functional  AND
68    ...      Wait For Reboot  start_boot_seconds=${state['epoch_seconds']}  AND
69    ...      Redfish Verify BMC Version  ${IMAGE_FILE_PATH}  AND
70    ...      Pass Execution  The firmware ${image_version} is backup image.
71
72    # Firmware inventory record of the given image version.
73    ${image_info}=  Get Software Inventory State By Version  ${image_version}
74
75    ${image_info_len}=  Get Length  ${image_info}
76
77    # REST delete the image to fresh code update for a given same image object
78    # which is already ACTIVE but on the alternate side. Irrespective of when
79    # REST is disabled in near future or the below operation fails, is purely
80    # done to give a chance.
81
82    Run Keyword If  '${image_info_len}' != 0
83    ...  Run Keywords  Print Timen
84    ...  The ${image_version} version is installed but not functional, delete and continue update.
85    ...    AND
86    ...  Run Keyword And Ignore Error
87    ...    Delete Software Object  /xyz/openbmc_project/software/${image_info['image_id']}
88
89    Redfish Update Firmware
90
91
92Redfish Firmware Update Loop
93    [Documentation]  Update the firmware image in loop.
94    [Tags]  Redfish_Firmware_Update_Loop
95    [Template]  Redfish Firmware Update In Loop
96
97    ${LOOP_COUNT}
98
99
100*** Keywords ***
101
102Suite Setup Execution
103    [Documentation]  Do the suite setup.
104
105    Redfish.Login
106    # Delete BMC dump and Error logs.
107    Run Keyword And Ignore Error  Redfish Delete All BMC Dumps
108    Run Keyword And Ignore Error  Redfish Purge Event Log
109    # Checking for file existence.
110    Valid File Path  IMAGE_FILE_PATH
111
112
113Redfish Firmware Update In Loop
114    [Documentation]  Update the firmware in loop.
115    [Arguments]  ${update_loop_count}
116
117    # Description of argument(s):
118    # update_loop_count    This value is used to run the firmware update in loop.
119
120    ${before_image_state}=  Get BMC Functional Firmware
121    ${temp_update_loop_count}=  Evaluate  ${update_loop_count} + 1
122
123    FOR  ${count}  IN RANGE  1  ${temp_update_loop_count}
124      Print Timen  **************************************
125      Print Timen  * The Current Loop Count is ${count} of ${update_loop_count} *
126      Print Timen  **************************************
127      Redfish Update Firmware
128      ${sw_inv}=  Get Functional Firmware  BMC update
129      ${nonfunctional_sw_inv}=  Get Non Functional Firmware  ${sw_inv}  False
130      Run Keyword If  ${nonfunctional_sw_inv['functional']} == False
131      ...  Set BMC Image Priority To Least  ${nonfunctional_sw_inv['version']}  ${nonfunctional_sw_inv}
132      Redfish.Login
133      ${sw_inv}=  Get Functional Firmware  BMC update
134      ${nonfunctional_sw_inv}=  Get Non Functional Firmware  ${sw_inv}  False
135      Delete BMC Image
136    END
137
138    ${after_image_state}=  Get BMC Functional Firmware
139    Valid Value  before_image_state["version"]  ['${after_image_state["version"]}']
140
141
142Delete BMC Image
143    [Documentation]  Delete a BMC image from the BMC flash chip.
144
145    ${software_object}=  Get Non Running BMC Software Object
146    Delete Image And Verify  ${software_object}  ${VERSION_PURPOSE_BMC}
147
148
149Activate Existing Firmware
150    [Documentation]  Set firmware image to lower priority.
151    [Arguments]  ${image_version}
152
153    # Description of argument(s):
154    # image_version     Version of image.
155
156    ${software_inventory_record}=  Get Software Inventory State By Version
157    ...  ${image_version}
158    ${num_keys}=  Get Length  ${software_inventory_record}
159
160    Rprint Vars  software_inventory_record
161
162    # If no software inventory record was found, there is no existing
163    # firmware for the given version and therefore no action to be taken.
164    Return From Keyword If  not ${num_keys}
165
166    # Check if the existing firmware is functional.
167    Pass Execution If  ${software_inventory_record['functional']}
168    ...  The existing ${image_version} firmware is already functional.
169
170    # If existing firmware is not functional, then set the priority to least.
171    Print Timen  The existing ${image_version} firmware is not yet functional.
172    Set BMC Image Priority To Least  ${image_version}  ${software_inventory_record}
173
174    Pass Execution  The existing ${image_version} firmware is now functional.
175
176
177Get Image Priority
178    [Documentation]  Get Current Image Priority.
179    [Arguments]  ${image_version}
180
181    # Description of argument(s):
182    # image_version       The Firmware image version (e.g. 2.8.0-dev-1107-g512028d95).
183
184    ${software_info}=  Read Properties
185    ...  ${SOFTWARE_VERSION_URI}/enumerate  quiet=1
186    # Get only the record associated with our image_version.
187
188    ${software_info}=  Filter Struct
189    ...  ${software_info}  [('Version', '${image_version}')]
190    # Convert from dict to list.
191    ${software_info}=  Get Dictionary Values  ${software_info}
192
193    [Return]  ${software_info[0]['Priority']}
194
195
196Set BMC Image Priority To Least
197    [Documentation]  Set BMC image priority to least value.
198    [Arguments]  ${image_version}  ${software_inventory}
199
200    # Description of argument(s):
201    # image_version       The Firmware image version (e.g. 2.8.0-dev-1107-g512028d95).
202    # software_inventory  Software inventory details.
203
204    ${least_priority}=  Get Least Value Priority Image  ${VERSION_PURPOSE_BMC}
205    ${cur_priority}=  Get Image Priority  ${image_version}
206    Rprint Vars  least_priority  cur_priority
207
208    Return From Keyword If  '${least_priority}' == ${cur_priority}
209    Set Host Software Property
210    ...  ${SOFTWARE_VERSION_URI}${software_inventory['image_id']}
211    ...  Priority  ${least_priority}
212
213    Redfish OBMC Reboot (off)
214
215
216Redfish Update Firmware
217    [Documentation]  Update the BMC firmware via redfish interface.
218
219    ${post_code_update_actions}=  Get Post Boot Action
220    ${state}=  Get Pre Reboot State
221    Rprint Vars  state
222    Run Keyword And Ignore Error  Set ApplyTime  policy=OnReset
223    Redfish Upload Image And Check Progress State
224    ${tar_version}=  Get Version Tar  ${IMAGE_FILE_PATH}
225    ${image_info}=  Get Software Inventory State By Version  ${tar_version}
226    Run Key  ${post_code_update_actions['${image_info["image_type"]}']['OnReset']}
227    Redfish.Login
228    Redfish Verify BMC Version  ${IMAGE_FILE_PATH}
229
230