summaryrefslogtreecommitdiff
path: root/test/system.c
blob: 6874708cc71ae2d76cbc1f5a40ec05fc26be7188 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Copyright 2013 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.
 *
 * Test system_common.
 */

#include "common.h"
#include "console.h"
#include "host_command.h"
#include "system.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

#define TEST_STATE_STEP_2	(1 << 0)
#define TEST_STATE_FAIL		(1 << 1)

static int test_reboot_on_shutdown(void)
{
	struct ec_params_reboot_ec params;

	/* Fails if the system reboots unexpectedly */
	system_set_scratchpad(TEST_STATE_FAIL);

	test_chipset_on();
	msleep(30);

	params.cmd = EC_REBOOT_COLD;
	params.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;

	TEST_ASSERT(test_send_host_command(
			EC_CMD_REBOOT_EC, 0, &params,
			sizeof(params), NULL, 0) == EC_SUCCESS);

	system_set_scratchpad(TEST_STATE_STEP_2);
	test_chipset_off();
	msleep(30);

	/* Shouldn't reach here */
	return EC_ERROR_UNKNOWN;
}

static int test_cancel_reboot(void)
{
	struct ec_params_reboot_ec params;

	/* Fails if the system reboots unexpectedly */
	system_set_scratchpad(TEST_STATE_FAIL);

	test_chipset_on();
	msleep(30);

	params.cmd = EC_REBOOT_COLD;
	params.flags = EC_REBOOT_FLAG_ON_AP_SHUTDOWN;

	TEST_ASSERT(test_send_host_command(
			EC_CMD_REBOOT_EC, 0, &params,
			sizeof(params), NULL, 0) == EC_SUCCESS);

	params.cmd = EC_REBOOT_CANCEL;
	params.flags = 0;

	TEST_ASSERT(test_send_host_command(
			EC_CMD_REBOOT_EC, 0, &params,
			sizeof(params), NULL, 0) == EC_SUCCESS);

	test_chipset_off();
	msleep(30);

	return EC_SUCCESS;
}

static void run_test_step1(void)
{
	if (test_reboot_on_shutdown() != EC_SUCCESS)
		test_fail();
}

static void run_test_step2(void)
{
	if (test_cancel_reboot() != EC_SUCCESS)
		test_fail();

	system_set_scratchpad(0);
	test_pass();
}

static void fail_and_clean_up(void)
{
	system_set_scratchpad(0);
	test_fail();
}

void run_test(int argc, char **argv)
{
	uint32_t state = system_get_scratchpad();

	test_reset();

	if (state == 0)
		run_test_step1();
	else if (state & TEST_STATE_STEP_2)
		run_test_step2();
	else if (state & TEST_STATE_FAIL)
		fail_and_clean_up();
}