run.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/bash
  2. usage ()
  3. {
  4. echo "Usage:"
  5. echo "./run.sh [<path_to_image>]"
  6. echo "Note: if <path_to_image> is not provided, will create a 'sd.bin'"
  7. echo "in the current directory and load it by default."
  8. }
  9. path_image=${1}
  10. if [ -z $path_image ]; then
  11. path_image="./sd.bin"
  12. if [ ! -f $path_image ]; then
  13. dd if=/dev/zero of=$path_image bs=1024 count=65536
  14. mkfs.fat $path_image
  15. fi
  16. fi
  17. if [ ! -f $path_image ]; then
  18. echo "ERROR: $path_image does not exist!"
  19. usage
  20. exit
  21. fi
  22. QEMU_CMD="qemu-system-riscv64 -nographic -machine virt -m 256M -kernel rtthread.bin"
  23. if grep -q "#define RT_USING_SMP" ./rtconfig.h 2>/dev/null; then
  24. hart_num=$(grep "RT_CPUS_NR = [0-9]*;" ./link_cpus.lds 2>/dev/null | awk -F'[=;]' '{gsub(/ /, "", $2); print $2}')
  25. if [ -z "$hart_num" ] || [ "$hart_num" -lt 1 ]; then
  26. echo "Warning: Invalid or missing RT_CPUS_NR, defaulting to 1"
  27. hart_num=1
  28. fi
  29. QEMU_CMD="$QEMU_CMD -smp $hart_num"
  30. fi
  31. QEMU_CMD="$QEMU_CMD \
  32. -drive if=none,file=$path_image,format=raw,id=blk0 -device virtio-blk-device,drive=blk0,bus=virtio-mmio-bus.0 \
  33. -netdev user,id=tap0 -device virtio-net-device,netdev=tap0,bus=virtio-mmio-bus.1 \
  34. -device virtio-serial-device -chardev socket,host=127.0.0.1,port=4321,server=on,wait=off,telnet=on,id=console0 -device virtserialport,chardev=console0"
  35. eval $QEMU_CMD