aboutsummaryrefslogtreecommitdiff
path: root/vm.sh
blob: 04f702797f06560bb9bd433b13b59b683bce35be (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
#!/bin/sh

set -xe

function run
{
    # 32 bit does NOT work, even if the kernel is obviously compiled
    # for 32 bit, the binary could be broken, but I didn't manage to
    # build qemu from source,
    # pretty sure that riscv32-unknown-linux-gnu- works fine
    qemu-system-riscv64 \
        -machine virt \
		-kernel "$BIN/$KERNEL" \
		-initrd "$BIN/$INITFS" \
	    -append "console=ttyS0" -nographic \
        -monitor none \
        -serial stdio
}

function initramfs
{
    IMG="$(pwd)/$BIN/$INITFS"
    DIR="${IMG%.*}"
    mkdir -p $DIR
    
    # copy the program
    cp $1 "$DIR/init"

    # make image and delete folder
    (cd $DIR; find . | cpio -H newc -o | gzip > $IMG)
    rm -rf $DIR
}

function kernel
{   
    DIR="${KERNEL%.*}"
    ARCHIVE="$DIR.tar.xz"
    
    BACKUP="backup"
    mkdir -p $BACKUP

    if [[ -n $(find backup/ -type f -name "$KERNEL") ]]; then
        cp $BACKUP/$KERNEL $BIN/
        exit 0
    fi

    if [[ -n $(find backup/ -type f -name "$ARCHIVE") ]]; then
        cp $BACKUP/$ARCHIVE $BIN/
    else
        wget -P $BIN https://cdn.kernel.org/pub/linux/kernel/v5.x/$ARCHIVE    
        cp $BIN/$ARCHIVE $BACKUP/
    fi
    
    tar -xf $BIN/$ARCHIVE -C $BIN/
    rm $BIN/$ARCHIVE
    
    # TOOLCHAIN in the Makefile
    cd $BIN/$DIR
    make ARCH=riscv CROSS_COMPILE=$TOOLCHAIN defconfig
    make ARCH=riscv CROSS_COMPILE=$TOOLCHAIN -j $(nproc)
    cd ../../
    
    cp $BIN/$DIR/arch/riscv/boot/Image $BIN/$KERNEL
    cp $BIN/$KERNEL $BACKUP
    
    rm -rf $BIN/$DIR
}

$1 $2