From 94efa4454225ca09db5c12dfcd2b6057dd1682bf Mon Sep 17 00:00:00 2001 From: Anastasia Klimchuk Date: Thu, 30 Jun 2022 15:25:04 +1000 Subject: tests: Split lifecycle test file into per-programmer files This patch creates individual files for each programmer's lifecycle tests. Common functions that are reusable for all tests are gathered in lifecycle.c. Each individual file needs to include lifecycle.h BUG=b:237606255 TEST=ninja test Change-Id: If2307699dcbb3a085b91a2dcd41156e6fd07f812 Signed-off-by: Anastasia Klimchuk Reviewed-on: https://review.coreboot.org/c/flashrom/+/65543 Reviewed-by: Peter Marheine Reviewed-by: Felix Singer Reviewed-by: Edward O'Callaghan Reviewed-by: Thomas Heijligen Tested-by: build bot (Jenkins) --- tests/linux_spi.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/linux_spi.c (limited to 'tests/linux_spi.c') diff --git a/tests/linux_spi.c b/tests/linux_spi.c new file mode 100644 index 00000000..2e3dc95f --- /dev/null +++ b/tests/linux_spi.c @@ -0,0 +1,72 @@ +/* + * This file is part of the flashrom project. + * + * Copyright 2021 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 "lifecycle.h" + +#if CONFIG_LINUX_SPI == 1 +static int linux_spi_ioctl(void *state, int fd, unsigned long request, va_list args) { + + if (request == SPI_IOC_MESSAGE(2)) { /* ioctl code for read request */ + struct spi_ioc_transfer* msg = va_arg(args, struct spi_ioc_transfer*); + + /* First message has write array and write count */ + unsigned int writecnt = msg[0].len; + unsigned char *writearr = (unsigned char *)msg[0].tx_buf; + /* Second message has read array and read count */ + unsigned int readcnt = msg[1].len; + + /* Detect probing */ + if (writecnt == 1 && writearr[0] == JEDEC_RDID && readcnt == 3) { + /* We need to populate read array. */ + unsigned char *readarr = (unsigned char *)msg[1].rx_buf; + readarr[0] = 0xEF; /* WINBOND_NEX_ID */ + readarr[1] = 0x40; /* WINBOND_NEX_W25Q128_V left byte */ + readarr[2] = 0x18; /* WINBOND_NEX_W25Q128_V right byte */ + } + } + + return 0; +} + +static char *linux_spi_fgets(void *state, char *buf, int len, FILE *fp) +{ + /* Emulate reading max buffer size from sysfs. */ + const char *max_buf_size = "1048576"; + + return memcpy(buf, max_buf_size, min(len, strlen(max_buf_size) + 1)); +} + +void linux_spi_probe_lifecycle_test_success(void **state) +{ + /* + * Current implementation tests a particular path of the init procedure. + * Specifically, it is reading the buffer size from sysfs. + */ + struct io_mock_fallback_open_state linux_spi_fallback_open_state = { + .noc = 0, + .paths = { "/dev/null", NULL }, + .flags = { O_RDWR }, + }; + const struct io_mock linux_spi_io = { + .fgets = linux_spi_fgets, + .ioctl = linux_spi_ioctl, + .fallback_open_state = &linux_spi_fallback_open_state, + }; + + run_probe_lifecycle(state, &linux_spi_io, &programmer_linux_spi, "dev=/dev/null", "W25Q128.V"); +} +#else + SKIP_TEST(linux_spi_probe_lifecycle_test_success) +#endif /* CONFIG_LINUX_SPI */ -- cgit v1.2.3