summaryrefslogtreecommitdiffstats
path: root/cpukit/libmisc/rtems-fdt/rtems-fdt-shell.c
blob: 9412b37750affe9a96f7cc77dbac1090e169de04 (plain) (blame)
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
/*
 *  COPYRIGHT (c) 2013-2017 Chris Johns <chrisj@rtems.org>
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */
/**
 * @file
 *
 * @ingroup rtems_fdt
 *
 * @brief RTEMS Flattened Device Tree Shell Command
 *
 * Command to play with the memory in a FDT.
 */

#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include <rtems/shell.h>
#include <rtems/rtems-fdt-shell.h>

/**
 * The type of the shell handlers we have.
 */
typedef int (*rtems_fdt_shell_handler) (int argc, char *argv[]);

/**
 * Table of handlers we parse to invoke the command.
 */
typedef struct
{
  const char*             name;    /**< The sub-command's name. */
  rtems_fdt_shell_handler handler; /**< The sub-command's handler. */
  const char*             help;    /**< The sub-command's help. */
} rtems_fdt_shell_cmd;

/**
 * The timeout for the test loop in seconds.
 */
static long rtems_fdt_test_timeout = 5;

/**
 * The FDT handle. Only one user of these command a time.
 */
static rtems_fdt_handle cmd_fdt_handle;

static void
rtems_fdt_write (uint32_t address, uint32_t value)
{
  volatile uint32_t* ap = (uint32_t*) address;
  *ap = value;
}

static uint32_t
rtems_fdt_read (uint32_t address)
{
  volatile uint32_t* ap = (uint32_t*) address;
  return *ap;
}

static int
rtems_fdt_wrong_number_of_args (void)
{
  printf ("error: wrong number of arguments\n");
  return 1;
}

static int
rtems_fdt_invalid_args (const char* arg)
{
  printf ("error: invalid argument: %s\n", arg);
  return 1;
}

static int
rtems_fdt_extra_args (const char* arg)
{
  printf ("error: extra argument is invalid: %s\n", arg);
  return 1;
}

static int
rtems_fdt_check_error (int errval, const char* message, const char* path)
{
  if (errval < 0)
  {
    if (path)
      printf ("error: %s: %s: (%d) %s\n",
              message, path, errval, rtems_fdt_strerror (errval));
    else
      printf ("error: %s: (%d) %s\n",
              message, errval, rtems_fdt_strerror (errval));
    return 1;
  }
  return 0;
}

static bool
rtems_fdt_get_value32 (const char* path,
                       const char* property,
                       size_t      size,
                       uint32_t*   value)
{
  const void* prop;
  int         node;
  int         length;

  node = rtems_fdt_path_offset(&cmd_fdt_handle, path);
  if (node < 0)
  {
    rtems_fdt_check_error (node, "path lookup", path);
    return false;
  }

  prop = rtems_fdt_getprop(&cmd_fdt_handle, node, property, &length);
  if (length < 0)
  {
    rtems_fdt_check_error (length, "get property", path);
    return false;
  }

  if (length != sizeof (uint32_t))
  {
    printf ("error: property is not sizeof(uint32_t): %s\n", path);
    return false;
  }

  *value = rtems_fdt_get_uint32 (prop);

  return true;
}

static int
rtems_fdt_shell_ld (int argc, char *argv[])
{
  if (argc != 2)
    return rtems_fdt_wrong_number_of_args ();

  return rtems_fdt_check_error (rtems_fdt_load (argv[1], &cmd_fdt_handle),
                                "loading FTB", argv[1]);
}

static int
rtems_fdt_shell_uld (int argc, char *argv[])
{
  if (argc != 2)
    return rtems_fdt_wrong_number_of_args ();

  return rtems_fdt_check_error (rtems_fdt_unload (&cmd_fdt_handle),
                                "unloading FTB", argv[1]);
}

