Pārlūkot izejas kodu

添加初始代码 (#1)

LoGin 5 mēneši atpakaļ
vecāks
revīzija
abdb84b821

+ 6 - 0
.cargo/config

@@ -0,0 +1,6 @@
+[unstable]
+build-std = ["core", "compiler_builtins", "alloc"]
+build-std-features = ["compiler-builtins-mem"]
+
+[build]
+target = "riscv64imac-unknown-none-elf"

+ 40 - 0
.github/workflows/cache-toolchain.yml

@@ -0,0 +1,40 @@
+name: Reusable workflow example
+
+on: workflow_call
+
+jobs:
+    build:
+
+        runs-on: ubuntu-latest
+
+        steps:
+        - uses: actions/checkout@v3
+
+
+        - name: Cache build tools
+          id: cache-build-tools
+          uses: actions/cache@v3
+          env:
+              cache-name: dragon-boot-cache-build-tools
+          with:
+            path: |
+              ~/.cargo
+              ~/.rustup
+              ~/.bashrc
+            key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}
+
+        - if: ${{ steps.cache-build-tools.outputs.cache-hit != 'true' }}
+          name: Install toolchain
+          continue-on-error: true
+          run:  |
+            sudo sh -c "apt update && apt install -y llvm-dev libclang-dev clang gcc-multilib libssl-dev pkg-config"
+            cargo install cargo-binutils
+            rustup toolchain install nightly
+            rustup default nightly
+            rustup component add rust-src
+            rustup component add llvm-tools-preview
+            rustup target add x86_64-unknown-none
+            rustup component add rust-src --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
+            rustup target add riscv64gc-unknown-none-elf --toolchain nightly-2023-08-15-riscv64gc-unknown-linux-gnu
+            rustup component add rustfmt --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
+

+ 38 - 0
.github/workflows/riscv64-build.yml

@@ -0,0 +1,38 @@
+name: Riscv64 Build Check
+
+on:
+  push:
+    branches: [ "master" ]
+  pull_request:
+    branches: [ "master" ]
+
+jobs:
+  # ensure the toolchain is cached
+  ensure-toolchain:
+    uses: ./.github/workflows/cache-toolchain.yml
+  
+  build:
+
+    runs-on: ubuntu-latest
+    needs: [ensure-toolchain]
+
+    steps:
+    - uses: actions/checkout@v3
+
+    
+    - name: Cache build tools
+      id: dragon-boot-cache-build-tools
+      uses: actions/cache@v3
+      env:
+          cache-name: dragon-boot-cache-build-tools
+      with:
+        path: |
+          ~/.cargo
+          ~/.rustup
+          ~/.bashrc
+        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}
+      
+    
+    - name: Riscv64 Build Check
+      run: |
+            ARCH=riscv64 make

+ 30 - 0
.github/workflows/rustfmt.yml

@@ -0,0 +1,30 @@
+name: Rust format check
+
+on: [push, pull_request]
+
+jobs:
+    # ensure the toolchain is cached
+    ensure-toolchain:
+        uses: ./.github/workflows/cache-toolchain.yml
+  
+    fmt:
+        name: check
+        runs-on: ubuntu-latest
+        needs: [ensure-toolchain]
+        steps:
+            - uses: actions/checkout@v3
+            - name: Cache build tools
+              id: dragon-boot-cache-build-tools
+              uses: actions/cache@v3
+              env:
+                  cache-name: dragon-boot-cache-build-tools
+              with:
+                path: |
+                  ~/.cargo
+                  ~/.rustup
+                  ~/.bashrc
+                key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('.github/workflows/cache-toolchain.yml') }}
+    
+            - name: Check format
+              run: |
+                    FMT_CHECK=1 make fmt

+ 5 - 0
.gitignore

@@ -12,3 +12,8 @@ Cargo.lock
 
 # MSVC Windows builds of rustc generate these, which store debugging information
 *.pdb
+
+
+# Added by cargo
+
+/target

+ 4 - 0
.vscode/settings.json

@@ -0,0 +1,4 @@
+{
+    "rust-analyzer.cargo.target": "riscv64imac-unknown-none-elf",
+    "rust-analyzer.checkOnSave.allTargets": false,
+}

+ 13 - 0
Cargo.toml

