summaryrefslogtreecommitdiffstats
path: root/discover/platform-powerpc.c
blob: 5d7cc597697f683fda11a439a0e2cc278d24f493 (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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033

#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <asm/byteorder.h>
#include <limits.h>

#include <file/file.h>
#include <talloc/talloc.h>
#include <list/list.h>
#include <log/log.h>
#include <process/process.h>
#include <crypt/crypt.h>

#include "hostboot.h"
#include "platform.h"
#include "ipmi.h"
#include "dt.h"

static const char *partition = "common";
static const char *sysparams_dir = "/sys/firmware/opal/sysparams/";
static const char *devtree_dir = "/proc/device-tree/";

struct platform_powerpc {
	struct param_list *params;
	struct ipmi	*ipmi;
	char		*ipmi_mailbox_original_config;
	int		(*get_ipmi_bootdev)(
				struct platform_powerpc *platform,
				uint8_t *bootdev, bool *persistent);
	int		(*clear_ipmi_bootdev)(
				struct platform_powerpc *platform,
				bool persistent);
	int		(*get_ipmi_boot_mailbox)(
				struct platform_powerpc *platform,
				char **buf);
	int		(*clear_ipmi_boot_mailbox)(
				struct platform_powerpc *platform);
	int 		(*set_os_boot_sensor)(
				struct platform_powerpc *platform);
	void		(*get_platform_versions)(struct system_info *info);
};

#define to_platform_powerpc(p) \
	(struct platform_powerpc *)(p->platform_data)

static int parse_nvram_params(struct platform_powerpc *platform,
		char *buf, int len)
{
	char *pos, *name, *value;
	unsigned int paramlen;
	int i, count;

	/* discard 2 header lines:
	 * "common" partiton"
	 * ------------------
	 */
	pos = buf;
	count = 0;

	for (i = 0; i < len; i++) {
		if (pos[i] == '\n')
			count++;
		if (count == 2)
			break;
	}

	if (i == len) {
		fprintf(stderr, "failure parsing nvram output\n");
		return -1;
	}

	for (pos = buf + i; pos < buf + len; pos += paramlen + 1) {
		unsigned int namelen;
		char *newline;

		newline = strchr(pos, '\n');
		if (!newline)
			break;

		*newline = '\0';

		paramlen = strlen(pos);

		name = pos;
		value = strchr(pos, '=');
		if (!value)
			continue;

		namelen = value - name;
		if (namelen == 0)
			continue;

		if (!param_list_is_known_n(platform->params, name, namelen))
			continue;

		*value = '\0';
		value++;

		param_list_set(platform->params, name, value, false);
	}

	return 0;
}

static int parse_nvram(struct platform_powerpc *platform)
{
	struct process_stdout *stdout;
	const char *argv[5];
	int rc;

	argv[0] = "nvram";
	argv[1] = "--print-config";
	argv[2] = "--partition";
	argv[3] = partition;
	argv[4] = NULL;

	rc = process_get_stdout_argv(NULL, &stdout, argv);

	if (rc) {
		fprintf(stderr, "nvram process returned "
				"non-zero exit status\n");
		rc = -1;
	} else {
		rc = parse_nvram_params(platform, stdout->buf, stdout->len);
	}

	talloc_free(stdout);
	return rc;
}

static int write_nvram(struct platform_powerpc *platform)
{
	struct process *process;
	struct param *param;
	const char *argv[6];
	int rc = 0;

	argv[0] = "nvram";
	argv[1] = "--update-config";
	argv[2] = NULL;
	argv[3] = "--partition";
	argv[4] = partition;
	argv[5] = NULL;

	process = process_create(platform);
	process->path = "nvram";
	process->argv = argv;

	param_list_for_each(platform->params, param) {
		char *paramstr;

		if (!param->modified)
			continue;

		paramstr = talloc_asprintf(platform, "%s=%s",
				param->name, param->value);
		argv[2] = paramstr;

		rc = process_run_sync(process);

		talloc_free(paramstr);

		if (rc || !process_exit_ok(process)) {
			rc = -1;
			pb_log("nvram update process returned "
					"non-zero exit status\n");
			break;
		}
	}

	process_release(process);
	return rc;
}

static void params_update_all(struct param_list *pl,
	const struct config *config, const struct config *defaults)
{
	char *tmp = NULL;
	const char *val;

	if (config->autoboot_enabled == defaults->autoboot_enabled)
		val = "";
	else
		val = config->autoboot_enabled ? "true" : "false";

	param_list_set_non_empty(pl, "auto-boot?", val, true);

	if (config->autoboot_timeout_sec == defaults->autoboot_timeout_sec)
		val = "";
	else
		val = tmp = talloc_asprintf(pl, "%d",
			config->autoboot_timeout_sec);

	param_list_set_non_empty(pl, "petitboot,timeout", val, true);
	if (tmp)
		talloc_free(tmp);

	val = config->lang ?: "";
	param_list_set_non_empty(pl, "petitboot,language", val, true);

	if (config->allow_writes == defaults->allow_writes)
		val = "";
	else
		val = config->allow_writes ? "true" : "false";
	param_list_set_non_empty(pl, "petitboot,write?", val, true);

	if (!config->manual_console) {
		val = config->boot_console ?: "";
		param_list_set_non_empty(pl, "petitboot,console", val, true);
	}

	val = config->http_proxy ?: "";
	param_list_set_non_empty(pl, "petitboot,http_proxy", val, true);
	val = config->https_proxy ?: "";
	param_list_set_non_empty(pl, "petitboot,https_proxy", val, true);

	params_update_network_values(pl, "petitboot,network", config);
	params_update_bootdev_values(pl, "petitboot,bootdevs", config);
}

static void config_set_ipmi_bootdev(struct config *config, enum ipmi_bootdev bootdev,
		bool persistent)
{
	config->ipmi_bootdev = bootdev;
	config->ipmi_bootdev_persistent = persistent;

	if (bootdev == IPMI_BOOTDEV_SAFE)
		config->safe_mode = true;
}

static int read_bootdev_sysparam(const char *name, uint8_t *val)
{
	uint8_t buf[2];
	char path[50];
	int fd, rc;

	assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
	snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		pb_debug("powerpc: can't access sysparam %s\n",
				name);
		return -1;
	}

	rc = read(fd, buf, sizeof(buf));

	close(fd);

	/* bootdev definitions should only be one byte in size */
	if (rc != 1) {
		pb_debug("powerpc: sysparam %s read returned %d\n",
				name, rc);
		return -1;
	}

	pb_debug("powerpc: sysparam %s: 0x%02x\n", name, buf[0]);

	if (!ipmi_bootdev_is_valid(buf[0]))
		return -1;

	*val = buf[0];
	return 0;
}

