Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
chainsx committed May 11, 2023
1 parent 4b978f5 commit 4230d08
Show file tree
Hide file tree
Showing 6 changed files with 5,251 additions and 49 deletions.
114 changes: 65 additions & 49 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ LOSETUP_D_IMG(){
umount ${boot_mnt}
fi
fi
if [ -d $img_mnt ]; then
if grep -q "$img_mnt " /proc/mounts ; then
umount $img_mnt
if [ -d $image_mnt ]; then
if grep -q "$image_mnt " /proc/mounts ; then
umount $image_mnt
fi
fi
if [ "x$device" != "x" ]; then
Expand All @@ -28,23 +28,30 @@ LOSETUP_D_IMG(){
if [ -d ${boot_mnt} ]; then
rm -rf ${boot_mnt}
fi
if [ -d ${image_mnt} ]; then
rm -rf ${image_mnt}
fi
set -e
}

install_reqpkg() {
apt install make bision bc flex xz kpartx qemu-user-static
apt install make bison bc flex kpartx xz-utils qemu-user-static
}

get_riscv_system() {
cd $build_dir
if [ -f $build_dir/system.img* ]; then
echo "clean..."
rm $build_dir/system.img*
fi
wget https://dl.fedoraproject.org/pub/alt/risc-v/repo/virt-builder-images/images/Fedora-Developer-37-20221130.n.0-mmc.raw.img.xz -O system.img.xz
unxz -v system.img.xz
img_file=system.img

if [ ! -f $build_dir/system.img.xz ]; then
echo "system image download failed!"
exit 2
fi
unxz -v system.img.xz
img_file=system.img

LOSETUP_D_IMG

device=`losetup -f --show -P ${img_file}`
Expand All @@ -56,42 +63,51 @@ get_riscv_system() {
bootp=/dev/mapper/${loopX}p3
rootp=/dev/mapper/${loopX}p4

if [ -d $img_mnt ]; then
if [ -d $image_mnt ]; then
LOSETUP_D_IMG
rmdir $img_mnt
rmdir $image_mnt
fi
mkdir $img_mnt
mkdir $image_mnt

mount $rootp $img_mnt
mount $bootp $img_mnt/boot
mount $rootp $image_mnt
mount $bootp $image_mnt/boot

chroot $img_mnt dnf remove kernel* opensbi* *bootloader*
chroot $image_mnt dnf remove kernel* -y
chroot $image_mnt dnf remove opensbi* -y
chroot $image_mnt dnf remove *bootloader* -y

cp -rfp $img_mnt $build_dir/rootfs
cp -rfp $image_mnt $build_dir/rootfs
sync

umount $img_mnt/boot
umount $img_mnt
umount $image_mnt/boot
umount $image_mnt
kpartx -d ${device}

rmdir $img_mnt
rmdir $image_mnt
}

prepare_toolchain() {
cd $build_dir
wget https://github.com/chainsx/armbian-riscv-build/releases/download/toolchain/Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.6.1-20220906.tar.gz
tar -zxvf Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.6.1-20220906.tar.gz
rm *tar.gz && mv Xuantie* riscv64-gcc
if [ ! -d $build_dir/riscv64-gcc ]; then
wget https://github.com/chainsx/armbian-riscv-build/releases/download/toolchain/Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.6.1-20220906.tar.gz
tar -zxf Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.6.1-20220906.tar.gz
rm *tar.gz && mv Xuantie* riscv64-gcc
fi
}


build_kernel() {
git clone --depth=1 http://github.com/revyos/thead-kernel.get -b lpi4a
if [ ! -d $build_dir/thead-kernel ]; then
git clone --depth=1 https://github.com/revyos/thead-kernel.git -b lpi4a
fi
cd thead-kernel
wget https://github.com/chainsx/armbian-riscv-build/raw/main/config/kernel/linux-thead-current.config -O arch/riscv/configs/linux-thead-current_defconfig
if [ -f arch/riscv/configs/linux-thead-current_defconfig ]; then
rm arch/riscv/configs/linux-thead-current_defconfig
fi
cp $build_dir/config/linux-thead-current.config arch/riscv/configs/linux-thead-current_defconfig
make ARCH=riscv CROSS_COMPILE=${build_dir}/riscv64-gcc/bin/riscv64-unknown-linux-gnu- linux-thead-current_defconfig
make ARCH=riscv CROSS_COMPILE=${build_dir}/riscv64-gcc/bin/riscv64-unknown-linux-gnu- -j$(nproc)
make ARCH=riscv CROSS_COMPILE=${build_dir}/riscv64-gcc/bin/riscv64-unknown-linux-gnu- modules_install INSTALL_MOD=kmod
make ARCH=riscv CROSS_COMPILE=${build_dir}/riscv64-gcc/bin/riscv64-unknown-linux-gnu- modules_install INSTALL_MOD_PATH=kmod
cd $build_dir
cp -rfp thead-kernel/kmod/lib/modules/* rootfs/lib/modules
}
Expand All @@ -106,7 +122,7 @@ mk_img() {
img_file=${build_dir}/sd.img
dd if=/dev/zero of=${img_file} bs=1MiB count=$size status=progress && sync

parted ${img_file} mklabel gpt mkpart primary fat32 32768s 524287s
parted ${img_file} mklabel gpt mkpart primary ext4 32768s 524287s
parted ${img_file} mkpart primary ext4 524288s 100%

device=`losetup -f --show -P ${img_file}`
Expand All @@ -118,61 +134,61 @@ mk_img() {
sdbootp=/dev/mapper/${loopX}p1
sdrootp=/dev/mapper/${loopX}p2

mkfs.vfat -n boot ${sdbootp}
mkfs.ext4 -L rootfs ${sdrootp}
mkfs.ext4 -L boot ${sdbootp}
mkfs.ext4 -L root ${sdrootp}
mkdir -p ${root_mnt} ${boot_mnt}
mount -t ext4 ${sdbootp} ${boot_mnt}
mount -t ext4 ${sdrootp} ${root_mnt}

echo "fdt_file=light-lpi4a.dtb
kernel_file=Image
bootargs=console=ttyS0,115200 root=/dev/mmcblk1p2 rootfstype=ext4 rootwait rw earlycon clk_ignore_unused loglevel=7 eth=$ethaddr rootrwoptions=rw,noatime rootrwreset=yes init=/lib/systemd/systemd" >> $boot_mnt/config.txt

cd $boot_mnt
wget https://github.com/chainsx/armbian-riscv-build/raw/main/packages/blobs/riscv64/thead/light_aon_fpga.bin
wget https://github.com/chainsx/armbian-riscv-build/raw/main/packages/blobs/riscv64/thead/light_c906_audio.bin
if [ -f $boot_mnt/config.txt ]; then
rm $boot_mnt/config.txt
fi
cp $build_dir/config/config.txt $boot_mnt/config.txt

cp $build_dir/thead-kernel/arch/riscv/boot/Image .
cp $build_dir/thead-kernel/arch/riscv/boot/dts/thrad/*lpi4a*dtb .
cp $build_dir/firmware/light_aon_fpga.bin $boot_mnt
cp $build_dir/firmware/light_c906_audio.bin $boot_mnt
cp $build_dir/thead-kernel/arch/riscv/boot/Image $boot_mnt
cp $build_dir/thead-kernel/arch/riscv/boot/dts/thead/*lpi4a*dtb $boot_mnt

sync

rsync -avHAXq ${build_dir}/rootfs/* ${root_mnt}
echo "/dev/mmcblk1p2 / ext4 defaults,noatime 0 0" > ${build_dir}/rootfs/etc/fstab
echo "/dev/mmcblk1p1 /boot ext4 defaults,noatime 0 0" >> ${build_dir}/rootfs/etc/fstab

rsync -avHAXq ${build_dir}/rootfs/* ${root_mnt}

sync

umount $sdrootp
umount $sdbootp

LOSETUP_D_IMG

dd if=$sdbootp of=boot.img status=progress
dd if=$sdrootp of=rootfs.img status=progress
sync

mount -t ext4 boot.img ${boot_mnt}
mount -t ext4 rootfs.img ${root_mnt}

rm $boot_mnt/config.txt
echo "fdt_file=light-lpi4a.dtb
kernel_file=Image
bootargs=console=ttyS0,115200 root=/dev/mmcblk0p3 rootfstype=ext4 rootwait rw earlycon clk_ignore_unused loglevel=7 eth=$ethaddr rootrwoptions=rw,noatime rootrwreset=yes init=/lib/systemd/systemd" >> $boot_mnt/config.txt
echo "/dev/mmcblk0p3 / ext4 defaults,noatime 0 0" > ${build_dir}/rootfs/etc/fstab
echo "/dev/mmcblk0p2 /boot ext4 defaults,noatime 0 0" >> ${build_dir}/rootfs/etc/fstab
if [ -f $boot_mnt/config.txt ]; then
rm $boot_mnt/config.txt
fi
cp $build_dir/config/config-emmc.txt $boot_mnt/config.txt

echo "/dev/mmcblk0p3 / ext4 defaults,noatime 0 0" > ${root_mnt}/etc/fstab
echo "/dev/mmcblk0p2 /boot ext4 defaults,noatime 0 0" >> ${root_mnt}/etc/fstab
sync
sleep 10

umount $sdrootp
umount $sdbootp
LOSETUP_D_IMG

losetup -D
kpartx -d ${img_file}
rm system.img
}

build_dir=$(pwd)
img_mnt=${build_dir}/img_mnt
image_mnt=${build_dir}/image_mnt
boot_mnt=${build_dir}/boot_tmp
root_mnt=${build_dir}/root_tmp

install_reqpkg
get_riscv_system
prepare_toolchain
Expand Down
3 changes: 3 additions & 0 deletions config/config-emmc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fdt_file=light-lpi4a.dtb
kernel_file=Image
bootargs=console=ttyS0,115200 root=/dev/mmcblk0p3 rootfstype=ext4 rootwait rw earlycon clk_ignore_unused loglevel=7 eth=$ethaddr rootrwoptions=rw,noatime rootrwreset=yes init=/lib/systemd/systemd
3 changes: 3 additions & 0 deletions config/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fdt_file=light-lpi4a.dtb
kernel_file=Image
bootargs=console=ttyS0,115200 root=/dev/mmcblk1p2 rootfstype=ext4 rootwait rw earlycon clk_ignore_unused loglevel=7 eth=$ethaddr rootrwoptions=rw,noatime rootrwreset=yes init=/lib/systemd/systemd
Loading

0 comments on commit 4230d08

Please sign in to comment.