1# Copyright 2021 Nikita Melekhin. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import sys
6import os
7QEMU_PATH_VAR = "qemu_exec"
8QEMU_PATH_ENV_VAR = ""
9QEMU_SMP_VAR = "qemu_smp"
10QEMU_SMP_ENV_VAR = "ONEOS_QEMU_SMP"
11QEMU_STD_PATH = ""
12qemu_run_cmd = ""
13arch = sys.argv[1]
14base = sys.argv[2]
15out = sys.argv[3]
16
17if arch == "x86":
18 QEMU_PATH_ENV_VAR = "ONEOS_QEMU_X86"
19 QEMU_STD_PATH = "qemu-system-i386"
20 qemu_run_cmd = "${2} -m 256M --drive file={1}/os-image.bin,format=raw,index=0,if=floppy -device piix3-ide,id=ide -drive id=disk,format=raw,file={1}/one.img,if=none -device ide-hd,drive=disk,bus=ide.0 -serial mon:stdio -rtc base=utc -vga std".format(
21 base, out, QEMU_PATH_VAR)
22if arch == "aarch32":
23 QEMU_PATH_ENV_VAR = "ONEOS_QEMU_ARM"
24 QEMU_STD_PATH = "qemu-system-arm"
25 qemu_run_cmd = "${2} -M vexpress-a15 -cpu cortex-a15 -kernel {1}/bootarm.bin -smp ${3} -serial mon:stdio -vga std -drive id=disk,if=sd,format=raw,file={1}/one.img".format(
26 base, out, QEMU_PATH_VAR, QEMU_SMP_VAR)
27
28if base[-1] == '/':
29 base = base[:-1]
30
31if out[-1] == '/':
32 out = out[:-1]
33
34env_var_checker = """
35{2}="{3}"
36{4}=1
37[[ -z "${1}" ]] && {2}='{3}' || {2}="${1}"
38[[ -z "${5}" ]] && {4}=1 || {4}="${5}"
39""".format("", QEMU_PATH_ENV_VAR, QEMU_PATH_VAR, QEMU_STD_PATH, QEMU_SMP_VAR, QEMU_SMP_ENV_VAR)
40
41sync = open("{0}/sync.sh".format(out), "w")
42sync.write(
43 """#!/bin/bash
44GREEN='\\033[0;32m'
45RED='\\033[0;31m'
46NC='\\033[0m'
47ERROR="${{RED}}[ERROR]${{NC}}"
48SUCCESS="${{GREEN}}[SUCCESS]${{NC}}"
49
50mkdir -p {1}/mountpoint
51sudo fuse-ext2 {1}/one.img {1}/mountpoint -o rw+
52if [ $? -ne 0 ]; then echo -e "${{ERROR}} Can't mount one.img to {1}/mountpoint" && exit 1; fi
53sudo mkdir -p {1}/mountpoint/boot
54sudo mkdir -p {1}/mountpoint/proc
55sudo mkdir -p {1}/mountpoint/var
56sudo mkdir -p {1}/mountpoint/dev
57sudo mkdir -p {1}/mountpoint/tmp
58sudo cp -r {0}/base/* {1}/mountpoint/
59sudo cp -r {1}/base/* {1}/mountpoint/
60
61sudo chmod -R 644 {1}/mountpoint/proc
62sudo chmod -R 644 {1}/mountpoint/dev
63sudo chmod -R 666 {1}/mountpoint/tmp
64sudo chmod -R 666 {1}/mountpoint/var
65sudo chmod -R 755 {1}/mountpoint/bin
66sudo chmod -R 700 {1}/mountpoint/home
67sudo chmod 777 {1}/mountpoint/home
68sudo chmod -R 755 {1}/mountpoint/System
69sudo chmod -R 755 {1}/mountpoint/Applications
70
71sudo chown -R 0 {1}/mountpoint/home/root
72sudo chown -R 0 {1}/mountpoint/bin/sudo
73sudo chmod 4755 {1}/mountpoint/bin/sudo
74
75sudo chown -R 10 {1}/mountpoint/home/user
76
77sudo umount {1}/mountpoint
78if [ $? -ne 0 ]; then echo -e "${{ERROR}} Can't umount {1}/mountpoint" && exit 1; fi
79echo -e "${{SUCCESS}} Sync"
80""".format(base, out))
81sync.close()
82
83
84build = open("{0}/build.sh".format(out), "w")
85build.write(
86 """#!/bin/bash
87GREEN='\\033[0;32m'
88RED='\\033[0;31m'
89NC='\\033[0m'
90ERROR="${{RED}}[ERROR]${{NC}}"
91SUCCESS="${{GREEN}}[SUCCESS]${{NC}}"
92
93ninja
94if [ $? -ne 0 ]; then echo -e "${{ERROR}} Can't build for arch: {0}" && exit 1; fi
95echo -e "${{SUCCESS}} Build for arch: {0}"
96""".format(arch))
97build.close()
98
99run = open("{0}/run.sh".format(out), "w")
100run.write(
101 """#!/bin/bash
102{1}
103{0}
104if [ $? -ne 0 ]; then echo -e "${{ERROR}} Run command failed" && exit 1; fi""".format(qemu_run_cmd, env_var_checker)
105)
106run.close()
107
108debug = open("{0}/debug.sh".format(out), "w")
109debug.write(
110 """#!/bin/bash
111{1}
112{0} -s -S
113if [ $? -ne 0 ]; then echo -e "${{ERROR}} Debug Run command failed" && exit 1; fi""".format(qemu_run_cmd, env_var_checker)
114)
115debug.close()
116
117allf = open("{0}/all.sh".format(out), "w")
118allf.write(
119 """#!/bin/bash
120GREEN='\\033[0;32m'
121RED='\\033[0;31m'
122NC='\\033[0m'
123ERROR="${RED}[ERROR]${NC}"
124SUCCESS="${GREEN}[SUCCESS]${NC}"
125
126./build.sh
127if [ $? -ne 0 ]; then echo -e "${ERROR} All command failed" && exit 1; fi
128./sync.sh
129if [ $? -ne 0 ]; then echo -e "${ERROR} All command failed" && exit 1; fi
130./run.sh
131if [ $? -ne 0 ]; then echo -e "${ERROR} All command failed" && exit 1; fi
132""")
133allf.close()
134
135allf = open("{0}/run_tester.sh".format(out), "w")
136allf.write(
137 """#!/bin/bash
138GREEN='\\033[0;32m'
139RED='\\033[0;31m'
140NC='\\033[0m'
141ERROR="${{RED}}[ERROR]${{NC}}"
142SUCCESS="${{GREEN}}[SUCCESS]${{NC}}"
143
144./build.sh
145if [ $? -ne 0 ]; then echo -e "${{ERROR}} All command failed" && exit 1; fi
146./sync.sh
147if [ $? -ne 0 ]; then echo -e "${{ERROR}} All command failed" && exit 1; fi
148{1}
149{0} --nographic
150if [ $? -ne 0 ]; then echo -e "${{ERROR}} All command failed" && exit 1; fi
151""".format(qemu_run_cmd, env_var_checker))
152allf.close()
153
154allf = open("{0}/dll.sh".format(out), "w")
155allf.write(
156 """#!/bin/bash
157GREEN='\\033[0;32m'
158RED='\\033[0;31m'
159NC='\\033[0m'
160ERROR="${RED}[ERROR]${NC}"
161SUCCESS="${GREEN}[SUCCESS]${NC}"
162
163./build.sh
164if [ $? -ne 0 ]; then echo -e "${ERROR} Debug All command failed" && exit 1; fi
165./sync.sh
166if [ $? -ne 0 ]; then echo -e "${ERROR} Debug All command failed" && exit 1; fi
167./debug.sh
168if [ $? -ne 0 ]; then echo -e "${ERROR} Debug All command failed" && exit 1; fi
169""")
170allf.close()
171
172gdbinit = open("{0}/.gdbinit".format(out), "w")
173gdbinit.write(
174 """file {0}/base/boot/kernel.bin
175target remote :1234""".format(out))
176gdbinit.close()