From 7180a8d572064a1cf08a954fba010d910b138b20 Mon Sep 17 00:00:00 2001 From: Zongyao Date: Tue, 26 Nov 2024 02:44:07 +0000 Subject: [PATCH] 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 solar1s@163.com --- .pre-commit-config.yaml | 47 +++++++++++++ CHANGELOG.md | 6 ++ Makefile.toml | 33 ++++++--- README.md | 49 +++++++++++++ _typos.toml | 7 ++ cliff.toml | 68 +++++++++++++++++++ ...-kernel-in-qemu-using-uboot-and-rustsbi.md | 22 +++--- prototyper/src/main.rs | 8 +-- prototyper/src/sbi/mod.rs | 1 + prototyper/src/sbi/rfence.rs | 7 +- prototyper/src/sbi/trap.rs | 6 +- supervisor/src/main.rs | 2 +- 12 files changed, 222 insertions(+), 34 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 CHANGELOG.md create mode 100644 _typos.toml create mode 100644 cliff.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..8f8fb49 --- /dev/null +++ b/.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 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6ee1e74 --- /dev/null +++ b/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] diff --git a/Makefile.toml b/Makefile.toml index 44692ea..7800c51 100644 --- a/Makefile.toml +++ b/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"] - diff --git a/README.md b/README.md index c3fdf88..4414534 100644 --- a/README.md +++ b/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. diff --git a/_typos.toml b/_typos.toml new file mode 100644 index 0000000..0eb49b0 --- /dev/null +++ b/_typos.toml @@ -0,0 +1,7 @@ +[default.extend-words] +rela = "rela" +sie = "sie" +stip = "stip" + +[files] +extend-exclude = ["CHANGELOG.md"] diff --git a/cliff.toml b/cliff.toml new file mode 100644 index 0000000..0711445 --- /dev/null +++ b/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 = "" +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" diff --git a/docs/booting-test-kernel-in-qemu-using-uboot-and-rustsbi.md b/docs/booting-test-kernel-in-qemu-using-uboot-and-rustsbi.md index 0b0ecb0..7ccb53f 100644 --- a/docs/booting-test-kernel-in-qemu-using-uboot-and-rustsbi.md +++ b/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 -``` \ No newline at end of file +``` diff --git a/prototyper/src/main.rs b/prototyper/src/main.rs index 49ab214..cab8ea0 100644 --- a/prototyper/src/main.rs +++ b/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("")); } 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}", diff --git a/prototyper/src/sbi/mod.rs b/prototyper/src/sbi/mod.rs index 1c5dff5..35e287a 100644 --- a/prototyper/src/sbi/mod.rs +++ b/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>, diff --git a/prototyper/src/sbi/rfence.rs b/prototyper/src/sbi/rfence.rs index 7d12c45..0e4ca9d 100644 --- a/prototyper/src/sbi/rfence.rs +++ b/prototyper/src/sbi/rfence.rs @@ -182,12 +182,7 @@ fn validate_address_range(start_addr: usize, size: usize) -> Result usize::MAX { - Ok(usize::MAX) - } else { - Ok(size) - } + Ok(size) } /// Processes a remote fence operation by sending IPI to target harts. diff --git a/prototyper/src/sbi/trap.rs b/prototyper/src/sbi/trap.rs index 216b21a..9f2676d 100644 --- a/prototyper/src/sbi/trap.rs +++ b/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 { diff --git a/supervisor/src/main.rs b/supervisor/src/main.rs index 1381cc1..848d3ca 100644 --- a/supervisor/src/main.rs +++ b/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