Sunday, September 2, 2018

Is it possible to boot an Android phone from a USB drive?


Is there any way to boot an Android phone* from a bus-powered USB drive**? If so, what are the steps to achieve this?


* E.g. one with USB OTG functionality.



** E.g. a flash drive.



Answer



Please clarify what is the intended goal and why?


Android phones have their own boot-loaders and cannot be overridden by other means.


It is not like a PC's BIOS where you can switch the ordering of boot to boot from certain devices such as Network PXE, USB, Primary/Secondary H.D.D...


Edit:


After the comments below, and in relation to the OP's question



Is there any way to boot an Android phone (E.g. one with USB OTG functionality.) via way of a bus-powered USB drive




The generic boot-loader (*which resides on the chip-set) has no knowledge of USB etc, as the lk (Little Kernel) is more concerned about trapping keystrokes in order to chain-load into recovery or to boot directly into Android environment (When holding Vol+Down key in this instance) - in pseudo-code (this is from the context/aspect of lk, and also, the memory addresses pertaining to how to read the partitions are hard-coded into this lk so it will know how to process the logic!)


The lk kernel is the de-facto standard by Qualcomm for MSM chipsets (Snapdragon) and adopted by manufacturers such as Sony, Motorola, LG, Samsung and can be found in the AOSP source under bootable/bootloader.


if (Is Volume Down key pressed?) then



  • chain-load kernel from /recovery partition into particular address in memory and jump to it and start execution, in bringing up the recovery environment


else



  • chain-load kernel from /system partition into particular address in memory and jump to it and start execution in bringing up the Android environment.



end if.


As the kernel within lk is pretty limited, considering that the binary image of the kernel is burned into the chip and therefore no way of modifying it. And also should be mentioned that lk contains the fastboot protocol in preparation for flashing /boot, /recovery, /system and /data partitions. There are two sequences to boot, primary boot and secondary boot as it is:



  • Primary Boot -> lk (depending on outcome of logic)

  • Go into Secondary Boot -> /boot or /recovery


Side note: Samsung is fond of the PBL/SBL (Which is Primary Boot Loader and Secondary Boot Loader respectively) in their jargon when it comes to modding. Thing about Samsung, is that, in some handsets, PBL and SBL may be encrypted (Samsung Wave GT-S8500 is one such example, where porting Android to it was nearly impossible to do because of the DRM within the boot loaders which was a nightmare to deal with and made modding it extremely difficult, nonetheless, it is sort of working via an exploit in the FOTA code!)


This is why there are no extra facilities such as OTG functionality or anything else such as serial communications, reading from SDCard, graphics etc as it would make the lk kernel bigger than is intended. In other words, it is the smallest possible size of kernel that is designated to do just the above pseudo-code happen.


Also, another way of looking at it is this, and this is dependent on the Android version - the USB OTG functionality is fully brought up within the Android environment, i.e when the familiar home screen appears, then OTG's functionality is enabled. Unfortunately not the case when looking at it from lk's perspective.


If you're curious, here's the Qualcomm entry on the above lk which is a part of the tiny C source that has ARM assembly included and found in JellyBean's AOSP source in bootable/bootloader/legacy/usbloader/main.c



int boot_linux_from_flash(void)
{
boot_img_hdr *hdr = (void*) raw_header;
unsigned n;
ptentry *p;
unsigned offset = 0;
const char *cmdline;

if((p = flash_find_ptn("boot")) == 0) {
cprintf("NO BOOT PARTITION\n");

return -1;
}

if(flash_read(p, offset, raw_header, 2048)) {
cprintf("CANNOT READ BOOT IMAGE HEADER\n");
return -1;
}
offset += 2048;

if(memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {

cprintf("INVALID BOOT IMAGE HEADER\n");
return -1;
}

n = (hdr->kernel_size + (FLASH_PAGE_SIZE - 1)) & (~(FLASH_PAGE_SIZE - 1));
if(flash_read(p, offset, (void*) hdr->kernel_addr, n)) {
cprintf("CANNOT READ KERNEL IMAGE\n");
return -1;
}
offset += n;


n = (hdr->ramdisk_size + (FLASH_PAGE_SIZE - 1)) & (~(FLASH_PAGE_SIZE - 1));
if(flash_read(p, offset, (void*) hdr->ramdisk_addr, n)) {
cprintf("CANNOT READ RAMDISK IMAGE\n");
return -1;
}
offset += n;

dprintf("\nkernel @ %x (%d bytes)\n", hdr->kernel_addr, hdr->kernel_size);
dprintf("ramdisk @ %x (%d bytes)\n\n\n", hdr->ramdisk_addr, hdr->ramdisk_size);


if(hdr->cmdline[0]) {
cmdline = (char*) hdr->cmdline;
} else {
cmdline = board_cmdline();
if(cmdline == 0) {
cmdline = "mem=50M console=null";
}
}
cprintf("cmdline = '%s'\n", cmdline);


cprintf("\nBooting Linux\n");

create_atags(ADDR_TAGS, cmdline,
hdr->ramdisk_addr, hdr->ramdisk_size);

boot_linux(hdr->kernel_addr);
return 0;
}

No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...