summaryrefslogtreecommitdiff
path: root/usr/src/test/util-tests/tests/smbios/smbios_test_chassis.c
blob: 1d6ef4550578ac092d17f923f6928c3a74837039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2021 Oxide Computer Company
 */

/*
 * Tests for SMBIOS Type 3 - SMB_TYPE_CHASSIS.
 */

#include "smbios_test.h"

static const char *smbios_chassis_mfg = "Shrina";
static const char *smbios_chassis_vers = "7R";
static const char *smbios_chassis_serial = "What's my number?";
static const char *smbios_chassis_asset = "lost";
static const char *smbios_chassis_sku = "Proud";
static const uint32_t smbios_chassis_oem = 0x36105997;
static const uint8_t smbios_chassis_uheight = 7;

boolean_t
smbios_test_chassis_mktable_invlen_base(smbios_test_table_t *table)
{
	smb_header_t hdr;

	hdr.smbh_type = SMB_TYPE_CHASSIS;
	hdr.smbh_len = sizeof (hdr);

	(void) smbios_test_table_append(table, &hdr, sizeof (hdr));
	smbios_test_table_append_eot(table);

	return (B_TRUE);
}

static void
smbios_test_chassis_mktable_fill_chassis(smb_chassis_t *ch)
{
	ch->smbch_hdr.smbh_type = SMB_TYPE_CHASSIS;
	ch->smbch_hdr.smbh_len = sizeof (*ch);
	ch->smbch_manufacturer = 1;
	ch->smbch_type = SMB_CHT_LUNCHBOX;
	ch->smbch_version = 2;
	ch->smbch_serial = 3;
	ch->smbch_asset = 4;
	ch->smbch_bustate = SMB_CHST_SAFE;
	ch->smbch_psstate = SMB_CHST_NONREC;
	ch->smbch_thstate = SMB_CHST_WARNING;
	ch->smbch_security = SMB_CHSC_NONE;
	ch->smbch_oemdata = htole32(smbios_chassis_oem);
	ch->smbch_uheight = smbios_chassis_uheight;
	ch->smbch_cords = smbios_chassis_uheight - 1;
	ch->smbch_cn = 0;
	ch->smbch_cm = sizeof (smb_chassis_entry_t);
}

static void
smbios_test_chassis_mktable_fill_entries(smb_chassis_entry_t *ents)
{
	ents[0].smbce_type = SMB_TYPE_COOLDEV | (1 << 7);
	ents[0].smbce_min = 1;
	ents[0].smbce_max = 42;
	ents[1].smbce_type = SMB_BBT_IO;
	ents[1].smbce_min = 5;
	ents[1].smbce_max = 123;
}

static void
smbios_test_chassis_mktable_append_strings(smbios_test_table_t *table)
{
	smbios_test_table_append_string(table, smbios_chassis_mfg);
	smbios_test_table_append_string(table, smbios_chassis_vers);
	smbios_test_table_append_string(table, smbios_chassis_serial);
	smbios_test_table_append_string(table, smbios_chassis_asset);
}

/*
 * This is an SMBIOS 2.4-esque table.
 */
boolean_t
smbios_test_chassis_mktable_base(smbios_test_table_t *table)
{
	smb_chassis_t ch;

	smbios_test_chassis_mktable_fill_chassis(&ch);
	(void) smbios_test_table_append(table, &ch, sizeof (ch));
	smbios_test_chassis_mktable_append_strings(table);
	smbios_test_table_str_fini(table);
	smbios_test_table_append_eot(table);

	return (B_TRUE);
}

boolean_t
smbios_test_chassis_mktable_comps(smbios_test_table_t *table)
{
	smb_chassis_t ch;
	smb_chassis_entry_t ents[2];

	smbios_test_chassis_mktable_fill_chassis(&ch);
	smbios_test_chassis_mktable_fill_entries(ents);
	ch.smbch_hdr.smbh_len += sizeof (ents);
	ch.smbch_cn = 2;
	(void) smbios_test_table_append(table, &ch, sizeof (ch));
	smbios_test_table_append_raw(table, ents, sizeof (ents));
	smbios_test_chassis_mktable_append_strings(table);
	smbios_test_table_str_fini(table);
	smbios_test_table_append_eot(table);

	return (B_TRUE);
}

