| 1 |
/* |
| 2 |
* This file is part of the flashrom project. |
| 3 |
* |
| 4 |
* Copyright 2021 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 <stdlib.h> |
| 17 |
|
| 18 |
#include "lifecycle.h" |
| 19 |
|
| 20 |
#if CONFIG_RAIDEN_DEBUG_SPI == 1 |
| 21 |
static ssize_t raiden_debug_libusb_get_device_list(void *state, libusb_context *ctx, libusb_device ***list) |
| 22 |
{ |
| 23 |
*list = calloc(1, sizeof(**list)); |
| 24 |
assert_non_null(*list); |
| 25 |
|
| 26 |
/* |
| 27 |
* libusb_device is opaque type, it is tossed around between libusb functions but always |
| 28 |
* stays opaque to the caller. |
| 29 |
* Given that all libusb functions are mocked in tests, and raiden_debug test is mocking |
| 30 |
* only one device, we don't need to initialise libusb_device. |
| 31 |
*/ |
| 32 |
return 1; |
| 33 |
} |
| 34 |
|
| 35 |
static void raiden_debug_libusb_free_device_list(void *state, libusb_device **list, int unref_devices) |
| 36 |
{ |
| 37 |
free(list); |
| 38 |
} |
| 39 |
|
| 40 |
static int raiden_debug_libusb_get_device_descriptor( |
| 41 |
void *state, libusb_device *dev, struct libusb_device_descriptor *desc) |
| 42 |
{ |
| 43 |
desc->idVendor = 0x18D1; /* GOOGLE_VID */ |
| 44 |
desc->idProduct = 0; |
| 45 |
desc->bNumConfigurations = 1; |
| 46 |
|
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static int raiden_debug_libusb_get_config_descriptor( |
| 51 |
void *state, libusb_device *dev, uint8_t config_index, struct libusb_config_descriptor **config) |
| 52 |
{ |
| 53 |
*config = calloc(1, sizeof(**config)); |
| 54 |
assert_non_null(*config); |
| 55 |
|
| 56 |
struct libusb_endpoint_descriptor *tmp_endpoint = calloc(2, sizeof(*tmp_endpoint)); |
| 57 |
assert_non_null(tmp_endpoint); |
| 58 |
struct libusb_interface_descriptor *tmp_interface_desc = calloc(1, sizeof(*tmp_interface_desc)); |
| 59 |
assert_non_null(tmp_interface_desc); |
| 60 |
struct libusb_interface *tmp_interface = calloc(1, sizeof(*tmp_interface)); |
| 61 |
assert_non_null(tmp_interface); |
| 62 |
|
| 63 |
/* in endpoint */ |
| 64 |
tmp_endpoint[0].bEndpointAddress = 0x80; |
| 65 |
tmp_endpoint[0].bmAttributes = 0x2; |
| 66 |
/* out endpoint */ |
| 67 |
tmp_endpoint[1].bEndpointAddress = 0x0; |
| 68 |
tmp_endpoint[1].bmAttributes = 0x2; |
| 69 |
|
| 70 |
tmp_interface_desc->bInterfaceClass = 0xff; /* LIBUSB_CLASS_VENDOR_SPEC */ |
| 71 |
tmp_interface_desc->bInterfaceSubClass = 0x51; /* GOOGLE_RAIDEN_SPI_SUBCLASS */ |
| 72 |
tmp_interface_desc->bInterfaceProtocol = 0x01; /* GOOGLE_RAIDEN_SPI_PROTOCOL_V1 */ |
| 73 |
tmp_interface_desc->bNumEndpoints = 2; /* in_endpoint and out_endpoint */ |
| 74 |
tmp_interface_desc->endpoint = tmp_endpoint; |
| 75 |
|
| 76 |
tmp_interface->num_altsetting = 1; |
| 77 |
tmp_interface->altsetting = tmp_interface_desc; |
| 78 |
|
| 79 |
(*config)->bConfigurationValue = 0; |
| 80 |
(*config)->bNumInterfaces = 1; |
| 81 |
(*config)->interface = tmp_interface; |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static void raiden_debug_libusb_free_config_descriptor(void *state, struct libusb_config_descriptor *config) |
| 87 |
{ |
| 88 |
free((void *)config->interface->altsetting->endpoint); |
| 89 |
free((void *)config->interface->altsetting); |
| 90 |
free((void *)config->interface); |
| 91 |
free(config); |
| 92 |
} |
| 93 |
|
| 94 |
void raiden_debug_basic_lifecycle_test_success(void **state) |
| 95 |
{ |
| 96 |
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = { |
| 97 |
.noc = 0, |
| 98 |
.paths = { NULL }, |
| 99 |
}; |
| 100 |
const struct io_mock raiden_debug_io = { |
| 101 |
.libusb_get_device_list = raiden_debug_libusb_get_device_list, |
| 102 |
.libusb_free_device_list = raiden_debug_libusb_free_device_list, |
| 103 |
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor, |
| 104 |
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor, |
| 105 |
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor, |
| 106 |
.fallback_open_state = &raiden_debug_fallback_open_state, |
| 107 |
}; |
| 108 |
|
| 109 |
char raiden_debug_param[32]; |
| 110 |
|
| 111 |
snprintf(raiden_debug_param, sizeof(raiden_debug_param), |
| 112 |
"address=%d", USB_DEVICE_ADDRESS); |
| 113 |
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param); |
| 114 |
} |
| 115 |
|
| 116 |
void raiden_debug_targetAP_basic_lifecycle_test_success(void **state) |
| 117 |
{ |
| 118 |
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = { |
| 119 |
.noc = 0, |
| 120 |
.paths = { NULL }, |
| 121 |
}; |
| 122 |
const struct io_mock raiden_debug_io = { |
| 123 |
.libusb_get_device_list = raiden_debug_libusb_get_device_list, |
| 124 |
.libusb_free_device_list = raiden_debug_libusb_free_device_list, |
| 125 |
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor, |
| 126 |
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor, |
| 127 |
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor, |
| 128 |
.fallback_open_state = &raiden_debug_fallback_open_state, |
| 129 |
}; |
| 130 |
|
| 131 |
char raiden_debug_param[32]; |
| 132 |
snprintf(raiden_debug_param, sizeof(raiden_debug_param), |
| 133 |
"address=%d,target=AP", USB_DEVICE_ADDRESS); |
| 134 |
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param); |
| 135 |
} |
| 136 |
|
| 137 |
void raiden_debug_targetEC_basic_lifecycle_test_success(void **state) |
| 138 |
{ |
| 139 |
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = { |
| 140 |
.noc = 0, |
| 141 |
.paths = { NULL }, |
| 142 |
}; |
| 143 |
const struct io_mock raiden_debug_io = { |
| 144 |
.libusb_get_device_list = raiden_debug_libusb_get_device_list, |
| 145 |
.libusb_free_device_list = raiden_debug_libusb_free_device_list, |
| 146 |
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor, |
| 147 |
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor, |
| 148 |
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor, |
| 149 |
.fallback_open_state = &raiden_debug_fallback_open_state, |
| 150 |
}; |
| 151 |
|
| 152 |
char raiden_debug_param[32]; |
| 153 |
snprintf(raiden_debug_param, sizeof(raiden_debug_param), |
| 154 |
"address=%d,target=ec", USB_DEVICE_ADDRESS); |
| 155 |
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param); |
| 156 |
} |
| 157 |
|
| 158 |
void raiden_debug_target0_basic_lifecycle_test_success(void **state) |
| 159 |
{ |
| 160 |
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = { |
| 161 |
.noc = 0, |
| 162 |
.paths = { NULL }, |
| 163 |
}; |
| 164 |
const struct io_mock raiden_debug_io = { |
| 165 |
.libusb_get_device_list = raiden_debug_libusb_get_device_list, |
| 166 |
.libusb_free_device_list = raiden_debug_libusb_free_device_list, |
| 167 |
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor, |
| 168 |
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor, |
| 169 |
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor, |
| 170 |
.fallback_open_state = &raiden_debug_fallback_open_state, |
| 171 |
}; |
| 172 |
|
| 173 |
char raiden_debug_param[32]; |
| 174 |
snprintf(raiden_debug_param, sizeof(raiden_debug_param), |
| 175 |
"address=%d,target=0", USB_DEVICE_ADDRESS); |
| 176 |
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param); |
| 177 |
} |
| 178 |
|
| 179 |
void raiden_debug_target1_basic_lifecycle_test_success(void **state) |
| 180 |
{ |
| 181 |
struct io_mock_fallback_open_state raiden_debug_fallback_open_state = { |
| 182 |
.noc = 0, |
| 183 |
.paths = { NULL }, |
| 184 |
}; |
| 185 |
const struct io_mock raiden_debug_io = { |
| 186 |
.libusb_get_device_list = raiden_debug_libusb_get_device_list, |
| 187 |
.libusb_free_device_list = raiden_debug_libusb_free_device_list, |
| 188 |
.libusb_get_device_descriptor = raiden_debug_libusb_get_device_descriptor, |
| 189 |
.libusb_get_config_descriptor = raiden_debug_libusb_get_config_descriptor, |
| 190 |
.libusb_free_config_descriptor = raiden_debug_libusb_free_config_descriptor, |
| 191 |
.fallback_open_state = &raiden_debug_fallback_open_state, |
| 192 |
}; |
| 193 |
|
| 194 |
char raiden_debug_param[32]; |
| 195 |
snprintf(raiden_debug_param, sizeof(raiden_debug_param), |
| 196 |
"address=%d,target=1", USB_DEVICE_ADDRESS); |
| 197 |
run_basic_lifecycle(state, &raiden_debug_io, &programmer_raiden_debug_spi, raiden_debug_param); |
| 198 |
} |
| 199 |
#else |
| 200 |
SKIP_TEST(raiden_debug_basic_lifecycle_test_success) |
| 201 |
SKIP_TEST(raiden_debug_targetAP_basic_lifecycle_test_success) |
| 202 |
SKIP_TEST(raiden_debug_targetEC_basic_lifecycle_test_success) |
| 203 |
SKIP_TEST(raiden_debug_target0_basic_lifecycle_test_success) |
| 204 |
SKIP_TEST(raiden_debug_target1_basic_lifecycle_test_success) |
| 205 |
#endif /* CONFIG_RAIDEN_DEBUG_SPI */ |