blob: 87962b99df852aa5c4ef5812cd22a152ea2c2bc1 [file] [log] [blame]
Patrick Venturebf58cd62018-12-11 09:05:46 -08001/*
2 * Copyright 2018 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 "updater.hpp"
18
Patrick Venture84778b82019-06-26 20:11:09 -070019#include "flags.hpp"
Patrick Venture01123b22019-06-20 13:49:06 -070020#include "handler.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -070021#include "status.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080022#include "tool_errors.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070023#include "util.hpp"
Patrick Venture0533d0b2018-12-13 08:48:24 -080024
Patrick Venture9b37b092020-05-28 20:58:57 -070025#include <ipmiblob/blob_errors.hpp>
26
Patrick Venture00887592018-12-11 10:57:06 -080027#include <algorithm>
Patrick Venture339dece2018-12-14 18:32:04 -080028#include <cstring>
Patrick Ventureaf696252018-12-11 10:22:14 -080029#include <memory>
Patrick Venture2a927e82019-02-01 07:29:47 -080030#include <string>
Patrick Ventured61b0ff2019-05-15 15:58:06 -070031#include <thread>
Patrick Venturec9792e72019-07-02 07:35:48 -070032#include <unordered_map>
Patrick Venture55646de2019-05-16 10:06:26 -070033#include <vector>
Patrick Ventureaf696252018-12-11 10:22:14 -080034
Patrick Venture9b534f02018-12-13 16:10:02 -080035namespace host_tool
36{
37
Patrick Venture1f09d412019-06-19 16:01:06 -070038void updaterMain(UpdateHandlerInterface* updater, const std::string& imagePath,
Patrick Venture9f937c42019-06-21 07:55:54 -070039 const std::string& signaturePath,
Brandon Kim6749ba12019-09-19 13:31:37 -070040 const std::string& layoutType, bool ignoreUpdate)
Patrick Venture55646de2019-05-16 10:06:26 -070041{
Patrick Venturec498caa2019-08-15 10:12:50 -070042 /* TODO: validate the layoutType isn't a special value such as: 'update',
43 * 'verify', or 'hash'
44 */
45 std::string layout = "/flash/" + layoutType;
Patrick Venture9f937c42019-06-21 07:55:54 -070046
47 bool goalSupported = updater->checkAvailable(layout);
Patrick Venture55646de2019-05-16 10:06:26 -070048 if (!goalSupported)
49 {
Benjamin Fair060be012019-11-14 13:12:49 -080050 throw ToolException("Goal firmware not supported");
Patrick Venture55646de2019-05-16 10:06:26 -070051 }
52
Benjamin Fair060be012019-11-14 13:12:49 -080053 /* Yay, our layout type is supported. */
Patrick Venture5f2fcc42019-06-20 07:21:05 -070054 try
Patrick Venture55646de2019-05-16 10:06:26 -070055 {
Patrick Venture5f2fcc42019-06-20 07:21:05 -070056 /* Send over the firmware image. */
57 std::fprintf(stderr, "Sending over the firmware image.\n");
Patrick Venture9f937c42019-06-21 07:55:54 -070058 updater->sendFile(layout, imagePath);
Patrick Venture5f2fcc42019-06-20 07:21:05 -070059
60 /* Send over the hash contents. */
61 std::fprintf(stderr, "Sending over the hash file.\n");
62 updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
63
64 /* Trigger the verification by opening and committing the verify file.
Patrick Venture14713be2019-06-05 13:42:28 -070065 */
Patrick Venture5f2fcc42019-06-20 07:21:05 -070066 std::fprintf(stderr, "Opening the verification file\n");
Brandon Kim6749ba12019-09-19 13:31:37 -070067 if (updater->verifyFile(ipmi_flash::verifyBlobId, false))
Patrick Venture5f2fcc42019-06-20 07:21:05 -070068 {
69 std::fprintf(stderr, "succeeded\n");
70 }
71 else
72 {
73 std::fprintf(stderr, "failed\n");
74 throw ToolException("Verification failed");
75 }
76
77 /* Trigger the update by opening and committing the update file. */
78 std::fprintf(stderr, "Opening the update file\n");
Brandon Kim6749ba12019-09-19 13:31:37 -070079 if (updater->verifyFile(ipmi_flash::updateBlobId, ignoreUpdate))
Patrick Venture5f2fcc42019-06-20 07:21:05 -070080 {
81 std::fprintf(stderr, "succeeded\n");
82 }
83 else
84 {
85 /* Depending on the update mechanism used, this may be
86 * uninteresting. For instance, for the static layout, we use the
87 * reboot update mechanism. Which doesn't always lead to a
88 * successful return before the BMC starts shutting down services.
89 */
90 std::fprintf(stderr, "failed\n");
91 throw ToolException("Update failed");
92 }
93 }
94 catch (...)
95 {
96 updater->cleanArtifacts();
97 throw;
Patrick Venture55646de2019-05-16 10:06:26 -070098 }
Patrick Venturebf58cd62018-12-11 09:05:46 -080099}
Patrick Venture9b534f02018-12-13 16:10:02 -0800100
101} // namespace host_tool