Hacking iPod and iTunes
Pages: 1, 2, 3, 4, 5

moderate

Hack #49 Run Linux on Your iPod

Turn your iPod into a Linux-running machine and write your own iPod applications.



Ever since the original iPod was released, users have come up with new and interesting ways to use their iPods as more than just MP3 players. Apple’s various updates have added to the firmware new features, such as calendars and contacts [Hack #33], games[Hack #36], HTML-like notes [Hack #39], and now photo storage capabilities [Hack #3].

Unfortunately, these new features can come only from Apple, so we are beholden to their timelines and commercial interests.

This is where Linux—or more specifically, uClinux (free)—comes to the rescue. uClinux is a special port of the Linux kernel that supports CPUs without a memory-management unit. Other than that, it’s basically a full-featured Linux kernel, including filesystem support for FAT32 and HFS+, TCP/IP networking, and Fire Wire, all with a nice Unix API.

By porting Linux to the iPod, we can create a freely available development platform where software can be developed for the iPod independently of the Apple firmware. What’s more, basing it on such an open system gives us immediate access to all the resources available on the Linux platform—that is, all those open source packages people use to build great software.

Installation

Installing Linux on your iPod consists of two basic steps: patching the iPod operating system stored on the hard drive, and installing a root filesystem for Linux. Installation is possible from Windows (using a third-party installer), Linux, or Mac OS X.

TIP: You must have root permissions to install Linux on your iPod.

Installation of Linux on your iPod is a nondestructive process and will leave all your existing configurations and music intact on your iPod. The included boot loader will also allow you to choose either the Apple or the Linux software to start at reset, so you can continue to enjoy the full-featured Apple firmware while you experiment with the Linux world.

Fire Wire/connection setup. To install Linux on your iPod, you first need to have your system configured for your iPod. If you are using Mac OS X, there is probably nothing further to do. However, if you are using Windows or Linux (especially if this is the first time!), you need to ensure that you have either Fire Wire or USB correctly configured for your iPod. To configure Fire Wire or USB for your system, consult the documentation provided with your operating system.

Once the operating system is configured, you need to determine how your operating system identifies your iPod. To do this, plug your iPod into your PC and ensure that it switches to Disk mode. Normally, it should do so automatically when the appropriate connection is established by your operating system; however, you can force entry into Disk mode by resetting your iPod (by holding down Menu and Play/Pause for three seconds) and then, when the Apple icon appears, holding down Fast-Forward and Rewind. Once in Disk mode, you should see the message “Do not disconnect” on your iPod. You should then check your operating system configuration to see how your iPod is configured.

TIP: This hack covers installing Linux on the iPod with a Linux based PC. If you want instructions for a Mac OS X or Windows machine, visit iPodLinux.com.

This hack works better with a Windows-formatted (FAT32) iPod. If your iPod is Mac-formatted (HFS+), it is generally easier to reformat it as FAT32 before you get started. See "Use Your iPod with a Mac and a PC" [Hack #35].

Locating your iPod. To locate your iPod under Linux, check the file /proc/scsi/scsi for an entry where the Vendor is Apple and the Model is iPod. For example:

  # cat /proc/scsi/scsi
  Attached devices:
  Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: Apple Model: iPod Rev: 1.50
  Type: Direct-Access ANSI SCSI revision: 02

The scsi0 (Host) portion indicates that the iPod is the first SCSI device on your machine and so will be visible as /dev/sda. If on your system you see scsi1, your iPod will be visible as device /dev/sdb. The following example commands will use sda as the device address for the iPod. Please replace this with the correct device address for your configuration.

Backing up. Since we will update the partition information and replace the hard-drive firmware image, we will first make a backup of this data. Use the following commands to create the two necessary backup files:

  # dd if=/dev/sda of=ipod_boot_sector_backup count=1
  # dd if=/dev/sda1 of=ipod_os_partition_backup

The first backup file is simply a copy of the master boot record (MBR), which contains the partition table for the disk. The second backup file is a complete backup of the operating system partition on the iPod and includes the Apple iPod operating system (this file is 40 MB and can be compressed once the installation process is complete). Should you need to restore these backups for any reason, simply use the following commands:

  # dd if=ipod_boot_sector_backup of=/dev/sda
  # dd if=ipod_os_partition_backup of=/dev/sda1

TIP: Note that if you want to upgrade the Apple firmware, you should first restore the iPod operating system backup.It is also possible to completely restore your iPod to its factory state by using the Restore program from Apple. This will restore the original operating system and standard partitioning to your iPod. Unfortunately, this means any settings or music will be erased and will have to be copied back to your iPod.

Partitioning. Now that the backups are done, we will add a new partition to the iPod to hold the root filesystem for Linux. This is required on Windows iPods, because Linux cannot use a FAT32 partition for its root filesystem.

The following commands describe a session with fdisk that deletes the existing firmware partition and then creates a new, smaller partition to hold the firmware. The additional space is then used to create a new partition, which we will format for use with Linux.

TIP: The output displayed in the following steps is correct for a 20-GB iPod. Some output will be different for different sized iPods; however, the values entered by the user remain the same.
  1. Start fdisk:

      # fdisk /dev/sda
      The number of cylinders for this disk is set to 2431.
      There is nothing wrong with that, but this is larger than 1024,
      and could in certain setups cause problems with:
      1) software that runs at boot time (e.g., old versions of LILO)
      2) booting and partitioning software from other OSs (e.g., DOS
      FDISK, OS/2 FDISK)
      Command (m for help):
  2. Delete the firmware partition:

      Command (m for help): d
      Partition number (1-4): 1
  3. Create a new primary partition with a length of 1 cylinder:

      Command (m for help): n
      Command action
      e extended
      p primary partition (1-4)
      p
      Partition number (1-4): 1
      First cylinder (1-2431, default 1): 1
      Last cylinder or +size or +sizeM or +sizeK (1-5, default 5): 1
  4. Activate the first partition:

      Command (m for help): a
      Partition number (1-4): 1
  5. Set the partition type to Empty:

      Command (m for help): t
      Partition number (1-4): 1
      Hex code (type L to list codes): 0
  6. Create the third primary partition from the second to fifth cylinders:

      Command (m for help): n
      Command action
      e extended
      p primary partition (1-4)
      p
      Partition number (1-4): 3
      First cylinder (1-2431, default 1): 2
      Last cylinder or +size or +sizeM or +sizeK (1-5, default 5): 5
  7. Review the changes. Note that this is for a 20-GB iPod; for other iPod versions, the sda2 partition size will vary.

      Command (m for help): p
      Disk /dev/sda: 20.0 GB, 20000268288 bytes
      255 heads, 63 sectors/track, 2431 cylinders
      Units = cylinders of 16065 * 512 = 8225280 bytes
      Device Boot Start End Blocks Id System
      /dev/sda1 * 1 1 8001 0 Empty
      /dev/sda2 * 6 2431 19486845 b Win95 FAT32
      /dev/sda3 2 5 32130 83 Linux
      Partition table entries are not in disk order
  8. If everything looks okay, write out the partition table:

      Command (m for help): w
  9. fdisk will now exit, and we can create the new filesystem:

      # mke2fs -j /dev/sda3
  10. Set the maximal mount count to never:

      # tune2fs -c 0 /dev/sda3
      tune2fs 1.34 (25-Jul-2003)
      Setting maximal mount count to -1

