spl_tool 0.2.0

Port of StarFive's C spl_tool with default support for VisionFive2
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// SPDX-License-Identifier: GPL-2.0+

use core::{cmp, mem};

use super::{Error, Result};

/// Default value of the offset of SPL header: `64+256+256 = 0x240`
pub const DEF_SOFS: u32 = 0x240;
/// Default SPL version ID.
pub const DEF_VERS: u32 = 0x01010101;
/// Default value of `SBL_BAK_OFFSET`.
pub const DEF_BACKUP: u32 = 0x20_0000;
/// Default value for the offset from `HDR` to `SPL_IMAGE`.
pub const DEF_RESL: u32 = 0x400;
/// Default filename of the U-Boot SPL binary.
pub const DEF_SPL_FILE: &str = "u-boot-spl.bin";
/// Maximum path length: defined in `linux/limits.h`.
pub const PATH_MAX: usize = 4096;
/// Value indicating a failed CRC32 calculation/check.
pub const CRC_FAILED: u32 = 0x5a5a5a5a;
/// Default size for an U-Boot SPL header.
///
/// Currently set to `1 KiB` (including reserved padding).
pub const SPL_HEADER_LEN: usize = 1024;
/// Maximum supported length of an U-Boot SPL image.
pub const MAX_SPL_LEN: usize = 180048;

const PATH_ZERO_BYTES: [u8; PATH_MAX] = [0u8; PATH_MAX];
const RES_PAD2_LEN: usize = 636;
const RES_PAD3_LEN: usize = 364;

/// Represents the U-Boot header for the SPL binary.
///
/// All `u32` end up little endian in output header.
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct UbootSplHeader {
    sofs: u32,
    bofs: u32,
    // reserved offset padding buffer
    zro2: [u8; RES_PAD2_LEN],
    vers: u32,
    fsiz: u32,
    resl: u32,
    crcs: u32,
    // reserved offset padding buffer
    zro3: [u8; RES_PAD3_LEN],
}

impl UbootSplHeader {
    /// Create a new [UbootSplHeader].
    pub const fn new() -> Self {
        Self {
            sofs: DEF_SOFS,
            bofs: DEF_BACKUP,
            zro2: [0; 636],
            vers: DEF_VERS,
            fsiz: 0,
            resl: DEF_RESL,
            crcs: 0,
            zro3: [0; 364],
        }
    }

    /// Gets the offset of SPL header: 64+256+256 = 0x240
    pub const fn sofs(&self) -> u32 {
        self.sofs
    }

    /// Gets the `SBL_BAK_OFFSET`:
    ///
    /// Offset of backup SBL from Flash info start from `input_sbl_normal.cfg`
    pub const fn bofs(&self) -> u32 {
        self.bofs
    }

    /// Sets the `SBL_BAK_OFFSET`:
    ///
    /// Offset of backup SBL from Flash info start from `input_sbl_normal.cfg`
    pub fn set_bofs(&mut self, val: u32) {
        self.bofs = val;
    }

    /// Builder function that sets the `SBL_BAK_OFFSET`:
    ///
    /// Offset of backup SBL from Flash info start from `input_sbl_normal.cfg`
    pub fn with_bofs(mut self, val: u32) -> Self {
        self.set_bofs(val);
        self
    }

    /// Gets the version: `0x01010101` by default
    pub const fn vers(&self) -> u32 {
        self.vers
    }

    /// Sets the version.
    pub fn set_vers(&mut self, val: u32) {
        self.vers = val;
    }

    /// Builder function that sets the version.
    pub fn with_vers(mut self, val: u32) -> Self {
        self.set_vers(val);
        self
    }

    /// Gets the `u-boot-spl.bin` size in bytes.
    pub const fn fsiz(&self) -> u32 {
        self.fsiz
    }

    /// Sets the `u-boot-spl.bin` size in bytes.
    pub fn set_fsiz(&mut self, val: u32) {
        self.fsiz = val;
    }

    /// Builder function that sets the `u-boot-spl.bin` size in bytes.
    pub fn with_fsiz(mut self, val: u32) -> Self {
        self.set_fsiz(val);
        self
    }

    /// Gets the offset from `HDR` to `SPL_IMAGE`.
    ///
    /// Defaults to `0x400 (00 04 00 00)` currently.
    pub const fn resl(&self) -> u32 {
        self.resl
    }

    /// Sets the offset from `HDR` to `SPL_IMAGE`.
    pub fn set_resl(&mut self, val: u32) {
        self.resl = val;
    }

