Kernel: Convert devide to uint types, split out MBR partition parser.
authorWill Sowerbutts <will@sowerbutts.com>
Mon, 29 Dec 2014 00:39:20 +0000 (00:39 +0000)
committerWill Sowerbutts <will@sowerbutts.com>
Mon, 29 Dec 2014 01:05:19 +0000 (01:05 +0000)
Kernel/dev/devide.c
Kernel/dev/mbr.c [new file with mode: 0644]
Kernel/dev/mbr.h [new file with mode: 0644]
Kernel/include/kernel.h
Kernel/platform-n8vem-mark4/Makefile
Kernel/platform-n8vem-mark4/config.h
Kernel/platform-n8vem-mark4/uzi.lnk
Kernel/platform-p112/Makefile
Kernel/platform-p112/uzi.lnk

index 7dac82d..f815e59 100644 (file)
 #include <printf.h>
 #include <stdbool.h>
 #include <timer.h>
-#include "devide.h"
-
-typedef struct {
-    unsigned char status;
-    unsigned char chs_first[3];
-    unsigned char type;
-    unsigned char chs_last[3];
-    unsigned long lba_first;
-    unsigned long lba_count;
-} partition_table_entry_t;
-
-#define MBR_ENTRY_COUNT 4
-typedef struct {
-    unsigned char bootcode[446];
-    partition_table_entry_t partition[MBR_ENTRY_COUNT];
-    unsigned int signature;
-} master_boot_record_t;
-
-#define DRIVE_COUNT 2
-static unsigned char ide_drives_present; /* bitmap */
-static unsigned long ide_partition_start[DRIVE_COUNT];
-static unsigned int  ide_slice_count[DRIVE_COUNT];
+#include <devide.h>
+#include <mbr.h>
+
+#define DRIVE_COUNT 2   /* range 1 -- 4 */
+#define MAX_SLICES 63
+
+static uint8_t  ide_drives_present; /* bitmap */
+static uint32_t ide_partition_start[DRIVE_COUNT];
+static uint8_t  ide_slice_count[DRIVE_COUNT];
 
 __sfr __at IDE_REG_ALTSTATUS ide_reg_altstatus;
 __sfr __at IDE_REG_COMMAND   ide_reg_command;
@@ -100,7 +87,7 @@ __sfr __at IDE_REG_LBA_3     ide_reg_lba_3;
 __sfr __at IDE_REG_SEC_COUNT ide_reg_sec_count;
 __sfr __at IDE_REG_STATUS    ide_reg_status;
 
-static void devide_read_data(void *buffer, unsigned char ioport) __naked
+static void devide_read_data(void *buffer, uint8_t ioport) __naked
 {
     buffer; ioport; /* silence compiler warning */
     __asm
@@ -118,7 +105,7 @@ static void devide_read_data(void *buffer, unsigned char ioport) __naked
     __endasm;
 }
 
-static void devide_write_data(void *buffer, unsigned char ioport) __naked
+static void devide_write_data(void *buffer, uint8_t ioport) __naked
 {
     buffer; ioport; /* silence compiler warning */
     __asm
@@ -146,9 +133,9 @@ static void devide_delay(void)
         platform_idle();
 }
 
-static bool devide_wait(unsigned char bits)
+static bool devide_wait(uint8_t bits)
 {
-    unsigned char status;
+    uint8_t status;
     timer_t timeout;
 
     timeout = set_timer_ms(500);
@@ -171,7 +158,7 @@ static bool devide_wait(unsigned char bits)
     };
 }
 
-static bool devide_read_sector(unsigned char *buffer)
+static bool devide_read_sector(uint8_t *buffer)
 {
     if(!devide_wait(IDE_STATUS_DATAREQUEST))
         return false;
@@ -181,7 +168,7 @@ static bool devide_read_sector(unsigned char *buffer)
     return true;
 }
 
