39 lines
929 B
C
Raw Normal View History

2023-01-27 13:15:37 +04:00
#include <fcntl.h>
2023-01-27 12:25:39 +00:00
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
2023-01-27 13:15:37 +04:00
2024-02-10 01:39:02 +04:00
enum {
MBR_END = 510,
};
int main(int argc, char *argv[]) {
2024-02-10 01:39:02 +04:00
if (argc != 3) {
fprintf(stderr, "Usage: %s mbr.bin fs.img\n", argv[0]);
2023-01-27 13:15:37 +04:00
return 1;
}
const char *filename = argv[1];
const char *fsimg_path = argv[2];
2023-01-27 13:15:37 +04:00
int fd = open(filename, O_RDWR);
off_t length = lseek(fd, 0, SEEK_END);
2024-02-10 01:39:02 +04:00
if (length > MBR_END) {
fprintf(stderr, "file %s is larger than %d bytes (size: %llu)\n",
filename, MBR_END, (unsigned long long)length);
2023-01-27 13:15:37 +04:00
return 1;
}
2024-02-10 01:39:02 +04:00
lseek(fd, MBR_END, SEEK_SET);
2023-01-27 13:15:37 +04:00
write(fd, "\x55\xaa", 2);
2024-02-10 01:39:02 +04:00
int fsimg = open(fsimg_path, O_RDONLY);
if (fsimg < 0) {
perror(fsimg_path);
return 1;
}
char buf[1024];
ssize_t bytes;
while ((bytes = read(fsimg, buf, sizeof(buf))) > 0) {
write(fd, buf, bytes);
}
2023-01-27 13:15:37 +04:00
return 0;
}