Raspberry Pi Developer's Guide: Build



Source


The source code of official foundation Raspbian images is scattered over few places :
  • Linux kernel
  • http://www.github.com/raspberrypi/linux
  • Modified packages/firmware
  • https://github.com/raspberrypi
  • Other open source packages
  • Use the command:
    
      apt-get build-dep (package)
      apt-get source (package)
    
    
    • apt-get build-dep (package)
    • Download and install the packages necessary to build the source Debian package (package).
    • apt-get source (package)
    • Download the source Debian package for and extract it. You must have deb-src lines in your /etc/apt/sources.list for this to work.
      
      deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free
      
      
      Then,
      
        apt-get update 
        apt-get source (package)
      
      
    APT will examine the available packages to decide which source package to fetch. It will then find and download into the current directory .



Linux Kernel


Setup the build host system


There are two main methods for building the kernel.

Local building on a Raspberry Pi


First install the latest version of Raspbian then boot your Pi.
Log in, install Git and the build dependencies:

sudo apt-get install git bc


Cross-compiling


Ubuntu is a suitable Linux cross-compilation host; since Raspbian is also a Debian distribution,

Install toolchain


Download the toolchain to the home folder:

install -d ~/pi
cd ~/pi
git clone https://github.com/raspberrypi/tools


Then setup the $PATH environment variable to include the toolchain's path.
  • For a 32-bit host
  • 
    echo PATH=\$PATH:~/pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin >> ~/build_pi
    source ~/build_pi
    
    
  • For a 64-bit host
  • 
    echo PATH=\$PATH:~/pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin >> ~/build_pi
    source ~/build_pi
    
    

Download the kernel source


git clone --depth=1 https://github.com/raspberrypi/linux

The above will download the current active branch.
Omitting the --depth=1 will download the entire repository, including the full history of all branches.
To download a different branch (again with no history), use the --branch option, refer to the original GitHub repository for information about the available branches.

Kernel configuration

  • Raspberry Pi 1, Pi Zero, Pi Zero W, and Compute Module default build configuration
  • 
    cd linux
    KERNEL=kernel
    make  bcmrpi_defconfig
    
    
  • Raspberry Pi 2, Pi 3, Pi 3+, and Compute Module 3 default build configuration
  • 
    cd linux
    KERNEL=kernel7
    export ARCH=arm
    export CROSS_COMPILE=arm-linux-gnueabihf-
    
    make bcm2709_defconfig
    
    

Build and install the kernel, modules, and Device Tree blobs


Build


make -j4 zImage modules dtbs


Backup


Backup the current Pi image before installing the built files.

Initialize the backup files of the source Raspbian image


