summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/Library/BhyveFwCtlLib/BhyveFwCtlLib.c
blob: cc3e64f0c44c0826abce7e3352cb07e995abe0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/** @file

  Copyright (c) 2020, Rebecca Cran <rebecca@bsdio.com>
  Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
  Copyright (C) 2013, Red Hat, Inc.
  Copyright (c) 2015, Nahanni Systems.

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "Uefi.h"
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BhyveFwCtlLib.h>
#include <Library/DebugLib.h>
#include <Library/IoLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>

#define FW_PORT   0x510
#define FW_IPORT  0x511

/* Transport protocol basic operations */
#define OP_NULL     1
#define OP_ECHO     2
#define OP_GET      3
#define OP_GET_LEN  4
#define OP_SET      5

/* Transport protocol error returns */
#define T_ESUCCESS  0
#define T_ENOENT    2
#define T_E2BIG     7
#define T_EMSGSIZE  40

#define ROUNDUP(x, y)  ((((x)+((y)-1))/(y))*(y))

STATIC CONST CHAR8  mBhyveSig[4] = { 'B', 'H', 'Y', 'V' };

STATIC BOOLEAN  mBhyveFwCtlSupported = FALSE;

STATIC INT32  mBhyveFwCtlTxid = 0xa5;

/* XXX Maybe a better inbuilt version of this ? */
typedef struct {
  VOID      *Base;
  UINT32    Len;
} BIO_VEC;

typedef struct {
  UINT32    Sz;
  UINT32    Op;
  UINT32    TxId;
  UINT32    Err;
} MSG_RX_HDR;

STATIC
RETURN_STATUS
EFIAPI
BhyveFwCtl_CvtErr (
  IN UINT32  errno
  )
{
  RETURN_STATUS  Status;

  switch (errno) {
    case T_ESUCCESS:
      Status = RETURN_SUCCESS;
      break;
    case T_ENOENT:
      Status = RETURN_NOT_FOUND;
      break;
    case T_E2BIG:
      Status = RETURN_INVALID_PARAMETER;
      break;
    case T_EMSGSIZE:
      Status = RETURN_BUFFER_TOO_SMALL;
      break;
    default:
      Status = RETURN_PROTOCOL_ERROR;
      break;
  }

  return Status;
}

STATIC
UINT32
EFIAPI
BIov_WLen (
  IN BIO_VEC  b[]
  )
{
  UINT32  i;
  UINT32  tLen;

  tLen = 0;

  if (b != NULL) {
    for (i = 0; b[i].Base != NULL; i++) {
      tLen += ROUNDUP (b[i].Len, sizeof (UINT32));
    }
  }

  return tLen;
}

/**
   Utility to send 1-3 bytes of input as a 4-byte value
   with trailing zeroes.
 **/
STATIC
UINT32
BIov_Send_Rem (
  IN UINT32  *Data,
  IN UINT32  Len
  )
{
  union {
    UINT8     c[4];
    UINT32    w;
  } u;
  UINT8   *cdata;
  UINT32  i;

  cdata = (UINT8 *)Data;
  u.w   = 0;

  for (i = 0; i < Len; i++) {
    u.c[i] = *cdata++;
  }

  return u.w;
}

/**
   Send a block of data out the i/o port as 4-byte quantities,
   appending trailing zeroes on the last if required.
 **/
STATIC
VOID
BIov_Send (
  IN char    *Data,
  IN UINT32  Len
  )
{
  UINT32  *LData;

  LData = (UINT32 *)Data;

  while (Len > sizeof (UINT32)) {
    IoWrite32 (FW_PORT, *LData++);
    Len -= sizeof (UINT32);
  }

  if (Len > 0) {
    IoWrite32 (FW_PORT, BIov_Send_Rem (LData, Len));
  }
}

/**
   Send data described by an array of iovecs out the i/o port.
 **/
STATIC
VOID
BIov_SendAll (
  IN BIO_VEC  b[]
  )
{
  INT32  i;

  if (b != NULL) {
    for (i = 0; b[i].Base; i++) {
      BIov_Send (b[i].Base, b[i].Len);
    }
  }
}

/**
   Prepend the transport header to a block of data and send.
 **/
STATIC
VOID
EFIAPI
BhyveFwCtl_MsgSend (
  IN  UINT32   OpCode,
  IN  BIO_VEC  Data[]
  )
{
  BIO_VEC  hIov[4];
  UINT32   Hdr[3];
  UINT32   i;

  /* Set up header as an iovec */
  for (i = 0; i < 3; i++) {
    hIov[i].Base = &Hdr[i];
    hIov[i].Len  = sizeof (Hdr[0]);
  }

  hIov[i].Base = NULL;
  hIov[i].Len  = 0;

  /* Initialize header */
  Hdr[0] = BIov_WLen (hIov) + BIov_WLen (Data);
  Hdr[1] = (UINT32)OpCode;
  Hdr[2] = mBhyveFwCtlTxid;

  /* Send header and data */
  BIov_SendAll (hIov);
  BIov_SendAll (Data);
}

/**
   Read a transport response and optional data from the i/o port.
 **/