static int write_bootdev_sysparam(const char *name, uint8_t val)
{
	char path[50];
	int fd, rc;

	assert(strlen(sysparams_dir) + strlen(name) < sizeof(path));
	snprintf(path, sizeof(path), "%s%s", sysparams_dir, name);

	fd = open(path, O_WRONLY);
	if (fd < 0) {
		pb_debug("powerpc: can't access sysparam %s for writing\n",
				name);
		return -1;
	}

	for (;;) {
		errno = 0;
		rc = write(fd, &val, sizeof(val));
		if (rc == sizeof(val)) {
			rc = 0;
			break;
		}

		if (rc <= 0 && errno != EINTR) {
			pb_log("powerpc: error updating sysparam %s: %s",
					name, strerror(errno));
			rc = -1;
			break;
		}
	}

	close(fd);

	if (!rc)
		pb_debug("powerpc: set sysparam %s: 0x%02x\n", name, val);

	return rc;
}

static int clear_ipmi_bootdev_sysparams(
		struct platform_powerpc *platform __attribute__((unused)),
		bool persistent)
{
	if (persistent) {
		/* invalidate default-boot-device setting */
		write_bootdev_sysparam("default-boot-device", 0xff);
	} else {
		/* invalidate next-boot-device setting */
		write_bootdev_sysparam("next-boot-device", 0xff);
	}
	return 0;
}

static int get_ipmi_bootdev_sysparams(
		struct platform_powerpc *platform __attribute__((unused)),
		uint8_t *bootdev, bool *persistent)
{
	uint8_t next_bootdev, default_bootdev;
	bool next_valid, default_valid;
	int rc;

	rc = read_bootdev_sysparam("next-boot-device", &next_bootdev);
	next_valid = rc == 0;

	rc = read_bootdev_sysparam("default-boot-device", &default_bootdev);
	default_valid = rc == 0;

	/* nothing valid? no need to change the config */
	if (!next_valid && !default_valid)
		return -1;

	*persistent = !next_valid;
	*bootdev = next_valid ? next_bootdev : default_bootdev;
	return 0;
}

