<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alex Roman on Medium]]></title>
        <description><![CDATA[Stories by Alex Roman on Medium]]></description>
        <link>https://medium.com/@alex.roman?source=rss-f3bba84d94c8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8SZPuHbrdwIuBIhj.</url>
            <title>Stories by Alex Roman on Medium</title>
            <link>https://medium.com/@alex.roman?source=rss-f3bba84d94c8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:21:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alex.roman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android Boot and Recovery Images]]></title>
            <link>https://medium.com/textnowengineering/android-boot-and-recovery-images-fd756e234c1f?source=rss-f3bba84d94c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd756e234c1f</guid>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kernel]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Alex Roman]]></dc:creator>
            <pubDate>Tue, 07 Mar 2017 17:46:53 GMT</pubDate>
            <atom:updated>2017-03-16T17:52:08.706Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*46U9tye_TfadB9up77Z1pw.png" /></figure><p>To fully grasp the role the Android boot and recovery images play, we first need to understand the general boot-up process of an Android phone. While embedded software is no longer part of my day-to-day work at TextNow, I used to wear the embedded-engineer hat in one of my previous jobs and I dabble with embedded electronics in my spare time, so I am intimately familiar with modern microprocessor boot-up routines. I must caution you that this is a very technical post, but one that is hopefully useful to somebody looking for a reference material on how this all fits together, with links to where to delve more in-depth if required. This type of post would have been very useful when I was learning it :)</p><p><strong>The Android Boot-Up Process</strong></p><p>When power and reset is applied to the processor, it wakes up and needs to figure out what to do. In modern processors, there is usually a very small program baked-in to the processor whose function is to verify the next component’s integrity. This first component is sometimes referred to as the <em>secure boot ROM, </em>and its job is to ensure the next component it loads is trusted, verified software. This prevents unwanted tampering of the system software and, as such, increases the security of the device. The verification usually involves some sort of cryptographic cipher using a public/private key scheme, with the private key usually kept a closely guarded secret by the manufacturer. <strong>Note:</strong> usually the public key is programmed in at assembly time by the phone manufacturer and its contents can never be changed, thereby requiring that processor to always run a signed bootloader, with the private counterpart of the public key burned in at assembly time.</p><p>The next component loaded by the secure boot ROM is the what people commonly refer to as the <em>bootloader</em>. The bootloader, as I mentioned, is usually signed with the phone manufacturer’s private key, and it has a few jobs:</p><p>1. Load a boot image from non-volatile memory (usually NAND flash nowadays) into volatile memory (RAM) and run it.</p><p>2. Optionally, that image needs to be cryptographically verified before being loaded (as is the case with more and more phones these days which don’t even provide a way to unlock the bootloader to run unsigned images).</p><p>3. Provide the ability to flash new boot and other images to the non-volatile memory for future upgrades and recovery from failure.</p><p>4. Display some sort of information on the screen, and optionally provide an interface for the user to interact with. This is commonly known as <em>fastboot mode</em> on some Motorola, Google, and other manufacturer’s devices.</p><p><strong>Android Boot Images</strong></p><p>We now come to the final component of the boot-up process — loading a boot or recovery image into RAM and running it.</p><p>Technically speaking, there are no structural differences between boot and recovery images. The only difference is which kernel and which initial ramdisk is loaded, but they are packaged the same. We will come back to how these are packaged to make a neat and tidy boot image.</p><p>There are a few sub-steps involved in running the boot image:</p><p>1. Cryptographic verification</p><p>2. Loading of the (Linux) kernel at the required memory address</p><p>3. Loading of the initial ramdisk at the required memory address</p><p>4. Jump to the start address of the kernel</p><p>The cryptographic verification is usually manufacturer-specific and it is typically very hard to reverse-engineer this method, though not impossible — it’s been done before for certain phones (for example, older Kit-Kat era LG phones).</p><p>The kernel is the heart of the operating system, as it ticks its constant heartbeat of interrupts and events which keep a modern operating system running. This magic has very meager origin as a simple chunk of bytes that was compiled in such a way to wake up and start running from a particular address of memory. This address varies depending on the particular phone and memory layout. Once execution starts, the kernel will start loading drivers and setup more advanced facilities so that it can start running some of its own programs.</p><p>In and of itself, the kernel wouldn’t know what to do once it’s loaded. It would sit there and do nothing. But what the Linux kernel <em>expects</em> is an initial ramdisk at a predefined memory address. This ramdisk contains a very rudimentary and usually read-only filesystem which contains the most basic root filesystem possible, and a file under <em>/sbin/init</em> which the kernel executes as the last thing it does as part of initialization. This <a href="https://android.googlesource.com/platform/system/core/+/master/init/init.cpp"><em>init</em></a><em> </em>program starts running some scripts that start other programs talking to each other and finally deliver the full experience the system has to offer.</p><p>So now we come back to the difference between a boot and recovery image. While the kernel sometimes is the same as the boot image, the recovery image ramdisk has a distinct sets of <em>init</em> scripts which don’t start the Java VM, but instead start a small C program called (did you guess it?) <em>recovery</em>. Recovery displays a limited user interface that allows the user to start the system, factory reset, clear the cache partition, etc. Some custom recoveries have their own program that offers even more functionality.</p><p><strong>Packing an Android Boot Image</strong></p><p>The Android boot images have a <a href="https://github.com/android/platform_system_core/blob/master/mkbootimg/bootimg.h">specific structure</a> whose internal organization reflects the memory in which it resides, namely flash. Flash is a specific type of memory that is not random-access, but is read in pages whose sizes vary depending on the particular chip, often within the 4K bytes range. For this reason, the boot image’s components are aligned to the page size, so that loading is more efficient (amongst other reasons I won’t go into detail here). The structure of the boot image is as shown in the figure below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*Yy9-5h_jk6XfxTtl0ZWenw.png" /><figcaption><em>The layout of an Android boot image</em></figcaption></figure><p>I should mention something of this second stage. To be completely honest, I do not know its <em>raison d’être</em>, the original intent of it. I have seen it used in more recent times to hold the kernel’s device tree block (DTB, another long and deeply technical topic best explained elsewhere) by certain phone manufacturers. The older kernels had the DTB as part of the kernel image, now they’ve split it out into its own separate piece to make it easier to build a more generic kernel that can be adapted to a variety of hardware configurations within the same architecture.</p><p>Moving swiftly along (and back!) to the boot image header, this primarily tells the bootloader how to load the components of the boot image, and it looks something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ftIjlRFoEB9NJUUxpC8tJQ.png" /><figcaption><em>Android boot image header</em></figcaption></figure><p>As you can see, it has a few “book-keeping” fields about how large the other sub-components of the boot image are, what address to load them into, etc.</p><p>It also has a field called <em>magic</em>. The magic field is simply a string of well-known characters to identify this as an Android boot image. In this case the string is “ANDROID!”, which fits neatly into 8 bytes.</p><p>Somebody familiar with the Linux kernel and boot-up process will recognize the kernel tags and command line argument fields as ways to pass more custom information into the kernel, such as where the console should be, and where to find that initial ramdisk from where to load its <em>init</em> program.</p><p><strong>Working With Android Boot Images</strong></p><p>So, since we have custom recovery and boot images for our Android phones (such as TWRP, CWM, etc.), obviously, someone had to create their own recovery image or modify an already existing boot image. Most of the work was done using the already open-sourced Google Android code, but there only was code there to create a boot image from its components. There was no tool to unpack a boot image for modification. Now, you can use a tool like <a href="https://github.com/osm0sis/mkbootimg">https://github.com/osm0sis/mkbootimg</a> to pack and unpack boot images and change out the kernel, or modify the initial ramdisk.</p><p><strong>Further Areas to Explore</strong></p><p>There are some interesting aspects relating to the initial ramdisk that apply to both the boot and recovery Android images that this article won’t cover in detail due to its already burgeoning size, but that are worth mentioning.</p><p>● The filesystem that initial ramdisks use is usually a gzipped <a href="https://en.wikipedia.org/wiki/Cpio">CPIO</a> archive</p><p>● You can pack and unpack the initial ramdisks and modify them</p><p>● init scripts are written in their own domain specific language (DSL) and you can read their <a href="https://android.googlesource.com/platform/system/core/+/master/init/README.md">README</a> for more information</p><p>● Default SELinux file contexts and policy usually reside in the ramdisk’s root</p><p>We can spend many hours talking about and playing with the above items, but at least for now I will leave them as an exercise to the keen reader.</p><p>Until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd756e234c1f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/textnowengineering/android-boot-and-recovery-images-fd756e234c1f">Android Boot and Recovery Images</a> was originally published in <a href="https://medium.com/textnowengineering">TextNow Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dissecting the Android OTA Update System]]></title>
            <link>https://medium.com/textnowengineering/dissecting-the-android-ota-update-system-a60dc7bf2ad5?source=rss-f3bba84d94c8------2</link>
            <guid isPermaLink="false">https://medium.com/p/a60dc7bf2ad5</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <dc:creator><![CDATA[Alex Roman]]></dc:creator>
            <pubDate>Tue, 17 Jan 2017 14:59:14 GMT</pubDate>
            <atom:updated>2017-03-16T17:56:15.774Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*y2sbv9zzrsWmuG5XqlUTKg.jpeg" /><figcaption>Installing an OTA Update</figcaption></figure><p>One of the most important features of any modern software platform is its ability to update itself to the latest version. Not only does it satisfy the need of the customer to receive the latest and greatest features, it also allows the timely distribution of security patches to address exploits.</p><p>As the Lead Firmware Developer at TextNow, one of my responsibilities involves developing and maintaining the custom system software that gets flashed onto the phones we sell. Part of that software involves the Over the Air (OTA) updates system. In order to deliver updates to our customers more effectively, I’ve built a custom system application and back-end server that work together with the Android APIs and Recovery. Together, these components make OTA updates a reality for TextNow phones.</p><p>OTA updates involve downloading one or more files that contain the updates to be installed. Usually, they are accompanied by a script that tells the update system how the updates are to be installed.</p><p>The installation script is executed by the recovery image — essentially a slimmed down Linux system — which takes very little room in the phone’s memory, and also shows the user a simple menu to perform basic system recovery functions such as wiping the phone, or the cache partition. In this case, the recovery image will be performing the installation of the OTA update.</p><p>We will take a brief dive into each of these steps in hopes to make the overall process of OTA updates on the Android platform a little easier to understand.</p><h3>Downloading the OTA Update</h3><p>This step is handled by a system updates application that is pre-installed by the phone manufacturer and often the download is automated. Most manufacturers either don’t allow downloading while on a cellular data connection, or they have a setting for it.</p><p>Once the OTA update is downloaded, it is usually checked for integrity using a checksum. This is important so that we don’t waste the user’s time rebooting the phone and not being able to install the update (there are additional integrity checks later in the process).</p><h3>Installing the OTA Update</h3><p>This system update is then responsible for initiating the installation process, but generally this requires user consent (I’m looking at you Microsoft!), and so the OTA update availability is communicated to the user via an Android Notification.</p><p>Once the user says — “Yes, go ahead and install” — the system updates application will call into Android’s <a href="https://developer.android.com/reference/android/os/RecoverySystem.html">Recovery System API</a> to initiate the installation process. The Recovery API does a few things:</p><p>1. Verifies the update file’s signature, to ensure it was signed with the same certificates that match <em>/system/etc/security/otacerts.zip</em></p><p>2. Writes a command in <em>/cache/recovery/command</em> to instruct the recovery to install the OTA update. This command is usually:<br>--<em>update-package=&lt;path_to_package.zip&gt;</em></p><p>3. Reboots the phone with special arguments to ensure the phone will boot up into recovery mode.</p><p>Once the recovery has booted up, it will look for the command file, parse it, and realize that it needs to install an OTA update. It will unzip the archive, and verify its signature once again. This time, though, it will compare the signature against certificates stored right in the recovery image’s initial ramdisk (more on this in a future blog post), under <em>/res/keys</em>.</p><p>Next, the recovery will start running the OTA update’s installation script mentioned earlier. This script can technically be a shell script (used by some non-standard open source recovery projects), but the standard Android way is to use a language called <a href="https://android.googlesource.com/platform/bootable/recovery/+/master/edify/">Edify</a> that was purpose-built for the Android Recovery System. There is also a reference to (most of) the commands that this language supports on the <a href="https://forum.xda-developers.com/wiki/Edify_script_language">XDA Developers’ Wiki</a>.</p><p>The update script typically does the following:</p><p>1. Verifies correct file versions and checksums on the system partition. This prevents installing the update on top of the incorrect OS version, thereby preventing a bricked phone and an angry user.</p><p>2. Copies new files, patches old ones, and deletes obsolete files.</p><p>3. Writes new recovery/bootloader/radio images. Since the recovery system is loaded into working memory at boot time, the flash partition where it was loaded from can be safely written to from the recovery itself.</p><p>4. Displays progress of the installation to the user.</p><p>5. Updates the version of the OS so that this OTA update can’t be installed again.</p><p>Finally, the recovery system will reboot the phone, but not before removing the recovery command file. This is important so we don’t get stuck in a recovery loop.</p><h3>My Final Update</h3><p>The Android recovery system is a complex beast, that addresses the complicated problem of OTA updates. There are many different components that need to be made to talk to each other. Debugging recovery problems can be tricky at the best of times, especially since it involves reboots and modifying system partitions, which can brick one’s phone and force them to start again from scratch. (Not that this ever happened to me, ever… ☺) However, once it works, it tends to work well, so you can find comfort in that.</p><p>If OTA updates and custom system software are your thing, check out some of our current <a href="http://about.textnow.com/careers/">job openings</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a60dc7bf2ad5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/textnowengineering/dissecting-the-android-ota-update-system-a60dc7bf2ad5">Dissecting the Android OTA Update System</a> was originally published in <a href="https://medium.com/textnowengineering">TextNow Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>