blob: fbb0275c07044fbc9b75478c22eaa3d50cfb3bbd [file] [log] [blame]
Patrick Venture01123b22019-06-20 13:49:06 -07001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "helper.hpp"
18
19#include "status.hpp"
20#include "tool_errors.hpp"
21
Patrick Venture01123b22019-06-20 13:49:06 -070022#include <ipmiblob/blob_errors.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070023
24#include <chrono>
Patrick Venture01123b22019-06-20 13:49:06 -070025#include <thread>
26
27namespace host_tool
28{
29
30/* Poll an open verification session. Handling closing the session is not yet
31 * owned by this method.
32 */
33bool pollStatus(std::uint16_t session, ipmiblob::BlobInterface* blob)
34{
35 using namespace std::chrono_literals;
36
37 static constexpr auto verificationSleep = 5s;
38 ipmi_flash::ActionStatus result = ipmi_flash::ActionStatus::unknown;
39
40 try
41 {
Benjamin Fair6f61af82020-05-14 10:41:13 -070042 /* sleep for 5 seconds and check 360 times, for a timeout of: 1800
43 * seconds (30 minutes).
Patrick Venture69abbc62019-08-13 13:47:36 -070044 * TODO: make this command line configurable and provide smaller
45 * default value.
46 */
Benjamin Fair6f61af82020-05-14 10:41:13 -070047 static constexpr int commandAttempts = 360;
Patrick Venture01123b22019-06-20 13:49:06 -070048 int attempts = 0;
49 bool exitLoop = false;
50
51 /* Reach back the current status from the verification service output.
52 */
53 while (attempts++ < commandAttempts)
54 {
55 ipmiblob::StatResponse resp = blob->getStat(session);
56
57 if (resp.metadata.size() != sizeof(std::uint8_t))
58 {
59 /* TODO: How do we want to handle the verification failures,
60 * because closing the session to the verify blob has a special
61 * as-of-yet not fully defined behavior.
62 */
63 std::fprintf(stderr, "Received invalid metadata response!!!\n");
64 }
65
66 result = static_cast<ipmi_flash::ActionStatus>(resp.metadata[0]);
67
68 switch (result)
69 {
70 case ipmi_flash::ActionStatus::failed:
71 std::fprintf(stderr, "failed\n");
72 exitLoop = true;
73 break;
74 case ipmi_flash::ActionStatus::unknown:
75 std::fprintf(stderr, "other\n");
76 break;
77 case ipmi_flash::ActionStatus::running:
78 std::fprintf(stderr, "running\n");
79 break;
80 case ipmi_flash::ActionStatus::success:
81 std::fprintf(stderr, "success\n");
82 exitLoop = true;
83 break;
84 default:
85 std::fprintf(stderr, "wat\n");
86 }
87
88 if (exitLoop)
89 {
90 break;
91 }
92 std::this_thread::sleep_for(verificationSleep);
93 }
94 }
95 catch (const ipmiblob::BlobException& b)
96 {
97 throw ToolException("blob exception received: " +
98 std::string(b.what()));
99 }
100
101 /* TODO: If this is reached and it's not success, it may be worth just
102 * throwing a ToolException with a timeout message specifying the final
103 * read's value.
104 *
105 * TODO: Given that excepting from certain points leaves the BMC update
106 * state machine in an inconsistent state, we need to carefully evaluate
107 * which exceptions from the lower layers allow one to try and delete the
108 * blobs to rollback the state and progress.
109 */
110 return (result == ipmi_flash::ActionStatus::success);
111}
112
113} // namespace host_tool