Following Unix tradition, every object in a Linux system is visible as a file, including devices.[5] All the device files (a.k.a. device "nodes") in a Linux root filesystem are located in the /dev directory. Most workstation and server distributions come packaged with a /dev directory containing more than 2,000 entries to account for all the possible system variations. Because embedded Linux systems are custom built, there is no need to fill the target's /dev directory with as many entries as a Linux workstation or server. Only the entries required for the system's proper operation should be created.
[5] The notable exception to this is networking interfaces, such as Ethernet cards, for which there are no device files.
Identifying which entries you need can be difficult if you don't have the required information. If you choose to use devfs instead of creating fixed static device entries, you will avoid having to look for the device information. Devfs has not been widely adopted, however, and static device entries are still the norm.
The official source of information for static device major and minor numbers is the Documentation/devices.txt file in the kernel sources. You can consult this file whenever you are uncertain about the name or numbering of a certain device.
Table 6-3 lists the most basic entries you will need in your /dev directory. Depending on your particular setup, you will probably need to add a few extra entries. In some cases, you may even need to use entries other than the ones listed below. On some systems, for example, the first serial port is not ttyS0. Such is the case of SuperH-based systems, for instance, where the first serial port is ttySC0 (major number: 204, minor number: 8), and StrongARM-based systems where the first serial port is ttySA0 (major number: 204, minor number: 5).
Filename |
Description |
Type |
Major number |
Minor number |
Permission bits |
---|---|---|---|---|---|
mem |
Physical memory access |
char |
1 |
1 |
600 |
null |
Null device |
char |
1 |
3 |
666 |
zero |
Null byte source |
char |
1 |
5 |
666 |
random |
Nondeterministic random number generator |
char |
1 |
8 |
644 |
tty0 |
Current virtual console |
char |
4 |
0 |
600 |
tty1 |
First virtual console |
char |
4 |
1 |
600 |
ttyS0 |
First UART serial port |
char |
4 |
64 |
600 |
tty |
Current TTY device |
char |
5 |
0 |
666 |
console |
System console |
char |
5 |
1 |
600 |
Chapter 6 of Running Linux explains how to create device files. Essentially, you will need to use the mknod command for each entry to be created. In contrast to most other commands we have used up until now, you will need to be logged in as root to use this command. Remember to log out from the root user mode once you are done creating the device files.
Here is a simple example showing the creation of the first few entries in Table 6-3:
$ cd ${PRJROOT}/rootfs/dev $ su -m Password: # mknod -m 600 mem c 1 1 # mknod -m 666 null c 1 3 # mknod -m 666 zero c 1 5 # mknod -m 644 random c 1 8 ... # exit
In addition to the basic device files, there are a few compulsory symbolic links that have to be part of your /dev directory. Table 6-4 provides a description of these symbolic links. As with other symbolic links, you can use the ln -s command to create these links.
Link name |
Target |
---|---|
fd |
/proc/self/fd |
stdin |
fd/0 |
stdout |
fd/1 |
stderr |
fd/2 |
We have now prepared a basic /dev directory for our target. We will come back to this directory later to create some additional entries for some types of storage devices. You can consult Linux Device Drivers for a more complete discussion about device files and device drivers in general.
Automated Creation of /dev EntriesThe creation tools of some filesystems, such as JFFS2 and CRAMFS, have been extended by Erik Andersen to allow the creation of /dev entries on the fly using a device table file. With such a file, it is no longer necessary to log in as root and use the mknod command to create entries in your target's root filesystem. Instead, the file creation tool parses the device table file and creates the entries while it builds the rest of the filesystem without requiring root login. Support for JFFS2 device table files is already part of the MTD tools package, which includes the mkfs.jffs2 command. Support for CRAMFS device table files is available in the form of a patch to be applied to the CRAMFS source package. The patch and the latest CRAMFS filesystem creation code are available at http://sourceforge.net/projects/cramfs/. I will not detail the use of device table files, since they can only be used with a very limited number of Linux filesystems at the time of this writing. Their format and their use are, however, fairly well explained in the device_table.txt file provided in both the MTD tools package and the CRAMFS patch. We will, nevertheless, discuss the MTD tools in Chapter 7 and the JFFS2 and CRAMFS filesystems in Chapter 8. |