1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-26 22:52:34 +02:00
flashrom/tests/raiden_debug_spi.c
Jes B. Klinke ea91d4fcf4 raiden: Support target index with generic REQ_ENABLE
Some devices such as the GSC knows how it is wired to AP and EC flash
chips, and can be told which specific device to talk to.  Other devices
such as Servo Micro and HyperDebug are generic, and do not know how they
are wired, the caller is responsible for first configure the appropriate
MUXes or buffers, and then tell the debugger which port to use (Servo
Micro has just one SPI port, HyperDebug is the first that has multiple).
The Raiden protocol allows both the cases of USB devices knowing their
wiring and not.

If I were to declare the protocol in Rust, this is how the information
of the Raiden protocol "enable request" would be encoded:
```
enum {
  EnableGeneric(u8),
  EnableAp,
  EnableEc,
  ...
}
```

The first label `EnableGeneric(u8)` is to be used with HyperDebug that
does not know how its ports are wired, and allow access by index.
The other labels `EnableAp` and `EnableEc` are to be used with the GSC.

The actual transmission of the enum above uses the bRequest and low byte
of wValue of a USB control request, but that is a detail and not
conceptually important.

Until now, `-p raiden_debug_spi:target=AP` or `...:target=EC` could be
used to make flashrom use `EnableAp` or `EnableEc`, and if neither was
given, it would default to `EnableGeneric`, which now that wValue is
used means `EnableGeneric(0)`.

I find it rather straight-forward, that `-p raiden_debug_spi:target=1`,
`...:target=2`, etc. should translate to `EnableGeneric(1)`, etc.

This patchset achieves this, by adding a second 16-bit parameter value,
next to request_enable.

I have tested that flashrom can detect the same Winbond flash chip
"W25Q128.V..M" with two different Raiden USB devices as below.

TEST=flashrom -p raiden_debug_spi:serial=0701B044-91AC3132,target=AP
TEST=flashrom -p raiden_debug_spi:serial=205635783236,target=1

Signed-off-by: Jes B. Klinke <jbk@chromium.org>
Change-Id: I03bf4f3210186fb5937b42e298761907b03e08b7
Reviewed-on: https://review.coreboot.org/c/flashrom/+/77999
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
2023-11-03 05:59:53 +00:00

204 lines
7.7 KiB
C

