aboutsummaryrefslogtreecommitdiffstats
path: root/tests/spi25.c
blob: 251c98c09ec1db48e844d03e91181a955e92c127 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * This file is part of the flashrom project.
 *
 * Copyright 2020 Google LLC
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <include/test.h>

#include "programmer.h"
#include "flashchips.h"
#include "chipdrivers.h"
#include "spi.h"

struct flashchip mock_chip = {
	.vendor		= "Generic",
	.name		= "unknown SPI chip (RDID)",
	.bustype	= BUS_SPI,
	.manufacture_id	= GENERIC_MANUF_ID,
	.model_id	= GENERIC_DEVICE_ID,
	.total_size	= 0,
	.page_size	= 256,
	.tested		= TEST_BAD_PREW,
	.probe		= probe_spi_rdid,
	.write		= NULL,
};

/*
 * This declaration is needed for visibility, so that wrap below could
 * redirect to real function.
 */
int __real_spi_send_command(const struct flashctx *flash,
		unsigned int writecnt, unsigned int readcnt,
		const unsigned char *writearr, unsigned char *readarr);

int __wrap_spi_send_command(const struct flashctx *flash,
		unsigned int writecnt, unsigned int readcnt,
		const unsigned char *writearr, unsigned char *readarr)
{
	if (flash->chip != &mock_chip)
		/*
		 * Caller is some other test, redirecting to real function.
		 * This test is the only one which uses wrap of spi_send_command,
		 * all other tests use real function.
		*/
		return __real_spi_send_command(flash, writecnt, readcnt, writearr, readarr);

	check_expected_ptr(flash);
	assert_int_equal(writecnt,    mock_type(int));
	assert_int_equal(writearr[0], mock_type(int));

	int rcnt = mock_type(int);
	assert_int_equal(readcnt, rcnt);
	for (int i = 0; i < rcnt; i++)
		readarr[i] = i;

	return 0;
}

void spi_write_enable_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_WREN_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_WREN);
	will_return(__wrap_spi_send_command, JEDEC_WREN_INSIZE);
	assert_int_equal(0, spi_write_enable(&flashctx));
}

void spi_write_disable_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_WRDI_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_WRDI);
	will_return(__wrap_spi_send_command, JEDEC_WRDI_INSIZE);
	assert_int_equal(0, spi_write_disable(&flashctx));
}

void probe_spi_rdid_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_RDID);
	will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE);
	assert_int_equal(0, probe_spi_rdid(&flashctx));
}

void probe_spi_rdid4_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_RDID);
	will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE + 1);
	assert_int_equal(0, probe_spi_rdid4(&flashctx));
}

void probe_spi_rems_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_REMS_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_REMS);
	will_return(__wrap_spi_send_command, JEDEC_REMS_INSIZE);
	assert_int_equal(0, probe_spi_rems(&flashctx));
}

void probe_spi_res1_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_RES);
	will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1);
	assert_int_equal(0, probe_spi_res2(&flashctx));
}

void probe_spi_res2_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	clear_spi_id_cache();
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_RES);
	will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1);
	assert_int_equal(0, probe_spi_res2(&flashctx));
}

void probe_spi_res3_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE);
	will_return(__wrap_spi_send_command, JEDEC_RES);
	will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 2);
	assert_int_equal(0, probe_spi_res3(&flashctx));
}

void probe_spi_at25f_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	will_return(__wrap_spi_send_command, AT25F_RDID_OUTSIZE);
	will_return(__wrap_spi_send_command, AT25F_RDID);
	will_return(__wrap_spi_send_command, AT25F_RDID_INSIZE);
	assert_int_equal(0, probe_spi_at25f(&flashctx));
}

/* spi95.c */
void probe_spi_st95_test_success(void **state)
{
	(void) state; /* unused */

	/* setup initial test state. */
	struct flashctx flashctx = { .chip = &mock_chip };
	expect_memory(__wrap_spi_send_command, flash,
			&flashctx, sizeof(flashctx));

	/* chip total size < 64K. */
	uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address

	will_return(__wrap_spi_send_command, rdid_outsize);
	will_return(__wrap_spi_send_command, ST_M95_RDID);
	will_return(__wrap_spi_send_command, ST_M95_RDID_INSIZE);
	assert_int_equal(0, probe_spi_st95(&flashctx));
}