STATIC
RETURN_STATUS
EFIAPI
BhyveFwCtl_MsgRecv (
  OUT  MSG_RX_HDR  *Rhdr,
  OUT  BIO_VEC     Data[]
  )
{
  RETURN_STATUS  Status;
  UINT32         *Dp;
  UINT32         Rd;
  UINT32         remLen;
  INT32          oLen;
  INT32          xLen;

  Rd = IoRead32 (FW_PORT);
  if (Rd < sizeof (MSG_RX_HDR)) {
  }

  /* Read in header and setup initial error */
  Rhdr->Sz   = Rd;
  Rhdr->Op   = IoRead32 (FW_PORT);
  Rhdr->TxId = IoRead32 (FW_PORT);
  Rhdr->Err  = IoRead32 (FW_PORT);

  /* Convert transport errno into UEFI error status */
  Status = BhyveFwCtl_CvtErr (Rhdr->Err);

  remLen = Rd - sizeof (MSG_RX_HDR);
  xLen   = 0;

  /*
   * A few cases to handle:
   *  - the user didn't supply a read buffer
   *  - the buffer is too small for the response
   *  - the response is zero-length
   */
  if (Data != NULL) {
    Dp   = (UINT32 *)Data[0].Base;
    oLen = remLen;
    if (remLen > Data[0].Len) {
      Status = RETURN_BUFFER_TOO_SMALL;
      xLen   = remLen - Data[0].Len;
      oLen   = remLen = Data[0].Len;
    }

    while (remLen > 0) {
      *Dp++   = IoRead32 (FW_PORT);
      remLen -= sizeof (UINT32);
    }

    Data[0].Len = oLen;
  } else {
    /* No user data, but data returned - drop */
    if (remLen > 0) {
      Status = RETURN_BUFFER_TOO_SMALL;
      xLen   = remLen;
    }
  }

  /* Drop additional data */
  while (xLen > 0) {
    (void)IoRead32 (FW_PORT);
    xLen -= sizeof (UINT32);
  }

  return Status;
}

STATIC
RETURN_STATUS
EFIAPI
BhyveFwCtl_Msg (
  IN   UINT32   OpCode,
  IN   BIO_VEC  Sdata[],
  OUT  BIO_VEC  Rdata[]
  )
{
  MSG_RX_HDR     Rh;
  RETURN_STATUS  Status;

  Status = RETURN_SUCCESS;

  BhyveFwCtl_MsgSend (OpCode, Sdata);
  Status = BhyveFwCtl_MsgRecv (&Rh, Rdata);

  mBhyveFwCtlTxid++;

  return Status;
}

STATIC
RETURN_STATUS
EFIAPI
BhyveFwCtlGetLen (
  IN CONST CHAR8   *Name,
  IN OUT   UINT32  *Size
  )
{
  BIO_VEC        Req[2], Resp[2];
  RETURN_STATUS  Status;

  Req[0].Base = (VOID *)Name;
  Req[0].Len  = (UINT32)AsciiStrLen (Name) + 1;
  Req[1].Base = NULL;

  Resp[0].Base = Size;
  Resp[0].Len  = sizeof (UINT32);
  Resp[1].Base = NULL;

  Status = BhyveFwCtl_Msg (OP_GET_LEN, Req, Resp);

  return Status;
}

#define FMAXSZ  1024
STATIC struct {
  UINT64    fSize;
  UINT32    fData[FMAXSZ];
} FwGetvalBuf;

STATIC
RETURN_STATUS
EFIAPI
BhyveFwCtlGetVal (
  IN CONST CHAR8   *Name,
  OUT      VOID    *Item,
  IN OUT   UINT32  *Size
  )
{
  BIO_VEC        Req[2];
  BIO_VEC        Resp[2];
  RETURN_STATUS  Status;

  /* Make sure temp buffer is larger than passed-in size */
  if (*Size > sizeof (FwGetvalBuf.fData)) {
    return RETURN_INVALID_PARAMETER;
  }

  Req[0].Base = (VOID *)Name;
  Req[0].Len  = (UINT32)AsciiStrLen (Name) + 1;
  Req[1].Base = NULL;

  Resp[0].Base = &FwGetvalBuf;
  Resp[0].Len  = sizeof (UINT64) + *Size;
  Resp[1].Base = NULL;

  Status = BhyveFwCtl_Msg (OP_GET, Req, Resp);

  /*
   * Copy out data on success (or on a truncated message).
   * XXX This step can be eliminted with Msg() supporting
   *     multiple iovecs.
   */
  if ((Status == RETURN_SUCCESS) || (Status == RETURN_BUFFER_TOO_SMALL)) {
    *Size = (UINT32)FwGetvalBuf.fSize;
    CopyMem (Item, FwGetvalBuf.fData, *Size);
  }

  return Status;
}

/**
   Front end to the internal GET_LEN and GET protocols
 **/
RETURN_STATUS
EFIAPI
BhyveFwCtlGet (
  IN   CONST CHAR8  *Name,
  OUT  VOID         *Item,
  IN OUT  UINTN     *Size
  )
{
  RETURN_STATUS  Status;

  if (mBhyveFwCtlSupported == FALSE) {
    return RETURN_UNSUPPORTED;
  }

  if (Item == NULL) {
    Status = BhyveFwCtlGetLen (Name, (UINT32 *)Size);
  } else {
    Status = BhyveFwCtlGetVal (Name, Item, (UINT32 *)Size);
  }

  return Status;
}

/**
   Library initialization. Probe the host to see if the f/w ctl
   interface is supported.
 **/
RETURN_STATUS
EFIAPI
BhyveFwCtlInitialize (
  VOID
  )
{
  UINT32  i;
  UINT8   ch;

  DEBUG ((DEBUG_INFO, "FwCtlInitialize\n"));

  IoWrite16 (FW_PORT, 0x0000);
  for (i = 0; i < 4; i++) {
    ch = IoRead8 (FW_IPORT);
    if (ch != mBhyveSig[i]) {
      DEBUG ((DEBUG_INFO, "Host f/w sig mismatch %c/%c\n", ch, mBhyveSig[i]));
      return RETURN_SUCCESS;
    }
  }

  mBhyveFwCtlSupported = TRUE;

  return RETURN_SUCCESS;
}