static int clear_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
				   bool persistent __attribute__((unused)))
{
	uint16_t resp_len;
	uint8_t resp[1];
	uint8_t req[] = {
		0x05, /* parameter selector: boot flags */
		0x80, /* data 1: valid */
		0x00, /* data 2: bootdev: no override */
		0x00, /* data 3: system defaults */
		0x00, /* data 4: no request for shared mode, mux defaults */
		0x00, /* data 5: no instance request */
	};

	resp_len = sizeof(resp);

	ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
			IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
			req, sizeof(req),
			resp, &resp_len,
			ipmi_timeout);
	return 0;
}

static int get_ipmi_bootdev_ipmi(struct platform_powerpc *platform,
		uint8_t *bootdev, bool *persistent)
{
	uint16_t resp_len;
	uint8_t resp[8];
	char *debug_buf;
	int rc;
	uint8_t req[] = {
		0x05, /* parameter selector: boot flags */
		0x00, /* no set selector */
		0x00, /* no block selector */
	};

	resp_len = sizeof(resp);
	rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
			IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
			req, sizeof(req),
			resp, &resp_len,
			ipmi_timeout);
	if (rc) {
		pb_log("platform: error reading IPMI boot options\n");
		return -1;
	}

	if (resp_len != sizeof(resp)) {
		pb_log("platform: unexpected length (%d) in "
				"boot options response\n", resp_len);
		return -1;
	}

	debug_buf = format_buffer(platform, resp, resp_len);
	pb_debug_fn("IPMI get_bootdev response:\n%s\n", debug_buf);
	talloc_free(debug_buf);

	if (resp[0] != 0) {
		pb_log("platform: non-zero completion code %d from IPMI req\n",
				resp[0]);
		return -1;
	}

	/* check for correct parameter version */
	if ((resp[1] & 0xf) != 0x1) {
		pb_log("platform: unexpected version (0x%x) in "
				"boot options response\n", resp[0]);
		return -1;
	}

	/* check for valid paramters */
	if (resp[2] & 0x80) {
		pb_debug("platform: boot options are invalid/locked\n");
		return -1;
	}

	*persistent = false;

	/* check for valid flags */
	if (!(resp[3] & 0x80)) {
		pb_debug("platform: boot flags are invalid, ignoring\n");
		return -1;
	}

	*persistent = resp[3] & 0x40;
	*bootdev = (resp[4] >> 2) & 0x0f;
	return 0;
}

static int get_ipmi_boot_mailbox_block(struct platform_powerpc *platform,
		char *buf, uint8_t block)
{
	size_t blocksize = 16;
	uint8_t resp[3 + 16];
	uint16_t resp_len;
	char *debug_buf;
	int rc;
	uint8_t req[] = {
		0x07,  /* parameter selector: boot initiator mailbox */
		block, /* set selector */
		0x00,  /* no block selector */
	};

	resp_len = sizeof(resp);
	rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
			IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
			req, sizeof(req),
			resp, &resp_len,
			ipmi_timeout);
	if (rc) {
		pb_log("platform: error reading IPMI boot options\n");
		return -1;
	}

	if (resp_len < sizeof(resp)) {
		if (resp_len < 3) {
			pb_log("platform: unexpected length (%d) in "
					"boot options mailbox response\n",
					resp_len);
			return -1;
		}

		if (resp_len == 4) {
			pb_debug_fn("block %hu empty\n", block);
			return 0;
		}

		blocksize = sizeof(resp) - 3;
		pb_debug_fn("Mailbox block %hu returns only %zu bytes in block\n",
				block, blocksize);
	}

	debug_buf = format_buffer(platform, resp, resp_len);
	pb_debug_fn("IPMI bootdev mailbox block %hu:\n%s\n", block, debug_buf);
	talloc_free(debug_buf);

	if (resp[0] != 0) {
		pb_log("platform: non-zero completion code %d from IPMI req\n",
				resp[0]);
		return -1;
	}

	/* check for correct parameter version */
	if ((resp[1] & 0xf) != 0x1) {
		pb_log("platform: unexpected version (0x%x) in "
				"boot mailbox response\n", resp[0]);
		return -1;
	}

	/* check for valid paramters */
	if (resp[2] & 0x80) {
		pb_debug("platform: boot mailbox parameters are invalid/locked\n");
		return -1;
	}

	memcpy(buf, &resp[3], blocksize);

	return blocksize;
}

