build.tex 21 KB

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