/* * write_data v1.3 (c) 1998,2004 by van Hauser / THC * http://www.thc.org * * writes data from a file to a blockdevice. */ #include #include #include #include #include #include #include #define MAX_SIZE 100000 /* max bytes to read */ void wrong_syntax () { printf("Error: Filename must be the original filename generated by get_date\n"); printf("Syntax of defined filename: . MAX_SIZE)) { printf("Error: number of bytes to read must be between 1 and %d\n", MAX_SIZE); exit (1); } if (lstat(dev, &st) != 0) { perror("Can't access blockdevice"); exit(1); } if ((st.st_mode & S_IFBLK) != S_IFBLK) { printf("Warning: %s is not a block device\n", dev); } if ((f = open(filename, O_RDONLY)) < 0) { perror("Can't open filename for reading"); exit(1); } fstat(f, &st); if (st.st_size != bytes) { printf("Error: size of file is %lu, but the filename suggests it should be %lu!\n", (long unsigned int)st.st_size, bytes); exit(1); } read(f, buf, bytes); close(f); sync(); if ((f = open(dev, O_WRONLY)) < 0) { perror("Could not open blockdevice for writing"); exit(1); } if (lseek(f, start, SEEK_SET) < 0) { if (sizeof(start) < 8) printf("Can't seek to offset %lu\n", (unsigned long) start); else printf("Can't seek to offset %llu\n", (unsigned long long) start); exit(1); } wrote = write(f, buf, bytes); if (wrote < bytes) { printf("Could only write %lu bytes data to %s!\n", wrote, dev); if (wrote < 1) { perror(""); } exit(1); } close(f); sync(); if (sizeof(start) < 8) printf("Wrote file %s (%lu bytes starting at address %lu) to blockdevice %s.\n", filename, bytes, (unsigned long) start, dev); else printf("Wrote file %s (%lu bytes starting at address %llu) to blockdevice %s.\n", filename, bytes, (unsigned long long) start, dev); exit(0); }