static int get_ipmi_boot_mailbox(struct platform_powerpc *platform,
		char **buf)
{
	char *mailbox_buffer, *prefix;
	const size_t blocksize = 16;
	char block_buffer[blocksize];
	size_t mailbox_size;
	int content_size;
	uint8_t i;
	int rc;

	mailbox_buffer = NULL;
	mailbox_size = 0;

	/*
	 * The BMC may hold up to 255 blocks of data but more likely the number
	 * will be closer to the minimum of 5 set by the specification and error
	 * on higher numbers.
	 */
	for (i = 0; i < UCHAR_MAX; i++) {
		rc = get_ipmi_boot_mailbox_block(platform, block_buffer, i);
		if (rc < 3 && i == 0) {
			/*
			 * Immediate failure, no blocks read or missing IANA
			 * number.
			 */
			return -1;
		}
		if (rc < 1) {
			/* Error or no bytes read */
			break;
		}

		if (i == 0) {
			/*
			 * The first three bytes of block zero are an IANA
			 * Enterprise ID number. Check it matches the IBM
			 * number, '2'.
			 */
			if (block_buffer[0] != 0x02 ||
				block_buffer[1] != 0x00 ||
				block_buffer[2] != 0x00) {
				pb_log_fn("IANA number unrecognised: 0x%x:0x%x:0x%x\n",
						block_buffer[0],
						block_buffer[1],
						block_buffer[2]);
				return -1;
			}
		}

		mailbox_buffer = talloc_realloc(platform, mailbox_buffer,
				char, mailbox_size + rc);
		if (!mailbox_buffer) {
			pb_log_fn("Failed to allocate mailbox buffer\n");
			return -1;
		}
		memcpy(mailbox_buffer + mailbox_size, block_buffer, rc);
		mailbox_size += rc;
	}

	if (i < 5)
		pb_log_fn("Only %hu blocks read, spec requires at least 5.\n"
			  "Send a bug report to your preferred BMC vendor!\n",
			  i);
	else
		pb_debug_fn("%hu blocks read (%zu bytes)\n", i, mailbox_size);

	if (mailbox_size < 3 + strlen("petitboot,bootdevs="))
		return -1;

	prefix = talloc_strndup(mailbox_buffer, mailbox_buffer + 3,
			strlen("petitboot,bootdevs="));
	if (!prefix) {
		pb_log_fn("Couldn't check prefix\n");
		talloc_free(mailbox_buffer);
		return -1;
	}

	if (strncmp(prefix, "petitboot,bootdevs=",
				strlen("petitboot,bootdevs=")) != 0 ) {
		/* Empty or garbage */
		pb_debug_fn("Buffer looks unconfigured\n");
		talloc_free(mailbox_buffer);
		*buf = NULL;
		return 0;
	}

	/* Don't include IANA number in buffer */
	content_size = mailbox_size - 3 - strlen("petitboot,bootdevs=");
	*buf = talloc_memdup(platform,
			mailbox_buffer + 3 + strlen("petitboot,bootdevs="),
			content_size + 1);
	(*buf)[content_size] = '\0';

	talloc_free(mailbox_buffer);
	return 0;
}

static int clear_ipmi_boot_mailbox(struct platform_powerpc *platform)
{
	uint8_t req[18] = {0}; /* req (2) + blocksize (16) */
	uint16_t resp_len;
	uint8_t resp[1];
	uint8_t i;
	int rc;

	req[0] = 0x07;  /* parameter selector: boot initiator mailbox */

	resp_len = sizeof(resp);

	for (i = 0; i < UCHAR_MAX; i++) {
		req[1] = i; /* set selector */
		rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
				IPMI_CMD_CHASSIS_SET_SYSTEM_BOOT_OPTIONS,
				req, sizeof(req),
				resp, &resp_len,
				ipmi_timeout);

		if (rc || resp[0]) {
			if (i == 0) {
				pb_log_fn("error clearing IPMI boot mailbox, "
						"rc %d resp[0] %hu\n",
						rc, resp[0]);
				return -1;
			}
			break;
		}
	}

	pb_debug_fn("Cleared %hu blocks\n", i);

	return 0;
}

