본문 바로가기

RISC-V/RISC-V 빌드 및 환경

[RISC-V] u-boot 크로스 컴파일

u-boot 크로스 컴파일 (RISC-V)

ubuntu (x86) 시스템에서 u-boot를 크로스 컴파일 (RISC-V)을 할 것이다.
먼저 빌드에 필요한 유틸리티를 설치하자:

sudo apt-get install libncurses-dev libssl-dev bc flex bison make gcc gcc-riscv64-linux-gnu -y

아래와 같은 명령어를 사용해 uboot 소스를 다운로드하자:

$ git clone https://github.com/u-boot/u-boot.git -b u-boot-2023.07.y
Cloning into 'u-boot'...
...
Checking out files: 100% (19873/19873), done.

다운로드가 마무리되면 u-boot 디렉토리가 생성됐다는 사실을 알 수 있어. 

$ ls
u-boot
$ cd u-boot

이제 크로스 컴파일을 위한 환경 설정을 하자: 

$ export ARCH=riscv CROSS_COMPILE=riscv64-linux-gnu-

starfive_visionfive2_defconfig 를 적용해서, 다음 명령어를 사용해서 빌드를 하자:

$ make starfive_visionfive2_defconfig
$ make -j4

많은 바이너리가 생성되는데, u-boot 파일이 심벌이 포함된 바이너리 파일이다:

$ ls
api           common     env       MAINTAINERS                        spl         u-boot-dtb.bin      u-boot.srec
arch          config.mk  examples  Makefile                           System.map  u-boot-dtb.img      u-boot.sym
bin           configs    fs        net                                test        u-boot.dtb.out
board         disk       include   post                               tools       u-boot.img
boot          doc        Kbuild    py                                 u-boot      u-boot.lds

아래 명령어를 사용해서 u-boot의 어셈블리 명령어를 추출하자: 

$ ./riscv64-unknown-linux-gnu-objdump -d u-boot > code_uboot.c

code_uboot.c 파일에서 추출된 어셈블리 명령어를 확인할 수 있다.

먼저 헤더 정보를 확인하자.

$ ./riscv64-unknown-linux-gnu-objdump -x u-boot | more
u-boot:     file format elf64-littleriscv
u-boot
architecture: riscv:rv64, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x0000000040200000

Program Header:
    LOAD off    0x0000000000001000 vaddr 0x0000000040200000 paddr 0x0000000040200000 align 2**12
         filesz 0x0000000000088298 memsz 0x000000000008ecb0 flags rwx
 DYNAMIC off    0x0000000000074e80 vaddr 0x0000000040273e80 paddr 0x0000000040273e80 align 2**3
         filesz 0x0000000000000110 memsz 0x0000000000000110 flags rw-
   STACK off    0x0000000000000000 vaddr 0x0000000000000000 paddr 0x0000000000000000 align 2**4
         filesz 0x0000000000000000 memsz 0x0000000000000000 flags rw-

'start address 0x0000000040200000' information reveals that the startup code is located address at 0x40200000.

startup 코드 code-walkthrough

_start 심벌에서 스타트업 코드가 확인된다. Trap vector entry 주소를 설정한다.

0000000040200000 <_start>:
    40200000:   822a                    mv      tp,a0
    40200002:   84ae                    mv      s1,a1
    40200004:   00000193                li      gp,0
    40200008:   00074297                auipc   t0,0x74
    4020000c:   4402b283                ld      t0,1088(t0) # 40274448 <trap_entry+0x7352c>
    40200010:   10529073                csrw    stvec,t0
    40200014:   10401073                csrw    sie,zero