diff options
author | Vic Yang <victoryang@chromium.org> | 2012-08-17 15:00:27 +0800 |
---|---|---|
committer | Gerrit <chrome-bot@google.com> | 2012-08-19 09:56:32 -0700 |
commit | f229fabd8be499b45fbb801b1458f3dd2c58bd32 (patch) | |
tree | d4e26197a5a7637e99803577ad20d3fac15691dd | |
parent | 088a248c794e8f2a48417256fc5fc5d0e9727ef2 (diff) | |
download | chrome-ec-f229fabd8be499b45fbb801b1458f3dd2c58bd32.tar.gz |
Fix flash_overwrite unit test
This also moves flash related tests to use new 'hostcmd' console command.
BUG=chrome-os-partner:10262
TEST=Test passed
BRANCH=none
Change-Id: I5616bfa93bcde0beb4cb2baf2d38e8b5d827c275
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/30665
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r-- | chip/lm4/mock_flash.c | 12 | ||||
-rw-r--r-- | test/flash.c | 154 | ||||
-rw-r--r-- | test/flash_overwrite.py | 16 | ||||
-rw-r--r-- | test/flash_test_util.py | 41 |
4 files changed, 52 insertions, 171 deletions
diff --git a/chip/lm4/mock_flash.c b/chip/lm4/mock_flash.c index 1261829dbd..8200b66c91 100644 --- a/chip/lm4/mock_flash.c +++ b/chip/lm4/mock_flash.c @@ -57,7 +57,17 @@ int flash_physical_get_protect(int block) return mock_protect[block]; } -int flash_physical_pre_init(void) +uint32_t flash_get_protect(void) +{ + return 0; +} + +int flash_set_protect(uint32_t mask, uint32_t flags) +{ + return 0; +} + +int flash_pre_init(void) { return EC_SUCCESS; } diff --git a/test/flash.c b/test/flash.c index 2ab0b749fb..1cc40382fd 100644 --- a/test/flash.c +++ b/test/flash.c @@ -21,157 +21,3 @@ DECLARE_CONSOLE_COMMAND(rosize, ro_image_size, NULL, "Report size of RO image", NULL); - -/* TODO(victoryang@): We should introduce a function to send fake host command - * just like ec_command in ectool. See crosbug/p/11350 */ -static int hc_flash_info(int argc, char **argv) -{ - uint8_t data[EC_HOST_PARAM_SIZE]; - enum ec_status res; - struct ec_response_flash_info *r; - struct host_cmd_handler_args args = - { .command = EC_CMD_FLASH_INFO, - .version = 0, - .params = NULL, - .params_size = 0, - .response = data, - .response_size = EC_HOST_PARAM_SIZE }; - - res = host_command_process(&args); - if (res != EC_RES_SUCCESS) - return EC_ERROR_UNKNOWN; - r = (struct ec_response_flash_info *)args.response; - uart_printf("flash_size = %d\n", r->flash_size); - uart_printf("write_block_size = %d\n", r->write_block_size); - uart_printf("erase_block_size = %d\n", r->erase_block_size); - uart_printf("protect_block_size = %d\n", r->protect_block_size); - - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(hcflashinfo, hc_flash_info, - NULL, NULL, NULL); - -static int hc_flash_read(int argc, char **argv) -{ - uint8_t data[EC_HOST_PARAM_SIZE]; - enum ec_status res; - struct ec_params_flash_read *p = - (struct ec_params_flash_read *)data; - struct host_cmd_handler_args args = - { .command = EC_CMD_FLASH_READ, - .version = 0, - .params = data, - .params_size = EC_HOST_PARAM_SIZE, - .response = data, - .response_size = EC_HOST_PARAM_SIZE }; - char *e; - int i, size; - - if (argc != 3) - return EC_ERROR_PARAM_COUNT; - - p->offset = strtoi(argv[1], &e, 0); - if (*e) - return EC_ERROR_PARAM1; - size = strtoi(argv[2], &e, 0); - p->size = size; - if (*e) - return EC_ERROR_PARAM2; - - res = host_command_process(&args); - if (res != EC_RES_SUCCESS) - return EC_ERROR_UNKNOWN; - for (i = 0; i < size; ++i) { - uart_printf("%02x", args.response[i]); - if ((i & 31) == 31) - uart_puts("\n"); - } - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(hcflashread, hc_flash_read, - NULL, NULL, NULL); - -static int hc_flash_write(int argc, char **argv) -{ - uint8_t data[EC_HOST_PARAM_SIZE]; - enum ec_status res; - struct ec_params_flash_write *p = - (struct ec_params_flash_write *)data; - struct host_cmd_handler_args args = - { .command = EC_CMD_FLASH_WRITE, - .version = 0, - .params = data, - .params_size = EC_HOST_PARAM_SIZE, - .response = data, - .response_size = EC_HOST_PARAM_SIZE }; - char *e; - int i, size; - int seed, mult, add; - - if (argc != 6) - return EC_ERROR_PARAM_COUNT; - - p->offset = strtoi(argv[1], &e, 0); - if (*e) - return EC_ERROR_PARAM1; - size = strtoi(argv[2], &e, 0); - p->size = size; - if (*e) - return EC_ERROR_PARAM2; - seed = strtoi(argv[3], &e, 0); - if (*e) - return EC_ERROR_PARAM3; - mult = strtoi(argv[4], &e, 0); - if (*e) - return EC_ERROR_PARAM4; - add = strtoi(argv[5], &e, 0); - if (*e) - return EC_ERROR_PARAM5; - - for (i = 0; i < size; ++i) { - p->data[i] = (uint8_t)(seed & 0xff); - seed = seed * mult + add; - } - - res = host_command_process(&args); - if (res != EC_RES_SUCCESS) - return EC_ERROR_UNKNOWN; - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(hcflashwrite, hc_flash_write, - NULL, NULL, NULL); - -static int hc_flash_erase(int argc, char **argv) -{ - uint8_t data[EC_HOST_PARAM_SIZE]; - enum ec_status res; - struct ec_params_flash_erase *p = - (struct ec_params_flash_erase *)data; - struct host_cmd_handler_args args = - { .command = EC_CMD_FLASH_ERASE, - .version = 0, - .params = data, - .params_size = EC_HOST_PARAM_SIZE, - .response = data, - .response_size = EC_HOST_PARAM_SIZE }; - char *e; - int size; - - if (argc != 3) - return EC_ERROR_PARAM_COUNT; - - p->offset = strtoi(argv[1], &e, 0); - if (*e) - return EC_ERROR_PARAM1; - size = strtoi(argv[2], &e, 0); - p->size = size; - if (*e) - return EC_ERROR_PARAM2; - - res = host_command_process(&args); - if (res != EC_RES_SUCCESS) - return EC_ERROR_UNKNOWN; - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(hcflasherase, hc_flash_erase, - NULL, NULL, NULL); diff --git a/test/flash_overwrite.py b/test/flash_overwrite.py index 15b3e4817a..93f282ad80 100644 --- a/test/flash_overwrite.py +++ b/test/flash_overwrite.py @@ -23,12 +23,12 @@ def test(helper): rosize = get_ro_size(helper) # We are in RO now. Writing to RO should fail. - test_write(helper, rosize / 2, 0x30, expect_fail=True) + test_write(helper, rosize / 2, 0x10, expect_fail=True) # Writing to RW should succeed. - test_write(helper, rosize, 0x30) # begin of RW - test_write(helper, (rosize + flashsize) / 2, 0x30) # mid-point of RW - test_write(helper, flashsize - 0x30, 0x30) # end of flash + test_write(helper, rosize, 0x10) # begin of RW + test_write(helper, (rosize + flashsize) / 2, 0x10) # mid-point of RW + test_write(helper, flashsize - 0x10, 0x10) # end of flash # Jump to RW-A helper.ec_command("sysjump a") @@ -36,11 +36,11 @@ def test(helper): time.sleep(0.5) # We are in RW now. Writing to RO should succeed. - test_write(helper, 0, 0x30) # begin of RO - test_write(helper, rosize / 2, 0x30) # mid-point of RO - test_write(helper, rosize - 0x30, 0x30) # end of RO + test_write(helper, 0, 0x10) # begin of RO + test_write(helper, rosize / 2, 0x10) # mid-point of RO + test_write(helper, rosize - 0x10, 0x10) # end of RO # Writing to RW-A should fail. - test_write(helper, rosize, 0x30, expect_fail=True) + test_write(helper, rosize, 0x10, expect_fail=True) return True diff --git a/test/flash_test_util.py b/test/flash_test_util.py index 06eea777da..5942ca7e2f 100644 --- a/test/flash_test_util.py +++ b/test/flash_test_util.py @@ -7,13 +7,28 @@ import random import re +import struct # Fixed random seed. random.seed(1234) +def hex_to_byte(text): + return ''.join(["%c" % chr(int(text[i:i+2], 16)) + for i in range(0, len(text), 2)]) + +def byte_to_hex(byte_arr): + return ''.join(["%02x" % ord(c) for c in byte_arr]) + +def offset_size_pair(offset, size): + return byte_to_hex(struct.pack("II", offset, size)) + +def get_flash_info(helper): + helper.ec_command("hostcmd 0x10 0 00") + resp = helper.wait_output("Response: (?P<r>.{32,32})", use_re=True)["r"] + return struct.unpack("IIII", hex_to_byte(resp)) + def get_flash_size(helper): - helper.ec_command("hcflashinfo") - return int(helper.wait_output("flash_size = (?P<f>\d+)", use_re=True)["f"]) + return get_flash_info(helper)[0] def get_ro_size(helper): helper.ec_command("rosize") @@ -28,7 +43,7 @@ def xor_sum(size, seed, mult, add): return ret def test_erase(helper, offset, size): - helper.ec_command("hcflasherase %d %d" % (offset, size)) + helper.ec_command("hostcmd 0x13 0 %s" % offset_size_pair(offset, size)) helper.wait_output("Flash erase at %x size %x" % (offset, size)) def _get_read_ref(helper, offset, size): @@ -38,7 +53,7 @@ def _get_read_ref(helper, offset, size): while size > 0: helper.ec_command("rw %d" % offset) h = helper.wait_output("read.*=\s+0x(?P<h>[0-9a-f]+)", use_re=True)["h"] - # Change endianess here + # Change endianess here retsub.append(re.sub('(..)(..)(..)(..)', r'\4\3\2\1', h)) if len(retsub) == 8: ret.append(''.join(retsub)) @@ -51,18 +66,28 @@ def _get_read_ref(helper, offset, size): def test_read(helper, offset, size): ref = _get_read_ref(helper, offset, size) - helper.ec_command("hcflashread %d %d" % (offset, size)) + helper.ec_command("hostcmd 0x11 0 %s" % offset_size_pair(offset, size)) for line in ref: helper.wait_output(line) +def _gen_data(size, seed, mult, add): + data = [] + for i in xrange(size): + data.append("%02x" % (seed & 255)) + seed = (seed * mult + add) & 4294967295; + return ''.join(data) + def test_write(helper, offset, size, expect_fail=False): + assert size <= 16 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)) + data = _gen_data(size, seed, mult, add) + payload = byte_to_hex(struct.pack("II", offset, size)) + helper.ec_command("hostcmd 0x12 0 %s%s" % + (offset_size_pair(offset, size), data)) if expect_fail: - helper.wait_output("Command returned error") + helper.wait_output("Command returned \d+", use_re=True) else: expected_sum = xor_sum(size, seed, mult, add) helper.wait_output("Flash write at %x size %x XOR %x" % |