@@ -0,0 +1,13 @@
+[package]
+name = "dragon_boot"
+version = "0.1.0"
+edition = "2021"
+build = "build.rs"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+
+
+[build-dependencies]
+cc = { version = "1.0.83", features = ["parallel"] }

+ 31 - 0
Makefile

@@ -0,0 +1,31 @@
+.PHONY: all clean
+
+# 检查是否需要进行fmt --check
+# 解析命令行参数  
+FMT_CHECK?=0
+
+ifeq ($(FMT_CHECK), 1)
+	FMT_CHECK=--check
+else
+	FMT_CHECK=
+endif
+
+export ARCH ?= riscv64
+
+all:
+ifeq ($(ARCH), riscv64)
+	$(MAKE) riscv64imac
+else
+	@echo "ARCH=$(ARCH) is not supported"
+	@exit 1
+endif
+
+riscv64imac:
+	@cargo build --release --target riscv64imac-unknown-none-elf
+
+clean:
+	@cargo clean
+
+
+fmt:
+	@cargo fmt --all $(FMT_CHECK)

+ 29 - 0
README.md

@@ -0,0 +1,29 @@
+# DragonBoot
+
+A stage2 UEFI bootloader of DragonOS in pure Rust.
+
+--- 
+
+## 功能
+
+- [ ] 从UEFI启动DragonBoot
+- [ ] 显示启动菜单
+- [ ] 从磁盘启动DragonOS
+- [ ] 启动配置
+- [ ] 平坦设备树解析
+
+## 目标架构
+
+- [x] riscv64
+
+## 关于DragonBoot
+
+由于目前Risc-V上,许多操作系统都是要把DTB编译进内核,导致操作系统无法作为一个与开发板无关的二进制文件进行传播,因此DragonBoot的目标是,作为一个第二阶段的引导加载程序,加载DragonOS内核,并把uboot传来的平坦设备树传递给内核。
+
+## Maintainer
+
+- longjin <longjin@dragonos.org>
+
+## License
+
+DragonBoot is licensed under the GPLv2 License. See [LICENSE](LICENSE) for details.

+ 1 - 0
build.rs

@@ -0,0 +1 @@
+fn main() {}

+ 3 - 0
rust-toolchain.toml

@@ -0,0 +1,3 @@
+[toolchain]
+channel = "nightly-2023-08-15"
+components = ["rust-src"]

+ 1 - 0
src/arch/mod.rs

@@ -0,0 +1 @@
+mod riscv64;

+ 168 - 0
src/arch/riscv64/crt0-efi-riscv64.S

