summaryrefslogtreecommitdiff
path: root/kernel/build.sh
blob: 530c253c944d7a5abf63d3dd84302baa6ab08358 (plain)
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
95
#!/usr/bin/sh

ASM_PATH="src/"
TARGET_PATH="target/"
TMP_PATH="$TARGET_PATH/temp/"
OBJ_PATH="$TMP_PATH"
LINK_SCRIPT_PATH="src/"
GRUB_CFG_PATH="src/grub.cfg"

remove_target() {
    rm -rf $TARGET_PATH &> /dev/null
}

create_target() {
    mkdir $TARGET_PATH &> /dev/null
    mkdir $TMP_PATH &> /dev/null
}

cleanup_temp() {
    rm -f $TMP_PATH/* &> /dev/null
}

# ---------------------------------------------------

build_kernel_elf() {
    create_target
    nasm -felf64 $ASM_PATH/multiboot_header.asm -o $OBJ_PATH/multiboot_header.o
    nasm -felf64 $ASM_PATH/boot.asm -o $OBJ_PATH/boot.o
    ld -n -o $TARGET_PATH/kernel.bin -T $LINK_SCRIPT_PATH/linker.ld $OBJ_PATH/*.o
    cleanup_temp
}

build_kernel_iso_from_elf() {
    create_target
    mkdir $TMP_PATH/isofiles &> /dev/null
    mkdir $TMP_PATH/isofiles/boot &> /dev/null
    mkdir $TMP_PATH/isofiles/boot/grub &> /dev/null
    cp $TARGET_PATH/kernel.bin $TMP_PATH/isofiles/boot &> /dev/null
    cp $GRUB_CFG_PATH $TMP_PATH/isofiles/boot/grub/grub.cfg &> /dev/null
    grub-mkrescue -d /usr/lib/grub/i386-pc -o $TARGET_PATH/uff.iso $TMP_PATH/isofiles
}

build_kernel_iso() {
    build_kernel_elf
    build_kernel_iso_from_elf
}

build_init() {
    create_target
}

build_clean() {
    remove_target
}

build_all() {
    build_kernel_elf
}

not_exec=true

for arg in "$@"; do
    case "$arg" in
        "kernel-elf") build_kernel_elf;;
        "kernel-iso-from-elf") build_kernel_iso_from_elf;;
        "kernel-iso") build_kernel_iso;;
        "init") build_init;;
        "clean") build_clean;;
        "all") build_all;;
        *)
            echo "warn: ignoring unknown option '$arg'"
            continue;;
    esac
    not_exec=false
    break
done

print_help() {
    echo "usage: $0 option"
    echo "options: "
    echo "  init                      initialise directory"
    echo "  clean                     cleanup binaries"
    echo
    echo "  kernel-elf                build the kernel-elf"
    echo "  kernel-iso-from-elf       build the kernel-iso from an existing kernel-elf"
    echo "  kernel-iso                build the kernel-elf and the kernel-iso"
    echo "  all                       build kernel"
}

if $not_exec; then
    echo "error: please specify an argument"
    echo
    print_help
    exit -1
fi