fat12: beginnings of open/close on partition
Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
		@@ -1,4 +1,8 @@
 | 
			
		||||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include "io.h"
 | 
			
		||||
 | 
			
		||||
#define try(x) do { int i = (x); if (i == -1) { return -1 } } while (0)
 | 
			
		||||
@@ -34,12 +38,12 @@ int fat12_defrag(int fd) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 32 root entries keeps it padded correctly.
 | 
			
		||||
    if (fat12_read_u16(mbr + 0x11, 32) != 32) {
 | 
			
		||||
    if (fat12_read_u16(buf + 0x11, 32) != 32) {
 | 
			
		||||
        return -2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Total number of 512-byte sectors
 | 
			
		||||
    fat12_write_u16(mbr + 0x13, bytes / sector_size);
 | 
			
		||||
    uint32_t clufat12_read_u16(buf + 0x13, bytes / sector_size);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										116
									
								
								src/fat12.c
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								src/fat12.c
									
									
									
									
									
								
							@@ -1,5 +1,32 @@
 | 
			
		||||
#include "fat12.h"
 | 
			
		||||
#include "fat12-internals.h"
 | 
			
		||||
#include "io.h"
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
struct fat12_partition {
 | 
			
		||||
    int fd;
 | 
			
		||||
 | 
			
		||||
    // Number of bytes in a sector (512)
 | 
			
		||||
    uint32_t sector_size;
 | 
			
		||||
    uint8_t sector_buffer[512];
 | 
			
		||||
 | 
			
		||||
    // These units are "sectors"
 | 
			
		||||
    uint32_t fat_offset;
 | 
			
		||||
    uint32_t fat_size;
 | 
			
		||||
    uint32_t root_offset;
 | 
			
		||||
    uint32_t first_offset;
 | 
			
		||||
    uint32_t cluster_size;
 | 
			
		||||
 | 
			
		||||
    // These are in single-unit quantities
 | 
			
		||||
    uint32_t root_entries;
 | 
			
		||||
    uint32_t cluster_count;
 | 
			
		||||
    uint32_t cluster_bytes;
 | 
			
		||||
    uint32_t sector_count;
 | 
			
		||||
 | 
			
		||||
    // Used for traversing directories
 | 
			
		||||
    uint32_t current_cluster;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void fat12_write_u8(void *ptr, uint8_t val) {
 | 
			
		||||
    ((uint8_t *)ptr)[0] = (val >> 0) & 0xff;
 | 
			
		||||
@@ -49,4 +76,93 @@ uint32_t fat12_get_cluster(void *fat, uint32_t cluster) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct fat12_partition *fat12_alloc(void) {
 | 
			
		||||
    struct fat12_partition *part = malloc(sizeof(struct fat12_partition));
 | 
			
		||||
    memset(part, 0, sizeof(*part));
 | 
			
		||||
    return part;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int fat12_open(struct fat12_partition *part, const char *filename) {
 | 
			
		||||
 | 
			
		||||
    if (!part)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    if (part->fd > 0)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    part->fd = pang_open(filename, O_RDWR);
 | 
			
		||||
    if (part->fd == -1) {
 | 
			
		||||
        part->fd = 0;
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (pang_read(part->fd, part->sector_buffer, sizeof(part->sector_buffer)) != sizeof(part->sector_buffer)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    part->sector_size = fat12_read_u16(part->sector_buffer + 0x0b);
 | 
			
		||||
 | 
			
		||||
    // These are in single-unit quantities
 | 
			
		||||
    part->root_entries = fat12_read_u16(part->sector_buffer + 0x11);
 | 
			
		||||
    part->sector_count = fat12_read_u16(part->sector_buffer + 0x13);
 | 
			
		||||
 | 
			
		||||
    // These units are "sectors"
 | 
			
		||||
    part->fat_offset = fat12_read_u16(part->sector_buffer + 0x0e);
 | 
			
		||||
    part->fat_size = fat12_read_u16(part->sector_buffer + 0x16);
 | 
			
		||||
    part->root_offset = part->fat_offset + part->fat_size;
 | 
			
		||||
    part->first_offset = part->root_offset + (part->root_entries * sizeof(struct fat_directory_entry)) / part->sector_size;
 | 
			
		||||
    part->cluster_size = fat12_read_u8(part->sector_buffer + 0x0d);
 | 
			
		||||
 | 
			
		||||
    uint32_t bytes = (part->sector_count - part->first_offset) * part->sector_size;
 | 
			
		||||
    part->cluster_count = bytes / part->sector_size / part->cluster_size;
 | 
			
		||||
    part->cluster_bytes = (part->cluster_count * 3) / 2;
 | 
			
		||||
 | 
			
		||||
///
 | 
			
		||||
    fprintf(stderr, "part->sector_size: %d\n", part->sector_size);
 | 
			
		||||
 | 
			
		||||
    // These are in single-unit quantities
 | 
			
		||||
    fprintf(stderr, "part->root_entries: %d\n", part->root_entries);
 | 
			
		||||
    fprintf(stderr, "part->sector_count: %d\n", part->sector_count);
 | 
			
		||||
 | 
			
		||||
    // These units are "sectors"
 | 
			
		||||
    fprintf(stderr, "part->fat_offset: %d\n", part->fat_offset);
 | 
			
		||||
    fprintf(stderr, "part->fat_size: %d\n", part->fat_size);
 | 
			
		||||
    fprintf(stderr, "part->root_offset: %d\n", part->root_offset);
 | 
			
		||||
    fprintf(stderr, "part->first_offset: %d\n", part->first_offset);
 | 
			
		||||
    fprintf(stderr, "part->cluster_size: %d\n", part->cluster_size);
 | 
			
		||||
 | 
			
		||||
    fprintf(stderr, "part->cluster_count: %d\n", part->cluster_count);
 | 
			
		||||
    fprintf(stderr, "part->cluster_bytes: %d\n", part->cluster_bytes);
 | 
			
		||||
    fprintf(stderr, "data_bytes: %d\n", bytes);
 | 
			
		||||
///
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int fat12_close(struct fat12_partition *part) {
 | 
			
		||||
    if (!part)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    if (part->fd <= 0)
 | 
			
		||||
        return -1;
 | 
			
		||||
 | 
			
		||||
    if (-1 == pang_close(part->fd)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    part->fd = 0;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void fat12_free(struct fat12_partition **part) {
 | 
			
		||||
    if (!part)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    if (!*part)
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    free(*part);
 | 
			
		||||
    *part = NULL;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								src/io.c
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/io.c
									
									
									
									
									
								
							@@ -1,3 +1,6 @@
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include "io.h"
 | 
			
		||||
 | 
			
		||||
#ifdef unix
 | 
			
		||||
@@ -12,4 +15,13 @@ ssize_t pang_read(int fd, void *buf, size_t count) {
 | 
			
		||||
off_t pang_seek(int fd, off_t offset, int whence) {
 | 
			
		||||
    return lseek(fd, offset, whence);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int pang_open(const char *pathname, int flags) {
 | 
			
		||||
    return open(pathname, flags);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int pang_close(int fd) {
 | 
			
		||||
    return close(fd);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										21
									
								
								src/mkfat.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/mkfat.c
									
									
									
									
									
								
							@@ -3,8 +3,11 @@
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "fat12.h"
 | 
			
		||||
#include "fat12-internals.h"
 | 
			
		||||
#include "io.h"
 | 
			
		||||
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
// Create a FAT12 filesystem using the given file descriptor,
 | 
			
		||||
// with length _bytes_.
 | 
			
		||||
// It will define a root directory size and padding size so that the file sectors
 | 
			
		||||
@@ -27,15 +30,15 @@ int fat12_mkfs(int fd, uint32_t bytes) {
 | 
			
		||||
    uint32_t root_directory_sector_count = cluster_size - (fat_size_sectors + reserved_sectors);
 | 
			
		||||
    uint32_t root_directory_entry_count = (sector_size / sizeof(struct fat_directory_entry)) * root_directory_sector_count;
 | 
			
		||||
 | 
			
		||||
    // fprintf(stderr, "bytes: %d\n", bytes);
 | 
			
		||||
    // fprintf(stderr, "sector_size: %d\n", sector_size);
 | 
			
		||||
    // fprintf(stderr, "cluster_size: %d\n", cluster_size);
 | 
			
		||||
    // fprintf(stderr, "reserved_sectors: %d\n", reserved_sectors);
 | 
			
		||||
    // fprintf(stderr, "fat_size: %d\n", fat_size);
 | 
			
		||||
    // fprintf(stderr, "fat_size_sectors: %d\n", fat_size_sectors);
 | 
			
		||||
    // fprintf(stderr, "root_directory_sector_count: %d\n", root_directory_sector_count);
 | 
			
		||||
    // fprintf(stderr, "root_directory_entry_count: %d\n", root_directory_entry_count);
 | 
			
		||||
    // fprintf(stderr, "\n");
 | 
			
		||||
    fprintf(stderr, "bytes: %d\n", bytes);
 | 
			
		||||
    fprintf(stderr, "sector_size: %d\n", sector_size);
 | 
			
		||||
    fprintf(stderr, "cluster_size: %d\n", cluster_size);
 | 
			
		||||
    fprintf(stderr, "reserved_sectors: %d\n", reserved_sectors);
 | 
			
		||||
    fprintf(stderr, "fat_size: %d\n", fat_size);
 | 
			
		||||
    fprintf(stderr, "fat_size_sectors: %d\n", fat_size_sectors);
 | 
			
		||||
    fprintf(stderr, "root_directory_sector_count: %d\n", root_directory_sector_count);
 | 
			
		||||
    fprintf(stderr, "root_directory_entry_count: %d\n", root_directory_entry_count);
 | 
			
		||||
    fprintf(stderr, "\n");
 | 
			
		||||
 | 
			
		||||
    memset(mbr, 0, sizeof(mbr));
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user