boolean_t
smbios_test_chassis_mktable_sku_nocomps(smbios_test_table_t *table)
{
	smb_chassis_t ch;
	const uint8_t sku_str = 5;

	smbios_test_chassis_mktable_fill_chassis(&ch);
	ch.smbch_hdr.smbh_len++;
	(void) smbios_test_table_append(table, &ch, sizeof (ch));
	smbios_test_table_append_raw(table, &sku_str, sizeof (sku_str));
	smbios_test_chassis_mktable_append_strings(table);
	smbios_test_table_append_string(table, smbios_chassis_sku);
	smbios_test_table_str_fini(table);
	smbios_test_table_append_eot(table);

	return (B_TRUE);
}

boolean_t
smbios_test_chassis_mktable_sku(smbios_test_table_t *table)
{
	smb_chassis_t ch;
	const uint8_t sku_str = 5;
	smb_chassis_entry_t ents[2];

	ch.smbch_cn = 2;

	smbios_test_chassis_mktable_fill_chassis(&ch);
	smbios_test_chassis_mktable_fill_entries(ents);
	ch.smbch_hdr.smbh_len += sizeof (ents) + 1;
	(void) smbios_test_table_append(table, &ch, sizeof (ch));
	smbios_test_table_append_raw(table, &sku_str, sizeof (sku_str));
	smbios_test_chassis_mktable_append_strings(table);
	smbios_test_table_append_string(table, smbios_chassis_sku);
	smbios_test_table_str_fini(table);
	smbios_test_table_append_eot(table);

	return (B_TRUE);
}

