安装基本工具
因为我这里不是用官方提供的虚拟机,用的是ubuntu20.4,所以需要先安装必要的工具:
sudo apt -y install make git gcc g++ bison flex libncurses5-dev libssl-dev libelf-dev u-boot-tools
sudo apt -y install cmake tree build-essential tcl-dev automake libtool
复制交叉工具链
复制toolchain-loongarch64-linux-gnu-gcc8-host-x86_64-2022-07-18.tar.xz到虚拟机
复制内核源码
复制u-boot源码
解压交叉编译工具、u-boot、内核源码
1、安装工具链的过程其实就是把压缩包解压到/opt目录下:
sudo tar -xf toolchain-loongarch64-linux-gnu-gcc8-host-x86_64-2022-07-18.tar.xz -C /opt/
2、编译u-boot,因为要先编译u-boot后才能有mkimage工具,这样才能打包uImange
设置交叉工具链等环境
lugl@lugl-virtual-machine:~/loonson/u-boot-2022.04-2k500-src-rel2.0.0-e5c962c-build.20221208111218/u-boot-2022.04-2k500-cbd-src$ source ./set_env.sh
====>setup env for LoongArch..
lugl@lugl-virtual-machine:~/loonson/u-boot-2022.04-2k500-src-rel2.0.0-e5c962c-build.20221208111218/u-boot-2022.04-2k500-cbd-src$ make loongson_2k500_mini_dp_defconfig
#
# configuration written to .config
#
lugl@lugl-virtual-machine:~/loonson/u-boot-2022.04-2k500-src-rel2.0.0-e5c962c-build.20221208111218/u-boot-2022.04-2k500-cbd-src$ make
......
BJCOPY spl/u-boot-spl-nodtb.bin
mkdir -p spl/dts/
FDTGREP spl/dts/dt-spl.dtb
COPY spl/u-boot-spl.dtb
CAT spl/u-boot-spl-dtb.bin
COPY spl/u-boot-spl.bin
SYM spl/u-boot-spl.sym
MKIMAGE u-boot.img
COPY u-boot.dtb
MKIMAGE u-boot-dtb.img
CAT u-boot-with-spl.bin
CFGCHK u-boot.cfg
OFCHK .config
到这里编译出了u-boot
把mkimage复制到/usr/bin/目录下
ugl@lugl-virtual-machine:~/loonson/u-boot-2022.04-2k500-src-rel2.0.0-e5c962c-build.20221208111218/u-boot-2022.04-2k500-cbd-src/tools$ sudo cp mkimage /usr/bin/
编译内核
进入内核源码下:
lugl@lugl-virtual-machine:~/loonson/linux-5.10-2k500-src-81224fa-build.20221101081146/linux-5.10-2k500-cbd-src$ source ./set_env.sh
====>setup env for LoongArch...
lugl@lugl-virtual-machine:~/loonson/linux-5.10-2k500-src-81224fa-build.20221101081146/linux-5.10-2k500-cbd-src$ make loongson_2k500_defconfig
lugl@lugl-virtual-machine:~/loonson/linux-5.10-2k500-src-81224fa-build.20221101081146/linux-5.10-2k500-cbd-src$ make loongson_2k500_defconfig
lugl@lugl-virtual-machine:~/loonson/linux-5.10-2k500-src-81224fa-build.20221101081146/linux-5.10-2k500-cbd-src$ make uImage
如果不复制mkimage到指定目录则会报如下错误:
ugl@lugl-virtual-machine:~/loonson/linux-5.10-2k500-src-81224fa-build.20221101081146/linux-5.10-2k500-cbd-src$ make uImage
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CC arch/loongarch/vdso/vgettimeofday.o
LD arch/loongarch/vdso/vdso.so.dbg
VDSOSYM include/generated/vdso-offsets.h
CHK include/generated/compile.h
CC arch/loongarch/kernel/vdso.o
AR arch/loongarch/kernel/built-in.a
CC arch/loongarch/vdso/vgettimeofday.o
LD arch/loongarch/vdso/vdso.so.dbg
OBJCOPY arch/loongarch/vdso/vdso.so
AS arch/loongarch/vdso/vdso.o
AR arch/loongarch/vdso/built-in.a
AR arch/loongarch/built-in.a
GEN .version
CHK include/generated/compile.h
UPD include/generated/compile.h
CC init/version.o
AR init/built-in.a
LD vmlinux.o
MODPOST vmlinux.symvers
MODINFO modules.builtin.modinfo
GEN modules.builtin
LD .tmp_vmlinux.kallsyms1
KSYMS .tmp_vmlinux.kallsyms1.S
AS .tmp_vmlinux.kallsyms1.S
LD .tmp_vmlinux.kallsyms2
KSYMS .tmp_vmlinux.kallsyms2.S
AS .tmp_vmlinux.kallsyms2.S
LD vmlinux
SORTTAB vmlinux
SYSMAP System.map
OBJCOPY arch/loongarch/boot/vmlinux.bin
GZIP arch/loongarch/boot/vmlinux.bin.gz
UIMAGE arch/loongarch/boot/uImage.gz
Invalid architecture, supported are:
alpha Alpha
arc ARC
arm ARM
arm64 AArch64
avr32 AVR32
blackfin Blackfin
ia64 IA64
invalid Invalid ARCH
m68k M68K
microblaze MicroBlaze
mips MIPS
mips64 MIPS 64 Bit
nds32 NDS32
nios2 NIOS II
or1k OpenRISC 1000
powerpc PowerPC
riscv RISC-V
s390 IBM S390
sandbox Sandbox
sh SuperH
sparc SPARC
sparc64 SPARC 64 Bit
x86 Intel x86
x86_64 AMD x86_64
xtensa Xtensa
Error: Invalid architecture
Usage: /usr/bin/mkimage -l image
-l ==> list image header information
/usr/bin/mkimage [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image
-A ==> set architecture to 'arch'
-O ==> set operating system to 'os'
-T ==> set image type to 'type'
-C ==> set compression type 'comp'
-a ==> set load address to 'addr' (hex)
-e ==> set entry point to 'ep' (hex)
-n ==> set image name to 'name'
-d ==> use image data from 'datafile'
-x ==> set XIP (execute in place)
/usr/bin/mkimage [-D dtc_options] [-f fit-image.its|-f auto|-F] [-b <dtb> [-b <dtb>]] [-E] [-B size] [-i <ramdisk.cpio.gz>] fit-image
<dtb> file is used with -f auto, it may occur multiple times.
-D => set all options for device tree compiler
-f => input filename for FIT source
-i => input filename for ramdisk file
-E => place data outside of the FIT structure
-B => align size in hex for FIT structure and header
Signing / verified boot options: [-k keydir] [-K dtb] [ -c <comment>] [-p addr] [-r] [-N engine]
-k => set directory containing private keys
-K => write public keys to this .dtb file
-G => use this signing key (in lieu of -k)
-c => add comment in signature node
-F => re-sign existing FIT image
-p => place external data at a static position
-r => mark keys used as 'required' in dtb
-N => openssl engine to use for signing
/usr/bin/mkimage -V ==> print version information and exit
Use '-T list' to see a list of available image types
make[1]: *** [arch/loongarch/boot/Makefile:80: arch/loongarch/boot/uImage.gz] Error 1
make: *** [arch/loongarch/Makefile:158: uImage] Error 2
复制好后,就会成功得内核:
lugl@lugl-virtual-machine:~/loonson/linux-5.10-2k500-src-81224fa-build.20221101081146/linux-5.10-2k500-cbd-src$ make uImage
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CC arch/loongarch/vdso/vgettimeofday.o
LD arch/loongarch/vdso/vdso.so.dbg
VDSOSYM include/generated/vdso-offsets.h
CHK include/generated/compile.h
CC arch/loongarch/kernel/vdso.o
AR arch/loongarch/kernel/built-in.a
CC arch/loongarch/vdso/vgettimeofday.o
LD arch/loongarch/vdso/vdso.so.dbg
OBJCOPY arch/loongarch/vdso/vdso.so
AS arch/loongarch/vdso/vdso.o
AR arch/loongarch/vdso/built-in.a
AR arch/loongarch/built-in.a
GEN .version
CHK include/generated/compile.h
UPD include/generated/compile.h
CC init/version.o
AR init/built-in.a
LD vmlinux.o
MODPOST vmlinux.symvers
MODINFO modules.builtin.modinfo
GEN modules.builtin
LD .tmp_vmlinux.kallsyms1
KSYMS .tmp_vmlinux.kallsyms1.S
AS .tmp_vmlinux.kallsyms1.S
LD .tmp_vmlinux.kallsyms2
KSYMS .tmp_vmlinux.kallsyms2.S
AS .tmp_vmlinux.kallsyms2.S
LD vmlinux
SORTTAB vmlinux
SYSMAP System.map
OBJCOPY arch/loongarch/boot/vmlinux.bin
GZIP arch/loongarch/boot/vmlinux.bin.gz
UIMAGE arch/loongarch/boot/uImage.gz
Image Name: Linux-5.10.0.lsgd
Created: Tue Jan 3 19:58:50 2023
Image Type: LoongArch Linux Kernel Image (gzip compressed)
Data Size: 7779209 Bytes = 7596.88 KiB = 7.42 MiB
Load Address: 00200000
Entry Point: 00c9b78c
Image arch/loongarch/boot/uImage is ready
到此龙芯2k0500的内核编译到此就成功了。