| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> | 
|  | 2 | #include <stdlib.h> | 
|  | 3 | #include <string.h> | 
|  | 4 | #include <stdint.h> | 
| Olivier Bailly | b93e581 | 2010-11-17 11:47:23 -0800 | [diff] [blame] | 5 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 6 | #include <fcntl.h> | 
|  | 7 | #include <sys/ioctl.h> | 
|  | 8 | #include <errno.h> | 
|  | 9 |  | 
|  | 10 | int 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | 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 Anderson | 7e4c303 | 2012-01-12 14:01:43 -0800 | [diff] [blame] | 70 | if(res == 0) | 
|  | 71 | break; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | 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 Anderson | 7e4c303 | 2012-01-12 14:01:43 -0800 | [diff] [blame] | 84 | if(res < 0) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | 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 | } |