boolean_t
smbios_test_chassis_verify_invlen(smbios_hdl_t *hdl)
{
	smbios_struct_t sp;
	smbios_chassis_t ch;

	if (smbios_lookup_type(hdl, SMB_TYPE_CHASSIS, &sp) == -1) {
		warnx("failed to lookup SMBIOS chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (smbios_info_chassis(hdl, sp.smbstr_id, &ch) != -1) {
		warnx("accidentally parsed invalid chassis as valid");
		return (B_FALSE);
	}

	if (smbios_errno(hdl) != ESMB_SHORT) {
		warnx("encountered wrong error for chassis, expected: "
		    "0x%x, found: 0x%x", ESMB_SHORT, smbios_errno(hdl));
		return (B_FALSE);
	}

	return (B_TRUE);
}

static boolean_t
smbios_test_chassis_verify_common(smbios_hdl_t *hdl, smbios_struct_t *sp,
    smbios_chassis_t *ch)
{
	boolean_t ret = B_TRUE;
	smbios_info_t info;

	if (ch->smbc_oemdata != smbios_chassis_oem) {
		warnx("chassis state mismatch, found unexpected oem data: 0x%x",
		    ch->smbc_oemdata);
		ret = B_FALSE;
	}

	if (ch->smbc_lock != 0) {
		warnx("chassis state mismatch, found unexpected lock: 0x%x",
		    ch->smbc_lock);
		ret = B_FALSE;
	}

	if (ch->smbc_type != SMB_CHT_LUNCHBOX) {
		warnx("chassis state mismatch, found unexpected type: 0x%x",
		    ch->smbc_type);
		ret = B_FALSE;
	}

	if (ch->smbc_bustate != SMB_CHST_SAFE) {
		warnx("chassis state mismatch, found unexpected boot state: "
		    "0x%x", ch->smbc_bustate);
		ret = B_FALSE;
	}

	if (ch->smbc_psstate != SMB_CHST_NONREC) {
		warnx("chassis state mismatch, found unexpected power state: "
		    "0x%x", ch->smbc_psstate);
		ret = B_FALSE;
	}

	if (ch->smbc_thstate != SMB_CHST_WARNING) {
		warnx("chassis state mismatch, found unexpected thermal state: "
		    "0x%x", ch->smbc_thstate);
		ret = B_FALSE;
	}

	if (ch->smbc_security != SMB_CHSC_NONE) {
		warnx("chassis state mismatch, found unexpected security "
		    "value: 0x%x", ch->smbc_security);
		ret = B_FALSE;
	}

	if (ch->smbc_uheight != smbios_chassis_uheight) {
		warnx("chassis state mismatch, found unexpected uheight value: "
		    "0x%x", ch->smbc_uheight);
		ret = B_FALSE;
	}

	if (ch->smbc_cords != smbios_chassis_uheight - 1) {
		warnx("chassis state mismatch, found unexpected cords value: "
		    "0x%x", ch->smbc_cords);
		ret = B_FALSE;
	}

	if (ch->smbc_elemlen != sizeof (smb_chassis_entry_t)) {
		warnx("chassis state mismatch, found unexpected elemlen value: "
		    "0x%x", ch->smbc_elemlen);
		ret = B_FALSE;
	}

	if (smbios_info_common(hdl, sp->smbstr_id, &info) != 0) {
		warnx("failed to get common chassis info: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (strcmp(info.smbi_manufacturer, smbios_chassis_mfg) != 0) {
		warnx("chassis state mismatch, found unexpected mfg: "
		    "%s", info.smbi_manufacturer);
		ret = B_FALSE;
	}

	if (strcmp(info.smbi_version, smbios_chassis_vers) != 0) {
		warnx("chassis state mismatch, found unexpected version: %s",
		    info.smbi_version);
		ret = B_FALSE;
	}

	if (strcmp(info.smbi_serial, smbios_chassis_serial) != 0) {
		warnx("chassis state mismatch, found unexpected serial: %s",
		    info.smbi_serial);
		ret = B_FALSE;
	}

	if (strcmp(info.smbi_asset, smbios_chassis_asset) != 0) {
		warnx("chassis state mismatch, found unexpected asset: %s",
		    info.smbi_asset);
		ret = B_FALSE;
	}

	return (ret);
}

boolean_t
smbios_test_chassis_verify_base(smbios_hdl_t *hdl)
{
	boolean_t ret = B_TRUE;
	smbios_struct_t sp;
	smbios_chassis_t ch;
	smbios_chassis_entry_t *elts;
	uint_t nelts;

	if (smbios_lookup_type(hdl, SMB_TYPE_CHASSIS, &sp) == -1) {
		warnx("failed to lookup SMBIOS chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (smbios_info_chassis(hdl, sp.smbstr_id, &ch) == -1) {
		warnx("failed to get chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (!smbios_test_chassis_verify_common(hdl, &sp, &ch)) {
		ret = B_FALSE;
	}

	if (ch.smbc_elems != 0) {
		warnx("chassis state mismatch, found unexpected number of "
		    "elements: 0x%x", ch.smbc_elems);
		ret = B_FALSE;
	}

	if (strcmp(ch.smbc_sku, "") != 0) {
		warnx("chassis state mismatch, found unexpected sku: %s",
		    ch.smbc_sku);
		ret = B_FALSE;
	}

	if (smbios_info_chassis_elts(hdl, sp.smbstr_id, &nelts, &elts) != 0) {
		warnx("failed to get chassis elements: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (nelts != 0) {
		warnx("chassis state mismatch, smbios_info_chassis_elts() "
		    "returned a non-zero number of entries: %u", nelts);
		ret = B_FALSE;
	}

	if (elts != NULL) {
		warnx("chassis state mismatch, smbios_info_chassis_elts() "
		    "returned a non-NULL pointer: %p", elts);
		ret = B_FALSE;
	}

	return (ret);
}

boolean_t
smbios_test_chassis_verify_sku_nocomps(smbios_hdl_t *hdl)
{
	boolean_t ret = B_TRUE;
	smbios_struct_t sp;
	smbios_chassis_t ch;
	smbios_chassis_entry_t *elts;
	uint_t nelts;

	if (smbios_lookup_type(hdl, SMB_TYPE_CHASSIS, &sp) == -1) {
		warnx("failed to lookup SMBIOS chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (smbios_info_chassis(hdl, sp.smbstr_id, &ch) == -1) {
		warnx("failed to get chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (!smbios_test_chassis_verify_common(hdl, &sp, &ch)) {
		ret = B_FALSE;
	}

	if (ch.smbc_elems != 0) {
		warnx("chassis state mismatch, found unexpected number of "
		    "elements: 0x%x", ch.smbc_elems);
		ret = B_FALSE;
	}

	if (strcmp(ch.smbc_sku, smbios_chassis_sku) != 0) {
		warnx("chassis state mismatch, found unexpected sku: %s",
		    ch.smbc_sku);
		ret = B_FALSE;
	}

	if (smbios_info_chassis_elts(hdl, sp.smbstr_id, &nelts, &elts) != 0) {
		warnx("failed to get chassis elements: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (nelts != 0) {
		warnx("chassis state mismatch, smbios_info_chassis_elts() "
		    "returned a non-zero number of entries: %u", nelts);
		ret = B_FALSE;
	}

	if (elts != NULL) {
		warnx("chassis state mismatch, smbios_info_chassis_elts() "
		    "returned a non-NULL pointer: %p", elts);
		ret = B_FALSE;
	}


	return (ret);
}

static boolean_t
smbios_test_chassis_verify_common_comps(smbios_hdl_t *hdl, smbios_struct_t *sp)
{
	boolean_t ret = B_TRUE;
	smbios_chassis_entry_t *elts;
	uint_t nelts;

	if (smbios_info_chassis_elts(hdl, sp->smbstr_id, &nelts, &elts) != 0) {
		warnx("failed to get chassis elements: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (nelts != 2) {
		warnx("chassis state mismatch, smbios_info_chassis_elts() "
		    "returned the wrong number of entries: %u", nelts);
		return (B_FALSE);
	}

	if (elts[0].smbce_type != SMB_CELT_SMBIOS) {
		warnx("chassis elts[0] type mismatch, found: %u",
		    elts[0].smbce_type);
		ret = B_FALSE;
	}

	if (elts[0].smbce_elt != SMB_TYPE_COOLDEV) {
		warnx("chassis elts[0] elt type mismatch, found: %u",
		    elts[0].smbce_elt);
		ret = B_FALSE;
	}

	if (elts[0].smbce_min != 1) {
		warnx("chassis elts[0] minimum number mismatch, found: %u",
		    elts[0].smbce_min);
		ret = B_FALSE;
	}

	if (elts[0].smbce_max != 42) {
		warnx("chassis elts[0] maximum number mismatch, found: %u",
		    elts[0].smbce_max);
		ret = B_FALSE;
	}

	if (elts[1].smbce_type != SMB_CELT_BBOARD) {
		warnx("chassis elts[1] type mismatch, found: %u",
		    elts[1].smbce_type);
		ret = B_FALSE;
	}

	if (elts[1].smbce_elt != SMB_BBT_IO) {
		warnx("chassis elts[1] elt type mismatch, found: %u",
		    elts[1].smbce_elt);
		ret = B_FALSE;
	}

	if (elts[1].smbce_min != 5) {
		warnx("chassis elts[1] minimum number mismatch, found: %u",
		    elts[1].smbce_min);
		ret = B_FALSE;
	}

	if (elts[1].smbce_max != 123) {
		warnx("chassis elts[1] maximum number mismatch, found: %u",
		    elts[1].smbce_max);
		ret = B_FALSE;
	}
	return (ret);
}

boolean_t
smbios_test_chassis_verify_comps(smbios_hdl_t *hdl)
{
	boolean_t ret = B_TRUE;
	smbios_struct_t sp;
	smbios_chassis_t ch;

	if (smbios_lookup_type(hdl, SMB_TYPE_CHASSIS, &sp) == -1) {
		warnx("failed to lookup SMBIOS chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (smbios_info_chassis(hdl, sp.smbstr_id, &ch) == -1) {
		warnx("failed to get chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (!smbios_test_chassis_verify_common(hdl, &sp, &ch)) {
		ret = B_FALSE;
	}

	if (ch.smbc_elems != 2) {
		warnx("chassis state mismatch, found unexpected number of "
		    "elements: 0x%x", ch.smbc_elems);
		ret = B_FALSE;
	}

	if (strcmp(ch.smbc_sku, "") != 0) {
		warnx("chassis state mismatch, found unexpected sku: %s",
		    ch.smbc_sku);
		ret = B_FALSE;
	}

	if (!smbios_test_chassis_verify_common_comps(hdl, &sp)) {
		ret = B_FALSE;
	}

	return (ret);
}


boolean_t
smbios_test_chassis_verify_sku(smbios_hdl_t *hdl)
{
	boolean_t ret = B_TRUE;
	smbios_struct_t sp;
	smbios_chassis_t ch;

	if (smbios_lookup_type(hdl, SMB_TYPE_CHASSIS, &sp) == -1) {
		warnx("failed to lookup SMBIOS chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (smbios_info_chassis(hdl, sp.smbstr_id, &ch) == -1) {
		warnx("failed to get chassis: %s",
		    smbios_errmsg(smbios_errno(hdl)));
		return (B_FALSE);
	}

	if (!smbios_test_chassis_verify_common(hdl, &sp, &ch)) {
		ret = B_FALSE;
	}

	if (ch.smbc_elems != 2) {
		warnx("chassis state mismatch, found unexpected number of "
		    "elements: 0x%x", ch.smbc_elems);
		ret = B_FALSE;
	}

	if (strcmp(ch.smbc_sku, smbios_chassis_sku) != 0) {
		warnx("chassis state mismatch, found unexpected sku: %s",
		    ch.smbc_sku);
		ret = B_FALSE;
	}

	if (!smbios_test_chassis_verify_common_comps(hdl, &sp)) {
		ret = B_FALSE;
	}

	return (ret);
}