blob: 7c9998ed00bd9286ba50591cba78d18a5f0a7d1c [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdint.h>
Olivier Baillyb93e5812010-11-17 11:47:23 -08005#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08006#include <fcntl.h>
7#include <sys/ioctl.h>
8#include <errno.h>
9
10int hd_main(int argc, char *argv[])
11{
12 int c;
13 int fd;
14 unsigned char buf[4096];
15 int res;
16 int read_len;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017 int i;
18 int filepos = 0;
19 int sum;
20 int lsum;
21
22 int base = -1;
23 int count = 0;
24 int repeat = 0;
25
26 do {
27 c = getopt(argc, argv, "b:c:r:");
28 if (c == EOF)
29 break;
30 switch (c) {
31 case 'b':
32 base = strtol(optarg, NULL, 0);
33 break;
34 case 'c':
35 count = strtol(optarg, NULL, 0);
36 break;
37 case 'r':
38 repeat = strtol(optarg, NULL, 0);
39 break;
40 case '?':
41 fprintf(stderr, "%s: invalid option -%c\n",
42 argv[0], optopt);
43 exit(1);
44 }
45 } while (1);
46
47 if (optind + 1 != argc) {
48 fprintf(stderr, "Usage: %s [-b base] [-c count] [-r delay] file\n", argv[0]);
49 exit(1);
50 }
51
52 fd = open(argv[optind], O_RDONLY);
53 if(fd < 0) {
54 fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
55 return 1;
56 }
57
58 do {
59 if(base >= 0) {
60 lseek(fd, base, SEEK_SET);
61 filepos = base;
62 }
63 sum = 0;
64 lsum = 0;
65 while(1) {
66 read_len = sizeof(buf);
67 if(count > 0 && base + count - filepos < read_len)
68 read_len = base + count - filepos;
69 res = read(fd, &buf, read_len);
Scott Anderson7e4c3032012-01-12 14:01:43 -080070 if(res == 0)
71 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 for(i = 0; i < res; i++) {
73 if((i & 15) == 0) {
74 printf("%08x: ", filepos + i);
75 }
76 lsum += buf[i];
77 sum += buf[i];
78 printf("%02x ", buf[i]);
79 if(((i & 15) == 15) || (i == res - 1)) {
80 printf("s %x\n", lsum);
81 lsum = 0;
82 }
83 }
Scott Anderson7e4c3032012-01-12 14:01:43 -080084 if(res < 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085 printf("Read error on %s, offset %d len %d, %s\n", argv[optind], filepos, read_len, strerror(errno));
86 return 1;
87 }
88 filepos += res;
89 if(filepos == base + count)
90 break;
91 }
92 printf("sum %x\n", sum);
93 if(repeat)
94 sleep(repeat);
95 } while(repeat);
96 return 0;
97}