summaryrefslogtreecommitdiff
path: root/test/flash_test_util.py
blob: c0194a3a6ef848e087f2684c857ec393c3eecba2 (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
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Utility functions for flash related test
#

import random

# Fixed random seed.
random.seed(1234)

def get_flash_size(helper):
    helper.ec_command("hcflashinfo")
    return int(helper.wait_output("flash_size = (?P<f>\d+)", use_re=True)["f"])

def get_ro_size(helper):
    helper.ec_command("rosize")
    return int(helper.wait_output(
        "RO image size = (?P<ro>0x[0-9a-f]+)", use_re=True)["ro"], 16)

def xor_sum(size, seed, mult, add):
    ret = 0
    for i in xrange(size):
        ret ^= (seed & 0xff)
        seed = seed * mult + add
    return ret

def test_write(helper, offset, size, expect_fail=False):
    seed = random.randint(2, 10000)
    mult = random.randint(2, 10000)
    add  = random.randint(2, 10000)
    helper.ec_command("hcflashwrite %d %d %d %d %d" %
                      (offset, size, seed, mult, add))
    if expect_fail:
        helper.wait_output("Command returned error")
    else:
        expected_sum = xor_sum(size, seed, mult, add)
        helper.wait_output("Flash write at %x size %x XOR %x" %
                           (offset, size, expected_sum))