static int
rtems_fdt_shell_ls (int argc, char *argv[])
{
  char*  path = NULL;
  bool   recursive = false;
  bool   long_path = false;
  bool   debug = false;
  int    arg = 1;
  size_t path_len = 0;
  int    num_entries = 0;
  int    i = 0;

  while (arg < argc)
  {
    if (argv[arg][0] == '-')
    {
      if (argv[arg][2] != 0)
        return rtems_fdt_invalid_args (argv[arg]);

      switch (argv[arg][1])
      {
        case 'l':
          long_path = true;
          break;
        case 'r':
          recursive = true;
          break;
        case 'd':
          debug = true;
          break;
        default:
          return rtems_fdt_invalid_args (argv[arg]);
      }
    }
    else
    {
      if (path)
        return rtems_fdt_extra_args (argv[arg]);
      if (strcmp (argv[arg], "/") != 0)
        path = argv[arg];
    }
    ++arg;
  }

  if (!path)
  {
    path = "";
  }

  /* Eliminate trailing slashes. */
  path_len = strlen (path);

  if (path_len > 0 && path[path_len - 1] == '/')
      path_len--;

  /* Loop through the entries, looking for matches. */
  num_entries = rtems_fdt_num_entries(&cmd_fdt_handle);
  printf("Total: %d\n", num_entries);
  for (i = 0; i < num_entries; i++)
  {
    /* Add it to the result set. */
    const char *name = rtems_fdt_entry_name(&cmd_fdt_handle, i);
    size_t name_len = strlen(name);

    if ((name_len > path_len) &&
        ((strncmp (path, name, path_len) == 0) && (name[path_len] == '/')) &&
        (recursive || (index(&name[path_len+1], '/') == 0)))
    {
      if (long_path)
      {
        printf ("%s", name);
      }
      else if (name_len != path_len)
      {
        printf ("%s", &name[path_len + 1]);
      }

      if (debug)
      {
        /* Get properties if we're in debug mode. */
        int proplen = 0;
        int offset = rtems_fdt_entry_offset(&cmd_fdt_handle, i);
        const void *prop = rtems_fdt_getprop(&cmd_fdt_handle, offset, "reg", &proplen);
        const void *prop2 = rtems_fdt_getprop(&cmd_fdt_handle, offset, "mask", &proplen);

        if (prop)
        {
            printf(" addr 0x%08" PRIx32, *(uint32_t *)prop);
        }

        proplen = 0;
        if (prop2)
        {
            printf(" mask 0x%08" PRIx32, *(uint32_t *)prop2);
        }
      }

      printf("\n");
    }
  }

  return 0;
}

static int
rtems_fdt_shell_wr (int argc, char *argv[])
{
  uint32_t address;
  uint32_t offset = 0;
  uint32_t value;

  if ((argc < 3) || (argc > 4))
    return rtems_fdt_wrong_number_of_args ();

  if (argc == 3)
  {
    value = strtoul (argv[2], 0, 0);
  }
  else
  {
    offset = strtoul (argv[2], 0, 0);
    value = strtoul (argv[3], 0, 0);
  }

  if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
    return 1;

  address += offset;

  printf ("0x%08" PRIx32 " <= 0x%08" PRIx32 "\n", address, value);

  rtems_fdt_write (address, value);

  return 0;
}

static int
rtems_fdt_shell_rd (int argc, char *argv[])
{
  uint32_t address;
  uint32_t offset = 0;

  if ((argc < 1) || (argc > 3))
    return rtems_fdt_wrong_number_of_args ();

  if (argc == 3)
    offset = strtoul (argv[2], 0, 0);

  if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
    return 1;

  address += offset;

  printf ("0x%08" PRIx32 " => 0x%08" PRIx32 "\n", address, rtems_fdt_read (address));

  return 0;
}

static int
rtems_fdt_shell_set (int argc, char *argv[])
{
  uint32_t address;
  uint32_t offset = 0;
  uint32_t value;
  int      mask_arg;
  uint32_t mask;

  if ((argc < 3) || (argc > 4))
    return rtems_fdt_wrong_number_of_args ();

  if (argc == 3)
    mask_arg = 2;
  else
  {
    offset = strtoul (argv[2], 0, 0);
    mask_arg = 3;
  }

  if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
    return 1;

  if (isdigit (argv[mask_arg][0]))
    mask = strtoul (argv[mask_arg], 0, 0);
  else
  {
    if (!rtems_fdt_get_value32 (argv[mask_arg], "mask", sizeof (uint32_t), &mask))
      return 1;
  }

  address += offset;
  value = rtems_fdt_read (address);

  printf ("0x%08" PRIx32 " <= 0x%08" PRIx32 " = 0x%08" PRIx32 " | 0x%08" PRIx32 "\n",
          address, value | mask, value, mask);

  rtems_fdt_write (address, value | mask);

  return 0;
}

