| 1 |
/* |
| 2 |
* This file is part of the flashrom project. |
| 3 |
* |
| 4 |
* Copyright 2020 Google LLC |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; version 2 of the License. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <include/test.h> |
| 17 |
|
| 18 |
#include "wraps.h" |
| 19 |
#include "tests.h" |
| 20 |
#include "programmer.h" |
| 21 |
#include "flashchips.h" |
| 22 |
#include "chipdrivers.h" |
| 23 |
#include "spi.h" |
| 24 |
|
| 25 |
struct flashchip mock_chip = { |
| 26 |
.vendor = "Generic", |
| 27 |
.name = "unknown SPI chip (RDID)", |
| 28 |
.bustype = BUS_SPI, |
| 29 |
.manufacture_id = GENERIC_MANUF_ID, |
| 30 |
.model_id = GENERIC_DEVICE_ID, |
| 31 |
.total_size = 0, |
| 32 |
.page_size = 256, |
| 33 |
.tested = TEST_BAD_PREW, |
| 34 |
.probe = PROBE_SPI_RDID, |
| 35 |
.write = NO_WRITE_FUNC, |
| 36 |
}; |
| 37 |
|
| 38 |
/* |
| 39 |
* This declaration is needed for visibility, so that wrap below could |
| 40 |
* redirect to real function. |
| 41 |
*/ |
| 42 |
int __real_spi_send_command(const struct flashctx *flash, |
| 43 |
unsigned int writecnt, unsigned int readcnt, |
| 44 |
const unsigned char *writearr, unsigned char *readarr); |
| 45 |
|
| 46 |
int __wrap_spi_send_command(const struct flashctx *flash, |
| 47 |
unsigned int writecnt, unsigned int readcnt, |
| 48 |
const unsigned char *writearr, unsigned char *readarr) |
| 49 |
{ |
| 50 |
if (flash->chip != &mock_chip) |
| 51 |
/* |
| 52 |
* Caller is some other test, redirecting to real function. |
| 53 |
* This test is the only one which uses wrap of spi_send_command, |
| 54 |
* all other tests use real function. |
| 55 |
*/ |
| 56 |
return __real_spi_send_command(flash, writecnt, readcnt, writearr, readarr); |
| 57 |
|
| 58 |
if (!flash->progress_callback) |
| 59 |
check_expected_ptr(flash); |
| 60 |
|
| 61 |
assert_int_equal(writecnt, mock_type(int)); |
| 62 |
assert_int_equal(writearr[0], mock_type(int)); |
| 63 |
|
| 64 |
int rcnt = mock_type(int); |
| 65 |
assert_int_equal(readcnt, rcnt); |
| 66 |
for (int i = 0; i < rcnt; i++) |
| 67 |
readarr[i] = i; |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static void spi_read_progress_cb(enum flashrom_progress_stage stage, size_t current, size_t total, void* user_data) |
| 73 |
{ |
| 74 |
uint32_t *cnt = (uint32_t *) user_data; |
| 75 |
assert_int_equal(0x400, total); |
| 76 |
switch (*cnt) { |
| 77 |
case 0: |
| 78 |
assert_int_equal(0x0, current); |
| 79 |
break; |
| 80 |
case 1: |
| 81 |
assert_int_equal(0x100, current); |
| 82 |
break; |
| 83 |
case 2: |
| 84 |
assert_int_equal(0x200, current); |
| 85 |
break; |
| 86 |
case 3: |
| 87 |
assert_int_equal(0x300, current); |
| 88 |
break; |
| 89 |
case 4: |
| 90 |
assert_int_equal(0x400, current); |
| 91 |
break; |
| 92 |
default: |
| 93 |
fail(); |
| 94 |
} |
| 95 |
(*cnt)++; |
| 96 |
} |
| 97 |
|
| 98 |
void default_spi_read_test_success(void **state) |
| 99 |
{ |
| 100 |
(void) state; /* unused */ |
| 101 |
uint8_t buf[0x400] = { 0x0 }; |
| 102 |
uint32_t cnt = 0; |
| 103 |
const unsigned int max_data_read = 0x100; |
| 104 |
const unsigned int offset = 0x100; |
| 105 |
struct registered_master mst = { |
| 106 |
.spi.read = default_spi_read, |
| 107 |
.spi.max_data_read = max_data_read |
| 108 |
}; |
| 109 |
|
| 110 |
/* setup initial test state */ |
| 111 |
struct flashctx flashctx = { |
| 112 |
.chip = &mock_chip, |
| 113 |
.mst = &mst |
| 114 |
}; |
| 115 |
flashrom_set_progress_callback_v2(&flashctx, spi_read_progress_cb, (void *) &cnt); |
| 116 |
init_progress(&flashctx, FLASHROM_PROGRESS_READ, 0x400); |
| 117 |
for (int i = 0; i < 4; i++) { |
| 118 |
will_return(__wrap_spi_send_command, JEDEC_WRDI); |
| 119 |
will_return(__wrap_spi_send_command, JEDEC_READ); |
| 120 |
will_return(__wrap_spi_send_command, max_data_read); |
| 121 |
} |
| 122 |
assert_int_equal(0, default_spi_read(&flashctx, buf, offset, sizeof(buf))); |
| 123 |
assert_int_equal(5, cnt); |
| 124 |
|
| 125 |
flashrom_set_progress_callback_v2(&flashctx, NULL, NULL); |
| 126 |
} |
| 127 |
|
| 128 |
void spi_write_enable_test_success(void **state) |
| 129 |
{ |
| 130 |
(void) state; /* unused */ |
| 131 |
|
| 132 |
/* setup initial test state. */ |
| 133 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 134 |
expect_memory(__wrap_spi_send_command, flash, |
| 135 |
&flashctx, sizeof(flashctx)); |
| 136 |
|
| 137 |
will_return(__wrap_spi_send_command, JEDEC_WREN_OUTSIZE); |
| 138 |
will_return(__wrap_spi_send_command, JEDEC_WREN); |
| 139 |
will_return(__wrap_spi_send_command, JEDEC_WREN_INSIZE); |
| 140 |
assert_int_equal(0, spi_write_enable(&flashctx)); |
| 141 |
} |
| 142 |
|
| 143 |
void spi_write_disable_test_success(void **state) |
| 144 |
{ |
| 145 |
(void) state; /* unused */ |
| 146 |
|
| 147 |
/* setup initial test state. */ |
| 148 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 149 |
expect_memory(__wrap_spi_send_command, flash, |
| 150 |
&flashctx, sizeof(flashctx)); |
| 151 |
|
| 152 |
will_return(__wrap_spi_send_command, JEDEC_WRDI_OUTSIZE); |
| 153 |
will_return(__wrap_spi_send_command, JEDEC_WRDI); |
| 154 |
will_return(__wrap_spi_send_command, JEDEC_WRDI_INSIZE); |
| 155 |
assert_int_equal(0, spi_write_disable(&flashctx)); |
| 156 |
} |
| 157 |
|
| 158 |
void probe_spi_rdid_test_success(void **state) |
| 159 |
{ |
| 160 |
(void) state; /* unused */ |
| 161 |
|
| 162 |
/* setup initial test state. */ |
| 163 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 164 |
expect_memory(__wrap_spi_send_command, flash, |
| 165 |
&flashctx, sizeof(flashctx)); |
| 166 |
|
| 167 |
will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE); |
| 168 |
will_return(__wrap_spi_send_command, JEDEC_RDID); |
| 169 |
will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE); |
| 170 |
assert_int_equal(0, probe_spi_rdid(&flashctx)); |
| 171 |
} |
| 172 |
|
| 173 |
void probe_spi_rdid4_test_success(void **state) |
| 174 |
{ |
| 175 |
(void) state; /* unused */ |
| 176 |
|
| 177 |
/* setup initial test state. */ |
| 178 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 179 |
expect_memory(__wrap_spi_send_command, flash, |
| 180 |
&flashctx, sizeof(flashctx)); |
| 181 |
|
| 182 |
will_return(__wrap_spi_send_command, JEDEC_RDID_OUTSIZE); |
| 183 |
will_return(__wrap_spi_send_command, JEDEC_RDID); |
| 184 |
will_return(__wrap_spi_send_command, JEDEC_RDID_INSIZE + 1); |
| 185 |
assert_int_equal(0, probe_spi_rdid4(&flashctx)); |
| 186 |
} |
| 187 |
|
| 188 |
void probe_spi_rems_test_success(void **state) |
| 189 |
{ |
| 190 |
(void) state; /* unused */ |
| 191 |
|
| 192 |
/* setup initial test state. */ |
| 193 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 194 |
expect_memory(__wrap_spi_send_command, flash, |
| 195 |
&flashctx, sizeof(flashctx)); |
| 196 |
|
| 197 |
will_return(__wrap_spi_send_command, JEDEC_REMS_OUTSIZE); |
| 198 |
will_return(__wrap_spi_send_command, JEDEC_REMS); |
| 199 |
will_return(__wrap_spi_send_command, JEDEC_REMS_INSIZE); |
| 200 |
assert_int_equal(0, probe_spi_rems(&flashctx)); |
| 201 |
} |
| 202 |
|
| 203 |
void probe_spi_res1_test_success(void **state) |
| 204 |
{ |
| 205 |
(void) state; /* unused */ |
| 206 |
|
| 207 |
/* setup initial test state. */ |
| 208 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 209 |
expect_memory(__wrap_spi_send_command, flash, |
| 210 |
&flashctx, sizeof(flashctx)); |
| 211 |
|
| 212 |
will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); |
| 213 |
will_return(__wrap_spi_send_command, JEDEC_RES); |
| 214 |
will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1); |
| 215 |
assert_int_equal(0, probe_spi_res2(&flashctx)); |
| 216 |
} |
| 217 |
|
| 218 |
void probe_spi_res2_test_success(void **state) |
| 219 |
{ |
| 220 |
(void) state; /* unused */ |
| 221 |
|
| 222 |
/* setup initial test state. */ |
| 223 |
clear_spi_id_cache(); |
| 224 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 225 |
expect_memory(__wrap_spi_send_command, flash, |
| 226 |
&flashctx, sizeof(flashctx)); |
| 227 |
|
| 228 |
will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); |
| 229 |
will_return(__wrap_spi_send_command, JEDEC_RES); |
| 230 |
will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 1); |
| 231 |
assert_int_equal(0, probe_spi_res2(&flashctx)); |
| 232 |
} |
| 233 |
|
| 234 |
void probe_spi_res3_test_success(void **state) |
| 235 |
{ |
| 236 |
(void) state; /* unused */ |
| 237 |
|
| 238 |
/* setup initial test state. */ |
| 239 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 240 |
expect_memory(__wrap_spi_send_command, flash, |
| 241 |
&flashctx, sizeof(flashctx)); |
| 242 |
|
| 243 |
will_return(__wrap_spi_send_command, JEDEC_RES_OUTSIZE); |
| 244 |
will_return(__wrap_spi_send_command, JEDEC_RES); |
| 245 |
will_return(__wrap_spi_send_command, JEDEC_RES_INSIZE + 2); |
| 246 |
assert_int_equal(0, probe_spi_res3(&flashctx)); |
| 247 |
} |
| 248 |
|
| 249 |
void probe_spi_at25f_test_success(void **state) |
| 250 |
{ |
| 251 |
(void) state; /* unused */ |
| 252 |
|
| 253 |
/* setup initial test state. */ |
| 254 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 255 |
expect_memory(__wrap_spi_send_command, flash, |
| 256 |
&flashctx, sizeof(flashctx)); |
| 257 |
|
| 258 |
will_return(__wrap_spi_send_command, AT25F_RDID_OUTSIZE); |
| 259 |
will_return(__wrap_spi_send_command, AT25F_RDID); |
| 260 |
will_return(__wrap_spi_send_command, AT25F_RDID_INSIZE); |
| 261 |
assert_int_equal(0, probe_spi_at25f(&flashctx)); |
| 262 |
} |
| 263 |
|
| 264 |
/* spi95.c */ |
| 265 |
void probe_spi_st95_test_success(void **state) |
| 266 |
{ |
| 267 |
(void) state; /* unused */ |
| 268 |
|
| 269 |
/* setup initial test state. */ |
| 270 |
struct flashctx flashctx = { .chip = &mock_chip }; |
| 271 |
expect_memory(__wrap_spi_send_command, flash, |
| 272 |
&flashctx, sizeof(flashctx)); |
| 273 |
|
| 274 |
/* chip total size < 64K. */ |
| 275 |
uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address |
| 276 |
|
| 277 |
will_return(__wrap_spi_send_command, rdid_outsize); |
| 278 |
will_return(__wrap_spi_send_command, ST_M95_RDID); |
| 279 |
will_return(__wrap_spi_send_command, ST_M95_RDID_INSIZE); |
| 280 |
assert_int_equal(0, probe_spi_st95(&flashctx)); |
| 281 |
} |