    /// Builder function that sets the offset from `HDR` to `SPL_IMAGE`.
    pub fn with_resl(mut self, val: u32) -> Self {
        self.set_resl(val);
        self
    }

    /// Gets the CRC32 of `u-boot-spl.bin`.
    pub const fn crcs(&self) -> u32 {
        self.crcs
    }

    /// Sets the CRC32 of `u-boot-spl.bin`.
    pub fn set_crcs(&mut self, val: u32) {
        self.crcs = val;
    }

    /// Builder function that Sets the CRC32 of `u-boot-spl.bin`.
    pub fn with_crcs(mut self, val: u32) -> Self {
        self.set_crcs(val);
        self
    }
}

impl From<&UbootSplHeader> for [u8; SPL_HEADER_LEN] {
    fn from(val: &UbootSplHeader) -> Self {
        const WORD_LEN: usize = mem::size_of::<u32>();

        let mut res = [0u8; SPL_HEADER_LEN];
        let mut idx = 0usize;

        // serialize SOFS field to buffer
        res[idx..idx.saturating_add(WORD_LEN)].copy_from_slice(val.sofs.to_le_bytes().as_ref());
        idx = idx.saturating_add(WORD_LEN);

        // serialize BOFS field to buffer
        res[idx..idx.saturating_add(WORD_LEN)].copy_from_slice(val.bofs.to_le_bytes().as_ref());
        idx = idx.saturating_add(WORD_LEN);

        // skip `zro2` reserved padding
        idx = idx.saturating_add(RES_PAD2_LEN);

        // serialize VERS field to buffer
        res[idx..idx.saturating_add(WORD_LEN)].copy_from_slice(val.vers.to_le_bytes().as_ref());
        idx = idx.saturating_add(WORD_LEN);

        // serialize FSIZ field to buffer
        res[idx..idx.saturating_add(WORD_LEN)].copy_from_slice(val.fsiz.to_le_bytes().as_ref());
        idx = idx.saturating_add(WORD_LEN);

        // serialize RESL field to buffer
        res[idx..idx.saturating_add(WORD_LEN)].copy_from_slice(val.resl.to_le_bytes().as_ref());
        idx = idx.saturating_add(WORD_LEN);

        // serialize CRCS field to buffer
        res[idx..idx.saturating_add(WORD_LEN)].copy_from_slice(val.crcs.to_le_bytes().as_ref());

        res
    }
}

impl From<UbootSplHeader> for [u8; SPL_HEADER_LEN] {
    fn from(val: UbootSplHeader) -> Self {
        (&val).into()
    }
}

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

    fn try_from(val: &[u8]) -> Result<Self> {
        const WORD_LEN: usize = mem::size_of::<u32>();

        if val.len() < SPL_HEADER_LEN {
            Err(Error::InvalidHeaderLen((val.len(), SPL_HEADER_LEN)))
        } else {
            let mut idx = 0usize;

            // deserialize SOFS field from buffer
            let sofs = u32::from_le_bytes(val[idx..idx.saturating_add(WORD_LEN)].try_into()?);
            idx = idx.saturating_add(WORD_LEN);

            // deserialize BOFS field from buffer
            let bofs = u32::from_le_bytes(val[idx..idx.saturating_add(WORD_LEN)].try_into()?);
            idx = idx.saturating_add(WORD_LEN);

            // skip deserializing `zro2` reserved padding
            // It doesn't matter what is in this field, so ignore it.
            let zro2 = [0u8; RES_PAD2_LEN];
            idx = idx.saturating_add(RES_PAD2_LEN);

            // deserialize VERS field from buffer
            let vers = u32::from_le_bytes(val[idx..idx.saturating_add(WORD_LEN)].try_into()?);
            idx = idx.saturating_add(WORD_LEN);

            // deserialize FSIZ field from buffer
            let fsiz = u32::from_le_bytes(val[idx..idx.saturating_add(WORD_LEN)].try_into()?);
            idx = idx.saturating_add(WORD_LEN);

            // deserialize RESL field from buffer
            let resl = u32::from_le_bytes(val[idx..idx.saturating_add(WORD_LEN)].try_into()?);
            idx = idx.saturating_add(WORD_LEN);

            // deserialize CRCS field from buffer
            let crcs = u32::from_le_bytes(val[idx..idx.saturating_add(WORD_LEN)].try_into()?);

            // skip deserializing `zro3` reserved padding
            // It doesn't matter what is in this field, so ignore it.
            let zro3 = [0u8; RES_PAD3_LEN];

            Ok(Self {
                sofs,
                bofs,
                zro2,
                vers,
                fsiz,
                resl,
                crcs,
                zro3,
            })
        }
    }
}

