Building A Linux Kernel Using Sourcery CodeBench
Building A Linux Kernel Using Sourcery CodeBench
w w w.m e n t o r.c o m
H O W - T O W H I T E P A P E R
INTRODUCTION
This document demonstrates various techniques using the Mentor Embedded Sourcery CodeBench IDE to build a Linux kernel and kernel modules. This paper also demonstrates how to use Sourcery CodeBench to assist in board bring-up and navigate around a large, complex project such as the Linux kernel. The emphasis is on maximizing the time spent in a friendly desktop UI, making this an ideal environment for developers who are more comfortable using an IDE rather than low-level, command-line tools and kernel build procedures. This paper assumes that you have a basic conceptual knowledge of embedded systems. To keep things brief many of the intermediate steps are omitted.
REQUIREMENTS
The host for running Sourcery CodeBench is an x86 computer running the 32-bit Ubuntu Linux 10.04 operating system. The uboot-mkimage and lzop packages are installed on the host system. Sourcery CodeBench Professional for ARM GNU/Linux 2011.09 or newer installed on the host system. A memory card reader capable of reading and writing to secure digital (SD) compatible memory cards. The target system is a PandaBoard Development Kit equipped with an OMAP 4430 processor. The bootloaders, kernel, root filesystem and source code for the PandaBoard come from the Mentor Embedded Linux (MEL) Kit for PandaBoard available from: http://go.mentor.com/linux-kits/ Sourcery CodeBench supports a wide range of hosts, JTAG units, and target processors. For more specific information, visit: http://go.mentor.com/codebench/
PREREQUISITES
Visit: http://go.mentor.com/linux-kits/ to download both the .bin installer, as well as the .tar source file archive. Follow instructions in the PandaBoard Installation and Quick Start Guide to setup the board. Unpack the source code from the .tar archive into ~/mel-kit-pandaboard _ sources.
PROCEDURE
1. Use the command line to go to the directory containing the Linux kernel source code from the MEL Kit for Panda: $ cd ~/mel-kit-pandaboard _ sources/copyleft _ sources/linux-omap4-2.6.35.7-r0c
w w w. m e nto r. co m
2. Create a working tree directory named src using the git clone command on the repository directory: $ git clone dev.omapzoom.org.pub.scm.integration.kernel-ubuntu.git src 3. Go to the src library:
$ cd ~/mel-kit-pandaboard _ sources/copyleft _ sources/linux-omap4-2.6.35.7-r0c/src
4. Check out the glp1.4 branch: $ git checkout glp1.4 5. Apply the kernel patches included in the MEL Kit source archive: $ for x in `ls -1 ../*.patch`; do patch -p1 < $x; done; 6. Copy the default Kernel configuration included in the MEL Kit source archive: $ cp ../defconfig .config 7. Verify the kernel version: $ head -4 Makefile VERSION = 2 PATCHLEVEL = 6 SUBLEVEL = 35 EXTRAVERSION = .7 The output indicates that you have 2.6.35.7. kernel source tree.
RESULTS
You have now checked out the correct source code branch, applied patches included in the MEL Kit for PandaBoard, and copied the existing MEL Kit for PandaBoard kernel configuration file into your source tree.
PROCEDURE
1. Start the Sourcery CodeBench IDE from the command line and create a new workspace to hold your work:
$ ~/CodeSourcery/Sourcery _ CodeBench _ for _ ARM _ GNU _ Linux/bin/sourcerygxx-ide
NOTE: The Sourcery CodeBench installer created a directory in your home directory with shortcuts to documentation and executables. The default name for this directory is Sourcery_CodeBench_for_ARM_EABI. You can use your file browser to browse the shortcuts directory and simply double-click on the icon representing Sourcery CodeBench IDE.
w w w. m e nto r. co m
a. Select File > New > Makefile Project with Existing Code. b. Name your project mel-kit-pandaboard _ sources. c. Browse to the src directory you prepared above for the Existing Code Location. d. Select both the C and C++ Language checkboxes. e. Select Sourcery CodeBench for ARM GNU/Linux under Toolchain for Indexer Settings. f. Click Finish.
w w w. m e nto r. co m
a. Select Projects > properties. b. Select C/C++ Build. c. Select the Behavior tab. d. Enable Parallel Builds. i. Select the Use Parallel Build option. ii. Select Use Parallel Jobs. iii. Enter the number of parallel jobs to run.
NOTE: The kernel build scales very well so that a parallel build on a quad core machine will finish almost four times faster. Using the same number of parallel jobs as cores on your build host is a reasonable choice. IMPORTANT: Do not select the Use Optimal jobs number option, as it spawns unlimited jobs which can cause your system to run out of memory and crash your machine. e. Disable Automatic Builds. i. Clear the Build on Resource save (Auto build) check box. You have more control over when you build the project if you disable automatic builds.
w w w. m e nto r. co m
a. Select and expand Project > Properties > C/C++ Build and select Environment. b. Click Add... on the right side of the dialog box to add the following environment variables.
Meaning for Kernel build system Define the target architecture Define the cross compiler prefix Pass additional toolchain flags Location for kernel module install
The ARCH variable tells the Kernel build system that you are targeting the ARM architecture. The CROSS_ COMPILE variable tells the Kernel build system the toolchain prefix for the cross compiler. The KCFLAGS variable passes additional toolchain options. In this case, the -mno-unassigned-access flag avoids an optimization that causes problems for kernel builds. The INSTALL_MOD_PATH variable indicates where the build system should install the kernel modules. c. Click OK to save your changes.
RESULTS
Up to this point, you have now started the Sourcery CodeBench IDE, created a new project, and imported your Linux source code, configured the project settings, and setup environment variables in preparation for building your Linux kernel.
w w w. m e nto r. co m
w w w. m e nto r. co m
PROCEDURE
1. Create makefile targets in Sourcery CodeBench IDE. a. Select Project > Make Target > Create
2. Create make targets for clean, modules, modules _ install, oldconfig, and uImage. 3. Display the makefile targets in the C/C++ perspective. a. Select the Make Target tab with the green circle icon in the upper right side of the screen. b. Click on the Hide Empty Folders icon.
w w w. m e nto r. co m
RESULTS
You have created the required makefile targets in the Sourcery CodeBench IDE to build the Linux kernel.
PROCEDURE
1. Double click on the targets in the Make Target tab in the following order:
Make Target Invocation Order clean oldconfig uImage modules modules_install Purpose Ensure that you are starting with a clean build Configure the source tree based on the .config file already in place Build the kernel and package into a uImage file as required by U-Boot Build the kernel modules Install the kernel modules in the INSTALL_MOD_PATH directory
w w w. m e nto r. co m
Heres a sample screenshot after the uImage build has been completed:
RESULTS
You have now built the Linux kernel and modules.
PROCEDURE
1. Safely shutdown and power off your PandaBoard. 2. Remove the memory card from your PandaBoard. 3. Insert the card into your memory card reader. 4. Connect the memory card reader to your build host where your kernel and modules reside. You will need root or sudo access on your host for all commands listed on the following page.
w w w. m e nto r. co m
10
5. Mount the partitions from the memory card on the host: $ sudo mount /dev/sdd1 /mnt/sdd1 $ sudo mount /dev/sdd2 /mnt/sdd2 6. Make a backup of your old kernel: $ sudo cp /mnt/sdd1/uImage /mnt/sdd1/uImage.old 7. Copy your new kernel uImage file to the boot media: $ sudo cp ~/mel-kit-pandaboard _ sources/copyleft _ sources/ linux-omap4-2.6.35.7-r0c/src/arch/arm/boot/uImage /mnt/sdd1 8. Make a backup of your old kernel modules: $ sudo mv /mnt/sdd2/lib/modules/2.6.35.7+ /mnt/sdd2/lib/modules/2.6.35.7+.old 9. Copy your new kernel modules to the boot media: $ sudo cp -a ~/modules _ install/lib /mnt/sdd2 NOTE: Remember, the INSTALL _ MOD _ PATH environment variable defined the installation directory as ~/modules _ install in the section above titled Preparing to Build a Kernel using the CodeBench IDE. When you built the modules _ install makefile target above, the modules were installed in that directory. 10. Recursively change the ownership and group to root on the lib directory in the PandaBoards root filesystem: $ sudo chown -R root:root /mnt/sdd2/lib 11. Verify the contents of the PandaBoards boot medias first partition with the ls command. The output should show MLO, u-boot.img, uImage and uImage.old files: $ sudo ls /mnt/sdd1MLO u-boot.img uImage uImage.old
12. Verify the contents of the PandaBoards root filesystem /lib directory with the ls command. You should see directories with your new and old kernel modules: $ sudo ls /mnt/sdd2/lib/modules 2.6.35.7+ 2.6.35.7+.old 13. Unmount both partitions from the host: $ sudo umount /mnt/sdd1 $ sudo umount /mnt/sdd2
w w w. m e nto r. co m
11
14. Disconnect the memory card reader from the host. 15. Remove the memory card from the memory card reader. 16. Insert the memory card into your PandaBoard. 17. Power on the PandaBoard to boot your new kernel. 18. Login and verify that you booted your new kernel with the dmesg command: # dmesg | grep Linux Version Linux version 2.6.35.7+ (user@buildhost) (gcc version 4.6.1 (Sourcery CodeBench 2011.09-78) ) #3 SMP PREEMPT Thu Feb 23 08:43:28 PST 2012
RESULTS
You have now installed and booted your newly built Linux kernel and modules.
FURTHER READING
Kernel build system documentation: http://kernel.org/doc/Documentation/kbuild/kbuild.txt Kernel makefile documentation: http://kernel.org/doc/Documentation/kbuild/makefiles.txt Kernel makefile target documentation: http://kernel.org/doc/makehelp.txt
w w w.m e n t o r.c o m
2012 Mentor Graphics Corporation, all rights reserved. This document contains information that is proprietary to Mentor Graphics Corporation and may be duplicated in whole or in part by the original recipient for internal business purposes only, provided that this entire notice appears in all copies. In accepting this document, the recipient agrees to make every reasonable effort to prevent unauthorized use of this information. All trademarks mentioned in this document are the trademarks of their respective owners.
MGC 05-12
1030520-w