OSDN Git Service

build_verity_metadata: Support --signer_args argument. am: 1522691d1d am: c2e9c0f0fc...
[android-x86/system-extras.git] / verity / build_verity_metadata.py
1 #! /usr/bin/env python
2 #
3 # Copyright (C) 2013 The Android Open Source Project
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import argparse
18 import os
19 import sys
20 import struct
21 import tempfile
22 import commands
23
24 VERSION = 0
25 MAGIC_NUMBER = 0xb001b001
26 BLOCK_SIZE = 4096
27 METADATA_SIZE = BLOCK_SIZE * 8
28
29 def run(cmd):
30     status, output = commands.getstatusoutput(cmd)
31     print output
32     if status:
33         exit(-1)
34
35 def get_verity_metadata_size(data_size):
36     return METADATA_SIZE
37
38 def build_metadata_block(verity_table, signature):
39     table_len = len(verity_table)
40     block = struct.pack("II256sI", MAGIC_NUMBER, VERSION, signature, table_len)
41     block += verity_table
42     block = block.ljust(METADATA_SIZE, '\x00')
43     return block
44
45 def sign_verity_table(table, signer_path, key_path, signer_args=None):
46     if signer_args is None:
47         signer_args = ''
48
49     with tempfile.NamedTemporaryFile(suffix='.table') as table_file:
50         with tempfile.NamedTemporaryFile(suffix='.sig') as signature_file:
51             table_file.write(table)
52             table_file.flush()
53             cmd = " ".join((signer_path, signer_args, table_file.name,
54                             key_path, signature_file.name))
55             print cmd
56             run(cmd)
57             return signature_file.read()
58
59 def build_verity_table(block_device, data_blocks, root_hash, salt):
60     table = "1 %s %s %s %s %s %s sha256 %s %s"
61     table %= (  block_device,
62                 block_device,
63                 BLOCK_SIZE,
64                 BLOCK_SIZE,
65                 data_blocks,
66                 data_blocks,
67                 root_hash,
68                 salt)
69     return table
70
71 def build_verity_metadata(data_blocks, metadata_image, root_hash, salt,
72         block_device, signer_path, signing_key, signer_args=None):
73     # build the verity table
74     verity_table = build_verity_table(block_device, data_blocks, root_hash, salt)
75     # build the verity table signature
76     signature = sign_verity_table(verity_table, signer_path, signing_key, signer_args)
77     # build the metadata block
78     metadata_block = build_metadata_block(verity_table, signature)
79     # write it to the outfile
80     with open(metadata_image, "wb") as f:
81         f.write(metadata_block)
82
83 if __name__ == "__main__":
84     parser = argparse.ArgumentParser()
85     subparsers = parser.add_subparsers()
86
87     parser_size = subparsers.add_parser('size')
88     parser_size.add_argument('partition_size', type=int, action='store', help='partition size')
89     parser_size.set_defaults(dest='size')
90
91     parser_build = subparsers.add_parser('build')
92     parser_build.add_argument('blocks', type=int, help='data image blocks')
93     parser_build.add_argument('metadata_image', action='store', help='metadata image')
94     parser_build.add_argument('root_hash', action='store', help='root hash')
95     parser_build.add_argument('salt', action='store', help='salt')
96     parser_build.add_argument('block_device', action='store', help='block device')
97     parser_build.add_argument('signer_path', action='store', help='verity signer path')
98     parser_build.add_argument('signing_key', action='store', help='verity signing key')
99     parser_build.add_argument('--signer_args', action='store', help='verity signer args')
100     parser_build.set_defaults(dest='build')
101
102     args = parser.parse_args()
103
104     if args.dest == 'size':
105         print get_verity_metadata_size(args.partition_size)
106     else:
107         build_verity_metadata(args.blocks / 4096, args.metadata_image,
108                               args.root_hash, args.salt, args.block_device,
109                               args.signer_path, args.signing_key,
110                               args.signer_args)