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