Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/ipmi/ipmi_bmc_sim.c
Line
Count
Source
1
/*
2
 * IPMI BMC emulation
3
 *
4
 * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
#include "qemu/osdep.h"
26
#include "system/system.h"
27
#include "qemu/timer.h"
28
#include "hw/ipmi/ipmi.h"
29
#include "qemu/error-report.h"
30
#include "qapi/error.h"
31
#include "qemu/module.h"
32
#include "hw/core/loader.h"
33
#include "hw/core/qdev-properties.h"
34
#include "hw/core/qdev-properties-system.h"
35
#include "migration/vmstate.h"
36
#include "net/net.h"
37
38
0
#define IPMI_NETFN_CHASSIS            0x00
39
40
#define IPMI_CMD_GET_CHASSIS_CAPABILITIES 0x00
41
#define IPMI_CMD_GET_CHASSIS_STATUS       0x01
42
#define IPMI_CMD_CHASSIS_CONTROL          0x02
43
#define IPMI_CMD_GET_SYS_RESTART_CAUSE    0x09
44
45
0
#define IPMI_NETFN_SENSOR_EVENT       0x04
46
47
#define IPMI_CMD_PLATFORM_EVENT_MSG       0x02
48
#define IPMI_CMD_SET_SENSOR_EVT_ENABLE    0x28
49
#define IPMI_CMD_GET_SENSOR_EVT_ENABLE    0x29
50
#define IPMI_CMD_REARM_SENSOR_EVTS        0x2a
51
#define IPMI_CMD_GET_SENSOR_EVT_STATUS    0x2b
52
#define IPMI_CMD_GET_SENSOR_READING       0x2d
53
#define IPMI_CMD_SET_SENSOR_TYPE          0x2e
54
#define IPMI_CMD_GET_SENSOR_TYPE          0x2f
55
#define IPMI_CMD_SET_SENSOR_READING       0x30
56
57
/* #define IPMI_NETFN_APP             0x06 In ipmi.h */
58
59
0
#define IPMI_CMD_GET_DEVICE_ID            0x01
60
#define IPMI_CMD_COLD_RESET               0x02
61
#define IPMI_CMD_WARM_RESET               0x03
62
#define IPMI_CMD_SET_ACPI_POWER_STATE     0x06
63
#define IPMI_CMD_GET_ACPI_POWER_STATE     0x07
64
#define IPMI_CMD_GET_DEVICE_GUID          0x08
65
#define IPMI_CMD_RESET_WATCHDOG_TIMER     0x22
66
#define IPMI_CMD_SET_WATCHDOG_TIMER       0x24
67
#define IPMI_CMD_GET_WATCHDOG_TIMER       0x25
68
#define IPMI_CMD_SET_BMC_GLOBAL_ENABLES   0x2e
69
#define IPMI_CMD_GET_BMC_GLOBAL_ENABLES   0x2f
70
#define IPMI_CMD_CLR_MSG_FLAGS            0x30
71
#define IPMI_CMD_GET_MSG_FLAGS            0x31
72
#define IPMI_CMD_GET_MSG                  0x33
73
#define IPMI_CMD_SEND_MSG                 0x34
74
#define IPMI_CMD_READ_EVT_MSG_BUF         0x35
75
#define IPMI_CMD_GET_CHANNEL_ACCESS       0x41
76
#define IPMI_CMD_GET_CHANNEL_INFO         0x42
77
78
0
#define IPMI_NETFN_STORAGE            0x0a
79
80
#define IPMI_CMD_GET_SDR_REP_INFO         0x20
81
#define IPMI_CMD_GET_SDR_REP_ALLOC_INFO   0x21
82
#define IPMI_CMD_RESERVE_SDR_REP          0x22
83
#define IPMI_CMD_GET_SDR                  0x23
84
#define IPMI_CMD_ADD_SDR                  0x24
85
#define IPMI_CMD_PARTIAL_ADD_SDR          0x25
86
#define IPMI_CMD_DELETE_SDR               0x26
87
#define IPMI_CMD_CLEAR_SDR_REP            0x27
88
#define IPMI_CMD_GET_SDR_REP_TIME         0x28
89
#define IPMI_CMD_SET_SDR_REP_TIME         0x29
90
#define IPMI_CMD_ENTER_SDR_REP_UPD_MODE   0x2A
91
#define IPMI_CMD_EXIT_SDR_REP_UPD_MODE    0x2B
92
#define IPMI_CMD_RUN_INIT_AGENT           0x2C
93
#define IPMI_CMD_GET_FRU_AREA_INFO        0x10
94
#define IPMI_CMD_READ_FRU_DATA            0x11
95
#define IPMI_CMD_WRITE_FRU_DATA           0x12
96
#define IPMI_CMD_GET_SEL_INFO             0x40
97
#define IPMI_CMD_GET_SEL_ALLOC_INFO       0x41
98
#define IPMI_CMD_RESERVE_SEL              0x42
99
#define IPMI_CMD_GET_SEL_ENTRY            0x43
100
#define IPMI_CMD_ADD_SEL_ENTRY            0x44
101
#define IPMI_CMD_PARTIAL_ADD_SEL_ENTRY    0x45
102
#define IPMI_CMD_DELETE_SEL_ENTRY         0x46
103
#define IPMI_CMD_CLEAR_SEL                0x47
104
#define IPMI_CMD_GET_SEL_TIME             0x48
105
#define IPMI_CMD_SET_SEL_TIME             0x49
106
107
0
#define IPMI_NETFN_TRANSPORT          0x0c
108
109
#define IPMI_CMD_SET_LAN_CONFIG           0x01
110
#define IPMI_CMD_GET_LAN_CONFIG           0x02
111
112
113
/* Same as a timespec struct. */
114
struct ipmi_time {
115
    long tv_sec;
116
    long tv_nsec;
117
};
118
119
0
#define MAX_SEL_SIZE 128
120
121
typedef struct IPMISel {
122
    uint8_t sel[MAX_SEL_SIZE][16];
123
    unsigned int next_free;
124
    long time_offset;
125
    uint16_t reservation;
126
    uint8_t last_addition[4];
127
    uint8_t last_clear[4];
128
    uint8_t overflow;
129
} IPMISel;
130
131
0
#define MAX_SDR_SIZE 16384
132
133
typedef struct IPMISdr {
134
    uint8_t sdr[MAX_SDR_SIZE];
135
    unsigned int next_free;
136
    uint16_t next_rec_id;
137
    uint16_t reservation;
138
    uint8_t last_addition[4];
139
    uint8_t last_clear[4];
140
    uint8_t overflow;
141
} IPMISdr;
142
143
typedef struct IPMIFru {
144
    char *filename;
145
    unsigned int nentries;
146
    uint16_t areasize;
147
    uint8_t *data;
148
} IPMIFru;
149
150
typedef struct IPMISensor {
151
    uint8_t status;
152
    uint8_t reading;
153
    uint16_t states_suppt;
154
    uint16_t assert_suppt;
155
    uint16_t deassert_suppt;
156
    uint16_t states;
157
    uint16_t assert_states;
158
    uint16_t deassert_states;
159
    uint16_t assert_enable;
160
    uint16_t deassert_enable;
161
    uint8_t  sensor_type;
162
    uint8_t  evt_reading_type_code;
163
} IPMISensor;
164
0
#define IPMI_SENSOR_GET_PRESENT(s)       ((s)->status & 0x01)
165
0
#define IPMI_SENSOR_SET_PRESENT(s, v)    ((s)->status = (s->status & ~0x01) | \
166
0
                                             !!(v))
167
#define IPMI_SENSOR_GET_SCAN_ON(s)       ((s)->status & 0x40)
168
0
#define IPMI_SENSOR_SET_SCAN_ON(s, v)    ((s)->status = (s->status & ~0x40) | \
169
0
                                             ((!!(v)) << 6))
170
0
#define IPMI_SENSOR_GET_EVENTS_ON(s)     ((s)->status & 0x80)
171
0
#define IPMI_SENSOR_SET_EVENTS_ON(s, v)  ((s)->status = (s->status & ~0x80) | \
172
0
                                             ((!!(v)) << 7))
173
0
#define IPMI_SENSOR_GET_RET_STATUS(s)    ((s)->status & 0xc0)
174
0
#define IPMI_SENSOR_SET_RET_STATUS(s, v) ((s)->status = (s->status & ~0xc0) | \
175
0
                                             (v & 0xc0))
176
0
#define IPMI_SENSOR_IS_DISCRETE(s) ((s)->evt_reading_type_code != 1)
177
178
0
#define MAX_SENSORS 20
179
0
#define IPMI_WATCHDOG_SENSOR 0
180
181
0
#define NBYTES_IP  4
182
0
#define NBYTES_MAC 6
183
184
typedef struct IPMILan {
185
    uint8_t channel;
186
    uint8_t ipaddr[NBYTES_IP];
187
    uint8_t ipsrc;
188
    MACAddr macaddr;
189
    uint8_t netmask[NBYTES_IP];
190
    uint8_t defgw_ipaddr[NBYTES_IP];
191
    MACAddr defgw_macaddr;
192
193
    char *arg_ipaddr;
194
    char *arg_netmask;
195
    char *arg_defgw_ipaddr;
196
} IPMILan;
197
198
0
#define MAX_NETFNS 64
199
200
typedef struct IPMIRcvBufEntry {
201
    QTAILQ_ENTRY(IPMIRcvBufEntry) entry;
202
    uint8_t len;
203
    uint8_t buf[MAX_IPMI_MSG_SIZE];
204
} IPMIRcvBufEntry;
205
206
struct IPMIBmcSim {
207
    IPMIBmc parent;
208
209
    QEMUTimer *timer;
210
211
    uint8_t bmc_global_enables;
212
    uint8_t msg_flags;
213
214
    bool     watchdog_initialized;
215
    uint8_t  watchdog_use;
216
    uint8_t  watchdog_action;
217
    uint8_t  watchdog_pretimeout; /* In seconds */
218
    uint8_t  watchdog_expired;
219
    uint16_t watchdog_timeout; /* in 100's of milliseconds */
220
221
    bool     watchdog_running;
222
    bool     watchdog_preaction_ran;
223
    int64_t  watchdog_expiry;
224
225
    uint8_t device_id;
226
    uint8_t ipmi_version;
227
    uint8_t device_rev;
228
    uint8_t fwrev1;
229
    uint8_t fwrev2;
230
    uint32_t mfg_id;
231
    uint16_t product_id;
232
233
    uint8_t restart_cause;
234
235
    uint8_t acpi_power_state[2];
236
    QemuUUID uuid;
237
238
    IPMISel sel;
239
    IPMISdr sdr;
240
    IPMIFru fru;
241
    IPMISensor sensors[MAX_SENSORS];
242
    char *sdr_filename;
243
    IPMILan lan;
244
245
    /* Odd netfns are for responses, so we only need the even ones. */
246
    const IPMINetfn *netfns[MAX_NETFNS / 2];
247
248
    /* We allow one event in the buffer */
249
    uint8_t evtbuf[16];
250
251
    QTAILQ_HEAD(, IPMIRcvBufEntry) rcvbufs;
252
};
253
254
0
#define IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK        (1 << 3)
255
0
#define IPMI_BMC_MSG_FLAG_EVT_BUF_FULL                 (1 << 1)
256
0
#define IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE                (1 << 0)
257
#define IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK_SET(s) \
258
0
    (IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK & (s)->msg_flags)
259
#define IPMI_BMC_MSG_FLAG_EVT_BUF_FULL_SET(s) \
260
0
    (IPMI_BMC_MSG_FLAG_EVT_BUF_FULL & (s)->msg_flags)
261
#define IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE_SET(s) \
262
0
    (IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE & (s)->msg_flags)
263
264
0
#define IPMI_BMC_GLOBAL_ENABLES_SUPPORTED 0x0f
265
0
#define IPMI_BMC_RCV_MSG_QUEUE_INT_BIT    0
266
0
#define IPMI_BMC_EVBUF_FULL_INT_BIT       1
267
0
#define IPMI_BMC_EVENT_MSG_BUF_BIT        2
268
0
#define IPMI_BMC_EVENT_LOG_BIT            3
269
0
#define IPMI_BMC_MSG_INTS_ON(s) ((s)->bmc_global_enables & \
270
0
                                 (1 << IPMI_BMC_RCV_MSG_QUEUE_INT_BIT))
271
0
#define IPMI_BMC_EVBUF_FULL_INT_ENABLED(s) ((s)->bmc_global_enables & \
272
0
                                        (1 << IPMI_BMC_EVBUF_FULL_INT_BIT))
273
0
#define IPMI_BMC_EVENT_LOG_ENABLED(s) ((s)->bmc_global_enables & \
274
0
                                       (1 << IPMI_BMC_EVENT_LOG_BIT))
275
0
#define IPMI_BMC_EVENT_MSG_BUF_ENABLED(s) ((s)->bmc_global_enables & \
276
0
                                           (1 << IPMI_BMC_EVENT_MSG_BUF_BIT))
277
278
0
#define IPMI_BMC_WATCHDOG_USE_MASK 0xc7
279
0
#define IPMI_BMC_WATCHDOG_ACTION_MASK 0x77
280
0
#define IPMI_BMC_WATCHDOG_GET_USE(s) ((s)->watchdog_use & 0x7)
281
0
#define IPMI_BMC_WATCHDOG_GET_DONT_LOG(s) (((s)->watchdog_use >> 7) & 0x1)
282
0
#define IPMI_BMC_WATCHDOG_GET_DONT_STOP(s) (((s)->watchdog_use >> 6) & 0x1)
283
0
#define IPMI_BMC_WATCHDOG_GET_PRE_ACTION(s) (((s)->watchdog_action >> 4) & 0x7)
284
0
#define IPMI_BMC_WATCHDOG_PRE_NONE               0
285
#define IPMI_BMC_WATCHDOG_PRE_SMI                1
286
0
#define IPMI_BMC_WATCHDOG_PRE_NMI                2
287
0
#define IPMI_BMC_WATCHDOG_PRE_MSG_INT            3
288
0
#define IPMI_BMC_WATCHDOG_GET_ACTION(s) ((s)->watchdog_action & 0x7)
289
0
#define IPMI_BMC_WATCHDOG_ACTION_NONE            0
290
0
#define IPMI_BMC_WATCHDOG_ACTION_RESET           1
291
0
#define IPMI_BMC_WATCHDOG_ACTION_POWER_DOWN      2
292
0
#define IPMI_BMC_WATCHDOG_ACTION_POWER_CYCLE     3
293
294
0
#define IPMI_CHANNEL_IMPLEMENTATION_MIN    0x1
295
0
#define IPMI_CHANNEL_IMPLEMENTATION_MAX    0xb
296
#define IPMI_CHANNEL_IS_IMPLEMENTATION_SPECIFIC(c) \
297
0
    (IPMI_CHANNEL_IMPLEMENTATION_MIN <= (c) && \
298
0
     (c) <= IPMI_CHANNEL_IMPLEMENTATION_MAX)
299
300
#define IPMI_BMC_CHANNEL_IS_LAN(ibs, c) \
301
0
    ((ibs)->lan.channel != 0 && (ibs)->lan.channel == (c))