/*
* 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_RAIDEN_DEBUG_SPI == 1
static ssize_t raiden_debug_libusb_get_device_list(void *state, libusb_context *ctx, libusb_device ***list)
{
*list = calloc(1, sizeof(**list));
assert_non_null(*list);
/*
* libusb_device is opaque type, it is tossed around between libusb functions but always
* stays opaque to the caller.
* Given that all libusb functions are mocked in tests, and raiden_debug test is mocking
* only one device, we don't need to initialise libusb_device.
*/
return 1;
}
static void raiden_debug_libusb_free_device_list(void *state, libusb_device **list, int unref_devices)
{
free(list);
}
static int raiden_debug_libusb_get_device_descriptor(
void *state, libusb_device *dev, struct libusb_device_descriptor *desc)
{
desc->idVendor = 0x18D1; /* GOOGLE_VID */
desc->idProduct = 0;
desc->bNumConfigurations = 1;
return 0;
}
static int raiden_debug_libusb_get_config_descriptor(
void *state, libusb_device *dev, uint8_t config_index, struct libusb_config_descriptor **config)
{
*config = calloc(1, sizeof(**config));
assert_non_null(*config);
struct libusb_endpoint_descriptor *tmp_endpoint = calloc(2, sizeof(*tmp_endpoint));
assert_non_null(tmp_endpoint);
struct libusb_interface_descriptor *tmp_interface_desc = calloc(1, sizeof(*tmp_interface_desc));
assert_non_null(tmp_interface_desc);
struct libusb_interface *tmp_interface = calloc(1, sizeof(*tmp_interface));
assert_non_null(tmp_interface);
/* in endpoint */
tmp_endpoint[0].bEndpointAddress = 0x80;
tmp_endpoint[0].bmAttributes = 0x2;
/* out endpoint */
tmp_endpoint[1].bEndpointAddress = 0x0;
tmp_endpoint[1].bmAttributes = 0x2;
tmp_interface_desc->bInterfaceClass = 0xff; /* LIBUSB_CLASS_VENDOR_SPEC */
tmp_interface_desc->bInterfaceSubClass = 0x51; /* GOOGLE_RAIDEN_SPI_SUBCLASS */
tmp_interface_desc->bInterfaceProtocol = 0x01; /* GOOGLE_RAIDEN_SPI_PROTOCOL_V1 */
tmp_interface_desc->bNumEndpoints = 2; /* in_endpoint and out_endpoint */
tmp_interface_desc->endpoint = tmp_endpoint;
tmp_interface->num_altsetting = 1;
tmp_interface->altsetting = tmp_interface_desc;
(*config)->bConfigurationValue = 0;
(*config)->bNumInterfaces = 1;
(*config)->interface = tmp_interface;
return 0;
}
static void raiden_debug_libusb_free_config_descriptor(void *state, struct libusb_config_descriptor *config)
{
free((void *)config->interface->altsetting->endpoint);
free((void *)config->interface->altsetting);
free((void *)config->interface);
free(config);
}
void raiden_debug_basic_lifecycle_test_success(void **state)
{
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock raiden_debug_io = {
.libusb_get_device_list = raiden_debug_libusb_get_device_list,
.libusb_free_device_list = raiden_debug_libusb_free_device_list,
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor,
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor,
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor,
.fallback_open_state = &raiden_debug_fallback_open_state,
};
char raiden_debug_param[32];
snprintf(raiden_debug_param, sizeof(raiden_debug_param),
"address=%d", USB_DEVICE_ADDRESS);
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param);
}
void raiden_debug_targetAP_basic_lifecycle_test_success(void **state)
{
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock raiden_debug_io = {
.libusb_get_device_list = raiden_debug_libusb_get_device_list,
.libusb_free_device_list = raiden_debug_libusb_free_device_list,
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor,
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor,
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor,
.fallback_open_state = &raiden_debug_fallback_open_state,
};
char raiden_debug_param[32];
snprintf(raiden_debug_param, sizeof(raiden_debug_param),
"address=%d,target=AP", USB_DEVICE_ADDRESS);
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param);
}
void raiden_debug_targetEC_basic_lifecycle_test_success(void **state)
{
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock raiden_debug_io = {
.libusb_get_device_list = raiden_debug_libusb_get_device_list,
.libusb_free_device_list = raiden_debug_libusb_free_device_list,
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor,
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor,
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor,
.fallback_open_state = &raiden_debug_fallback_open_state,
};
char raiden_debug_param[32];
snprintf(raiden_debug_param, sizeof(raiden_debug_param),
"address=%d,target=ec", USB_DEVICE_ADDRESS);
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param);
}
void raiden_debug_target0_basic_lifecycle_test_success(void **state)
{
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock raiden_debug_io = {
.libusb_get_device_list = raiden_debug_libusb_get_device_list,
.libusb_free_device_list = raiden_debug_libusb_free_device_list,
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor,
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor,
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor,
.fallback_open_state = &raiden_debug_fallback_open_state,
};
char raiden_debug_param[32];
snprintf(raiden_debug_param, sizeof(raiden_debug_param),
"address=%d,target=0", USB_DEVICE_ADDRESS);
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param);
}
void raiden_debug_target1_basic_lifecycle_test_success(void **state)
{
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock raiden_debug_io = {
.libusb_get_device_list = raiden_debug_libusb_get_device_list,
.libusb_free_device_list = raiden_debug_libusb_free_device_list,
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor,
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor,
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor,
.fallback_open_state = &raiden_debug_fallback_open_state,
};
char raiden_debug_param[32];
snprintf(raiden_debug_param, sizeof(raiden_debug_param),
"address=%d,target=1", USB_DEVICE_ADDRESS);
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param);
}
#else
SKIP_TEST(raiden_debug_basic_lifecycle_test_success)
SKIP_TEST(raiden_debug_targetAP_basic_lifecycle_test_success)
SKIP_TEST(raiden_debug_targetEC_basic_lifecycle_test_success)
SKIP_TEST(raiden_debug_target0_basic_lifecycle_test_success)
SKIP_TEST(raiden_debug_target1_basic_lifecycle_test_success)
#endif /* CONFIG_RAIDEN_DEBUG_SPI */