Kernel installation. To install the kernel, you need the iPod boot loader (ipodloader), a version of the iPod Linux kernel, and the iPod operating system backup created earlier in the backup process.

First, extract the ipodloader and iPod Linux kernel archives into a working directory and check for any last-minute release notes. In the following steps, the make_fw tool and loader.bin file are from the ipodloader distribution, and the iPod Linux kernel version is uclinux-2.4.24-ipod0.bin.

  1. Extract the Apple OS from the backup image:

      # make_fw -o apple_os.bin -e 0 ipod_os_partition_backup
  2. Create a new image including the Linux and Apple operating systems:

        # make_fw -o my_sw.bin -l uclinux-2.4.24-ipod0.bin -i apple_os.bin loader.bin
  3. Copy the new image (which is about 4–5 MB in size) back to your iPod:

        # dd if=my_sw.bin of=/dev/sda1
  4. Copy the kernel modules onto the iPod, noting that the directory /mnt/ipod must exist and should not be in use. The lib directory refers to the directory from the kernel release and contains the modules directory:

        # mount -t ext2 /dev/sda3 /mnt/tmp
        # cp -r lib /mnt/tmp
        # umount /mnt/tmp

Userland installation. The iPod Linux Userland contains the operating system tools and libraries (including the Podzilla and mp3 applications). This archive should be installed on the root partition of your iPod, created during the partitioning step in the previous section.

  1. Mount your iPod root partition:

        # mount -t ext2 /dev/sda3 /mnt/tmp
  2. Extract the root filesystem:

        # cd /mnt/tmp
        # tar zxf /tmp/ipod_fs_040403.tar.gz
  3. Update the root filesystem with the latest kernel modules:

        # tar zxf /tmp/uclinux-2.4.24-ipod0.tar.gz lib
  4. Unmount your iPod root partition:

        # umount /mnt/tmp

