2006-10-16 04:21:44 +00:00
One of the biggest challenges to getting started with embedded devices is that you
2007-01-05 15:26:38 +00:00
cannot just install a copy of Linux and expect to be able to compile a firmware.
2006-11-06 23:37:55 +00:00
Even if you did remember to install a compiler and every development tool offered,
2007-01-05 15:26:38 +00:00
you still would not have the basic set of tools needed to produce a firmware image.
2006-10-16 04:21:44 +00:00
The embedded device represents an entirely new hardware platform, which is
2007-01-05 15:26:38 +00:00
most of the time incompatible with the hardware on your development machine, so in a process called
2006-10-16 04:21:44 +00:00
cross compiling you need to produce a new compiler capable of generating code for
2006-11-06 23:37:55 +00:00
your embedded platform, and then use it to compile a basic Linux distribution to
2006-10-16 04:21:44 +00:00
run on your device.
2007-01-05 15:26:38 +00:00
The process of creating a cross compiler can be tricky, it is not something that is
regularly attempted and so there is a certain amount of mystery and black magic
associated with it. In many cases when you are dealing with embedded devices you will
2006-11-06 23:37:55 +00:00
be provided with a binary copy of a compiler and basic libraries rather than
2007-01-05 15:26:38 +00:00
instructions for creating your own -- it is a time saving step but at the same time
often means you will be using a rather dated set of tools. Likewise, it is also common
2006-11-26 05:22:01 +00:00
to be provided with a patched copy of the Linux kernel from the board or chip vendor,
2006-10-16 04:21:44 +00:00
but this is also dated and it can be difficult to spot exactly what has been
2006-11-26 05:22:01 +00:00
modified to make the kernel run on the embedded platform.
2006-10-16 04:21:44 +00:00
\subsection { Building an image}
2006-11-26 05:22:01 +00:00
OpenWrt takes a different approach to building a firmware; downloading, patching
and compiling everything from scratch, including the cross compiler. To put it
2007-01-05 15:26:38 +00:00
in simpler terms, OpenWrt does not contain any executables or even sources, it is an
2006-11-06 23:37:55 +00:00
automated system for downloading the sources, patching them to work with the given
2006-11-26 05:22:01 +00:00
platform and compiling them correctly for that platform. What this means is that
2006-10-16 04:21:44 +00:00
just by changing the template, you can change any step in the process.
2006-11-06 23:37:55 +00:00
As an example, if a new kernel is released, a simple change to one of the Makefiles
2006-10-16 04:21:44 +00:00
will download the latest kernel, patch it to run on the embedded platform and produce
2007-01-05 15:26:38 +00:00
a new firmware image -- there is no work to be done trying to track down an unmodified
2006-11-06 23:37:55 +00:00
copy of the existing kernel to see what changes had been made, the patches are
2007-01-05 15:26:38 +00:00
already provided and the process ends up almost completely transparent. This does not
just apply to the kernel, but to anything included with OpenWrt -- It is this one
2006-10-16 04:21:44 +00:00
simple understated concept which is what allows OpenWrt to stay on the bleeding edge
with the latest compilers, latest kernels and latest applications.
2006-11-26 05:22:01 +00:00
So let's take a look at OpenWrt and see how this all works.
2006-10-16 04:21:44 +00:00
\subsubsection { Download openwrt}
This article refers to the "Kamikaze" branch of OpenWrt, which can be downloaded via
subversion using the following command:
\begin { Verbatim}
2006-11-26 05:22:01 +00:00
$ svn co https: / / svn.openwrt.org / openwrt / trunk kamikaze
2006-10-16 04:21:44 +00:00
\end { Verbatim}
2007-01-05 15:26:38 +00:00
Additionally, ther is a trac interface on \href { https://dev.openwrt.org/} { https://dev.openwrt.org/}
2006-10-16 04:21:44 +00:00
which can be used to monitor svn commits and browse the sources.
\subsubsection { The directory structure}
There are four key directories in the base:
\begin { itemize}
2006-11-26 05:22:01 +00:00
\item \texttt { tools}
\item \texttt { toolchain}
\item \texttt { package}
\item \texttt { target}
2006-10-16 04:21:44 +00:00
\end { itemize}
\texttt { tools} and \texttt { toolchain} refer to common tools which will be
2007-01-05 15:26:38 +00:00
used to build the firmware image, the compiler, and the C library.
2006-10-16 04:21:44 +00:00
The result of this is three new directories, \texttt { tool\_ build} , which is a temporary
directory for building the target independent tools, \texttt { toolchain\_ build\_ \textit { <arch>} }
which is used for building the toolchain for a specific architecture, and
\texttt { staging\_ dir\_ \textit { <arch>} } where the resulting toolchain is installed.
2007-01-05 15:26:38 +00:00
You will not need to do anything with the toolchain directory unless you intend to
2006-10-16 04:21:44 +00:00
add a new version of one of the components above.
2006-11-26 05:22:01 +00:00
\begin { itemize}
\item \texttt { tool\_ build}
\item \texttt { toolchain\_ build\_ \textit { <arch>} }
\end { itemize}
2006-11-06 23:37:55 +00:00
\texttt { package} is for exactly that -- packages. In an OpenWrt firmware, almost everything
2006-10-16 04:21:44 +00:00
is an \texttt { .ipk} , a software package which can be added to the firmware to provide new
2006-11-26 05:22:01 +00:00
features or removed to save space. Note that packages are also maintained outside of the main
trunk and can be obtained from subversion at the following location:
\begin { Verbatim}
$ svn co https: / / svn.openwrt.org / openwrt / packages .. / packages
\end { Verbatim}
Those packages can be used to extend the functionality of the build system and need to be
symlinked into the main trunk. Once you do that, the packages will show up in the menu for
configuration. From kamikaze you would do something like this:
\begin { Verbatim}
$ ls
kamikaze packages
$ ln - s packages / net / nmap kamikaze / package / nmap
\end { Verbatim}
2006-10-16 04:21:44 +00:00
2007-01-05 15:26:38 +00:00
To include all packages, issue the following command :
\begin { Verbatim}
$ ln - s packages / * / * kamikaze / package /
\end { Verbatim}
2006-10-16 04:21:44 +00:00
\texttt { target} refers to the embedded platform, this contains items which are specific to
2006-11-06 23:37:55 +00:00
a specific embedded platform. Of particular interest here is the "\texttt { target/linux} "
directory which is broken down by platform and contains the kernel config and patches
2006-10-16 04:21:44 +00:00
to the kernel for a particular platform. There's also the "\texttt { target/image} " directory
which describes how to package a firmware for a specific platform.
Both the target and package steps will use the directory "\texttt { build\_ \textit { <arch>} } "
as a temporary directory for compiling. Additionally, anything downloaded by the toolchain,
target or package steps will be placed in the "\texttt { dl} " directory.
2006-11-26 05:22:01 +00:00
\begin { itemize}
\item \texttt { build\_ \textit { <arch>} }
\item \texttt { dl}
\end { itemize}
2006-10-16 04:21:44 +00:00
\subsubsection { Building OpenWrt}
While the OpenWrt build environment was intended mostly for developers, it also has to be
simple enough that an inexperienced end user can easily build his or her own customized firmware.
Running the command "\texttt { make menuconfig} " will bring up OpenWrt's configuration menu
screen, through this menu you can select which platform you're targeting, which versions of
2006-11-06 23:37:55 +00:00
the toolchain you want to use to build and what packages you want to install into the
2006-11-26 05:22:01 +00:00
firmware image. Note that it will also check to make sure you have the basic dependencies for it
to run correctly. If that fails, you will need to install some more tools in your local environment
before you can begin.
Similar to the linux kernel config, almost every option has three choices,
2006-10-16 04:21:44 +00:00
\texttt { y/m/n} which are represented as follows:
\begin { itemize}
2006-11-06 23:37:55 +00:00
\item { \texttt { <*>} (pressing y)} \\
This will be included in the firmware image
\item { \texttt { <M>} (pressing m)} \\
This will be compiled but not included (for later install)
\item { \texttt { < >} (pressing n)} \\
This will not be compiled
2006-10-16 04:21:44 +00:00
\end { itemize}
2006-11-06 23:37:55 +00:00
After you've finished with the menu configuration, exit and when prompted, save your
2006-10-16 04:21:44 +00:00
configuration changes. To begin compiling the firmware, type "\texttt { make} ". By default
OpenWrt will only display a high level overview of the compile process and not each individual
command.
\subsubsection { Example:}
\begin { Verbatim}
make[2] toolchain/install
make[3] -C toolchain install
make[2] target/compile
make[3] -C target compile
make[4] -C target/utils prepare
[...]
\end { Verbatim}
This makes it easier to monitor which step it's actually compiling and reduces the amount
2006-11-06 23:37:55 +00:00
of noise caused by the compile output. To see the full output, run the command
2006-10-16 04:21:44 +00:00
"\texttt { make V=99} ".
2006-11-06 23:37:55 +00:00
During the build process, buildroot will download all sources to the "\texttt { dl} "
2006-10-16 04:21:44 +00:00
directory and will start patching and compiling them in the "\texttt { build\_ \textit { <arch>} } "
directory. When finished, the resulting firmware will be in the "\texttt { bin} " directory
and packages will be in the "\texttt { bin/packages} " directory.
\subsection { Creating packages}
One of the things that we've attempted to do with OpenWrt's template system is make it
incredibly easy to port software to OpenWrt. If you look at a typical package directory
in OpenWrt you'll find two things:
\begin { itemize}
2006-11-06 23:37:55 +00:00
\item \texttt { package/\textit { <name>} /Makefile}
\item \texttt { package/\textit { <name>} /patches}
2007-01-05 15:26:38 +00:00
\item \texttt { package/\textit { <name>} /files}
2006-10-16 04:21:44 +00:00
\end { itemize}
The patches directory is optional and typically contains bug fixes or optimizations to
reduce the size of the executable. The package makefile is the important item, provides
the steps actually needed to download and compile the package.
2007-01-05 15:26:38 +00:00
The files directory is also optional and typicall contains package specific startup scripts or default configuration files that can be used out of the box with OpenWrt.
2006-10-16 04:21:44 +00:00
Looking at one of the package makefiles, you'd hardly recognize it as a makefile.
Through what can only be described as blatant disregard and abuse of the traditional
make format, the makefile has been transformed into an object oriented template which
simplifies the entire ordeal.
Here for example, is \texttt { package/bridge/Makefile} :
\begin { Verbatim} [frame=single,numbers=left]
2006-11-26 05:22:01 +00:00
#
# Copyright (C) 2006 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
# $ Id: Makefile 5624 2006 - 11 - 23 00 : 29 : 07 Z nbd $
2006-10-16 04:21:44 +00:00
include $ ( TOPDIR ) / rules.mk
PKG_ NAME:=bridge
PKG_ VERSION:=1.0.6
PKG_ RELEASE:=1
PKG_ SOURCE:=bridge-utils-$ ( PKG _ VERSION ) .tar.gz
PKG_ SOURCE_ URL:=@SF/bridge
PKG_ MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd
PKG_ CAT:=zcat
2006-11-26 05:22:01 +00:00
PKG_ BUILD_ DIR:=$ ( BUILD _ DIR ) / bridge - utils - $ (PKG_ VERSION)
2006-10-16 04:21:44 +00:00
include $ ( INCLUDE _ DIR ) / package.mk
define Package/bridge
2006-11-26 05:22:01 +00:00
SECTION:=net
CATEGORY:=Base system
2006-10-16 04:21:44 +00:00
TITLE:=Ethernet bridging configuration utility
2006-11-26 05:22:01 +00:00
DESCRIPTION:=\
Manage ethernet bridging: a way to connect networks together to \\ \
form a larger network.
2006-10-16 04:21:44 +00:00
URL:=http://bridge.sourceforge.net/
endef
define Build/Configure
2006-11-26 05:22:01 +00:00
$ ( call Build / Configure / Default, \
--with-linux-headers="$ ( LINUX _ DIR ) " \
)
2006-10-16 04:21:44 +00:00
endef
define Package/bridge/install
2006-11-26 05:22:01 +00:00
$ ( INSTALL _ DIR ) $ (1)/usr/sbin
$ ( INSTALL _ BIN ) $ (PKG_ BUILD_ DIR)/brctl/brctl $ ( 1 ) / usr / sbin /
2006-10-16 04:21:44 +00:00
endef
$ ( eval $ (call BuildPackage,bridge))
\end { Verbatim}
As you can see, there's not much work to be done; everything is hidden in other makefiles
and abstracted to the point where you only need to specify a few variables.
\begin { itemize}
2006-11-06 23:37:55 +00:00
\item \texttt { PKG\_ NAME} \\
The name of the package, as seen via menuconfig and ipkg
\item \texttt { PKG\_ VERSION} \\
2007-01-05 15:26:38 +00:00
The upstream version number that we are downloading
2006-11-06 23:37:55 +00:00
\item \texttt { PKG\_ RELEASE} \\
The version of this package Makefile
\item \texttt { PKG\_ SOURCE} \\
The filename of the original sources
\item \texttt { PKG\_ SOURCE\_ URL} \\
2007-01-05 15:26:38 +00:00
Where to download the sources from (no trailing slash), you can add multiple download sources by separating them with a \\ and a carriage return.
2006-11-06 23:37:55 +00:00
\item \texttt { PKG\_ MD5SUM} \\
A checksum to validate the download
\item \texttt { PKG\_ CAT} \\
How to decompress the sources (zcat, bzcat, unzip)
2006-11-26 05:22:01 +00:00
\item \texttt { PKG\_ BUILD\_ DIR} \\
Where to compile the package
2006-10-16 04:21:44 +00:00
\end { itemize}
The \texttt { PKG\_ * } variables define where to download the package from;
2006-11-26 21:28:16 +00:00
\texttt { @SF} is a special keyword for downloading packages from sourceforge. There is also
2007-01-05 15:26:38 +00:00
another keyword of \texttt { @GNU} for grabbing GNU source releases. If any of the above mentionned download source fails, the OpenWrt mirrors will be used as source.
2006-11-26 21:28:16 +00:00
2007-01-05 15:26:38 +00:00
The md5sum (if present) is used to verify the package was downloaded correctly and
2006-10-16 04:21:44 +00:00
\texttt { PKG\_ BUILD\_ DIR} defines where to find the package after the sources are
uncompressed into \texttt { \$ (BUILD\_ DIR)} .
At the bottom of the file is where the real magic happens, "BuildPackage" is a macro
2006-11-26 05:22:01 +00:00
set up by the earlier include statements. BuildPackage only takes one argument directly --
2006-10-16 04:21:44 +00:00
the name of the package to be built, in this case "\texttt { bridge} ". All other information
is taken from the define blocks. This is a way of providing a level of verbosity, it's
inherently clear what the contents of the \texttt { description} template in
\texttt { Package/bridge} is, which wouldn't be the case if we passed this information
directly as the Nth argument to \texttt { BuildPackage} .
\texttt { BuildPackage} uses the following defines:
\textbf { \texttt { Package/\textit { <name>} } :} \\
2006-11-06 23:37:55 +00:00
\texttt { \textit { <name>} } matches the argument passed to buildroot, this describes
the package the menuconfig and ipkg entries. Within \texttt { Package/\textit { <name>} }
you can define the following variables:
\begin { itemize}
\item \texttt { SECTION} \\
The type of package (currently unused)
\item \texttt { CATEGORY} \\
2007-01-05 15:26:38 +00:00
Which menu it appears in menuconfig : Network, Sound, Utilities, Multimedia ...
2006-11-06 23:37:55 +00:00
\item \texttt { TITLE} \\
A short description of the package
\item \texttt { URL} \\
Where to find the original software
\item \texttt { MAINTAINER} (optional) \\
Who to contact concerning the package
\item \texttt { DEPENDS} (optional) \\
2007-01-05 15:26:38 +00:00
Which packages must be built/installed before this package. To reference a dependency defined in the same Makefile, use \textit { <dependency name>} . If defined as an external package, use \textit { +<dependency name>} . For a kernel version dependency use: \textit { @LINUX\_ 2\_ <minor version>}
2006-11-06 23:37:55 +00:00
\end { itemize}
2006-10-16 04:21:44 +00:00
\textbf { \texttt { Package/\textit { <name>} /conffiles} (optional):} \\
A list of config files installed by this package, one file per line.
2006-11-06 23:37:55 +00:00
2006-10-16 04:21:44 +00:00
\textbf { \texttt { Build/Prepare} (optional):} \\
A set of commands to unpack and patch the sources. You may safely leave this
undefined.
\textbf { \texttt { Build/Configure} (optional):} \\
You can leave this undefined if the source doesn't use configure or has a
normal config script, otherwise you can put your own commands here or use
2007-01-05 15:26:38 +00:00
"\texttt { \$ (call Build/Configure/Default,\textit { <first list of arguments, second list>} )} " as above to
pass in additional arguments for a standard configure script. The first list of arguments will be passed to the configure script like that : $ - - arg 1 $ $ - - arg 2 $ . The second list contains arguments that should be defined before running the configure script such as autoconf or compiler specific variables.
2006-10-16 04:21:44 +00:00
\textbf { \texttt { Build/Compile} (optional):} \\
How to compile the source; in most cases you should leave this undefined.
\textbf { \texttt { Package/\textit { <name>} /install} :} \\
A set of commands to copy files out of the compiled source and into the ipkg
2006-11-26 05:22:01 +00:00
which is represented by the \texttt { \$ (1)} directory. Note that there are currently
2007-01-05 15:26:38 +00:00
4 defined install macros:
2006-11-26 05:22:01 +00:00
\begin { itemize}
\item \texttt { INSTALL\_ DIR} \\
install -d -m0755
\item \texttt { INSTALL\_ BIN} \\
install -m0755
\item \texttt { INSTALL\_ DATA} \\
install -m0644
2007-01-05 15:26:38 +00:00
\item \texttt { INSTALL\_ CONF} \\
install -m0600
2006-11-26 05:22:01 +00:00
\end { itemize}
2006-11-06 23:37:55 +00:00
2006-10-16 04:21:44 +00:00
The reason that some of the defines are prefixed by "\texttt { Package/\textit { <name>} } "
and others are simply "\texttt { Build} " is because of the possibility of generating
2006-11-06 23:37:55 +00:00
multiple packages from a single source. OpenWrt works under the assumption of one
2006-11-26 05:22:01 +00:00
source per package Makefile, but you can split that source into as many packages as
2006-11-06 23:37:55 +00:00
desired. Since you only need to compile the sources once, there's one global set of
2006-10-16 04:21:44 +00:00
"\texttt { Build} " defines, but you can add as many "Package/<name>" defines as you want
by adding extra calls to \texttt { BuildPackage} -- see the dropbear package for an example.
2007-01-05 15:26:38 +00:00
After you have created your \texttt { package/\textit { <name>} /Makefile} , the new package
2006-10-16 04:21:44 +00:00
will automatically show in the menu the next time you run "make menuconfig" and if selected
will be built automatically the next time "\texttt { make} " is run.
2006-11-26 21:28:16 +00:00
\subsection { Conventions}
There are a couple conventions to follow regarding packages:
\begin { itemize}
\item \texttt { files}
\begin { enumerate}
\item configuration files follow the convention \\
\texttt { \textit { <name>} .conf}
\item init files follow the convention \\
\texttt { \textit { <name>} .init}
\end { enumerate}
\item \texttt { patches}
\begin { enumerate}
\item patches are numerically prefixed and named related to what they do
\end { enumerate}
\end { itemize}
\subsection { Troubleshooting}
2006-10-16 04:21:44 +00:00
2006-11-06 23:37:55 +00:00
If you find your package doesn't show up in menuconfig, try the following command to
2006-10-16 04:21:44 +00:00
see if you get the correct description:
\begin { Verbatim}
TOPDIR=$ PWD make - C package / <name> DUMP = 1 V = 99
\end { Verbatim}
If you're just having trouble getting your package to compile, there's a few
shortcuts you can take. Instead of waiting for make to get to your package, you can
run one of the following:
\begin { itemize}
2006-11-06 23:37:55 +00:00
\item \texttt { make package/\textit { <name>} -clean V=99}
\item \texttt { make package/\textit { <name>} -install V=99}
2006-10-16 04:21:44 +00:00
\end { itemize}
Another nice trick is that if the source directory under \texttt { build\_ \textit { <arch>} }
is newer than the package directory, it won't clobber it by unpacking the sources again.
If you were working on a patch you could simply edit the sources under the
\texttt { build\_ \textit { <arch>} /\textit { <source>} } directory and run the install command above,
2006-11-06 23:37:55 +00:00
when satisfied, copy the patched sources elsewhere and diff them with the unpatched
2006-10-16 04:21:44 +00:00
sources. A warning though - if you go modify anything under \texttt { package/\textit { <name>} }
it will remove the old sources and unpack a fresh copy.
2006-11-26 05:22:01 +00:00
Other useful targets include:
\begin { itemize}
\item \texttt { make package/\textit { <name>} -prepare V=99}
\item \texttt { make package/\textit { <name>} -compile V=99}
\item \texttt { make package/\textit { <name>} -configure V=99}
\end { itemize}