Coverage Report

Created: 2026-10-13 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/commands/crc.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
// SPDX-FileCopyrightText: © 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
3
4
/* crc.c - Calculate a crc32 checksum of a memory area */
5
6
#include <common.h>
7
#include <command.h>
8
#include <fs.h>
9
#include <crc.h>
10
#include <getopt.h>
11
#include <malloc.h>
12
#include <libfile.h>
13
#include <environment.h>
14
15
static int crc_from_file(const char* file, ulong *crc)
16
0
{
17
0
  char * buf;
18
19
0
  buf= read_file(file, NULL);
20
21
0
  if (!buf)
22
0
    return -ENOMEM;
23
24
0
  *crc = simple_strtoul(buf, NULL, 16);
25
0
  return 0;
26
0
}
27
28
static int do_crc(int argc, char *argv[])
29
0
{
30
0
  loff_t start = 0, size = ~0;
31
0
  ulong crc = 0, vcrc = 0, total = 0;
32
0
  char *filename = "/dev/mem";
33
0
#ifdef CONFIG_CMD_CRC_CMP
34
0
  char *vfilename = NULL;
35
0
#endif
36
0
  char *crcvarname = NULL, *sizevarname = NULL;
37
0
  int opt, err = 0, filegiven = 0, verify = 0;
38
39
0
  while((opt = getopt(argc, argv, "f:F:v:V:r:s:")) > 0) {
40
0
    switch(opt) {
41
0
    case 'f':
42
0
      filename = optarg;
43
0
      filegiven = 1;
44
0
      break;
45
0
#ifdef CONFIG_CMD_CRC_CMP
46
0
    case 'F':
47
0
      verify = 1;
48
0
      vfilename = optarg;
49
0
      break;
50
0
#endif
51
0
    case 'r':
52
0
      crcvarname = optarg;
53
0
      break;
54
0
    case 's':
55
0
      sizevarname = optarg;
56
0
      break;
57
0
    case 'v':
58
0
      verify = 1;
59
0
      vcrc = simple_strtoul(optarg, NULL, 0);
60
0
      break;
61
0
    case 'V':
62
0
      if (!crc_from_file(optarg, &vcrc))
63
0
        verify = 1;
64
0
      break;
65
0
    default:
66
0
      return COMMAND_ERROR_USAGE;
67
0
    }
68
0
  }
69
70
0
  if (!filegiven && optind == argc)
71
0
    return COMMAND_ERROR_USAGE;
72
73
0
  if (optind < argc) {
74
0
    if (parse_area_spec(argv[optind], &start, &size)) {
75
0
      printf("could not parse area description: %s\n", argv[optind]);
76
0
      return 1;
77
0
    }
78
0
  }
79
80
0
  if (file_crc(filename, start, size, &crc, &total) < 0)
81
0
    return 1;
82
83
0
  printf("CRC32 for %s 0x%08lx ... 0x%08lx ==> 0x%08lx",
84
0
      filename, (ulong)start, (ulong)start + total - 1, crc);
85
86
0
  if (crcvarname)
87
0
    pr_setenv(crcvarname, "0x%lx", crc);
88
89
0
  if (sizevarname)
90
0
    pr_setenv(sizevarname, "0x%lx", total);
91
92
0
#ifdef CONFIG_CMD_CRC_CMP
93
0
  if (vfilename) {
94
0
    size = total;
95
0
    puts("\n");
96
0
    if (file_crc(vfilename, start, size, &vcrc, &total) < 0)
97
0
      return 1;
98
0
  }
99
0
#endif
100
101
0
  if (verify && crc != vcrc) {
102
0
    printf(" != 0x%08lx ** ERROR **", vcrc);
103
0
    err = 1;
104
0
  }
105
106
0
  printf("\n");
107
108
0
  return err;
109
0
}
110
111
BAREBOX_CMD_HELP_START(crc32)
112
BAREBOX_CMD_HELP_TEXT("Calculate a CRC32 checksum of a memory area.")
113
BAREBOX_CMD_HELP_TEXT("")
114
BAREBOX_CMD_HELP_TEXT("Options:")
115
BAREBOX_CMD_HELP_OPT ("-f FILE", "Use file instead of memory.")
116
#ifdef CONFIG_CMD_CRC_CMP
117
BAREBOX_CMD_HELP_OPT ("-F FILE", "Use file to compare.")
118
#endif
119
BAREBOX_CMD_HELP_OPT ("-v CRC",  "Verify")
120
BAREBOX_CMD_HELP_OPT ("-V FILE", "Verify with CRC read from FILE")
121
BAREBOX_CMD_HELP_OPT  ("-r <var>",  "Set <var> to the checksum result")
122
BAREBOX_CMD_HELP_OPT  ("-s <var>",  "Set <var> to the data size")
123
BAREBOX_CMD_HELP_END
124
125
BAREBOX_CMD_START(crc32)
126
  .cmd    = do_crc,
127
  BAREBOX_CMD_DESC("CRC32 checksum calculation")
128
  BAREBOX_CMD_OPTS("[-f"
129
#ifdef CONFIG_CMD_CRC_CMP
130
            "F"
131
#endif
132
            "vV] AREA")
133
  BAREBOX_CMD_GROUP(CMD_GRP_MEM)
134
  BAREBOX_CMD_HELP(cmd_crc32_help)
135
BAREBOX_CMD_END