Blame view

cmd/rng.c 1.1 KB
4f24ac08a   Heinrich Schuchardt   cmd: add rng command
1
2
3
4
5
6
7
8
9
10
  // SPDX-License-Identifier: GPL-2.0+
  /*
   * The 'rng' command prints bytes from the hardware random number generator.
   *
   * Copyright (c) 2019, Heinrich Schuchardt <xypron.glpk@gmx.de>
   */
  #include <common.h>
  #include <command.h>
  #include <dm.h>
  #include <hexdump.h>
336d4615f   Simon Glass   dm: core: Create ...
11
  #include <malloc.h>
4f24ac08a   Heinrich Schuchardt   cmd: add rng command
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
  #include <rng.h>
  
  static int do_rng(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	size_t n = 0x40;
  	struct udevice *dev;
  	void *buf;
  	int ret = CMD_RET_SUCCESS;
  
  	if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
  		printf("No RNG device
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (argc >= 2)
  		n = simple_strtoul(argv[1], NULL, 16);
  
  	buf = malloc(n);
  	if (!buf) {
  		printf("Out of memory
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	if (dm_rng_read(dev, buf, n)) {
  		printf("Reading RNG failed
  ");
  		ret = CMD_RET_FAILURE;
  	} else {
  		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
  	}
  
  	free(buf);
  
  	return ret;
  }
  
  #ifdef CONFIG_SYS_LONGHELP
  static char rng_help_text[] =
  	"[n]
  "
  	"  - print n random bytes
  ";
  #endif
  
  U_BOOT_CMD(
  	rng, 2, 0, do_rng,
  	"print bytes from the hardware random number generator",
  	rng_help_text
  );