Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d1628288 authored by Sami Tolvanen's avatar Sami Tolvanen
Browse files

mkbootimg: add os_version

Adds operating system version and security patch level to the boot
image header. This change take into use one of the existing unused
fields to preserve compatibility.

Note: All information is packed into a single field, because there
are a lot of existing devices that break if the first unused field
is set to a non-zero value.

Bug: 27498078
Bug: 22914603
Change-Id: I24953129e06019b95014a050e916fe4f5c199286
parent ff038574
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -43,7 +43,14 @@ struct boot_img_hdr

    uint32_t tags_addr;    /* physical addr for kernel tags */
    uint32_t page_size;    /* flash page size we assume */
    uint32_t unused[2];    /* future expansion: should be 0 */
    uint32_t unused;       /* reserved for future expansion: MUST be 0 */

    /* operating system version and security patch level; for
     * version "A.B.C" and patch level "Y-M-D":
     * ver = A << 14 | B << 7 | C         (7 bits for each of A, B, C)
     * lvl = ((Y - 2000) & 127) << 4 | M  (7 bits for Y, 4 bits for M)
     * os_version = ver << 11 | lvl */
    uint32_t os_version;

    uint8_t name[BOOT_NAME_SIZE]; /* asciiz product name */

+35 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ from os import fstat
from struct import pack
from hashlib import sha1
import sys
import re

def filesize(f):
    if f is None:
@@ -47,7 +48,7 @@ def pad_file(f, padding):
def write_header(args):
    BOOT_MAGIC = 'ANDROID!'.encode()
    args.output.write(pack('8s', BOOT_MAGIC))
    args.output.write(pack('8I',
    args.output.write(pack('10I',
        filesize(args.kernel),                          # size in bytes
        args.base + args.kernel_offset,                 # physical load addr
        filesize(args.ramdisk),                         # size in bytes
@@ -55,8 +56,9 @@ def write_header(args):
        filesize(args.second),                          # size in bytes
        args.base + args.second_offset,                 # physical load addr
        args.base + args.tags_offset,                   # physical addr for kernel tags
        args.pagesize))                                 # flash page size we assume
    args.output.write(pack('8x'))                       # future expansion: should be 0
        args.pagesize,                                  # flash page size we assume
        0,                                              # future expansion: MUST be 0
        (args.os_version << 11) | args.os_patch_level)) # os version and patch level
    args.output.write(pack('16s', args.board.encode())) # asciiz product name
    args.output.write(pack('512s', args.cmdline[:512].encode()))

@@ -97,6 +99,32 @@ def write_padded_file(f_out, f_in, padding):
def parse_int(x):
    return int(x, 0)

def parse_os_version(x):
    match = re.search(r'^(\d{1,3})(?:\.(\d{1,3})(?:\.(\d{1,3}))?)?', x)
    if match:
        a = parse_int(match.group(1))
        b = c = 0
        if match.lastindex >= 2:
            b = parse_int(match.group(2))
        if match.lastindex == 3:
            c = parse_int(match.group(3))
        # 7 bits allocated for each field
        assert a < 128
        assert b < 128
        assert c < 128
        return (a << 14) | (b << 7) | c
    return 0

def parse_os_patch_level(x):
    match = re.search(r'^(\d{4})-(\d{2})-(\d{2})', x)
    if match:
        y = parse_int(match.group(1)) - 2000
        m = parse_int(match.group(2))
        # 7 bits allocated for the year, 4 bits for the month
        assert y >= 0 and y < 128
        assert m > 0 and m <= 12
        return (y << 4) | m
    return 0

def parse_cmdline():
    parser = ArgumentParser()
@@ -111,6 +139,10 @@ def parse_cmdline():
    parser.add_argument('--ramdisk_offset', help='ramdisk offset', type=parse_int, default=0x01000000)
    parser.add_argument('--second_offset', help='2nd bootloader offset', type=parse_int,
                        default=0x00f00000)
    parser.add_argument('--os_version', help='operating system version', type=parse_os_version,
                        default=0)
    parser.add_argument('--os_patch_level', help='operating system patch level',
                        type=parse_os_patch_level, default=0)
    parser.add_argument('--tags_offset', help='tags offset', type=parse_int, default=0x00000100)
    parser.add_argument('--board', help='board name', default='', action=ValidateStrLenAction,
                        maxlen=16)