#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;
__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
__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
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);
};
}
-static bool devide_read_sector(unsigned char *buffer)
+static bool devide_read_sector(uint8_t *buffer)
{
if(!devide_wait(IDE_STATUS_DATAREQUEST))
return false;
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;
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;
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
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];
int devide_open(uint8_t minor, uint16_t flags)
{
- unsigned char drive;
+ uint8_t drive;
flags; /* not used */
drive = minor >> 6;
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;
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;
/* 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);
void devide_init(void)
{
- unsigned char d;
+ uint8_t d;
ide_drives_present = 0;
--- /dev/null
+#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);
+}