static int set_ipmi_os_boot_sensor(struct platform_powerpc *platform)
{
	int sensor_number;
	uint16_t resp_len;
	uint8_t resp[1];
	uint8_t req[] = {
		0x00, /* sensor number: os boot */
		0xA9, /* operation: set everything */
		0x00, /* sensor reading: none */
		0x40, /* assertion mask lsb: set state 6 */
		0x00, /* assertion mask msb: none */
		0x00, /* deassertion mask lsb: none */
		0x00, /* deassertion mask msb: none */
		0x00, /* event data 1: none */
		0x00, /* event data 2: none */
		0x00, /* event data 3: none */
	};

	sensor_number = get_ipmi_sensor(platform, IPMI_SENSOR_ID_OS_BOOT);
	if (sensor_number < 0) {
		pb_log("Couldn't find OS boot sensor in device tree\n");
		return -1;
	}

	req[0] = sensor_number;

	resp_len = sizeof(resp);

	ipmi_transaction(platform->ipmi, IPMI_NETFN_SE,
			IPMI_CMD_SENSOR_SET,
			req, sizeof(req),
			resp, &resp_len,
			ipmi_timeout); return 0;

	return 0;
}

static void get_ipmi_network_override(struct platform_powerpc *platform,
			struct config *config)
{
	uint16_t min_len = 12, resp_len = 53, version;
	const uint32_t magic_value = 0x21706221;
	uint8_t resp[resp_len];
	char *debug_buf;
	uint32_t cookie;
	bool persistent;
	int i, rc;
	uint8_t req[] = {
		0x61, /* parameter selector: OEM section (network) */
		0x00, /* no set selector */
		0x00, /* no block selector */
	};

	rc = ipmi_transaction(platform->ipmi, IPMI_NETFN_CHASSIS,
			IPMI_CMD_CHASSIS_GET_SYSTEM_BOOT_OPTIONS,
			req, sizeof(req),
			resp, &resp_len,
			ipmi_timeout);

	debug_buf = format_buffer(platform, resp, resp_len);
	pb_debug_fn("IPMI net override response:\n%s\n", debug_buf);
	talloc_free(debug_buf);

	if (rc) {
		pb_debug("IPMI network config option unavailable\n");
		return;
	}

	if (resp_len < min_len) {
		pb_debug("IPMI net response too small\n");
		return;
	}

	if (resp[0] != 0) {
		pb_log("platform: non-zero completion code %d from IPMI network req\n",
		       resp[0]);
		return;
	}

	/* Check for correct parameter version */
	if ((resp[1] & 0xf) != 0x1) {
		pb_log("platform: unexpected version (0x%x) in network override response\n",
		       resp[0]);
		return;
	}

	/* Check that the parameters are valid */
	if (resp[2] & 0x80) {
		pb_debug("platform: network override is invalid/locked\n");
		return;
	}

	/* Check for valid parameters in the boot flags section */
	if (!(resp[3] & 0x80)) {
		pb_debug("platform: network override valid flag not set\n");
		return;
	}
	/* Read the persistent flag; if it is set we need to save this config */
	persistent = resp[3] & 0x40;
	if (persistent)
		pb_debug("platform: network override is persistent\n");

	/* Check 4-byte cookie value */
	i = 4;
	memcpy(&cookie, &resp[i], sizeof(cookie));
	cookie = __be32_to_cpu(cookie);
	if (cookie != magic_value) {
		pb_log_fn("Incorrect cookie %x\n", cookie);
		return;
	}
	i += sizeof(cookie);

	/* Check 2-byte version number */
	memcpy(&version, &resp[i], sizeof(version));
	version = __be16_to_cpu(version);
	i += sizeof(version);
	if (version != 1) {
		pb_debug("Unexpected version: %u\n", version);
		return;
	}

	/* Interpret the rest of the interface config */
	rc = parse_ipmi_interface_override(config, &resp[i], resp_len - i);

	if (!rc && persistent) {
		/* Write this new config to NVRAM */
		params_update_network_values(platform->params,
			"petitboot,network", config);
		rc = write_nvram(platform);
		if (rc)
			pb_log("platform: Failed to save persistent interface override\n");
	}
}

