aboutsummaryrefslogtreecommitdiff
path: root/external/gard/gard.c
blob: ba15e877d1a9beacb9d04d59c9df27b44e5f66c6 (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
/* Copyright 2013-2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <inttypes.h>
#include <ctype.h>

#include <ccan/array_size/array_size.h>

#include <mtd/mtd-abi.h>

#include <getopt.h>

#include <libflash/libflash.h>
#include <libflash/libffs.h>
#include <libflash/file.h>
#include <libflash/blocklevel.h>
#include <common/arch_flash.h>

#include "gard.h"

#define FDT_PATH "/proc/device-tree"
#define FDT_FSP_NODE FDT_PATH"/fsps"
#define FDT_ACTIVE_FLASH_PATH FDT_PATH"/chosen/ibm,system-flash"
#define SYSFS_MTD_PATH "/sys/class/mtd/"
#define FLASH_GARD_PART "GUARD"

#define VPNOR_GARD_DIR "/media/pnor-prsv"
#define VPNOR_GARD_FILE VPNOR_GARD_DIR"/GUARD"

/* Full gard version number (possibly includes gitid). */
extern const char version[];


#define __unused __attribute__((unused))

struct gard_ctx {
	uint32_t f_size;
	uint32_t f_pos;

	uint32_t gard_part_idx;
	uint32_t gard_data_pos;
	uint32_t gard_data_len;

	struct blocklevel_device *bl;
	struct ffs_handle *ffs;
};

static void show_flash_err(int rc)
{
	switch (rc) {
		case FFS_ERR_BAD_MAGIC:
			fprintf(stderr, "libffs bad magic\n");
			break;
		case FFS_ERR_BAD_VERSION:
			fprintf(stderr, "libffs bad version\n");
			break;
		case FFS_ERR_BAD_CKSUM:
			fprintf(stderr, "libffs bad check sum\n");
			break;
		case FFS_ERR_PART_NOT_FOUND:
			fprintf(stderr, "libffs flash partition not found\n");
			break;
			/* ------- */
		case FLASH_ERR_MALLOC_FAILED:
			fprintf(stderr, "libflash malloc failed\n");
			break;
		case FLASH_ERR_CHIP_UNKNOWN:
			fprintf(stderr, "libflash unknown flash chip\n");
			break;
		case FLASH_ERR_PARM_ERROR:
			fprintf(stderr, "libflash parameter error\n");
			break;
		case FLASH_ERR_ERASE_BOUNDARY:
			fprintf(stderr, "libflash erase boundary error\n");
			break;
		case FLASH_ERR_WREN_TIMEOUT:
			fprintf(stderr, "libflash WREN timeout\n");
			break;
		case FLASH_ERR_WIP_TIMEOUT:
			fprintf(stderr, "libflash WIP timeout\n");
			break;
		case FLASH_ERR_VERIFY_FAILURE:
			fprintf(stderr, "libflash verification failure\n");
			break;
		case FLASH_ERR_4B_NOT_SUPPORTED:
			fprintf(stderr, "libflash 4byte mode not supported\n");
			break;
		case FLASH_ERR_CTRL_CONFIG_MISMATCH:
			fprintf(stderr, "libflash control config mismatch\n");
			break;
		case FLASH_ERR_CHIP_ER_NOT_SUPPORTED:
			fprintf(stderr, "libflash chip not supported\n");
			break;
		case FLASH_ERR_CTRL_CMD_UNSUPPORTED:
			fprintf(stderr, "libflash unsupported control command\n");
			break;
		case FLASH_ERR_CTRL_TIMEOUT:
			fprintf(stderr, "libflash control timeout\n");
			break;
		case FLASH_ERR_ECC_INVALID:
			fprintf(stderr, "libflash ecc invalid\n");
			break;
		default:
			fprintf(stderr, "A libflash/libffs error has occurred %d\n", rc);
	}
}

const struct chip_unit_desc *chip_units;
int chip_unit_count;

static void set_chip_gen(const struct chip_unit_desc *c)
{
	chip_units = c;
	chip_unit_count = 0;

	while (strcmp("LAST_IN_RANGE", c->desc)) {
		chip_unit_count++;
		c++;
	}
}

#ifdef __powerpc64__
static void guess_chip_gen(void)
{
	/*
	 * Guesstimate what chip generation based on the PVR if we're running
	 * on ppc64.
	 */
	uint32_t pvr;

	/* grab the chip type from the PVR SPR */
	asm ("mfspr  %0,0x11f" : "=r" (pvr));

	switch (pvr >> 16) {
	case 0x004b: /* murano */
	case 0x004c: /* naples */
	case 0x004d: /* venice */
		set_chip_gen(p8_chip_units);
		return;

	case 0x004e: /* nimbus */
		set_chip_gen(p9_chip_units);
		return;

	default:
		fprintf(stderr, "Unsupported processor (pvr %#x)! Set the processor generation manually with -8 or -9\n", pvr);
		exit(1);
	}
}
#else
static void guess_chip_gen(void)
{
#ifdef ASSUME_P8
	set_chip_gen(p8_chip_units);
#else
	set_chip_gen(p9_chip_units);
#endif
}
#endif

static const char *target_type_to_str(int type)
{
	int i;

	for (i = 0; i < chip_unit_count; i++)
		if (chip_units[i].type == type)
			return chip_units[i].desc;

	return "UNKNOWN";
}

static int str_to_target_type(const char *path)
{
	int i, len;

	for (i = 0; i < chip_unit_count; i++) {
		len = strlen(chip_units[i].desc);

		if (!strncasecmp(chip_units[i].desc, path, len))
			return chip_units[i].type; /* match! */
	}

	return -1;
}

static const char *deconfig_reason_str(enum gard_reason reason)
{
	switch (reason) {
	case GARD_NO_REASON:
		return "None";
	case GARD_MANUAL:
		return "Manual";
	case GARD_UNRECOVERABLE:
		return "Unrecoverable";
	case GARD_FATAL:
		return "Fatal";
	case GARD_PREDICTIVE:
		return "Predictive";
	case GARD_POWER:
		return "Power"; // What does this even mean?
	case GARD_HYP:
		return "Hypervisor";
	case GARD_RECONFIG:
		return "Reconfig";
	default:
		return "Unknown";
	}
};

static const char *path_type_to_str(enum path_type t)
{
	switch (t) {
		case PATH_NA:
			return "not applicable";
		case PATH_AFFINITY:
			return "affinity";
		case PATH_PHYSICAL:
			return "physical";
		case PATH_DEVICE:
			return "device";
		case PATH_POWER:
			return "power";
	}
	return "Unknown";
}

/*
 * NB: buffer is assumped to be MAX_PATH_SIZE
 */
static char *format_path(struct entity_path *path, char *buffer)
{
	int elements = path->type_size & PATH_ELEMENTS_MASK;
	int i, offset = 0;

	for (i = 0; i < elements; i++) {
		const struct path_element *e = &path->path_elements[i];

		offset += sprintf(buffer + offset, "/%s%d",
			target_type_to_str(e->target_type),
			e->instance);
	}

	return buffer;
}

/*
 * parses a Path string into the entity_path structured provided.
 *
 * str    - In param, String to parse
 * parsed - Out param, resultant entity_path
 *
 * e.g.
 *
 * "/Sys0/Node0/Proc1" -> {
 *      type_size = 0x23,
 *
 *      path_element[0] = {0, 0}
 *      path_element[1] = {1, 0}
 *      path_element[2] = {2, 1}
 * }
 */
static int parse_path(const char *str, struct entity_path *parsed)
{
	int unit_count = 0;

	memset(parsed, 0, sizeof(*parsed));

	while (*str != '\0') {
		int unit_id = str_to_target_type(++str); /* ++ skips the '/' */
		long instance;
		char *end;
		size_t len;

		if (unit_count > MAX_PATH_ELEMENTS - 1) {
			fprintf(stderr, "Path has more than 10 components!\n");
			return -1;
		}

		/* find the type Id of this component */
		if (unit_id < 0) { /* unknown unit, bail out */
			fprintf(stderr, "Unknown unit at: '%s'\n", str);
			return -1;
		}

		parsed->path_elements[unit_count].target_type = unit_id;

		/* now parse the instance # */
		len = strlen(target_type_to_str(unit_id));
		instance = strtol(str + len, &end, 10);

		if (!isdigit(*(str + len))) {
			fprintf(stderr, "Missing instance number after '%s'\n",
					str);
			return -1;
		}

		if (*end != '\0' && *end != '/') {
			fprintf(stderr, "Unable to parse instance after '%s'\n",
					str);
			return -1;
		}

		if (instance > 255 || instance < 0) {
			fprintf(stderr,
				"Instance %ld is invalid. Must be 0 to 255\n",
				instance);
			return -1;
		}
		parsed->path_elements[unit_count].instance = instance;

		str = end;
		unit_count++;
	}

	/*
	 * We assume the path is a physical path because every gard record I've
	 * seen so far uses them. We might need to fix this later on, but lets
	 * cross the bridge when we have to.
	 */
	parsed->type_size = (unit_count & 0xf) |
			(PATH_PHYSICAL << PATH_TYPE_SHIFT);

	return 0;
}

static struct gard_record blank_record;

static bool is_valid_record(struct gard_record *g)
{
	return memcmp(&blank_record, g, sizeof(*g));
}

static int do_iterate(struct gard_ctx *ctx,
		int (*func)(struct gard_ctx *ctx, int pos,
			struct gard_record *gard, void *priv),
		void *priv)
{
	int rc = 0;
	unsigned int i;
	struct gard_record gard, null_gard;

	memset(&null_gard, UINT_MAX, sizeof(gard));
	for (i = 0; i * sizeof(gard) < ctx->gard_data_len && rc == 0; i++) {
		memset(&gard, 0, sizeof(gard));

		rc = blocklevel_read(ctx->bl, ctx->gard_data_pos + (i * sizeof(gard)),
					&gard, sizeof(gard));
		/* It isn't super clear what constitutes the end, this should do */
		if (rc || memcmp(&gard, &null_gard, sizeof(gard)) == 0)
			break;

		rc = func(ctx, i, &gard, priv);
	}

	return rc;
}

/*
 * read the next guard record into the supplied buffer (gard)
 *
 * returns the record id (nb: 1 based not zero)
 *
 */
static int __gard_next(struct gard_ctx *ctx, int pos, struct gard_record *gard, int *rc)
{
	uint32_t offset = pos * sizeof(*gard);

	if (offset > ctx->gard_data_len) /* too big */
		return -1;

	/* you lose error handling information, *gruble* */
	memset(gard, 0, sizeof(*gard));
	*rc = blocklevel_read(ctx->bl, ctx->gard_data_pos + offset,
				gard, sizeof(*gard));

	if (!is_valid_record(gard))
		return -1;

	if (*rc)
		return -1;

	return pos;
}

#define for_each_gard(ctx, pos, gard, rc) \
	for (pos = __gard_next(ctx, 0, gard, rc); \
		pos >= 0; pos = __gard_next(ctx, ++pos, gard, rc))

static int count_records(struct gard_ctx *ctx)
{
	struct gard_record record;
	int rc, pos, count = 0;

	for_each_gard(ctx, pos, &record, &rc)
		count++;

	return rc ? rc : count;
}

static int count_valid_records(struct gard_ctx *ctx)
{
	struct gard_record record;
	int rc, pos, count = 0;

	for_each_gard(ctx, pos, &record, &rc)
		count++;

	return rc ? rc : count;
}

static size_t find_longest_path(struct gard_ctx *ctx)
{
	char scratch[MAX_PATH_SIZE];
	struct gard_record gard;
	size_t len, longest = 0;
	int rc, pos;

	for_each_gard(ctx, pos, &gard, &rc) {
		len = strlen(format_path(&gard.target_id, scratch));
		if (len > longest)
			longest = len;
	}

	return longest;
}

static void draw_ruler(char c, int size)
{
	int i;

	for (i = 0; i < size; i++)
		putchar(c);
	putchar('\n');
}

static int do_list(struct gard_ctx *ctx, int argc __attribute__((unused)),
		   char **argv __attribute__((unused)))
{
	/* This header matches the line formatting above in do_list_i() */
	const char *header = " ID       | Error    | Type       | Path";
	size_t ruler_size;
	char scratch[MAX_PATH_SIZE];
	struct gard_record gard;
	int rc = 0, pos;

	/* No entries */
	if (count_valid_records(ctx) == 0) {
		printf("No GARD entries to display\n");
		return 0;
	}

	puts(header);

	ruler_size = strlen(header) + find_longest_path(ctx);
	draw_ruler('-', ruler_size);

	for_each_gard(ctx, pos, &gard, &rc) {
		printf(" %08x | %08x | %-10s | %s%s\n",
			be32toh(gard.record_id),
			be32toh(gard.errlog_eid),
			deconfig_reason_str(gard.error_type),
			format_path(&gard.target_id, scratch),
                        gard.record_id == 0xffffffff ? " *CLEARED*" : "");
	}

	draw_ruler('=', ruler_size);

	return rc;
}

static int do_show_i(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv)
{
	uint32_t id;

	(void)ctx;
	(void)pos;

	if (!priv || !gard)
		return -1;

	id = *(uint32_t *)priv;

	if (be32toh(gard->record_id) == id) {
		unsigned int count, i;

		printf("Record ID:    0x%08x%s\n", id, id == 0xffffffff ? " *CLEARED*" : "");
		printf("========================\n");
		printf("Error ID:     0x%08x\n", be32toh(gard->errlog_eid));
		printf("Error Type:   %s (0x%02x)\n",
			deconfig_reason_str(gard->error_type),
			gard->error_type);
		printf("Path Type: %s\n", path_type_to_str(gard->target_id.type_size >> PATH_TYPE_SHIFT));
		count = gard->target_id.type_size & PATH_ELEMENTS_MASK;
		for (i = 0; i < count && i < MAX_PATH_ELEMENTS; i++)
			printf("%*c%s, Instance #%d\n", i + 1, '>', target_type_to_str(gard->target_id.path_elements[i].target_type),
			       gard->target_id.path_elements[i].instance);
	}

	return 0;
}

static int do_show(struct gard_ctx *ctx, int argc, char **argv)
{
	uint32_t id;
	int rc;

	if (argc != 2) {
		fprintf(stderr, "%s option requires a GARD record\n", argv[0]);
		return -1;
	}

	id = strtoul(argv[1], NULL, 16);

	rc = do_iterate(ctx, &do_show_i, &id);

	return rc;
}

static int do_clear_i(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv)
{
	int largest, rc = 0;
	char *buf;
	struct gard_record null_gard;

	if (!gard || !ctx || !priv)
		return -1;

	/* Not this one */
	if (be32toh(gard->record_id) != *(uint32_t *)priv)
		return 0;

	memset(&null_gard, 0xFF, sizeof(null_gard));

	largest = count_records(ctx);

	printf("Clearing gard record 0x%08x...", be32toh(gard->record_id));

	if (largest < 0 || pos > largest) {
		/* Something went horribly wrong */
		fprintf(stderr, "largest index out of range %d\n", largest);
		return -1;
	}

	if (pos < largest) {
		/* We're not clearing the last record, shift all the records up */
		int buf_len = ((largest - pos) * sizeof(struct gard_record));
		int buf_pos = ctx->gard_data_pos + ((pos + 1) * sizeof(struct gard_record));
		buf = malloc(buf_len);
		if (!buf)
			return -ENOMEM;

		rc = blocklevel_read(ctx->bl, buf_pos, buf, buf_len);
		if (rc) {
			free(buf);
			fprintf(stderr, "Couldn't read from flash at 0x%08x for len 0x%08x\n", buf_pos, buf_len);
			return rc;
		}

		rc = blocklevel_smart_write(ctx->bl, buf_pos - sizeof(gard), buf, buf_len);
		free(buf);
		if (rc) {
			fprintf(stderr, "Couldn't write to flash at 0x%08x for len 0x%08x\n",
					buf_pos - (int) sizeof(struct gard_record), buf_len);
			return rc;
		}
	}

	/* Now wipe the last record */
	rc = blocklevel_smart_write(ctx->bl, ctx->gard_data_pos + (largest * sizeof(null_gard)),
	                            &null_gard, sizeof(null_gard));
	printf("done\n");

	return rc;
}

static int reset_partition(struct gard_ctx *ctx)
{
	int no_ecc_len = (ctx->gard_data_len / 9) * 8;
	struct gard_record *gard;
	int rc = 0;

	gard = malloc(ctx->gard_data_len);
	if (!gard) {
		return FLASH_ERR_MALLOC_FAILED;
	}
	memset(gard, 0xFF, ctx->gard_data_len);

	rc = blocklevel_smart_erase(ctx->bl, ctx->gard_data_pos, ctx->gard_data_len);
	if (rc) {
		fprintf(stderr, "Couldn't erase the gard partition. Bailing out\n");
		goto out;
	}

	rc = blocklevel_write(ctx->bl, ctx->gard_data_pos, gard, no_ecc_len);
	if (rc)
		fprintf(stderr, "Couldn't reset the entire gard partition. Bailing out\n");

out:
	free(gard);
	return rc;
}

static int do_clear(struct gard_ctx *ctx, int argc, char **argv)
{
	int rc;
	uint32_t id;

	if (argc != 2) {
		fprintf(stderr, "%s option requires a GARD record or 'all'\n", argv[0]);
		return -1;
	}

	if (strncmp(argv[1], "all", strlen("all")) == 0) {
		printf("Clearing the entire gard partition...");
		fflush(stdout);
		rc = reset_partition(ctx);
		printf("done\n");
	} else {
		id = strtoul(argv[1], NULL, 16);
		rc = do_iterate(ctx, do_clear_i, &id);
	}

	return rc;
}

static int do_create(struct gard_ctx *ctx, int argc, char **argv)
{
	int rc, pos, max_id = 0, last_pos = 0;
	struct gard_record gard;
	struct entity_path path;

	if (argc < 2) {
		fprintf(stderr, "create requires path to gard\n");
		fprintf(stderr, "e.g.\n");
		fprintf(stderr, "     /Sys0/Node0/Proc0\n");
		fprintf(stderr, "     /Sys0/Node0/DIMM15\n");
		return -1;
	}

	if (parse_path(argv[1], &path)) {
		fprintf(stderr, "Unable to parse path\n");
		return -1;
	}

	/* check if we already have a gard record applied to this path */
	for_each_gard(ctx, pos, &gard, &rc) {
		if (!memcmp(&path, &gard.target_id, sizeof(path))) {
			fprintf(stderr,
				"Unit %s is already GARDed by record %#08x\n",
				argv[1], be32toh(gard.record_id));
			return -1;
		}

		/*
		 * Keep track of the largest record ID seen so far,
		 * we'll give the new record the max + 1 to ensure
		 * that it's unique
		 */
		if (be32toh(gard.record_id) > max_id)
			max_id = be32toh(gard.record_id);

		last_pos++;
	}

	/* do we have an empty record to write into? */
	if (!rc && !is_valid_record(&gard)) {
		int offset = last_pos * sizeof(gard);

		memset(&gard, 0xff, sizeof(gard));

		gard.record_id = be32toh(max_id + 1);
		gard.error_type = GARD_MANUAL;
		gard.target_id = path;
		gard.errlog_eid = 0x0;

		if (offset > ctx->gard_data_len - sizeof(gard)) {
			fprintf(stderr, "No space in GUARD for a new record\n");
			return -1;
		}

		rc = blocklevel_smart_write(ctx->bl,
			ctx->gard_data_pos + offset, &gard, sizeof(gard));
	}

	return rc;
}

static int check_gard_partition(struct gard_ctx *ctx)
{
	int rc;
	struct gard_record gard;
	char msg[2];

	if (ctx->gard_data_len == 0 || ctx->gard_data_len % sizeof(struct gard_record) != 0)
		/* Just warn for now */
		fprintf(stderr, "The %s partition doesn't appear to be an exact multiple of"
				"gard records in size: %zd vs %u (or partition is zero in length)\n",
				FLASH_GARD_PART, sizeof(struct gard_record), ctx->gard_data_len);

	/*
	 * Attempt to read the first record, nothing can really operate if the
	 * first record is dead. There (currently) isn't a way to validate more
	 * than ECC correctness.
	 */
	rc = blocklevel_read(ctx->bl, ctx->gard_data_pos, &gard, sizeof(gard));
	if (rc == FLASH_ERR_ECC_INVALID) {
		fprintf(stderr, "The data at the GUARD partition does not appear to be valid gard data\n");
		fprintf(stderr, "Clear the entire GUARD partition? [y/N]\n");
		if (fgets(msg, sizeof(msg), stdin) == NULL) {
			fprintf(stderr, "Couldn't read from standard input\n");
			return -1;
		}
		if (msg[0] == 'y') {
			rc = reset_partition(ctx);
			if (rc) {
				fprintf(stderr, "Couldn't reset the GUARD partition. Bailing out\n");
				return rc;
			}
		}
		/*
		 * else leave rc as is so that the main bails out, not going to be
		 * able to do sensible anyway
		 */
	}
	return rc;
}

__attribute__ ((unused))
static int do_nop(struct gard_ctx *ctx, int argc, char **argv)
{
	(void)ctx;
	(void)argc;
	fprintf(stderr, "Unimplemented action '%s'\n", argv[0]);
	return EXIT_SUCCESS;
}

struct {
	const char	*name;
	const char	*desc;
	int		(*fn)(struct gard_ctx *, int, char **);
} actions[] = {
	{ "list", "List current GARD records", do_list },
	{ "show", "Show details of a GARD record", do_show },
	{ "clear", "Clear GARD records", do_clear },
	{ "create", "Create a GARD record", do_create },
};

static void print_version(void)
{
	printf("Open-Power GARD tool %s\n", version);
}

static void usage(const char *progname)
{
	unsigned int i;

	print_version();
	fprintf(stderr, "Usage: %s [-a -e -f <file> -p] <command> [<args>]\n\n",
			progname);
	fprintf(stderr, "-8 --p8\n");
	fprintf(stderr, "-9 --p9\n\tSet the processor generation\n\n");
	fprintf(stderr, "-e --ecc\n\tForce reading/writing with ECC bytes.\n\n");
	fprintf(stderr, "-f --file <file>\n\tDon't search for MTD device,"
	                " read from <file>.\n\n");
	fprintf(stderr, "-p --part\n\tUsed in conjunction with -f to specify"
	                " that just\n");
	fprintf(stderr, "\tthe GUARD partition is in <file> and libffs\n");
	fprintf(stderr, "\tshouldn't be used.\n\n");


	fprintf(stderr, "Where <command> is one of:\n\n");

	for (i = 0; i < ARRAY_SIZE(actions); i++) {
		fprintf(stderr,  "\t%-7s\t%s\n",
				actions[i].name, actions[i].desc);
	}
}

static bool is_fsp(void)
{
	return access(FDT_FSP_NODE, F_OK) == 0;
}

static struct option global_options[] = {
	{ "file", required_argument, 0, 'f' },
	{ "part", no_argument, 0, 'p' },
	{ "ecc", no_argument, 0, 'e' },
	{ "p8", no_argument, 0, '8' },
	{ "p9", no_argument, 0, '9' },
	{ 0 },
};
static const char *global_optstring = "+ef:p89";

int main(int argc, char **argv)
{
	const char *action, *progname;
	char *filename = NULL;
	struct gard_ctx _ctx, *ctx;
	uint64_t bl_size;
	int rc, i = 0;
	bool part = 0;
	bool ecc = 0;

	progname = argv[0];

	ctx = &_ctx;
	memset(ctx, 0, sizeof(*ctx));
	memset(&blank_record, 0xff, sizeof(blank_record));

	if (is_fsp()) {
		fprintf(stderr, "This is the OpenPower gard tool which does "
				"not support FSP systems\n");
		return EXIT_FAILURE;
	}

	/* process global options */
	for (;;) {
		int c;

		c = getopt_long(argc, argv, global_optstring, global_options,
				NULL);
		if (c == -1)
			break;
		switch (c) {
		case 'e':
			ecc = true;
			break;
		case 'f':
			/* If they specify -f twice */
			free(filename);

			filename = strdup(optarg);
			if (!filename) {
				fprintf(stderr, "Out of memory\n");
				return EXIT_FAILURE;
			}
			break;
		case 'p':
			part = true;
			break;
		case '8':
			set_chip_gen(p8_chip_units);
			break;
		case '9':
			set_chip_gen(p9_chip_units);
			break;
		case '?':
			usage(progname);
			rc = EXIT_FAILURE;
			goto out_free;
		}
	}



	/*
	 * It doesn't make sense to specify that we have the gard partition but
	 * read from flash
	 */
	if (part && !filename) {
		usage(progname);
		fprintf(stderr, "-p only makes sense when used with -f!\n");
		return EXIT_FAILURE;
	}

	/* do we have a command? */
	if (optind == argc) {
		usage(progname);
		rc = EXIT_FAILURE;
		goto out_free;
	}

	argc -= optind;
	argv += optind;
	action = argv[0];

#ifdef __arm__
	/*
	 * HACK: Look for a vPNOR GUARD file if we haven't been given anything
	 * explitly. If it exists then we can safely assume that:
	 * a) The host is a P9
	 * b) The file is ECC protected
	 * c) The file is a bare partition.
	 *
	 * This is a stupid hack, but there's not other sane place for it.
	 * arch_init_flash() always looks for a FFS formatted PNOR when
	 * filename is NULL
	 */
	if (!filename) {
		struct stat buf;

		if (!stat(VPNOR_GARD_FILE, &buf)) {
			filename = strdup(VPNOR_GARD_FILE);
			/* BUG: This ignores the command line settings */
			part = true;
			ecc = true;
		} else if (!stat(VPNOR_GARD_DIR, &buf)) {
			printf(VPNOR_GARD_FILE" is missing. Nothing to do\n");
			return 0;
		}
	}
#endif

	if (!chip_units)
		guess_chip_gen();

	/*
	 * Force libflash to do flash accesses via the MTD. Direct mode is
	 * generally unsafe since it fiddles with the flash controller state
	 * underneath the kernel. Anyone who needs direct mode can use pflash
	 * instead.
	 */
	arch_flash_access(ctx->bl, PNOR_MTD);

	if (arch_flash_init(&(ctx->bl), filename, true)) {
		/* Can fail for a few ways, most likely couldn't open MTD device */
		fprintf(stderr, "Can't open %s\n", filename ? filename : "MTD Device. Are you root?");
		rc = EXIT_FAILURE;
		goto out_free;
	}

	rc = blocklevel_get_info(ctx->bl, NULL, &bl_size, NULL);
	if (rc)
		goto out;

	if (bl_size > UINT_MAX) {
		fprintf(stderr, "MTD device bigger than %i: size: %" PRIu64 "\n",
			UINT_MAX, bl_size);
		rc = EXIT_FAILURE;
		goto out;
	}
	ctx->f_size = bl_size;

	if (!part) {
		rc = ffs_init(0, ctx->f_size, ctx->bl, &ctx->ffs, 1);
		if (rc)
			goto out;

		rc = ffs_lookup_part(ctx->ffs, FLASH_GARD_PART, &ctx->gard_part_idx);
		if (rc)
			goto out;

		rc = ffs_part_info(ctx->ffs, ctx->gard_part_idx, NULL, &(ctx->gard_data_pos),
				&(ctx->gard_data_len), NULL, NULL);
		if (rc)
			goto out;
	} else {
		if (ecc) {
			rc = blocklevel_ecc_protect(ctx->bl, 0, ctx->f_size);
			if (rc)
				goto out;
		}

		ctx->gard_data_pos = 0;
		ctx->gard_data_len = ctx->f_size;
	}

	rc = check_gard_partition(ctx);
	if (rc) {
		fprintf(stderr, "Does not appear to be sane gard data\n");
		goto out;
	}

	for (i = 0; i < ARRAY_SIZE(actions); i++) {
		if (!strcmp(actions[i].name, action)) {
			rc = actions[i].fn(ctx, argc, argv);
			break;
		}
	}

out:
	if (ctx->ffs)
		ffs_close(ctx->ffs);

	file_exit_close(ctx->bl);

	if (i == ARRAY_SIZE(actions)) {
		fprintf(stderr, "%s: '%s' isn't a valid command\n", progname, action);
		usage(progname);
		rc = EXIT_FAILURE;
		goto out_free;
	}

	if (rc > 0) {
		show_flash_err(rc);
		if (filename && rc == FFS_ERR_BAD_MAGIC)
			fprintf(stderr, "Maybe you didn't give a full flash image file?\nDid you mean '--part'?\n");
	}

out_free:
	free(filename);
	return rc;
}