| // Copyright 2021 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include <unistd.h> |
| |
| #include <flasher/device.hpp> |
| #include <flasher/file.hpp> |
| #include <flasher/file/memory.hpp> |
| #include <flasher/mod.hpp> |
| #include <flasher/mutate.hpp> |
| #include <flasher/ops.hpp> |
| #include <flashupdate/args.hpp> |
| #include <flashupdate/info.hpp> |
| #include <flashupdate/logging.hpp> |
| #include <stdplus/exception.hpp> |
| #include <stdplus/raw.hpp> |
| |
| #include <filesystem> |
| #include <memory> |
| #include <optional> |
| #include <stdexcept> |
| #include <vector> |
| |
| namespace flashupdate |
| { |
| namespace ops |
| { |
| |
| using flasher::ModArgs; |
| using stdplus::fd::OpenAccess; |
| using stdplus::fd::OpenFlag; |
| using stdplus::fd::OpenFlags; |
| |
| info::UpdateInfo fetchInfo(const Args& args) |
| { |
| // Get the filename only |
| auto devMod = ModArgs(args.config.metadata.path); |
| auto dev = flasher::openDevice(devMod); |
| std::vector<std::byte> fileData(sizeof(info::UpdateInfo)); |
| dev->readAt(fileData, args.config.metadata.offset); |
| |
| // Convert bytes to struct and update the state |
| return stdplus::raw::copyFrom<info::UpdateInfo>(fileData); |
| } |
| |
| void writeInfo(const Args& args, info::UpdateInfo updateInfo) |
| { |
| auto devMod = ModArgs(args.config.metadata.path); |
| flasher::NestedMutate mutate{}; |
| auto dev = flasher::openDevice(devMod); |
| |
| // Convert struct into bytes |
| // The UpdateInfo struct saves the staged/active version of the image and |
| // the stage of the update process. It will also save information like |
| // latest used stage flash index and the expected hash of the CR51 |
| // descriptor in the staging flash. |
| auto file = flasher::file::Memory(); |
| file.writeAtExact(stdplus::raw::asSpan<std::byte>(updateInfo), 0); |
| flasher::ops::automatic(*dev, args.config.metadata.offset, file, 0, mutate, |
| sizeof(info::UpdateInfo), std::nullopt, false); |
| } |
| |
| } // namespace ops |
| } // namespace flashupdate |