loadwarka/src/main.c

126 lines
3.0 KiB
C
Raw Normal View History

2022-01-16 15:54:23 +00:00
#include <stdbool.h>
2022-01-16 14:34:20 +00:00
#include <stdint.h>
2022-01-16 07:37:34 +00:00
#include <stdio.h>
2022-01-16 15:14:56 +00:00
#include <string.h>
2022-01-16 07:37:34 +00:00
2022-01-17 06:15:57 +00:00
#include <kernaux/mbr.h>
2022-01-16 14:34:20 +00:00
2022-01-16 15:54:23 +00:00
static bool create_mbr_file(
2022-01-16 15:57:29 +00:00
bool print_debug,
2022-01-16 15:54:23 +00:00
const char *output_filename,
uint32_t disk_id,
const char *bootstrap_filename
);
2022-01-16 16:04:08 +00:00
int main(const int argc, char **const argv)
2022-01-16 07:37:34 +00:00
{
2022-01-16 16:04:08 +00:00
if (argc < 2) {
fprintf(stderr, "Invalid usage\n");
return 1;
}
const char *const format = argv[1];
if (strcmp(format, "mbr") == 0) {
if (argc != 3 && argc != 4) {
fprintf(stderr, "Invalid usage\n");
return 1;
}
const char *const output_filename = argv[2];
const char *const bootstrap_filename = argc == 3 ? NULL :argv[3];
const bool result = create_mbr_file(
true,
output_filename,
0xf01834d0,
bootstrap_filename
);
if (!result) return 1;
} else {
fprintf(stderr, "Invalid format: %s\n", format);
}
return 0;
2022-01-16 15:54:23 +00:00
}
bool create_mbr_file(
2022-01-16 15:57:29 +00:00
const bool print_debug,
2022-01-16 15:54:23 +00:00
const char *const output_filename,
const uint32_t disk_id,
const char *const bootstrap_filename
) {
2022-01-17 06:15:57 +00:00
struct KernAux_Mbr mbr;
2022-01-16 15:14:56 +00:00
{
memset(&mbr, 0, sizeof(mbr));
2022-01-17 06:15:57 +00:00
mbr.info.magic = KERNAUX_MBR_MAGIC;
2022-01-16 15:54:23 +00:00
mbr.info.disk_id = disk_id;
2022-01-16 15:14:56 +00:00
mbr.info.reserved = 0;
2022-01-16 16:30:11 +00:00
// 00
mbr.info.entries[0].drive_attributes = 0;
// 00 02 00
2022-01-17 07:06:54 +00:00
mbr.info.entries[0].first_sector_chs_addr = 0x0200;
2022-01-16 16:30:11 +00:00
// 01
mbr.info.entries[0].partition_type = 1;
// 02 03 00
2022-01-17 07:06:54 +00:00
mbr.info.entries[0].last_sector_chs_addr = 0x0302;
2022-01-16 16:30:11 +00:00
// 01 00 00 00
2022-01-17 07:06:54 +00:00
mbr.info.entries[0].first_sector_lba_addr = 0x01;
2022-01-16 16:30:11 +00:00
// 80 00 00 00
mbr.info.entries[0].sectors_count = 0x80;
2022-01-16 15:14:56 +00:00
}
2022-01-16 15:54:23 +00:00
if (bootstrap_filename) {
FILE *fd = fopen(bootstrap_filename, "r");
2022-01-16 15:14:56 +00:00
if (fd == NULL) {
fprintf(stderr, "Can't open bootstrap file\n");
2022-01-16 15:54:23 +00:00
return false;
2022-01-16 15:14:56 +00:00
}
2022-01-17 06:15:57 +00:00
const size_t size = fread(&mbr.bootstrap, 1, KERNAUX_MBR_BOOTSTRAP_SIZE, fd);
2022-01-16 15:14:56 +00:00
if (size == 0) {
fprintf(stderr, "Empty bootstrap file\n");
2022-01-16 15:54:23 +00:00
return false;
2022-01-16 15:14:56 +00:00
}
if (!feof(fd)) {
fprintf(stderr, "Too long bootstrap file\n");
2022-01-16 15:54:23 +00:00
return false;
2022-01-16 15:14:56 +00:00
}
fclose(fd);
}
uint8_t *mbr_ptr = (uint8_t*)&mbr;
2022-01-16 15:57:29 +00:00
if (print_debug) {
for (size_t i = 0; i < 512 / 16; ++i) {
printf("%02x", mbr_ptr[i * 16]);
for (size_t j = 1; j < 16; ++j) {
printf(" %02x", mbr_ptr[i * 16 + j]);
}
printf("\n");
2022-01-16 15:14:56 +00:00
}
2022-01-16 16:09:12 +00:00
}
2022-01-16 15:14:56 +00:00
2022-01-16 16:09:12 +00:00
{
FILE *fd = fopen(output_filename, "wb");
if (fd == NULL) {
fprintf(stderr, "Can't open image file\n");
return false;
}
2022-01-16 15:14:56 +00:00
2022-01-16 16:09:12 +00:00
const size_t size = fwrite(mbr_ptr, 1, sizeof(mbr), fd);
if (size != sizeof(mbr)) {
fprintf(stderr, "Can't write image file\n");
return false;
2022-01-16 15:57:29 +00:00
}
2022-01-16 16:09:12 +00:00
fclose(fd);
2022-01-16 15:14:56 +00:00
}
2022-01-16 15:54:23 +00:00
return true;
2022-01-16 07:37:34 +00:00
}