302
303
0
#define IPMI_BMC_LAN_CFG_CC_PARAM_NOT_SUPPORTED    0x80
304
0
#define IPMI_BMC_LAN_CFG_CC_PARAM_READONLY         0x82
305
306
0
#define IPMI_BMC_LAN_CFG_PARAM_SET_IN_PROGRESS        0x00
307
0
#define IPMI_BMC_LAN_CFG_PARAM_AUTH_TYPE_SUPPORT      0x01
308
0
#define IPMI_BMC_LAN_CFG_PARAM_AUTH_TYPE_ENABLES      0x02
309
0
#define IPMI_BMC_LAN_CFG_PARAM_IP_ADDR                0x03
310
0
#define IPMI_BMC_LAN_CFG_PARAM_IP_ADDR_SOURCE         0x04
311
0
#define IPMI_BMC_LAN_CFG_PARAM_MAC_ADDR               0x05
312
0
#define IPMI_BMC_LAN_CFG_PARAM_SUBNET_MASK            0x06
313
0
#define IPMI_BMC_LAN_CFG_PARAM_IPV4_HDR_PARAMS        0x07
314
0
#define IPMI_BMC_LAN_CFG_PARAM_DEFAULT_GW_IP_ADDR     0x0c
315
0
#define IPMI_BMC_LAN_CFG_PARAM_DEFAULT_GW_MAC_ADDR    0x0d
316
0
#define IPMI_BMC_LAN_CFG_PARAM_BACKUP_GW_ADDR         0x0e
317
0
#define IPMI_BMC_LAN_CFG_PARAM_BACKUP_GW_MAC_ADDR     0x0f
318
0
#define IPMI_BMC_LAN_CFG_PARAM_COMMUNITY_STRING       0x10
319
0
#define IPMI_BMC_LAN_CFG_PARAM_NUM_DESTINATIONS       0x11
320
321
0
#define IPMI_BMC_LAN_CFG_PARAMETER_REVISION    0x11
322
323
0
#define IPMI_BMC_LAN_CFG_IS_VALID_IP_SOURCE(v)    (0x0 <= (v) && (v) <= 0x4)
324
325
0
#define RSP_BUFFER_INITIALIZER { }
326
327
static inline void rsp_buffer_pushmore(RspBuffer *rsp, uint8_t *bytes,
328
                                       unsigned int n)
329
0
{
330
0
    if (rsp->len + n >= sizeof(rsp->buffer)) {
331
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
332
0
        return;
333
0
    }
334
335
0
    memcpy(&rsp->buffer[rsp->len], bytes, n);
336
0
    rsp->len += n;
337
0
}
338
339
static void ipmi_sim_handle_timeout(IPMIBmcSim *ibs);
340
341
static void ipmi_gettime(struct ipmi_time *time)
342
0
{
343
0
    int64_t stime;
344
345
0
    stime = qemu_clock_get_ns(QEMU_CLOCK_HOST);
346
0
    time->tv_sec = stime / 1000000000LL;
347
0
    time->tv_nsec = stime % 1000000000LL;
348
0
}
349
350
static int64_t ipmi_getmonotime(void)
351
0
{
352
0
    return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
353
0
}
354
355
static void ipmi_timeout(void *opaque)
356
0
{
357
0
    IPMIBmcSim *ibs = opaque;
358
359
0
    ipmi_sim_handle_timeout(ibs);
360
0
}
361
362
static void set_timestamp(IPMIBmcSim *ibs, uint8_t *ts)
363
0
{
364
0
    unsigned int val;
365
0
    struct ipmi_time now;
366
367
0
    ipmi_gettime(&now);
368
0
    val = now.tv_sec + ibs->sel.time_offset;
369
0
    ts[0] = val & 0xff;
370
0
    ts[1] = (val >> 8) & 0xff;
371
0
    ts[2] = (val >> 16) & 0xff;
372
0
    ts[3] = (val >> 24) & 0xff;
373
0
}
374
375
static void sdr_inc_reservation(IPMISdr *sdr)
376
0
{
377
0
    sdr->reservation++;
378
0
    if (sdr->reservation == 0) {
379
0
        sdr->reservation = 1;
380
0
    }
381
0
}
382
383
static int sdr_add_entry(IPMIBmcSim *ibs,
384
                         const struct ipmi_sdr_header *sdrh_entry,
385
                         unsigned int len, uint16_t *recid)
386
0
{
387
0
    struct ipmi_sdr_header *sdrh =
388
0
        (struct ipmi_sdr_header *) &ibs->sdr.sdr[ibs->sdr.next_free];
389
390
0
    if ((len < IPMI_SDR_HEADER_SIZE) || (len > 255)) {
391
0
        return 1;
392
0
    }
393
394
0
    if (ipmi_sdr_length(sdrh_entry) != len) {
395
0
        return 1;
396
0
    }
397
398
0
    if (ibs->sdr.next_free + len > MAX_SDR_SIZE) {
399
0
        ibs->sdr.overflow = 1;
400
0
        return 1;
401
0
    }
402
403
0
    memcpy(sdrh, sdrh_entry, len);
404
0
    sdrh->rec_id[0] = ibs->sdr.next_rec_id & 0xff;
405
0
    sdrh->rec_id[1] = (ibs->sdr.next_rec_id >> 8) & 0xff;
406
0
    sdrh->sdr_version = 0x51; /* Conform to IPMI 1.5 spec */
407
408
0
    if (recid) {
409
0
        *recid = ibs->sdr.next_rec_id;
410
0
    }
411
0
    ibs->sdr.next_rec_id++;
412
0
    set_timestamp(ibs, ibs->sdr.last_addition);
413
0
    ibs->sdr.next_free += len;
414
0
    sdr_inc_reservation(&ibs->sdr);
415
0
    return 0;
416
0
}
417
418
static int sdr_find_entry(IPMISdr *sdr, uint16_t recid,
419
                          unsigned int *retpos, uint16_t *nextrec)
420
0
{
421
0
    unsigned int pos = *retpos;
422
423
0
    while (pos < sdr->next_free) {
424
0
        struct ipmi_sdr_header *sdrh =
425
0
            (struct ipmi_sdr_header *) &sdr->sdr[pos];
426
0
        uint16_t trec = ipmi_sdr_recid(sdrh);
427
0
        unsigned int nextpos = pos + ipmi_sdr_length(sdrh);
428
429
0
        if (trec == recid) {
430
0
            if (nextrec) {
431
0
                if (nextpos >= sdr->next_free) {
432
0
                    *nextrec = 0xffff;
433
0
                } else {
434
0
                    *nextrec = (sdr->sdr[nextpos] |
435
0
                                (sdr->sdr[nextpos + 1] << 8));
436
0
                }
437
0
            }
438
0
            *retpos = pos;
439
0
            return 0;
440
0
        }
441
0
        pos = nextpos;
442
0
    }
443
0
    return 1;
444
0
}
445
446
int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid,
447
                      const struct ipmi_sdr_compact **sdr, uint16_t *nextrec)
448
449
0
{
450
0
    IPMIBmcSim *ibs = IPMI_BMC_SIMULATOR(b);
451
0
    unsigned int pos;
452
453
0
    pos = 0;
454
0
    if (sdr_find_entry(&ibs->sdr, recid, &pos, nextrec)) {
455
0
        return -1;
456
0
    }
457
458
0
    *sdr = (const struct ipmi_sdr_compact *) &ibs->sdr.sdr[pos];
459
0
    return 0;
460
0
}
461
462
static void sel_inc_reservation(IPMISel *sel)
463
0
{
464
0
    sel->reservation++;
465
0
    if (sel->reservation == 0) {
466
0
        sel->reservation = 1;
467
0
    }
468
0
}
469
470
/* Returns 1 if the SEL is full and can't hold the event. */
471
static int sel_add_event(IPMIBmcSim *ibs, uint8_t *event)
472
0
{
473
0
    uint8_t ts[4];
474
475
0
    event[0] = 0xff;
476
0
    event[1] = 0xff;
477
0
    set_timestamp(ibs, ts);
478
0
    if (event[2] < 0xe0) { /* Don't set timestamps for type 0xe0-0xff. */
479
0
        memcpy(event + 3, ts, 4);
480
0
    }
481
0
    if (ibs->sel.next_free == MAX_SEL_SIZE) {
482
0
        ibs->sel.overflow = 1;
483
0
        return 1;
484
0
    }
485
0
    event[0] = ibs->sel.next_free & 0xff;
486
0
    event[1] = (ibs->sel.next_free >> 8) & 0xff;
487
0
    memcpy(ibs->sel.last_addition, ts, 4);
488
0
    memcpy(ibs->sel.sel[ibs->sel.next_free], event, 16);
489
0
    ibs->sel.next_free++;
490
0
    sel_inc_reservation(&ibs->sel);
491
0
    return 0;
492
0
}
493
494
static int attn_set(IPMIBmcSim *ibs)
495
0
{
496
0
    return IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE_SET(ibs)
497
0
        || IPMI_BMC_MSG_FLAG_EVT_BUF_FULL_SET(ibs)
498
0
        || IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK_SET(ibs);
499
0
}
500
501
static int attn_irq_enabled(IPMIBmcSim *ibs)
502
0
{
503
0
    return (IPMI_BMC_MSG_INTS_ON(ibs) &&
504
0
            (IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE_SET(ibs) ||
505
0
             IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK_SET(ibs)))
506
0
        || (IPMI_BMC_EVBUF_FULL_INT_ENABLED(ibs) &&
507
0
            IPMI_BMC_MSG_FLAG_EVT_BUF_FULL_SET(ibs));
508
0
}
509
510
void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log)
511
0
{
512
0
    IPMIBmcSim *ibs = IPMI_BMC_SIMULATOR(b);
513
0
    IPMIInterface *s = ibs->parent.intf;
514
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
515
516
0
    if (!IPMI_BMC_EVENT_MSG_BUF_ENABLED(ibs)) {
517
0
        return;
518
0
    }
519
520
0
    if (log && IPMI_BMC_EVENT_LOG_ENABLED(ibs)) {
521
0
        sel_add_event(ibs, evt);
522
0
    }
523
524
0
    if (ibs->msg_flags & IPMI_BMC_MSG_FLAG_EVT_BUF_FULL) {
525
0
        return;
526
0
    }
527
528
0
    memcpy(ibs->evtbuf, evt, 16);
529
0
    ibs->msg_flags |= IPMI_BMC_MSG_FLAG_EVT_BUF_FULL;
530
0
    k->set_atn(s, 1, attn_irq_enabled(ibs));
531
0
}
532
static void gen_event(IPMIBmcSim *ibs, unsigned int sens_num, uint8_t deassert,
533
                      uint8_t evd1, uint8_t evd2, uint8_t evd3)
534
0
{
535
0
    IPMIInterface *s = ibs->parent.intf;
536
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
537
0
    uint8_t evt[16];
538
0
    IPMISensor *sens = ibs->sensors + sens_num;
539
540
0
    if (!IPMI_BMC_EVENT_MSG_BUF_ENABLED(ibs)) {
541
0
        return;
542
0
    }
543
0
    if (!IPMI_SENSOR_GET_EVENTS_ON(sens)) {
544
0
        return;
545
0
    }
546
547
0
    evt[2] = 0x2; /* System event record */
548
0
    evt[7] = ibs->parent.slave_addr;
549
0
    evt[8] = 0;
550
0
    evt[9] = 0x04; /* Format version */
551
0
    evt[10] = sens->sensor_type;
552
0
    evt[11] = sens_num;
553
0
    evt[12] = sens->evt_reading_type_code | (!!deassert << 7);
554
0
    evt[13] = evd1;
555
0
    evt[14] = evd2;
556
0
    evt[15] = evd3;
557
558
0
    if (IPMI_BMC_EVENT_LOG_ENABLED(ibs)) {
559
0
        sel_add_event(ibs, evt);
560
0
    }
561
562
0
    if (ibs->msg_flags & IPMI_BMC_MSG_FLAG_EVT_BUF_FULL) {
563
0
        return;
564
0
    }
565
566
0
    memcpy(ibs->evtbuf, evt, 16);
567
0
    ibs->msg_flags |= IPMI_BMC_MSG_FLAG_EVT_BUF_FULL;
568
0
    k->set_atn(s, 1, attn_irq_enabled(ibs));
569
0
}
570
571
static void sensor_set_discrete_bit(IPMIBmcSim *ibs, unsigned int sensor,
572
                                    unsigned int bit, unsigned int val,
573
                                    uint8_t evd1, uint8_t evd2, uint8_t evd3,
574
                                    bool do_log)
575
0
{
576
0
    IPMISensor *sens;
577
0
    uint16_t mask;
578
579
0
    if (sensor >= MAX_SENSORS) {
580
0
        return;
581
0
    }
582
0
    if (bit >= 16) {
583
0
        return;
584
0
    }
585
586
0
    mask = (1 << bit);
587
0
    sens = ibs->sensors + sensor;
588
0
    if (val) {
589
0
        sens->states |= mask & sens->states_suppt;
590
0
        if (sens->assert_states & mask) {
591
0
            return; /* Already asserted */
592
0
        }
593
0
        sens->assert_states |= mask & sens->assert_suppt;
594
0
        if (do_log && (sens->assert_enable & mask & sens->assert_states)) {
595
            /* Send an event on assert */
596
0
            gen_event(ibs, sensor, 0, evd1, evd2, evd3);
597
0
        }
598
0
    } else {
599
0
        sens->states &= ~(mask & sens->states_suppt);
600
0
        if (sens->deassert_states & mask) {
601
0
            return; /* Already deasserted */
602
0
        }
603
0
        sens->deassert_states |= mask & sens->deassert_suppt;
604
0
        if (do_log && (sens->deassert_enable & mask & sens->deassert_states)) {
605
            /* Send an event on deassert */
606
0
            gen_event(ibs, sensor, 1, evd1, evd2, evd3);
607
0
        }
608
0
    }
609
0
}
610
611
static void ipmi_init_sensors_from_sdrs(IPMIBmcSim *s)
612
0
{
613
0
    unsigned int i, pos;
614
0
    IPMISensor *sens;
615
616
0
    for (i = 0; i < MAX_SENSORS; i++) {
617
0
        memset(s->sensors + i, 0, sizeof(*sens));
618
0
    }
619
620
0
    pos = 0;
621
0
    for (i = 0; !sdr_find_entry(&s->sdr, i, &pos, NULL); i++) {
622
0
        struct ipmi_sdr_compact *sdr =
623
0
            (struct ipmi_sdr_compact *) &s->sdr.sdr[pos];
624
0
        unsigned int len = sdr->header.rec_length;
625
626
0
        if (len < 20) {
627
0
            continue;
628
0
        }
629
0
        if (sdr->header.rec_type != IPMI_SDR_COMPACT_TYPE) {
630
0
            continue; /* Not a sensor SDR we set from */
631
0
        }
632
633
0
        if (sdr->sensor_owner_number >= MAX_SENSORS) {
634
0
            continue;
635
0
        }
636
0
        sens = s->sensors + sdr->sensor_owner_number;
637
638
0
        IPMI_SENSOR_SET_PRESENT(sens, 1);
639
0
        IPMI_SENSOR_SET_SCAN_ON(sens, (sdr->sensor_init >> 6) & 1);
640
0
        IPMI_SENSOR_SET_EVENTS_ON(sens, (sdr->sensor_init >> 5) & 1);
641
0
        sens->assert_suppt = sdr->assert_mask[0] | (sdr->assert_mask[1] << 8);
642
0
        sens->deassert_suppt =
643
0
            sdr->deassert_mask[0] | (sdr->deassert_mask[1] << 8);
644
0
        sens->states_suppt =
645
0
            sdr->discrete_mask[0] | (sdr->discrete_mask[1] << 8);
646
0
        sens->sensor_type = sdr->sensor_type;
647
0
        sens->evt_reading_type_code = sdr->reading_type & 0x7f;
648
649
        /* Enable all the events that are supported. */
650
0
        sens->assert_enable = sens->assert_suppt;
651
0
        sens->deassert_enable = sens->deassert_suppt;
652
0
    }
653
0
}
654
655
int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn,
656
                        const IPMINetfn *netfnd)
657
0
{
658
0
    if ((netfn & 1) || (netfn >= MAX_NETFNS) || (s->netfns[netfn / 2])) {
659
0
        return -1;
660
0
    }
661
0
    s->netfns[netfn / 2] = netfnd;
662
0
    return 0;
663
0
}
664
665
static const IPMICmdHandler *ipmi_get_handler(IPMIBmcSim *ibs,
666
                                              unsigned int netfn,
667
                                              unsigned int cmd)
