Skip to content

Commit 5824965

Browse files
committed
tool: 新增算能芯片 ROM 辅助芯片
Signed-off-by: zhujunxing <zjx1319@hust.edu.cn>
1 parent 018f66f commit 5824965

File tree

7 files changed

+283
-0
lines changed

7 files changed

+283
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
# Rust compiler cache
12
/target
3+
4+
# Generated by Sophgo ROM Tool package
5+
fip.bin

Cargo.lock

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ resolver = "2"
33
members = [
44
"sophgo-rom-rt",
55
"sophgo-rom-rt/macros",
6+
"sophgo-rom-tool",
67
"examples/hello-world",
78
]

examples/hello-world/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ fn main() -> ! {
1212
let uart = unsafe { &*(0x04140000 as *const uart16550::Uart16550<u32>) };
1313
loop {
1414
uart.write(b"Hello World from Rust!\n");
15+
// TODO fix Uart16550 crate bug; it doesn't block when UART FIFO is not empty
1516
}
1617
}

sophgo-rom-tool/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "sophgo-rom-tool"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
clap = { version = "4.4.16", features = ["derive"] }
10+
crc = "3.0.1"

sophgo-rom-tool/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 算能芯片 ROM 辅助工具
2+
3+
本工具可由二进制文件生成算能芯片 ROM 镜像文件(fip.bin)。
4+
5+
Example:
6+
7+
```bash
8+
cargo build -p hello-world --target riscv64imac-unknown-none-elf --release
9+
rust-objcopy --binary-architecture=riscv64 --strip-all -O binary .\target\riscv64imac-unknown-none-elf\release\hello-world .\target\hello-world.bin
10+
cargo run --bin sophgo-rom-tool -- .\target\hello-world.bin -o .\target\fip.bin
11+
```

sophgo-rom-tool/src/main.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use clap::Parser;
2+
use crc::{Crc, CRC_16_XMODEM};
3+
use std::fs;
4+
5+
/// Generate a ROM image for Sophgo chips.
6+
#[derive(Parser, Debug)]
7+
#[command(author, version, about, long_about = None)]
8+
struct Args {
9+
/// Input filename.
10+
input: String,
11+
/// Output ROM image filename.
12+
#[arg(short, long, default_value_t = String::from("fip.bin"))]
13+
output: String,
14+
}
15+
16+
const SIGNATURE: [u8; 32] = [
17+
0x6F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19+
];
20+
21+
const MAGIC: [u8; 12] = [
22+
0x43, 0x56, 0x42, 0x4C, 0x30, 0x31, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00,
23+
]; // CVBL01
24+
25+
fn main() {
26+
let args = Args::parse();
27+
let data = fs::read(&args.input).expect("Unable to read file");
28+
let data_len = data.len();
29+
if data_len < SIGNATURE.len() || data[..SIGNATURE.len()] != SIGNATURE {
30+
panic!("Invalid input file");
31+
}
32+
33+
let mut image = vec![0; 0x1000];
34+
image.extend(data);
35+
let padding_len = 512 - (data_len % 512);
36+
image.extend(core::iter::repeat(0).take(padding_len));
37+
38+
let crc = Crc::<u16>::new(&CRC_16_XMODEM);
39+
let bl2_checksum = crc.checksum(&image[0x1000..]);
40+
41+
image[..MAGIC.len()].copy_from_slice(&MAGIC);
42+
image[0xBC..0xC0].copy_from_slice(&0x2F8u32.to_le_bytes()); // chip_conf_size
43+
image[0xC0..0xC4].copy_from_slice(&0xCAFE0000u32.to_le_bytes()); // blcp_img_cksum
44+
image[0xD4..0xD8].copy_from_slice(&(0xCAFE0000u32 + bl2_checksum as u32).to_le_bytes()); // bl2_img_cksum
45+
image[0xD8..0xDC].copy_from_slice(&((data_len + padding_len) as u32).to_le_bytes()); // bl2_img_size
46+
47+
let param_checksum = crc.checksum(&image[0x10..0x800]);
48+
image[0xC..0x10].copy_from_slice(
49+
(0xCAFE0000u32 + param_checksum as u32)
50+
.to_le_bytes()
51+
.as_ref(),
52+
); // param_cksum
53+
54+
fs::write(&args.output, image).expect("Unable to write file");
55+
}

0 commit comments

Comments
 (0)