summaryrefslogtreecommitdiff
path: root/releasetools.py
blob: 368834a3138b9c88bcd8278a1bbb71dfca9dcdd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Emit extra commands needed for Group during OTA installation
(installing the bootloader)."""

import struct
import common


def WriteIfwi(info):
  info.script.AppendExtra('package_extract_file("ifwi.bin", "/tmp/ifwi.bin");')
  info.script.AppendExtra("""fugu.flash_ifwi("/tmp/ifwi.bin");""")

def WriteDroidboot(info):
  info.script.WriteRawImage("/fastboot", "droidboot.img")

def WriteSplashscreen(info):
  info.script.WriteRawImage("/splashscreen", "splashscreen.img")

def WriteBootloader(info, bootloader):
  header_fmt = "<8sHHI"
  header_size = struct.calcsize(header_fmt)
  magic, revision, reserved, reserved = struct.unpack(
    header_fmt, bootloader[:header_size])

  assert magic == "BOOTLDR!", "bootloader.img bad magic value"

  if revision == 1:
    offset = header_size;
    header_v1_fmt = "II"
    header_v1_size = struct.calcsize(header_v1_fmt)
    ifwi_size, droidboot_size = struct.unpack(header_v1_fmt, bootloader[offset:offset + header_v1_size])
    offset += header_v1_size
    ifwi = bootloader[offset:offset + ifwi_size]
    offset += ifwi_size
    droidboot = bootloader[offset:]
    common.ZipWriteStr(info.output_zip, "droidboot.img", droidboot)
    common.ZipWriteStr(info.output_zip, "ifwi.bin", ifwi)
    WriteIfwi(info)
    WriteDroidboot(info)
    return

  offset = header_size;
  while offset < len(bootloader):
    c_header_fmt = "<8sIBBBB"
    c_header_size = struct.calcsize(c_header_fmt)
    c_magic, size, flags, _, _ , _ = struct.unpack(c_header_fmt, bootloader[offset:offset + c_header_size])
    buf = bootloader[offset + c_header_size: offset + c_header_size + size]
    offset += c_header_size + size

    if not flags & 1:
      continue

    if c_magic == "IFWI!!!!":
      common.ZipWriteStr(info.output_zip, "ifwi.bin", buf)
      WriteIfwi(info);
      continue
    if c_magic == "DROIDBT!":
      common.ZipWriteStr(info.output_zip, "droidboot.img", buf)
      WriteDroidboot(info);
      continue
    if c_magic == "SPLASHS!":
      common.ZipWriteStr(info.output_zip, "splashscreen.img", buf)
      WriteSplashscreen(info);
      continue

def FullOTA_InstallEnd(info):
  try:
    bootloader_img = info.input_zip.read("RADIO/bootloader.img")
  except KeyError:
    print "no bootloader.img in target_files; skipping install"
  else:
    WriteBootloader(info, bootloader_img)


def IncrementalOTA_InstallEnd(info):
  try:
    target_bootloader_img = info.target_zip.read("RADIO/bootloader.img")
    try:
      source_bootloader_img = info.source_zip.read("RADIO/bootloader.img")
    except KeyError:
      source_bootloader_img = None

    if source_bootloader_img == target_bootloader_img:
      print "bootloader unchanged; skipping"
    else:
      print "bootloader changed; adding it"
      WriteBootloader(info, target_bootloader_img)
  except KeyError:
    print "no bootloader.img in target target_files; skipping install"