static void config_get_active_consoles(struct config *config)
{
	struct stat sbuf;
	char *fsp_prop = NULL;

	config->n_consoles = 2;
	config->consoles = talloc_array(config, char *, config->n_consoles);
	if (!config->consoles)
		goto err;

	config->consoles[0] = talloc_asprintf(config->consoles,
					"/dev/hvc0 [IPMI / Serial]");
	config->consoles[1] = talloc_asprintf(config->consoles,
					"/dev/tty1 [VGA]");

	fsp_prop = talloc_asprintf(config, "%sfsps", devtree_dir);
	if (stat(fsp_prop, &sbuf) == 0) {
		/* FSP based machines also have a separate serial console */
		config->consoles = talloc_realloc(config, config->consoles,
						char *,	config->n_consoles + 1);
		if (!config->consoles)
			goto err;
		config->consoles[config->n_consoles++] = talloc_asprintf(
						config->consoles,
						"/dev/hvc1 [Serial]");
	}

	return;
err:
	config->n_consoles = 0;
	pb_log("Failed to allocate memory for consoles\n");
}

static int load_config(struct platform *p, struct config *config)
{
	struct platform_powerpc *platform = to_platform_powerpc(p);
	const char *hash;
	int rc;

	rc = parse_nvram(platform);
	if (rc)
		pb_log_fn("Failed to parse nvram\n");

	/*
	 * If we have an IPMI mailbox configuration available use it instead of
	 * the boot order found in NVRAM.
	 */
	if (platform->get_ipmi_boot_mailbox) {
		char *mailbox;
		struct param *param;
		rc = platform->get_ipmi_boot_mailbox(platform, &mailbox);
		if (!rc && mailbox) {
			platform->ipmi_mailbox_original_config =
				talloc_strdup(
					platform,
					param_list_get_value(
						platform->params, "petitboot,bootdevs"));
			param_list_set(platform->params, "petitboot,bootdevs",
					mailbox, false);
			param = param_list_get_param(platform->params,
					"petitboot,bootdevs");
			/* Avoid writing this to NVRAM */
			param->modified = false;
			config->ipmi_bootdev_mailbox = true;
			talloc_free(mailbox);
		}
	}

	config_populate_all(config, platform->params);

	if (platform->get_ipmi_bootdev) {
		bool bootdev_persistent;
		uint8_t bootdev = IPMI_BOOTDEV_INVALID;
		rc = platform->get_ipmi_bootdev(platform, &bootdev,
				&bootdev_persistent);
		if (!rc && ipmi_bootdev_is_valid(bootdev)) {
			config_set_ipmi_bootdev(config, bootdev,
				bootdev_persistent);
		}
	}

	if (platform->ipmi)
		get_ipmi_network_override(platform, config);

	config_get_active_consoles(config);


	hash = param_list_get_value(platform->params, "petitboot,password");
	if (hash) {
		rc = crypt_set_password_hash(platform, hash);
		if (rc)
			pb_log("Failed to set password hash\n");
	}

	return 0;
}

static int save_config(struct platform *p, struct config *config)
{
	struct platform_powerpc *platform = to_platform_powerpc(p);
	struct config *defaults;
	struct param *param;

	if (config->ipmi_bootdev == IPMI_BOOTDEV_INVALID &&
	    platform->clear_ipmi_bootdev) {
		platform->clear_ipmi_bootdev(platform,
				config->ipmi_bootdev_persistent);
		config->ipmi_bootdev = IPMI_BOOTDEV_NONE;
		config->ipmi_bootdev_persistent = false;
	}

	if (!config->ipmi_bootdev_mailbox &&
			platform->ipmi_mailbox_original_config) {
		param = param_list_get_param(platform->params,
				"petitboot,bootdevs");
		/* Restore old boot order if unmodified */
		if (!param->modified) {
			param_list_set(platform->params, "petitboot,bootdevs",
					platform->ipmi_mailbox_original_config,
					false);
			param->modified = false;
			config_populate_bootdev(config, platform->params);
		}
		platform->clear_ipmi_boot_mailbox(platform);
		talloc_free(platform->ipmi_mailbox_original_config);
		platform->ipmi_mailbox_original_config = NULL;
	}

	defaults = talloc_zero(platform, struct config);
	config_set_defaults(defaults);

	params_update_all(platform->params, config, defaults);

	talloc_free(defaults);
	return write_nvram(platform);
}

