run-qemu.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. # uboot版本
  3. UBOOT_VERSION="v2023.10"
  4. RISCV64_UBOOT_PATH="arch/riscv64/u-boot-${UBOOT_VERSION}-riscv64"
  5. export ARCH=${ARCH:=riscv64}
  6. echo "ARCH: ${ARCH}"
  7. # 磁盘镜像
  8. DISK_NAME="disk-${ARCH}.img"
  9. QEMU=qemu-system-${ARCH}
  10. QEMU_MEMORY="512M"
  11. QEMU_SMP="2,cores=2,threads=1,sockets=1"
  12. QEMU_ACCELARATE=""
  13. QEMU_DISK_IMAGE="../output/${DISK_NAME}"
  14. QEMU_DRIVE="-drive id=disk,file=${QEMU_DISK_IMAGE},if=none"
  15. QEMU_DEVICES=" -device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 "
  16. # 标准的trace events
  17. # qemu_trace_std=cpu_reset,guest_errors
  18. qemu_trace_std=cpu_reset
  19. QEMU_ARGUMENT=""
  20. if [ ${ARCH} == "riscv64" ]; then
  21. QEMU_MACHINE=" -machine virt "
  22. QEMU_MONITOR=""
  23. QEMU_ARGUMENT+=" --nographic "
  24. else
  25. echo "不支持的架构"
  26. exit 1
  27. fi
  28. QEMU_ARGUMENT+=" -d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d ${QEMU_MONITOR} -d ${qemu_trace_std} "
  29. QEMU_ARGUMENT+=" -s ${QEMU_MACHINE} "
  30. QEMU_ARGUMENT+=" ${QEMU_DEVICES} ${QEMU_DRIVE} "
  31. TMP_LOOP_DEVICE=""
  32. # 安装riscv64的uboot
  33. install_riscv_uboot()
  34. {
  35. if [ ! -d ${RISCV64_UBOOT_PATH} ]; then
  36. echo "正在下载u-boot..."
  37. uboot_tar_name="u-boot-${UBOOT_VERSION}-riscv64.tar.xz"
  38. uboot_parent_path=$(dirname ${RISCV64_UBOOT_PATH}) || (echo "获取riscv u-boot 版本 ${UBOOT_VERSION} 的父目录失败" && exit 1)
  39. if [ ! -f ${uboot_tar_name} ]; then
  40. wget https://mirrors.dragonos.org.cn/pub/third_party/u-boot/${uboot_tar_name} || (echo "下载riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
  41. fi
  42. echo "下载完成"
  43. echo "正在解压u-boot到 '$uboot_parent_path'..."
  44. mkdir -p $uboot_parent_path
  45. tar xvf u-boot-${UBOOT_VERSION}-riscv64.tar.xz -C ${uboot_parent_path} || (echo "解压riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
  46. echo "解压完成"
  47. rm -rf u-boot-${UBOOT_VERSION}-riscv64.tar.xz
  48. fi
  49. echo "riscv u-boot 版本 ${UBOOT_VERSION} 已经安装"
  50. }
  51. run_qemu()
  52. {
  53. echo "正在启动qemu..."
  54. if [ ${ARCH} == "riscv64" ]; then
  55. QEMU_ARGUMENT+=" -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin "
  56. fi
  57. echo "qemu命令: ${QEMU} ${QEMU_ARGUMENT}"
  58. ${QEMU} ${QEMU_ARGUMENT}
  59. }
  60. format_as_mbr() {
  61. echo "Formatting as MBR..."
  62. # 使用fdisk把disk.img的分区表设置为MBR格式(下方的空行请勿删除)
  63. fdisk ${QEMU_DISK_IMAGE} << EOF
  64. o
  65. n
  66. a
  67. w
  68. EOF
  69. }
  70. mount_disk_image(){
  71. echo "正在挂载磁盘镜像..."
  72. TMP_LOOP_DEVICE=$(sudo losetup -f --show -P ${QEMU_DISK_IMAGE}) || exit 1
  73. # 根据函数入参判断是否需要格式化磁盘镜像
  74. if [ "$1" == "mnt" ]; then
  75. mkdir -p ../output/mnt
  76. sudo mount ${TMP_LOOP_DEVICE}p1 ../output/mnt
  77. fi
  78. echo "挂载磁盘镜像完成"
  79. }
  80. umount_disk_image(){
  81. echo "正在卸载磁盘镜像..."
  82. if [ "$1" == "mnt" ]; then
  83. sudo umount ../output/mnt
  84. fi
  85. sudo losetup -d ${TMP_LOOP_DEVICE} || (echo "卸载磁盘镜像失败" && exit 1)
  86. echo "卸载磁盘镜像完成"
  87. }
  88. prepare_disk_image()
  89. {
  90. # 如果磁盘镜像不存在,则创建磁盘镜像
  91. echo "正在准备磁盘镜像..."
  92. if [ ! -f ${QEMU_DISK_IMAGE} ]; then
  93. echo "正在创建磁盘镜像..."
  94. qemu-img create -f raw ${QEMU_DISK_IMAGE} 256M || (echo "创建磁盘镜像失败" && exit 1)
  95. format_as_mbr
  96. mount_disk_image
  97. echo "loop device: ${TMP_LOOP_DEVICE}"
  98. echo "正在格式化磁盘镜像..."
  99. sudo mkfs.vfat -F 32 ${TMP_LOOP_DEVICE}p1
  100. umount_disk_image
  101. echo "Successfully mkfs"
  102. chmod 777 ${QEMU_DISK_IMAGE}
  103. echo "创建磁盘镜像完成"
  104. fi
  105. echo "磁盘镜像已经准备好"
  106. }
  107. write_disk_image(){
  108. mkdir -p ../output/sysroot
  109. echo "正在写入磁盘镜像..."
  110. mount_disk_image mnt
  111. mkdir -p ../output/sysroot/efi/boot
  112. if [ ${ARCH} == "riscv64" ]; then
  113. cp ../output/dragon_stub-riscv64.efi ../output/sysroot/efi/boot/bootriscv64.efi
  114. fi
  115. sudo cp -r ../output/sysroot/* ../output/mnt
  116. umount_disk_image mnt
  117. echo "写入磁盘镜像完成"
  118. }
  119. main()
  120. {
  121. install_riscv_uboot
  122. prepare_disk_image
  123. write_disk_image
  124. run_qemu
  125. }
  126. main