blob: a622d6eaf0df706ab63e7378a1ccee12e58afdb6 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +09303
4#define _GNU_SOURCE /* fallocate */
5#include <assert.h>
6#include <fcntl.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/mman.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <unistd.h>
15
16#include "mbox.h"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093017#include "flash.h"
Andrew Jefferycd186112018-08-08 10:47:55 +093018#include "lpc.h"
Andrew Jeffery457a6e52018-08-08 11:21:08 +093019#include "transport_mbox.h"
Andrew Jefferyf593b1b2018-08-08 11:01:04 +093020#include "windows.h"
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +093021
22#include "test/mbox.h"
23
24#define STEP 16
25
Andrew Jefferyee7af882018-03-01 11:57:50 +103026void dump_buf(const void *buf, size_t len)
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +093027{
Andrew Jefferyee7af882018-03-01 11:57:50 +103028 const uint8_t *buf8 = buf;
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +093029 int i;
30
31 for (i = 0; i < len; i += STEP) {
32 int delta;
33 int max;
34 int j;
35
36 delta = len - i;
37 max = delta > STEP ? STEP : delta;
38
39 printf("0x%08x:\t", i);
40 for (j = 0; j < max; j++)
Andrew Jefferyee7af882018-03-01 11:57:50 +103041 printf("0x%02x, ", buf8[i + j]);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +093042
43 printf("\n");
44 }
45 printf("\n");
46}
47
48/*
49 * Because we are using a file and not a pipe for the mbox file descriptor we
50 * need to handle a difference in behaviour. For a given command and response
51 * sequence the first 16 bytes of the file are occupied by the mbox command.
52 * The response occupies the following 14 bytes for a total of 30 bytes.
53 *
54 * We also have to ensure we lseek() to reset the file descriptor offset back
55 * to the start of the file before dispatching the mbox command.
56 */
57
58/* Macros for handling the pipe/file discrepancy */
59#define RESPONSE_OFFSET 16
60#define RESPONSE_SIZE 14
61
62int mbox_cmp(struct mbox_context *context, const uint8_t *expected, size_t len)
63{
64 struct stat details;
65 uint8_t *map;
66 int rc;
67 int fd;
68
69 fd = context->fds[MBOX_FD].fd;
70 fstat(fd, &details);
71
72 map = mmap(NULL, details.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
73 assert(map != MAP_FAILED);
74 assert(details.st_size >= (RESPONSE_OFFSET + len));
75
76 rc = memcmp(expected, &map[RESPONSE_OFFSET], len);
77
78 if (rc != 0) {
79 printf("\nMBOX state (%ld):\n", details.st_size);
80 dump_buf(map, details.st_size);
81 printf("Expected response (%lu):\n", len);
82 dump_buf(expected, len);
83 }
84
85 munmap(map, details.st_size);
86
87 return rc;
88}
89
Andrew Jefferyca1dfc92018-03-22 16:49:40 +103090void mbox_rspcpy(struct mbox_context *context, struct mbox_msg *msg)
91{
92 struct stat details;
93 uint8_t *map;
94 int fd;
95
96 fd = context->fds[MBOX_FD].fd;
97 fstat(fd, &details);
98
99 map = mmap(NULL, details.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
100 assert(map != MAP_FAILED);
101 assert(details.st_size >= (RESPONSE_OFFSET + RESPONSE_SIZE));
102
103 memcpy(msg, &map[RESPONSE_OFFSET], RESPONSE_SIZE);
104
105 munmap(map, details.st_size);
106}
107
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930108int mbox_command_write(struct mbox_context *context, const uint8_t *command,
109 size_t len)
110{
111 size_t remaining;
112 int rc;
113 int fd;
114
115 fd = context->fds[MBOX_FD].fd;
116 rc = lseek(fd, 0, SEEK_SET);
117 if (rc != 0)
118 return -1;
119
120 remaining = len;
121 while (remaining > 0) {
122 rc = write(fd, command, remaining);
123 if (rc < 0)
124 goto out;
125 remaining -= rc;
126 }
127
128 rc = lseek(fd, 0, SEEK_SET);
129 if (rc != 0)
130 return -1;
131
132out:
133 return rc;
134}
135
136int mbox_command_dispatch(struct mbox_context *context, const uint8_t *command,
137 size_t len)
138{
139 uint8_t status;
140 int rc;
141
142 rc = mbox_command_write(context, command, len);
143 if (rc < 0)
144 return rc;
145
146 rc = dispatch_mbox(context);
147 if (rc < 0)
148 return -rc;
149
150 /*
151 * The aspeed-lpc-ctrl driver implements mailbox register access
152 * through the usual read()/write() chardev interface.
153 *
154 * The typical access sequence is:
155 *
156 * 1. Read all the registers out
157 * 2. Perform the action specified
158 * 3. Write a response to the registers.
159 *
160 * For the tests the "device" file descriptor is backed by a temporary
161 * file. The above sequence leads to a file-size of 30 bytes: 16 bytes
162 * for the issued command, followed by a 14-byte response.
163 *
164 * However, the typical access pattern isn't the only access pattern.
165 * Individual byte registers can be accessed by lseek()'ing on the
166 * device's file descriptor and issuing read() or write() as desired.
167 * The daemon uses this property to manage the BMC status byte, and the
168 * implementation cleans up after status byte operations by lseek()'ing
169 * back to offset zero.
170 *
171 * Thus for the BMC_EVENT_ACK command the file only reaches a size of
172 * 16 bytes; the daemon's response overwrites the first 14 bytes of the
173 * command injected by the tests.
174 *
175 * The consequence of this is that the response status byte can either
176 * appear at offset 13, or offset 29, depending on the command.
177 *
178 * However, regardless of what command is issued the response data is
179 * written to the device and the file descriptor is left in the
180 * post-write() state. This means the status byte can always be
181 * accessed relative to the current position by an lseek() of type
182 * SEEK_CUR for offset -1.
183 *
184 */
185 rc = lseek(context->fds[MBOX_FD].fd, -1, SEEK_CUR);
186 if (rc < 0)
187 return rc;
188
189 rc = read(context->fds[MBOX_FD].fd, &status, sizeof(status));
190 if (rc < 0)
191 return rc;
192
193 return status;
194}
195
196struct mbox_test_context {
197 struct tmpf mbox;
198 struct tmpf flash;
199 struct tmpf lpc;
200 struct mbox_context context;
201} test;
202
203void cleanup(void)
204{
205 tmpf_destroy(&test.mbox);
206 tmpf_destroy(&test.flash);
207 tmpf_destroy(&test.lpc);
208}
209
210int __init_mbox_dev(struct mbox_context *context, const char *path);
Andrew Jefferycb9b2102018-08-08 16:31:07 +0930211int __lpc_dev_init(struct mbox_context *context, const char *path);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930212
213struct mbox_context *mbox_create_test_context(int n_windows, size_t len)
214{
215 int rc;
216
217 mbox_vlog = &mbox_log_console;
218 verbosity = 2;
219
220 atexit(cleanup);
221
Andrew Jefferyc3144042018-02-26 13:24:52 +1030222 rc = tmpf_init(&test.mbox, "mbox-store.XXXXXX");
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930223 assert(rc == 0);
224
Andrew Jefferyc3144042018-02-26 13:24:52 +1030225 rc = tmpf_init(&test.flash, "flash-store.XXXXXX");
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930226 assert(rc == 0);
227
Andrew Jefferyc3144042018-02-26 13:24:52 +1030228 rc = tmpf_init(&test.lpc, "lpc-store.XXXXXX");
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930229 assert(rc == 0);
230
231 test.context.windows.num = n_windows;
232 test.context.windows.default_size = len;
233
Andrew Jeffery1e531af2018-08-07 13:32:57 +0930234 rc = protocol_init(&test.context);
235 assert(rc == 0);
236
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930237 /*
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030238 * We need to call __init_mbox_dev() to initialise the handler table.
239 * However, afterwards we need to discard the fd of the clearly useless
240 * /dev/null and replace it with our own fd for mbox device emulation
241 * by the test framework.
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930242 */
Andrew Jefferyefb09de2018-03-26 14:36:43 +1030243 __init_mbox_dev(&test.context, "/dev/null");
244 rc = close(test.context.fds[MBOX_FD].fd);
245 assert(rc == 0);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930246 test.context.fds[MBOX_FD].fd = test.mbox.fd;
247
Andrew Jefferyd6b09bc2018-08-08 16:47:54 +0930248 rc = flash_dev_init(&test.context);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930249 assert(rc == 0);
250
251 rc = fallocate(test.flash.fd, 0, 0, test.context.mtd_info.size);
252 assert(rc == 0);
253
Andrew Jefferycb9b2102018-08-08 16:31:07 +0930254 rc = __lpc_dev_init(&test.context, test.lpc.path);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930255 assert(rc == 0);
256
Andrew Jefferyd23affd2018-03-23 12:13:39 +1030257 rc = fallocate(test.lpc.fd, 0, 0, test.context.mem_size);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930258 assert(rc == 0);
259
Andrew Jefferyc1a67fa2018-08-08 17:07:38 +0930260 rc = windows_init(&test.context);
Andrew Jeffery5ab4e3e2017-04-12 14:23:24 +0930261 assert(rc == 0);
262
263 return rc ? NULL : &test.context;
264}
265
266/* From ccan's container_of module, CC0 license */
267#define container_of(member_ptr, containing_type, member) \
268 ((containing_type *) \
269 ((char *)(member_ptr) \
270 - container_off(containing_type, member)) \
271 + check_types_match(*(member_ptr), ((containing_type *)0)->member))
272
273/* From ccan's container_of module, CC0 license */
274#define container_off(containing_type, member) \
275 offsetof(containing_type, member)
276
277/* From ccan's check_type module, CC0 license */
278#define check_type(expr, type) \
279 ((typeof(expr) *)0 != (type *)0)
280
281/* From ccan's check_type module, CC0 license */
282#define check_types_match(expr1, expr2) \
283 ((typeof(expr1) *)0 != (typeof(expr2) *)0)
284
285int mbox_set_mtd_data(struct mbox_context *context, const void *data,
286 size_t len)
287{
288 struct mbox_test_context *arg;
289 void *map;
290
291 /* Sanity check */
292 arg = container_of(context, struct mbox_test_context, context);
293 if (&test != arg)
294 return -1;
295
296 if (len > test.context.mtd_info.size)
297 return -2;
298
299 map = mmap(NULL, test.context.mtd_info.size, PROT_WRITE, MAP_SHARED,
300 test.flash.fd, 0);
301 assert(map != MAP_FAILED);
302 memcpy(map, data, len);
303 munmap(map, test.context.mtd_info.size);
304
305 return 0;
306}
307
308char *get_dev_mtd(void)
309{
310 return strdup(test.flash.path);
311}