summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xtools/extract_head_tail.sh20
-rwxr-xr-xtools/mk_combined_img.py105
-rwxr-xr-xtools/mk_vbmeta_boot_params.sh5
-rw-r--r--tools/prebuilt/gpt/1_3080/head.imgbin0 -> 1048576 bytes
-rw-r--r--tools/prebuilt/gpt/1_3080/tail.imgbin0 -> 1048576 bytes
5 files changed, 96 insertions, 34 deletions
diff --git a/tools/extract_head_tail.sh b/tools/extract_head_tail.sh
new file mode 100755
index 0000000..5941da7
--- /dev/null
+++ b/tools/extract_head_tail.sh
@@ -0,0 +1,20 @@
+#!/bin/bash
+
+set -e
+
+if [ "$#" -ne 3 ]; then
+ echo "$0 src.img head.img tail.img" >&2
+ exit 1
+fi
+
+srcimg=$1
+headimg=$2
+tailimg=$3
+
+disksize=$(stat -c %s $srcimg)
+
+mycount=`expr $disksize \/ 1024 \/ 1024 - 1`
+
+dd if=$srcimg of=$headimg ibs=1M obs=1M count=1
+dd if=$srcimg of=$tailimg ibs=1M obs=1M count=1 skip=$mycount
+#dd if=/dev/zero of=file.txt count=3083 bs=1M
diff --git a/tools/mk_combined_img.py b/tools/mk_combined_img.py
index 1b7bbff..1e35dc6 100755
--- a/tools/mk_combined_img.py
+++ b/tools/mk_combined_img.py
@@ -1,26 +1,33 @@
#!/usr/bin/python
-import sys
+
+from __future__ import print_function
+
+import argparse
+import codecs
+import operator
import os
+import sys
from subprocess import Popen, PIPE
from tempfile import mkstemp
-import argparse
-import operator
def check_sparse(filename):
magic = 3978755898
with open(filename, 'rb') as i:
word = i.read(4)
- if magic == int(word[::-1].encode('hex'), 16):
+ if magic == int(codecs.encode(word[::-1], 'hex'), 16):
return True
return False
+
def shell_command(comm_list):
- command = Popen(comm_list)
- execute = command.wait()
+ command = Popen(comm_list, stdout=PIPE, stderr=PIPE)
+ command.communicate()
+ command.wait()
if command.returncode != 0:
sys.exit(1)
+
def parse_input(input_file):
parsed_lines = list()
lines = input_file.readlines()
@@ -39,24 +46,27 @@ def parse_input(input_file):
num_used = set()
for line in parsed_lines:
partition_info = dict()
- partition_info["path"] = line[0]
+ partition_info["path"] = line[0]
partition_info["label"] = line[1]
+ # round up by 1M
+ sizeByMb = str((1024 * 1024 - 1 + os.path.getsize(line[0])) / 1024 / 1024)
+ partition_info["sizeByMb"] = sizeByMb
try:
partition_info["num"] = int(line[2])
except ValueError:
- print "'%s' cannot be converted to int" % (line[2])
+ print("'%s' cannot be converted to int" % (line[2]))
sys.exit(1)
# check if the partition number is out of range
if partition_info["num"] > len(lines) or partition_info["num"] < 0:
- print "Invalid partition number: %d, range [1..%d]" % \
- (partition_info["num"], len(lines))
+ print("Invalid partition number: %d, range [1..%d]" % \
+ (partition_info["num"], len(lines)))
sys.exit(1)
# check if the partition number is duplicated
if partition_info["num"] in num_used:
- print "Duplicated partition number:%d" % (partition["num"])
+ print("Duplicated partition number:%d" % (partition_info["num"]))
sys.exit(1)
num_used.add(partition_info["num"])
partitions.append(partition_info)
@@ -64,42 +74,47 @@ def parse_input(input_file):
partitions.sort(key=operator.itemgetter("num"))
return partitions
+
def write_partition(partition, output_file, offset):
# $ dd if=/path/to/image of=/path/to/output conv=notrunc,sync \
# ibs=1024k obs=1024k seek=<offset>
- dd_comm = ['dd', 'if='+partition["path"], 'of='+output_file,'conv=notrunc,sync',
- 'ibs=1024k','obs=1024k', 'seek='+str(offset)]
+ dd_comm = ['dd', 'if=' + partition["path"], 'of=' + output_file, 'conv=notrunc,sync',
+ 'ibs=1024k', 'obs=1024k', 'seek=' + str(offset)]
shell_command(dd_comm)
return
+
def unsparse_partition(partition):
# if the input image is in sparse format, unsparse it
simg2img = os.environ.get('SIMG2IMG', 'simg2img')
- print "Unsparsing %s" % (partition["path"]),
+ print("Unsparsing %s" % (partition["path"]), end=' ')
partition["fd"], temp_file = mkstemp()
shell_command([simg2img, partition["path"], temp_file])
partition["path"] = temp_file
- print "Done"
+ print("Done")
return
+
def clear_partition_table(filename):
sgdisk = os.environ.get('SGDISK', 'sgdisk')
- print "%s --clear %s" % (sgdisk, filename)
+ print("%s --clear %s" % (sgdisk, filename))
shell_command([sgdisk, '--clear', filename])
return
+
def add_partition(partition, output_file):
sgdisk = os.environ.get('SGDISK', 'sgdisk')
num = str(partition["num"])
- new_comm = '--new='+num+':'+partition["start"]+':'+partition["end"]
- type_comm = '--type='+num+':8300'
- name_comm = '--change-name='+num+':'+partition["label"]
+ new_comm = '--new=' + num + ':' + partition["start"] + ':' + partition["end"]
+ type_comm = '--type=' + num + ':8300'
+ name_comm = '--change-name=' + num + ':' + partition["label"]
# build partition table in order. for example:
# $ sgdisk --new=1:2048:5244927 --type=1:8300 --change-name=1:system \
# /path/to/output
shell_command([sgdisk, new_comm, type_comm, name_comm, output_file])
return
+
def main():
# check usage:
parser = argparse.ArgumentParser()
@@ -108,15 +123,19 @@ def main():
default="image_config")
parser.add_argument("-o", "--output",
type=str, help="output filename",
- default=os.environ.get("OUT", ".")+"/combined.img")
+ default=os.environ.get("OUT", ".") + "/combined.img")
args = parser.parse_args()
output_filename = os.path.expandvars(args.output)
+ # remove the output_filename.qcow2
+ print("removing " + output_filename + ".qcow2")
+ shell_command(['rm', '-rf', output_filename + ".qcow2"])
+
# check input file
config_filename = args.input
if not os.path.exists(config_filename):
- print "Invalid config file name " + config_filename
+ print("Invalid config file name " + config_filename)
sys.exit(1)
# read input file
@@ -124,29 +143,57 @@ def main():
partitions = parse_input(config)
config.close()
+ # take a shortcut in build environment
+ if os.path.exists(output_filename) and len(partitions) == 2:
+ print("updating " + output_filename + " ...")
+ shell_command(['dd', "if=" + partitions[0]["path"], "of=" + output_filename,
+ "conv=notrunc,sync", "ibs=1024k", "obs=1024k", "seek=1"])
+ shell_command(['dd', "if=" + partitions[1]["path"], "of=" + output_filename,
+ "conv=notrunc,sync", "ibs=1024k", "obs=1024k", "seek=2"])
+ print("done")
+ sys.exit(0)
+ elif len(partitions) == 2:
+ gptprefix = partitions[0]["sizeByMb"] + "_" + partitions[1]["sizeByMb"]
+ prebuilt_gpt_dir = os.path.dirname(os.path.abspath( __file__ )) + "/prebuilt/gpt/" + gptprefix
+ gpt_head = prebuilt_gpt_dir + "/head.img"
+ gpt_tail = prebuilt_gpt_dir + "/head.img"
+ if os.path.exists(gpt_head) and os.path.exists(gpt_tail):
+ print("found prebuilt gpt header and footer, use it")
+ shell_command(['dd', "if=" + gpt_head, "of=" + output_filename, "bs=1024k",
+ "conv=notrunc,sync", "count=1"])
+ shell_command(['dd', "if=" + partitions[0]["path"], "of=" + output_filename,
+ "bs=1024k", "conv=notrunc,sync", "seek=1"])
+ shell_command(['dd', "if=" + partitions[1]["path"], "of=" + output_filename,
+ "bs=1024k", "conv=notrunc,sync", "seek=" + str(1 + int(partitions[0]["sizeByMb"]))])
+ shell_command(['dd', "if=" + gpt_tail, "of=" + output_filename,
+ "bs=1024k", "conv=notrunc,sync",
+ "seek=" + str(1 + int(partitions[0]["sizeByMb"]) + int(partitions[1]["sizeByMb"]))])
+ print("done")
+ sys.exit(0)
+
# combine the images
# add padding
- shell_command(['dd', 'if=/dev/zero', 'of='+output_filename, 'ibs=1024k', 'count=1'])
+ shell_command(['dd', 'if=/dev/zero', 'of=' + output_filename, 'ibs=1024k', 'count=1'])
for partition in partitions:
offset = os.path.getsize(output_filename)
- partition["start"] = str(offset / 512)
+ partition["start"] = str(offset // 512)
# dectect sparse file format
if check_sparse(partition["path"]):
unsparse_partition(partition)
# TODO: extract the partition if the image file is already formatted
- write_partition(partition, output_filename, offset/1024/1024)
+ write_partition(partition, output_filename, offset // 1024 // 1024)
offset = os.path.getsize(output_filename)
- partition["end"] = str(offset / 512 - 1)
+ partition["end"] = str(offset // 512 - 1)
# add padding
# $ dd if=/dev/zero of=/path/to/output conv=notrunc bs=1 \
# count=1024k seek=<offset>
- offset = os.path.getsize(output_filename) / 1024 / 1024
- shell_command(['dd', 'if=/dev/zero', 'of='+output_filename,
- 'conv=notrunc', 'bs=1024k', 'count=1', 'seek='+str(offset)])
+ offset = os.path.getsize(output_filename) // 1024 // 1024
+ shell_command(['dd', 'if=/dev/zero', 'of=' + output_filename,
+ 'conv=notrunc', 'bs=1024k', 'count=1', 'seek=' + str(offset)])
# make partition table
# $ sgdisk --clear /path/to/output
@@ -159,6 +206,6 @@ def main():
os.close(partition["fd"])
os.remove(partition["path"])
+
if __name__ == "__main__":
main()
-
diff --git a/tools/mk_vbmeta_boot_params.sh b/tools/mk_vbmeta_boot_params.sh
index 17a39e8..fbdce56 100755
--- a/tools/mk_vbmeta_boot_params.sh
+++ b/tools/mk_vbmeta_boot_params.sh
@@ -42,14 +42,11 @@ readonly TARGET=$3
# Extract the digest
readonly VBMETA_DIGEST=$(${AVBTOOL:-avbtool} calculate_vbmeta_digest --image $VBMETAIMG)
-echo "digest is $VBMETA_DIGEST"
readonly INFO_OUTPUT=$(${AVBTOOL:-avbtool} info_image --image $VBMETAIMG | grep "^Algorithm:")
-echo "output is $INFO_OUTPUT"
# Extract the algorithm
readonly ALG_OUTPUT=$(echo $INFO_OUTPUT | grep "Algorithm:")
-echo \"$ALG_OUTPUT\"
readonly ALG_SPLIT=($(echo $ALG_OUTPUT | tr ' ' '\n'))
readonly ORG_ALGORITHM=${ALG_SPLIT[1]}
@@ -59,7 +56,6 @@ else
die "Don't know anything about $ORG_ALGORITHM"
fi
-echo "hash algorithm is $VBMETA_HASH_ALG"
# extract the size
@@ -77,7 +73,6 @@ SYSMETA_SIZE=$(get_bytes $SYSIMG "VBMeta size:")
VBMETA_SIZE=$(expr $HEADER_SIZE + $AUTHEN_SIZE + $AUX_SIZE + $SYSMETA_SIZE)
-echo "vbmeta size $VBMETA_SIZE"
HEADER_COMMENT="# androidboot.vbmeta.size=$VBMETA_SIZE androidboot.vbmeta.hash_alg=$VBMETA_HASH_ALG androidboot.vbmeta.digest=$VBMETA_DIGEST"
diff --git a/tools/prebuilt/gpt/1_3080/head.img b/tools/prebuilt/gpt/1_3080/head.img
new file mode 100644
index 0000000..f16ea54
--- /dev/null
+++ b/tools/prebuilt/gpt/1_3080/head.img
Binary files differ
diff --git a/tools/prebuilt/gpt/1_3080/tail.img b/tools/prebuilt/gpt/1_3080/tail.img
new file mode 100644
index 0000000..2f69829
--- /dev/null
+++ b/tools/prebuilt/gpt/1_3080/tail.img
Binary files differ