forked from Dentosal/rust_os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·94 lines (71 loc) · 3.2 KB
/
build.sh
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
set -e
# set -x # turn on command printing
# config
TARGET="rust_os"
# some constants
PATH=$PATH:$HOME/.cargo/bin
# prepare for build
mkdir -p build
echo "Compiling source files..."
echo "* bootloader"
# compile bootloader
nasm src/boot/stage0.asm -f bin -o build/stage0.bin
nasm src/boot/stage1.asm -f bin -o build/stage1.bin
nasm src/boot/stage2.asm -f bin -o build/stage2.bin
echo "* kernel entry point"
# compile kernel entry point
nasm -f elf64 src/entry.asm -o build/entry.o
echo "* kernel"
# compile kernel (with full optimizations)
# RUST_TARGET_PATH=$(pwd) xargo build --target $TARGET --release
RUSTFLAGS=-g RUST_TARGET_PATH=$(pwd) xargo rustc --target $TARGET --release -- -C opt-level=s
echo "* kernel assembly routines"
mkdir -p build/asm_routines/
for fpath in src/asm_routines/*.asm
do
filename=$(basename "$fpath") # remove path
base="${filename%.*}" # get basename
nasm -f elf64 "$fpath" -o "build/asm_routines/$base.o"
done
echo "* D7_StaticFS cli tools"
( cd libs/d7staticfs/ && cargo build --release )
echo "Linking objects..."
# link (use --print-gc-sections to debug)
# ld -z max-page-size=0x1000 -T build_config/linker.ld -o build/kernel.bin build/entry.o target/$TARGET/release/librust_os.a build/asm_routines/*.o
# ld -z max-page-size=0x1000 --unresolved-symbols=ignore-all -T build_config/linker.ld -o build/kernel.bin build/entry.o target/$TARGET/release/librust_os.a build/asm_routines/*.o
# ld -z max-page-size=0x1000 --gc-sections --print-gc-sections -T build_config/linker.ld -o build/kernel.bin build/entry.o target/$TARGET/release/librust_os.a build/asm_routines/*.o
ld -z max-page-size=0x1000 --gc-sections -T build_config/linker.ld -o build/kernel.bin build/entry.o target/$TARGET/release/librust_os.a build/asm_routines/*.o
cp build/kernel.bin build/kernel_unstripped.bin
strip build/kernel.bin
echo "Cheking boundries..."
# image size check
imgsize=$(wc -c build/kernel.bin | xargs -n 1 2>/dev/null | head -n 1)
echo "imgsize: $imgsize"
maxsize=400 # size in blocks
toobig=$(python3 -c "print(int($imgsize//0x200>$maxsize))")
if [ $toobig -eq 1 ]
then
echo "Kernel image seems to be too large."
exit 1
fi
echo "Creating disk image..."
DISK_SIZE_BYTES=$(python3 -c 'print(0x200*0x800)') # a disk of 0x800=2048 0x200-byte sectors, 2**20 bytes, one mebibyte
DISK_SIZE_SECTORS=$(python3 -c "print($DISK_SIZE_BYTES // 0x200)")
# create disk
echo "* create disk"
dd "if=/dev/zero" "of=build/disk.img" "bs=512" "count=$DISK_SIZE_SECTORS" "conv=notrunc"
echo "* copy boot stages"
dd "if=build/stage0.bin" "of=build/disk.img" "bs=512" "seek=0" "count=1" "conv=notrunc"
dd "if=build/stage1.bin" "of=build/disk.img" "bs=512" "seek=1" "count=1" "conv=notrunc"
dd "if=build/stage2.bin" "of=build/disk.img" "bs=512" "seek=2" "count=1" "conv=notrunc"
echo "* copy kernel"
dd "if=build/kernel.bin" "of=build/disk.img" "bs=512" "seek=3" "conv=notrunc"
echo "* write filesystem"
./libs/d7staticfs/target/release/mkimg build/disk.img $(($imgsize/0x200+4)) $(< build_config/staticfs_files.txt)
echo "Saving objdump..."
objdump -CShdr -M intel build/kernel_unstripped.bin > objdump.txt
echo "Saving readelf..."
readelf -e build/kernel_unstripped.bin > readelf.txt
# TODO? clean?
echo "Done"