Previous section   Next section

Hack 100 Cross-Compiling C Code for TiVo

figs/expert.giffigs/hack100.gif

Sometimes, you just need to use C. For those times, you'll need a compiler that is capable of generating binaries to run on your Series 1 or Series 2 TiVo.

There are times that you realize why C is one of the most popular programming languages out there. If you use it right, it makes code that's small and fast. Plus, there is already a lot of code out there written in C, including most of the Unix utilities [Hack #34] we've been using in this book. Using a cross-compiler is your best shot at turning your favorite little program into a TiVo binary.

Cross-compiling is a black art that deserves a book unto itself, so we'll keep to installing a cross-compiler and writing a small C program to satisfy ourselves that it works and provide yet another avenue for further TiVo hacking. If you look at http://tivoutils.sourceforge.net, you'll notice quite a few cross-compilers listed—most notably, http://prdownloads.sourceforge.net/tivoutils/usr.local.powerpc-tivo.tar.bz2?download. These are the cross-compilers that run under Linux and generate binaries to run on Series 1 and Series 2 TiVos, respectively. Both are set up in basically the same way, thanks to the hard work of M. Drew Streib.

Download the appropriate cross-compiler to a Linux PC, and unpack the entire archive to /home/tivodev (more than likely, you will have to do this as root):

# bunzip2 usr.local.powerpc.tivo.tar.bz2
# tar xf usr.local.powerpc.tivo.tar
# mv tivodev /home

Once you have installed your cross-compiler, all the software you need for C development for your TiVo (Series 1, in this case) will be available for your coding pleasure. You have gcc (/home/tivodev/root/bin/gcc) as your compiler, and a whole slew of libraries are already precompiled. However, in his README, Streib makes it clear that "you may need to install additional libraries, do some porting, etc., but that is par for the course. If you're not prepared to get your hands in lots of Makefiles and a little bit of code, you're in the wrong place."

Two libraries of note are also preinstalled: libtivohack and ncurses. Craig Leres's libtivohack (http://www.xse.com/leres/tivo/downloads/libtivohack/) fills in a bunch of the missing routines in the Series 1's system libraries with dummy functions so that software can at least compile. As mentioned in [Hack #53], the Series 1 is missing the ability to perform domain name resolution, the appropriate routines dummied by libtivohack.

ncurses is a simple, text-based windowing system. If you are planning any work in cross-compilation, this system is going to be a must for any of the more interesting programs out there.

The Code

As a quick hack, let's compile a really simple "hello world" C application. Save the following code as compilertest.c:

int main( int argc, char** argv ) {
  printf( "hello, tivo!\n" ); 
}

Compile it using the tools you just installed:

$ /home/tivodev/root/bin/gcc -o compilertest compilertest.c

Upload the resulting compilertest binary to TiVo's /var/hack/bin directory, make it executable, and give it a whirl:

bash-2.02# chmod 755 /var/hack/bin/test
bash-2.02# test 
hello, tivo!

  Previous section   Next section
Top