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

78 lines
1.6 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#define KERNLDR_START_SECTOR 1
#define HD_SECTOR_SIZE 512
int main(int argc, char* argv[])
{
if(argc == 3)
{
char *input = argv[1];
char *output = argv[2];
printf("Copy kernLdr 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);
return 1;
}
int outputFile = open(output, O_WRONLY | O_CREAT, 0664);
if(outputFile == -1)
{
fprintf(stderr, "Error when openning file <%s>\n", output);
return 1;
}
lseek(inputFile, 0, SEEK_SET);
lseek(outputFile, HD_SECTOR_SIZE * KERNLDR_START_SECTOR, SEEK_SET);
struct stat info;
fstat(inputFile, &info);
unsigned int kernLdrCodeSize = info.st_size;
if(kernLdrCodeSize >= 32256)
{
perror("Error kernLdr is too large\n");
close(inputFile);
close(outputFile);
return 2;
}
unsigned char *kernLdrCode = malloc(kernLdrCodeSize);
int bytesRead = read(inputFile, kernLdrCode, kernLdrCodeSize);
if(bytesRead == -1)
{
perror("Reading error\n");
free(kernLdrCode);
close(inputFile);
close(outputFile);
return 3;
}
int bytesWrite = write(outputFile, kernLdrCode, kernLdrCodeSize);
if(bytesWrite == -1)
{
perror("Writing error\n");
free(kernLdrCode);
close(inputFile);
close(outputFile);
return 3;
}
free(kernLdrCode);
close(inputFile);
close(outputFile);
printf("done\n");
}
else
{
perror("[help] : w_kernLdr <kernLdr> <disk image>\n");
return 4;
}
return 0;
}