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
use super::{Chip, CodeSegment, RomSegment};
use crate::{
    image::{BootHeaderCfg, PartitionCfg},
    Error,
};
use deku::prelude::*;

pub const DEFAULT_PARTITION_CFG: &'static [u8] = include_bytes!("cfg/partition_cfg_2M.toml");
pub const DEFAULT_BOOTHEADER_CFG: &'static [u8] = include_bytes!("cfg/efuse_bootheader_cfg.conf");
pub const RO_PARAMS: &'static [u8] = include_bytes!("cfg/ro_params.dtb");
pub const BLSP_BOOT2: &'static [u8] = include_bytes!("image/blsp_boot2.bin");
pub const EFLASH_LOADER: &'static [u8] = include_bytes!("image/eflash_loader_40m.bin");
const ROM_START: u32 = 0x23000000;
// 16MB
const ROM_END: u32 = 0x23000000 + 0x1000000;

#[derive(Copy, Clone)]
pub struct Bl602;

impl Bl602 {
    fn addr_is_flash(&self, addr: u32) -> bool {
        addr >= ROM_START && addr < ROM_END
    }
}

impl Chip for Bl602 {
    fn target(&self) -> &'static str {
        "riscv32imac-unknown-none-elf"
    }

    fn get_eflash_loader(&self) -> &[u8] {
        EFLASH_LOADER
    }

    fn get_flash_segment<'a>(&self, code_segment: CodeSegment<'a>) -> Option<RomSegment<'a>> {
        if self.addr_is_flash(code_segment.addr) {
            Some(RomSegment::from_code_segment(
                code_segment.addr - ROM_START,
                code_segment,
            ))
        } else {
            None
        }
    }

    fn with_boot2(
        &self,
        mut partition_cfg: PartitionCfg,
        mut bootheader_cfg: BootHeaderCfg,
        ro_params: Vec<u8>,
        bin: &[u8],
    ) -> Result<Vec<RomSegment>, Error> {
        partition_cfg.update()?;
        let partition_cfg = partition_cfg.to_bytes()?;

        let boot2image = bootheader_cfg.make_image(0x2000, Vec::from(BLSP_BOOT2))?;
        let fw_image = bootheader_cfg.make_image(0x1000, Vec::from(bin))?;

        let segments = vec![
            RomSegment::from_vec(0x0, boot2image),
            RomSegment::from_vec(0xe000, partition_cfg.clone()),
            RomSegment::from_vec(0xf000, partition_cfg),
            RomSegment::from_vec(0x10000, fw_image),
            // TODO: generate from dts
            RomSegment::from_vec(0x1f8000, ro_params),
        ];

        Ok(segments)
    }
}