@@ -0,0 +1,168 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Mitchell Horne <mhorne@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+	.section	.text.head
+
+	/*
+	 * Magic "MZ" signature for PE/COFF
+	 */
+	.globl	ImageBase
+ImageBase:
+	.ascii	"MZ"
+	.skip	58				// 'MZ' + pad + offset == 64
+	.long	pe_header - ImageBase		// Offset to the PE header.
+pe_header:
+	.ascii	"PE"
+	.short 	0
+coff_header:
+	.short	0x5064			// RISCV64
+	.short	2				// nr_sections
+	.long	0 				// TimeDateStamp
+	.long	0				// PointerToSymbolTable
+	.long	0				// NumberOfSymbols
+	.short	section_table - optional_header	// SizeOfOptionalHeader
+	.short	0x20e			// Characteristics.
+							// IMAGE_FILE_DEBUG_STRIPPED |
+							// IMAGE_FILE_EXECUTABLE_IMAGE |
+							// IMAGE_FILE_LOCAL_SYMS_STRIPPED |
+							// IMAGE_FILE_LINE_NUMS_STRIPPED
+optional_header:
+	.short	0x20b				// PE32+ format
+	.byte	0x02				// MajorLinkerVersion
+	.byte	0x14				// MinorLinkerVersion
+	.long	_edata - _start			// SizeOfCode
+	.long	0				// SizeOfInitializedData
+	.long	0				// SizeOfUninitializedData
+	.long	_start - ImageBase		// AddressOfEntryPoint
+	.long	_start - ImageBase		// BaseOfCode
+
+extra_header_fields:
+	.quad	0				// ImageBase
+	.long	32				// SectionAlignment
+	.long	8				// FileAlignment
+
+	.short	0				// MajorOperatingSystemVersion
+	.short	0				// MinorOperatingSystemVersion
+	.short	0				// MajorImageVersion
+	.short	0				// MinorImageVersion
+	.short	0				// MajorSubsystemVersion
+	.short	0				// MinorSubsystemVersion
+	.long	0				// Win32VersionValue
+
+	.long	_edata - ImageBase		// SizeOfImage
+
+	// Everything before the kernel image is considered part of the header
+	.long	_start - ImageBase		// SizeOfHeaders
+	.long	0				// CheckSum
+	.short	10				// Subsystem (EFI)
+	.short	0				// DllCharacteristics
+	.quad	0				// SizeOfStackReserve
+	.quad	0				// SizeOfStackCommit
+	.quad	0				// SizeOfHeapReserve
+	.quad	0				// SizeOfHeapCommit
+	.long	0				// LoaderFlags
+	.long	16				// NumberOfRvaAndSizes
+
+	.quad	0				// ExportTable
+	.quad	0				// ImportTable
+	.quad	0				// ResourceTable
+	.quad	0				// ExceptionTable
+	.quad	0				// CertificationTable
+	.quad	0				// BaseRelocationTable
+	.quad	0				// Debug
+	.quad	0				// Architecture
+	.quad	0				// Global Ptr
+	.quad	0				// TLS Table
+	.quad	0				// Load Config Table
+	.quad	0				// Bound Import
+	.quad	0				// IAT
+	.quad	0				// Delay Import Descriptor
+	.quad	0				// CLR Runtime Header
+	.quad	0				// Reserved
+
+	// Section table
+section_table:
+
+	/*
+	 * The EFI application loader requires a relocation section
+	 * because EFI applications must be relocatable.  This is a
+	 * dummy section as far as we are concerned.
+	 */
+	.ascii	".reloc"
+	.byte	0
+	.byte	0			// end of 0 padding of section name
+	.long	0
+	.long	0
+	.long	0			// SizeOfRawData
+	.long	0			// PointerToRawData
+	.long	0			// PointerToRelocations
+	.long	0			// PointerToLineNumbers
+	.short	0			// NumberOfRelocations
+	.short	0			// NumberOfLineNumbers
+	.long	0x42100040		// Characteristics (section flags)
+
+
+	.ascii	".text"
+	.byte	0
+	.byte	0
+	.byte	0        		// end of 0 padding of section name
+	.long	_edata - _start		// VirtualSize
+	.long	_start - ImageBase	// VirtualAddress
+	.long	_edata - _start		// SizeOfRawData
+	.long	_start - ImageBase	// PointerToRawData
+
+	.long	0		// PointerToRelocations (0 for executables)
+	.long	0		// PointerToLineNumbers (0 for executables)
+	.short	0		// NumberOfRelocations  (0 for executables)
+	.short	0		// NumberOfLineNumbers  (0 for executables)
+	.long	0xe0500020	// Characteristics (section flags)
+
+	.globl _start
+_start:
+	/* Save boot parameters to the stack */
+	addi		sp, sp, -24
+	sd			a0, 0(sp)
+	sd			a1, 8(sp)
+	sd			ra, 16(sp)
+
+	/* Run relocation */
+	lla			a0, ImageBase
+	lla			a1, _DYNAMIC
+	call		_relocate
+	bne			a0, zero, 0f
+
+	/* Call EFI code */
+	ld			a1, 8(sp)
+	ld			a0, 0(sp)
+	call		efi_main
+
+	ld			ra, 16(sp)
+
+0:	addi		sp, sp, 24
+	ret

+ 1 - 0
src/arch/riscv64/mod.rs

@@ -0,0 +1 @@
+

+ 2 - 0
src/lib.rs

@@ -0,0 +1,2 @@
+#![no_std]
+#![no_main]

+ 21 - 0
src/main.rs

@@ -0,0 +1,21 @@
+#![no_std]
+#![no_main]
+
+mod arch;
+
+#[no_mangle]
+fn efi_main() {
+    // println!("Hello, world!");
+    loop {}
+}
+
+#[no_mangle]
+fn _relocate() {
+    loop {}
+}
+
+#[panic_handler]
+fn panic_handler(_info: &core::panic::PanicInfo) -> ! {
+    // println!("{}", info);
+    loop {}
+}