static int
rtems_fdt_shell_cl (int argc, char *argv[])
{
  uint32_t address;
  uint32_t offset = 0;
  uint32_t value;
  int      mask_arg;
  uint32_t mask;

  if ((argc < 3) || (argc > 4))
    return rtems_fdt_wrong_number_of_args ();

  if (argc == 3)
    mask_arg = 2;
  else
  {
    offset = strtoul (argv[2], 0, 0);
    mask_arg = 3;
  }

  if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
    return 1;

  if (isdigit (argv[mask_arg][0]))
    mask = strtoul (argv[mask_arg], 0, 0);
  else
  {
    if (!rtems_fdt_get_value32 (argv[mask_arg], "mask", sizeof (uint32_t), &mask))
      return 1;
  }

  address += offset;
  value = rtems_fdt_read (address);

  printf ("0x%08" PRIx32 " <= 0x%08" PRIx32 " = 0x%08" PRIx32 \
          " & ~0x%08" PRIx32 " (0x%08" PRIx32 ")\n",
          address, value & ~mask, value, mask, ~mask);

  rtems_fdt_write (address, value & ~mask);

  return 0;
}

static int
rtems_fdt_shell_up (int argc, char *argv[])
{
  uint32_t address;
  uint32_t offset = 0;
  uint32_t set;
  uint32_t value;
  int      mask_arg;
  uint32_t mask;

  if ((argc < 4) || (argc > 5))
    return rtems_fdt_wrong_number_of_args ();

  if (argc == 4)
    mask_arg = 2;
  else
  {
    offset = strtoul (argv[2], 0, 0);
    mask_arg = 3;
  }

  set = strtoul (argv[mask_arg + 1], 0, 0);

  if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
    return 1;

  if (isdigit (argv[mask_arg][0]))
    mask = strtoul (argv[mask_arg], 0, 0);
  else
  {
    if (!rtems_fdt_get_value32 (argv[mask_arg], "mask", sizeof (uint32_t), &mask))
      return 1;
  }

  address += offset;
  value = rtems_fdt_read (address);

  printf ("0x%08" PRIx32 " <= 0x%08" PRIx32 " = (0x%08" PRIx32 \
          " & ~0x%08" PRIx32 " (0x%08" PRIx32 ")) | 0x%08" PRIx32 "\n",
          address, (value & ~mask) | set, value, mask, ~mask, set);

  rtems_fdt_write (address, (value & ~mask) | set);

  return 0;
}

static int
rtems_fdt_shell_tst (int argc, char *argv[])
{
  uint32_t address;
  uint32_t offset = 0;
  uint32_t test;
  uint32_t value = 0;
  int      mask_arg;
  uint32_t mask;
  time_t   start;

  if ((argc < 4) || (argc > 5))
    return rtems_fdt_wrong_number_of_args ();

  if (argc == 4)
    mask_arg = 2;
  else
  {
    offset = strtoul (argv[2], 0, 0);
    mask_arg = 3;
  }

  test = strtoul (argv[mask_arg + 1], 0, 0);

  if (!rtems_fdt_get_value32 (argv[1], "reg", sizeof (uint32_t), &address))
    return 1;

  if (isdigit (argv[mask_arg][0]))
    mask = strtoul (argv[mask_arg], 0, 0);
  else
  {
     if (!rtems_fdt_get_value32 (argv[mask_arg], "mask", sizeof (uint32_t), &mask))
      return 1;
  }

  address += offset;

  start = time (NULL);

  printf ("0x%08" PRIx32 " => (value & 0x%08" PRIx32 ") == 0x%08" PRIx32 \
          " for %" PRIu32 " seconds\n",
          address, mask, test, rtems_fdt_test_timeout);

  while ((time (NULL) - start) < rtems_fdt_test_timeout)
  {
    int i;
    for (i = 0; i < 10000; ++i)
    {
      value = rtems_fdt_read (address);
      if ((value & mask) == test)
        return 0;
    }
  }

  printf ("0x%08" PRIx32 " => 0x%08" PRIx32 ": timeout\n", address, value);

  return 1;
}

