Compiling the Linux kernel for Amazon Kindle

So, I recently bought a Kindle 2. As usual, the minute it arrived, I ripped it apart, poked every chip, and then started to reverse engineer the damn thing. Wait. I didn’t have to! I found this out days late, after messing with IDA Pro. Amazon has generously released most of the back end code for the Kindle as open source. (The front end, aka the stuff you see, is written in Java and we might get to that another day). So I decided to compile my own Kindle kernel. Why? Why not. Here’s how:

Part 1: Prerequisites

  • Get a root shell of your Kindle. If you don’t know, Google “usbNetworking”
  • A Linux computer for compiling code
  • Amazon’s sources for your version of the Kindle: http://www.amazon.com/gp/help/customer/display.html?nodeId=200203720
  • An ARM cross-compiler. You can compile Amazon’s code, or if you’re lazy, use CodeSourcery’s precompiled toolchain: http://www.codesourcery.com/sgpp/lite/arm
  • The following packages, get them from your distro’s repo: libncurses-dev (for menuconfig), uboot-mkimage (for making the kernel image), and module-init-tools (depmod)

Part 2: Compiling the kernel

  1. Extract the source to anywhere. If you can’t decide, use “~/src/kernel/” and “cd” to the source files.
  2. Now, you need to configure for the Kindle, type “make mario_mx_defconfig
  3. Edit the “.config” file and look for the line that starts with “CONFIG_INITRAMFS_SOURCE“. We don’t need that, delete that line or comment (#) it out.
  4. Here’s the part were you make all your modifications to the kernel. You might want to do “make menuconfig” and add extra drivers/modules. I’ll wait while you do that.
  5. Back? Let’s do the actual compiling. Type the following: “make ARCH=arm CROSS_COMPILE=~/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi- uImage”. This will make the kernel image. I assume you installed CodeSourcery’s cross compiler to your home folder (default). If your cross compiler is elsewhere, change the command to match it.
  6. Compile the modules into a compressed TAR archive (for easy moving to the kindle): “make ARCH=arm CROSS_COMPILE=~/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-linux-gnueabi- targz-pkg” (again, if your cross compiler is installed to a different location, change it).
  7. For some reason, depmod refuses to run with the compile script, so we’re going to do it manually. Do the following “depmod -ae -F System.map -b tar-install -r 2.6.22.19-lab126 -n > modules.dep” Change 2.6.22.19-lab126 to your compiled kernel version.
  8. Open modules.dep up with a text editor and do a search & replace. Replace all instances of “kernel/” with “/lib/modules/2.6.22.19-lab126/kernel/” (again, use your version string). I’m not sure this is needed, but better safe then brick.
  9. Now copy arch/arm/boot/uImage, linux-2.6.22.19-lab126.tar.gz (or whatever your version is), and modules.dep to an easy to access location.

Part 3: Installing on Kindle

  1. Connect the Kindle to your computer, and open up the storage device. Copy the three files you moved from the previous part to your Kindle via USB.
  2. This part is mostly commands, so get a root shell to your Kindle, and do the following commands line by line. Again, anywhere the version string “2.6.22.19-lab126” is used, change it to your kernel’s version. Explanation follows.

mv /mnt/us/linux-2.6.22.19-lab126.tar.gz /mnt/us/modules.dep /mnt/us/uImage /tmp

mv /lib/modules /lib/modules.old

cd /tmp & tar xvzf /tmp/linux-2.6.22.19-lab126.tar.gz

mv lib/modules /lib/

chmod 644 modules.dep

mv modules.dep /lib/modules/2.6.22.19-lab126/

/test/flashtools/update-kernel-both uImage

sync

shutdown -r now

Wow, that’s a lot of commands. What did that do? Well, line by line:

  1. Move the files we compiled to the temp folder. That way, we don’t have to clean up.
  2. Back up the old kernel modules
  3. Go to the temp folder and untar the modules
  4. Install the modules
  5. Correct the permissions for the modules.dep file (in case something happened after copying from your computer)
  6. Move the module dependencies list to it’s correct folder.
  7. Flash the kernel (I don’t know why it has to be flashed twice to two different partitions, but if you don’t, it won’t load, maybe sig checks?)
  8. Make sure everything is finished writing
  9. Reboot

ASCIIMan: A Windows Console platformer game written in Java

For my final project in my Computer Science class, I decided to write a game in Windows Console, in Java. It’s hard to appreciate how hard this was unless you REALLY know Java and you REALLY know Windows SDK. I basically wrote a entire game engine complete with collision detection, physics, etc from scratch in Java. I used almost every obscure Java knowledge I have including reflections, JNI, enums, and thread handling. It is really less of a game and more of a technology demo because I can’t design anything. I hope someone else can write a better level (I made it very extendable), and fix collisions (those are the only major “bugs”, otherwise, it’s a fully playable game). Again, this is one of those things where people who don’t know alot about Java won’t think it’s a big accomplishment, but those who do will bow down to this wonderful code. /ego trip

ASCIIMan Project Page

P.S: I updated Josh again, I converted the whole project to NetBeans, my new found love.

Update to TweetBot

After a good night’s sleep, I “fixed up” TweetBot. No, the code is still junk, but I made it a bit better. New features:

-Doesn’t crash if no geolocation is found

-Multiple commands separated by period.

-Update shout

-Update Twitter/Facebook

-Removed debug logging (it showed things down a bit)

-Commands that begin with “set” ignores the word “set”, this way your sentences can flow better.

Some new commands:

Set a shout

Status is “I’m shopping”

Update Twitter, facebook, or both

Show twitter

Show facebook

Show twitter and facebook

Multiple commands

Search around me for “best buy”. Set status to “buying a new computer”. Show twitter & facebook. Checkin to result 0.

Remember, you can always throw in extra words outside of quotes. Only the first word, quotes, and (if no quotes), the last word are read.

http://www.yifanlu.com/p/tweetbot

Update Foursquare from Twitter

Ok, so I THOUGHT this was going to be a quick one hour project. I want to update foursquare from Twitter because my cell phone plan ONLY allows access to Twitter and MS Exchange (why, I don’t know). The goal was to write a application that sits in the background and waits for “command” tweets. My original plan was to do it in C++, however, networking & sockets in C++ is too complicated for such a small project, plus no good libraries are available for Twitter in C++. Ok, so I moved to Python. It has great networking tools right? Plus a wonderful Twitter API library. I was halfway through when I found that Foursquare support was crappy. Finally, I went to the language I hate the most. Java. Also, note that I’ve been messing around for hours now. Fuck. So I quickly wrote this in Java, tired and angry. The result is the worst code I ever written. I am the only person who would ever make use of this, so I didn’t care. I’m only releasing it for archival purposes, for some laughs to random strangers, and as an example of what you should NOT do. It’s a great example of “it compiles, ship it”.

So, the only features are: search foursquare, checkin to foursquare, and greet the user. To use it, you need to set up two twitter accounts, one for the client (you will tweet commands from here) and one for the server (the server will use this account to tweet). Make the client follow the server and the server follow the client. Make sure your twitter client supports geotags. If your tweet doesn’t have a geotag, the listener will crash. (I know, stupid)

Again, you MUST have a geotag with every tweet.

http://www.yifanlu.com/p/tweetbot

To search, tweet:

Search for “restaurant”

Note: You MUST put the quotes for the search to work. For all commands, the server only reads “key” words. That’s the first word and any word in quotes. You can type in

Search for some shitty “restaurant” up in this bitch

and it’ll work just fine. For search, if you don’t have any words in quotes, then you’ll just get a list of 10 closest venues.

Now, the server will return top ten results near you, in two tweets (a random character appearing at the end of server tweets is not a bug, I did that because Twitter rejects any two tweets that are identical).

0: Random Chinese Restaurant

1: Some Mexican Restaurant

2: Another Crappy Restaurant

and so on, to check in to “Another Crappy Restaurant”, tweet

Checkin 2

If you know the name of the venue, you don’t have to search, you can just tweet.

Checkin to “another crappy restaurant”

That’s basically it.

Now, about the spaghetti code, here’s some of the things I used: depreciated methods, tons of try-catch just to bypass errors (no actual error handling), bad variable name and no documentation, bad code flow, usage of Runtime.getRuntime().exec() to call cURL because I was too lazy to write a proper HTTP controller which is a security and stability issue, use of reflections with data across the internet, and so much more. My hope is that someone will take it and fix it up or something, because I’m too tired to do anything about it.

Google Apps User Registration Script

Here’s another one of my famous 3-hour-projects. I finally decided to cleanup my email. It’s too hard to “clean”, so I decided to start from scratch by making a new email account. So, I made a Google Apps account. Google Apps is a great product, but one thing missing is registration for users. (You must make an account manually for your user) So, I decided to make one myself. This PHP script acts as a proxy between you and Google Apps. It allows your users to create their own account with you and your Google Apps. It is composed of a backend and a frontend. The backend does the work of taking your admin credentials and form data from a user and creating an account for the user. The frontend hosts the GUI. I made sure to well-comment the code, so it should be easy to create your own frontend to match the style and code of your site. I’m probity won’t work on this project again, but because it’s released under GNU v3 (as all my projects), you can take it and add on to it.

Google Apps User Registration Script