668
0
{
669
0
    const IPMICmdHandler *hdl;
670
671
0
    if (netfn & 1 || netfn >= MAX_NETFNS || !ibs->netfns[netfn / 2]) {
672
0
        return NULL;
673
0
    }
674
675
0
    if (cmd >= ibs->netfns[netfn / 2]->cmd_nums) {
676
0
        return NULL;
677
0
    }
678
679
0
    hdl = &ibs->netfns[netfn / 2]->cmd_handlers[cmd];
680
0
    if (!hdl->cmd_handler) {
681
0
        return NULL;
682
0
    }
683
684
0
    return hdl;
685
0
}
686
687
static void next_timeout(IPMIBmcSim *ibs)
688
0
{
689
0
    int64_t next;
690
0
    if (ibs->watchdog_running) {
691
0
        next = ibs->watchdog_expiry;
692
0
    } else {
693
        /* Wait a minute */
694
0
        next = ipmi_getmonotime() + 60 * 1000000000LL;
695
0
    }
696
0
    timer_mod_ns(ibs->timer, next);
697
0
}
698
699
static void ipmi_sim_handle_command(IPMIBmc *b,
700
                                    uint8_t *cmd, unsigned int cmd_len,
701
                                    unsigned int max_cmd_len,
702
                                    uint8_t msg_id)
703
0
{
704
0
    IPMIBmcSim *ibs = IPMI_BMC_SIMULATOR(b);
705
0
    IPMIInterface *s = ibs->parent.intf;
706
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
707
0
    const IPMICmdHandler *hdl;
708
0
    RspBuffer rsp = RSP_BUFFER_INITIALIZER;
709
710
    /* Set up the response, set the low bit of NETFN. */
711
    /* Note that max_rsp_len must be at least 3 */
712
0
    if (sizeof(rsp.buffer) < 3) {
713
0
        rsp_buffer_set_error(&rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
714
0
        goto out;
715
0
    }
716
717
0
    rsp_buffer_push(&rsp, cmd[0] | 0x04);
718
0
    rsp_buffer_push(&rsp, cmd[1]);
719
0
    rsp_buffer_push(&rsp, 0); /* Assume success */
720
721
    /* If it's too short or it was truncated, return an error. */
722
0
    if (cmd_len < 2) {
723
0
        rsp_buffer_set_error(&rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
724
0
        goto out;
725
0
    }
726
0
    if (cmd_len > max_cmd_len) {
727
0
        rsp_buffer_set_error(&rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
728
0
        goto out;
729
0
    }
730
731
0
    if ((cmd[0] & 0x03) != 0) {
732
        /* Only have stuff on LUN 0 */
733
0
        rsp_buffer_set_error(&rsp, IPMI_CC_COMMAND_INVALID_FOR_LUN);
734
0
        goto out;
735
0
    }
736
737
0
    hdl = ipmi_get_handler(ibs, cmd[0] >> 2, cmd[1]);
738
0
    if (!hdl) {
739
0
        rsp_buffer_set_error(&rsp, IPMI_CC_INVALID_CMD);
740
0
        goto out;
741
0
    }
742
743
0
    if (cmd_len < hdl->cmd_len_min) {
744
0
        rsp_buffer_set_error(&rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
745
0
        goto out;
746
0
    }
747
748
0
    hdl->cmd_handler(ibs, cmd, cmd_len, &rsp);
749
750
0
 out:
751
0
    k->handle_rsp(s, msg_id, rsp.buffer, rsp.len);
752
753
0
    next_timeout(ibs);
754
0
}
755
756
static void ipmi_sim_handle_timeout(IPMIBmcSim *ibs)
757
0
{
758
0
    IPMIInterface *s = ibs->parent.intf;
759
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
760
0
    bool do_log = !IPMI_BMC_WATCHDOG_GET_DONT_LOG(ibs);
761
762
0
    if (!ibs->watchdog_running) {
763
0
        goto out;
764
0
    }
765
766
0
    if (!ibs->watchdog_preaction_ran) {
767
0
        switch (IPMI_BMC_WATCHDOG_GET_PRE_ACTION(ibs)) {
768
0
        case IPMI_BMC_WATCHDOG_PRE_NMI:
769
0
            ibs->msg_flags |= IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK;
770
0
            k->do_hw_op(s, IPMI_SEND_NMI, 0);
771
0
            sensor_set_discrete_bit(ibs, IPMI_WATCHDOG_SENSOR, 8, 1,
772
0
                                    0xc8, (2 << 4) | 0xf, 0xff,
773
0
                                    do_log);
774
0
            break;
775
776
0
        case IPMI_BMC_WATCHDOG_PRE_MSG_INT:
777
0
            ibs->msg_flags |= IPMI_BMC_MSG_FLAG_WATCHDOG_TIMEOUT_MASK;
778
0
            k->set_atn(s, 1, attn_irq_enabled(ibs));
779
0
            sensor_set_discrete_bit(ibs, IPMI_WATCHDOG_SENSOR, 8, 1,
780
0
                                    0xc8, (3 << 4) | 0xf, 0xff,
781
0
                                    do_log);
782
0
            break;
783
784
0
        default:
785
0
            goto do_full_expiry;
786
0
        }
787
788
0
        ibs->watchdog_preaction_ran = 1;
789
        /* Issued the pretimeout, do the rest of the timeout now. */
790
0
        ibs->watchdog_expiry = ipmi_getmonotime();
791
0
        ibs->watchdog_expiry += ibs->watchdog_pretimeout * 1000000000LL;
792
0
        goto out;
793
0
    }
794
795
0
 do_full_expiry:
796
0
    ibs->watchdog_running = 0; /* Stop the watchdog on a timeout */
797
0
    ibs->watchdog_expired |= (1 << IPMI_BMC_WATCHDOG_GET_USE(ibs));
798
0
    switch (IPMI_BMC_WATCHDOG_GET_ACTION(ibs)) {
799
0
    case IPMI_BMC_WATCHDOG_ACTION_NONE:
800
0
        sensor_set_discrete_bit(ibs, IPMI_WATCHDOG_SENSOR, 0, 1,
801
0
                                0xc0, ibs->watchdog_use & 0xf, 0xff,
802
0
                                do_log);
803
0
        break;
804
805
0
    case IPMI_BMC_WATCHDOG_ACTION_RESET:
806
0
        sensor_set_discrete_bit(ibs, IPMI_WATCHDOG_SENSOR, 1, 1,
807
0
                                0xc1, ibs->watchdog_use & 0xf, 0xff,
808
0
                                do_log);
809
0
        k->do_hw_op(s, IPMI_RESET_CHASSIS, 0);
810
0
        break;
811
812
0
    case IPMI_BMC_WATCHDOG_ACTION_POWER_DOWN:
813
0
        sensor_set_discrete_bit(ibs, IPMI_WATCHDOG_SENSOR, 2, 1,
814
0
                                0xc2, ibs->watchdog_use & 0xf, 0xff,
815
0
                                do_log);
816
0
        k->do_hw_op(s, IPMI_POWEROFF_CHASSIS, 0);
817
0
        break;
818
819
0
    case IPMI_BMC_WATCHDOG_ACTION_POWER_CYCLE:
820
0
        sensor_set_discrete_bit(ibs, IPMI_WATCHDOG_SENSOR, 2, 1,
821
0
                                0xc3, ibs->watchdog_use & 0xf, 0xff,
822
0
                                do_log);
823
0
        k->do_hw_op(s, IPMI_POWERCYCLE_CHASSIS, 0);
824
0
        break;
825
0
    }
826
827
0
 out:
828
0
    next_timeout(ibs);
829
0
}
830
831
static void chassis_capabilities(IPMIBmcSim *ibs,
832
                                 uint8_t *cmd, unsigned int cmd_len,
833
                                 RspBuffer *rsp)
834
0
{
835
0
    rsp_buffer_push(rsp, 0);
836
0
    rsp_buffer_push(rsp, ibs->parent.slave_addr);
837
0
    rsp_buffer_push(rsp, ibs->parent.slave_addr);
838
0
    rsp_buffer_push(rsp, ibs->parent.slave_addr);
839
0
    rsp_buffer_push(rsp, ibs->parent.slave_addr);
840
0
}
841
842
static void chassis_status(IPMIBmcSim *ibs,
843
                           uint8_t *cmd, unsigned int cmd_len,
844
                           RspBuffer *rsp)
845
0
{
846
0
    rsp_buffer_push(rsp, 0x61); /* Unknown power restore, power is on */
847
0
    rsp_buffer_push(rsp, 0);
848
0
    rsp_buffer_push(rsp, 0);
849
0
    rsp_buffer_push(rsp, 0);
850
0
}
851
852
static void chassis_control(IPMIBmcSim *ibs,
853
                            uint8_t *cmd, unsigned int cmd_len,
854
                            RspBuffer *rsp)
855
0
{
856
0
    IPMIInterface *s = ibs->parent.intf;
857
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
858
859
0
    switch (cmd[2] & 0xf) {
860
0
    case 0: /* power down */
861
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_POWEROFF_CHASSIS, 0));
862
0
        break;
863
0
    case 1: /* power up */
864
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_POWERON_CHASSIS, 0));
865
0
        break;
866
0
    case 2: /* power cycle */
867
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_POWERCYCLE_CHASSIS, 0));
868
0
        break;
869
0
    case 3: /* hard reset */
870
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_RESET_CHASSIS, 0));
871
0
        break;
872
0
    case 4: /* pulse diagnostic interrupt */
873
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_PULSE_DIAG_IRQ, 0));
874
0
        break;
875
0
    case 5: /* soft shutdown via ACPI by overtemp emulation */
876
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s,
877
0
                                          IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP, 0));
878
0
        break;
879
0
    default:
880
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
881
0
        return;
882
0
    }
883
0
}
884
885
static void chassis_get_sys_restart_cause(IPMIBmcSim *ibs,
886
                           uint8_t *cmd, unsigned int cmd_len,
887
                           RspBuffer *rsp)
888
889
0
{
890
0
    rsp_buffer_push(rsp, ibs->restart_cause & 0xf); /* Restart Cause */
891
0
    rsp_buffer_push(rsp, 0);  /* Channel 0 */
892
0
}
893
894
static void get_device_id(IPMIBmcSim *ibs,
895
                          uint8_t *cmd, unsigned int cmd_len,
896
                          RspBuffer *rsp)
897
0
{
898
0
    rsp_buffer_push(rsp, ibs->device_id);
899
0
    rsp_buffer_push(rsp, ibs->device_rev & 0xf);
900
0
    rsp_buffer_push(rsp, ibs->fwrev1 & 0x7f);
901
0
    rsp_buffer_push(rsp, ibs->fwrev2);
902
0
    rsp_buffer_push(rsp, ibs->ipmi_version);
903
0
    rsp_buffer_push(rsp, 0x07); /* sensor, SDR, and SEL. */
904
0
    rsp_buffer_push(rsp, ibs->mfg_id & 0xff);
905
0
    rsp_buffer_push(rsp, (ibs->mfg_id >> 8) & 0xff);
906
0
    rsp_buffer_push(rsp, (ibs->mfg_id >> 16) & 0xff);
907
0
    rsp_buffer_push(rsp, ibs->product_id & 0xff);
908
0
    rsp_buffer_push(rsp, (ibs->product_id >> 8) & 0xff);
909
0
}
910
911
static void set_global_enables(IPMIBmcSim *ibs, uint8_t val)
912
0
{
913
0
    IPMIInterface *s = ibs->parent.intf;
914
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
915
0
    bool irqs_on;
916
917
0
    ibs->bmc_global_enables = val;
918
919
0
    irqs_on = val & (IPMI_BMC_EVBUF_FULL_INT_BIT |
920
0
                     IPMI_BMC_RCV_MSG_QUEUE_INT_BIT);
921
922
0
    k->set_irq_enable(s, irqs_on);
923
0
}
924
925
static void cold_reset(IPMIBmcSim *ibs,
926
                       uint8_t *cmd, unsigned int cmd_len,
927
                       RspBuffer *rsp)
928
0
{
929
0
    IPMIInterface *s = ibs->parent.intf;
930
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
931
932
    /* Disable all interrupts */
933
0
    set_global_enables(ibs, 1 << IPMI_BMC_EVENT_LOG_BIT);
934
935
0
    if (k->reset) {
936
0
        k->reset(s, true);
937
0
    }
938
0
}
939
940
static void warm_reset(IPMIBmcSim *ibs,
941
                       uint8_t *cmd, unsigned int cmd_len,
942
                       RspBuffer *rsp)
943
0
{
944
0
    IPMIInterface *s = ibs->parent.intf;
945
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
946
947
0
    if (k->reset) {
948
0
        k->reset(s, false);
949
0
    }
950
0
}
951
static void set_acpi_power_state(IPMIBmcSim *ibs,
952
                                 uint8_t *cmd, unsigned int cmd_len,
953
                                 RspBuffer *rsp)
954
0
{
955
0
    ibs->acpi_power_state[0] = cmd[2];
956
0
    ibs->acpi_power_state[1] = cmd[3];
957
0
}
958
959
static void get_acpi_power_state(IPMIBmcSim *ibs,
960
                                 uint8_t *cmd, unsigned int cmd_len,
961
                                 RspBuffer *rsp)
962
0
{
963
0
    rsp_buffer_push(rsp, ibs->acpi_power_state[0]);
964
0
    rsp_buffer_push(rsp, ibs->acpi_power_state[1]);
965
0
}
966
967
static void get_device_guid(IPMIBmcSim *ibs,
968
                            uint8_t *cmd, unsigned int cmd_len,
969
                            RspBuffer *rsp)
970
0
{
971
0
    unsigned int i;
972
973
    /* An uninitialized uuid is all zeros, use that to know if it is set. */
974
0
    for (i = 0; i < 16; i++) {
975
0
        if (ibs->uuid.data[i]) {
976
0
            goto uuid_set;
977
0
        }
978
0
    }
979
    /* No uuid is set, return an error. */
980
0
    rsp_buffer_set_error(rsp, IPMI_CC_INVALID_CMD);
981
0
    return;
982
983
0
 uuid_set:
984
0
    for (i = 0; i < 16; i++) {
985
0
        rsp_buffer_push(rsp, ibs->uuid.data[i]);
986
0
    }
987
0
}
988
989
static void set_bmc_global_enables(IPMIBmcSim *ibs,
990
                                   uint8_t *cmd, unsigned int cmd_len,
991
                                   RspBuffer *rsp)
992
0
{
993
0
    uint8_t val = cmd[2];
994
995
0
    if (val & ~IPMI_BMC_GLOBAL_ENABLES_SUPPORTED) {
996
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
997
0
        return;
998
0
    }
999
1000
0
    set_global_enables(ibs, val);
1001
0
}
1002
1003
static void get_bmc_global_enables(IPMIBmcSim *ibs,
1004
                                   uint8_t *cmd, unsigned int cmd_len,
1005
                                   RspBuffer *rsp)
1006
0
{
1007
0
    rsp_buffer_push(rsp, ibs->bmc_global_enables);
1008
0
}
1009
1010
static void clr_msg_flags(IPMIBmcSim *ibs,
1011
                          uint8_t *cmd, unsigned int cmd_len,
1012
                          RspBuffer *rsp)
1013
0
{
1014
0
    IPMIInterface *s = ibs->parent.intf;
1015
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
1016
1017
0
    ibs->msg_flags &= ~cmd[2];
1018
0
    k->set_atn(s, attn_set(ibs), attn_irq_enabled(ibs));
1019
0
}
1020
1021
static void get_msg_flags(IPMIBmcSim *ibs,
1022
                          uint8_t *cmd, unsigned int cmd_len,
1023
                          RspBuffer *rsp)
1024
0
{
1025
0
    rsp_buffer_push(rsp, ibs->msg_flags);
1026
0
}
1027
1028
static void read_evt_msg_buf(IPMIBmcSim *ibs,
1029
                             uint8_t *cmd, unsigned int cmd_len,
1030
                             RspBuffer *rsp)
