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
|
/* Copyright 2013-2015 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 <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/ecc.h>
#include <libflash/blocklevel.h>
#include <common/arch_flash.h>
#include "gard.h"
#define CLEARED_RECORD_ID 0xFFFFFFFF
#define FDT_ACTIVE_FLASH_PATH "/proc/device-tree/chosen/ibm,system-flash"
#define SYSFS_MTD_PATH "/sys/class/mtd/"
#define FLASH_GARD_PART "GUARD"
/* Full gard version number (possibly includes gitid). */
extern const char version[];
struct gard_ctx {
bool ecc;
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;
};
/*
* Return the size of a struct gard_ctx depending on if the buffer contains
* ECC bits
*/
static inline size_t sizeof_gard(struct gard_ctx *ctx)
{
return ctx->ecc ? ecc_buffer_size(sizeof(struct gard_record)) : sizeof(struct gard_record);
}
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);
}
}
static const char *target_type_to_str(enum target_type t)
{
switch (t) {
case TYPE_NA:
return "Not applicable";
case TYPE_SYS:
return "System";
case TYPE_NODE:
return "Node";
case TYPE_DIMM:
return "Dimm";
case TYPE_MEMBUF:
return "Memory Buffer";
case TYPE_PROC:
return "Processor";
case TYPE_EX:
return "EX";
case TYPE_CORE:
return "Core";
case TYPE_L2:
return "L2 cache";
case TYPE_L3:
return "L3 cache";
case TYPE_L4:
return "L4 cache";
case TYPE_MCS:
return "MSC";
case TYPE_MBA:
return "MBA";
case TYPE_XBUS:
return "XBUS";
case TYPE_ABUS:
return "ABUS";
case TYPE_PCI:
return "PCI";
case TYPE_DPSS:
return "DPSS";
case TYPE_APSS:
return "APSS";
case TYPE_OCC:
return "OCC";
case TYPE_PSI:
return "PSI";
case TYPE_FSP:
return "FSP";
case TYPE_PNOR:
return "PNOR";
case TYPE_OSC:
return "OSC";
case TYPE_TODCLK:
return "Time of day clock";
case TYPE_CONTROL_NODE:
return "Control Node";
case TYPE_OSCREFCLK:
return "OSC Ref Clock";
case TYPE_OSCPCICLK:
return "OSC PCI Clock";
case TYPE_REFCLKENDPT:
return "Ref Clock";
case TYPE_PCICLKENDPT:
return "PCI Clock";
case TYPE_NX:
return "NX";
case TYPE_PORE:
return "PORE";
case TYPE_PCIESWITCH:
return "PCIE Switch";
case TYPE_CAPP:
return "CAPP";
case TYPE_FSI:
return "FSI";
case TYPE_TEST_FAIL:
return "Test Fail";
case TYPE_LAST_IN_RANGE:
return "Last";
}
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";
}
static bool is_valid_id(uint32_t record_id)
{
return record_id != CLEARED_RECORD_ID;
}
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) < ctx->gard_data_len && rc == 0; i++) {
memset(&gard, 0, sizeof(gard));
rc = blocklevel_read(ctx->bl, ctx->gard_data_pos +
(i * sizeof_gard(ctx)), &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;
}
static int get_largest_pos_i(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv)
{
if (!priv)
return -1;
*(int *)priv = pos;
return 0;
}
static int get_largest_pos(struct gard_ctx *ctx)
{
int rc, largest = -1;
rc = do_iterate(ctx, &get_largest_pos_i, &largest);
if (rc)
return -1;
return largest;
}
static int count_valid_records_i(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv)
{
if (!gard || !priv)
return -1;
if (is_valid_id(be32toh(gard->record_id)))
(*(int *)priv)++;
return 0;
}
static int count_valid_records(struct gard_ctx *ctx)
{
int rc, count = 0;
rc = do_iterate(ctx, &count_valid_records_i, &count);
if (rc)
return 0;
return count;
}
static int do_list_i(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv)
{
if (!gard)
return -1;
if (is_valid_id(be32toh(gard->record_id)))
printf("| %08x | %08x | %-15s |\n", be32toh(gard->record_id), be32toh(gard->errlog_eid),
path_type_to_str(gard->target_id.type_size >> PATH_TYPE_SHIFT));
return 0;
}
static int do_list(struct gard_ctx *ctx, int argc, char **argv)
{
int rc;
/* No entries */
if (count_valid_records(ctx) == 0) {
printf("No GARD entries to display\n");
rc = 0;
} else {
printf("| ID | Error | Type |\n");
printf("+---------------------------------------+\n");
rc = do_iterate(ctx, &do_list_i, NULL);
printf("+=======================================+\n");
}
return rc;
}
static int do_show_i(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv)
{
uint32_t id;
if (!priv || !gard)
return -1;
id = *(uint32_t *)priv;
if (be32toh(gard->record_id) == id) {
unsigned int count, i;
printf("Record ID: 0x%08x\n", id);
printf("========================\n");
printf("Error ID: 0x%08x\n", be32toh(gard->errlog_eid));
printf("Error Type: 0x%02x\n", gard->error_type);
printf("Res Recovery: 0x%02x\n", gard->resource_recovery);
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 = get_largest_pos(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_gard(ctx));
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(ctx), 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_gard(ctx), buf_len);
return rc;
}
}
/* Now wipe the last record */
rc = blocklevel_smart_write(ctx->bl, ctx->gard_data_pos + (largest * sizeof_gard(ctx)),
&null_gard, sizeof(null_gard));
printf("done\n");
return rc;
}
static int reset_partition(struct gard_ctx *ctx)
{
int i, rc;
struct gard_record gard;
memset(&gard, 0xFF, sizeof(gard));
rc = blocklevel_erase(ctx->bl, ctx->gard_data_pos, ctx->gard_data_len);
if (rc) {
fprintf(stderr, "Couldn't erase the gard partition. Bailing out\n");
return rc;
}
for (i = 0; i + sizeof_gard(ctx) < ctx->gard_data_len; i += sizeof_gard(ctx)) {
rc = blocklevel_write(ctx->bl, ctx->gard_data_pos + i, &gard, sizeof(gard));
if (rc) {
fprintf(stderr, "Couldn't reset the entire gard partition. Bailing out\n");
return rc;
}
}
return 0;
}
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;
}
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)
{
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 },
};
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, "-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 struct option global_options[] = {
{ "file", required_argument, 0, 'f' },
{ "part", no_argument, 0, 'p' },
{ "ecc", no_argument, 0, 'e' },
{ 0 },
};
static const char *global_optstring = "+ef:p";
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));
/* 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 '?':
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);
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];
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:%lu\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, &(ctx->ecc));
if (rc)
goto out;
} else {
if (ecc) {
rc = blocklevel_ecc_protect(ctx->bl, 0, ctx->f_size);
if (rc)
goto out;
}
ctx->ecc = ecc;
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;
}
|