Finishing up. The current release contains a number of usability bugs that can be resolved by executing the following commands:

  1. Mount your iPod root partition:

        # mount -t ext2 /dev/sda3 /mnt/tmp
  2. Perform fixes for /etc/rc. Replace /mnt/tmp/etc/rc with the following:

        hostname ipod
        mount -t proc proc /proc
        ln -s /dev/pty/m0 /dev/ptyp0
        ln -s /dev/pty/m1 /dev/ptyp1
        ln -s /dev/ide/host0/bus0/target0/lun0/disc /dev/hda
        ln -s /dev/ide/host0/bus0/target0/lun0/part2 /dev/hda2
        ln -s /dev/ide/host0/bus0/target0/lun0/part3 /dev/hda3
        ln -s /dev/tts/1 /dev/ttyS1
        mknod /dev/ttyp0 c 3 0
        mknod /dev/ttyp1 c 3 0
        modprobe tsb43aa82
        modprobe eth1394
        ifconfig eth0 192.168.222.2 mtu 170
        mount -t vfat -o ro /dev/hda2 /mnt
        mount -o remount,rw /
        hdparm -S 3 /dev/hda
  3. Perform fixes for /etc/inittab. Replace /mnt/tmp/etc/inittab with the following:

        inet:unknown:/bin/inetd
        pz:unknown:/bin/podzilla
  4. Unmount your iPod root partition:

    	# umount /mnt/tmp
  5. Eject your iPod. The following command will unload the Fire Wire driver for your iPod. Once unloaded, your iPod will either reboot automatically or display the “OK to disconnect” message.

        # modprobe -r sbp2
  6. If your iPod did not automatically reboot, hold down the Menu and Play/Pause buttons for three seconds to reboot it.

Starting Linux on the iPod

Using the configuration in the previous section, your iPod will automatically start Linux on reboot. You should see the Tux logo, as shown in Figure 3-34, and then the normal Linux boot console messages will scroll by.

Once the operating system is booted, the Podzilla application will start, as shown in Figure 3-35. This work-in-progress interface mimics the Apple interface.

When you are finished with Linux, you can reboot the iPod by holding down the Menu and Play/Pause buttons for three seconds. When the Apple logo appears, hold down the Rewind button to start the Apple firmware.

To completely remove Linux from your iPod, restore the original firmware partition from your backup:

  # dd if=ipod_firmware_partition_backup of=/dev/sda1

Writing Your Own Applications

To develop applications for your iPod running Linux, you’ll need to use a special compiler that targets the ARM processor, because this is the instruction set used by the CPU on the iPod. You can download this for either Mac OS X or Linux from iPodLinux.com.

This compiler includes a special flag to generate binaries that will run on uClinux, so when compiling, you need to ensure it is used. Normally, you

Figure 3-35. The Podzilla interface
Figure 3-34. A Linux iPod on boot up

Figure 3-35. The Podzilla interface
Figure 3-35. The Podzilla interface

would use the LDFLAGS variable in your Make file to set this flag. The source package for Podzilla has an example of this:

  LDFLAGS+=-elf2flt

So, to create a simple “hello world” application, use the following files:

  ---hello.c---
  #include <stdio.h>
  int main(int argc, char **argv)
  {
  printf("Hello world!\n");
  return 0;
  }  Make file
  CC=arm-elf-gcc
  LDFLAGS=-elf2flt
  hello: hello.o
          $(CC) $(LDFLAGS) -o hello hello.o 

To install this application, simply copy it to the root filesystem on your iPod and set the executable bit. To execute it during the boot up of the iPod, you can modify the /etc/rc file; just indicate the full path to your executable.

To develop graphic applications, use the Microwindows library (free), which provides a free set of primitives for developing graphical applications. Included are: ipodloader; iPod Linux Kernel; and iPod Linux root filesystem.

Hacking the Hack

There is now an active community working on developing applications for the iPod and providing assistance to newcomers. Different people have different interests, from games (see Figure 3-36), text editors, and new codec support to speech synthesis; all are possible with the Linux kernel.

Figure 3-36. Tetris on the iPod?!
Figure 3-36. Tetris on the iPod?!

In addition to these user applications, core support for iPod features such as voice recording and Fire Wire disk support is also being improved to further extend the possibilities. Use this hack as a springboard from your Linux on iPod imagination. Just as with Apple, with Linux on the iPod, you can write any number of applications for the iPod.

—Bernard Leach

Pages: 1, 2, 3, 4, 5

Next Pagearrow