1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
unsigned char buf[16384];
main(int argc, char **argv)
{
int len,i, stubfd, st2fd, outfd, retval=0;
unsigned char checksum=0;
memset(buf, 0xff, 16384);
if (argc < 4) {
fprintf(stderr, "Usage: makecsum <stubfile> <stage2> <outfile>\n");
exit(1);
}
stubfd = open(argv[1], O_RDONLY);
if (stubfd < 0) {
perror("open stub file");
exit(1);
}
st2fd = open(argv[2], O_RDONLY);
if (st2fd < 0) {
perror("open stage2 file");
close(stubfd);
exit(1);
}
outfd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0664);
if (outfd < 0) {
perror("open output file");
exit(1);
}
/* Read the stub loader (stage1) */
len = read(stubfd,buf,769);
if (len < 0) {
perror("read from stub file");
close(outfd);
close(st2fd);
close(stubfd);
exit(1);
}
if (len > 768) {
fprintf(stderr, "stub file is too large (>768 bytes)\n");
close(outfd);
close(st2fd);
close(stubfd);
exit(1);
}
close(stubfd);
/* Read enough of the remainder to calculate the csum */
len = read(st2fd, buf+768, 16384-768);
if (len < 0) {
perror("read from stage2 file\n");
close(outfd);
close(st2fd);
exit(1);
}
/* Calculate the csum for 512-byte-page devices */
buf[767] = 0;
i=0;
while (i<16384) {
checksum += buf[i];
i++;
if ((i >> 8)&1)
i += 256;
}
/* Set the slack byte to fix the csum */
buf[767] = 0x55 - checksum;
/* Calculate the csum for 256-byte-page devices */
checksum = 0;
buf[511] = 0;
i=0;
while (i<8192) {
checksum += buf[i++];
}
/* Set the slack byte to fix the csum */
buf[511] = 0x55 - checksum;
/* Write the original buffer */
len = write(outfd, buf, 16384);
if (len < 0) {
perror("write output file");
close(outfd);
close(st2fd);
exit(1);
}
if (len < 16384) {
fprintf(stderr, "short write of output file (%d bytes < 16384)\n",
len);
close(outfd);
close(st2fd);
exit(1);
}
/* Now chuck out the rest of the stage2 */
while (1) {
len = read(st2fd, buf, 16384);
if (len < 0) {
perror("read stage2 file");
retval = 1;
break;
}
if (len == 0)
break;
i = write(outfd, buf, len);
if (i < 0) {
perror("write output file");
retval = 1;
break;
}
if (i<len) {
fprintf(stderr, "short write of output file (%d bytes < %d)\n",
i,len);
retval = 1;
break;
}
}
close(outfd);
close(st2fd);
return retval;
}
|