# 5-GuEe Studio **Repository Path**: openeuler2020/team-1375238611 ## Basic Information - **Project Name**: 5-GuEe Studio - **Description**: XBook and GNL forever! - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2020-12-28 - **Last Updated**: 2023-07-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RISC-V GRUB-2.04 #### 介绍 本项目是以RISC-V 64 virtio为架构、u-boot-spl为BIOS、GRUB-2.04为内核启动器的实验项目 #### 来源 本项目是参加2020 openEuler高校开发者大赛赛题5:为openEuler - RISC-V 添加grub的引导启动方式 的作品。 #### 赛题要求 产出标准:为openEuler for RISC-V 版本增加grub的 引导启动支持 #### 完成概况 按照赛题要求完成了RISC-V 64上通过EFI模式GRUB-2.04的启动,测试内核为`openEuler Kernel 5.10` #### 项目固件构建 * 构建[openEuler Kernel 5.10](https://gitee.com/openeuler/kernel/repository/archive/OLK-5.10.zip) ```shell make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- menuconfig # 在config中启用EFI_STUB sudo make ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu- -j16 cp ./arch/riscv/boot/Image ./vmlinuz-OLK-v5.10 cp ./arch/riscv/boot/Image.gz ./initrd-OLK-v5.10.img ``` 此时获得 `vmlinuz-OLK-v5.10`、`initrd-OLK-v5.10.img` * 构建GRUB 2.04 * 添加linux内核启动补丁(补丁来源:[grub-devel Archives](https://lists.endsoftwarepatents.org/archive/html/grub-devel/2021-03/threads.html)) * 源文件:`grub-2.04/include/grub/riscv64/linux.h` * 补丁路径:`./grub-2.04-patch/linux.h`(补丁来源:[linux.h](https://lists.endsoftwarepatents.org/archive/html/grub-devel/2021-03/msg00342.html)) * 源文件:`grub-2.04/grub-core/loader/riscv/linux.c` * 补丁路径:`./grub-2.04-patch/linux.c`(补丁来源:[linux.c](https://lists.endsoftwarepatents.org/archive/html/grub-devel/2021-03/msg00341.html)) ```shell ./configure \ --disable-werror \ --with-platform=efi \ --target=riscv64 \ CC=gcc \ TARGET_CC=riscv64-linux-gnu-gcc \ TARGET_OBJCOPY=riscv64-linux-gnu-objcopy \ TARGET_STRIP=riscv64-linux-gnu-strip \ TARGET_NM=riscv64-linux-gnu-nm \ TARGET_RANLIB=riscv64-linux-gnu-gcc-ranlib make ``` * 构建U-Boot 在u-boot源码中,在`include\configs\qemu-riscv.h`中添加启动命令 ```c // 防止提示重复定义 #undef CONFIG_BOOTCOMMAND #define CONFIG_BOOTCOMMAND "load virtio 0:1 0x80200000 boot/efi/boot/bootriscv64.efi;bootefi 0x80200000" ``` 开始构建 ```shell git clone https://github.com/u-boot/u-boot cd u-boot make qemu-riscv64_defconfig make CROSS_COMPILE=riscv64-linux-gnu- ``` 此时获得 `u-boot-master/u-boot.bin` * 构建opensbi ```shell git clone https://github.com/riscv/opensbi.git cd opensbi-master export CROSS_COMPILE=riscv64-linux-gnu- make PLATFORM=generic FW_PAYLOAD_PATH=../u-boot/u-boot.bin ``` 此时获得 `opensbi-master/build/platform/generic/firmware/fw_dynamic.bin` * 再次编译U-Boot ```shell cp opensbi-master/build/platform/generic/firmware/fw_dynamic.bin u-boot/fw_dynamic.bin make CONFIG_CMD_BOOTEFI=y \ CONFIG_EFI_LOADER=y \ CONFIG_BLK=y \ CONFIG_PARTITIONS=y \ qemu-riscv64_spl_defconfig make CROSS_COMPILE=riscv64-linux-gnu- ``` 此时获得并复制 `u-boot-master/u-boot.itb`、`u-boot-master/spl/u-boot-spl` #### GRUB配置 * 编写grub配置文件: * grub efi 启动脚本 `./compile.cfg` ```shell search.file /boot/grub/grub.cfg root set prefix=($root)/boot/grub configfile ($root)/boot/grub/grub.cfg ``` * grub 菜单脚本 `./boot/grub/grub.cfg` ```shell set timeout=10 set timeout_style=menu set default=0 set pager=1 menuentry "openEuler" --class openeuler --class os { set root=(hd0,msdos1) linux /boot/vmlinuz-OLK-v5.10 root=/dev/vda1 systemd.default_timeout_start_sec=600 selinux=0 rw highres=off console=ttyS0 mem=4096M earlycon initrd /boot/initrd-OLK-v5.10.img set root=(hd0) boot } ``` * 创建efi启动引导文件 `./Makefile` ```shell grub-mkimage \ -d ./boot/grub/riscv64-efi \ -c ./compile.cfg \ -p ./boot/grub/ \ -o ./efi/boot/bootriscv64.efi \ -O riscv64-efi \ cat chain configfile echo efinet ext2 fat halt help linux \ lsefisystab loadenv lvm minicmd normal part_msdos part_gpt reboot \ search search_fs_file search_fs_uuid search_label serial sleep test \ true ``` * 挂载oe-rv-rv64g-30G.qcow2(OpenEuler RISC-V [地址1](https://repo.openeuler.org/openEuler-preview/) [地址2](https://mirror.iscas.ac.cn/openeuler/openEuler-preview/),命令执行均在root模式下完成) ```shell modprobe nbd max_part=8 qemu-nbd -c /dev/nbd0 oe-rv-rv64g-30G.qcow2 mount /dev/nbd0p1 /mnt/ ``` * 手动安装efi、GRUB、内核 ```shell cp -r ./efi/ /mnt/boot/ cp ./boot/vmlinuz-OLK-v5.10 /mnt/boot/vmlinuz-OLK-v5.10 cp ./boot/initrd-OLK-v5.10.img /mnt/boot/initrd-OLK-v5.10.img cp -r ./boot/grub/ /mnt/boot/ ``` * 卸载oe-rv-rv64g-30G.qcow2 ```shell umount /mnt/ qemu-nbd -d /dev/nbd0 ``` * 启动测试 ```shell qemu-system-riscv64 \ -nographic \ -machine virt \ -smp 2 \ -m 2G \ -bios u-boot-spl \ -rtc base=localtime \ -device loader,file=u-boot.itb,addr=0x80200000 \ -drive file=oe-rv-rv64g-30G.qcow2,format=qcow2,id=hd0 \ -device virtio-blk-device,drive=hd0 \ -device virtio-net-device,netdev=usernet \ -netdev user,id=usernet,hostfwd=tcp::12055-:22 ``` qemu `-smp` `-m`根据设备条件确定 测试截图 ![进入grub](https://gitee.com/openeuler2020/team-1375238611/raw/master/screenshot/grub-menu.png "进入grub") 内核启动 ![内核启动](https://gitee.com/openeuler2020/team-1375238611/raw/master/screenshot/kernel_load.png "内核启动") OpenEuler Login Service ![OpenEuler Login Service](https://gitee.com/openeuler2020/team-1375238611/raw/master/screenshot/OpenEuler_Login_Service.png "OpenEuler Login Service") OpenEuler Poweroff ![OpenEuler Poweroff](https://gitee.com/openeuler2020/team-1375238611/raw/master/screenshot/OpenEuler_Poweroff.png "OpenEuler Poweroff")