1 Linux is now one of the most widespread operating system for embedded devices due to its openess as well as the wide variety of platforms it can run on. Many manufacturer actually use it in firmware you can find on many devices : DVB-T decoders, routers, print servers, DVD players ... Most of the time the stock firmware is not really open to the consumer, even if it uses open source software.
3 You might be interested in running a Linux based firmware for your router for various reasons : extending the use of a network protocol (such as IPv6), having new features, new piece of software inside, or for security reasons. A fully open-source firmware is de-facto needed for such applications, since you want to be free to use this or that version of a particular reason, be able to correct a particular bug. Few manufacturers do ship their routers with a Sample Developpment Kit, that would allow you to create your own and custom firmware and most of the time, when they do, you will most likely not be able to complete the firmware creation process.
5 This is one of the reasons why OpenWrt and other firmware exists : providing a version independent, and tools independent firmware, that can be run on various platforms, known to be running Linux originaly.
7 \subsection{Which Operating System does this device run ?
}
9 There is a lot of methods to ensure your device is running Linux. Some of them do need your router to be unscrewed and open, some can be done by probing the device using its external network interfaces.
11 \subsubsection{Operating System fingerprinting and port scanning
}
13 A large bunch of tools over the Internet exists in order to let you do OS fingerprinting, we will show here an example using
\textbf{nmap
} :
16 nmap -P0 -O <IP address>
17 Not shown:
1694 closed ports
22 Device type: general purpose
23 Running: Apple Mac OS X
10.4.X
24 OS details: Apple Mac OS X
10.4.8 (Tiger)
27 nmap is able to
report whether your device uses a Linux TCP/IP stack, and if so, will show you which Linux kernel version is probably runs. This
report is quite reliable and it can make the distinction between BSD and Linux TCP/IP stacks and others.
29 Using the same tool, you can also do port scanning and service version discovery. For instance, the following command will
report which IP-based services are running on the device, and which version of the service is being used :
32 nmap -P0 -sV <IP address>
36 The web server version, if identified, can be determining in knowing the Operating System. For instance, the
\textbf{BOA
} web server is typical from devices running an open-source Unix or Unix-like.
38 \subsubsection{Wireless Communications Fingerprinting
}
40 Although this method is not really known and widespread, using a wireless scanner to discover which OS your router or Access Point run can be used. We do not have a clear example of how this could be achieved, but you will have to monitor raw
802.11 frames and compare them to a very similar device running a Linux based firmware.
42 \subsubsection{Web server security exploits
}
44 The Linksys WRT54G was originally hacked by using a "ping bug" discoverd in the web interface. This tip has not been fixed for months by Linksys, allowing people to enable the "boot
\_wait" helper process via the web interface. Many web servers used in firmwares are open source web server, thus allowing the code to be audited to find an exploit. Once you know the web server version that runs on your device, by using
\textbf{nmap -sV
} or so, you might be interested in using exploits to reach shell access on your device.
46 \subsubsection{Native Telnet/SSH access
}
48 Some firmwares might have restricted or unrestricted Telnet/SSH access, if so, try to log in with the web interface login/password and see if you can type in some commands. This is actually the case for some Broadcom BCM963xx based firmwares such as the one in Neuf/Cegetel ISP routers, Club-Internet ISP CI-Box and many others. Some commands, like
\textbf{cat
} might be left here and be used to determine the Linux kernel version.
50 \subsubsection{Analysing a binary firmware image
}
52 You are very likely to find a firmware binary image on the manufacturer website, even if your device runs a proprietary operating system. If so, you can download it and use an hexadecimal editor to find printable words such as
\textbf{vmlinux
},
\textbf{linux
},
\textbf{ramdisk
},
\textbf{mtd
} and others.
54 Some Unix tools like
\textbf{hexdump
} or
\textbf{strings
} can be used to analyse the firmware. Below there is an example with a binary firmware found other the Internet :
57 hexdump -C <binary image.extension> | less (more)
58 00000000 46 49 52 45 32 2e
35 2e
30 00 00 00 00 00 00 00 |FIRE2.5
.0.......|
59 00000010 00 00 00 00 31 2e
30 2e
30 00 00 00 00 00 00 00 |...
.1.0.0.......|
60 00000020 00 00 00 00 00 00 00 38 00 43 36 29 00 0a e6 dc |......
.8.C6)..??|
61 00000030 54 49 44 45 92 89 54 66 1f
8b
08 08 f8
10 68 42 |TIDE..Tf....?.hB|
62 00000040 02 03 72 61 6d
64 69 73 6b
00 ec
7d
09 bc d5 d3 |..ramdisk.?
}.???|
63 00000050 da ff f3
9b f7
39 7b ef
73 f6
19 3b
53 67 ea
44 |???.?
9{?s?.;Sg?D|
66 Scroll over the firmware to find printable words that can be significant.
68 \subsubsection{Amount of flash memory
}
70 Linux can hardly fit in a
2MB flash device, once you have open the device and located the flash chip, try to find other the Internet its characteristics. If your flash chip is a
2MB or less device, your device is most likely to run a proprietary OS such as Windriver VxWorks, or a custom manufacturer OS like Zyxel ZynOS.
72 OpenWrt does not currently run on devices which have equal or less than
2MB of flash memory. This limitation will probably not be worked around since those devices are most of the time micro routers, or Wireless Access Points, which are not the main OpenWrt target.
74 \subsubsection{Pluging a serial port
}
76 By using a serial port, you may reach the console that is being shown by the device for debugging or flashing purposes. By analysing the output of this device, you can easily notice if the device uses a Linux kenrel or something different.
78 \subsection{Finding and using the manufacturer SDK
}
80 Once you are sure your device run a Linux based firmware, you will be able to start hacking on it. If the manufacturer respect the GPL, it will have release with the device, a Sample Developpment Kit.
82 \subsubsection{GPL violations
}
84 Some manufacturers do release a Linux based binary firmware, with no sources at all. The first step before doing anything is to read the license coming with your device, then write them about this lack of Open Source code. If the manufacturer answers you they do not have to release a SDK containing Open Source software, then we recommend you get in touch with the gpl-violations.org community.
86 You will find below a sample letter that can be sent to the manufacturer :
91 I am using a <device name>, and I cannot find neither on your website nor on the CD-ROM the open source software used to build or modify the firmware.
93 In conformance to the GPL license, you have to release the following sources :
95 - complete toolchain that made the kernel and applications be compiled (gcc, binutils, libc)
96 - tools to build a custom firmware (mksquashfs, mkcramfs ...)
97 - kernel sources with patches to make it run on this specific hardware, this does not include binary drivers
99 Thank you very much in advance for your answer.
101 Best regards, <your namne>
104 \subsubsection{Using the SDK
}
106 Once the SDK is available, you are most likely not to be able to build a complete or functionnal firmware using it, but parts of it, like only the kernel, or only the root filesystem. Most manufacturers do not really care releasing a tool that do work every time you uncompress and use it.
108 You should anyway be able to use the following components :
111 \item kernel sources with more or less functionnal patches for your hardware
112 \item binary drivers linked or to be linked with the shipped kernel version
113 \item packages of the toolchain used to compile the whole firmware : gcc, binutils, libc or uClibc
114 \item binary tools to create a valid firmware image
117 Your work is now divided into the following tasks :
120 \item create a clean patch of the hardware specific part of the linux kernel
121 \item spot potential kernel GPL violations especially on firewall and USB stack stuff
122 \item make the binary drivers work, until there are open source drivers
123 \item use standard a GNU toolchain to make working executables
124 \item understand and write open source tools to generate a valid firmware image
127 \subsubsection{Creating a hardware specific kernel patch
}
129 Most of the time, the kernel source that comes along with the SDK is not really clean, and is not a standard Linux version, it also has architecture specific fixes backported from the
\textbf{CVS
} or the
\textbf{git
} repository of the kernel developpment trees. Anyway, some parts can be easily isolated and used as a good start to make a vanilla kernel work your hardware.
131 Some directories are very likely to have local modifications needed to make your hardware be recognized and used under Linux. First of all, you need to find out the linux kernel version that is used by your hardware, this can be found by editing the
\textbf{linux/Makefile
} file.
134 head -
5 linux-
2.x.x/Makefile
139 NAME=Avast! A bilge rat!
142 So now, you know that you have to download a standard kernel tarball at
\textbf{kernel.org
} that matches the version being used by your hardware.
144 Then you can create a
\textbf{diff
} file between the two trees, especially for the following directories :
147 diff -urN linux-
2.x.x/arch/<sub architecture> linux-
2.x.x-modified/arch/<sub architecture> >
01-architecture.patch
148 diff -urN linux-
2.x.x/include/ linux-
2.x.x-modified/include >
02-includes.patch
149 diff -urN linux-
2.x.x/drivers/ linux-
2.x.x-modified/drivers >
03-drivers.patch
152 This will constitute a basic set of three patches that are very likely to contain any needed modifications that has been made to the stock Linux kernel to run on your specific device. Of course, the content produced by the
\textbf{diff -urN
} may not always be relevant, so that you have to clean up those patches to only let the "must have" code into them.
154 The fist patch will contain all the code that is needed by the board to be initialized at startup, as well as processor detection and other boot time specific fixes.
156 The second patch will contain all useful definitions for that board : adresses, kernel granularity, redifinitions, processor family and features ...
158 The third patch may contain drivers for : serial console, ethernet NIC, wireless NIC, USB NIC ... Most of the time this patch contains nothing else than "glue" code that has been added to make the binary driver work with the Linux kernel. This code might not be useful if you plan on writing from scratch drivers for this hardware.
160 \subsubsection{Making binary drivers work
}
162 As we have explained before, manufacturers do release binary drivers in their GPL tarball. When those drivers are statically linked into the kernel, they become GPL as well, fortunately or unfortunately, most of the drivers are not statically linked. This anyway lets you a chance to dynamically link the driver with the current kernel version, and try to make them work together.
164 This is one of the most tricky and grey part of the fully open source projects. Some drivers require few modifications to be working with your custom kernel, because they worked with an earlier kernel, and few modifications have been made to the kernel in-between those versions. This is for instance the case with the binary driver of the Broadcom BCM43xx Wireless Chipsets, where only few differences were made to the network interface structures.
166 Some general principles can be applied no matter which kernel version is used in order to make binary drivers work with your custom kernel :
169 \item turn on kernel debugging features such as :
171 \item CONFIG
\_DEBUG\_KERNEL
172 \item CONFIG
\_DETECT\_SOFTLOCKUP
173 \item CONFIG
\_DEBUG\_KOBJECT
174 \item CONFIG
\_EMBEDDED
175 \item CONFIG
\_KALLSYMS
176 \item CONFIG
\_KALLSYMS\_ALL
178 \item link binary drivers when possible to the current kernel version
179 \item try to load those binary drivers
180 \item catch the lockups and understand them
183 Most of the time, loading binary drivers will fail, and generate a kernel oops. You can know the last symbol the binary drivers attempted to use, and see in the kernel headers file, if you do not have to move some structures field before or after that symbol in order to keep compatibily with both the binary driver and the stock kernel drivers.
185 \subsubsection{Understanding the firmware format
}
187 You might want to understand the firmware format, even if you are not yet capable of running a custom firmware on your device, because this is sometimes a blocking part of the flashing process.
189 A firmare format is most of the time composed of the following fields :
192 \item header, containing a firmare version and additionnal fields : Vendor, Hardware version ...
193 \item CRC32 checksum on either the whole file or just part of it
194 \item Binary or compressed kernel image
195 \item Binary or compressed root filesystem image
196 \item potential garbage
199 Once you have figured out how the firmware format is partitionned, you will have to write your own tool that produces valid firmare binaries. One thing to be very careful here is the endianness of either the machine that produces the binary firmware and the device that will be flashed using this binary firmware.
201 \subsubsection{Writing a flash map driver
}
203 The flash map driver has an important role in making your custom firmware work because it is responsible of mapping the correct flash regions and associated rights to specific parts of the system such as : bootloader, kernel, user filesystem.
205 Writing your own flash map driver is not really a hard task once you know how your firmware image and flash is structured. You will find below a commented example that covers the case of the device where the bootloader can pass to the kernel its partition plan.
207 First of all, you need to make your flash map driver be visible in the kernel configuration options, this can be done by editing the file
\textbf{linux/drivers/mtd/maps/Kconfig
} :
210 config MTD_DEVICE_FLASH
211 tristate "Device Flash device"
212 depends on ARCHITECTURE && DEVICE
214 Flash memory access on DEVICE boards. Currently only works with
215 Bootloader Foo and Bootloader Bar.
218 Then add your source file to the
\textbf{linux/drivers/mtd/maps/Makefile
}, so that it will be compiled along with the kernel.
221 obj-\$(CONFIG_MTD_DEVICE_FLASH) += device-flash.o
224 You can then write the kernel driver itself, by creating a
\textbf{linux/drivers/mtd/maps/device-flash.c
} C source file.
227 // Includes that are required for the flash map driver to know of the prototypes :
229 #include <linux/init.h>
230 #include <linux/kernel.h>
231 #include <linux/mtd/map.h>
232 #include <linux/mtd/mtd.h>
233 #include <linux/mtd/partitions.h>
234 #include <linux/vmalloc.h>
236 // Put some flash map definitions here :
237 #define WINDOW_ADDR
0x1FC00000 /* Real address of the flash */
238 #define WINDOW_SIZE
0x400000 /* Size of flash */
239 #define BUSWIDTH
2 /* Buswidth */
241 static void __exit device_mtd_cleanup(void);
243 static struct mtd_info *device_mtd_info;
245 static struct map_info devicd_map =
{
248 .bankwidth = BUSWIDTH,
252 static int __init device_mtd_init(void)
254 // Display that we found a flash map device
255 printk("device:
0x\%
08x at
0x\%
08x
\n", WINDOW_SIZE, WINDOW_ADDR);
256 // Remap the device address to a kernel address
257 device_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
259 // If impossible to remap, exit with the EIO error
260 if (!device_map.virt)
{
261 printk("device: Failed to ioremap
\n");
265 // Initlialise the device map
266 simple_map_init(&device_map);
268 /* MTD informations are closely linked to the flash map device
269 you might also use "jedec_probe" "amd_probe" or "intel_probe" */
270 device_mtd_info = do_map_probe("cfi_probe", &device_map);
272 if (device_mtd_info)
{
273 device_mtd_info->owner = THIS_MODULE;
275 int parsed_nr_parts =
0;
277 // We try here to use the partition schema provided by the bootloader specific code
278 if (parsed_nr_parts ==
0)
{
279 int ret = parse_bootloader_partitions(device_mtd_info, &parsed_parts,
0);
281 part_type = "BootLoader";
282 parsed_nr_parts = ret;
286 add_mtd_partitions(devicd_mtd_info, parsed_parts, parsed_nr_parts);
290 iounmap(device_map.virt);
295 // This function will make the driver clean up the MTD device mapping
296 static void __exit device_mtd_cleanup(void)
298 // If we found a MTD device before
299 if (device_mtd_info)
{
300 // Delete every partitions
301 del_mtd_partitions(device_mtd_info);
302 // Delete the associated map
303 map_destroy(device_mtd_info);
306 // If the virtual address is already in use
307 if (device_map.virt)
{
308 // Unmap the physical address to a kernel space address
309 iounmap(device_map.virt);
310 // Reset the structure field
316 // Macros that indicate which function is called on loading/unloading the module
317 module_init(device_mtd_init);
318 module_exit(device_mtd_cleanup);
321 // Macros defining licence and author, parameters can be defined here too.
322 MODULE_LICENSE("GPL");
323 MODULE_AUTHOR("Me, myself and I <memyselfandi@domain.tld");