static int
rtems_fdt_shell_nap (int argc, char *argv[])
{
  uint32_t time;

  if (argc != 2)
    return rtems_fdt_wrong_number_of_args ();

  time = strtoul (argv[1], 0, 0);

  if (time == 0)
  {
    printf ("error: 0 is not a valid time; check you have a valid number.\n");
    return 1;
  }

  usleep (time * 1000);

  return 0;
}

static int
rtems_fdt_shell_to (int argc, char *argv[])
{
  uint32_t to;

  if (argc == 1)
  {
    printf ("timeout: %" PRIu32 " seconds\n", rtems_fdt_test_timeout);
    return 0;
  }

  if (argc != 2)
    return rtems_fdt_wrong_number_of_args ();

  to = strtoul (argv[1], 0, 0);

  if (to == 0)
  {
    printf ("error: 0 is not a valid timeout; check you have a number.\n");
    return 1;
  }

  rtems_fdt_test_timeout = to;

  return 0;
}

static void
rtems_fdt_shell_usage (const char* arg)
{
  printf ("%s: FDT Help\n", arg);
  printf ("  %s [-hl] <command>\n", arg);
  printf ("   where:\n");
  printf ("     command: The FDT subcommand. See -l for a list plus help.\n");
  printf ("     -h:      This help\n");
  printf ("     -l:      The command list.\n");
}

static const rtems_fdt_shell_cmd table[] =
{
  { "ld",  rtems_fdt_shell_ld,  "<filename> : Load a FDT blob" },
  { "uld", rtems_fdt_shell_uld, "Uload an FDT blob" },
  { "ls",  rtems_fdt_shell_ls,  "<path> : List the nodes at the path and optionally below" },
  { "wr",  rtems_fdt_shell_wr,  "<path> [<offset>] <value> : Write the value." },
  { "rd",  rtems_fdt_shell_rd,  "<path> [<offset>] : Read the value." },
  { "set", rtems_fdt_shell_set, "<path> [<offset>] <mask> : Set the mask bits" },
  { "cl",  rtems_fdt_shell_cl,  "<path> [<offset>] <mask> : Clear the mask bits." },
  { "up",  rtems_fdt_shell_up,  "<path> [<offset>] <mask> <value> : Update the mask bit with value" },
  { "tst", rtems_fdt_shell_tst, "<path> [<offset>] <mask> <value> : Testing loop for masked value." },
  { "nap", rtems_fdt_shell_nap, "<time> : Sleep for the time period. It is in milli-seconds." },
  { "to",  rtems_fdt_shell_to,  "<value> : Set the test timeout (seconds)" },
};

#define RTEMS_FDT_COMMANDS (sizeof (table) / sizeof (const rtems_fdt_shell_cmd))

static int
rtems_fdt_shell_command (int argc, char* argv[])
{
  int    arg;
  size_t t;

  for (arg = 1; arg < argc; arg++)
  {
    if (argv[arg][0] != '-')
      break;

    switch (argv[arg][1])
    {
      case 'h':
        rtems_fdt_shell_usage (argv[0]);
        return 0;
      case 'l':
        printf ("%s: commands are:\n", argv[0]);
        for (t = 0; t < RTEMS_FDT_COMMANDS; ++t)
          printf ("  %-3s %s\n", table[t].name, table[t].help);
        return 0;
      default:
        printf ("error: unknown option: %s\n", argv[arg]);
        return 1;
    }
  }

  if ((argc - arg) < 1)
    printf ("error: you need to provide a command, try %s -h\n", argv[0]);
  else
  {
    for (t = 0; t < RTEMS_FDT_COMMANDS; ++t)
    {
      if (strncmp (argv[arg], table[t].name, strlen (argv[arg])) == 0)
      {
        int r = table[t].handler (argc - arg, argv + 1);
        return r;
      }
    }
    printf ("error: command not found: %s (try -h)\n", argv[arg]);
  }

  return 1;
}

void
rtems_fdt_add_shell_command(void)
{
  rtems_shell_add_cmd ("fdt", "mem",
                       "Flattened device tree", rtems_fdt_shell_command);
}

rtems_fdt_handle*
rtems_fdt_get_shell_handle (void)
{
  return &cmd_fdt_handle;
}