After the Raspbian image is downloaded,
  • Discover the image file
  • 
    cd pi/mnt/
    sudo unzip ../backup/2018-11-13-raspbian-stretch-full.zip
    sudo fdisk -u -l 2018-11-13-raspbian-stretch-full.img
    
    
    Write down the "Start" sector number for each partition as P1_SS and P2_SS: 8192 and 98304. Each sector is 512 bytes, calculate the offset for each partition:
    
    P1_OFFSET = 8192 * 512 = 4194304
    P1_OFFSET = 98304 * 512 = 50331648
    
  • Mount boot and root partitions
  • 
    install -d /mnt/loop/boot
    install -d /mnt/loop/root
    sudo mount -t vfat -o loop,offset=P1_OFFSET 2018-11-13-raspbian-stretch-full.img /mnt/loop/boot
    sudo mount -t ext4 -o loop,offset=P2_OFFSET 2018-11-13-raspbian-stretch-full.img /mnt/loop/root
    
    
  • Copy the files from the mounted partitions
  • Use rsync,
    
      rsync -au "source_folder_path" "target_folder_path"
    
    
    where:
    • -a
    • Do the sync preserving all filesystem attributes
    • -v
    • run verbosely
    • -u
    • skip files that are newer on the target folder
    Therefore,
    
    install -d fat32   
    install -d ext4
    sudo rsync -avu /mnt/loop/boot/* fat32/
    sudo rsync -avu /mnt/loop/root/* ext4/
    
    

    Backup the SD card

    The use of the dd tool can overwrite any partition of your machine. If you specify the wrong device in the instructions below, you could delete your primary Linux partition. Please be careful. Log in the Pi:
    • Discovering the SD card mountpoint
    • Run lsblk. It will be listed as something like /dev/mmcblk0
    • Dump the image from the SD card
    • Make sure the device name is the name of the whole SD card as described above, not just a partition.
      
      sudo dd bs=4M if=/dev/mmcblk0 of=pi.img
      
      
    If there is any problem someday, restore the image from the backup file:
    
    sudo dd if=pi.img of=/dev/mmcblk0 bs=4M
    
    

Install


Having built the kernel, you need to copy it onto your Raspberry Pi using 2 ways.

Sync the files remotely


Prepare the files to be synced:
  • install modules
  • 
    make INSTALL_MOD_PATH=$PWD/../mnt/ext4 modules_install
    
    
  • copy the kernel and Device Tree blobs
  • 
     cp arch/arm/boot/zImage ../mnt/fat32/$KERNEL.img
     cp arch/arm/boot/dts/*.dtb ../mnt/fat32/
     cp arch/arm/boot/dts/overlays/*.dtb* ../mnt/fat32/overlays/
     cp arch/arm/boot/dts/overlays/README ../mnt/fat32/overlays/
    
    

To sync your files from the build host system(A) to the Raspberry pi (B), follow these steps.

  • Install rsync
  • Install rsync on both server A and server B.
    
    apt-get install rsync
    
    
  • Generate an SSH key on B
  • Run the following command on B to generate an SSH key.
    
    
    ssh-keygen
    
    
    Press enter to skip all inputs. Copy the content generated in /root/.ssh/id_rsa.pub into /home/jerry/.ssh/authorized_keys on A. Now B can log into A as "jerry" using SSH . If SSH server is not installed,
    
    sudo apt-get install openssh-server
    
    
    After that, you should have SSH service enabled in your system, you may check its status by running command:
    
    sudo service ssh status
    
    
  • Start sync
  • Go back to B and start rsync for the first time. Review and run the command below. You will need to add the path to your files on both A and B and the IP address of server A.
    
    
    rsync -avrt --delete --rsh='ssh -p 22' jerry@A_IP:/mnt/work/jerry/STB/pi/mnt/ext4 mnt
    
    
    Type yes if you been asked to save the authentication information. All remote files under "/mnt/work/jerry/STB/pi/mnt/ext4" will be synced into the local folder "mnt/ext4".

Install directly onto the SD card


Use lsblk to identify your SD card.
You should end up with something like this:

sdb
   sdx1
   sdx2

with sdx1 being the FAT (boot) partition, and sdx2 being the ext4 filesystem (root) partition.
If you use a NOOBS card, you should see something like this:

sdb
  sdx1
  sdx2
  sdx5
  sdx6
  sdx7

with sdx6 being the FAT (boot) partition, and sdx7 being the ext4 filesystem (root) partition.
Mount the boot and root partitions:

mkdir mnt
mkdir mnt/fat32
mkdir mnt/ext4
sudo mount /dev/sdx1 mnt/fat32
sudo mount /dev/sdx2 mnt/ext4


Install the modules:

sudo make INSTALL_MOD_PATH=mnt/ext4 modules_install

Finally, copy the kernel and Device Tree blobs:

sudo cp arch/arm/boot/zImage mnt/fat32/$KERNEL.img
sudo cp arch/arm/boot/dts/*.dtb mnt/fat32/
sudo cp arch/arm/boot/dts/overlays/*.dtb* mnt/fat32/overlays/
sudo cp arch/arm/boot/dts/overlays/README mnt/fat32/overlays/
sudo umount mnt/fat32
sudo umount mnt/ext4

You can edit the config.txt file to tell Pi to select the kernel to boot:

kernel=kernel-myconfig.img

留言

熱門文章