init_script_riscv64.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/mnt/busybox sh
  2. BUSYBOX=/mnt/busybox
  3. TERMINAL=/dev/ttyS0
  4. VERBOSE=
  5. error() {
  6. printf "\033[91merror: \033[0m%s\n" "$1" >&2
  7. }
  8. warn() {
  9. printf "\033[93mwarn : \033[0m%s\n" "$1" >&2
  10. }
  11. info() {
  12. printf "\033[92minfo : \033[0m%s\n" "$1" >&2
  13. }
  14. die() {
  15. error "$1" && freeze
  16. }
  17. freeze() {
  18. info "freezing..." >&2
  19. while true; do
  20. :
  21. done
  22. exit 1
  23. }
  24. unrecoverable() {
  25. die "unrecoverable error occurred. check the message above."
  26. }
  27. busybox() {
  28. $BUSYBOX "$@"
  29. }
  30. trap unrecoverable EXIT
  31. set -euo pipefail
  32. if [ -n "$VERBOSE" ]; then
  33. set -x
  34. fi
  35. busybox mkdir -p /dev
  36. busybox mknod -m 666 /dev/console c 5 1
  37. busybox mknod -m 666 /dev/null c 1 3
  38. busybox mknod -m 666 /dev/zero c 1 5
  39. busybox mknod -m 666 /dev/vda b 8 0
  40. busybox mknod -m 666 /dev/vda1 b 8 1
  41. busybox mknod -m 666 /dev/vdb b 8 16
  42. busybox mknod -m 666 /dev/ttyS0 c 4 64
  43. busybox mknod -m 666 /dev/ttyS1 c 4 65
  44. exec < "$TERMINAL"
  45. exec > "$TERMINAL" 2>&1
  46. info "deploying busybox..."
  47. busybox mkdir -p /bin /lib
  48. busybox --install -s /bin
  49. info "done"
  50. export PATH="/bin"
  51. mkdir -p /etc /root /proc
  52. mount -t procfs proc proc
  53. cat > /etc/passwd <<EOF
  54. root:x:0:0:root:/root:/bin/sh
  55. EOF
  56. cat > /etc/group <<EOF
  57. root:x:0:root
  58. EOF
  59. cat > /etc/profile <<EOF
  60. export PATH=/bin
  61. EOF
  62. cat > /root/.profile <<EOF
  63. export HOME=/root
  64. alias ll="ls -l "
  65. alias la="ls -la "
  66. EOF
  67. cat > /root/test.c <<EOF
  68. #include <stdio.h>
  69. int main() {
  70. int var = 0;
  71. printf("Hello, world!\n");
  72. printf("Please input a number: \n");
  73. scanf("%d", &var);
  74. if (var > 0) {
  75. printf("You typed a positive number.\n");
  76. } else if (var == 0 ) {
  77. printf("You input a zero.\n");
  78. } else {
  79. printf("You typed a negative number.\n");
  80. }
  81. return 0;
  82. }
  83. EOF
  84. exec sh -l
  85. # We don't have a working init yet, so we use busybox sh directly for now.
  86. # exec /mnt/init /bin/sh -c 'exec sh -l < /dev/ttyS0 > /dev/ttyS0 2> /dev/ttyS0'