Browse Source

feat: Add pre-commit, typos, and git-cliff for automation and check typos

- Added `pre-commit` to run code checks before committing.
- Integrated `typos-cli` for spell checking.
- Added `git-cliff` to generate changelogs automatically.
- Fixed some typos in the documentation, comments and code.

Signed-off-by: Zongyao Chen [email protected]
Zongyao 4 months ago
parent
commit
7180a8d572

+ 47 - 0
.pre-commit-config.yaml

@@ -0,0 +1,47 @@
+fail_fast: false
+repos:
+  - repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v4.3.0
+    hooks:
+      - id: check-byte-order-marker
+      - id: check-case-conflict
+      - id: check-merge-conflict
+      - id: check-symlinks
+      - id: check-yaml
+      - id: end-of-file-fixer
+      - id: mixed-line-ending
+      - id: trailing-whitespace
+  - repo: https://github.com/psf/black
+    rev: 22.10.0
+    hooks:
+      - id: black
+  - repo: local
+    hooks:
+      - id: cargo-fmt
+        name: cargo fmt
+        description: Format files with rustfmt.
+        entry: bash -c 'cargo fmt -- --check'
+        language: rust
+        files: \.rs$
+        args: []
+      - id: typos
+        name: typos
+        description: check typo
+        entry: bash -c 'typos'
+        language: rust
+        files: \.*$
+        pass_filenames: false
+      - id: cargo-check
+        name: cargo check
+        description: Check the package for errors.
+        entry: bash -c 'cargo check --target riscv64imac-unknown-none-elf --all --no-default-features'
+        language: rust
+        files: \.rs$
+        pass_filenames: false
+      - id: cargo-clippy
+        name: cargo clippy
+        description: Lint rust sources
+        entry: bash -c 'cargo clippy --target riscv64imac-unknown-none-elf --all --no-default-features -- -D warnings'
+        language: rust
+        files: \.rs$
+        pass_filenames: false

+ 6 - 0
CHANGELOG.md

@@ -0,0 +1,6 @@
+# Changelog
+
+All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.
+
+---
+## [unreleased]

+ 24 - 9
Makefile.toml

@@ -11,20 +11,33 @@ args = ["build", "-prustsbi-prototyper", "--release", "--features=nemu,payload"]
 
 [tasks.prototyper-nemu]
 command = "rust-objcopy"
