GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing newandroidbook.com — Reverse Engineering Android's Aboot Reverse Engineering Android's AbootJonathan Levin, http://NewAndroidBook.comAndroid's boot loader is a fairly uncharted area of the landscape. What little is known is largely due to partial open source, and for some devices - notably Amazon's and Samsung's - even that isn't available. Most device modders generally leave it be, and (given an unlocked bootloader) start off with the boot.img (kernel + ramdisk) and follow on to various modifications in The Confectioner's Cookbook devotes an entire chapter to the boot process, wherein I touch (among other things) on the boot loader format and structure. In an effort to keep things simple, however, I stop shy of reverse engineering and disassembly - largely because the first part of the book is aimed at power users, and less at developers or hackers. There's obviously great benefit in the more advanced techniques, however, so the discussion is deferred to the companion article on the book's web site. This is said article. For those of you who haven't read the book, I recap the key points from the Boot chapter here. I then continue to discuss how to reverse engineer the binary, which comes in especially handy when handling the proprietary loaders. What follows is a discussion of ARM-architecture specifics, and then additional observations I've found thus far. Recap: Dramatis Personae of the Android Boot ProcessAndroid's boot process starts with the firmware The BootROM loads several other components, each from a dedicated partition. Chief amongst those is a secondary boot loader (SBL), which is responsible for overcoming the tight constraints of ROM - limited space and an inability to upgrade. When you "flash the bootloader", you flash those partitions (as discussed in the book). For now, however, our focus is Android's own boot loader, which often resides in a partition called "aboot". aboot is a native ARM binary, which is wrapped by a very thin header, which is usually 40 bytes in length. This header provides metadata which is used by the SBL in the process of validating and loading aboot. Note, "validating and loading" - in this order - because the SBL will very likely reject an aboot image which is not properly signed by the vendor. This works to extend a chain of trust, which starts at the ROM, all the way to aboot, which further extends it to the boot.img (unless the boot loader is "unlocked"). In other words, the ROM has a built-in key (which, by its nature, cannot be modified), used to validate the precursors of the SBL. Those, in turn, validate the SBL, which validates aboot, thus securing the boot sequence and preventing any modification by malware (or unauthorized rooting attempts). aboot is the first component which may opt to break the chain - and this is what is meant by "boot loader unlocking": The unlocking simply disables the signature check on the next part of the boot sequence, which is loading the kernel and RAM disk from the boot partition (flashed from boot.img). Not all boot loaders can be unlocked, however, as that is left to the vendor's discretion. For those which do, it is usually a straightforward matter - the device is placed into bootloader mode ( Thus, the SBL loads aboot, and inspects its header to find the "directions" for loading. You can obtain aboot from a factory image (by using morpheus@Forge (~/...-kot49h/)% od -A d -t x4 aboot |head -5 Magic Version NULL ImgBase 0000000 00000005 00000003 00000000 0f900000 ImgSize CodeSize ImgBase+CodeSize SigSize 0000016 0003fe2c 0003e52c 0f93e52c 00000100 ImgBase+CodeSize+SigSize Certs 0000032 0f93e62c 00001800 ea000006 ea00351c 0000048 ea003522 ea003528 ea00352e ea003534 0000064 ea003534 ea00354b ee110f10 e3c00a0b morpheus@Forge (~/S5/)% od -A d -t x4 aboot |head -5 Magic Version NULL ImgBase 0000000 00000005 00000003 00000000 0f800000 ImgSize CodeSize ImgBase+CodeSize SigSize 0000020 00107e24 000fed24 0f8fed24 00000100 ImgBase+CodeSize+SigSize Certs 0000040 0f8fee24 00009000 ea000006 ea005487 0000048 ea00548d ea005493 ea005499 ea00549f 0000064 ea00549f ea0054b6 ee110f10 e3c00a0b morpheus@Forge (~/HDX/)% od -A d -t x4 aboot |head -5 Magic Version NULL ImgBase 0000000 00000005 00000003 00000000 0ff00000 ImgSize CodeSize ImgBase+CodeSize SigSize 0000016 00054d18 00053418 0ff53418 00000100 ImgBase+CodeSize+SigSize Certs 0000032 0ff53518 00001800 ea000006 ea003b13 0000048 ea003b19 ea003b1f ea003b25 ea003b2b 0000064 ea003b2b ea003b42 ee110f10 e3c00a0b You can toggle between the devices shown here, but note the format of aboot is still very much the same. The Magic (0x0000005) misleads the The first byte of the aboot binary image is identifiable by the "eaXXXXXX" value. The book alludes to this being an ARM B(ranch) instruction, but stops short of explaining its significance - which is explained next. ARM Processor Boot, in a NutshellARM processors use an Exception Vector during their lifecycle. This vector is a set of 6 or seven addresses, set by the operating system, and instructing the processor what to do when certain traps are encountered. These traps are architecture defined, and so the vector indices are always the same across all oeprating systems
Boot loaders and operating systems alike, then, have a simple mode of operation: When a component wants to transfer control to another component, it loads it into memory, in the process overwriting the exception vector with that of the next stage. A processor reset instruction is issued, which causes the processor to jump to the next stage's entry point. Armed with this information, we are now ready to proceed to a meaningful disassembly of aboot Disassembling AbootAs discussed in the book, the standard aboot is derived from the "LittleKernel" project, hosted by CodeAurora. This project (often referred to as "LK"), is partially open sourced, in that some of its components can be found there (A simple way to get the sources is via
Following these steps, and loading the result into a disassembler (e.g. IDA or Hopper), you'll still need to rebase the image to the ImgBase value specified in the header (which you've stripped). You'll see something similar to this:
The code above can be seen in LK's ARM support, verbatim. If you follow the branches to the various exception handlers you should likely see the code from LK's ARM exception support, as well. All Bootloaders I've checked (including Samsung's and Amazon's) seem to derive from LK (in fact printing out the "welcome to lk\n\n" from its kmain), though Amazon's take steps to disable fastboot, as does Samsung, which then uses their own custom App, called Odin. This makes sense, since LK is developed by Qualcomm for use with SnapDragon (msm), which was a common denominator for my test devices (I haven't had a chance to check the Exynos-based devices). Aboot flowAboot's flow spans multiple files. It is, however, a one-way flow, since functions are not expected to return. This useful tidbit lends itself to finding stop points throughout aboot, which are "B ." (i.e. goto here) instructions in the code, which effectively halt the processor for those cases that something in the flow goes wrong. This actually made it easier to piece the flow from the disassembly first
After the creation of the bootstrap2 thread, the main thread re-enables interrupts, and becomes idle. The very last call in kmain handles the idle thread, as you can see by the following disassembly. The addresses are those of the S5 (as you can tell by the 0F8xxxxx). The N5 code is identical, right up to the addresses, which is why the code below has labels: _become_idle_thread:
0F8164CC STMFD SP!, {R3,LR}
0F8164D0 LDR R0, "idle"
0F8164D4 BL _thread_set_name
0F8164D8 LDR R3, current_thread
0F8164DC MOV R2, #0
0F8164E0 LDR R3, [R3]
0F8164E4 STR R2, [R3,#0x14]
0F8164E8 LDR R2, idle_thread
0F8164EC STR R3, [R2]
__idle_loop:
0F8164F0 BL _arch_idle ; Calls ARM WFI (Wait-For-Interrupt)
0F8164F4 BL _arch_idle
0F8164F8 B __idle_loop
The flow thus continues in bootstrap2..
The
This should get you started on your own explorations of Android's Boot Loader. The platform support stuff isn't that interesting (USB being somewhat of an exception, due to its usage as a potential attack vector). Apps, in particular, are obviously important. Of special interest is Odin, to which I hope to devote another article at some point. As usual, feedback and/or requests are appreciated - info@ (this domain). Q&AQ: How did you uncover the header format? A: I did it by reversing - all boot loaders on ARM have a similar structure imposed by the architecture (e.g. the Exception vector), so it was easy to work backwards from the eXXXXXXX instructions ("e" is ARM speak for "always" and so branch instructions stand out). Values which repeat (e.g. the code size, which then appears along with ImgBase) make it easy to figure out "hey, this value is that value + that value". When compared to the file size, the values further add up. Certs stick out like a sore thumb because they are a known format (PKCS #7? #12? whatever), and their hard coded strings can be easily seen. Of course, it turns out that this is all also in a C file in some obscure LK directory... :-P Q: What's this about rebasing the image? A: Needed only if you plan to load in a disassembler, e.g. IDA. When you do, you will see the branch addresses are all relative to PC, but there are quite a few hard coded addresses (for LDR..) which are all absolute, so you need to know what the ImgBase is. Even if you don't, you will see those addresses begin with 0xF9 or similar, which gives you the image base. What's nice is that, if you get it wrong, the addresses won't make sense (i.e. will be outside the image). If you do, everything literally falls into place.
Footnotes
* - At least, on ARM devices it does. On the few Intel devices I've had the pleasure of working with, BIOS was used. Intel is likely trying to push UEFI for its devices, though I haven't seen any personally. If you have an Intel device and want to send me a dump of it's boot partition, I'd appreciate it.
** - iOS uses the same technique throughout its boot stage, which involves iBSS, iBoot, and XNU. This is covered in the other book, in Chapters 6 and (with disassembly) 8. *** - (which I did, foolishly, before I knew about the source being accessible - talk about hours of my life I won't be getting back). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402639 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |