1'''
2This is an example validator to be used with oem commands that allow you to
3upload data afterwards that you wish to validate locally.
4'''
5import sys
6import os
7import hashlib
8
9def eprint(msg):
10  '''
11  A helper function for logging error messages to fuzzy_fastboot
12  Use this function as you would "print()"
13  '''
14  sys.stderr.write(msg + '\n')
15
16
17def main():
18  '''
19  Data is sent back to the parent fuzzy_fastboot process through the stderr pipe.
20
21  If this script has a non-zero return code, anything written to STDERR is part of
22  the error message that will logged by FF to explain why this validation failed.
23
24  Feel free to print to to STDOUT with print() as usual to print info to console
25  '''
26  if len(sys.argv) != 3:
27    eprint("This script is intended to be called by fuzzy_fastboot")
28    return -1
29
30  script, command, fname = sys.argv
31
32  with open(fname, "rb") as fd:
33    buf = fd.read()
34    sha = buf[0x10:0x30]
35    tlvs = buf[0x30:]
36    hash = hashlib.sha256(tlvs)
37    # Assert the hash matches
38    pretty = "".join("{:02x}".format(ord(c)) for c in sha)
39    if hash.digest() != sha:
40      eprint("'%s' does not match '%s'" % (hash.hexdigest(), pretty))
41    assert hash.digest() == sha
42
43  # non-zero return code signals error
44  return 0
45
46
47if __name__ == "__main__":
48  sys.exit(main())
49