1#![no_std]
2#[cfg(feature = "device")]
5extern crate alloc;
6
7#[cfg(feature = "device")]
8pub mod device;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Error {
12 InvalidState,
13 Length,
14 Encoding,
15 Authentication,
16 Target,
17 SecurityVersion,
18 Storage,
19 Geometry,
20 Unsupported,
21 Recovery,
22}
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24#[repr(u8)]
25pub enum State {
26 Idle,
27 Receiving,
28 Verified,
29 PendingBoot,
30 Failed,
31}
32
33pub const AUTHENTICATED_UPDATE: u32 = 1;
36pub const FUNCTIONAL_REVERT: u32 = 2;
37pub const VERIFIED_BOOT: u32 = 4;
38pub const HARDWARE_ROOT: u32 = 8;
39#[derive(Debug, Clone, Copy)]
40pub struct Capabilities {
41 pub max_image_size: u64,
42 pub max_chunk_size: u32,
43 pub guarantees: u32,
44}
45
46pub trait FirmwareBackend {
54 type Verified;
55 fn capabilities(&self) -> Capabilities;
56 fn begin(&mut self, size: u64, metadata: &[u8]) -> Result<(), Error>;
57 fn write(&mut self, bytes: &[u8]) -> Result<usize, Error>;
58 fn finish(&mut self) -> Result<Self::Verified, Error>;
59 fn activate(&mut self, verified: Self::Verified) -> Result<(), Error>;
60 fn abort(&mut self) -> Result<(), Error>;
61 fn confirm_running(&mut self) -> Result<(), Error>;
62}
63
64pub struct Update<B: FirmwareBackend> {
69 backend: B,
70 state: State,
71 expected: u64,
72 received: u64,
73 verified: Option<B::Verified>,
74}
75impl<B: FirmwareBackend> Update<B> {
76 pub fn new(backend: B) -> Self {
77 Self {
78 backend,
79 state: State::Idle,
80 expected: 0,
81 received: 0,
82 verified: None,
83 }
84 }
85 pub fn capabilities(&self) -> Capabilities {
86 self.backend.capabilities()
87 }
88 pub fn state(&self) -> State {
89 self.state
90 }
91 pub fn received(&self) -> u64 {
92 self.received
93 }
94 pub fn into_backend(self) -> B {
95 self.backend
96 }
97 fn failed<T>(&mut self, error: Error) -> Result<T, Error> {
98 self.verified = None;
99 self.state = State::Failed;
100 Err(error)
101 }
102 pub fn begin(&mut self, size: u64, metadata: &[u8]) -> Result<(), Error> {
103 if self.state != State::Idle {
104 return Err(Error::InvalidState);
105 }
106 if size == 0 || size > self.capabilities().max_image_size {
107 return Err(Error::Length);
108 }
109 if let Err(e) = self.backend.begin(size, metadata) {
110 return self.failed(e);
111 }
112 self.expected = size;
113 self.received = 0;
114 self.state = State::Receiving;
115 Ok(())
116 }
117 pub fn write(&mut self, bytes: &[u8]) -> Result<usize, Error> {
118 if self.state == State::Verified && !bytes.is_empty() {
119 return self.failed(Error::InvalidState);
120 }
121 if self.state != State::Receiving {
122 return Err(Error::InvalidState);
123 }
124 if bytes.len() as u64 > self.expected - self.received {
125 return self.failed(Error::Length);
126 }
127 if bytes.is_empty() {
128 return Ok(0);
129 }
130 let limit = bytes.len().min(self.capabilities().max_chunk_size as usize);
131 if limit == 0 {
132 return self.failed(Error::Geometry);
133 }
134 let count = match self.backend.write(&bytes[..limit]) {
135 Ok(n) => n,
136 Err(e) => return self.failed(e),
137 };
138 if count == 0 || count > limit {
139 return self.failed(Error::Storage);
140 }
141 self.received += count as u64;
142 Ok(count)
143 }
144 pub fn finish(&mut self) -> Result<(), Error> {
145 if self.state != State::Receiving {
146 return Err(Error::InvalidState);
147 }
148 if self.received != self.expected {
149 return self.failed(Error::Length);
150 }
151 self.verified = Some(match self.backend.finish() {
152 Ok(v) => v,
153 Err(e) => return self.failed(e),
154 });
155 self.state = State::Verified;
156 Ok(())
157 }
158 pub fn activate(&mut self) -> Result<(), Error> {
159 if self.state != State::Verified {
160 return Err(Error::InvalidState);
161 }
162 let proof = self.verified.take().ok_or(Error::InvalidState)?;
163 if let Err(e) = self.backend.activate(proof) {
164 return self.failed(e);
165 }
166 self.state = State::PendingBoot;
167 Ok(())
168 }
169 pub fn abort(&mut self) -> Result<(), Error> {
170 self.verified = None;
171 if let Err(e) = self.backend.abort() {
172 return self.failed(e);
173 }
174 self.state = State::Idle;
175 self.expected = 0;
176 self.received = 0;
177 Ok(())
178 }
179 pub fn confirm_running(&mut self) -> Result<(), Error> {
180 if self.state != State::Idle {
181 return Err(Error::InvalidState);
182 }
183 self.backend.confirm_running()
184 }
185}