1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use crate::{utils, PartitionError, PartitionType, SliceExt};

#[cfg(feature = "heapless")]
use heapless::String;

/// Data buffer for partition entry
pub type PartitionBuffer = [u8; PartitionEntry::SIZE];

/// ESP Partition info
///
/// Binary representation:
///
/// Off | Len | Desc
/// --- | --- | ----
///   0 |   2 | Magic
///   2 |   1 | Type
///   3 |   1 | SubType
///   4 |   4 | Offset
///   8 |   4 | Size
///  12 |  16 | Name
///  28 |   4 | Flags
///
#[derive(Clone, Debug, Default)]
pub struct PartitionEntry {
    /// Partition type and subtype
    pub type_: PartitionType,

    /// Partition offset
    pub offset: u32,

    /// Partition size
    pub size: usize,

    /// Partition name
    #[cfg(feature = "heapless")]
    pub name: String<{ Self::MAX_NAME_LEN }>,

    #[cfg(not(feature = "heapless"))]
    name: [u8; Self::MAX_NAME_LEN],

    /// Partition encrypted flag
    pub encrypted: bool,
}

impl PartitionEntry {
    /// Magic bytes at beginning binary partition representation
    pub const MAGIC: [u8; 2] = [0xAA, 0x50];

    /// The size of binary represented partition data
    pub const SIZE: usize = 32;

    /// Max partition name length
    pub const MAX_NAME_LEN: usize = 16;

    /// Create partition info
    pub fn new(
        type_: impl Into<PartitionType>,
        offset: u32,
        size: usize,
        name: impl AsRef<str>,
        encrypted: bool,
    ) -> Result<Self, PartitionError> {
        let name = name.as_ref();

        #[cfg(feature = "heapless")]
        let name = name.try_into().map_err(|_| PartitionError::InvalidString)?;

        #[cfg(not(feature = "heapless"))]
        let name = {
            let mut name_data = [0u8; Self::MAX_NAME_LEN];
            utils::name_into(&mut name_data, name)?;
            name_data
        };

        Ok(Self {
            type_: type_.into(),
            offset,
            size,
            name,
            encrypted,
        })
    }

    /// Set partition offset with alignment check
    pub fn set_offset(&mut self, offset: u32) -> Result<(), PartitionError> {
        self.type_.check_offset(offset)?;
        self.offset = offset;
        Ok(())
    }

    /// Get partition name
    pub fn name(&self) -> &str {
        #[cfg(not(feature = "heapless"))]
        {
            let name = utils::name_trim(&self.name);
            // utf8 data already validated
            unsafe { core::str::from_utf8_unchecked(name) }
        }

        #[cfg(feature = "heapless")]
        {
            &self.name
        }
    }

    /// Set partition name
    pub fn set_name(&mut self, name: impl AsRef<str>) -> Result<(), PartitionError> {
        let name = name.as_ref();

        #[cfg(feature = "heapless")]
        {
            self.name = name.try_into().map_err(|_| PartitionError::InvalidString)?;
            Ok(())
        }

        #[cfg(not(feature = "heapless"))]
        {
            utils::name_into(&mut self.name, name)
        }
    }

    /// Convert partition data from binary representation
    pub fn from_bytes(data: &PartitionBuffer) -> Result<Self, PartitionError> {
        let (magic, data) = data.split_array_ref_();
        if magic != &Self::MAGIC {
            return Err(PartitionError::InvalidMagic);
        }

        let (type_data, data) = data.split_array_ref_();
        let type_ = type_data.try_into()?;

        let (offset_data, data) = data.split_array_ref_();
        let offset = u32::from_le_bytes(*offset_data);

        let (size_data, data) = data.split_array_ref_();
        let size = u32::from_le_bytes(*size_data) as usize;

        let (name_data, data) = data.split_array_ref_();
        let _name_str = utils::name_from(name_data)?;

        #[cfg(feature = "heapless")]
        let name = _name_str.into();

        #[cfg(not(feature = "heapless"))]
        let name = *name_data;

        let (flags_data, _) = data.split_array_ref_();
        let flags = u32::from_le_bytes(*flags_data);

        let encrypted = flags & 0x01 != 0;

        Ok(Self {
            type_,
            offset,
            size,
            name,
            encrypted,
        })
    }

