Mobile devices from the inside. What is GPT?

    What is GPT?



    1. Introduction


    We continue to consider the structure of the software part ( software ) of mobile devices ( MU ). Today we’ll talk about the device GPT- partition memory. I was prompted to write about this by studying the MBR and GPT structures , instead of writing comments on it. I would not like to scold or correct the author, but to supplement the above publication with a bias in the MU .


    So, GPT (GUID Partition Table) is:

    • first of all, the name of the memory markup scheme ( GPT scheme ) of the MU;
    • then the name of the memory partition ( GPT partition ), where this circuit is physically located;
    • Well, also the name of the image file of the GPT partition ( GPT file ).

    There are two memory markup schemes: MBR and GPT . Each circuit is contained in a separate memory section, called, respectively, MBR or GPT .

    The layout of the MBR layout can be found in [1] , and the GPT is another format for describing a memory layout - GUID (GUID Partition Table). It is part of the Extensible Firmware Interface (EFI), the UEFI standard used instead of the BIOS to load partitions.

    Switching to another format allowed eliminating the most significant drawback of the MBR format - a small number of partitions. If in MBRonly 4 records were placed with a restriction on the length of the partition and its offset due to the fact that these parameters were described by 32-bit numbers, then 128 sections records can be placed in the GPT . Moreover, their parameters are already described in a 64-bit number system ...

    For compatibility with the old standard for loading (BIOS) and in order to protect the GPT partition table itself, the GPT partition also starts with MBR , which describes only one partition - all MU memory. MBR itself is now called "protective" (protective), i.e. PMBR It is also located in the first memory sector at the address 0x0000-0x01FF (512 bytes). The Offset field indicates the beginning of the GPT header., and the partition size is set equal to the length of the entire device memory. The partition type is set to 0xEE (GPT partition).

    The essence of protecting the GPT section and the entire memory of the MU is as follows. If such a section opens the means for working with the MBR markup diagram, then it will see empty unallocated memory consisting only of the markup description section. Accordingly, he will not succeed in doing anything with this memory.

    Here's what PMBR looks like , for example, from MU ...:

    PMBR

    Fig. 1 “Protective” MBR, i.e. PMBR

    At 0x01FE , the signature MBR (0xAA55) is visible .

    We proceed to consider directly the structure of the GPT section.

    2. GPT structure


    The GPT section consists of a PMBR , a header, and a description section for the memory partitions.

    Immediately after the PMBR , i.e. from the address 0x0200 , the GPT header is located , having a length of 0x5C (92) bytes, but occupying the entire sector (512 bytes). Here is how it looks in the same MU :

    GPT header


    Fig. 2 GPT header.

    It has the following structure:

     ===============================================================================
    | Смещение |       Имя       | Длина |                 Значение                 |
    |   байт   |       поля      |  байт |                   поля                   |
    |==========|=================|=======|==========================================|
    |   0x00   | Magic           |   8   | сигнатура раздела                        |
    |   0x08   | Revision        |   4   | версия GPT-раздела                       |
    |   0x0C   | HeaderSize      |   4   | размер заголовка в байтах                |
    |   0x10   | HeaderCRC32     |   4   | CRC32 заголовка                          |
    |   0x14   | Reserved1       |   4   | резерв; должен быть 0                    |
    |   0x18   | MyLBA           |   8   | LBA первичного GPT-раздела               |
    |   0x20   | AlternateLBA    |   8   | LBA резервного GPT-раздела               |
    |   0x28   | FirstUsableLBA  |   8   | LBA первого сектора для использования    |
    |   0x30   | LastUsableLBA   |   8   | LBA последнего сектора для использования |
    |   0x38   | DiskGUID        |  16   | GUID диска (памяти)                      |
    |   0x48   | PartitionsLBA   |   8   | LBA первого сектора GPT                  |
    |   0x50   | NumberParts     |   4   | число записей о разделах                 |
    |   0x54   | PartitionSize   |   4   | размер записи о разделе в байтах         |
    |   0x58   | PartitionsCRC32 |   4   | CRC32 таблицы записей о разделах         |
    |   0x5C   | Reserved2       |   *   | резерв                                   |
     ===============================================================================
    

    The Magic field contains the character string “EFI PART” - 45h 46h 49h 20h 50h 41h 52h 54h, which is highlighted in turquoise in Fig. 2 and uniquely identifies the GPT section.

    The Revision field , highlighted in green, contains the version number of the GPT partition structure (0x00010000). For example, for GPT version 1.0 it should be 00h 00h 01h 00h

    The HeaderSize field , highlighted in orange, contains the size of the header, expressed in bytes. So far this is 0x0000005Ch, which means 92 bytes.

    The HeaderCRC32 field , highlighted in pink, contains the header checksum (0x09BE8E1F) calculated using the CRC32 algorithm. When performing the calculation directly, only 92 bytes are taken into account, and 0 is entered in this field before the calculation.

    The next Reserved1 field is not selected in any way, it is reserved and contains 0.

    The MyLBA field , highlighted in dark blue, contains the offset in the blocks of the primary GPT partition (0x00000001).

    The AlternateLBA field contains the offset in the placement blocks of the backup GPT section. It is not indicated.

    The FirstUsableLBA field , highlighted in red, contains the offset in the placement blocks of the first memory sector allowed for use (0x00000022). It is calculated as follows: LBA of the last sector occupied by the primary GPT partition, + 1. LastUsableLBA

    fieldcontains the offset of the placement of the last sector of memory that is allowed for use. It is calculated as follows: LBA of the first sector of the backup GPT-partition - 1. It is not specified.

    The DiskGUID field highlighted in blue contains the firmware GUID.

    The PartitionsLBA field , highlighted in red, contains the offset of the start of the GPT. The primary is always 2, and the backup is LastUsableLBA.

    The NumberParts field highlighted in bright green contains the size of the section description table, i.e. number of partition entries (0x00000018).

    The PartitionSize field , highlighted in pink, contains the size of one section record in bytes. According to UEFI SpecificationThe record size is fixed at 128 bytes or 0x00000080.

    The PartitionsCRC field , highlighted in red, contains the checksum of the partition description table (0x93D54D33) calculated using the CRC32 algorithm. When performing the calculation, all bytes are taken into account, starting from PartitionsLBA and up to FirstUsableLBA.

    The Reserved2 field contains the backup field. Contains 0 to the end of the sector, i.e. 420 bytes for a 512-byte sector.

    Directly immediately after the heading, starting from the address 0x400 , there is a section description table containing records about each memory section, including the markup section itself. Here's what it looks like:


    Fig . 3 Section description table

    Each 128 byte record has the following structure:

     ====================================================================================
    | Смещение  | Длина, |         Имя         |                 Значение                |
    |   байт    |  байт  |         поля        |                   поля                  |
    |===========|========|=====================|=========================================|
    |  0 | 0x00 |   16   | PartitionTypeGUID   | GUID типа раздела                       |
    | 16 | 0x10 |   16   | UniquePartitionGUID | GUID раздела                            |
    | 32 | 0x20 |    8   | StartingLBA         | LBA первого сектора размещения раздела  |
    | 40 | 0x24 |    8   | EndingLBA           | последний LBA сектор размещения раздела |
    | 48 | 0x30 |    8   | Attributes          | атрибуты раздела                        |
    | 56 | 0x38 |   72   | PartitionName       | строка из имени раздела                 |
     ====================================================================================
    

    The PartitionTypeGUID field , highlighted in red, contains the partition type GUID that defines the file system used to store data in this partition. Each file system receives its own GUID, which uniquely identifies it. The UEFI standard strictly defines only the following GUIDs for partition types:

     =============================================================================
    | Значение GUID                        |   Описание                           |
    |======================================|======================================|
    | 00000000-0000-0000-0000-000000000000 | Неиспользуемый раздел                |
    | C12A7328-F81F-11d2-BA4B-00A0C93EC93B | Системный раздел UEFI                |
    | 024DEE41-33E7-11d3-9D69-0008C781F39F | Раздел, содержащий традиционную MBR  |
     =============================================================================
    

    OS vendors need to generate their own partition type GUIDs to identify them. Some well-known GUIDs can be found in [3] .

    When writing to memory or to an image file, the GUID value is written in a different order. For example, the EID of the EFI system partition is as follows: C12A7328-F81F-11D2-BA4B-00A0C93EC93B. The byte order in the GUID spellings is little-endian, and bytes are written backwards only in the first three blocks. For the above GUID, the entry in the partition table will look like this:

    28 73 2A C1 1F F8 D2 11 BA 4B 00 A0 C9 3E C9 3B

    The UniquePartitionGUID field , highlighted in blue, contains the section GUID. It is a unique identifier for a section, so it is created every time a section is created.

    The StartingLBA field , highlighted in green, contains the offset in blocks by the first sector of the section (0x00020000).

    The EndingLBA field , highlighted in orange, contains the offset by the last sector of the section (0x0003FFFF). The size of the partition (PartitionSize) is determined by the formula

    	PartitionSize = EndingLBA - StartingLBA + 1

    The Attributes field , highlighted in purple, contains the attributes (flags) of the section. 8 bytes (64 bits) of flags are allocated as follows. Bits 0 through 47 (48) are reserved for the general attributes of partition types, and the remaining 16 bits (48 to 63) describe a specific partition.

    Here is a short description of these bits:

     ================================================
    | Бит  |   Значение                              |
    |======|=========================================|
    |   0  | системный раздел                        |
    |   1  | EFI игнорирует содержимое этого раздела |
    | 2-47 | Зарезервировано для нужд UEFI           |
    | ---- |                                         |
    |  60  | Read-only (только для чтения)           |
    |  61  | Теневая копия другого раздела           |
    |  62  | Hidden (скрытый)                        |
    |  63  | автоматически не монтируется            |
     ================================================
    

    The PartitionName field , highlighted in blue, contains the section label (“modem”) containing a line of text with a terminating zero of no more than 36 characters, expressed in UTF-16LE encoding.

    All that GPT is, we already know, only this is the standard structure of the GPT section. It turns out that there is a modification ...

    Because I discovered a GPT section of a different structure when working with Intel- based MUs , I called it a GPT section like Intel .

    3. What is an Intel GPT partition?


    The firmware of new Lenovo mobile devices (MUs) based on Intel chips has a Gpt memory layout, but the structure of the Gpt file itself is different from the standard one described in [1]. This applies, for example, to YOGA BOOK YB1-X90 devices.

    Compared to the standard Intel structure , the type is reduced to the maximum:

    • Guid, checksums, offsets of all partitions except the very first and all standard values ​​or empty, for example, the size of the section parameter record, the placement of Primary and Backup tables;
    • attributes were removed from the description of the section parameters, and the offsets of the first and last block were replaced with the section size.

    Because The structure of the Gpt- file of this type differs from the standard structure, then for its firmware it is necessary to use a special flasher from Intel - PhoneFlashTool_5.3.2.0.

    The GPT section, like the standard one, consists of:

    • header ;
    • Partition parameter description tables .


    Fig. 4. Structure of the Intel-type Gpt-file.

    Consider the structure of the header.

    3.1. The header structure of the new GPT section.


    The header has a size of only 12 ( 0x0C ) bytes (versus 512 in the standard version):


    Fig.5. The header of the Intel-type Gpt-file

    and contains the following fields:

     =============================================================
    |  №  | Смещение |   Имя    | Размер | Значение поля            |
    | п/п |   поля   |   поля   |  байт  |                          |
    |=============================================================|
    |  1  |   0x00   | Magic    |    4   | Сигнатура файла          |
    |  2  |   0x04   | StartLba |    4   | Смещение первого раздела |
    |  3  |   0x08   | Number   |    4   | Число разделов памяти    |
     =============================================================
    

    The Magic field , marked in blue, contains the number 0x6A8B0DA1 , which identifies the image of the Intel GPT section .

    The StartLba field , marked in red, contains the offset value of the placement of the first memory partition.

    The Number field , marked in green, contains the total number of memory partitions, i.e. The number of entries in the section description table. The table itself is located immediately after the header.

    3.2. Description structure of the new GPT section description


    The section description table contains entries containing the parameters of each memory section. The number of entries is equal to the number of sections, and the end of the table is not marked.

    Each record has a size of 108 ( 0x6C ) bytes, while according to UEFI it contained 128 bytes. Here's what the new section description record looks like:


    Fig . 6 The section description record

    and contains the following fields that describe the section parameters:

     =============================================================
    |  №  | Смещение |      Имя      | Размер | Значение поля     |
    | п/п |   поля   |      поля     |  байт  |                   |
    |=============================================================|
    |  1  |   0x00   | Size          |    4   | Размер раздела    |
    |  2  |   0x04   | Label         |   72   | метка раздела     |
    |  3  |   0x4C   | GuidType      |   16   | GUID типа раздела |
    |  3  |   0x5C   | GuidPartition |   16   | GUID раздела      |
     =============================================================
    

    The Size field , highlighted in blue in Fig. 6, contains the size of the partition, expressed in MB (1024 * 1024 = 1048576 bytes).

    The Label field highlighted in red in Fig. 6 contains the section label, i.e. section name expressed in UTF-16 encoding.

    The GuidType field , highlighted in green in Figure 6, contains the section type GUID.

    The GuidPartition field , highlighted in yellow in Figure 6, contains the GUID of the section itself.

    4. Conclusion


    Having studied the structure of the GPT- section of the memory layout of the MU , you can start practical exercises.

    In the following publications, I will share the experience of re-marking the memory of the MU performed according to the GPT scheme.

    5. Sources of information


    1. "What is an MBR?"
    2. UEFI Specification, Version 2.7 Errata A
    3. GUID Partition Table.

    Also popular now: