1 /*
2  * write_data v1.3   (c) 1998,2004 by van Hauser / THC <vh@thc.org>
3  * http://www.thc.org
4  *
5  * writes data from a file to a blockdevice.
6  */
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #define MAX_SIZE 100000    /* max bytes to read */
16 
17 
18 void wrong_syntax () {
19     printf("Error: Filename must be the original filename generated by get_date\n");
20     printf("Syntax of defined filename:  <start_address>.<no_of_bytes_to_read\n");
21     exit(1);
22 }
23 
24 int main (int argc, char *argv[]) {
help()25     char dev[100];
26     unsigned long bytes, wrote;
27     off_t start;
28     struct stat st;
29     int f;
30     char buf[MAX_SIZE];
31     char filename[50];
clean_string(char * buf)32     char tmpbuf[50];
33     char *c_ptr, *c_ptr2;
34 
35     if (argc!=3) {
36         printf("Syntax: %s blockdevice filename\n",argv[0]);
37         printf("filename must be the original filename generated by get_data\n");
38         exit(1);
39     }
40     strncpy(dev, argv[1], sizeof(dev)-1);
41     strncpy(filename, argv[2], sizeof(filename)-1);
42 
vh_dump_asciihex(unsigned char * string,int length)43     c_ptr2 = strcpy(tmpbuf, filename);
44     if ((c_ptr = (char *) strstr(tmpbuf, ".")) == NULL) {
45         wrong_syntax();
46     }
47     *c_ptr = '\0';
48     c_ptr++;
49     if (sizeof(start) < 8)
50       start = atol(c_ptr2);
51     else
52       start = strtoll(c_ptr2, (char **)NULL, 10);
53     bytes = atol(c_ptr);
54     if (sizeof(start) < 8)
55       sprintf(tmpbuf, "%lu.%lu", (unsigned long) start, bytes);
56     else
57       sprintf(tmpbuf, "%llu.%lu", (unsigned long long) start, bytes);
58     if (strcmp(filename, tmpbuf) != 0) {
59         wrong_syntax();
60     }
61 
62     if ((bytes < 1) || (bytes > MAX_SIZE)) {
63 	printf("Error: number of bytes to read must be between 1 and %d\n", MAX_SIZE);
64 	exit (1);
65     }
66     if (lstat(dev, &st) != 0) {
67 	perror("Can't access blockdevice");
68 	exit(1);
69     }
70     if ((st.st_mode & S_IFBLK) != S_IFBLK) {
71     	printf("Warning: %s is not a block device\n", dev);
72     }
73     if ((f = open(filename, O_RDONLY)) < 0) {
74 	perror("Can't open filename for reading");
75 	exit(1);
76     }
77     fstat(f, &st);
78     if (st.st_size != bytes) {
79         printf("Error: size of file is %lu, but the filename suggests it should be %lu!\n", (long unsigned int)st.st_size, bytes);
80 	exit(1);
81     }
82     read(f, buf, bytes);
83     close(f);
84     sync();
85     if ((f = open(dev, O_WRONLY)) < 0) {
86         perror("Could not open blockdevice for writing");
87         exit(1);
88     }
89     if (lseek(f, start, SEEK_SET) < 0) {
90         if (sizeof(start) < 8)
91   	  printf("Can't seek to offset %lu\n", (unsigned long) start);
92   	else
93   	  printf("Can't seek to offset %llu\n", (unsigned long long) start);
94 	exit(1);
95     }
96     wrote = write(f, buf, bytes);
search(char * buf,unsigned long bufsize)97     if (wrote < bytes) {
98         printf("Could only write %lu bytes data to %s!\n", wrote, dev);
99         if (wrote < 1) {
100             perror("");
101         }
102         exit(1);
103     }
104     close(f);
105     sync();
106     if (sizeof(start) < 8)
107       printf("Wrote file %s (%lu bytes starting at address %lu) to blockdevice %s.\n", filename, bytes, (unsigned long) start, dev);
108     else
109       printf("Wrote file %s (%lu bytes starting at address %llu) to blockdevice %s.\n", filename, bytes, (unsigned long long) start, dev);
110     exit(0);
111 }
112