impl<const N: usize> TryFrom<&[u8; N]> for UbootSplHeader {
    type Error = Error;

    fn try_from(val: &[u8; N]) -> Result<Self> {
        val.as_ref().try_into()
    }
}

impl<const N: usize> TryFrom<[u8; N]> for UbootSplHeader {
    type Error = Error;

    fn try_from(val: [u8; N]) -> Result<Self> {
        val.as_ref().try_into()
    }
}

impl Default for UbootSplHeader {
    fn default() -> Self {
        Self::new()
    }
}

/// Represents configuration arguments for SPL header generation.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct HeaderConf {
    name: [u8; PATH_MAX],
    vers: u32,
    bofs: u32,
    create_header: bool,
    fix_image_header: bool,
    resl: u32,
}

impl HeaderConf {
    /// Creates a new [HeaderConf].
    pub const fn new() -> Self {
        Self {
            name: [0u8; PATH_MAX],
            vers: DEF_VERS,
            bofs: DEF_BACKUP,
            resl: DEF_RESL,
            create_header: false,
            fix_image_header: false,
        }
    }

    /// Gets the header name as a string.
    pub fn name(&self) -> &str {
        core::str::from_utf8(self.name[..self.name_len()].as_ref()).unwrap_or("")
    }

    fn name_len(&self) -> usize {
        self.name
            .iter()
            .position(|&b| b == 0)
            .unwrap_or(self.name.len())
    }

    /// Sets the header name from a string.
    pub fn set_name(&mut self, val: &str) {
        let val_bytes = val.as_bytes();
        let len = cmp::min(PATH_MAX - 1, val_bytes.len());
        self.name[..len].copy_from_slice(val_bytes[..len].as_ref());
        self.name[len..].copy_from_slice(PATH_ZERO_BYTES[len..].as_ref());
    }

    /// Sets the header name from a string.
    pub fn with_name(mut self, val: &str) -> Self {
        self.set_name(val);
        self
    }

    /// Gets the version.
    pub const fn vers(&self) -> u32 {
        self.vers
    }

    /// Sets the version.
    pub fn set_vers(&mut self, val: u32) {
        self.vers = val;
    }

    /// Builder function that sets the version.
    pub fn with_vers(mut self, val: u32) -> Self {
        self.set_vers(val);
        self
    }

    /// Gets the `SBL_BAK_OFFSET`:
    ///
    /// Offset of backup SBL from Flash info start from `input_sbl_normal.cfg`
    pub const fn bofs(&self) -> u32 {
        self.bofs
    }

    /// Sets the `SBL_BAK_OFFSET`:
    ///
    /// Offset of backup SBL from Flash info start from `input_sbl_normal.cfg`
    pub fn set_bofs(&mut self, val: u32) {
        self.bofs = val;
    }

    /// Builder function that sets the `SBL_BAK_OFFSET`:
    ///
    /// Offset of backup SBL from Flash info start from `input_sbl_normal.cfg`
    pub fn with_bofs(mut self, val: u32) -> Self {
        self.set_bofs(val);
        self
    }

    /// Gets the `resl` field:
    ///
    /// Offset of SBL image from SBL header.
    pub const fn resl(&self) -> u32 {
        self.resl
    }

    /// Sets the `resl` field:
    ///
    /// Offset of SBL image from start of SBL header.
    pub fn set_resl(&mut self, val: u32) {
        self.resl = val;
    }

    /// Builder function that sets the `resl` field:
    ///
    /// Offset of SBL image from start of SBL header.
    pub fn with_resl(mut self, val: u32) -> Self {
        self.set_resl(val);
        self
    }

    /// Gets whether to create the SPL header.
    pub const fn create_header(&self) -> bool {
        self.create_header
    }

    /// Sets whether to create the SPL header.
    pub fn set_create_header(&mut self, val: bool) {
        self.create_header = val;
    }

    /// Builder function that sets whether to create the SPL header.
    pub fn with_create_header(mut self, val: bool) -> Self {
        self.set_create_header(val);
        self
    }

    /// Gets whether to create the fix image header.
    pub const fn fix_image_header(&self) -> bool {
        self.fix_image_header
    }

    /// Sets whether to create the fix image header.
    pub fn set_fix_image_header(&mut self, val: bool) {
        self.fix_image_header = val;
    }

    /// Builder function that sets whether to create the fix image header.
    pub fn with_fix_image_header(mut self, val: bool) -> Self {
        self.set_fix_image_header(val);
        self
    }
}

impl Default for HeaderConf {
    fn default() -> Self {
        Self::new()
    }
}