Banner

Banner
Ramast
Linux Developer

Thursday, January 31, 2019

How to unbrick your kindle paperwhite? (For Linux Users)

The process of unbricking itself, is covered here and I will assume you have already read that link before you continue reading this article.

There you had two options, either flashing system image yourself or downloading an amazon official update and installing that instead.

But sometimes that's not good enough. What if you have already made changed to the system that you want to maintain? What if you already know what is the problem and just need to access the system and correct the mistake?

For that there is a Recovery mode where you can have shell prompt and can modify the existing system as u wish.

You get there by booting into diagnostics mode, then from the options to login to recovery mode however you must provide username/password.

username is always root but finding the password can be a bit tricky since it's NOT same as your main system's password.
For me password was "mario" but if that didn't work for you then you will need to find your device serial number and generate password using this guide or this.

Once you are logged in, your system partition is the first partition /dev/mmcblk0p1 you can mount it and modify it as you please. Your data partition would be already mounted for you in /mnt/us

After you finish make sure you disable diags mode so that when you reboot you are rebooted to main system.

How NOT to install custom fonts on your kindle paperwhite.

This article is meant for people who have rooted Kindle and have good understanding of Linux command line. I explain here how I tried to manually - without use of any scripts - install custom fonts to my kindle, How I bricked it twice because of this and how I got it working at last.

If you are just looking for installing custom fonts, please refer to this post instead.

Attempt 1


I've tried to locate the directory where kindle store it's fonts and tried to add my custom font there.

The main and most important font directory is located in /usr/java/lib/fonts/ however that directory is mounted readonly and can NOT be mounted in read-write mode - for reasons I will explain later -.

There was however other directories that contained fonts and even though I could add my font there, I later realized that it's very difficult to make amazon use my custom font. It's there, it's installed but you can't select it when reading a book.

Attempt 2

My only option was to replace one of the fonts the reader allow u choose from (Baskerville, Caecilia, Futura, Helvetica and Palatino). The two in red are used by the system (especially Futura) so I chose another one.
Only problem is that they are located in the read only directory  /usr/java/lib/fonts/.

Why is it read only?
The directory is just a mount point, the fonts are actually stored in a disk image   /usr/java/lib/fonts/fonts.cramfs.img That disk image uses readonly file system called cramfs. It's a read only filesystem, once it's been created it can never be modified again

The good news however is that the tools needed to create such filesystem are already bundled in your kindle so the idea is to copy over the fonts to a directory of your choice, modify it as you desire, use the tools bundled with kindle to convert it back to cramfs and replace the original one with your version.

mkdir /mnt/base-us/fonts
cd /mnt/base-us/fonts
cp /usr/java/lib/fonts/* .

# <<Modify fonts as desired at this point>>
# Regenerate fonts special files to accommodate your changes.

mkfontscale
mkfontdir
# Generate new cramfs filesystem
cd ..
mkcramfs fonts/ fonts.cramfs.img
# Backup original file
cp /usr/java/lib/fonts.cramfs.img fonts.cramfs.img.backup
# Replace original file with your file
mntroot rw
cp fonts.cramfs.img /usr/java/lib/

After you run this code you will get an error about not enough disk space and as soon as you reboot, you will find that your device is BRICKED.
I repeat, following the code above will almost certainly BRICK your device, don't try it until you read rest of the article.

Why this bricked my device?
When you try to replace one file with another, what actually happen is that the original file is deleted first then the second file is copied in the same place.

But what if the original file is in use by another program? In Windows you would get an error and the operation will fail but in Linux (and Unix), It will mark the original file as deleted - while still preserving it on disk - and put your file in place. When the program(s) using the original file are closed then the system can truly delete the original file and free the disk space it's occupying.

In this case original fonts.cramfs.img file was obviously being used by the system so deleting it didn't release the space it's occupying. Unfortunately for me, the remaining disk space was much smaller than my custom fonts.cramfs.img so when I tried to replaced the original I got disk space error and ended up with partial fonts.cramfs.img file. When I restarted kindle, it tried to mount my custom file and it failed so the system didn't load at all. [ bricked ]

Attempt 3


I will not go in details here on how I unbricked my kindle, I will put that in a separate article but for the most part I followed this amazing guide

My next idea was to write a script that restart kindle UI and before it get chance to start again I unmount the original fonts disk image and mount mine instead. That way the original would not be in use and when I delete it, the system should free it's disk space.

This was my code

pkill blanket
while ! umount /usr/java/lib/fonts
do 
  true # Do nothing
done
mount -oloop /mnt/us/fonts.cramfs.img /usr/java/lib/fonts/

Even though I succeeded in unmounting the original fonts image file, for some reasons system still didn't free the space it's occupying when I deleted it.
I ended up bricking my device once more.

Attempt 4


By that time I became an expert at unbricking my kindle and wasn't afraid to play around so I went back to my custom fonts, I deleted many chinese and korean fonts, regenerated my custom image using the code mentioned above and replaced original while in recovery mode and now I have extra free disk space and also my custom font. Yaay

My final advice

Modifying fonts file will always pose risk of bricking your device so don't do it if you can't deal with bricked kindle.
If you are willing to take that risk however, I advice you to make sure there is more disk space in your root directory than the size of your custom fonts image BEFORE replacing the original.

If you found yourself in situation where the original has been deleted but you don't have enough disk space to copy your own, create a custom font image that ONLY contain the fonts Futura, Helvetica then copy that image instead of corrupted one. This solution has not been tested however so it may or may not work but it's certainly better than rebooting with a corrupted fonts image.

Happy Hacking