/src/hothd/payload_update.hpp
Line | Count | Source |
1 | | // Copyright 2024 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #pragma once |
16 | | |
17 | | #include "google3/cr51_image_descriptor.h" |
18 | | #include "google3/host_commands.h" |
19 | | |
20 | | #include "host_command.hpp" |
21 | | #include "message_util.hpp" |
22 | | #include "payload_update_interface.hpp" |
23 | | #include "sys.hpp" |
24 | | |
25 | | #include <stdplus/handle/managed.hpp> |
26 | | |
27 | | #include <optional> |
28 | | #include <span> |
29 | | #include <string> |
30 | | #include <vector> |
31 | | |
32 | | namespace google |
33 | | { |
34 | | namespace hoth |
35 | | { |
36 | | namespace internal |
37 | | { |
38 | | |
39 | | // Struct used to send activate request: |
40 | | struct ActivateRequest |
41 | | { |
42 | | payload_update_packet header; |
43 | | payload_update_activate activate; |
44 | | }; |
45 | | |
46 | | // Struct used to send confirm request: |
47 | | struct ConfirmRequest |
48 | | { |
49 | | payload_update_packet header; |
50 | | payload_update_confirm confirm; |
51 | | }; |
52 | | |
53 | | // Maximum allowed payload size |
54 | | constexpr size_t max_packet_size = |
55 | | mailbox_size - sizeof(payload_update_packet) - sizeof(ReqHeader); |
56 | | |
57 | | constexpr size_t kImageDescriptorAlignment = 64 * 1024; |
58 | | |
59 | | constexpr uint64_t kDescriptorMagic = 0x5f435344474d495f; // "_IMGDSC_" |
60 | | |
61 | | constexpr uint32_t kSectorSizeBytes = 4096; |
62 | | constexpr uint32_t kEraseChunkSizeBytes = kSectorSizeBytes * 32; |
63 | | |
64 | | /** @class PayloadUpdateImpl |
65 | | * @brief PayloadUpdate method implementation |
66 | | * @details Methods to interface with Hoth to perform payload update |
67 | | */ |
68 | | class PayloadUpdateImpl : public PayloadUpdate |
69 | | { |
70 | | public: |
71 | | explicit PayloadUpdateImpl(HostCommand* hostCmd, |
72 | | const Sys* sys = &sys_impl) : |
73 | 1.03k | hostCmd(hostCmd), sys(sys) |
74 | 1.03k | {} |
75 | | |
76 | | void initiate() const override; |
77 | | void erase(uint32_t offset, uint32_t size) const override; |
78 | | void eraseAndSendStaticWP(const std::string& path) const override; |
79 | | void read(uint32_t offset, std::span<uint8_t> data) const override; |
80 | | void verify() const override; |
81 | | payload_update_status getStatus() const override; |
82 | | void activate(Side side, Persistence persistence) const override; |
83 | | void send(const std::string& imagePath) const override; |
84 | | std::optional<payload_update_confirm_response> confirm( |
85 | | enum payload_update_confirm_option option, uint32_t timeout, |
86 | | uint64_t confirmation_cookie) const override; |
87 | | bool findDescriptor(const std::string& path, |
88 | | uint32_t* desc_offset) const override; |
89 | | void eraseAndSendStaticWPRegions(const std::string& path, |
90 | | uint32_t desc_offset) const override; |
91 | | // Arbitrarily picked due to the performance of the calls taking ~0.4s |
92 | | static constexpr size_t verify_size = 512 * 1024; |
93 | | |
94 | | private: |
95 | | void sendCommand(uint8_t command, uint32_t offset = 0, |
96 | | uint32_t len = 0) const; |
97 | | void sendCommand(std::span<const uint8_t> request) const; |
98 | | [[nodiscard]] std::span<const uint8_t> sendCommand( |
99 | | std::vector<uint8_t>& buf, uint8_t command, uint32_t offset = 0, |
100 | | uint32_t len = 0) const; |
101 | | [[nodiscard]] std::span<const uint8_t> sendCommand( |
102 | | std::vector<uint8_t>& buf, std::span<const uint8_t> request) const; |
103 | | void trimAndSend(std::span<const uint8_t> data, |
104 | | uint32_t globalOffset) const; |
105 | | /** @brief RAII wrapper and its destructor for opening a file descriptor */ |
106 | | static void closeFd(int&& fd, const Sys*& sys) |
107 | 5.03k | { |
108 | 5.03k | if (sys) |
109 | 5.03k | { |
110 | 5.03k | sys->close(fd); |
111 | 5.03k | } |
112 | 5.03k | } |
113 | | using Fd = stdplus::Managed<int, const Sys*>::Handle<closeFd>; |
114 | | |
115 | | /** @brief Connection to Hoth for sending and receiving host commands */ |
116 | | HostCommand* hostCmd = nullptr; |
117 | | const Sys* sys = nullptr; |
118 | | }; |
119 | | |
120 | | } // namespace internal |
121 | | |
122 | | } // namespace hoth |
123 | | |
124 | | } // namespace google |