1031
0
{
1032
0
    IPMIInterface *s = ibs->parent.intf;
1033
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
1034
0
    unsigned int i;
1035
1036
0
    if (!(ibs->msg_flags & IPMI_BMC_MSG_FLAG_EVT_BUF_FULL)) {
1037
0
        rsp_buffer_set_error(rsp, 0x80);
1038
0
        return;
1039
0
    }
1040
0
    for (i = 0; i < 16; i++) {
1041
0
        rsp_buffer_push(rsp, ibs->evtbuf[i]);
1042
0
    }
1043
0
    ibs->msg_flags &= ~IPMI_BMC_MSG_FLAG_EVT_BUF_FULL;
1044
0
    k->set_atn(s, attn_set(ibs), attn_irq_enabled(ibs));
1045
0
}
1046
1047
static void get_msg(IPMIBmcSim *ibs,
1048
                    uint8_t *cmd, unsigned int cmd_len,
1049
                    RspBuffer *rsp)
1050
0
{
1051
0
    IPMIRcvBufEntry *msg;
1052
1053
0
    if (QTAILQ_EMPTY(&ibs->rcvbufs)) {
1054
0
        rsp_buffer_set_error(rsp, 0x80); /* Queue empty */
1055
0
        return;
1056
0
    }
1057
0
    rsp_buffer_push(rsp, 0); /* Channel 0 */
1058
0
    msg = QTAILQ_FIRST(&ibs->rcvbufs);
1059
0
    rsp_buffer_pushmore(rsp, msg->buf, msg->len);
1060
0
    QTAILQ_REMOVE(&ibs->rcvbufs, msg, entry);
1061
0
    g_free(msg);
1062
1063
0
    if (QTAILQ_EMPTY(&ibs->rcvbufs)) {
1064
0
        IPMIInterface *s = ibs->parent.intf;
1065
0
        IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
1066
1067
0
        ibs->msg_flags &= ~IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE;
1068
0
        k->set_atn(s, attn_set(ibs), attn_irq_enabled(ibs));
1069
0
    }
1070
0
}
1071
1072
static unsigned char
1073
ipmb_checksum(unsigned char *data, int size, unsigned char csum)
1074
0
{
1075
0
    for (; size > 0; size--, data++) {
1076
0
            csum += *data;
1077
0
    }
1078
1079
0
    return -csum;
1080
0
}
1081
1082
static void send_msg(IPMIBmcSim *ibs,
1083
                     uint8_t *cmd, unsigned int cmd_len,
1084
                     RspBuffer *rsp)
1085
0
{
1086
0
    IPMIInterface *s = ibs->parent.intf;
1087
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
1088
0
    IPMIRcvBufEntry *msg;
1089
0
    uint8_t *buf;
1090
0
    uint8_t netfn, rqLun, rsLun, rqSeq;
1091
1092
0
    if (cmd[2] != IPMI_CHANNEL_IPMB) {
1093
        /* We only handle channel 0h (IPMB) with no options */
1094
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1095
0
        return;
1096
0
    }
1097
1098
0
    if (cmd_len < 10) {
1099
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
1100
0
        return;
1101
0
    }
1102
1103
0
    if (cmd[3] != 0x40) {
1104
        /* We only emulate a MC at address 0x40. */
1105
0
        rsp_buffer_set_error(rsp, 0x83); /* NAK on write */
1106
0
        return;
1107
0
    }
1108
1109
0
    cmd += 3; /* Skip the header. */
1110
0
    cmd_len -= 3;
1111
1112
    /*
1113
     * At this point we "send" the message successfully.  Any error will
1114
     * be returned in the response.
1115
     */
1116
0
    if (ipmb_checksum(cmd, cmd_len, 0) != 0 ||
1117
0
        cmd[3] != 0x20) { /* Improper response address */
1118
0
        return; /* No response */
1119
0
    }
1120
1121
0
    netfn = cmd[1] >> 2;
1122
0
    rqLun = cmd[4] & 0x3;
1123
0
    rsLun = cmd[1] & 0x3;
1124
0
    rqSeq = cmd[4] >> 2;
1125
1126
0
    if (rqLun != 2) {
1127
        /* We only support LUN 2 coming back to us. */
1128
0
        return;
1129
0
    }
1130
1131
0
    msg = g_malloc(sizeof(*msg));
1132
0
    msg->buf[0] = ((netfn | 1) << 2) | rqLun; /* NetFN, and make a response */
1133
0
    msg->buf[1] = ipmb_checksum(msg->buf, 1, 0);
1134
0
    msg->buf[2] = cmd[0]; /* rsSA */
1135
0
    msg->buf[3] = (rqSeq << 2) | rsLun;
1136
0
    msg->buf[4] = cmd[5]; /* Cmd */
1137
0
    msg->buf[5] = 0; /* Completion Code */
1138
0
    msg->len = 6;
1139
1140
0
    if ((cmd[1] >> 2) != IPMI_NETFN_APP || cmd[5] != IPMI_CMD_GET_DEVICE_ID) {
1141
        /* Not a command we handle. */
1142
0
        msg->buf[5] = IPMI_CC_INVALID_CMD;
1143
0
        goto end_msg;
1144
0
    }
1145
1146
0
    buf = msg->buf + msg->len; /* After the CC */
1147
0
    buf[0] = 0;
1148
0
    buf[1] = 0;
1149
0
    buf[2] = 0;
1150
0
    buf[3] = 0;
1151
0
    buf[4] = 0x51;
1152
0
    buf[5] = 0;
1153
0
    buf[6] = 0;
1154
0
    buf[7] = 0;
1155
0
    buf[8] = 0;
1156
0
    buf[9] = 0;
1157
0
    buf[10] = 0;
1158
0
    msg->len += 11;
1159
1160
0
 end_msg:
1161
0
    msg->buf[msg->len] = ipmb_checksum(msg->buf, msg->len, 0);
1162
0
    msg->len++;
1163
0
    QTAILQ_INSERT_TAIL(&ibs->rcvbufs, msg, entry);
1164
0
    ibs->msg_flags |= IPMI_BMC_MSG_FLAG_RCV_MSG_QUEUE;
1165
0
    k->set_atn(s, 1, attn_irq_enabled(ibs));
1166
0
}
1167
1168
static void do_watchdog_reset(IPMIBmcSim *ibs)
1169
0
{
1170
0
    if (IPMI_BMC_WATCHDOG_GET_ACTION(ibs) ==
1171
0
        IPMI_BMC_WATCHDOG_ACTION_NONE) {
1172
0
        ibs->watchdog_running = 0;
1173
0
        return;
1174
0
    }
1175
0
    ibs->watchdog_preaction_ran = 0;
1176
1177
1178
    /* Timeout is in tenths of a second, offset is in seconds */
1179
0
    ibs->watchdog_expiry = ipmi_getmonotime();
1180
0
    ibs->watchdog_expiry += ibs->watchdog_timeout * 100000000LL;
1181
0
    if (IPMI_BMC_WATCHDOG_GET_PRE_ACTION(ibs) != IPMI_BMC_WATCHDOG_PRE_NONE) {
1182
0
        ibs->watchdog_expiry -= ibs->watchdog_pretimeout * 1000000000LL;
1183
0
    }
1184
0
    ibs->watchdog_running = 1;
1185
0
}
1186
1187
static void reset_watchdog_timer(IPMIBmcSim *ibs,
1188
                                 uint8_t *cmd, unsigned int cmd_len,
1189
                                 RspBuffer *rsp)
1190
0
{
1191
0
    if (!ibs->watchdog_initialized) {
1192
0
        rsp_buffer_set_error(rsp, 0x80);
1193
0
        return;
1194
0
    }
1195
0
    do_watchdog_reset(ibs);
1196
0
}
1197
1198
static void set_watchdog_timer(IPMIBmcSim *ibs,
1199
                               uint8_t *cmd, unsigned int cmd_len,
1200
                               RspBuffer *rsp)
1201
0
{
1202
0
    IPMIInterface *s = ibs->parent.intf;
1203
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
1204
0
    unsigned int val;
1205
1206
0
    val = cmd[2] & 0x7; /* Validate use */
1207
0
    if (val == 0 || val > 5) {
1208
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1209
0
        return;
1210
0
    }
1211
0
    val = cmd[3] & 0x7; /* Validate action */
1212
0
    switch (val) {
1213
0
    case IPMI_BMC_WATCHDOG_ACTION_NONE:
1214
0
        break;
1215
1216
0
    case IPMI_BMC_WATCHDOG_ACTION_RESET:
1217
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_RESET_CHASSIS, 1));
1218
0
        break;
1219
1220
0
    case IPMI_BMC_WATCHDOG_ACTION_POWER_DOWN:
1221
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_POWEROFF_CHASSIS, 1));
1222
0
        break;
1223
1224
0
    case IPMI_BMC_WATCHDOG_ACTION_POWER_CYCLE:
1225
0
        rsp_buffer_set_error(rsp, k->do_hw_op(s, IPMI_POWERCYCLE_CHASSIS, 1));
1226
0
        break;
1227
1228
0
    default:
1229
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1230
0
    }
1231
0
    if (rsp->buffer[2]) {
1232
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1233
0
        return;
1234
0
    }
1235
1236
0
    val = (cmd[3] >> 4) & 0x7; /* Validate preaction */
1237
0
    switch (val) {
1238
0
    case IPMI_BMC_WATCHDOG_PRE_MSG_INT:
1239
0
    case IPMI_BMC_WATCHDOG_PRE_NONE:
1240
0
        break;
1241
1242
0
    case IPMI_BMC_WATCHDOG_PRE_NMI:
1243
0
        if (k->do_hw_op(s, IPMI_SEND_NMI, 1)) {
1244
            /* NMI not supported. */
1245
0
            rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1246
0
            return;
1247
0
        }
1248
0
        break;
1249
1250
0
    default:
1251
        /* We don't support PRE_SMI */
1252
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1253
0
        return;
1254
0
    }
1255
1256
0
    ibs->watchdog_initialized = 1;
1257
0
    ibs->watchdog_use = cmd[2] & IPMI_BMC_WATCHDOG_USE_MASK;
1258
0
    ibs->watchdog_action = cmd[3] & IPMI_BMC_WATCHDOG_ACTION_MASK;
1259
0
    ibs->watchdog_pretimeout = cmd[4];
1260
0
    ibs->watchdog_expired &= ~cmd[5];
1261
0
    ibs->watchdog_timeout = cmd[6] | (((uint16_t) cmd[7]) << 8);
1262
0
    if (ibs->watchdog_running & IPMI_BMC_WATCHDOG_GET_DONT_STOP(ibs)) {
1263
0
        do_watchdog_reset(ibs);
1264
0
    } else {
1265
0
        ibs->watchdog_running = 0;
1266
0
    }
1267
0
}
1268
1269
static void get_watchdog_timer(IPMIBmcSim *ibs,
1270
                               uint8_t *cmd, unsigned int cmd_len,
1271
                               RspBuffer *rsp)
1272
0
{
1273
0
    rsp_buffer_push(rsp, ibs->watchdog_use);
1274
0
    rsp_buffer_push(rsp, ibs->watchdog_action);
1275
0
    rsp_buffer_push(rsp, ibs->watchdog_pretimeout);
1276
0
    rsp_buffer_push(rsp, ibs->watchdog_expired);
1277
0
    rsp_buffer_push(rsp, ibs->watchdog_timeout & 0xff);
1278
0
    rsp_buffer_push(rsp, (ibs->watchdog_timeout >> 8) & 0xff);
1279
0
    if (ibs->watchdog_running) {
1280
0
        long timeout;
1281
0
        timeout = ((ibs->watchdog_expiry - ipmi_getmonotime() + 50000000)
1282
0
                   / 100000000);
1283
0
        rsp_buffer_push(rsp, timeout & 0xff);
1284
0
        rsp_buffer_push(rsp, (timeout >> 8) & 0xff);
1285
0
    } else {
1286
0
        rsp_buffer_push(rsp, 0);
1287
0
        rsp_buffer_push(rsp, 0);
1288
0
    }
1289
0
}
1290
1291
static void get_channel_access(IPMIBmcSim *ibs,
1292
                               uint8_t *cmd, unsigned int cmd_len,
1293
                               RspBuffer *rsp)
1294
0
{
1295
0
    uint8_t channel = cmd[2] & 0xf;
1296
1297
0
    if (channel == IPMI_CHANNEL_IPMB || channel == IPMI_CHANNEL_SYSTEM ||
1298
0
        IPMI_BMC_CHANNEL_IS_LAN(ibs, channel)) {
1299
        /* alerting disabled */
1300
        /* per message authentication disabled */
1301
        /* user level authentication disabled */
1302
        /* channel always available */
1303
0
        rsp_buffer_push(rsp, 0x20 | 0x10 | 0x08 | 0x02);
1304
0
        rsp_buffer_push(rsp, 0x04);  /* privilege limit: ADMINISTRATOR */
1305
0
    } else {
1306
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1307
0
    }
1308
0
}
1309
1310
static void get_channel_info(IPMIBmcSim *ibs,
1311
                             uint8_t *cmd, unsigned int cmd_len,
1312
                             RspBuffer *rsp)
1313
0
{
1314
0
    IPMIInterface *s = ibs->parent.intf;
1315
0
    IPMIInterfaceClass *k = IPMI_INTERFACE_GET_CLASS(s);
1316
0
    IPMIFwInfo info = {};
1317
0
    uint8_t ch = cmd[2] & 0x0f;
1318
1319
0
    if (ch == 0x0e) { /* "This channel" */
1320
0
        ch = IPMI_CHANNEL_SYSTEM;
1321
0
    }
1322
0
    rsp_buffer_push(rsp, ch);
1323
1324
0
    if (k->get_fwinfo) {
1325
0
        k->get_fwinfo(s, &info);
1326
0
    }
1327
1328
    /* Only define channel 0h (IPMB), LAN, and Fh (system interface) */
1329
0
    if (ch == IPMI_CHANNEL_IPMB) {
1330
0
        rsp_buffer_push(rsp, IPMI_CHANNEL_MEDIUM_IPMB);
1331
0
        rsp_buffer_push(rsp, IPMI_CHANNEL_PROTOCOL_IPMB);
1332
0
    } else if (IPMI_BMC_CHANNEL_IS_LAN(ibs, ch)) {
1333
0
        rsp_buffer_push(rsp, IPMI_CHANNEL_MEDIUM_802_3_LAN);
1334
0
        rsp_buffer_push(rsp, IPMI_CHANNEL_PROTOCOL_IPMB);
1335
0
    } else if (ch == IPMI_CHANNEL_SYSTEM) {
1336
0
        rsp_buffer_push(rsp, IPMI_CHANNEL_MEDIUM_SYSTEM);
1337
0
        rsp_buffer_push(rsp, info.ipmi_channel_protocol);
1338
0
    } else {
1339
        /* Not a supported channel */
1340
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1341
0
        return;
1342
0
    }
1343
1344
0
    rsp_buffer_push(rsp, 0x00); /* Session-less */
1345
1346
    /* IPMI Enterprise Number for Vendor ID */
1347
0
    rsp_buffer_push(rsp, 0xf2);
1348
0
    rsp_buffer_push(rsp, 0x1b);
1349
0
    rsp_buffer_push(rsp, 0x00);
1350
1351
0
    if (ch == IPMI_CHANNEL_SYSTEM) {
1352
0
        uint8_t irq;
1353
1354
0
        if (info.irq_source == IPMI_ISA_IRQ) {
1355
0
            irq = info.interrupt_number;
1356
0
        } else if (info.irq_source == IPMI_PCI_IRQ) {
1357
0
            irq = 0x10 + info.interrupt_number;
1358
0
        } else {
1359
0
            irq = 0xff; /* no interrupt / unspecified */
1360
0
        }
1361
1362
        /* Both interrupts use the same irq number */
1363
0
        rsp_buffer_push(rsp, irq);
1364
0
        rsp_buffer_push(rsp, irq);
1365
0
    } else {
1366
        /* Reserved */
1367
0
        rsp_buffer_push(rsp, 0x00);
1368
0
        rsp_buffer_push(rsp, 0x00);
1369
0
    }
1370
0
}
1371
1372
static void get_sdr_rep_info(IPMIBmcSim *ibs,
1373
                             uint8_t *cmd, unsigned int cmd_len,
1374
                             RspBuffer *rsp)
