2 * small utility to read from atapi and scsi devices
10 #include <sys/ioctl.h>
13 #include <linux/cdrom.h>
15 #define DISK "/dev/sda"
17 #define MAX_XFER (65535)
19 #define ALIGN(buf) (char *) (((unsigned long) (buf) + 4095) & ~(4095))
21 static char device[128];
22 static char output[128];
23 static char rate_output[128];
25 static int block_size;
26 static unsigned int blocks;
27 static unsigned long nr_blocks;
28 static int write_output;
29 static unsigned long start_lba;
30 static int force_unaligned;
31 static int write_rate;
33 int parse_options(int argc, char *argv[])
40 c = getopt(argc, argv, "d:n:o:s:p:ur:");
46 strcpy(device, optarg);
49 blocks = strtol(optarg, NULL, 10);
52 strcpy(output, optarg);
56 nr_blocks = strtol(optarg, NULL, 10);
59 start_lba = strtol(optarg, NULL, 10);
65 strcpy(rate_output, optarg);
69 printf("%s: -d device [-s # blocks] [-n # blocks per cmd] [-o output file] [-p start lba] [-u force unaligned buffer]\n", argv[0]);
77 int get_capacity(int fd, int *bs, unsigned long *lba)
79 struct sg_io_hdr *hdr = malloc(sizeof(*hdr));
80 unsigned char buf[8], cdb[16];
84 memset(hdr, 0, sizeof(*hdr));
85 memset(cdb, 0, sizeof(cdb));
86 memset(buf, 0, sizeof(buf));
88 hdr->interface_id = 'S';
89 hdr->cmd_len = sizeof(cdb);
90 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
92 hdr->dxfer_len = sizeof(buf);
95 hdr->cmdp[0] = GPCMD_READ_CDVD_CAPACITY;
97 if (ioctl(fd, SG_IO, hdr) < 0) {
98 perror("read capacity\n");
99 printf("sense key: %x\n", hdr->status);
103 *lba = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
107 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
111 unsigned long time_elapsed(struct timeval *start, struct timeval *end)
113 int seconds = end->tv_sec - start->tv_sec;
114 int useconds = end->tv_usec - start->tv_usec;
116 return (1000 * seconds) + (useconds / 1000);
119 void print_datarate(unsigned long sectors_since_last, int time_spent)
124 printf("disk %s: data rate %luKib/sec\r", device, 1000 * (sectors_since_last * block_size / 1024) / time_spent);
127 void write_datarate(int fd, unsigned long lba, unsigned long sectors, int time)
134 memset(buffer, 0, sizeof(buffer));
135 snprintf(buffer, sizeof(buffer), "%lu, %lu\n", lba, (sectors * block_size) / time);
136 write(fd, buffer, strlen(buffer));
139 int write_the_crap(int fd_out, char *buffer, int length)
143 ret = write(fd_out, buffer, length);
145 printf("\nwanted to write %u, wrote %d\n", length, ret);
150 void hdr_init(struct sg_io_hdr *hdr, char *p, unsigned char *cdb)
152 memset(hdr, 0, sizeof(*hdr));
155 memset(hdr->cmdp, 0, 10);
157 hdr->interface_id = 'S';
159 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
161 hdr->cmdp[0] = GPCMD_READ_10;
164 int main(int argc, char *argv[])
166 struct sg_io_hdr *hdr;
167 unsigned long lba, bytes;
169 int fd, fd_out, fd_rate;
170 unsigned long time_spent;
171 unsigned long sectors_since_last, end_lba;
172 struct timeval start_time, end_time;
173 unsigned char cdb[16];
175 if (parse_options(argc, argv))
178 fd = open(device, O_RDONLY | O_NONBLOCK);
180 perror("open input");
186 fd_out = open(output, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
188 perror("open output");
195 fd_rate = open(rate_output, O_WRONLY | O_CREAT | O_TRUNC, 0644);
197 perror("open rate output");
202 if (get_capacity(fd, &block_size, &end_lba)) {
203 printf("%s: can't retrieve capacity\n", device);
208 blocks = MAX_XFER / block_size;
210 hdr = malloc(sizeof(*hdr));
212 ptr = malloc(blocks * block_size + block_size - 1);
213 if (force_unaligned) {
215 if (!(((unsigned long) buffer) & (block_size - 1)))
222 nr_blocks = end_lba - lba + 1;
224 printf("disk %s: reading LBA %lu -> %lu", device, start_lba, end_lba);
225 printf(" in chunks of %u %ub blocks\n", blocks, block_size);
228 time_spent = sectors_since_last = 0;
229 gettimeofday(&start_time, NULL);
231 if (blocks > nr_blocks)
234 hdr_init(hdr, buffer, cdb);
236 hdr->dxfer_len = blocks * block_size;
237 hdr->cmdp[2] = (lba >> 24) & 0xff;
238 hdr->cmdp[3] = (lba >> 16) & 0xff;
239 hdr->cmdp[4] = (lba >> 8) & 0xff;
240 hdr->cmdp[5] = lba & 0xff;
241 hdr->cmdp[7] = (blocks >> 8) & 0xff;
242 hdr->cmdp[8] = blocks & 0xff;
244 if (ioctl(fd, SG_IO, hdr) < 0) {
245 int good_blocks = (hdr->dxfer_len - hdr->resid) / block_size;
246 printf("sense key: %x\n", hdr->status);
248 printf("\n%s: IO error at lba %lu\n", device, lba + good_blocks);
249 blocks = good_blocks;
251 printf("\n%s: giving up at lba %lu\n", device, lba);
257 printf("%s: residual %u\n", device, hdr->resid);
259 gettimeofday(&end_time, NULL);
263 sectors_since_last += blocks;
264 time_spent = time_elapsed(&start_time, &end_time);
265 if (time_spent >= 500) {
266 print_datarate(sectors_since_last, time_spent);
269 write_datarate(fd_rate, lba, sectors_since_last, time_spent);
270 sectors_since_last = 0;
271 gettimeofday(&start_time, NULL);
275 bytes += blocks * block_size;
278 write_the_crap(fd_out, buffer, blocks * block_size);
280 } while (lba <= end_lba && nr_blocks);
282 print_datarate(sectors_since_last, time_spent);