os3/host/mbrcopy/mbrcopy.c
Johnh Stefanelli 642385797c
[Host/KernelCopy] First implementation of KernelCopy
[LibFat] Add libfat
[Stage1] Add stage1 project
2022-10-06 21:20:29 +02:00

63 lines
No EOL
1.3 KiB
C

#include "libfat.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "libhost.h"
os_u32 loaded_fat_segment[512];
os_u8 mbr_buffer[512];
int main(int argc, char** argv) {
if (argc < 3) {
printf("No files provided\n");
return 1;
}
FILE* f = fopen(argv[1], "rb+");
LFFileHandle h = libhost_create_handle(f);
LFControlBlock fat;
if(libfat_init(h, loaded_fat_segment, 512 * sizeof(os_u32), &fat) != OS_TRUE) {
fprintf(stderr, "libfat_init failed.\n");
return 1;
}
FILE* f2 = fopen(argv[2], "rb");
LFFileHandle mbrHandle = libhost_create_handle(f2);
if(libfat_test_mbr(&mbrHandle) != OS_TRUE) {
printf("Failed to test MBR...\n");
return 1;
}
printf("FAT Ok.\n");
FatHeader_32 header = fat.header;
fseek(f2, 0, SEEK_SET);
os_u32 read = fread(mbr_buffer, 1, 512, f2);
if (read < 512) {
fprintf(stderr, "MBR size is incorrect: %d\n", read);
return 1;
}
if (*(os_u16*)(mbr_buffer + 510) != 0xAA55) {
fprintf(stderr, "MBR boot signature is missing: %#x\n", *(os_u16*)(mbr_buffer + 510));
return 1;
}
printf("MBR Ok.\n");
memcpy(mbr_buffer + 3, ((char*)&header) + 3, sizeof(FatHeader_32) - 3);
fseek(f, 0, SEEK_SET);
fwrite(mbr_buffer, 1, 512, f);
fclose(f);
fclose(f2);
printf("MBR Written on FAT.\n");
return 0;
}