patched diskwrite, changed 'make clean' procedure

This commit is contained in:
notsomeidiot123 2024-09-26 13:08:35 -04:00
parent 0628705419
commit 975c522731
3 changed files with 26 additions and 8 deletions

View File

@ -5,6 +5,7 @@ BL_ASFLAGS := -f bin
all: bootloader tools all: bootloader tools
cp bin/bootloader.bin image.bin cp bin/bootloader.bin image.bin
qemu-img resize -f raw image.bin 512M qemu-img resize -f raw image.bin 512M
./diskwrite -v kernel.elf -o image.bin
qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio qemu-system-i386 -hda image.bin --no-reboot --no-shutdown -m 32m -smp 2 -serial mon:stdio
tools: tools:

View File

@ -402,7 +402,8 @@ open_file:
mov si, DAP mov si, DAP
int 0x13 int 0x13
jc load_fail jc load_fail
debug jmp $
; debug
read_file: read_file:
;reads all data from file ;reads all data from file
load_elf: load_elf:

View File

@ -94,27 +94,32 @@ int read_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf)
} }
int write_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf){ int write_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf){
// printf("pre seek\n");
int result = fseek(file, sector_start * 512, SEEK_SET); int result = fseek(file, sector_start * 512, SEEK_SET);
if(result){ if(result){
printf("Error during seek\n"); printf("Error during seek\n");
return result; return result;
} }
// printf("pre write\n");
result = fwrite(buf, 512, count, file); result = fwrite(buf, 512, count, file);
return result; return result;
} }
uint32_t find_free_cluster(fat_info *info){ uint32_t find_free_cluster(fat_info *info){
// printf("finding free cluster\n");
uint32_t start = info->fsinfo.cluster_search_start; uint32_t start = info->fsinfo.cluster_search_start;
if(start < 2){ if(start < 2){
start = 2; start = 2;
} }
for(int i = start; i < (info->bpb.bytes_per_sector/4) * info->bpb.sectors_per_fat; i++){ for(int i = start; i < (info->bpb.sectors_per_fat * info->bpb.bytes_per_sector)/4; i++){
// printf("%d, %p\n", i);
if(info->file_table[i]) continue; if(info->file_table[i]) continue;
return i; return i;
} }
} }
void write_cluster_value(fat_info *info, uint32_t value, uint32_t cluster){ void write_cluster_value(fat_info *info, uint32_t value, uint32_t cluster){
printf("writing cluster :3\n");
for(int i = 0; i < info->bpb.fat_count; i++){ for(int i = 0; i < info->bpb.fat_count; i++){
info->file_table[cluster + i * info->bpb.sectors_per_fat * (info->bpb.bytes_per_sector/4)] = value; info->file_table[cluster + i * info->bpb.sectors_per_fat * (info->bpb.bytes_per_sector/4)] = value;
} }
@ -159,6 +164,7 @@ int create_file(char *path, fat_info *info, FILE *file){
dirent.start_cluster_high = cluster >> 16; dirent.start_cluster_high = cluster >> 16;
dirent.start_cluster_low = cluster & 0xFFFF; dirent.start_cluster_low = cluster & 0xFFFF;
} }
// printf("found free cluster\n");
write_sector(file, (info->bpb.reserved_sectors + info->bpb.fat_count * info->bpb.sectors_per_fat) + cluster * info->bpb.sectors_per_cluster, info->bpb.sectors_per_cluster, ifile_data); write_sector(file, (info->bpb.reserved_sectors + info->bpb.fat_count * info->bpb.sectors_per_fat) + cluster * info->bpb.sectors_per_cluster, info->bpb.sectors_per_cluster, ifile_data);
verbose && printf("wrote to sector %x %x sectors of data from buffer\n", (info->bpb.reserved_sectors + info->bpb.fat_count * info->bpb.sectors_per_fat) + cluster * info->bpb.sectors_per_cluster, info->bpb.sectors_per_cluster); verbose && printf("wrote to sector %x %x sectors of data from buffer\n", (info->bpb.reserved_sectors + info->bpb.fat_count * info->bpb.sectors_per_fat) + cluster * info->bpb.sectors_per_cluster, info->bpb.sectors_per_cluster);
if(last_cluster != 0) write_cluster_value(info, cluster, last_cluster); if(last_cluster != 0) write_cluster_value(info, cluster, last_cluster);
@ -183,9 +189,19 @@ int create_file(char *path, fat_info *info, FILE *file){
} }
root_dir[free_file_index] = dirent; root_dir[free_file_index] = dirent;
verbose && printf("Found a free dirent at index %u\n", free_file_index); verbose && printf("Found a free dirent at index %u\n", free_file_index);
for(uint32_t i = 0; i < (dirent_count * sizeof(file_t)) / info->bpb.bytes_per_sector /info->bpb.sectors_per_cluster; i++){ uint32_t cluster = info->bpb.root_dir_cluster;
write_sector(file, (info->bpb.reserved_sectors + (info->bpb.fat_count * info->bpb.sectors_per_fat) + info->file_table[i] * info->bpb.sectors_per_cluster), info->bpb.sectors_per_cluster, (buffer_t)(root_dir + i * info->bpb.sectors_per_cluster * info->bpb.bytes_per_sector)); uint32_t cluster_count = (dirent_count * sizeof(file_t)) / info->bpb.bytes_per_sector /info->bpb.sectors_per_cluster;
for(uint32_t i = 0; i < cluster_count; i++){
write_sector(file, (info->bpb.reserved_sectors + (info->bpb.fat_count * info->bpb.sectors_per_fat) + cluster * info->bpb.sectors_per_cluster), info->bpb.sectors_per_cluster, (buffer_t)(root_dir + i * info->bpb.sectors_per_cluster * info->bpb.bytes_per_sector));
uint32_t lcluster = cluster;
cluster = info->file_table[cluster];
if(cluster == FILE_END && i < cluster_count - 1){
cluster = find_free_cluster(info);
if(cluster != FILE_END){
write_cluster_value(info, cluster, lcluster);
write_cluster_value(info, cluster, FILE_END);
}
}
} }
} }
@ -228,8 +244,8 @@ int detect_repair_fs(FILE *file, fat_info *info){
verbose && printf("allocated 0x%x bytes for fat\n", (info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector)); verbose && printf("allocated 0x%x bytes for fat\n", (info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector));
int result = read_sector(file, info->bpb.reserved_sectors, info->bpb.fat_count * info->bpb.sectors_per_fat, fat); int result = read_sector(file, info->bpb.reserved_sectors, info->bpb.fat_count * info->bpb.sectors_per_fat, fat);
if(result == 0){ if(result == 0){
printf("Error: Could not read from disk\n"); printf("Error: Could not read from disk %p\n", file);
return 0; return 1;
} }
info->file_table = (uint32_t *)fat; info->file_table = (uint32_t *)fat;
verbose && printf("FAT assigned\n"); verbose && printf("FAT assigned\n");
@ -275,10 +291,10 @@ int main(int argc, char **argv){
return 0; return 0;
} }
detect_repair_fs(output_file, &info); detect_repair_fs(output_file, &info);
verbose && printf("Finished. Exiting...\n");
for(int i = 0; i < filec; i++){ for(int i = 0; i < filec; i++){
create_file(files[i], &info, output_file); create_file(files[i], &info, output_file);
} }
fat_write_back(output_file, &info); fat_write_back(output_file, &info);
verbose && printf("Finished. Exiting...\n");
return 0; return 0;
} }