Sunday, 2 February 2020

FreeBSD ARM cross compile

Free BSD build from chroot for ARM

 

Post by marius » Tue Sep 10, 2019 4:17 pm
Prerequisites
  • Burn the image.
    Make some research to make the BBB to boot from sdcard. Eventually kill the u-boot from emmc
Shell: PC freeBSD x86

Install qemu
Code: Select all
sudo pkg install qemu-user-static
sudo pkg install bash
  • Get the sdcard and plug it to the PC slot
    Type dmesg for find the mount block device. Mine is /dev/da0s2a
    Create a working folder for this image
Code: Select all
cd ~
mkdir BBB
mkdir BBB/ROOTFS
mkdir BBB/mnnt
  • Mount the SD card /dev/da0s2a to BBB/mnnt
  • Copy the whole content from BBB/mnt to BBB/ROOTFS


Create a file chroot.sh and paste the content.
Code: Select all
cd BBB
nano chroot.sh
chmod +x ./chroot.sh
chroot.sh file
Code: Select all
#!/bin/bash
ROOT_FS=./ROOTFS

[[ ! -f /usr/local/bin/qemu-arm-static ]] && echo "install qemu arm static please" && exit
[[ ! -f ./$ROOT_FS//usr/local/bin/qemu-arm-static  ]] && cp /usr/local/bin/qemu-arm-static ./$ROOT_FS/usr/local/bin/qemu-arm-static

if [[ -f ./$ROOT_FS/etc/resolv.conf ]];then
 mv ./$ROOT_FS/etc/resolv.conf ./$ROOT_FS/etc/resolv.conf-old
fi
cp  /etc/resolv.conf  ./$ROOT_FS/etc/resolv.conf

if [[ -f ./$ROOT_FS/etc/resolv.conf ]];then
    mount_nullfs /sys ./$ROOT_FS/sys/
    mount_nullfs /dev ./$ROOT_FS/dev/

    chroot ./$ROOT_FS 
#    chroot ./$ROOT_FS qemu-arm-static /bin/bash

    umount  ./$ROOT_FS/dev/
    umount ./$ROOT_FS/sys/
fi

if [[ -f ./$ROOT_FS/etc/resolv.conf-old ]];then
 mv ./$ROOT_FS/etc/resolv.conf-old ./$ROOT_FS/etc/resolv.conf
fi
  • So far we have a rootfs filesystem and a chroot.sh script
We can chroot to BBB debian ARM image by running the script
Code: Select all
[marius@marius ~/BBB]$ sudo ./chroot.sh 
root@marius:/ # uname -a
FreeBSD marius 12.0-RELEASE FreeBSD 12.0-RELEASE r341666 GENERIC  arm
root@marius:/ # 




We can compile an ARM program so we can run it on the BBB.

Code: Select all
cd /home/freebsd/
nano hello.cpp
# paste this content
//-------------------------------------------------from here
#include <iostream>
int main(int n, char* va[])
{
        std::cout << "hello \n";
        return 0;
}
//-------------------------------------------------to here

# save
# compile
root@marius:/home/freebsd # c++ -o hello ./hello.cpp 
# check the arhitecture
root@marius:/home/freebsd # readelf -h ./hello
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 09 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            FreeBSD
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM  <--------------------------------   is ARM
  Version:                           0x1
  Entry point address:               0x11000
  Start of program headers:          52 (bytes into file)
  Start of section headers:          28428 (bytes into file)
  Flags:                             0x5000400, Version5 EABI, VFP
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         10
  Size of section headers:           40 (bytes)
  Number of section headers:         38
  Section header string table index: 36
root@marius:/home/freebsd # 

#run it
root@marius:/home/freebsd # ./hello 
hello 
root@marius:/home/freebsd #

No comments:

Post a Comment