Sunday, October 16, 2011

The Golden Rule

The Golden Rule of computing is "data you don't have copies of is data you don't care about." This is a rule I live by in my digital life, faithfully making copies of all the things I know I'd have a hard time getting back in the event of a theft, fire or, far more likely, my own blundering.

One of these things is the current state of my phone. I am very tempted to rush headlong into Android development and nuke my current installation because hell, getting away from this setup is why I'm porting Cyanogenmod in the first place! However, my data-safety tendencies kick in before my curiosity gets the better of me.

So then, how do you back up a device like an Android phone? The naive answer is "just run adb pull /". For the purposes of grabbing the relevant files, this would work fine (nevermind the fact that adb won't accept that particular command). However, the Android root filesystem contains not just files, but also mount points of various important partitions. In order to jury rig a working backup ROM, I'd have to faithfully preserve this layout.

Consider first a Linux machine. A Linux machine's hard drive is laid out in partitions. The bulk of the spaces belongs to data partitions, but some partitions have special purposes, with which you might be familiar. The swap partition serves as a place to store swapped-out pages in the virtual file system. The boot partition stores the kernel and all files required to start the operating system at boot time. Extended partitions operate as a hack to allow for more than four partitions on master boot record-organized disks.

The Android disk is laid out in a similar fashion, with various partitions serving various purposes. An excellent and concise description of each partition can be found here, so I won't go into what each partition does. Rather, I'll give a description of the relevant concerns you must keep in mind when handling each partition during a port.

One of the crucial points with my situation is that this phone has very little support and community based around it. A successful port will involve combining the widely available portions of the android platform, i.e. Cyanogenmod, with LG's own modifications and closed source libraries, like the camera, radio, etc. For this reason, it's important that I handle each partition extremely carefully to avoid destroying data.

/boot

The boot partition contains the bare minimum required to bootstrap the system, namely the bootloader, the kernel, and the init binary. I will end up writing and compiling a custom kernel, probably backporting LG's changes, but I would be very very hesitant to do any harm to the bootloader. If I screw up the bootloader, the phone will be bricked, and I will have to throw it out and give up on my little project, because LG USA doesn't support this model.

As an aside, please, please, please correct me if I'm wrong. That last point is a source of some anxiety on my part.

There is an very nice wiki page describing the byte-level layout of the boot image, and I will most likely be using the tools described there to unpack and repack the boot image as necessary. This allows me to modify the kernel, but still keep LG's own init scripts in place until I'm ready to remove them.

/recovery

So what if you do screw up the boot partition? You can always fall back on the recovery partition. This partition is a free standing (not so sure about this point), bootable partition containing some code to fix your screwups. I believe this partition is typically used to perform carrier over-the-air updates, although I suspect LG doesn't use it. LG's update process (at least for this phone) is an oldschool "connect your phone to your windows box and run the update application" type deal, so this partition might not contain anything useful, if it exists at all. 

That being said, I would be very surprised if this partition wasn't there, so I'll assume I have some space to work with. Writing my own recovery image would be very useful, since it would grant the ability to screw around with the various partitions without worrying too much about how to bring the phone back to life. 

/system

This bit contains the proper Android stuff. For what it's worth, it's going to be completely blown away and replaced with Cyanogenmod. The only thing it's really good for is the closed source OEM libraries, including the graphics and camera libraries, so it will have to be preserved at least in filesystem form. 

There are other partitions, but I'm not really concerned with them at this stage. A backup would consist of grabbing each of these partitions, byte by byte, and a recovery would consist of booting into some minimally useable state, be it recovery or bootloader, and flashing each partition with those backed up images. 

So then, this has been a fine bit of theory, but my next post will describe the practical concerns with backing up these partitions, and subsequent posts will describe what goodies I find on them once I unpack them. 

3 comments:

  1. I'm really enjoying this blog, but please can you use a non white on black theme. It sears writing into my eyes.

    Otherwise, an interesting project to follow

    ReplyDelete
  2. Ah excellent, it even ties in, cyanogenmod-wise

    ReplyDelete