144 lines
No EOL
2.7 KiB
C
144 lines
No EOL
2.7 KiB
C
#include "libfat.h"
|
|
#include "stdio.h"
|
|
#include "stdlib.h"
|
|
#include "string.h"
|
|
|
|
os_u32 loaded_fat_segment[512];
|
|
os_u8 mbr_buffer[512];
|
|
|
|
os_u32 readRoutine(os_u32 amount, void* buffer, void* user_data) {
|
|
if (user_data == 0) {
|
|
return 0;
|
|
}
|
|
FILE** f = (FILE**)user_data;
|
|
|
|
os_u32 read_bytes = fread(buffer, 1, amount, *f);
|
|
return read_bytes;
|
|
}
|
|
|
|
os_u32 writeRoutine(os_u32 amount, void* buffer, void* user_data) {
|
|
if (user_data == 0) {
|
|
return 0;
|
|
}
|
|
|
|
FILE** f = (FILE**)user_data;
|
|
|
|
os_u32 written_bytes = fwrite(buffer, 1, amount, *f);
|
|
return written_bytes;
|
|
}
|
|
|
|
os_bool seekRoutine(os_u32 offset, os_u32 direction, void* user_data) {
|
|
if (user_data == 0) {
|
|
return OS_FALSE;
|
|
}
|
|
|
|
FILE** f = (FILE**)user_data;
|
|
int seek_dir = SEEK_SET;
|
|
switch(direction) {
|
|
case LIBFAT_SEEK_ORIGIN:
|
|
seek_dir = SEEK_SET;
|
|
break;
|
|
case LIBFAT_SEEK_CURRENT:
|
|
seek_dir = SEEK_CUR;
|
|
break;
|
|
case LIBFAT_SEEK_END:
|
|
seek_dir = SEEK_END;
|
|
break;
|
|
}
|
|
|
|
int result = fseek(*f, offset, seek_dir);
|
|
return result == 0 ? OS_TRUE : OS_FALSE;
|
|
}
|
|
|
|
os_u32 tellRoutine(void* user_data) {
|
|
if (user_data == 0) {
|
|
return 0;
|
|
}
|
|
|
|
FILE** f = (FILE**)user_data;
|
|
|
|
os_u32 result = ftell(*f);
|
|
return result;
|
|
}
|
|
|
|
void printRoutine(os_s8* data, os_u16 size) {
|
|
if (size == 0) {
|
|
printf("%s\n", data);
|
|
return;
|
|
}
|
|
|
|
for(os_u16 s = 0; s < size; s++) {
|
|
printf("%c", *data);
|
|
data++;
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
void printNumber(os_u32 number) {
|
|
printf("%#x\n", number);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 3) {
|
|
printf("No files provided\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE* f = fopen(argv[1], "rb+");
|
|
|
|
LFFileHandle h;
|
|
h.read = readRoutine;
|
|
h.write = writeRoutine;
|
|
h.seek = seekRoutine;
|
|
h.tell = tellRoutine;
|
|
h.user_data = (void*)&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;
|
|
mbrHandle.read = readRoutine;
|
|
mbrHandle.write = writeRoutine;
|
|
mbrHandle.seek = seekRoutine;
|
|
mbrHandle.tell = tellRoutine;
|
|
mbrHandle.user_data = (void*)&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, ((void*)&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;
|
|
} |