1375
0
{
1376
0
    unsigned int i;
1377
1378
0
    rsp_buffer_push(rsp, 0x51); /* Conform to IPMI 1.5 spec */
1379
0
    rsp_buffer_push(rsp, ibs->sdr.next_rec_id & 0xff);
1380
0
    rsp_buffer_push(rsp, (ibs->sdr.next_rec_id >> 8) & 0xff);
1381
0
    rsp_buffer_push(rsp, (MAX_SDR_SIZE - ibs->sdr.next_free) & 0xff);
1382
0
    rsp_buffer_push(rsp, ((MAX_SDR_SIZE - ibs->sdr.next_free) >> 8) & 0xff);
1383
0
    for (i = 0; i < 4; i++) {
1384
0
        rsp_buffer_push(rsp, ibs->sdr.last_addition[i]);
1385
0
    }
1386
0
    for (i = 0; i < 4; i++) {
1387
0
        rsp_buffer_push(rsp, ibs->sdr.last_clear[i]);
1388
0
    }
1389
    /* Only modal support, reserve supported */
1390
0
    rsp_buffer_push(rsp, (ibs->sdr.overflow << 7) | 0x22);
1391
0
}
1392
1393
static void reserve_sdr_rep(IPMIBmcSim *ibs,
1394
                            uint8_t *cmd, unsigned int cmd_len,
1395
                            RspBuffer *rsp)
1396
0
{
1397
0
    rsp_buffer_push(rsp, ibs->sdr.reservation & 0xff);
1398
0
    rsp_buffer_push(rsp, (ibs->sdr.reservation >> 8) & 0xff);
1399
0
}
1400
1401
static void get_sdr(IPMIBmcSim *ibs,
1402
                    uint8_t *cmd, unsigned int cmd_len,
1403
                    RspBuffer *rsp)
1404
0
{
1405
0
    unsigned int pos;
1406
0
    uint16_t nextrec;
1407
0
    struct ipmi_sdr_header *sdrh;
1408
1409
0
    if (cmd[6]) {
1410
0
        if ((cmd[2] | (cmd[3] << 8)) != ibs->sdr.reservation) {
1411
0
            rsp_buffer_set_error(rsp, IPMI_CC_INVALID_RESERVATION);
1412
0
            return;
1413
0
        }
1414
0
    }
1415
1416
0
    pos = 0;
1417
0
    if (sdr_find_entry(&ibs->sdr, cmd[4] | (cmd[5] << 8),
1418
0
                       &pos, &nextrec)) {
1419
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1420
0
        return;
1421
0
    }
1422
1423
0
    sdrh = (struct ipmi_sdr_header *) &ibs->sdr.sdr[pos];
1424
1425
0
    if (cmd[6] > ipmi_sdr_length(sdrh)) {
1426
0
        rsp_buffer_set_error(rsp, IPMI_CC_PARM_OUT_OF_RANGE);
1427
0
        return;
1428
0
    }
1429
1430
0
    rsp_buffer_push(rsp, nextrec & 0xff);
1431
0
    rsp_buffer_push(rsp, (nextrec >> 8) & 0xff);
1432
1433
0
    if (cmd[7] == 0xff) {
1434
0
        cmd[7] = ipmi_sdr_length(sdrh) - cmd[6];
1435
0
    }
1436
1437
0
    if ((cmd[7] + rsp->len) > sizeof(rsp->buffer)) {
1438
0
        rsp_buffer_set_error(rsp, IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES);
1439
0
        return;
1440
0
    }
1441
1442
0
    rsp_buffer_pushmore(rsp, ibs->sdr.sdr + pos + cmd[6], cmd[7]);
1443
0
}
1444
1445
static void add_sdr(IPMIBmcSim *ibs,
1446
                    uint8_t *cmd, unsigned int cmd_len,
1447
                    RspBuffer *rsp)
1448
0
{
1449
0
    uint16_t recid;
1450
0
    struct ipmi_sdr_header *sdrh = (struct ipmi_sdr_header *) cmd + 2;
1451
1452
0
    if (sdr_add_entry(ibs, sdrh, cmd_len - 2, &recid)) {
1453
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1454
0
        return;
1455
0
    }
1456
0
    rsp_buffer_push(rsp, recid & 0xff);
1457
0
    rsp_buffer_push(rsp, (recid >> 8) & 0xff);
1458
0
}
1459
1460
static void clear_sdr_rep(IPMIBmcSim *ibs,
1461
                          uint8_t *cmd, unsigned int cmd_len,
1462
                          RspBuffer *rsp)
1463
0
{
1464
0
    if ((cmd[2] | (cmd[3] << 8)) != ibs->sdr.reservation) {
1465
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_RESERVATION);
1466
0
        return;
1467
0
    }
1468
1469
0
    if (cmd[4] != 'C' || cmd[5] != 'L' || cmd[6] != 'R') {
1470
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1471
0
        return;
1472
0
    }
1473
0
    if (cmd[7] == 0xaa) {
1474
0
        ibs->sdr.next_free = 0;
1475
0
        ibs->sdr.overflow = 0;
1476
0
        set_timestamp(ibs, ibs->sdr.last_clear);
1477
0
        rsp_buffer_push(rsp, 1); /* Erasure complete */
1478
0
        sdr_inc_reservation(&ibs->sdr);
1479
0
    } else if (cmd[7] == 0) {
1480
0
        rsp_buffer_push(rsp, 1); /* Erasure complete */
1481
0
    } else {
1482
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1483
0
        return;
1484
0
    }
1485
0
}
1486
1487
static void get_sel_info(IPMIBmcSim *ibs,
1488
                         uint8_t *cmd, unsigned int cmd_len,
1489
                         RspBuffer *rsp)
1490
0
{
1491
0
    unsigned int i, val;
1492
1493
0
    rsp_buffer_push(rsp, 0x51); /* Conform to IPMI 1.5 */
1494
0
    rsp_buffer_push(rsp, ibs->sel.next_free & 0xff);
1495
0
    rsp_buffer_push(rsp, (ibs->sel.next_free >> 8) & 0xff);
1496
0
    val = (MAX_SEL_SIZE - ibs->sel.next_free) * 16;
1497
0
    rsp_buffer_push(rsp, val & 0xff);
1498
0
    rsp_buffer_push(rsp, (val >> 8) & 0xff);
1499
0
    for (i = 0; i < 4; i++) {
1500
0
        rsp_buffer_push(rsp, ibs->sel.last_addition[i]);
1501
0
    }
1502
0
    for (i = 0; i < 4; i++) {
1503
0
        rsp_buffer_push(rsp, ibs->sel.last_clear[i]);
1504
0
    }
1505
    /* Only support Reserve SEL */
1506
0
    rsp_buffer_push(rsp, (ibs->sel.overflow << 7) | 0x02);
1507
0
}
1508
1509
static void get_fru_area_info(IPMIBmcSim *ibs,
1510
                         uint8_t *cmd, unsigned int cmd_len,
1511
                         RspBuffer *rsp)
1512
0
{
1513
0
    uint8_t fruid;
1514
0
    uint16_t fru_entry_size;
1515
1516
0
    fruid = cmd[2];
1517
1518
0
    if (fruid >= ibs->fru.nentries) {
1519
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1520
0
        return;
1521
0
    }
1522
1523
0
    fru_entry_size = ibs->fru.areasize;
1524
1525
0
    rsp_buffer_push(rsp, fru_entry_size & 0xff);
1526
0
    rsp_buffer_push(rsp, fru_entry_size >> 8 & 0xff);
1527
0
    rsp_buffer_push(rsp, 0x0);
1528
0
}
1529
1530
static void read_fru_data(IPMIBmcSim *ibs,
1531
                         uint8_t *cmd, unsigned int cmd_len,
1532
                         RspBuffer *rsp)
1533
0
{
1534
0
    uint8_t fruid;
1535
0
    uint16_t offset;
1536
0
    int i;
1537
0
    uint8_t *fru_entry;
1538
0
    unsigned int count;
1539
1540
0
    fruid = cmd[2];
1541
0
    offset = (cmd[3] | cmd[4] << 8);
1542
1543
0
    if (fruid >= ibs->fru.nentries) {
1544
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1545
0
        return;
1546
0
    }
1547
1548
0
    if (offset >= ibs->fru.areasize - 1) {
1549
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1550
0
        return;
1551
0
    }
1552
1553
0
    fru_entry = &ibs->fru.data[fruid * ibs->fru.areasize];
1554
1555
0
    count = MIN(cmd[5], ibs->fru.areasize - offset);
1556
1557
0
    rsp_buffer_push(rsp, count & 0xff);
1558
0
    for (i = 0; i < count; i++) {
1559
0
        rsp_buffer_push(rsp, fru_entry[offset + i]);
1560
0
    }
1561
0
}
1562
1563
static void write_fru_data(IPMIBmcSim *ibs,
1564
                         uint8_t *cmd, unsigned int cmd_len,
1565
                         RspBuffer *rsp)
1566
0
{
1567
0
    uint8_t fruid;
1568
0
    uint16_t offset;
1569
0
    uint8_t *fru_entry;
1570
0
    unsigned int count;
1571
1572
0
    fruid = cmd[2];
1573
0
    offset = (cmd[3] | cmd[4] << 8);
1574
1575
0
    if (fruid >= ibs->fru.nentries) {
1576
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1577
0
        return;
1578
0
    }
1579
1580
0
    if (offset >= ibs->fru.areasize - 1) {
1581
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1582
0
        return;
1583
0
    }
1584
1585
0
    fru_entry = &ibs->fru.data[fruid * ibs->fru.areasize];
1586
1587
0
    count = MIN(cmd_len - 5, ibs->fru.areasize - offset);
1588
1589
0
    memcpy(fru_entry + offset, cmd + 5, count);
1590
1591
0
    rsp_buffer_push(rsp, count & 0xff);
1592
0
}
1593
1594
static void reserve_sel(IPMIBmcSim *ibs,
1595
                        uint8_t *cmd, unsigned int cmd_len,
1596
                        RspBuffer *rsp)
1597
0
{
1598
0
    rsp_buffer_push(rsp, ibs->sel.reservation & 0xff);
1599
0
    rsp_buffer_push(rsp, (ibs->sel.reservation >> 8) & 0xff);
1600
0
}
1601
1602
static void get_sel_entry(IPMIBmcSim *ibs,
1603
                          uint8_t *cmd, unsigned int cmd_len,
1604
                          RspBuffer *rsp)
1605
0
{
1606
0
    unsigned int val;
1607
1608
0
    if (cmd[6]) {
1609
0
        if ((cmd[2] | (cmd[3] << 8)) != ibs->sel.reservation) {
1610
0
            rsp_buffer_set_error(rsp, IPMI_CC_INVALID_RESERVATION);
1611
0
            return;
1612
0
        }
1613
0
    }
1614
0
    if (ibs->sel.next_free == 0) {
1615
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1616
0
        return;
1617
0
    }
1618
0
    if (cmd[6] > 15) {
1619
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1620
0
        return;
1621
0
    }
1622
0
    if (cmd[7] == 0xff) {
1623
0
        cmd[7] = 16;
1624
0
    } else if ((cmd[7] + cmd[6]) > 16) {
1625
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1626
0
        return;
1627
0
    } else {
1628
0
        cmd[7] += cmd[6];
1629
0
    }
1630
1631
0
    val = cmd[4] | (cmd[5] << 8);
1632
0
    if (val == 0xffff) {
1633
0
        val = ibs->sel.next_free - 1;
1634
0
    } else if (val >= ibs->sel.next_free) {
1635
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1636
0
        return;
1637
0
    }
1638
0
    if ((val + 1) == ibs->sel.next_free) {
1639
0
        rsp_buffer_push(rsp, 0xff);
1640
0
        rsp_buffer_push(rsp, 0xff);
1641
0
    } else {
1642
0
        rsp_buffer_push(rsp, (val + 1) & 0xff);
1643
0
        rsp_buffer_push(rsp, ((val + 1) >> 8) & 0xff);
1644
0
    }
1645
0
    for (; cmd[6] < cmd[7]; cmd[6]++) {
1646
0
        rsp_buffer_push(rsp, ibs->sel.sel[val][cmd[6]]);
1647
0
    }
1648
0
}
1649
1650
static void add_sel_entry(IPMIBmcSim *ibs,
1651
                          uint8_t *cmd, unsigned int cmd_len,
1652
                          RspBuffer *rsp)
1653
0
{
1654
0
    if (sel_add_event(ibs, cmd + 2)) {
1655
0
        rsp_buffer_set_error(rsp, IPMI_CC_OUT_OF_SPACE);
1656
0
        return;
1657
0
    }
1658
    /* sel_add_event fills in the record number. */
1659
0
    rsp_buffer_push(rsp, cmd[2]);
1660
0
    rsp_buffer_push(rsp, cmd[3]);
1661
0
}
1662
1663
static void clear_sel(IPMIBmcSim *ibs,
1664
                      uint8_t *cmd, unsigned int cmd_len,
1665
                      RspBuffer *rsp)
1666
0
{
1667
0
    if ((cmd[2] | (cmd[3] << 8)) != ibs->sel.reservation) {
1668
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_RESERVATION);
1669
0
        return;
1670
0
    }
1671
1672
0
    if (cmd[4] != 'C' || cmd[5] != 'L' || cmd[6] != 'R') {
1673
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1674
0
        return;
1675
0
    }
1676
0
    if (cmd[7] == 0xaa) {
1677
0
        ibs->sel.next_free = 0;
1678
0
        ibs->sel.overflow = 0;
1679
0
        set_timestamp(ibs, ibs->sdr.last_clear);
1680
0
        rsp_buffer_push(rsp, 1); /* Erasure complete */
1681
0
        sel_inc_reservation(&ibs->sel);
1682
0
    } else if (cmd[7] == 0) {
1683
0
        rsp_buffer_push(rsp, 1); /* Erasure complete */
1684
0
    } else {
1685
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1686
0
        return;
1687
0
    }
1688
0
}
1689
1690
static void get_sel_time(IPMIBmcSim *ibs,
1691
                         uint8_t *cmd, unsigned int cmd_len,
1692
                         RspBuffer *rsp)
1693
0
{
1694
0
    uint32_t val;
1695
0
    struct ipmi_time now;
1696
1697
0
    ipmi_gettime(&now);
1698
0
    val = now.tv_sec + ibs->sel.time_offset;
1699
0
    rsp_buffer_push(rsp, val & 0xff);
1700
0
    rsp_buffer_push(rsp, (val >> 8) & 0xff);
1701
0
    rsp_buffer_push(rsp, (val >> 16) & 0xff);
1702
0
    rsp_buffer_push(rsp, (val >> 24) & 0xff);
1703
0
}
1704
1705
static void set_sel_time(IPMIBmcSim *ibs,
1706
                         uint8_t *cmd, unsigned int cmd_len,
1707
                         RspBuffer *rsp)
1708
0
{
1709
0
    uint32_t val;
1710
0
    struct ipmi_time now;
1711
1712
0
    val = cmd[2] | (cmd[3] << 8) | (cmd[4] << 16) | (cmd[5] << 24);
1713
0
    ipmi_gettime(&now);
1714
0
    ibs->sel.time_offset = now.tv_sec - ((long) val);
1715
0
}
1716
1717
static void platform_event_msg(IPMIBmcSim *ibs,
1718
                               uint8_t *cmd, unsigned int cmd_len,
1719
                               RspBuffer *rsp)
