%include "default.mgp" %default 1 bgrad 0 0 256 0 1 "black" "blue" %page %nodefault %center, size 7, font "standard", fore "white", vgap 20 Intro to Embedded Linux Programming %center, size 5, font "standard", fore "white" SCALE2X Los Angeles 24 November 2003 %center, size 7, font "standard", fore "white", vgap 20 Dan Kegel www.kegel.com www.lalugs.org %page Intro to Embedded Linux Programming What's an Embedded System? Embedded Linux in Wide Use Today Why Linux? Cramming Linux into Limited RAM Cross-Compiling Logging In Running Your Program Installing a Crosscompiler on Your PC Autoconf Dealing with Subset C libraries Building Embedded Linux from Scratch Integrated Development Environments (IDEs) Summary / Links %page What's an Embedded System? The control system for a router, cellphone, car, or TV Must be small, lightweight, low power, cheap Often use non-x86 chips According to a LinuxDevices.com developer poll in 2002: 32% x86 27% Arm (Linksys) 16% PowerPC (TiVo 1) 9% MIPS (Linksys, TiVo 2) 5% SH (Dreamcast) 4% Coldfire (Snapgear) %page Embedded Linux in Wide Use Today Many devices officially support running Linux programs Upcoming cellphones from Motorola and Samsung (Qt!) Handheld barcode scanners Ixia network testers Others use Linux internally, but don't let you run programs The TiVo personal video recorder Linksys routers %page Why Linux? Highly reliable, customizable, royalty-free Huge base of existing software New protocols often developed for Linux first Open Source means you can fix problems yourself Continuous rapid improvement Linux is the OS of choice for new embedded systems %page Cramming Linux into Limited RAM On systems with > 64 megabytes RAM: RAM is plentiful, can use glibc, bash, libstdc++, etc. On systems with < 16 megabytes RAM: RAM is scarce, must use uclibc, busybox, no libstdc++ Can also use an older version of glibc, or omit unused parts May need to rewrite software a bit (e.g. use libhttp instead of libcurl, or nonblocking I/O instead of threads) %page Cross-Compiling Crosscompiler generates code for alien CPU Often needs special compiler options $ target=arm-unknown-linux-gnu $ xcflags="-mcpu=arm7 -D__ARM7__" $ PATH=/opt/$target/gcc-2.95.3-glibc-2.1.3/bin:$PATH $ $target-gcc $xcflags hello.c Good Makefiles let you cross-compile like this: $ make CC=$target-gcc EXTRA_CFLAGS=$xcflags but read the Makefile to see what variables to set %page Logging In A serial line is good; handy for debugging boot problems Ethernet better than serial line for most purposes If vendor doc not handy, use nmap to see if target is running a telnet, rlogin, or ssh server Most embedded systems don't even have a password on the root account! Worst case: If target has security hole, an exploit script may be available %page Running Your Program If target is running a ftp, ssh, or rsh server, use ftp, scp, or rcp on pc to transfer files via ethernet If target has ftp, tftp, wget, or scp in /bin or /usr/bin, use them on target to transfer files via ethernet If target has /bin/rz, use sz to transfer files via serial line Worst case: use exploit script to transfer file to target, or if target boots off IDE drive, mount drive on your PC Then run as usual, e.g. # chmod +x a.out; ./a.out %page Installing a Crosscompiler on Your PC Determine CPU Determine C Library Build Toolchain %page Installing a Crosscompiler on Your PC : Determine CPU Log into device and identify CPU e.g. on WRT54G: # uname -m mips # grep cpu /proc/cpuinfo cpu : MIPS cpu model : BCM4710 V0.0 %page Installing a Crosscompiler on Your PC : Determine C Library Log into device and identify C library e.g. on AML-7100 handheld barcode scanner: # ls /lib/libc-*.so libc-2.1.3.so e.g. on WRT54G: # strings /lib/libc.so.6 | grep GLIBC GLIBC_2.2.3 %page Installing a Crosscompiler on Your PC : Build Toolchain One easy way: $ wget www.kegel.com/crosstool/crosstool-0.26.tar.gz $ tar -xzvf crosstool-0.26.tar.gz $ cd crosstool-0.26 $ sh demo-mipsel.sh This creates /opt/crosstool/ mipsel-unknown-linux-gnu/gcc-3.2.3-glibc-2.2.3/bin Edit demo-mipsel.sh to pick different cpu, gcc, or glibc Join crossgcc mailing list for help %page Autoconf Many open source packages use Autoconf for portability Maintainer uses autoconf to generate configure script User runs configure script to generate Makefile $ ./configure --target=mipsel-unknown-linux-gnu Older configure scripts require overriding CC, e.g. $ CC=$target-gcc ./configure ... Some configure scripts need hints, e.g. $ ac_cv_func_setpgrp_void=yes ./configure ... It pays to understand configure scripts Join the crossgcc or autoconf mailing lists for help %page Dealing with Subset C libraries If running a program just prints an error like symbol gethostbyaddr not defined in file libc.so.6 some functions may have been left out of the C library to save space Three workarounds: Don't call those functions Link with -static Write a stub library and link to that %page Building Embedded Linux from Scratch Cross-compiling not standardized; each package different Best practices dictate that you script the cross-compile Laziness dictates that you use someone else's scripts :-) Ptxdist is a Makefile that knows how to cross-compile about 100 packages. It uses "make menuconfig" to select which apps to build. See http://ptxdist.sf.net Remoting environments like Scratchbox or Dodes let you build without worrying about cross-compiling See http://scratchbox.org, http://www.dodes.org %page Integrated Development Environments (IDEs) Programmers used to Microsoft Visual Studio want something similar, are intimidated by Linux commandline Industry moving towards Eclipse; see http://eclipse.org Other options include kdevelop, emacs ptxdist and scratchbox eventually available as IDE plugins Real men use vi :-) %page Summary Embedded Linux is becoming ubiquitous Embedded Linux is just like regular Linux, but slimmer Free tools are available and 'good enough' in right hands Cross-compiling third-party apps has steep learning curve Tools like ptxdist and scratchbox try to ease learning curve IDEs are coming %page Links Slides: kegel.com/linux/embed Crosstool: kegel.com/crosstool ptxdist: ptxdist.sf.net Scratchbox: scratchbox.org Dodes: dodes.org Hacking the Linksys WRT54g Router http://seattlewireless.net/index.cgi/LinksysWrt54g http://www.batbox.org/wrt54g-linux.html Hacking TiVo, by Jeff Keegan http://keegan.org/tivobook %page Legal stuff Copyright (C) 2003, Dan Kegel All rights reserved