int format(const char *mbr_fn, const char *bootloader_fn, const char *kern_fn, const char *output_fn)
{
FILE *mbr_f = fopen(mbr_fn, "r");
if (mbr_f == 0) return E_MBR_F;
fseek(mbr_f, 0L, SEEK_END);
uint64_t mbr_f_sz = ftell(mbr_f);
if (mbr_f_sz != 512) return E_MBR_F_SZ;
fseek(mbr_f, 0L, SEEK_SET);
FILE *bootloader_f = fopen(bootloader_fn, "r");
if (bootloader_f == 0) return E_BOOT_F;
fseek(bootloader_f, 0L, SEEK_END);
uint64_t bootloader_f_sz = ftell(bootloader_f);
fseek(mbr_f, 0L, SEEK_SET);
FILE *kern_f = fopen(kern_fn, "r");
if (kern_f == 0) return E_KERN_F;
fseek(kern_f, 0L, SEEK_END);
uint64_t kern_f_sz = ftell(kern_f);
fseek(kern_f, 0L, SEEK_SET);
char output[512 + bootloader_f_sz + kern_f_sz];
fread(&output[0], 1, 512, mbr_f);
fclose(mbr_f);
fread(&output[0x200], 1, bootloader_f_sz, bootloader_f);
fclose(bootloader_f);
fread(&output[0x200 + bootloader_f_sz], 1, kern_f_sz, kern_f);
fclose(kern_f);
output[0x1BA] = 'N';
output[0x1BB] = 'y';
output[0x1BC] = 'a';
output[0x1BD] = 'n';
output[0x1BE] = bootloader_f_sz;
output[0x1C6] = kern_f_sz;
FILE *output_f = fopen(output_fn, "w");
if (output_f == 0) return E_OUT_F;
fwrite(output, 1, 512 + bootloader_f_sz + kern_f_sz, output_f);
fclose(output_f);
return 0;
}
В файле output_f по смещению 0x200 вместо данных из файла bootloader_f записывается какой-то мусор.