-static bool devide_write_sector(unsigned char *buffer)
+static bool devide_write_sector(uint8_t *buffer)
 {
     if(!devide_wait(IDE_STATUS_DATAREQUEST))
         return false;
@@ -196,9 +183,9 @@ static bool devide_write_sector(unsigned char *buffer)
 
 static int devide_transfer(uint8_t minor, bool is_read, uint8_t rawflag)
 {
-    unsigned char *target, *p;
-    unsigned long lba;
-    unsigned char drive;
+    uint8_t *target, *p;
+    uint32_t lba;
+    uint8_t drive;
 
     drive = minor >> 6;
     minor = minor & 0x3F;
@@ -213,7 +200,7 @@ static int devide_transfer(uint8_t minor, bool is_read, uint8_t rawflag)
     if(minor > 0){
         /* minor 1+ are slices located within the partition */
         lba += (ide_partition_start[drive]);
-        lba += ((unsigned long)(minor-1) << 16);
+        lba += ((uint32_t)(minor-1) << SLICE_SIZE_LOG2_SECTORS);
     }
 
 #if 0
@@ -223,7 +210,7 @@ static int devide_transfer(uint8_t minor, bool is_read, uint8_t rawflag)
     ide_reg_lba_0 = lba;
 #else
     /* sdcc sadly unable to figure this out for itself yet */
-    p = (unsigned char *)&lba;
+    p = (uint8_t *)&lba;
     ide_reg_lba_3 = (p[3] & 0x0F) | ((drive == 0) ? 0xE0 : 0xF0); // select drive, start loading LBA
     ide_reg_lba_2 = p[2];
     ide_reg_lba_1 = p[1];
@@ -249,7 +236,7 @@ xferfail:
 
 int devide_open(uint8_t minor, uint16_t flags)
 {
-    unsigned char drive;
+    uint8_t drive;
     flags; /* not used */
 
     drive = minor >> 6;
@@ -274,10 +261,9 @@ int devide_write(uint8_t minor, uint8_t rawflag, uint8_t flag)
     return devide_transfer(minor, false, rawflag);
 }
 
-void devide_init_drive(unsigned char drive)
+void devide_init_drive(uint8_t drive)
 {
-    unsigned char *buffer, i, select;
-    master_boot_record_t *mbr;
+    uint8_t *buffer, select;
 
     switch(drive){
         case 0: select = 0xE0; break;
@@ -313,7 +299,7 @@ void devide_init_drive(unsigned char drive)
     ide_reg_command = IDE_CMD_IDENTIFY;
 
     /* allocate temporary sector buffer memory */
-    buffer = (unsigned char *)tmpbuf();
+    buffer = (uint8_t *)tmpbuf();
 
     if(!devide_read_sector(buffer))
         goto failout;
@@ -340,23 +326,7 @@ void devide_init_drive(unsigned char drive)
     /* if we get this far the drive is apparently present and functioning */
     ide_drives_present |= (1 << drive);
 
-    /* check for MBR table signature */
-    mbr = (master_boot_record_t*)buffer;
-    if(mbr->signature != 0xaa55){
-        kputs("no partition table\n");
-        goto failout;
-    }
-
-    /* look for a fuzix partition (type 0x5A) */
-    for(i=0; i<MBR_ENTRY_COUNT; i++){
-        if(mbr->partition[i].type == 0x5A){
-            ide_partition_start[drive] = mbr->partition[i].lba_first;
-            ide_slice_count[drive] = mbr->partition[i].lba_count >> 16;
-            break;
-        }
-    }
-
-    kprintf("%d slices\n", ide_slice_count[drive]);
+    parse_partition_table(buffer, &ide_partition_start[drive], &ide_slice_count[drive], MAX_SLICES);
 
 failout:
     brelse((bufptr)buffer);
@@ -364,7 +334,7 @@ failout:
 
 void devide_init(void)
 {
-    unsigned char d;
+    uint8_t d;
 
     ide_drives_present = 0;
 
diff --git a/Kernel/dev/mbr.c b/Kernel/dev/mbr.c
new file mode 100644 (file)
index 0000000..9985b64
--- /dev/null
@@ -0,0 +1,46 @@
+#include <kernel.h>
+#include <kdata.h>
+#include <printf.h>
+
+typedef struct {
+    unsigned char status;
+    unsigned char chs_first[3];
+    unsigned char type;
+    unsigned char chs_last[3];
+    unsigned long lba_first;
+    unsigned long lba_count;
+} partition_table_entry_t;
+
+#define MBR_ENTRY_COUNT 4
+typedef struct {
+    unsigned char bootcode[446];
+    partition_table_entry_t partition[MBR_ENTRY_COUNT];
+    unsigned int signature;
+} master_boot_record_t;
+
+void parse_partition_table(void *buffer, uint32_t *first_lba, uint8_t *slice_count, uint8_t max_slices)
+{
+    master_boot_record_t *mbr = (master_boot_record_t*)buffer;
+    uint16_t slices = 0;
+    uint8_t i;
+
+    /* check for MBR table signature */
+    if(mbr->signature != 0xaa55){
+        kputs("no partition table\n");
+        return;
+    }
+
+    /* look for a fuzix partition (type 0x5A) */
+    for(i=0; i<MBR_ENTRY_COUNT; i++){
+        if(mbr->partition[i].type == 0x5A){
+            *first_lba = mbr->partition[i].lba_first;
+            slices = mbr->partition[i].lba_count >> SLICE_SIZE_LOG2_SECTORS;
+            if(slices > max_slices)
+                slices = max_slices;
+            *slice_count = slices;
+            break;
+        }
+    }
+
+    kprintf("%d slices\n", slices);
+}
diff --git a/Kernel/dev/mbr.h b/Kernel/dev/mbr.h
new file mode 100644 (file)
index 0000000..1bb5c01
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef __MBR_DOT_H__
+#define __MBR_DOT_H__
+
+void parse_partition_table(void *buffer, uint32_t *first_lba, uint8_t *slice_count, uint8_t max_slices);
+
+#endif
index ba68911..c03ef7b 100644 (file)
@@ -92,6 +92,8 @@ typedef uint16_t blkno_t;    /* Can have 65536 512-byte blocks in filesystem */
 #define BLKSHIFT       9
 #define BLKMASK                511
 
+#define SLICE_SIZE_LOG2_SECTORS 16    /* 32MB slices */
+
 /* FIXME: if we could split the data and the header we could keep blocks
    outside of our kernel data (as ELKS does) which would be a win, but need
    some more care on copies, block indexes and directory ops */
index 572519b..e6680a0 100644 (file)
@@ -1,5 +1,5 @@
 CSRCS += devices.c main.c devtty.c
-DSRCS = ../dev/devide.c
+DSRCS = ../dev/devide.c ../dev/mbr.c
 
 ASRCS = crt0.s z180.s mark4.s commonmem.s
 
index e56ad57..2b6ce54 100644 (file)
@@ -48,8 +48,9 @@
 
 /* Hardware parameters */
 #define Z180_IO_BASE       0x40
+#define MARK4_IO_BASE      0x80
 
 #define DEVICE_IDE                  /* enable if IDE interface present */
-#define IDE_REG_BASE       0x80
+#define IDE_REG_BASE       MARK4_IO_BASE
 #define IDE_8BIT_ONLY
 #define IDE_REG_CS1_FIRST
index c881d86..6d10b67 100644 (file)
@@ -33,4 +33,5 @@ swap.rel
 devsys.rel
 platform-n8vem-mark4/devtty.rel
 platform-n8vem-mark4/devide.rel
+platform-n8vem-mark4/mbr.rel
 -e
index 702be27..70a1bf3 100644 (file)
@@ -1,5 +1,5 @@
 CSRCS += devices.c main.c devtty.c
-DSRCS = ../dev/devide.c
+DSRCS = ../dev/devide.c ../dev/mbr.c
 
 ASRCS = crt0.s z180.s p112.s commonmem.s
 
index 42407da..73e8928 100644 (file)
@@ -33,4 +33,5 @@ swap.rel
 devsys.rel
 platform-p112/devtty.rel
 platform-p112/devide.rel
+platform-p112/mbr.rel
 -e