-args = ["--binary-architecture=riscv64", "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper",
-        "--output-target=binary", "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin"
+args = [
+        "--binary-architecture=riscv64",
+        "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper",
+        "--output-target=binary",
+        "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin",
 ]
 dependencies = ["prototyper-nemu-build"]
 
 [tasks.prototyper-build]
 command = "cargo"
-args = ["build", "-prustsbi-prototyper", "--release", "--target", "riscv64imac-unknown-none-elf", "-Zbuild-std=core"]
-env = {"RUSTFLAGS"="-C relocation-model=pie -C link-arg=-pie" }
+args = [
+        "build",
+        "-prustsbi-prototyper",
+        "--release",
+        "--target",
+        "riscv64imac-unknown-none-elf",
+        "-Zbuild-std=core",
+]
+env = { "RUSTFLAGS" = "-C relocation-model=pie -C link-arg=-pie" }
 
 [tasks.prototyper]
 command = "rust-objcopy"
-args = ["--binary-architecture=riscv64", "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper",
-        "--output-target=binary", "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin"
+args = [
+        "--binary-architecture=riscv64",
+        "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper",
+        "--output-target=binary",
+        "target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin",
 ]
 dependencies = ["prototyper-build"]
 
@@ -34,8 +47,11 @@ args = ["build", "-prustsbi-test-kernel", "--release"]
 
 [tasks.test-kernel]
 command = "rust-objcopy"
-args = ["--binary-architecture=riscv64", "target/riscv64imac-unknown-none-elf/release/rustsbi-test-kernel",
-        "--output-target=binary", "target/riscv64imac-unknown-none-elf/release/rustsbi-test-kernel.bin"
+args = [
+        "--binary-architecture=riscv64",
+        "target/riscv64imac-unknown-none-elf/release/rustsbi-test-kernel",
+        "--output-target=binary",
+        "target/riscv64imac-unknown-none-elf/release/rustsbi-test-kernel.bin",
 ]
 dependencies = ["test-kernel-build"]
 
@@ -48,4 +64,3 @@ rm rustsbi-test-kernel.its
 cd ../../../
 '''
 dependencies = ["prototyper", "test-kernel"]
-

+ 49 - 0
README.md

@@ -2,6 +2,55 @@
 
 RustSBI Prototyper is a developing RISC-V Secure Bootloader solution. It can be integrated with the Rust or C language ecosystem to form a complete RISC-V bootloader ecosystem.
 
+## Setting Up the Development Environment
+
+### Install Cargo Make
+
+Cargo Make is a Rust task runner and build tool, which is essential for development.
+
+```bash
+cargo install cargo-make
+```
+
+### Optional Tools
+
+The following tools are not mandatory but can be useful for enhancing your development experience.
+
+#### Install pre-commit
+
+pre-commit is a tool that runs code checks before you commit your code.
+
+```bash
+pipx install pre-commit
+
+# After installation, run pre-commit install to set it up for your project.
+pre-commit install
+```
+
+#### Install Cargo Deny
+
+Cargo deny is a Cargo plugin used to check the security of your dependencies.
+
+```bash
+cargo install --locked cargo-deny
+```
+
+#### Install typos
+
+typos is a spell-checking tool.
+
+```bash
+cargo install typos-cli
+```
+
+#### Install git cliff
+
+git cliff is a tool for generating changelogs.
+
+```bash
+cargo install git-cliff
+```
+
 ## License
 
 This project is dual-licensed under MIT or Mulan-PSL v2. See [LICENSE-MIT](./LICENSE-MIT) and [LICENSE-MULAN](./LICENSE-MULAN) for details.

+ 7 - 0
_typos.toml

@@ -0,0 +1,7 @@
+[default.extend-words]
+rela = "rela"
+sie = "sie"
+stip = "stip"
+
+[files]
+extend-exclude = ["CHANGELOG.md"]

+ 68 - 0
cliff.toml

@@ -0,0 +1,68 @@
+# git-cliff ~ configuration file
+# https://git-cliff.org/docs/configuration
+
+[changelog]
+header = """
+# Changelog\n
+All notable changes to this project will be documented in this file. See [conventional commits](https://www.conventionalcommits.org/) for commit guidelines.\n
+"""
+
+body = """
+---
+{% if version %}
+    {% if previous.version %}
+        ## [{{ version | trim_start_matches(pat="v") }}]($REPO/compare/{{ previous.version }}..{{ version }}) - {{ timestamp | date(format="%Y-%m-%d") }}
+    {% else %}
+        ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }}
+    {% endif %}
+{% else %}
+    ## [unreleased]
+{% endif %}
+{% for group, commits in commits | group_by(attribute="group") %}
+    ### {{ group | striptags | trim | upper_first }}
+    {% for commit in commits | filter(attribute="scope") | sort(attribute="scope") %}
+        - **({{commit.scope}})**{% if commit.breaking %} [**breaking**]{% endif %} {{ commit.message|trim }} - ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }})) - {{ commit.author.name }}
+    {%- endfor -%}
+    {% raw %}\n{% endraw %}
+    {%- for commit in commits %}
+        {%- if not commit.scope -%}
+            - {% if commit.breaking %} [**breaking**]{% endif %}{{ commit.message|trim }} - ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }})) - {{ commit.author.name }}
+        {%- endif -%}
+    {% endfor -%}
+{% endfor %}\n
+"""
+
+footer = "<!-- generated by git-cliff -->"
+trim = true
+postprocessors = [
+    { pattern = '\$REPO', replace = "https://github.com/rustsbi/prototyper" },
+]
+
+[git]
+conventional_commits = true
+filter_unconventional = false
+split_commits = false
+commit_preprocessors = []
+commit_parsers = [
+    { message = "\\[skip", skip = true },
+    { message = "\\p{Han}", skip = true },
+    { message = "^feat", group = "Features" },
+    { message = "^fix", group = "Bug Fixes" },
+    { message = "^doc", group = "Documentation" },
+    { message = "^perf", group = "Performance" },
+    { message = "^refactor", group = "Refactoring" },
+    { message = "^style", group = "Style" },
+    { message = "^revert", group = "Revert" },
+    { message = "^test", group = "Tests" },
+    { message = "^chore\\(version\\):", skip = true },
+    { message = "^chore", group = "Miscellaneous Chores" },
+    { message = ".*", group = "Other" },
+    { body = ".*security", group = "Security" },
+]
+protect_breaking_commits = false
+filter_commits = false
+tag_pattern = "v[0-9].*"
+skip_tags = "v0.1.0-beta.1"
+ignore_tags = ""
+topo_order = false
+sort_commits = "oldest"

+ 11 - 11
docs/booting-test-kernel-in-qemu-using-uboot-and-rustsbi.md

@@ -10,9 +10,9 @@
 
 [环境配置](#环境配置)小节给出了本教程的环境配置方法,用户在使用本教程时需要先完成环境配置小节内容。
 
-[使用U-Boot SPL启动Test Kerenl](#使用U-Boot-SPL启动Test-Kerenl)小节给出了只使用U-Boot SPL的启动流程。
+[使用U-Boot SPL启动Test Kernel](#使用U-Boot-SPL启动Test-Kernel)小节给出了只使用U-Boot SPL的启动流程。
 
-[使用U-Boot SPL和U-Boot启动Test Kerenl](#使用U-Boot-SPL和U-Boot启动Test-Kerenl)小节给出了同时使用U-Boot SPL和U-Boot的启动流程。
+[使用U-Boot SPL和U-Boot启动Test Kernel](#使用U-Boot-SPL和U-Boot启动Test-Kernel)小节给出了同时使用U-Boot SPL和U-Boot的启动流程。
 
 本教程使用软件版本如下:
 
@@ -52,7 +52,7 @@ $ riscv64-linux-gnu-gcc --version
 
 它将输出以下版本信息
 
-``` 
+```
 riscv64-linux-gnu-gcc (GCC) 14.1.0
 Copyright (C) 2024 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
@@ -66,7 +66,7 @@ $ qemu-system-riscv64 --version
 
 它将输出以下版本信息
 
-``` 
+```
 QEMU emulator version 9.0.1
 Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
 ```
@@ -91,7 +91,7 @@ Clone U-Boot
 $ git clone https://github.com/u-boot/u-boot.git && cd u-boot && git checkout v2024.04 && cd ..
 ```
 
-## 使用U-Boot SPL启动Test Kerenl
+## 使用U-Boot SPL启动Test Kernel
 ### 编译RustSBI  Prototyper和Test Kernel
 
 进入prototyper目录
@@ -121,7 +121,7 @@ $ cd u-boot
 ``` shell
 $ export ARCH=riscv
 $ export CROSS_COMPILE=riscv64-linux-gnu-
-$ export OPENSBI=../prototyper/target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin 
+$ export OPENSBI=../prototyper/target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin
 ```
 
 生成`.config`文件
@@ -155,10 +155,10 @@ $ cd workshop
 ``` shell
 $ qemu-system-riscv64 -M virt -smp 1 -m 256M -nographic \
           -bios ./u-boot/spl/u-boot-spl \
-          -device loader,file=./prototyper/target/riscv64imac-unknown-none-elf/release/rustsbi-test-kernel.itb,addr=0x80200000 
+          -device loader,file=./prototyper/target/riscv64imac-unknown-none-elf/release/rustsbi-test-kernel.itb,addr=0x80200000
 ```
 
-## 使用U-Boot SPL和U-Boot启动Test Kerenl
+## 使用U-Boot SPL和U-Boot启动Test Kernel
 ### 编译RustSBI  Prototyper和Test Kernel
 
 进入prototyper目录
@@ -188,7 +188,7 @@ $ cd u-boot
 ``` shell
 $ export ARCH=riscv
 $ export CROSS_COMPILE=riscv64-linux-gnu-
-$ export OPENSBI=../prototyper/target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin 
+$ export OPENSBI=../prototyper/target/riscv64imac-unknown-none-elf/release/rustsbi-prototyper.bin
 ```
 
 生成`.config`文件
@@ -202,7 +202,7 @@ $ make menuconfig
 
 U-Boot 配置选项将加载到终端。导航到 `Boot options` $\rightarrow$ `bootcmd value` 并将以下内容写入 `bootcmd` 值:
 
-``` 
+```
 ext4load virtio 0:1 84000000 rustsbi-test-kernel.bin; booti 0x84000000 - ${fdtcontroladdr}
 ```
 
@@ -315,4 +315,4 @@ $ qemu-system-riscv64 -M virt -smp 1 -m 256M -nographic \
           -device loader,file=./u-boot/u-boot.itb,addr=0x80200000 \
           -blockdev driver=file,filename=./test-kernel.img,node-name=hd0 \
           -device virtio-blk-device,drive=hd0
-```
+```

+ 4 - 4
prototyper/src/main.rs

@@ -49,7 +49,7 @@ extern "C" fn rust_main(_hart_id: usize, opaque: usize, nonstandard_a2: usize) {
 
         // 1. Init FDT
         // parse the device tree.
-        // TODO: shoule remove `fail:device_tree_format`.
+        // TODO: should remove `fail:device_tree_format`.
         let dtb = dt::parse_device_tree(fdt_addr).unwrap_or_else(fail::device_tree_format);
         let dtb = dtb.share();
 
@@ -104,7 +104,7 @@ extern "C" fn rust_main(_hart_id: usize, opaque: usize, nonstandard_a2: usize) {
             info!("Model: {}", model.iter().next().unwrap_or("<unspecified>"));
         }
         info!("Clint device: {}", ipi_base_address);
-        info!("Console deivce: {}", console_base_address);
+        info!("Console device: {}", console_base_address);
         info!(
             "Chosen stdout item: {}",
             tree.chosen
@@ -194,7 +194,7 @@ unsafe extern "C" fn start() -> ! {
     core::arch::asm!(
         // 1. Turn off interrupt.
         "   csrw    mie, zero",
-        // 2. Initialize programming langauge runtime.
+        // 2. Initialize programming language runtime.
         // only clear bss if hartid matches preferred boot hart id.
         "   csrr    t0, mhartid",
         "   bne     t0, zero, 4f",
@@ -216,7 +216,7 @@ unsafe extern "C" fn start() -> ! {
         "   li      t1, 1
             lla     t0, 6f
             lw      t0, 0(t0)
-            bne     t0, t1, 4b", 
+            bne     t0, t1, 4b",
         "5:",
          // 4. Prepare stack for each hart.
         "   call    {locate_stack}",

+ 1 - 0
prototyper/src/sbi/mod.rs

@@ -21,6 +21,7 @@ use rfence::SbiRFence;
 
 #[derive(RustSBI, Default)]
 #[rustsbi(dynamic)]
+#[allow(clippy::upper_case_acronyms)]
 pub struct SBI<'a, C: ConsoleDevice, I: IpiDevice, R: ResetDevice> {
     #[rustsbi(console)]
     pub console: Option<SbiConsole<'a, C>>,

+ 1 - 6
prototyper/src/sbi/rfence.rs

@@ -182,12 +182,7 @@ fn validate_address_range(start_addr: usize, size: usize) -> Result<usize, SbiRe
         return Err(SbiRet::invalid_address());
     }
 
-    let end_addr = start_addr + size;
-    if end_addr > usize::MAX {
-        Ok(usize::MAX)
-    } else {
-        Ok(size)
-    }
+    Ok(size)
 }
 
 /// Processes a remote fence operation by sending IPI to target harts.

+ 3 - 3
prototyper/src/sbi/trap.rs

@@ -185,7 +185,7 @@ pub unsafe extern "C" fn msoft() -> ! {
         "sd     t2, 1*8(sp)",
         // Call handler with context pointer
         "mv     a0, sp",
-        "call   {msoft_hanlder}",
+        "call   {msoft_handler}",
         // Restore mepc
         "ld     t0, 31*8(sp)
         csrw    mepc, t0",
@@ -226,7 +226,7 @@ pub unsafe extern "C" fn msoft() -> ! {
         "csrrw  sp, mscratch, sp",
         // Return from machine mode
         "mret",
-        msoft_hanlder = sym msoft_hanlder,
+        msoft_handler = sym msoft_handler,
         options(noreturn)
     );
 }
@@ -234,7 +234,7 @@ pub unsafe extern "C" fn msoft() -> ! {
 /// Machine software interrupt handler implementation.
 ///
 /// Handles HSM (Hart State Management) and RFence operations.
-pub extern "C" fn msoft_hanlder(ctx: &mut SupervisorContext) {
+pub extern "C" fn msoft_handler(ctx: &mut SupervisorContext) {
     #[inline(always)]
     fn boot(ctx: &mut SupervisorContext, start_addr: usize, opaque: usize) {
         unsafe {

+ 1 - 1
supervisor/src/main.rs

@@ -36,7 +36,7 @@ unsafe extern "C" fn start() -> ! {
     asm!(
         // 1. Turn off interrupt
         "   csrw    sie, zero",
-        // 2. Initialize programming langauge runtime
+        // 2. Initialize programming language runtime
         // only initialize if it is boot hart (hart ID 0)
         "   bnez    a0, 3f",
         // clear bss segment