1720
0
{
1721
0
    uint8_t event[16];
1722
1723
0
    event[2] = 2; /* System event record */
1724
0
    event[7] = cmd[2]; /* Generator ID */
1725
0
    event[8] = 0;
1726
0
    event[9] = cmd[3]; /* EvMRev */
1727
0
    event[10] = cmd[4]; /* Sensor type */
1728
0
    event[11] = cmd[5]; /* Sensor number */
1729
0
    event[12] = cmd[6]; /* Event dir / Event type */
1730
0
    event[13] = cmd[7]; /* Event data 1 */
1731
0
    event[14] = cmd[8]; /* Event data 2 */
1732
0
    event[15] = cmd[9]; /* Event data 3 */
1733
1734
0
    if (sel_add_event(ibs, event)) {
1735
0
        rsp_buffer_set_error(rsp, IPMI_CC_OUT_OF_SPACE);
1736
0
    }
1737
0
}
1738
1739
static void set_sensor_evt_enable(IPMIBmcSim *ibs,
1740
                                  uint8_t *cmd, unsigned int cmd_len,
1741
                                  RspBuffer *rsp)
1742
0
{
1743
0
    IPMISensor *sens;
1744
1745
0
    if ((cmd[2] >= MAX_SENSORS) ||
1746
0
            !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1747
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1748
0
        return;
1749
0
    }
1750
0
    sens = ibs->sensors + cmd[2];
1751
0
    switch ((cmd[3] >> 4) & 0x3) {
1752
0
    case 0: /* Do not change */
1753
0
        break;
1754
0
    case 1: /* Enable bits */
1755
0
        if (cmd_len > 4) {
1756
0
            sens->assert_enable |= cmd[4];
1757
0
        }
1758
0
        if (cmd_len > 5) {
1759
0
            sens->assert_enable |= cmd[5] << 8;
1760
0
        }
1761
0
        if (cmd_len > 6) {
1762
0
            sens->deassert_enable |= cmd[6];
1763
0
        }
1764
0
        if (cmd_len > 7) {
1765
0
            sens->deassert_enable |= cmd[7] << 8;
1766
0
        }
1767
0
        break;
1768
0
    case 2: /* Disable bits */
1769
0
        if (cmd_len > 4) {
1770
0
            sens->assert_enable &= ~cmd[4];
1771
0
        }
1772
0
        if (cmd_len > 5) {
1773
0
            sens->assert_enable &= ~(cmd[5] << 8);
1774
0
        }
1775
0
        if (cmd_len > 6) {
1776
0
            sens->deassert_enable &= ~cmd[6];
1777
0
        }
1778
0
        if (cmd_len > 7) {
1779
0
            sens->deassert_enable &= ~(cmd[7] << 8);
1780
0
        }
1781
0
        break;
1782
0
    case 3:
1783
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1784
0
        return;
1785
0
    }
1786
0
    IPMI_SENSOR_SET_RET_STATUS(sens, cmd[3]);
1787
0
}
1788
1789
static void get_sensor_evt_enable(IPMIBmcSim *ibs,
1790
                                  uint8_t *cmd, unsigned int cmd_len,
1791
                                  RspBuffer *rsp)
1792
0
{
1793
0
    IPMISensor *sens;
1794
1795
0
    if ((cmd[2] >= MAX_SENSORS) ||
1796
0
        !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1797
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1798
0
        return;
1799
0
    }
1800
0
    sens = ibs->sensors + cmd[2];
1801
0
    rsp_buffer_push(rsp, IPMI_SENSOR_GET_RET_STATUS(sens));
1802
0
    rsp_buffer_push(rsp, sens->assert_enable & 0xff);
1803
0
    rsp_buffer_push(rsp, (sens->assert_enable >> 8) & 0xff);
1804
0
    rsp_buffer_push(rsp, sens->deassert_enable & 0xff);
1805
0
    rsp_buffer_push(rsp, (sens->deassert_enable >> 8) & 0xff);
1806
0
}
1807
1808
static void rearm_sensor_evts(IPMIBmcSim *ibs,
1809
                              uint8_t *cmd, unsigned int cmd_len,
1810
                              RspBuffer *rsp)
1811
0
{
1812
0
    IPMISensor *sens;
1813
1814
0
    if ((cmd[2] >= MAX_SENSORS) ||
1815
0
        !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1816
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1817
0
        return;
1818
0
    }
1819
0
    sens = ibs->sensors + cmd[2];
1820
1821
0
    if ((cmd[3] & 0x80) == 0) {
1822
        /* Just clear everything */
1823
0
        sens->states = 0;
1824
0
        return;
1825
0
    }
1826
0
}
1827
1828
static void get_sensor_evt_status(IPMIBmcSim *ibs,
1829
                                  uint8_t *cmd, unsigned int cmd_len,
1830
                                  RspBuffer *rsp)
1831
0
{
1832
0
    IPMISensor *sens;
1833
1834
0
    if ((cmd[2] >= MAX_SENSORS) ||
1835
0
        !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1836
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1837
0
        return;
1838
0
    }
1839
0
    sens = ibs->sensors + cmd[2];
1840
0
    rsp_buffer_push(rsp, sens->reading);
1841
0
    rsp_buffer_push(rsp, IPMI_SENSOR_GET_RET_STATUS(sens));
1842
0
    rsp_buffer_push(rsp, sens->assert_states & 0xff);
1843
0
    rsp_buffer_push(rsp, (sens->assert_states >> 8) & 0xff);
1844
0
    rsp_buffer_push(rsp, sens->deassert_states & 0xff);
1845
0
    rsp_buffer_push(rsp, (sens->deassert_states >> 8) & 0xff);
1846
0
}
1847
1848
static void get_sensor_reading(IPMIBmcSim *ibs,
1849
                               uint8_t *cmd, unsigned int cmd_len,
1850
                               RspBuffer *rsp)
1851
0
{
1852
0
    IPMISensor *sens;
1853
1854
0
    if ((cmd[2] >= MAX_SENSORS) ||
1855
0
            !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1856
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1857
0
        return;
1858
0
    }
1859
0
    sens = ibs->sensors + cmd[2];
1860
0
    rsp_buffer_push(rsp, sens->reading);
1861
0
    rsp_buffer_push(rsp, IPMI_SENSOR_GET_RET_STATUS(sens));
1862
0
    rsp_buffer_push(rsp, sens->states & 0xff);
1863
0
    if (IPMI_SENSOR_IS_DISCRETE(sens)) {
1864
0
        rsp_buffer_push(rsp, (sens->states >> 8) & 0xff);
1865
0
    }
1866
0
}
1867
1868
static void set_sensor_type(IPMIBmcSim *ibs,
1869
                            uint8_t *cmd, unsigned int cmd_len,
1870
                            RspBuffer *rsp)
1871
0
{
1872
0
    IPMISensor *sens;
1873
1874
1875
0
    if ((cmd[2] >= MAX_SENSORS) ||
1876
0
            !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1877
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1878
0
        return;
1879
0
    }
1880
0
    sens = ibs->sensors + cmd[2];
1881
0
    sens->sensor_type = cmd[3];
1882
0
    sens->evt_reading_type_code = cmd[4] & 0x7f;
1883
0
}
1884
1885
static void get_sensor_type(IPMIBmcSim *ibs,
1886
                            uint8_t *cmd, unsigned int cmd_len,
1887
                            RspBuffer *rsp)
1888
0
{
1889
0
    IPMISensor *sens;
1890
1891
1892
0
    if ((cmd[2] >= MAX_SENSORS) ||
1893
0
            !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1894
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1895
0
        return;
1896
0
    }
1897
0
    sens = ibs->sensors + cmd[2];
1898
0
    rsp_buffer_push(rsp, sens->sensor_type);
1899
0
    rsp_buffer_push(rsp, sens->evt_reading_type_code);
1900
0
}
1901
1902
/*
1903
 * bytes   parameter
1904
 *    1    sensor number
1905
 *    2    operation (see below for bits meaning)
1906
 *    3    sensor reading
1907
 *  4:5    assertion states (optional)
1908
 *  6:7    deassertion states (optional)
1909
 *  8:10   event data 1,2,3 (optional)
1910
 */
1911
static void set_sensor_reading(IPMIBmcSim *ibs,
1912
                               uint8_t *cmd, unsigned int cmd_len,
1913
                               RspBuffer *rsp)
1914
0
{
1915
0
    IPMISensor *sens;
1916
0
    uint8_t evd1 = 0;
1917
0
    uint8_t evd2 = 0;
1918
0
    uint8_t evd3 = 0;
1919
0
    uint8_t new_reading = 0;
1920
0
    uint16_t new_assert_states = 0;
1921
0
    uint16_t new_deassert_states = 0;
1922
0
    bool change_reading = false;
1923
0
    bool change_assert = false;
1924
0
    bool change_deassert = false;
1925
0
    enum {
1926
0
        SENSOR_GEN_EVENT_NONE,
1927
0
        SENSOR_GEN_EVENT_DATA,
1928
0
        SENSOR_GEN_EVENT_BMC,
1929
0
    } do_gen_event = SENSOR_GEN_EVENT_NONE;
1930
1931
0
    if ((cmd[2] >= MAX_SENSORS) ||
1932
0
            !IPMI_SENSOR_GET_PRESENT(ibs->sensors + cmd[2])) {
1933
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQ_ENTRY_NOT_PRESENT);
1934
0
        return;
1935
0
    }
1936
1937
0
    sens = ibs->sensors + cmd[2];
1938
1939
    /* [1:0] Sensor Reading operation */
1940
0
    switch ((cmd[3]) & 0x3) {
1941
0
    case 0: /* Do not change */
1942
0
        break;
1943
0
    case 1: /* write given value to sensor reading byte */
1944
0
        new_reading = cmd[4];
1945
0
        if (sens->reading != new_reading) {
1946
0
            change_reading = true;
1947
0
        }
1948
0
        break;
1949
0
    case 2:
1950
0
    case 3:
1951
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
1952
0
        return;
1953
0
    }
1954
1955
    /* [3:2] Deassertion bits operation */
1956
0
    switch ((cmd[3] >> 2) & 0x3) {
1957
0
    case 0: /* Do not change */
1958
0
        break;
1959
0
    case 1: /* write given value */
1960
0
        if (cmd_len > 7) {
1961
0
            new_deassert_states = cmd[7];
1962
0
            change_deassert = true;
1963
0
        }
1964
0
        if (cmd_len > 8) {
1965
0
            new_deassert_states |= (cmd[8] << 8);
1966
0
        }
1967
0
        break;
1968
1969
0
    case 2: /* mask on */
1970
0
        if (cmd_len > 7) {
1971
0
            new_deassert_states = (sens->deassert_states | cmd[7]);
1972
0
            change_deassert = true;
1973
0
        }
1974
0
        if (cmd_len > 8) {
1975
0
            new_deassert_states |= (sens->deassert_states | (cmd[8] << 8));
1976
0
        }
1977
0
        break;
1978
1979
0
    case 3: /* mask off */
1980
0
        if (cmd_len > 7) {
1981
0
            new_deassert_states = (sens->deassert_states & cmd[7]);
1982
0
            change_deassert = true;
1983
0
        }
1984
0
        if (cmd_len > 8) {
1985
0
            new_deassert_states |= (sens->deassert_states & (cmd[8] << 8));
1986
0
        }
1987
0
        break;
1988
0
    }
1989
1990
0
    if (change_deassert && (new_deassert_states == sens->deassert_states)) {
1991
0
        change_deassert = false;
1992
0
    }
1993
1994
    /* [5:4] Assertion bits operation */
1995
0
    switch ((cmd[3] >> 4) & 0x3) {
1996
0
    case 0: /* Do not change */
1997
0
        break;
1998
0
    case 1: /* write given value */
1999
0
        if (cmd_len > 5) {
2000
0
            new_assert_states = cmd[5];
2001
0
            change_assert = true;
2002
0
        }
2003
0
        if (cmd_len > 6) {
2004
0
            new_assert_states |= (cmd[6] << 8);
2005
0
        }
2006
0
        break;
2007
2008
0
    case 2: /* mask on */
2009
0
        if (cmd_len > 5) {
2010
0
            new_assert_states = (sens->assert_states | cmd[5]);
2011
0
            change_assert = true;
2012
0
        }
2013
0
        if (cmd_len > 6) {
2014
0
            new_assert_states |= (sens->assert_states | (cmd[6] << 8));
2015
0
        }
2016
0
        break;
2017
2018
0
    case 3: /* mask off */
2019
0
        if (cmd_len > 5) {
2020
0
            new_assert_states = (sens->assert_states & cmd[5]);
2021
0
            change_assert = true;
2022
0
        }
2023
0
        if (cmd_len > 6) {
2024
0
            new_assert_states |= (sens->assert_states & (cmd[6] << 8));
2025
0
        }
2026
0
        break;
2027
0
    }
2028
2029
0
    if (change_assert && (new_assert_states == sens->assert_states)) {
2030
0
        change_assert = false;
2031
0
    }
2032
2033
0
    if (cmd_len > 9) {
2034
0
        evd1 = cmd[9];
2035
0
    }
2036
0
    if (cmd_len > 10) {
2037
0
        evd2 = cmd[10];
2038
0
    }
2039
0
    if (cmd_len > 11) {
2040
0
        evd3 = cmd[11];
2041
0
    }
2042
2043
    /* [7:6] Event Data Bytes operation */
2044
0
    switch ((cmd[3] >> 6) & 0x3) {
2045
0
    case 0: /*
2046
             * Don’t use Event Data bytes from this command. BMC will
2047
             * generate it's own Event Data bytes based on its sensor
2048
             * implementation.
2049
             */
2050
0
        evd1 = evd2 = evd3 = 0x0;
2051
0
        do_gen_event = SENSOR_GEN_EVENT_BMC;
2052
0
        break;
2053
0
    case 1: /*
2054
             * Write given values to event data bytes including bits
2055
             * [3:0] Event Data 1.
2056
             */
2057
0
        do_gen_event = SENSOR_GEN_EVENT_DATA;
2058
0
        break;
2059
0
    case 2: /*
2060
             * Write given values to event data bytes excluding bits
2061
             * [3:0] Event Data 1.
2062
             */
2063
0
        evd1 &= 0xf0;
2064
0
        do_gen_event = SENSOR_GEN_EVENT_DATA;
2065
0
        break;
2066
0
    case 3:
2067
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
2068
0
        return;
2069
0
    }
2070
2071
    /*
2072
     * Event Data Bytes operation and parameter are inconsistent. The
2073
     * Specs are not clear on that topic but generating an error seems
2074
     * correct.
2075
     */
2076
0
    if (do_gen_event == SENSOR_GEN_EVENT_DATA && cmd_len < 10) {
2077
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
2078
0
        return;
2079
0
    }
2080
2081
    /* commit values */
2082
0
    if (change_reading) {
2083
0
        sens->reading = new_reading;
2084
0
    }
2085
2086
0
    if (change_assert) {
2087
0
        sens->assert_states = new_assert_states;
2088
0
    }
2089
2090
0
    if (change_deassert) {
2091
0
        sens->deassert_states = new_deassert_states;
2092
0
    }
2093
2094
    /* TODO: handle threshold sensor */
2095
0
    if (!IPMI_SENSOR_IS_DISCRETE(sens)) {
2096
0
        return;
2097
0
    }
2098
2099
0
    switch (do_gen_event) {
2100
0
    case SENSOR_GEN_EVENT_DATA: {
2101
0
        unsigned int bit = evd1 & 0xf;
2102
0
        uint16_t mask = (1 << bit);
2103
2104
0
        if (sens->assert_states & mask & sens->assert_enable) {
2105
0
            gen_event(ibs, cmd[2], 0, evd1, evd2, evd3);
2106
0
        }
2107
2108
0
        if (sens->deassert_states & mask & sens->deassert_enable) {
2109
0
            gen_event(ibs, cmd[2], 1, evd1, evd2, evd3);
2110
0
        }
2111
0
        break;
2112
0
    }
2113
0
    case SENSOR_GEN_EVENT_BMC:
2114
        /*
2115
         * TODO: generate event and event data bytes depending on the
2116
         * sensor
2117
         */
2118
0
        break;
2119
0
    case SENSOR_GEN_EVENT_NONE:
2120
0
        break;
2121
0
    }