static void pre_boot(struct platform *p, const struct config *config)
{
	struct platform_powerpc *platform = to_platform_powerpc(p);

	if (!config->ipmi_bootdev_persistent && platform->clear_ipmi_bootdev)
		platform->clear_ipmi_bootdev(platform, false);

	if (platform->set_os_boot_sensor)
		platform->set_os_boot_sensor(platform);
}

static int get_sysinfo(struct platform *p, struct system_info *sysinfo)
{
	struct platform_powerpc *platform = p->platform_data;
	char *buf, *filename;
	int len, rc;

	filename = talloc_asprintf(platform, "%smodel", devtree_dir);
	rc = read_file(platform, filename, &buf, &len);
	if (rc == 0)
		sysinfo->type = talloc_steal(sysinfo, buf);
	talloc_free(filename);

	filename = talloc_asprintf(platform, "%ssystem-id", devtree_dir);
	rc = read_file(platform, filename, &buf, &len);
	if (rc == 0)
		sysinfo->identifier = talloc_steal(sysinfo, buf);
	talloc_free(filename);

	sysinfo->bmc_mac = talloc_zero_size(sysinfo, HWADDR_SIZE);

	if (platform->ipmi) {
		ipmi_get_bmc_mac(platform->ipmi, sysinfo->bmc_mac);
		ipmi_get_bmc_versions(platform->ipmi, sysinfo);
	}

	if (platform->get_platform_versions)
		platform->get_platform_versions(sysinfo);

	return 0;
}

static bool restrict_clients(struct platform *p)
{
	struct platform_powerpc *platform = to_platform_powerpc(p);

	return param_list_get_value(platform->params, "petitboot,password") != NULL;
}

static int set_password(struct platform *p, const char *hash)
{
	struct platform_powerpc *platform = to_platform_powerpc(p);

	param_list_set(platform->params, "petitboot,password", hash, true);
	write_nvram(platform);

	return 0;
}

static bool probe(struct platform *p, void *ctx)
{
	struct platform_powerpc *platform;
	struct stat statbuf;
	bool bmc_present;
	int rc;

	/* we need a device tree */
	rc = stat("/proc/device-tree", &statbuf);
	if (rc)
		return false;

	if (!S_ISDIR(statbuf.st_mode))
		return false;

	platform = talloc_zero(ctx, struct platform_powerpc);
	platform->params = talloc_zero(platform, struct param_list);
	param_list_init(platform->params, common_known_params());

	p->platform_data = platform;

	bmc_present = stat("/proc/device-tree/bmc", &statbuf) == 0;

	if (ipmi_present() && bmc_present) {
		pb_debug("platform: using direct IPMI for IPMI paramters\n");
		platform->ipmi = ipmi_open(platform);
		platform->get_ipmi_bootdev = get_ipmi_bootdev_ipmi;
		platform->clear_ipmi_bootdev = clear_ipmi_bootdev_ipmi;
		platform->get_ipmi_boot_mailbox = get_ipmi_boot_mailbox;
		platform->clear_ipmi_boot_mailbox = clear_ipmi_boot_mailbox;
		platform->set_os_boot_sensor = set_ipmi_os_boot_sensor;
	} else if (!stat(sysparams_dir, &statbuf)) {
		pb_debug("platform: using sysparams for IPMI paramters\n");
		platform->get_ipmi_bootdev = get_ipmi_bootdev_sysparams;
		platform->clear_ipmi_bootdev = clear_ipmi_bootdev_sysparams;

	} else {
		pb_log("platform: no IPMI parameter support\n");
	}

	if (bmc_present)
		platform->get_platform_versions = hostboot_load_versions;

	return true;
}


static struct platform platform_powerpc = {
	.name			= "powerpc",
	.dhcp_arch_id		= 0x000e,
	.probe			= probe,
	.load_config		= load_config,
	.save_config		= save_config,
	.pre_boot		= pre_boot,
	.get_sysinfo		= get_sysinfo,
	.restrict_clients	= restrict_clients,
	.set_password		= set_password,
};

register_platform(platform_powerpc);
OpenPOWER on IntegriCloud