NutsOS/test/tools/w_mbr.c
2020-04-17 17:44:23 +02:00

67 lines
1.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#define BOOT_CODE_SIZE 446
int main(int argc, char* argv[])
{
if(argc == 3)
{
char *input = argv[1];
char *output = argv[2];
printf("Copy bootcode from <%s> to <%s> :\n", input, output);
int inputFile = open(input, O_RDONLY , 0664);
if(inputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", input);
close(inputFile);
return 1;
}
int outputFile = open(output, O_WRONLY | O_CREAT, 0664);
if(outputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", output);
close(outputFile);
return 1;
}
lseek(inputFile, 0, SEEK_SET);
lseek(outputFile, 0, SEEK_SET);
unsigned char *bootCode = malloc(BOOT_CODE_SIZE);
int bytesRead = read(inputFile, bootCode, BOOT_CODE_SIZE);
if(bytesRead == -1)
{
perror("Reading error\n");
free(bootCode);
close(inputFile);
close(outputFile);
return 2;
}
int bytesWrite = write(outputFile, bootCode, BOOT_CODE_SIZE);
if(bytesWrite == -1)
{
perror("Writing error\n");
free(bootCode);
close(inputFile);
close(outputFile);
return 2;
}
free(bootCode);
close(inputFile);
close(outputFile);
printf("done\n");
}
else
{
fprintf(stderr, "[help] : w_mbr <boot code> <disk image>\n");
return 3;
}
return 0;
}