2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
15 void usage(char const *progname
)
17 printf("usage: %s [-p #padding] bootblock payload outfile\n", progname
);
20 int main(int argc
, char *argv
[])
25 unsigned char bootsector
[1024];
26 unsigned char buf
[512];
38 while( (opt
= getopt(argc
, argv
, "p:")) != -1)
41 padding
= atoi(optarg
);
56 printf("insufficient args\n");
61 err
= stat(argv
[2], &st
);
63 printf("error stating file '%s'\n", argv
[2]);
67 outfd
= open(argv
[3], O_BINARY
|O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
69 printf("error: cannot open output file '%s'\n", argv
[3]);
73 // first read the bootblock
74 infd
= open(argv
[1], O_BINARY
|O_RDONLY
);
76 printf("error: cannot open bootblock file '%s'\n", argv
[1]);
79 if(read(infd
, bootsector
, sizeof(bootsector
)) < sizeof(bootsector
)
80 || lseek(infd
, 0, SEEK_END
) != sizeof(bootsector
)) {
81 printf ("error: size of bootblock file '%s' must match %d bytes.\n", argv
[1], sizeof(bootsector
));
86 // patch the size of the output into bytes 3 & 4 of the bootblock
87 blocks
= st
.st_size
/ 512;
88 if((st
.st_size
% 512) != 0)
90 printf("size %d, blocks %d (size %d)\n", (unsigned long)st
.st_size
, blocks
, blocks
* 512);
91 bootsector
[2] = (blocks
& 0x00ff);
92 bootsector
[3] = (blocks
& 0xff00) >> 8;
94 write(outfd
, bootsector
, sizeof(bootsector
));
95 written_bytes
= sizeof(bootsector
);
97 infd
= open(argv
[2], O_BINARY
|O_RDONLY
);
99 printf("error: cannot open input file '%s'\n", argv
[1]);
103 while((read_size
= read(infd
, buf
, sizeof(buf
))) > 0) {
104 write(outfd
, buf
, read_size
);
105 written_bytes
+= read_size
;
109 if (written_bytes
% padding
) {
110 size_t towrite
= padding
- written_bytes
% padding
;
111 unsigned char *buf
= malloc(towrite
);
113 memset(buf
, 0, towrite
);
114 write(outfd
, buf
, towrite
);
115 written_bytes
+= towrite
;
117 printf("output file padded to %ld\n", (unsigned long)written_bytes
);