553ddb16e0b4a78b91581439ae5b1e6d9b3a5cef
1 One of the biggest challenges to getting started with embedded devices is that you
2 cannot just install a copy of Linux and expect to be able to compile a firmware.
3 Even if you did remember to install a compiler and every development tool offered,
4 you still would not have the basic set of tools needed to produce a firmware image.
5 The embedded device represents an entirely new hardware platform, which is
6 most of the time incompatible with the hardware on your development machine, so in a process called
7 cross compiling you need to produce a new compiler capable of generating code for
8 your embedded platform, and then use it to compile a basic Linux distribution to
11 The process of creating a cross compiler can be tricky, it is not something that is
12 regularly attempted and so there is a certain amount of mystery and black magic
13 associated with it. In many cases when you are dealing with embedded devices you will
14 be provided with a binary copy of a compiler and basic libraries rather than
15 instructions for creating your own -- it is a time saving step but at the same time
16 often means you will be using a rather dated set of tools. Likewise, it is also common
17 to be provided with a patched copy of the Linux kernel from the board or chip vendor,
18 but this is also dated and it can be difficult to spot exactly what has been
19 modified to make the kernel run on the embedded platform.
21 \subsection{Building an image}
24 and compiling everything from scratch, including the cross compiler. To put it
25 in simpler terms, OpenWrt does not contain any executables or even sources, it is an
27 platform and compiling them correctly for that platform. What this means is that
28 just by changing the template, you can change any step in the process.
30 As an example, if a new kernel is released, a simple change to one of the Makefiles
31 will download the latest kernel, patch it to run on the embedded platform and produce
32 a new firmware image -- there is no work to be done trying to track down an unmodified
33 copy of the existing kernel to see what changes had been made, the patches are
34 already provided and the process ends up almost completely transparent. This does not
35 just apply to the kernel, but to anything included with OpenWrt -- It is this one
36 simple understated concept which is what allows OpenWrt to stay on the bleeding edge
37 with the latest compilers, latest kernels and latest applications.
39 So let's take a look at OpenWrt and see how this all works.
45 subversion using the following command:
47 \begin{Verbatim}
48 $svn co https://svn.openwrt.org/openwrt/trunk kamikaze 49 \end{Verbatim} 51 Additionally, ther is a trac interface on \href{https://dev.openwrt.org/}{https://dev.openwrt.org/} 52 which can be used to monitor svn commits and browse the sources. 55 \subsubsection{The directory structure} 57 There are four key directories in the base: 59 \begin{itemize} 60 \item \texttt{tools} 61 \item \texttt{toolchain} 62 \item \texttt{package} 63 \item \texttt{target} 64 \end{itemize} 66 \texttt{tools} and \texttt{toolchain} refer to common tools which will be 67 used to build the firmware image, the compiler, and the C library. 68 The result of this is three new directories, \texttt{tool\_build}, which is a temporary 69 directory for building the target independent tools, \texttt{toolchain\_build\_\textit{<arch>}} 70 which is used for building the toolchain for a specific architecture, and 71 \texttt{staging\_dir\_\textit{<arch>}} where the resulting toolchain is installed. 72 You will not need to do anything with the toolchain directory unless you intend to 73 add a new version of one of the components above. 75 \begin{itemize} 76 \item \texttt{tool\_build} 77 \item \texttt{toolchain\_build\_\textit{<arch>}} 78 \end{itemize} 80 \texttt{package} is for exactly that -- packages. In an OpenWrt firmware, almost everything 81 is an \texttt{.ipk}, a software package which can be added to the firmware to provide new 82 features or removed to save space. Note that packages are also maintained outside of the main 83 trunk and can be obtained from subversion at the following location: 85 \begin{Verbatim} 86$ svn co https://svn.openwrt.org/openwrt/packages ../packages
87 \end{Verbatim}
89 Those packages can be used to extend the functionality of the build system and need to be
90 symlinked into the main trunk. Once you do that, the packages will show up in the menu for
91 configuration. From kamikaze you would do something like this:
93 \begin{Verbatim}
94 $ls 95 kamikaze packages 96$ ln -s packages/net/nmap kamikaze/package/nmap
97 \end{Verbatim}
99 To include all packages, issue the following command:
101 \begin{Verbatim}
102 $ln -s packages/*/* kamikaze/package/ 103 \end{Verbatim} 106 \texttt{target} refers to the embedded platform, this contains items which are specific to 107 a specific embedded platform. Of particular interest here is the "\texttt{target/linux}" 108 directory which is broken down by platform and contains the kernel config and patches 109 to the kernel for a particular platform. There's also the "\texttt{target/image}" directory 110 which describes how to package a firmware for a specific platform. 112 Both the target and package steps will use the directory "\texttt{build\_\textit{<arch>}}" 113 as a temporary directory for compiling. Additionally, anything downloaded by the toolchain, 114 target or package steps will be placed in the "\texttt{dl}" directory. 116 \begin{itemize} 117 \item \texttt{build\_\textit{<arch>}} 118 \item \texttt{dl} 119 \end{itemize} 121 \subsubsection{Building OpenWrt} 123 While the OpenWrt build environment was intended mostly for developers, it also has to be 124 simple enough that an inexperienced end user can easily build his or her own customized firmware. 126 Running the command "\texttt{make menuconfig}" will bring up OpenWrt's configuration menu 127 screen, through this menu you can select which platform you're targeting, which versions of 128 the toolchain you want to use to build and what packages you want to install into the 129 firmware image. Note that it will also check to make sure you have the basic dependencies for it 130 to run correctly. If that fails, you will need to install some more tools in your local environment 131 before you can begin. 133 Similar to the linux kernel config, almost every option has three choices, 134 \texttt{y/m/n} which are represented as follows: 136 \begin{itemize} 137 \item{\texttt{<*>} (pressing y)} \\ 138 This will be included in the firmware image 139 \item{\texttt{<M>} (pressing m)} \\ 140 This will be compiled but not included (for later install) 141 \item{\texttt{< >} (pressing n)} \\ 142 This will not be compiled 143 \end{itemize} 145 After you've finished with the menu configuration, exit and when prompted, save your 146 configuration changes. 148 If you want, you can also modify the kernel config for the selected target system. 149 simply run "\texttt{make kernel\_menuconfig}" and the build system will unpack the kernel sources 150 (if necessary), run menuconfig inside of the kernel tree, and then copy the kernel config 151 to \texttt{target/linux/\textit{<platform>}/config} so that it is preserved over 152 "\texttt{make clean}" calls. 154 To begin compiling the firmware, type "\texttt{make}". By default 155 OpenWrt will only display a high level overview of the compile process and not each individual 156 command. 158 \subsubsection{Example:} 160 \begin{Verbatim} 161 make[2] toolchain/install 162 make[3] -C toolchain install 163 make[2] target/compile 164 make[3] -C target compile 165 make[4] -C target/utils prepare 167 [...] 168 \end{Verbatim} 170 This makes it easier to monitor which step it's actually compiling and reduces the amount 171 of noise caused by the compile output. To see the full output, run the command 172 "\texttt{make V=99}". 174 During the build process, buildroot will download all sources to the "\texttt{dl}" 175 directory and will start patching and compiling them in the "\texttt{build\_\textit{<arch>}}" 176 directory. When finished, the resulting firmware will be in the "\texttt{bin}" directory 177 and packages will be in the "\texttt{bin/packages}" directory. 180 \subsection{Creating packages} 182 One of the things that we've attempted to do with OpenWrt's template system is make it 183 incredibly easy to port software to OpenWrt. If you look at a typical package directory 184 in OpenWrt you'll find two things: 186 \begin{itemize} 187 \item \texttt{package/\textit{<name>}/Makefile} 188 \item \texttt{package/\textit{<name>}/patches} 189 \item \texttt{package/\textit{<name>}/files} 190 \end{itemize} 192 The patches directory is optional and typically contains bug fixes or optimizations to 193 reduce the size of the executable. The package makefile is the important item, provides 194 the steps actually needed to download and compile the package. 196 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. 198 Looking at one of the package makefiles, you'd hardly recognize it as a makefile. 199 Through what can only be described as blatant disregard and abuse of the traditional 200 make format, the makefile has been transformed into an object oriented template which 201 simplifies the entire ordeal. 203 Here for example, is \texttt{package/bridge/Makefile}: 205 \begin{Verbatim}[frame=single,numbers=left] 206 # 207 # Copyright (C) 2006 OpenWrt.org 208 # 209 # This is free software, licensed under the GNU General Public License v2. 210 # See /LICENSE for more information. 211 # 212 #$Id: Makefile 5624 2006-11-23 00:29:07Z nbd $214 include$(TOPDIR)/rules.mk
216 PKG_NAME:=bridge
217 PKG_VERSION:=1.0.6
218 PKG_RELEASE:=1
220 PKG_SOURCE:=bridge-utils-$(PKG_VERSION).tar.gz 221 PKG_SOURCE_URL:=@SF/bridge 222 PKG_MD5SUM:=9b7dc52656f5cbec846a7ba3299f73bd 223 PKG_CAT:=zcat 225 PKG_BUILD_DIR:=$(BUILD_DIR)/bridge-utils-$(PKG_VERSION) 227 include$(INCLUDE_DIR)/package.mk
229 define Package/bridge
230 SECTION:=net
231 CATEGORY:=Base system
232 TITLE:=Ethernet bridging configuration utility
233 DESCRIPTION:=\
234 Manage ethernet bridging: a way to connect networks together to \\\
235 form a larger network.
236 URL:=http://bridge.sourceforge.net/
237 endef
239 define Build/Configure
240 $(call Build/Configure/Default, \ 241 --with-linux-headers="$(LINUX_DIR)" \
242 )
243 endef
245 define Package/bridge/install
246 $(INSTALL_DIR)$(1)/usr/sbin
247 $(INSTALL_BIN)$(PKG_BUILD_DIR)/brctl/brctl $(1)/usr/sbin/ 248 endef 250$(eval $(call BuildPackage,bridge)) 251 \end{Verbatim} 253 As you can see, there's not much work to be done; everything is hidden in other makefiles 254 and abstracted to the point where you only need to specify a few variables. 256 \begin{itemize} 257 \item \texttt{PKG\_NAME} \\ 258 The name of the package, as seen via menuconfig and ipkg 259 \item \texttt{PKG\_VERSION} \\ 260 The upstream version number that we are downloading 261 \item \texttt{PKG\_RELEASE} \\ 262 The version of this package Makefile 263 \item \texttt{PKG\_SOURCE} \\ 264 The filename of the original sources 265 \item \texttt{PKG\_SOURCE\_URL} \\ 266 Where to download the sources from (no trailing slash), you can add multiple download sources by separating them with a \\ and a carriage return. 267 \item \texttt{PKG\_MD5SUM} \\ 268 A checksum to validate the download 269 \item \texttt{PKG\_CAT} \\ 270 How to decompress the sources (zcat, bzcat, unzip) 271 \item \texttt{PKG\_BUILD\_DIR} \\ 272 Where to compile the package 273 \end{itemize} 275 The \texttt{PKG\_*} variables define where to download the package from; 276 \texttt{@SF} is a special keyword for downloading packages from sourceforge. There is also 277 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. 279 The md5sum (if present) is used to verify the package was downloaded correctly and 280 \texttt{PKG\_BUILD\_DIR} defines where to find the package after the sources are 281 uncompressed into \texttt{\$(BUILD\_DIR)}.
283 At the bottom of the file is where the real magic happens, "BuildPackage" is a macro
284 set up by the earlier include statements. BuildPackage only takes one argument directly --
285 the name of the package to be built, in this case "\texttt{bridge}". All other information
286 is taken from the define blocks. This is a way of providing a level of verbosity, it's
287 inherently clear what the contents of the \texttt{description} template in
288 \texttt{Package/bridge} is, which wouldn't be the case if we passed this information
289 directly as the Nth argument to \texttt{BuildPackage}.
291 \texttt{BuildPackage} uses the following defines:
293 \textbf{\texttt{Package/\textit{<name>}}:} \\
294 \texttt{\textit{<name>}} matches the argument passed to buildroot, this describes
295 the package the menuconfig and ipkg entries. Within \texttt{Package/\textit{<name>}}
296 you can define the following variables:
298 \begin{itemize}
299 \item \texttt{SECTION} \\
300 The type of package (currently unused)
301 \item \texttt{CATEGORY} \\
302 Which menu it appears in menuconfig: Network, Sound, Utilities, Multimedia ...
303 \item \texttt{TITLE} \\
304 A short description of the package
305 \item \texttt{URL} \\
306 Where to find the original software
307 \item \texttt{MAINTAINER} (optional) \\
308 Who to contact concerning the package
309 \item \texttt{DEPENDS} (optional) \\
310 Which packages must be built/installed before this package. To reference a dependency defined in the
311 same Makefile, use \textit{<dependency name>}. If defined as an external package, use
312 \textit{+<dependency name>}. For a kernel version dependency use: \textit{@LINUX\_2\_<minor version>}
313 \end{itemize}
315 \textbf{\texttt{Package/\textit{<name>}/conffiles} (optional):} \\
316 A list of config files installed by this package, one file per line.
318 \textbf{\texttt{Build/Prepare} (optional):} \\
319 A set of commands to unpack and patch the sources. You may safely leave this
320 undefined.
322 \textbf{\texttt{Build/Configure} (optional):} \\
323 You can leave this undefined if the source doesn't use configure or has a
324 normal config script, otherwise you can put your own commands here or use
325 "\texttt{\$(call Build/Configure/Default,\textit{<first list of arguments, second list>})}" as above to 326 pass in additional arguments for a standard configure script. The first list of arguments will be passed 327 to the configure script like that: \texttt{--arg 1} \texttt{--arg 2}. The second list contains arguments that should be 328 defined before running the configure script such as autoconf or compiler specific variables. 330 To make it easier to modify the configure command line, you can either extend or completely override the following variables: 331 \begin{itemize} 332 \item \texttt{CONFIGURE\_ARGS} \\ 333 Contains all command line arguments (format: \texttt{--arg 1} \texttt{--arg 2}) 334 \item \texttt{CONFIGURE\_VARS} \\ 335 Contains all environment variables that are passed to ./configure (format: \texttt{NAME="value"}) 336 \end{itemize} 338 \textbf{\texttt{Build/Compile} (optional):} \\ 339 How to compile the source; in most cases you should leave this undefined. 341 \textbf{\texttt{Package/\textit{<name>}/install}:} \\ 342 A set of commands to copy files out of the compiled source and into the ipkg 343 which is represented by the \texttt{\$(1)} directory. Note that there are currently
344 4 defined install macros:
345 \begin{itemize}
346 \item \texttt{INSTALL\_DIR} \\
347 install -d -m0755
348 \item \texttt{INSTALL\_BIN} \\
349 install -m0755
350 \item \texttt{INSTALL\_DATA} \\
351 install -m0644
352 \item \texttt{INSTALL\_CONF} \\
353 install -m0600
354 \end{itemize}
356 The reason that some of the defines are prefixed by "\texttt{Package/\textit{<name>}}"
357 and others are simply "\texttt{Build}" is because of the possibility of generating
358 multiple packages from a single source. OpenWrt works under the assumption of one
359 source per package Makefile, but you can split that source into as many packages as
360 desired. Since you only need to compile the sources once, there's one global set of
361 "\texttt{Build}" defines, but you can add as many "Package/<name>" defines as you want
362 by adding extra calls to \texttt{BuildPackage} -- see the dropbear package for an example.
364 After you have created your \texttt{package/\textit{<name>}/Makefile}, the new package
365 will automatically show in the menu the next time you run "make menuconfig" and if selected
366 will be built automatically the next time "\texttt{make}" is run.
369 \subsection{Conventions}
371 There are a couple conventions to follow regarding packages:
373 \begin{itemize}
374 \item \texttt{files}
375 \begin{enumerate}
376 \item configuration files follow the convention \\
377 \texttt{\textit{<name>}.conf}
378 \item init files follow the convention \\
379 \texttt{\textit{<name>}.init}
380 \end{enumerate}
381 \item \texttt{patches}
382 \begin{enumerate}
383 \item patches are numerically prefixed and named related to what they do
384 \end{enumerate}
385 \end{itemize}
387 \subsection{Troubleshooting}
389 If you find your package doesn't show up in menuconfig, try the following command to
390 see if you get the correct description:
392 \begin{Verbatim}
393 TOPDIR=\$PWD make -C package/<name> DUMP=1 V=99
394 \end{Verbatim}
396 If you're just having trouble getting your package to compile, there's a few
397 shortcuts you can take. Instead of waiting for make to get to your package, you can
398 run one of the following:
400 \begin{itemize}
401 \item \texttt{make package/\textit{<name>}-clean V=99}
402 \item \texttt{make package/\textit{<name>}-install V=99}
403 \end{itemize}
405 Another nice trick is that if the source directory under \texttt{build\_\textit{<arch>}}
406 is newer than the package directory, it won't clobber it by unpacking the sources again.
407 If you were working on a patch you could simply edit the sources under the
408 \texttt{build\_\textit{<arch>}/\textit{<source>}} directory and run the install command above,
409 when satisfied, copy the patched sources elsewhere and diff them with the unpatched
410 sources. A warning though - if you go modify anything under \texttt{package/\textit{<name>}}
411 it will remove the old sources and unpack a fresh copy.
413 Other useful targets include:
415 \begin{itemize}
416 \item \texttt{make package/\textit{<name>}-prepare V=99}
417 \item \texttt{make package/\textit{<name>}-compile V=99}
418 \item \texttt{make package/\textit{<name>}-configure V=99}
419 \end{itemize}