1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
#!/usr/bin/env python3
# pylint: disable=invalid-name,missing-docstring
#
# Copyright 2021 Richard Hughes <richard@hughsie.com>
#
# SPDX-License-Identifier: LGPL-2.1-or-later
import json
from flask import Flask, Response, request
app = Flask(__name__)
HARDCODED_SMC_USERNAME = "smc_username"
HARDCODED_UNL_USERNAME = "unlicensed_username"
HARDCODED_HPE_USERNAME = "hpe_username"
HARDCODED_DELL_USERNAME = "dell_username"
HARDCODED_USERNAMES = {
"username2",
HARDCODED_SMC_USERNAME,
HARDCODED_UNL_USERNAME,
HARDCODED_HPE_USERNAME,
HARDCODED_DELL_USERNAME,
}
HARDCODED_PASSWORD = "password2"
app._percentage545: int = 0
app._percentage546: int = 0
app._hpeupdatestate: str = "Idle"
def _failure(msg: str, status=400):
res = {
"error": {"message": msg},
}
return Response(
response=json.dumps(res), status=status, mimetype="application/json"
)
@app.route("/redfish/v1/")
def index():
# reset counter
app._percentage545 = 0
app._percentage546 = 0
app._hpeupdatestate = "Idle"
# check password from the config file
try:
if (
request.authorization["username"] not in HARDCODED_USERNAMES
or request.authorization["password"] != HARDCODED_PASSWORD
):
return _failure("unauthorised", status=401)
except (KeyError, TypeError):
return _failure("invalid")
res = {
"@odata.id": "/redfish/v1/",
"@odata.etag": "653b835e9ee4af9ea7ea",
"RedfishVersion": "1.6.0",
"UUID": "92384634-2938-2342-8820-489239905423",
"UpdateService": {"@odata.id": "/redfish/v1/UpdateService"},
}
if request.authorization["username"] == HARDCODED_HPE_USERNAME:
res["Vendor"] = "HPE"
if request.authorization["username"] == HARDCODED_DELL_USERNAME:
res["Vendor"] = "Dell"
if request.authorization["username"] in (
HARDCODED_SMC_USERNAME,
HARDCODED_UNL_USERNAME,
):
res["Vendor"] = "SMCI"
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/Systems")
def systems():
res = {
"@odata.id": "/redfish/v1/Systems",
"@odata.type": "#ComputerSystemCollection.ComputerSystemCollection",
"Members": [
{"@odata.id": "/redfish/v1/Systems/System.Embedded.1"},
],
"Members@odata.count": 1,
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/Systems/System.Embedded.1")
def system():
res = {
"@odata.id": "/redfish/v1/Systems/System.Embedded.1",
"@odata.type": "#ComputerSystem.v1_20_1.ComputerSystem",
}
if request.authorization["username"] == HARDCODED_DELL_USERNAME:
res["Oem"] = {
"Dell": {
"DellSystem": {
"SystemID": 3168,
}
}
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/UpdateService")
def update_service():
res = {
"@odata.id": "/redfish/v1/UpdateService",
"@odata.type": "#UpdateService.v1_8_0.UpdateService",
"@odata.etag": "653b835e9ee4af9ea7ea",
"FirmwareInventory": {
"@odata.id": "/redfish/v1/UpdateService/FirmwareInventory"
},
"HttpPushUri": "/FWUpdate",
"HttpPushUriOptions": {
"HttpPushUriApplyTime": {
"ApplyTime": "Immediate",
}
},
"HttpPushUriOptionsBusy": False,
"ServiceEnabled": True,
}
if request.authorization["username"] == HARDCODED_UNL_USERNAME:
res["MultipartHttpPushUri"] = "/FWUpdate-unlicensed"
res["Actions"] = {
"#UpdateService.StartUpdate": {
"target": "/redfish/v1/UpdateService/Actions/UpdateService.StartUpdate"
}
}
elif request.authorization["username"] == HARDCODED_SMC_USERNAME:
res["MultipartHttpPushUri"] = "/FWUpdate-smc"
res["Actions"] = {
"#UpdateService.StartUpdate": {
"target": "/redfish/v1/UpdateService/Actions/UpdateService.StartUpdate"
}
}
elif request.authorization["username"] == HARDCODED_HPE_USERNAME:
res["Oem"] = {"Hpe": {"State": app._hpeupdatestate}}
res["HttpPushUri"] = "/FWUpdate-hpe"
else:
res["MultipartHttpPushUri"] = "/FWUpdate"
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/UpdateService/FirmwareInventory")
def firmware_inventory():
res = {
"@odata.id": "/redfish/v1/UpdateService/FirmwareInventory",
"@odata.type": "#SoftwareInventoryCollection.SoftwareInventoryCollection",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Members": [
{"@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/BMC"},
{"@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/BIOS"},
],
"Members@odata.count": 2,
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/UpdateService/FirmwareInventory/BMC")
def firmware_inventory_bmc():
res = {
"@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/BMC",
"@odata.type": "#SoftwareInventory.v1_2_3.SoftwareInventory",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Id": "BMC",
"LowestSupportedVersion": "11A-0.12",
"Name": "Lenovo BMC Firmware",
"Oem": {
"Hpe": {
"DeviceClass": "aa148d2e-6e09-453e-bc6f-63baa5f5ccc4",
"Targets": [
"00000000-0000-0000-0000-000000000229",
"00000000-0000-0000-0000-000001413436",
],
}
},
"RelatedItem": [{"@odata.id": "/redfish/v1/Managers/BMC"}],
"SoftwareId": "UEFI-AFE1-6",
"UefiDevicePaths": ["BMC(0x1,0x0ABCDEF)"],
"Updateable": True,
"Version": "11A-1.02",
"ReleaseDate": "2019-03-15T00:00:00",
}
if request.authorization["username"] in {
HARDCODED_UNL_USERNAME,
HARDCODED_SMC_USERNAME,
}:
res["Manufacturer"] = "SMCI"
else:
res["Manufacturer"] = "Lenovo"
if request.authorization["username"] == HARDCODED_DELL_USERNAME:
res["Oem"] = {"Dell": {"DellSoftwareInventory": {"Status": "Installed"}}}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/Managers/BMC")
def redfish_managers_bmc():
res = {}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/Chassis/1/PCIeDevices/slot_3/PCIeFunctions/slot_2.00")
def firmware_chassis_pcie_function_slot2():
res = {
"VendorId": "0x14e4",
"FunctionId": 1,
"SubsystemId": "0x4042",
"DeviceClass": "NetworkController",
"SubsystemVendorId": "0x17aa",
"DeviceId": "0x165f",
"RevisionId": "0x00",
"ClassCode": "0x020000",
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/Chassis/1/PCIeDevices/slot_3/PCIeFunctions")
def firmware_chassis_pcie_functions():
res = {
"Members": [
{
"@odata.id": "/redfish/v1/Chassis/1/PCIeDevices/slot_3/PCIeFunctions/slot_2.00"
}
],
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/Systems/437XR1138R2")
def firmware_systems_slot7():
res = {
"SerialNumber": "12345",
"@odata.id": "/redfish/v1/Chassis/1/PCIeDevices/slot_3",
"@odata.etag": "653b835e9ee4af9ea7ea",
"PCIeFunctions": {
"@odata.id": "/redfish/v1/Chassis/1/PCIeDevices/slot_3/PCIeFunctions"
},
"DeviceType": "SingleFunction",
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/UpdateService/FirmwareInventory/BIOS")
def firmware_inventory_bios():
res = {
"@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/BIOS",
"@odata.type": "#SoftwareInventory.v1_2_3.SoftwareInventory",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Id": "BIOS",
"LowestSupportedVersion": "P79 v1.10",
"Name": "Contoso BIOS Firmware",
"RelatedItem": [{"@odata.id": "/redfish/v1/Systems/437XR1138R2"}],
"SoftwareId": "FEE82A67-6CE2-4625-9F44-237AD2402C28",
"Updateable": True,
"Version": "P79 v1.45",
"ReleaseDate": "2019-03-15T00:00:00Z",
}
if request.authorization["username"] in {
HARDCODED_UNL_USERNAME,
HARDCODED_SMC_USERNAME,
}:
res["Manufacturer"] = "SMCI"
else:
res["Manufacturer"] = "Contoso"
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/SessionService/Sessions", methods=["POST"])
def session_service_sessions():
username = request.authorization["username"]
if username not in HARDCODED_USERNAMES:
return _failure("unauthorised")
if request.authorization["password"] != HARDCODED_PASSWORD:
return _failure("invalid password")
res = {
"@odata.id": "/redfish/v1/SessionService/Sessions/1",
"@odata.type": "#Session.v1_0_0.Session",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Id": "1",
"Name": "Session 1",
"UserName": username,
}
return Response(
json.dumps(res),
status=200,
mimetype="application/json",
headers={"X-Auth-Token": "1234eabcdeabcdeabcdeabcdeabc1234"},
)
@app.route("/redfish/v1/TaskService/999")
def task_manager():
res = {
"@odata.id": "/redfish/v1/TaskService/999",
"@odata.type": "#Task.v1_4_3.Task",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Id": "545",
"Name": "Task 545",
}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/TaskService/Tasks/545")
def task_status_545():
res = {
"@odata.id": "/redfish/v1/TaskService/Tasks/545",
"@odata.type": "#Task.v1_4_3.Task",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Id": "545",
"Name": "Task 545",
"PercentComplete": app._percentage545,
}
if app._percentage545 == 0:
res["TaskState"] = "Running"
elif app._percentage545 in [25, 50, 75]:
res["TaskState"] = "Running"
res["TaskStatus"] = "OK"
res["Messages"] = [
{
"Message": "Applying image",
"MessageId": "Update.1.1.TransferringToComponent",
}
]
elif app._percentage545 == 100:
res["TaskState"] = "Completed"
res["TaskStatus"] = "OK"
res["Messages"] = [
{
"Message": "Applying image",
"MessageId": "Update.1.1.TransferringToComponent",
},
{
"Message": "A reset is required",
"MessageId": "Base.1.10.ResetRequired",
},
{
"Message": "Task completed OK",
"MessageId": "TaskEvent.1.0.TaskCompletedOK",
},
]
else:
res["TaskState"] = "Exception"
res["TaskStatus"] = "Warning"
res["Messages"] = [
{
"Message": "Error verifying image",
"MessageId": "Update.1.0.ApplyFailed",
"Severity": "Warning",
}
]
app._percentage545 += 25
return Response(response=json.dumps(res), status=200, mimetype="application/json")
@app.route("/redfish/v1/TaskService/Tasks/546")
def task_status_546():
res = {
"@odata.type": "#Task.v1_4_3.Task",
"@odata.id": "/redfish/v1/TaskService/Tasks/546",
"@odata.etag": "653b835e9ee4af9ea7ea",
"Id": "546",
"Name": "BIOS Verify",
"TaskState": "Running",
"StartTime": "2022-09-29T14:50:54+00:00",
"PercentComplete": app._percentage546,
"HidePayload": True,
"TaskMonitor": "/redfish/v1/TaskMonitor/gd5n5ffS4gi9r6YKVZmgIIaj8ECLfnc",
"TaskStatus": "OK",
"Messages": [
{
"MessageId": "",
"RelatedProperties": [""],
"Message": "",
"MessageArgs": [""],
"Severity": "",
}
],
"Oem": {},
}
if app._percentage546 == 0:
res["TaskState"] = "Running"
elif app._percentage546 in [25, 50, 75]:
res["TaskState"] = "Running"
elif app._percentage546 == 100:
res["TaskState"] = "Completed"
app._percentage546 += 25
return Response(response=json.dumps(res), status=200, mimetype="application/json")
@app.route("/FWUpdate-unlicensed", methods=["GET"])
def fwupdate_unlicensed():
res = {
"error": {
"code": "Base.v1_4_0.GeneralError",
"Message": "A general error has occurred. See ExtendedInfo for more information.",
"@Message.ExtendedInfo": [
{
"MessageId": "SMC.1.0.OemLicenseNotPassed",
"Message": "Feature not available.",
"MessageArgs": ["BIOS"],
}
],
}
}
return Response(json.dumps(res), status=405, mimetype="application/json")
@app.route("/FWUpdate-smc", methods=["GET"])
def fwupdate_smc_query():
res = {"Accepted": {"code": "Base.v1_4_0.Accepted"}}
return Response(json.dumps(res), status=200, mimetype="application/json")
@app.route("/FWUpdate-smc", methods=["POST"])
def fwupdate_smc():
data = json.loads(request.form["UpdateParameters"])
if data["@Redfish.OperationApplyTime"] != "OnStartUpdateRequest":
return _failure("apply invalid")
if data["Targets"][0] != "/redfish/v1/Systems/1/Bios":
return _failure("id invalid")
fileitem = request.files["UpdateFile"]
if not fileitem.filename.endswith(".bin"):
return _failure("filename invalid")
filecontents = fileitem.read().decode()
if filecontents == "hello":
app._percentage546 = 0
res = {
"Accepted": {
"code": "Base.v1_4_0.Accepted",
"Message": "Successfully Accepted Request. Please see the location header and ExtendedInfo for more information.",
"@Message.ExtendedInfo": [
{
"MessageId": "SMC.1.0.OemSimpleupdateAcceptedMessage",
"Severity": "Ok",
"Resolution": "No resolution was required.",
"Message": "Please also check Task Resource /redfish/v1/TaskService/Tasks/546 to see more information.",
"MessageArgs": ["/redfish/v1/TaskService/Tasks/546"],
"RelatedProperties": ["BiosVerifyAccepted"],
}
],
}
}
# Location set to the URI of a task monitor.
return Response(
json.dumps(res),
status=202,
mimetype="application/json",
headers={"Location": "/redfish/v1/TaskService/Tasks/546"},
)
elif filecontents == "stuck":
res = {
"error": {
"code": "Base.v1_4_0.GeneralError",
"Message": "A general error has occurred. See ExtendedInfo for more information.",
"@Message.ExtendedInfo": [
{
"MessageId": "SMC.1.0.OemFirmwareAlreadyInUpdateMode",
"Severity": "Warning",
"Resolution": "Please check if there was the next step with respective API to execute.",
"Message": "The BIOS firmware update was already in update mode.",
"MessageArgs": ["BIOS"],
"RelatedProperties": ["EnterUpdateMode_StatusCheck"],
}
],
}
}
return Response(json.dumps(res), status=405, mimetype="application/json")
else:
return _failure("data invalid")
@app.route("/FWUpdate-hpe", methods=["POST"])
def fwupdate_hpe():
print(request.form)
if not request.form["sessionKey"]:
return _failure("no sessionKey", status=401)
data = json.loads(request.form["parameters"])
if not data["UpdateTarget"]:
return _failure("payload will not update the target")
if data["UpdateRepository"]:
return _failure("payload will update the repository")
fileitem = request.files["files[]"]
if not fileitem:
return _failure("no file supplied")
app._hpeupdatestate = "Complete"
return Response(status=200)
@app.route("/FWUpdate", methods=["POST"])
def fwupdate():
data = json.loads(request.form["UpdateParameters"])
if data["@Redfish.OperationApplyTime"] != "Immediate":
return _failure("apply invalid")
if data["Targets"][0] != "/redfish/v1/UpdateService/FirmwareInventory/BMC":
return _failure("id invalid")
if len(request.files) != 1:
return _failure("no file supplied")
fileitem = request.files["UpdateFile"]
if not fileitem.filename.endswith(".exe"):
return _failure("filename invalid")
if fileitem.read().decode() != "hello":
return _failure("data invalid")
res = {
"Version": "P79 v1.45",
"@odata.id": "/redfish/v1/TaskService/Tasks/545",
"@odata.etag": "653b835e9ee4af9ea7ea",
"TaskMonitor": "/redfish/v1/TaskService/999",
}
# Location set to the URI of a task monitor.
return Response(
json.dumps(res),
status=202,
mimetype="application/json",
headers={"Location": "/redfish/v1/TaskService/Tasks/545"},
)
@app.route(
"/redfish/v1/UpdateService/Actions/UpdateService.StartUpdate", methods=["POST"]
)
def startupdate():
res = {
"Accepted": {
"code": "Base.v1_4_0.Accepted",
"Message": "Successfully Accepted Request. Please see the location header and ExtendedInfo for more information.",
"@Message.ExtendedInfo": [
{
"MessageId": "SMC.1.0.OemSimpleupdateAcceptedMessage",
"Severity": "Ok",
"Resolution": "No resolution was required.",
"Message": "Please also check Task Resource /redfish/v1/TaskService/Tasks/546 to see more information.",
"MessageArgs": ["/redfish/v1/TaskService/Tasks/546"],
"RelatedProperties": ["BiosUpdateAccepted"],
}
],
}
}
app._percentage546 = 0
return Response(
json.dumps(res),
status=202,
mimetype="application/json",
headers={"Location": "/redfish/v1/TaskService/Tasks/546"},
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=4661)
|