1 /* 2 * Copyright 2016 The Android Open Source Project 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 #ifndef AVB_MESSAGES_H_ 18 #define AVB_MESSAGES_H_ 19 20 #include <stdint.h> 21 #include <uapi/err.h> 22 23 #include <UniquePtr.h> 24 25 // Message serialization objects for communicating with Android 26 // Verified Boot app. 27 namespace avb { 28 29 // error codes for AVB protocol 30 enum class AvbError : uint32_t { 31 kNone = 0, // All OK 32 kInvalid = 1, // Invalid input, e.g. slot error is not valid. 33 kInternal = 2, // Error occurred during an operation in Trusty 34 }; 35 36 // Abstract base class of all AVB messages. 37 class AvbMessage { 38 public: 39 // Returns serialized size in bytes of the current state of the 40 // object. 41 virtual uint32_t GetSerializedSize() const = 0; 42 43 // Converts the object into its serialized representation. Returns 44 // number of bytes serialized. |payload| points to the start of 45 // the the message buffer, |end| points past the end of the message 46 // buffer. 47 virtual uint32_t Serialize(uint8_t* payload, const uint8_t* end) const = 0; 48 49 // Inflates the object from its serial representation. |payload| points to 50 // start of the message buffer, |end| points past the end of the message 51 // buffer. Returns a Trusty error. 52 virtual int Deserialize(const uint8_t* payload, const uint8_t* end) = 0; 53 set_error(AvbError error)54 void set_error(AvbError error) { error_ = error; } get_error()55 AvbError get_error() const { return error_; } 56 57 private: 58 AvbError error_ = AvbError::kNone; 59 }; 60 61 class RollbackIndexRequest : public AvbMessage { 62 public: RollbackIndexRequest()63 RollbackIndexRequest() {} RollbackIndexRequest(uint32_t slot,uint64_t value)64 RollbackIndexRequest(uint32_t slot, uint64_t value) 65 : value_(value), slot_(slot) {} 66 67 uint32_t GetSerializedSize() const override; 68 uint32_t Serialize(uint8_t* payload, const uint8_t* end) const override; 69 int Deserialize(const uint8_t* payload, const uint8_t* end) override; 70 set_slot(uint32_t slot)71 void set_slot(uint32_t slot) { slot_ = slot; } set_value(uint64_t value)72 void set_value(uint64_t value) { value_ = value; } get_value()73 uint64_t get_value() const { return value_; } get_slot()74 uint32_t get_slot() const { return slot_; } 75 76 private: 77 uint64_t value_ = 0; // Value to write to rollback index. 78 uint32_t slot_ = 0; // Slot number of requested rollback index. 79 }; 80 81 class RollbackIndexResponse : public AvbMessage { 82 public: RollbackIndexResponse()83 RollbackIndexResponse() {} RollbackIndexResponse(uint64_t value)84 RollbackIndexResponse(uint64_t value) : value_(value) {} 85 86 uint32_t GetSerializedSize() const override; 87 uint32_t Serialize(uint8_t* payload, const uint8_t* end) const override; 88 int Deserialize(const uint8_t* payload, const uint8_t* end) override; 89 set_value(uint64_t value)90 void set_value(uint64_t value) { value_ = value; } get_value()91 uint64_t get_value() { return value_; } 92 93 private: 94 uint64_t value_ = 0; // Value of requested rollback index. 95 }; 96 97 class EmptyMessage : public AvbMessage { 98 public: EmptyMessage()99 EmptyMessage() {} 100 GetSerializedSize()101 uint32_t GetSerializedSize() const override { return 0; } Serialize(uint8_t * payload,const uint8_t * end)102 uint32_t Serialize(uint8_t* payload, const uint8_t* end) const override { 103 return 0; 104 } Deserialize(const uint8_t * payload,const uint8_t * end)105 int Deserialize(const uint8_t* payload, const uint8_t* end) override { 106 return NO_ERROR; 107 } 108 }; 109 110 class GetVersionRequest : public EmptyMessage {}; 111 112 class GetVersionResponse : public AvbMessage { 113 public: GetVersionResponse()114 GetVersionResponse() {} GetVersionResponse(uint32_t version)115 GetVersionResponse(uint32_t version) : version_(version) {} 116 117 uint32_t GetSerializedSize() const override; 118 uint32_t Serialize(uint8_t* payload, const uint8_t* end) const override; 119 int Deserialize(const uint8_t* payload, const uint8_t* end) override; 120 set_version(uint32_t version)121 void set_version(uint32_t version) { version_ = version; } get_version()122 uint32_t get_version() { return version_; } 123 124 private: 125 uint32_t version_ = 0; 126 }; 127 128 class PermanentAttributesMessage : public AvbMessage { 129 public: PermanentAttributesMessage()130 PermanentAttributesMessage() {} 131 132 uint32_t GetSerializedSize() const override; 133 uint32_t Serialize(uint8_t* payload, const uint8_t* end) const override; 134 int Deserialize(const uint8_t* payload, const uint8_t* end) override; 135 get_attributes_size()136 uint32_t get_attributes_size() const { return attributes_size_; } get_attributes_buf()137 uint8_t* get_attributes_buf() const { return attributes_.get(); } set_attributes_buf(const uint8_t * buf,const uint32_t size)138 int set_attributes_buf(const uint8_t* buf, const uint32_t size) { 139 return Deserialize(buf, buf + size); 140 } 141 142 private: 143 UniquePtr<uint8_t[]> attributes_; 144 uint32_t attributes_size_ = 0; 145 }; 146 147 class WritePermanentAttributesRequest : public PermanentAttributesMessage {}; 148 class WritePermanentAttributesResponse : public EmptyMessage {}; 149 150 class ReadPermanentAttributesRequest : public EmptyMessage {}; 151 class ReadPermanentAttributesResponse : public PermanentAttributesMessage {}; 152 153 class LockStateMessage : public AvbMessage { 154 public: LockStateMessage()155 LockStateMessage() {} 156 157 uint32_t GetSerializedSize() const override; 158 uint32_t Serialize(uint8_t* payload, const uint8_t* end) const override; 159 int Deserialize(const uint8_t* payload, const uint8_t* end) override; 160 get_lock_state()161 uint8_t get_lock_state() const { return lock_state_; } set_lock_state(uint8_t lock_state)162 void set_lock_state(uint8_t lock_state) { lock_state_ = lock_state; } 163 164 private: 165 uint8_t lock_state_ = 0; 166 }; 167 168 class ReadLockStateRequest : public EmptyMessage {}; 169 class ReadLockStateResponse : public LockStateMessage {}; 170 171 class WriteLockStateRequest : public LockStateMessage {}; 172 class WriteLockStateResponse : public EmptyMessage {}; 173 174 } // namespace avb 175 176 #endif // AVB_MESSAGES_H_ 177