blob: 8fc9a52a63482a348d304ee2d8917070c1f55b74 [file] [log] [blame]
Patrick Venture54c3b532018-08-01 11:45:49 -07001/*
Patrick Venture514f6482018-08-07 14:27:58 -07002 * Copyright 2018 Google Inc.
Patrick Venture54c3b532018-08-01 11:45:49 -07003 *
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 "flash-ipmi.hpp"
18
Patrick Venture8ec019f2018-08-07 11:22:33 -070019#include <cstdio>
20#include <phosphor-logging/log.hpp>
21
22using namespace phosphor::logging;
23
24namespace
25{
26
27/**
28 * Close a file pointer and set to null.
29 *
30 * @param[in] fd a pointer to your file handle.
31 */
32void closeFile(std::FILE** fd)
33{
34 if (!fd)
35 {
36 return;
37 }
38
39 if (*fd)
40 {
41 std::fclose(*fd);
42 (*fd) = nullptr;
43 }
44}
45}
46
47void FlashUpdate::closeEverything()
48{
49 closeFile(&flashFd);
Patrick Venture6f17bd22018-08-07 13:24:17 -070050 closeFile(&hashFd);
Patrick Venture8ec019f2018-08-07 11:22:33 -070051}
52
53FlashUpdate::~FlashUpdate()
54{
55 /* Close without deleting. This object can only be destroyed if the ipmi
56 * daemon unloads it, by closing down. In this event, we want the verified
57 * file to live on.
58 */
59 closeEverything();
60}
61
Patrick Venture54c3b532018-08-01 11:45:49 -070062void FlashUpdate::abortEverything()
63{
Patrick Venture8ec019f2018-08-07 11:22:33 -070064 closeEverything();
65
66 /* TODO: And now delete everything */
Patrick Venture54c3b532018-08-01 11:45:49 -070067 return;
68}
69
70bool FlashUpdate::openEverything()
71{
Patrick Venture3c086f22018-08-07 11:59:20 -070072 flashFd = std::fopen(tmpPath.c_str(), "wb");
Patrick Venture8ec019f2018-08-07 11:22:33 -070073 if (flashFd == nullptr)
74 {
75 log<level::INFO>("Unable to open staging path",
76 entry("PATH=%s", tmpPath.c_str()));
77 return false;
78 }
79
Patrick Venture6f17bd22018-08-07 13:24:17 -070080 /* hash path is basically optional. */
81 if (!hashPath.empty())
82 {
83 hashFd = std::fopen(hashPath.c_str(), "wb");
84 if (hashFd == nullptr)
85 {
86 log<level::INFO>("Unable to open hash storage path",
87 entry("PATH=%s", hashPath.c_str()));
88 closeFile(&flashFd);
89 return false;
90 }
91 }
92
Patrick Venture54c3b532018-08-01 11:45:49 -070093 return true;
94}
95
96/* Prepare to receive a BMC image and then a signature. */
Patrick Venture8ec019f2018-08-07 11:22:33 -070097bool FlashUpdate::start(uint32_t length)
Patrick Venture54c3b532018-08-01 11:45:49 -070098{
Patrick Venture54c3b532018-08-01 11:45:49 -070099 /* Close out and delete everything. */
100 abortEverything();
101
Patrick Venture8ec019f2018-08-07 11:22:33 -0700102 /* TODO: Validate request->length */
103 flashLength = length;
104
Patrick Venture54c3b532018-08-01 11:45:49 -0700105 /* Start over! */
106 return openEverything();
107}
Patrick Venture79e131f2018-08-01 13:34:35 -0700108
Patrick Venture3c086f22018-08-07 11:59:20 -0700109bool FlashUpdate::writeBlock(std::FILE* fd, uint32_t offset,
110 const std::vector<uint8_t>& bytes)
111{
112 /* Seek into position, let's assume fseek won't call if offset matches
113 * position.
114 */
115 if (std::fseek(fd, offset, SEEK_SET))
116 {
117 log<level::ERR>("Unable to seek into file to write bytes.");
118 return false;
119 }
120
121 /* Write the bytes. */
122 auto written = std::fwrite(bytes.data(), 1, bytes.size(), fd);
123
124 if (written != bytes.size())
125 {
126 log<level::ERR>("Unable to write all the bytes requested.");
127 return false;
128 }
129
130 (void)std::fflush(fd);
131 return true;
132}
133
Patrick Venture79e131f2018-08-01 13:34:35 -0700134bool FlashUpdate::flashData(uint32_t offset, const std::vector<uint8_t>& bytes)
135{
Patrick Venture3c086f22018-08-07 11:59:20 -0700136 if (flashFd)
137 {
138 return writeBlock(flashFd, offset, bytes);
139 }
140
Patrick Venture79e131f2018-08-01 13:34:35 -0700141 return false;
142}
Patrick Venture2c1205d2018-08-03 10:23:14 -0700143
144bool FlashUpdate::flashFinish()
145{
Patrick Venture57703662018-08-07 12:52:24 -0700146 /* If it's open, close it. */
147 if (flashFd)
148 {
149 closeFile(&flashFd);
150 return true;
151 }
152
Patrick Venture2c1205d2018-08-03 10:23:14 -0700153 return false;
154}
Patrick Venture8d9f7322018-08-03 10:39:13 -0700155
156bool FlashUpdate::startHash(uint32_t length)
157{
Patrick Venture6f17bd22018-08-07 13:24:17 -0700158 if (!hashFd)
159 {
160 return false;
161 }
162
163 hashLength = length;
164 return true;
Patrick Venture8d9f7322018-08-03 10:39:13 -0700165}
Patrick Venturecfe66872018-08-03 13:32:33 -0700166
167bool FlashUpdate::hashData(uint32_t offset, const std::vector<uint8_t>& bytes)
168{
Patrick Venturecbe51492018-08-07 14:09:17 -0700169 if (hashFd)
170 {
171 return writeBlock(hashFd, offset, bytes);
172 }
173
Patrick Venturecfe66872018-08-03 13:32:33 -0700174 return false;
175}
Patrick Venturefbc7d192018-08-03 13:54:21 -0700176
177bool FlashUpdate::hashFinish()
178{
179 /* TODO: implement. */
180 return false;
181}
Patrick Venture1cb87d22018-08-03 18:22:09 -0700182
183bool FlashUpdate::startDataVerification()
184{
185 /* TODO: implement. */
186 return false;
187}
Patrick Venture5c251ca2018-08-03 18:31:01 -0700188
189bool FlashUpdate::abortUpdate()
190{
191 /* TODO: implement. */
192 return false;
193}