    /// Convert partition data to binary representation
    pub fn to_bytes(&self, data: &mut PartitionBuffer) -> Result<(), PartitionError> {
        self.type_.check_offset(self.offset)?;

        let (magic_data, data) = data.split_array_mut_();
        *magic_data = Self::MAGIC;

        let (type_data, data) = data.split_array_mut_();
        self.type_.to_bytes(type_data)?;

        let (offset_data, data) = data.split_array_mut_();
        *offset_data = self.offset.to_le_bytes();

        let (size_data, data) = data.split_array_mut_();
        *size_data = (self.size as u32).to_le_bytes();

        let (name_data, data) = data.split_array_mut_();

        #[cfg(feature = "heapless")]
        utils::name_into(name_data, self.name.as_str())?;

        #[cfg(not(feature = "heapless"))]
        {
            *name_data = self.name;
        }

        let (flags_data, _) = data.split_array_mut_();
        *flags_data = (self.encrypted as u32).to_le_bytes();

        Ok(())
    }
}

impl AsRef<PartitionEntry> for PartitionEntry {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl TryFrom<&[u8]> for PartitionEntry {
    type Error = PartitionError;

    fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
        <&PartitionBuffer>::try_from(data)
            .map_err(|_| PartitionError::NotEnoughData)
            .and_then(Self::try_from)
    }
}

impl TryFrom<&PartitionBuffer> for PartitionEntry {
    type Error = PartitionError;

    fn try_from(data: &PartitionBuffer) -> Result<Self, Self::Error> {
        Self::from_bytes(data)
    }
}

/// MD5 checksum data
pub type Md5Data = [u8; 16];

/// ESP Partition MD5
///
/// Binary representation:
///
/// Off | Len | Desc
/// --- | --- | ----
///   0 |   2 | Magic
///   2 |  14 | Reserved
///  16 |  16 | MD5 data
///
#[derive(Clone, Debug, Default)]
pub struct PartitionMd5 {
    /// MD5 checksum data
    pub data: Md5Data,
}

impl From<PartitionMd5> for Md5Data {
    fn from(md5: PartitionMd5) -> Self {
        md5.data
    }
}

impl From<Md5Data> for PartitionMd5 {
    fn from(data: Md5Data) -> Self {
        Self { data }
    }
}

#[cfg(feature = "md5")]
impl From<md5::Digest> for PartitionMd5 {
    fn from(digest: md5::Digest) -> Self {
        Self {
            data: digest.into(),
        }
    }
}

impl PartitionMd5 {
    /// Magic bytes
    pub const MAGIC: [u8; 2] = [0xeb, 0xeb];

    /// The size of reserved space between magic bytes and MD5 data
    pub const RESERVED_SIZE: usize = 14;

    /// The content of reserved space between magic bytes and MD5 data
    pub const RESERVED_DATA: u8 = 0xff;

    /// Convert md5 data from binary representation
    pub fn from_bytes(data: &PartitionBuffer) -> Result<Self, PartitionError> {
        let (magic, data) = data.split_array_ref_();
        if magic != &Self::MAGIC {
            return Err(PartitionError::InvalidMagic);
        }

        let (reserved_data, data) = data.split_array_ref_::<{ Self::RESERVED_SIZE }>();
        for reserved in reserved_data {
            if *reserved != Self::RESERVED_DATA {
                return Err(PartitionError::InvalidMagic);
            }
        }

        let (md5_data, _) = data.split_array_ref_();

        Ok(Self { data: *md5_data })
    }

    /// Convert md5 data to binary representation
    pub fn to_bytes(&self, data: &mut PartitionBuffer) -> Result<(), PartitionError> {
        let (magic_data, data) = data.split_array_mut_();
        *magic_data = Self::MAGIC;

        let (reserved_data, data) = data.split_array_mut_::<{ Self::RESERVED_SIZE }>();
        reserved_data.fill(Self::RESERVED_DATA);

        let (md5_data, _) = data.split_array_mut_();
        *md5_data = self.data;

        Ok(())
    }
}

impl TryFrom<&[u8]> for PartitionMd5 {
    type Error = PartitionError;

    fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
        <&PartitionBuffer>::try_from(data)
            .map_err(|_| PartitionError::NotEnoughData)
            .and_then(Self::try_from)
    }
}

impl TryFrom<&PartitionBuffer> for PartitionMd5 {
    type Error = PartitionError;

    fn try_from(data: &PartitionBuffer) -> Result<Self, Self::Error> {
        Self::from_bytes(data)
    }
}