2122
0
}
2123
2124
static inline bool is_ipv4_netmask_valid(const void *buf)
2125
0
{
2126
0
    uint32_t netmask = ldl_be_p(buf);
2127
2128
0
    return netmask != 0 && clo32(netmask) + ctz32(netmask) == 32;
2129
0
}
2130
2131
/*
2132
 * Request data (from cmd[2] on):
2133
 * bytes   meaning
2134
 *    1    [bits 3:0] channel number
2135
 *    2    parameter selector
2136
 * [3:N]   configuration parameter data (from cmd[4] on)
2137
 */
2138
static void set_lan_config(IPMIBmcSim *ibs,
2139
                           uint8_t *cmd, unsigned int cmd_len,
2140
                           RspBuffer *rsp)
2141
0
{
2142
0
    uint8_t channel;
2143
0
    uint8_t *param;  /* pointer to configuration parameter data */
2144
0
    unsigned int param_len;
2145
2146
0
    if (ibs->lan.channel == 0) {
2147
        /* LAN channel disabled. Fail as if this command were not defined. */
2148
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_CMD);
2149
0
        return;
2150
0
    }
2151
0
    if (cmd_len < 5) {
2152
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2153
0
        return;
2154
0
    }
2155
0
    channel = cmd[2] & 0xf;
2156
0
    param = cmd + 4;
2157
0
    param_len = cmd_len - 4;
2158
2159
0
    if (!IPMI_BMC_CHANNEL_IS_LAN(ibs, channel)) {
2160
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
2161
0
        return;
2162
0
    }
2163
2164
0
    switch (cmd[3]) {
2165
0
    case IPMI_BMC_LAN_CFG_PARAM_IP_ADDR:
2166
0
        if (param_len < NBYTES_IP) {
2167
0
            rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2168
0
            return;
2169
0
        }
2170
0
        memcpy(ibs->lan.ipaddr, param, NBYTES_IP);
2171
0
        break;
2172
2173
0
    case IPMI_BMC_LAN_CFG_PARAM_IP_ADDR_SOURCE:
2174
0
        if (param_len < 1) {
2175
0
            rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2176
0
            return;
2177
0
        }
2178
0
        if (!IPMI_BMC_LAN_CFG_IS_VALID_IP_SOURCE(*param)) {
2179
0
            rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
2180
0
            return;
2181
0
        }
2182
0
        ibs->lan.ipsrc = *param;
2183
0
        break;
2184
2185
0
    case IPMI_BMC_LAN_CFG_PARAM_MAC_ADDR:
2186
0
        if (param_len < NBYTES_MAC) {
2187
0
            rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2188
0
            return;
2189
0
        }
2190
0
        memcpy(ibs->lan.macaddr.a, param, NBYTES_MAC);
2191
0
        break;
2192
2193
0
    case IPMI_BMC_LAN_CFG_PARAM_SUBNET_MASK:
2194
0
        if (param_len < NBYTES_IP) {
2195
0
            rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2196
0
            return;
2197
0
        }
2198
0
        if (!is_ipv4_netmask_valid(param)) {
2199
0
            rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
2200
0
            return;
2201
0
        }
2202
0
        memcpy(ibs->lan.netmask, param, NBYTES_IP);
2203
0
        break;
2204
2205
0
    case IPMI_BMC_LAN_CFG_PARAM_DEFAULT_GW_IP_ADDR:
2206
0
        if (param_len < NBYTES_IP) {
2207
0
            rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2208
0
            return;
2209
0
        }
2210
0
        memcpy(ibs->lan.defgw_ipaddr, param, NBYTES_IP);
2211
0
        break;
2212
2213
0
    case IPMI_BMC_LAN_CFG_PARAM_DEFAULT_GW_MAC_ADDR:
2214
0
        if (param_len < NBYTES_MAC) {
2215
0
            rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2216
0
            return;
2217
0
        }
2218
0
        memcpy(ibs->lan.defgw_macaddr.a, param, NBYTES_MAC);
2219
0
        break;
2220
2221
0
    case IPMI_BMC_LAN_CFG_PARAM_SET_IN_PROGRESS:
2222
0
    case IPMI_BMC_LAN_CFG_PARAM_AUTH_TYPE_SUPPORT:
2223
0
    case IPMI_BMC_LAN_CFG_PARAM_AUTH_TYPE_ENABLES:
2224
0
    case IPMI_BMC_LAN_CFG_PARAM_IPV4_HDR_PARAMS:
2225
0
    case IPMI_BMC_LAN_CFG_PARAM_BACKUP_GW_ADDR:
2226
0
    case IPMI_BMC_LAN_CFG_PARAM_BACKUP_GW_MAC_ADDR:
2227
0
    case IPMI_BMC_LAN_CFG_PARAM_COMMUNITY_STRING:
2228
0
    case IPMI_BMC_LAN_CFG_PARAM_NUM_DESTINATIONS:
2229
0
        rsp_buffer_set_error(rsp, IPMI_BMC_LAN_CFG_CC_PARAM_READONLY);
2230
0
        return;
2231
2232
0
    default:
2233
0
        rsp_buffer_set_error(rsp, IPMI_BMC_LAN_CFG_CC_PARAM_NOT_SUPPORTED);
2234
0
        return;
2235
0
    }
2236
0
}
2237
2238
/*
2239
 * Request data (from cmd[2] to cmd[5] inclusive):
2240
 * bytes   meaning
2241
 *    1    [bit 7] revision only flag, [bits 3:0] channel number
2242
 *    2    parameter selector
2243
 *    3    set selector
2244
 *    4    block selector
2245
 */
2246
static void get_lan_config(IPMIBmcSim *ibs,
2247
                           uint8_t *cmd, unsigned int cmd_len,
2248
                           RspBuffer *rsp)
2249
0
{
2250
0
    uint8_t channel;
2251
2252
0
    if (ibs->lan.channel == 0) {
2253
        /* LAN channel disabled. Fail as if this command were not defined. */
2254
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_CMD);
2255
0
        return;
2256
0
    }
2257
0
    if (cmd_len < 6) {
2258
0
        rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_LENGTH_INVALID);
2259
0
        return;
2260
0
    }
2261
0
    channel = cmd[2] & 0xf;
2262
2263
0
    rsp_buffer_push(rsp, IPMI_BMC_LAN_CFG_PARAMETER_REVISION);
2264
0
    if (cmd[2] & 0x80) {
2265
        /* The requester only requests parameter revision, not the parameter */
2266
0
        return;
2267
0
    }
2268
0
    if (!IPMI_BMC_CHANNEL_IS_LAN(ibs, channel)) {
2269
0
        rsp_buffer_set_error(rsp, IPMI_CC_INVALID_DATA_FIELD);
2270
0
        return;
2271
0
    }
2272
2273
0
    switch (cmd[3]) {
2274
0
    case IPMI_BMC_LAN_CFG_PARAM_SET_IN_PROGRESS:
2275
0
        rsp_buffer_push(rsp, 0x0);  /* set complete */
2276
0
        break;
2277
2278
0
    case IPMI_BMC_LAN_CFG_PARAM_AUTH_TYPE_SUPPORT:
2279
0
        rsp_buffer_push(rsp, 0x01); /* Authentication type "none" supported */
2280
0
        break;
2281
2282
0
    case IPMI_BMC_LAN_CFG_PARAM_AUTH_TYPE_ENABLES:
2283
        /* Only authentication type "none" enabled */
2284
0
        rsp_buffer_push(rsp, 0x01); /* for privilege level "Callback" */
2285
0
        rsp_buffer_push(rsp, 0x01); /* for privilege level "User" */
2286
0
        rsp_buffer_push(rsp, 0x01); /* for privilege level "Operator" */
2287
0
        rsp_buffer_push(rsp, 0x01); /* for privilege level "Administrator" */
2288
0
        rsp_buffer_push(rsp, 0x01); /* for privilege level "OEM" */
2289
0
        break;
2290
2291
0
    case IPMI_BMC_LAN_CFG_PARAM_IP_ADDR:
2292
0
        rsp_buffer_pushmore(rsp, ibs->lan.ipaddr, NBYTES_IP);
2293
0
        break;
2294
2295
0
    case IPMI_BMC_LAN_CFG_PARAM_IP_ADDR_SOURCE:
2296
0
        rsp_buffer_push(rsp, ibs->lan.ipsrc);
2297
0
        break;
2298
2299
0
    case IPMI_BMC_LAN_CFG_PARAM_MAC_ADDR:
2300
0
        rsp_buffer_pushmore(rsp, ibs->lan.macaddr.a, NBYTES_MAC);
2301
0
        break;
2302
2303
0
    case IPMI_BMC_LAN_CFG_PARAM_SUBNET_MASK:
2304
0
        rsp_buffer_pushmore(rsp, ibs->lan.netmask, NBYTES_IP);
2305
0
        break;
2306
2307
0
    case IPMI_BMC_LAN_CFG_PARAM_IPV4_HDR_PARAMS:
2308
        /* TTL 0x40 */
2309
0
        rsp_buffer_push(rsp, 0x40);
2310
        /* don't fragment */
2311
0
        rsp_buffer_push(rsp, 0x40);
2312
        /* precedence 0x0, minimize delay */
2313
0
        rsp_buffer_push(rsp, 0x10);
2314
0
        break;
2315
2316
0
    case IPMI_BMC_LAN_CFG_PARAM_DEFAULT_GW_IP_ADDR:
2317
0
        rsp_buffer_pushmore(rsp, ibs->lan.defgw_ipaddr, NBYTES_IP);
2318
0
        break;
2319
2320
0
    case IPMI_BMC_LAN_CFG_PARAM_DEFAULT_GW_MAC_ADDR:
2321
0
        rsp_buffer_pushmore(rsp, ibs->lan.defgw_macaddr.a, NBYTES_MAC);
2322
0
        break;
2323
2324
0
    case IPMI_BMC_LAN_CFG_PARAM_BACKUP_GW_ADDR:
2325
        /* 0.0.0.0 */
2326
0
        rsp_buffer_push(rsp, 0x00);
2327
0
        rsp_buffer_push(rsp, 0x00);
2328
0
        rsp_buffer_push(rsp, 0x00);
2329
0
        rsp_buffer_push(rsp, 0x00);
2330
0
        break;
2331
2332
0
    case IPMI_BMC_LAN_CFG_PARAM_BACKUP_GW_MAC_ADDR:
2333
        /* 00:00:00:00:00:00 */
2334
0
        rsp_buffer_push(rsp, 0x00);
2335
0
        rsp_buffer_push(rsp, 0x00);
2336
0
        rsp_buffer_push(rsp, 0x00);
2337
0
        rsp_buffer_push(rsp, 0x00);
2338
0
        rsp_buffer_push(rsp, 0x00);
2339
0
        rsp_buffer_push(rsp, 0x00);
2340
0
        break;
2341
2342
0
    case IPMI_BMC_LAN_CFG_PARAM_COMMUNITY_STRING:
2343
0
        {
2344
0
            static uint8_t community_str[18] = "public";
2345
2346
0
            rsp_buffer_pushmore(rsp, community_str, sizeof(community_str));
2347
0
        }
2348
0
        break;
2349
2350
0
    case IPMI_BMC_LAN_CFG_PARAM_NUM_DESTINATIONS:
2351
0
        rsp_buffer_push(rsp, 0x00); /* LAN Alerting not supported */
2352
0
        break;
2353
2354
0
    default:
2355
0
        rsp_buffer_set_error(rsp, IPMI_BMC_LAN_CFG_CC_PARAM_NOT_SUPPORTED);
2356
0
        return;
2357
0
    };
