begin working on MBR dump utility

This commit is contained in:
mykola2312 2024-08-07 09:33:29 +03:00
parent 9143073dd3
commit 87a280ae0d
2 changed files with 66 additions and 3 deletions

View file

@ -2,15 +2,17 @@ CC = gcc
AS = as
LD = ld
OBJCOPY = objcopy
CLFAGS = -g -Wall
ASFLAGS =
LDFLAGS = -g
QEMU = qemu-system-x86_64
SRC_DIR = src
INC_DIR = include
OBJ_DIR = obj
BIN_DIR = bin
CLFAGS = -g -Wall -I$(INC_DIR)/
ASFLAGS =
LDFLAGS = -g
UID := $(shell id -u)
GID := $(shell id -g)
@ -97,6 +99,9 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.s
$(AS) $(ASFLAGS) -o $@ $<
$(OBJCOPY) --remove-section .note.gnu.property $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c -o $@ $<
MBR_TEST_OBJ = obj/mbr_test.o obj/serial.o obj/itoa.o obj/util.o obj/test_disk_target.o
mbr_test: $(MBR_TEST_OBJ) $(DISK)
@ -123,6 +128,11 @@ mbr_test_qemu: mbr_test
-drive format=raw,media=disk,file=$(BIN_DIR)/mbr_test.bin \
-drive format=raw,media=disk,file=$(BIN_DIR)/disk.img
READMBR_OBJ = obj/readmbr.o
readmbr: $(READMBR_OBJ)
$(CC) $(LDFLAGS) -o $(BIN_DIR)/readmbr $(READMBR_OBJ)
clean:
rm -f bin/*
rm -f obj/*

53
src/readmbr.c Normal file
View file

@ -0,0 +1,53 @@
// program to read MBR and its partition table
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
typedef struct {
uint8_t cylinder;
uint8_t head;
uint8_t sector;
} __attribute__((packed)) chs_t;
typedef struct {
uint8_t attributes;
chs_t chs_first;
uint8_t type;
chs_t chs_last;
uint32_t lba_start;
uint32_t num_sectors;
} __attribute__((packed)) mbr_part_t;
typedef struct {
char bootstrap[440];
uint32_t unique_id;
uint16_t reserved;
mbr_part_t part_table[4];
uint16_t signature;
} __attribute__((packed)) mbr_t;
int main(int argc, char** argv)
{
mbr_t mbr;
// if (argc < 2)
// {
// fputs("provide filename to disk image as first argument\n", stderr);
// return 1;
// }
// int fd = open(argv[1], O_RDONLY);
// if (fd < 0)
// {
// perror("failed to open disk image\n");
// return 1;
// }
// close(fd);
printf("%lu\n", sizeof(mbr_t));
printf("%lu\n", sizeof(mbr_part_t));
return 0;
}