Jump to content

Heimdall

From Nura Wiki
Graphical user interface for Heimdall

Heimdall is a reverse-engineered, open-source version of Samsung's flashing software Odin for Galaxy devices. Heimdall runs on Windows, Mac and Linux, and can flash the majority of Samsung phones[1] either through a graphical user interface or the command line.

As the official GitHub repository is unmaintained, it is strongly advised to use Henrik Grimler's fork with additional patches available at the bottom of this page.

Entering Download Mode

The Download Mode screen on the Galaxy A70. On older devices, it may have a black background with the Android mascot instead.

To flash a Samsung device with Heimdall or Odin, it must be in Download Mode, which is Samsung's alternative to Fastboot. It's possible to boot into Download Mode by connecting the device to a computer through a USB cable and pressing one of the following button combinations:

  • Power+Volume Down+Home
  • Volume Up+Volume Down
  • Bixby+Volume Down

Refer to your device's article for the proper combination.

Once booted, you will enter an initial warning screen, from which you can unlock/relock your bootloader or continue into Download Mode.

It is also possible to make a special USB cable (similar to a serial debugging cable), where the ID pin in a Micro-USB male header is usually connected to GND with a 150 kΩ resistor[2]. When attached, the device automatically boots into Download Mode.

GUI usage

Heimdall has its own user interface heimdall-frontend for flashing. The device's PIT file can be extracted by navigating to the Utilities tab and choosing Save location→Download.

Warning WARNING: Don't select "Repartition", it will most certainly brick your phone!

After acquiring the PIT file and the required .img files, the device can be flashed from the Flash tab. Select the PIT file, add and configure a new partition (inserting the partition name and the image to be flashed) and press Start.

CLI usage

From Download Mode, you can use heimdall detect to see if your device is detected. To flash from command line, run

$ heimdall flash [--<partition name> <filename>]

The command will process the PIT file automatically and flash into the given partition (usually BOOT or RECOVERY). If you are unsure about your partition layout, the following command will print the PIT file into standard output:

$ heimdall print-pit --no-reboot
Note The inclusion of --no-reboot ensures the phone will not reboot after the PIT file has been downloaded and displayed. After executing a command with this argument, the next command should include the --resume argument. You can still safely reboot your phone manually (with the power button) after executing --no-reboot commands. See also pmbootstrap#2258

Troubleshooting (rootfs)

Heimdall and Odin may be unable to flash Nura's rootfs image. The transfer may fail on the second chunk, which indicates that the bootloader tried to verify the first package and then rejected it. This happens if the device requires the sparse image format. To enable it, add the following option in the device package's deviceinfo file:

deviceinfo_flash_sparse="true"

For further details on this file, refer to the Deviceinfo Reference page in Nura's documentation.

The format specification in AOSP specifies three different type of chunks for sparse images:

  • Raw chunk, containing data as it is; (cannot be shrunk)
  • Fill chunk, if the image contains blank regions[3] it can be shrunk into a chunk header and information about which byte to later fill into the entire memory region;
  • Don't care chunk, data region that is not important for some reason (not quite sure what it means, maybe a cache or /tmp region?), bootloader can just skip over it when writing image to memory.

In some Samsung devices, the sparse image format is slightly altered, with both the file header and each chunk header containing an extra 32-bit entry:

  • In old devices like the Galaxy S III, there are no extra entries header entries and the sparse format is identical to the AOSP specification.
  • In the Android 5 firmware for the Galaxy S6, the extra entries are there and the file header entry is 0000 0000, while the chunk entry seems to be counting up with the block offset, possibly keeping track of the expanded image's size.
  • In the Android 7 firmware for the Galaxy S6, and in all other devices up to the Galaxy S8, the extra entries are there. The file header entry is 0000 0000 and each chunk has a number that is the constant for most (but not always all) of the chunks.
  • In the Galaxy S9 and Tab S6 Lite (and supposedly other devices), the extra entries are gone, and the format is identical to AOSP's images again.

The purpose of these extra entries is unclear, but for some devices it's possible to just use zeros everywhere.

For device with non-standard Samsung sparse images, sm_sparse_image_tool.py is used to patch the image so that the Odin loader accepts it. Adding this option to the deviceinfo alongside deviceinfo_flash_sparse will do it automatically:

deviceinfo_flash_sparse_samsung_format="1"

This has been known to help with the Galaxy Note 10.1 (2014) and the Galaxy S4 Exynos. The "1" refers to the strategy used by sm-sparse-image-tool to patch the vanilla sparse image; currently this is the only existing strategy and it does the following:

  1. Extend the sparse image header and the header of every chunk with some null padding bytes;
  2. Translate fill chunks as don't-care chunks only if it's a zero fill, otherwise expand the fill chunk into a raw chunk.


i'm a magnifying glass This section's factual accuracy is disputed. Relevant discussion may be found on the talk page.
In particular: img2simg works with Heimdall for the Galaxy A70 though?

Generating a sparse image with a tool like img2simg generally shrinks an image by using these types of chunks. However, the fill chunk is not supported by Samsung's bootloaders (mentioned here), and hence the img2simg tool cannot be used to create a sparse image suitable for flashing with Heimdall or Odin. img2simg also cannot create don't-care chunks, due to it not being smart enough to analyse the compressed file system. This means that even if img2simg could be patched to no longer create fill chunks, then it would only contain raw chunks, and should hence be slightly larger than the original image. Other tools, like mke2fs, are better suited to create images in this case.

Notes

  1. ↑ Some devices, usually the ones that don't use PIT files, are unsupported.
  2. ↑ To be sure, check the kernel driver for MFD/MUIC sources.
  3. ↑ Regions where every bit is 0, which would appear in a hex editor as 00 00 00 00..., or every bit is 1, appearing as FF FF FF FF....

External links