2358
0
}
2359
2360
static const IPMICmdHandler chassis_cmds[] = {
2361
    [IPMI_CMD_GET_CHASSIS_CAPABILITIES] = { chassis_capabilities },
2362
    [IPMI_CMD_GET_CHASSIS_STATUS] = { chassis_status },
2363
    [IPMI_CMD_CHASSIS_CONTROL] = { chassis_control, 3 },
2364
    [IPMI_CMD_GET_SYS_RESTART_CAUSE] = { chassis_get_sys_restart_cause }
2365
};
2366
static const IPMINetfn chassis_netfn = {
2367
    .cmd_nums = ARRAY_SIZE(chassis_cmds),
2368
    .cmd_handlers = chassis_cmds
2369
};
2370
2371
static const IPMICmdHandler sensor_event_cmds[] = {
2372
    [IPMI_CMD_PLATFORM_EVENT_MSG] = { platform_event_msg, 10 },
2373
    [IPMI_CMD_SET_SENSOR_EVT_ENABLE] = { set_sensor_evt_enable, 4 },
2374
    [IPMI_CMD_GET_SENSOR_EVT_ENABLE] = { get_sensor_evt_enable, 3 },
2375
    [IPMI_CMD_REARM_SENSOR_EVTS] = { rearm_sensor_evts, 4 },
2376
    [IPMI_CMD_GET_SENSOR_EVT_STATUS] = { get_sensor_evt_status, 3 },
2377
    [IPMI_CMD_GET_SENSOR_READING] = { get_sensor_reading, 3 },
2378
    [IPMI_CMD_SET_SENSOR_TYPE] = { set_sensor_type, 5 },
2379
    [IPMI_CMD_GET_SENSOR_TYPE] = { get_sensor_type, 3 },
2380
    [IPMI_CMD_SET_SENSOR_READING] = { set_sensor_reading, 5 },
2381
};
2382
static const IPMINetfn sensor_event_netfn = {
2383
    .cmd_nums = ARRAY_SIZE(sensor_event_cmds),
2384
    .cmd_handlers = sensor_event_cmds
2385
};
2386
2387
static const IPMICmdHandler app_cmds[] = {
2388
    [IPMI_CMD_GET_DEVICE_ID] = { get_device_id },
2389
    [IPMI_CMD_COLD_RESET] = { cold_reset },
2390
    [IPMI_CMD_WARM_RESET] = { warm_reset },
2391
    [IPMI_CMD_SET_ACPI_POWER_STATE] = { set_acpi_power_state, 4 },
2392
    [IPMI_CMD_GET_ACPI_POWER_STATE] = { get_acpi_power_state },
2393
    [IPMI_CMD_GET_DEVICE_GUID] = { get_device_guid },
2394
    [IPMI_CMD_SET_BMC_GLOBAL_ENABLES] = { set_bmc_global_enables, 3 },
2395
    [IPMI_CMD_GET_BMC_GLOBAL_ENABLES] = { get_bmc_global_enables },
2396
    [IPMI_CMD_CLR_MSG_FLAGS] = { clr_msg_flags, 3 },
2397
    [IPMI_CMD_GET_MSG_FLAGS] = { get_msg_flags },
2398
    [IPMI_CMD_GET_MSG] = { get_msg },
2399
    [IPMI_CMD_SEND_MSG] = { send_msg, 3 },
2400
    [IPMI_CMD_READ_EVT_MSG_BUF] = { read_evt_msg_buf },
2401
    [IPMI_CMD_RESET_WATCHDOG_TIMER] = { reset_watchdog_timer },
2402
    [IPMI_CMD_SET_WATCHDOG_TIMER] = { set_watchdog_timer, 8 },
2403
    [IPMI_CMD_GET_WATCHDOG_TIMER] = { get_watchdog_timer },
2404
    [IPMI_CMD_GET_CHANNEL_ACCESS] = { get_channel_access, 4 },
2405
    [IPMI_CMD_GET_CHANNEL_INFO] = { get_channel_info, 3 },
2406
};
2407
static const IPMINetfn app_netfn = {
2408
    .cmd_nums = ARRAY_SIZE(app_cmds),
2409
    .cmd_handlers = app_cmds
2410
};
2411
2412
static const IPMICmdHandler storage_cmds[] = {
2413
    [IPMI_CMD_GET_FRU_AREA_INFO] = { get_fru_area_info, 3 },
2414
    [IPMI_CMD_READ_FRU_DATA] = { read_fru_data, 5 },
2415
    [IPMI_CMD_WRITE_FRU_DATA] = { write_fru_data, 5 },
2416
    [IPMI_CMD_GET_SDR_REP_INFO] = { get_sdr_rep_info },
2417
    [IPMI_CMD_RESERVE_SDR_REP] = { reserve_sdr_rep },
2418
    [IPMI_CMD_GET_SDR] = { get_sdr, 8 },
2419
    [IPMI_CMD_ADD_SDR] = { add_sdr },
2420
    [IPMI_CMD_CLEAR_SDR_REP] = { clear_sdr_rep, 8 },
2421
    [IPMI_CMD_GET_SEL_INFO] = { get_sel_info },
2422
    [IPMI_CMD_RESERVE_SEL] = { reserve_sel },
2423
    [IPMI_CMD_GET_SEL_ENTRY] = { get_sel_entry, 8 },
2424
    [IPMI_CMD_ADD_SEL_ENTRY] = { add_sel_entry, 18 },
2425
    [IPMI_CMD_CLEAR_SEL] = { clear_sel, 8 },
2426
    [IPMI_CMD_GET_SEL_TIME] = { get_sel_time },
2427
    [IPMI_CMD_SET_SEL_TIME] = { set_sel_time, 6 },
2428
};
2429
2430
static const IPMINetfn storage_netfn = {
2431
    .cmd_nums = ARRAY_SIZE(storage_cmds),
2432
    .cmd_handlers = storage_cmds
2433
};
2434
2435
static const IPMICmdHandler transport_cmds[] = {
2436
    [IPMI_CMD_SET_LAN_CONFIG] = { set_lan_config },
2437
    [IPMI_CMD_GET_LAN_CONFIG] = { get_lan_config },
2438
};
2439
static const IPMINetfn transport_netfn = {
2440
    .cmd_nums = ARRAY_SIZE(transport_cmds),
2441
    .cmd_handlers = transport_cmds
2442
};
2443
2444
2445
static void register_cmds(IPMIBmcSim *s)
2446
0
{
2447
0
    ipmi_sim_register_netfn(s, IPMI_NETFN_CHASSIS, &chassis_netfn);
2448
0
    ipmi_sim_register_netfn(s, IPMI_NETFN_SENSOR_EVENT, &sensor_event_netfn);
2449
0
    ipmi_sim_register_netfn(s, IPMI_NETFN_APP, &app_netfn);
2450
0
    ipmi_sim_register_netfn(s, IPMI_NETFN_STORAGE, &storage_netfn);
2451
0
    ipmi_sim_register_netfn(s, IPMI_NETFN_TRANSPORT, &transport_netfn);
2452
0
}
2453
2454
static uint8_t init_sdrs[] = {
2455
    /* Watchdog device */
2456
    0x00, 0x00, 0x51, 0x02,   35, 0x20, 0x00, 0x00,
2457
    0x23, 0x01, 0x63, 0x00, 0x23, 0x6f, 0x0f, 0x01,
2458
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2459
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc8,
2460
    'W',  'a',  't',  'c',  'h',  'd',  'o',  'g',
2461
};
2462
2463
static void ipmi_sdr_init(IPMIBmcSim *ibs)
2464
0
{
2465
0
    unsigned int i;
2466
0
    int len;
2467
0
    size_t sdrs_size;
2468
0
    uint8_t *sdrs;
2469
2470
0
    sdrs_size = sizeof(init_sdrs);
2471
0
    sdrs = init_sdrs;
2472
0
    if (ibs->sdr_filename &&
2473
0
        !g_file_get_contents(ibs->sdr_filename, (gchar **) &sdrs, &sdrs_size,
2474
0
                             NULL)) {
2475
0
        error_report("failed to load sdr file '%s'", ibs->sdr_filename);
2476
0
        sdrs_size = sizeof(init_sdrs);
2477
0
        sdrs = init_sdrs;
2478
0
    }
2479
2480
0
    for (i = 0; i < sdrs_size; i += len) {
2481
0
        struct ipmi_sdr_header *sdrh;
2482
2483
0
        if (i + IPMI_SDR_HEADER_SIZE > sdrs_size) {
2484
0
            error_report("Problem with recid 0x%4.4x", i);
2485
0
            break;
2486
0
        }
2487
0
        sdrh = (struct ipmi_sdr_header *) &sdrs[i];
2488
0
        len = ipmi_sdr_length(sdrh);
2489
0
        if (i + len > sdrs_size) {
2490
0
            error_report("Problem with recid 0x%4.4x", i);
2491
0
            break;
2492
0
        }
2493
0
        sdr_add_entry(ibs, sdrh, len, NULL);
2494
0
    }
2495
2496
0
    if (sdrs != init_sdrs) {
2497
0
        g_free(sdrs);
2498
0
    }
2499
0
}
2500
2501
static const VMStateDescription vmstate_ipmi_sim_lan = {
2502
    .name = TYPE_IPMI_BMC_SIMULATOR "-lan",
2503
    .version_id = 1,
2504
    .minimum_version_id = 1,
2505
    .fields = (const VMStateField[]) {
2506
        VMSTATE_UINT8(channel, IPMILan),
2507
        VMSTATE_UINT8_ARRAY(ipaddr, IPMILan, NBYTES_IP),
2508
        VMSTATE_UINT8(ipsrc, IPMILan),
2509
        VMSTATE_UINT8_ARRAY(macaddr.a, IPMILan, NBYTES_MAC),
2510
        VMSTATE_UINT8_ARRAY(netmask, IPMILan, NBYTES_IP),
2511
        VMSTATE_UINT8_ARRAY(defgw_ipaddr, IPMILan, NBYTES_IP),
2512
        VMSTATE_UINT8_ARRAY(defgw_macaddr.a, IPMILan, NBYTES_MAC),
2513
        VMSTATE_END_OF_LIST()
2514
    }
2515
};
2516
2517
static const VMStateDescription vmstate_ipmi_sim = {
2518
    .name = TYPE_IPMI_BMC_SIMULATOR,
2519
    .version_id = 2,
2520
    .minimum_version_id = 1,
2521
    .fields = (const VMStateField[]) {
2522
        VMSTATE_UINT8(bmc_global_enables, IPMIBmcSim),
2523
        VMSTATE_UINT8(msg_flags, IPMIBmcSim),
2524
        VMSTATE_BOOL(watchdog_initialized, IPMIBmcSim),
2525
        VMSTATE_UINT8(watchdog_use, IPMIBmcSim),
2526
        VMSTATE_UINT8(watchdog_action, IPMIBmcSim),
2527
        VMSTATE_UINT8(watchdog_pretimeout, IPMIBmcSim),
2528
        VMSTATE_UINT8(watchdog_expired, IPMIBmcSim),
2529
        VMSTATE_UINT16(watchdog_timeout, IPMIBmcSim),
2530
        VMSTATE_BOOL(watchdog_running, IPMIBmcSim),
2531
        VMSTATE_BOOL(watchdog_preaction_ran, IPMIBmcSim),
2532
        VMSTATE_INT64(watchdog_expiry, IPMIBmcSim),
2533
        VMSTATE_UINT8_ARRAY(evtbuf, IPMIBmcSim, 16),
2534
        VMSTATE_UINT8(sensors[IPMI_WATCHDOG_SENSOR].status, IPMIBmcSim),
2535
        VMSTATE_UINT8(sensors[IPMI_WATCHDOG_SENSOR].reading, IPMIBmcSim),
2536
        VMSTATE_UINT16(sensors[IPMI_WATCHDOG_SENSOR].states, IPMIBmcSim),
2537
        VMSTATE_UINT16(sensors[IPMI_WATCHDOG_SENSOR].assert_states, IPMIBmcSim),
2538
        VMSTATE_UINT16(sensors[IPMI_WATCHDOG_SENSOR].deassert_states,
2539
                       IPMIBmcSim),
2540
        VMSTATE_UINT16(sensors[IPMI_WATCHDOG_SENSOR].assert_enable, IPMIBmcSim),
2541
        VMSTATE_STRUCT(lan, IPMIBmcSim, 2, vmstate_ipmi_sim_lan, IPMILan),
2542
        VMSTATE_END_OF_LIST()
2543
    }
2544
};
2545
2546
static void ipmi_fru_init(IPMIFru *fru)
2547
0
{
2548
0
    int fsize;
2549
0
    int size = 0;
2550
2551
0
    if (!fru->filename) {
2552
0
        goto out;
2553
0
    }
2554
2555
0
    fsize = get_image_size(fru->filename, NULL);
2556
0
    if (fsize > 0) {
2557
0
        size = QEMU_ALIGN_UP(fsize, fru->areasize);
2558
0
        fru->data = g_malloc0(size);
2559
0
        if (load_image_size(fru->filename, fru->data, fsize) != fsize) {
2560
0
            error_report("Could not load file '%s'", fru->filename);
2561
0
            g_free(fru->data);
2562
0
            fru->data = NULL;
2563
0
        }
2564
0
    } else {
2565
0
        error_report("Could not get file size '%s'", fru->filename);
2566
0
    }
2567
2568
0
out:
2569
0
    if (!fru->data) {
2570
        /* give one default FRU */
2571
0
        size = fru->areasize;
2572
0
        fru->data = g_malloc0(size);
2573
0
    }
2574
2575
0
    fru->nentries = size / fru->areasize;
2576
0
}
2577
2578
static void ipmi_lan_init(IPMILan *lan, Error **errp)
2579
0
{
2580
0
    struct in_addr ip;
2581
2582
    /*
2583
     * `lan->channel` can be either 0 (meaning LAN channel disabled) or
2584
     * a valid IPMI implementation-specific channel.
2585
     */
2586
0
    if (lan->channel != 0 &&
2587
0
        !IPMI_CHANNEL_IS_IMPLEMENTATION_SPECIFIC(lan->channel)) {
2588
0
        error_setg(errp, "invalid LAN channel %d", lan->channel);
2589
0
        return;
2590
0
    }
2591
0
    if (lan->arg_ipaddr) {
2592
0
        if (inet_pton(AF_INET, lan->arg_ipaddr, &ip) != 1) {
2593
0
            error_setg(errp, "invalid ip address '%s'", lan->arg_ipaddr);
2594
0
            return;
2595
0
        }
2596
0
        memcpy(lan->ipaddr, &ip.s_addr, NBYTES_IP);
2597
0
    }
2598
0
    if (!IPMI_BMC_LAN_CFG_IS_VALID_IP_SOURCE(lan->ipsrc)) {
2599
0
        error_setg(errp, "invalid ip source %d", lan->ipsrc);
2600
0
        return;
2601
0
    }
2602
0
    if (lan->arg_netmask) {
2603
0
        if (inet_pton(AF_INET, lan->arg_netmask, &ip) != 1 ||
2604
0
            !is_ipv4_netmask_valid(&ip.s_addr)) {
2605
0
            error_setg(errp, "invalid subnet mask '%s'", lan->arg_netmask);
2606
0
            return;
2607
0
        }
2608
0
        memcpy(lan->netmask, &ip.s_addr, NBYTES_IP);
2609
0
    }
2610
0
    if (lan->arg_defgw_ipaddr) {
2611
0
        if (inet_pton(AF_INET, lan->arg_defgw_ipaddr, &ip) != 1) {
2612
0
            error_setg(errp, "invalid ip address '%s'", lan->arg_defgw_ipaddr);
2613
0
            return;
2614
0
        }
2615
0
        memcpy(lan->defgw_ipaddr, &ip.s_addr, NBYTES_IP);
2616
0
    }
2617
0
}
2618
2619
static void ipmi_sim_realize(DeviceState *dev, Error **errp)
2620
0
{
2621
0
    IPMIBmc *b = IPMI_BMC(dev);
2622
0
    unsigned int i;
2623
0
    IPMIBmcSim *ibs = IPMI_BMC_SIMULATOR(b);
2624
2625
0
    QTAILQ_INIT(&ibs->rcvbufs);
2626
2627
0
    ibs->bmc_global_enables = (1 << IPMI_BMC_EVENT_LOG_BIT);
2628
0
    ibs->device_id = 0x20;
2629
0
    ibs->ipmi_version = 0x02; /* IPMI 2.0 */
2630
0
    ibs->restart_cause = 0;
2631
0
    for (i = 0; i < 4; i++) {
2632
0
        ibs->sel.last_addition[i] = 0xff;
2633
0
        ibs->sel.last_clear[i] = 0xff;
2634
0
        ibs->sdr.last_addition[i] = 0xff;
2635
0
        ibs->sdr.last_clear[i] = 0xff;
2636
0
    }
2637
2638
0
    ipmi_sdr_init(ibs);
2639
2640
0
    ipmi_fru_init(&ibs->fru);
2641
2642
0
    ibs->acpi_power_state[0] = 0;
2643
0
    ibs->acpi_power_state[1] = 0;
2644
2645
0
    ipmi_init_sensors_from_sdrs(ibs);
2646
2647
0
    ipmi_lan_init(&ibs->lan, errp);
2648
2649
0
    register_cmds(ibs);
2650
2651
0
    ibs->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ipmi_timeout, ibs);
2652
0
}
2653
2654
static const Property ipmi_sim_properties[] = {
2655
    DEFINE_PROP_UINT16("fruareasize", IPMIBmcSim, fru.areasize, 1024),
2656
    DEFINE_PROP_STRING("frudatafile", IPMIBmcSim, fru.filename),
2657
    DEFINE_PROP_STRING("sdrfile", IPMIBmcSim, sdr_filename),
2658
    DEFINE_PROP_UINT8("device_id", IPMIBmcSim, device_id, 0x20),
2659
    DEFINE_PROP_UINT8("ipmi_version", IPMIBmcSim, ipmi_version, 0x02),
2660
    DEFINE_PROP_UINT8("device_rev", IPMIBmcSim, device_rev, 0),
2661
    DEFINE_PROP_UINT8("fwrev1", IPMIBmcSim, fwrev1, 0),
2662
    DEFINE_PROP_UINT8("fwrev2", IPMIBmcSim, fwrev2, 0),
2663
    DEFINE_PROP_UINT32("mfg_id", IPMIBmcSim, mfg_id, 0),
2664
    DEFINE_PROP_UINT16("product_id", IPMIBmcSim, product_id, 0),
2665
    DEFINE_PROP_UUID_NODEFAULT("guid", IPMIBmcSim, uuid),
2666
    DEFINE_PROP_UINT8("lan.channel", IPMIBmcSim, lan.channel, 0),
2667
    DEFINE_PROP_STRING("lan.ipaddr", IPMIBmcSim, lan.arg_ipaddr),
2668
    DEFINE_PROP_UINT8("lan.ipsrc", IPMIBmcSim, lan.ipsrc, 0),
2669
    DEFINE_PROP_MACADDR("lan.macaddr", IPMIBmcSim, lan.macaddr),
2670
    DEFINE_PROP_STRING("lan.netmask", IPMIBmcSim, lan.arg_netmask),
2671
    DEFINE_PROP_STRING("lan.defgw_ipaddr", IPMIBmcSim, lan.arg_defgw_ipaddr),
2672
    DEFINE_PROP_MACADDR("lan.defgw_macaddr", IPMIBmcSim, lan.defgw_macaddr),
2673
};
2674
2675
static void ipmi_sim_class_init(ObjectClass *oc, const void *data)
2676
63
{
2677
63
    DeviceClass *dc = DEVICE_CLASS(oc);
2678
63
    IPMIBmcClass *bk = IPMI_BMC_CLASS(oc);
2679
2680
63
    dc->hotpluggable = false;
2681
63
    dc->realize = ipmi_sim_realize;
2682
63
    dc->vmsd = &vmstate_ipmi_sim;
2683
63
    device_class_set_props(dc, ipmi_sim_properties);
2684
63
    bk->handle_command = ipmi_sim_handle_command;
2685
63
}
2686
2687
static const TypeInfo ipmi_sim_type = {
2688
    .name          = TYPE_IPMI_BMC_SIMULATOR,
2689
    .parent        = TYPE_IPMI_BMC,
2690
    .instance_size = sizeof(IPMIBmcSim),
2691
    .class_init    = ipmi_sim_class_init,
2692
};
2693
2694
static void ipmi_sim_register_types(void)
2695
63
{
2696
63
    type_register_static(&ipmi_sim_type);
2697
63
}
2698
2699
type_init(ipmi_sim_register_types)